1 /*
2  * Copyright (C) 2020 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 android.adb.cts;
18 
19 import com.android.tradefed.testtype.DeviceTestCase;
20 import com.android.tradefed.testtype.IDeviceTest;
21 
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 
29 public class AdbHostTest extends DeviceTestCase implements IDeviceTest {
30     // TODO: there are too many copies of this in CTS!
copyResourceToTempFile(String resName)31     public static File copyResourceToTempFile(String resName) throws IOException {
32         InputStream is = AdbHostTest.class.getResourceAsStream(resName);
33         File tf = File.createTempFile("AdbHostTest", ".tmp");
34         FileOutputStream os = new FileOutputStream(tf);
35         byte[] buf = new byte[8192];
36         int length;
37         while ((length = is.read(buf)) != -1) {
38             os.write(buf, 0, length);
39         }
40         os.flush();
41         os.close();
42         tf.deleteOnExit();
43         return tf;
44     }
45 
46     // *** READ THIS IF YOUR DEVICE IS FAILING THIS TEST ***
47     // This test checks to see that Microsoft OS Descriptors are enabled on the adb USB interface,
48     // so that users don't have to install a specific driver for their device in order to use adb
49     // with it (and OEMs don't have to sign and host that driver).
50     //
51     // adbd exports Microsoft OS descriptors when setting up its endpoint descriptors, but the
52     // kernel requires that the OS descriptor functionality be enabled separately by writing "1" to
53     // the gadget's configfs file at /config/usb_gadget/g<N>/os_desc/use (where <N> is probably 1).
54     // To pass this test, you need to modify your USB HAL implementation to do this.
55     //
56     // See https://android.googlesource.com/platform/hardware/google/pixel/+/5c7c6d5edcbe04454eb9a40f947ac0a3045f64eb
57     // for the patch that enabled this on Pixel devices.
testHasMsOsDescriptors()58     public void testHasMsOsDescriptors() throws Exception {
59         File check_ms_os_desc = copyResourceToTempFile("/check_ms_os_desc");
60         check_ms_os_desc.setExecutable(true);
61 
62         String serial = getDevice().getSerialNumber();
63         if (serial.startsWith("emulator-")) {
64             return;
65         }
66 
67         if (getDevice().isAdbTcp()) { // adb over WiFi, no point checking it
68             return;
69         }
70 
71         ProcessBuilder pb = new ProcessBuilder(check_ms_os_desc.getAbsolutePath());
72         pb.environment().put("ANDROID_SERIAL", serial);
73         pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
74         pb.redirectErrorStream(true);
75         Process p = pb.start();
76         int result = p.waitFor();
77         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
78         String line;
79         StringBuilder output = new StringBuilder();
80         while ((line = br.readLine()) != null) {
81             output.append(line + "\n");
82         }
83         assertTrue("check_ms_os_desc failed:\n" + output, result == 0);
84     }
85 }
86