• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.usbtest;
18 
19 import android.content.Context;
20 import android.hardware.usb.UsbManager;
21 import android.hardware.usb.flags.Flags;
22 import android.platform.test.annotations.EnableFlags;
23 import android.platform.test.flag.junit.CheckFlagsRule;
24 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.filters.SmallTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.server.usblib.UsbManagerTestLib;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  * Unit tests for {@link android.hardware.usb.UsbManager}.
38  * Note: MUST claimed MANAGE_USB permission in Manifest
39  */
40 @SmallTest
41 @RunWith(AndroidJUnit4.class)
42 public class UsbManagerApiTest {
43     private Context mContext;
44 
45     private final UsbManagerTestLib mUsbManagerTestLib =
46             new UsbManagerTestLib(mContext = InstrumentationRegistry.getContext());
47 
48     @Rule
49     public final CheckFlagsRule mCheckFlagsRule =
50             DeviceFlagsValueProvider.createCheckFlagsRule();
51     /**
52      * Verify NO SecurityException
53      * Go through System Server
54      */
55     @Test
testUsbApi_GetCurrentFunctionsSys_shouldNoSecurityException()56     public void testUsbApi_GetCurrentFunctionsSys_shouldNoSecurityException() throws Exception {
57         mUsbManagerTestLib.testGetCurrentFunctionsSysEx();
58     }
59 
60     /**
61      * Verify NO SecurityException
62      * Go through System Server
63      */
64     @Test
testUsbApi_SetCurrentFunctionsSys_shouldNoSecurityException()65     public void testUsbApi_SetCurrentFunctionsSys_shouldNoSecurityException() throws Exception {
66         mUsbManagerTestLib.testSetCurrentFunctionsSysEx(UsbManager.FUNCTION_NONE);
67     }
68 
69     /**
70      * Verify NO SecurityException
71      * Go through Direct API, will not be denied by @RequiresPermission annotation
72      */
73     @Test
testUsbApi_GetCurrentFunctions_shouldNoSecurityException()74     public void testUsbApi_GetCurrentFunctions_shouldNoSecurityException() throws Exception {
75         mUsbManagerTestLib.testGetCurrentFunctionsEx();
76     }
77 
78     /**
79      * Verify NO SecurityException
80      * Go through Direct API, will not be denied by @RequiresPermission annotation
81      */
82     @Test
testUsbApi_SetCurrentFunctions_shouldNoSecurityException()83     public void testUsbApi_SetCurrentFunctions_shouldNoSecurityException() throws Exception {
84         mUsbManagerTestLib.testSetCurrentFunctionsEx(UsbManager.FUNCTION_NONE);
85     }
86 
87     /**
88      * Verify API path from UsbManager to UsbService
89      */
90     @Test
testUsbApi_GetCurrentFunctions_shouldMatched()91     public void testUsbApi_GetCurrentFunctions_shouldMatched() {
92         mUsbManagerTestLib.testGetCurrentFunctions_shouldMatched();
93     }
94 
95     /**
96      * Verify API path from UsbManager to UsbService
97      */
98     @Test
testUsbApi_SetCurrentFunctions_shouldMatched()99     public void testUsbApi_SetCurrentFunctions_shouldMatched() {
100         mUsbManagerTestLib.testSetCurrentFunctions_shouldMatched();
101     }
102 
103     @Test
104     @EnableFlags(Flags.FLAG_ENABLE_ACCESSORY_STREAM_API)
testUsbApi_closesParcelFileDescriptorAfterAllStreamsClosed()105     public void testUsbApi_closesParcelFileDescriptorAfterAllStreamsClosed() {
106         mUsbManagerTestLib.testParcelFileDescriptorClosedWhenAllOpenStreamsAreClosed();
107     }
108 
109     @Test
110     @EnableFlags(Flags.FLAG_ENABLE_ACCESSORY_STREAM_API)
testUsbApi_callingOpenAccessoryInputStreamTwiceThrowsException()111     public void testUsbApi_callingOpenAccessoryInputStreamTwiceThrowsException() {
112         mUsbManagerTestLib.testOnlyOneOpenInputStreamAllowed();
113     }
114 
115     @Test
116     @EnableFlags(Flags.FLAG_ENABLE_ACCESSORY_STREAM_API)
testUsbApi_callingOpenAccessoryOutputStreamTwiceThrowsException()117     public void testUsbApi_callingOpenAccessoryOutputStreamTwiceThrowsException() {
118         mUsbManagerTestLib.testOnlyOneOpenOutputStreamAllowed();
119     }
120 
121 }
122