1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.cts;
18 
19 import android.service.usb.UsbAccessoryProto;
20 import android.service.usb.UsbDebuggingManagerProto;
21 import android.service.usb.UsbDeviceFilterProto;
22 import android.service.usb.UsbDeviceManagerProto;
23 import android.service.usb.UsbDeviceProto;
24 import android.service.usb.UsbHandlerProto;
25 import android.service.usb.UsbHostManagerProto;
26 import android.service.usb.UsbProfileGroupSettingsManagerProto;
27 import android.service.usb.UsbServiceDumpProto;
28 import android.service.usb.UsbSettingsDevicePreferenceProto;
29 import android.service.usb.UsbSettingsManagerProto;
30 
31 /**
32  * Tests for the UsbService proto dump.
33  */
34 public class UsbIncidentTest extends ProtoDumpTestCase {
testUsbServiceDump()35     public void testUsbServiceDump() throws Exception {
36         final UsbServiceDumpProto dump = getDump(UsbServiceDumpProto.parser(),
37                 "dumpsys usb --proto");
38 
39         verifyUsbServiceDumpProto(dump, PRIVACY_NONE);
40     }
41 
verifyUsbServiceDumpProto(UsbServiceDumpProto dump, final int filterLevel)42     static void verifyUsbServiceDumpProto(UsbServiceDumpProto dump, final int filterLevel) throws Exception {
43         verifyUsbDeviceManagerProto(dump.getDeviceManager(), filterLevel);
44         verifyUsbHostManagerProto(dump.getHostManager(), filterLevel);
45         verifyUsbSettingsManagerProto(dump.getSettingsManager(), filterLevel);
46     }
47 
verifyUsbDeviceManagerProto(UsbDeviceManagerProto udmp, final int filterLevel)48     private static void verifyUsbDeviceManagerProto(UsbDeviceManagerProto udmp, final int filterLevel) throws Exception {
49         verifyUsbHandlerProto(udmp.getHandler(), filterLevel);
50         verifyUsbDebuggingManagerProto(udmp.getDebuggingManager(), filterLevel);
51     }
52 
verifyUsbHandlerProto(UsbHandlerProto uhp, final int filterLevel)53     private static void verifyUsbHandlerProto(UsbHandlerProto uhp, final int filterLevel) throws Exception {
54         for (UsbHandlerProto.Function f : uhp.getCurrentFunctionsList()) {
55             assertTrue(UsbHandlerProto.Function.getDescriptor().getValues().contains(f.getValueDescriptor()));
56         }
57         for (UsbHandlerProto.Function f : uhp.getScreenUnlockedFunctionsList()) {
58             assertTrue(UsbHandlerProto.Function.getDescriptor().getValues().contains(f.getValueDescriptor()));
59         }
60         verifyUsbAccessoryProto(uhp.getCurrentAccessory(), filterLevel);
61     }
62 
verifyUsbAccessoryProto(UsbAccessoryProto uap, final int filterLevel)63     private static void verifyUsbAccessoryProto(UsbAccessoryProto uap, final int filterLevel) throws Exception {
64         if (filterLevel == PRIVACY_AUTO) {
65             assertTrue(uap.getUri().isEmpty());
66             assertTrue(uap.getSerial().isEmpty());
67         }
68     }
69 
verifyUsbDebuggingManagerProto(UsbDebuggingManagerProto udmp, final int filterLevel)70     private static void verifyUsbDebuggingManagerProto(UsbDebuggingManagerProto udmp, final int filterLevel) throws Exception {
71         if (filterLevel < PRIVACY_LOCAL) {
72             assertTrue(udmp.getSystemKeys().isEmpty());
73             assertTrue(udmp.getUserKeys().isEmpty());
74             if (filterLevel < PRIVACY_EXPLICIT) {
75                 assertTrue(udmp.getLastKeyReceived().isEmpty());
76             }
77         }
78     }
79 
verifyUsbHostManagerProto(UsbHostManagerProto uhmp, final int filterLevel)80     private static void verifyUsbHostManagerProto(UsbHostManagerProto uhmp, final int filterLevel) throws Exception {
81         for (UsbDeviceProto udp : uhmp.getDevicesList()) {
82             verifyUsbDeviceProto(udp, filterLevel);
83         }
84     }
85 
verifyUsbDeviceProto(UsbDeviceProto udp, final int filterLevel)86     private static void verifyUsbDeviceProto(UsbDeviceProto udp, final int filterLevel) throws Exception {
87         if (filterLevel == PRIVACY_AUTO) {
88             assertTrue(udp.getSerialNumber().isEmpty());
89         }
90     }
91 
verifyUsbSettingsManagerProto(UsbSettingsManagerProto usmp, final int filterLevel)92     private static void verifyUsbSettingsManagerProto(UsbSettingsManagerProto usmp, final int filterLevel) throws Exception {
93         for (UsbProfileGroupSettingsManagerProto upgsmp : usmp.getProfileGroupSettingsList()) {
94             verifyUsbProfileGroupSettingsManagerProto(upgsmp, filterLevel);
95         }
96     }
97 
verifyUsbProfileGroupSettingsManagerProto(UsbProfileGroupSettingsManagerProto pgsp, final int filterLevel)98     private static void verifyUsbProfileGroupSettingsManagerProto(UsbProfileGroupSettingsManagerProto pgsp, final int filterLevel) throws Exception {
99         for (UsbSettingsDevicePreferenceProto usdp : pgsp.getDevicePreferencesList()) {
100             verifyUsbDeviceFilterProto(usdp.getFilter(), filterLevel);
101         }
102     }
103 
verifyUsbDeviceFilterProto(UsbDeviceFilterProto udfp, final int filterLevel)104     private static void verifyUsbDeviceFilterProto(UsbDeviceFilterProto udfp, final int filterLevel) throws Exception {
105         if (filterLevel == PRIVACY_AUTO) {
106             assertTrue(udfp.getSerialNumber().isEmpty());
107         }
108     }
109 }
110