Debian Codegraph @

tests/UsbTests/src/com/android/server/usb/UsbManagerNoPermTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 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.usb;
18
19 import static org.junit.Assert.assertEquals;
20
21 import android.content.Context;
22 import android.hardware.usb.UsbManager;
23
24 import androidx.test.InstrumentationRegistry;
25 import androidx.test.filters.SmallTest;
26 import androidx.test.runner.AndroidJUnit4;
27
28 import com.android.server.usblib.UsbManagerTestLib;
29
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
34 /**
35 * Unit tests for {@link android.hardware.usb.UsbManager}.
36 * Note: NOT claimed MANAGE_USB permission in Manifest
37 */
38 @SmallTest
39 @RunWith(AndroidJUnit4.class)
40 public class UsbManagerNoPermTest {
41 private Context mContext;
42
43 private final UsbManagerTestLib mUsbManagerTestLib =
44 new UsbManagerTestLib(mContext = InstrumentationRegistry.getContext());
45
46 /**
47 * Verify SecurityException resulting from required permissions missing
48 * Go through System Server
49 */
50 @Test(expected = SecurityException.class)
51 public void testUsbApi_GetCurrentFunctionsSys_OnSecurityException() throws Exception {
52 mUsbManagerTestLib.testGetCurrentFunctionsSysEx();
53 }
54
55 /**
56 * Verify SecurityException resulting from required permissions missing
57 * Go through System Server
58 */
59 @Test(expected = SecurityException.class)
60 public void testUsbApi_SetCurrentFunctionsSys_OnSecurityException() throws Exception {
61 mUsbManagerTestLib.testSetCurrentFunctionsSysEx(UsbManager.FUNCTION_NONE);
62 }
63
64 /**
65 * Verify SecurityException resulting from required permissions missing
66 * Go through Direct API, will not be denied by @RequiresPermission annotation
67 */
68 @Test(expected = SecurityException.class)
69 @Ignore
70 public void testUsbApi_GetCurrentFunctions_OnSecurityException() throws Exception {
71 mUsbManagerTestLib.testGetCurrentFunctionsEx();
72 }
73
74 /**
75 * Verify SecurityException resulting from required permissions missing
76 * Go through Direct API, will not be denied by @RequiresPermission annotation
77 */
78 @Test(expected = SecurityException.class)
79 @Ignore
80 public void testUsbApi_SetCurrentFunctions_OnSecurityException() throws Exception {
81 mUsbManagerTestLib.testSetCurrentFunctionsEx(UsbManager.FUNCTION_NONE);
82 }
83
84 public void assertSettableFunctions(boolean settable, long functions) {
85 assertEquals(
86 "areSettableFunctions(" + UsbManager.usbFunctionsToString(functions) + "):",
87 settable, UsbManager.areSettableFunctions(functions));
88 }
89
90 /**
91 * Tests the behaviour of the static areSettableFunctions method. This method performs no IPCs
92 * and requires no permissions.
93 */
94 @Test
95 public void testUsbManager_AreSettableFunctions() {
96 // NONE is settable.
97 assertSettableFunctions(true, UsbManager.FUNCTION_NONE);
98
99 // MTP, PTP, RNDIS, MIDI, NCM are all settable by themselves.
100 assertSettableFunctions(true, UsbManager.FUNCTION_MTP);
101 assertSettableFunctions(true, UsbManager.FUNCTION_PTP);
102 assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS);
103 assertSettableFunctions(true, UsbManager.FUNCTION_MIDI);
104 assertSettableFunctions(true, UsbManager.FUNCTION_NCM);
105
106 // Setting two functions at the same time is not allowed...
107 assertSettableFunctions(false, UsbManager.FUNCTION_MTP | UsbManager.FUNCTION_PTP);
108 assertSettableFunctions(false, UsbManager.FUNCTION_PTP | UsbManager.FUNCTION_RNDIS);
109 assertSettableFunctions(false, UsbManager.FUNCTION_MIDI | UsbManager.FUNCTION_NCM);
110
111 // ... except in the special case of RNDIS and NCM.
112 assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS | UsbManager.FUNCTION_NCM);
113 }
114 }