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 com.android.tests.fastboot;
18 
19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 import com.android.tradefed.testtype.junit4.AfterClassWithInfo;
25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
26 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
27 import com.android.tradefed.util.CommandResult;
28 import com.android.tradefed.util.CommandStatus;
29 import com.android.tradefed.util.IRunUtil;
30 import com.android.tradefed.util.RunUtil;
31 import java.io.File;
32 import java.lang.Thread;
33 import java.util.Arrays;
34 import java.util.HashSet;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import org.junit.Assert;
38 import org.junit.Assume;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.JUnit4;
44 
45 /* VTS test to verify userspace fastboot implementation. */
46 @RunWith(DeviceJUnit4ClassRunner.class)
47 public class FastbootVerifyUserspaceTest extends BaseHostJUnit4Test {
48     // Default maximum command run time is set to 1 minute.
49     private static final long MAX_CMD_RUN_TIME = 60000L;
50 
51     private static ITestDevice sDevice;
52     private static String executeShellKernelARM64 =
53             "cat /proc/config.gz | gzip -d | grep CONFIG_ARM64=y";
54     private static boolean isGKI10;
55 
56     private IRunUtil mRunUtil = RunUtil.getDefault();
57     private String mFuzzyFastbootPath;
58 
59     @BeforeClassWithInfo
setUpClass(TestInformation testInfo)60     public static void setUpClass(TestInformation testInfo) throws Exception {
61         sDevice = testInfo.getDevice();
62 
63         boolean isKernelARM64 =
64                 sDevice.executeShellCommand(executeShellKernelARM64).contains("CONFIG_ARM64");
65         isGKI10 = false;
66         if (isKernelARM64) {
67             String output = sDevice.executeShellCommand("uname -r");
68             Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)");
69             Matcher m1 = p.matcher(output);
70             Assert.assertTrue(m1.find());
71             isGKI10 = (Integer.parseInt(m1.group(1)) == 5 && Integer.parseInt(m1.group(2)) == 4);
72         }
73 
74         // Transfers from adb to fastbootd.
75         if (!isGKI10) {
76             sDevice.rebootIntoFastbootd();
77         }
78     }
79 
80     @Before
setUp()81     public void setUp() throws Exception {
82         Assume.assumeFalse("Skipping test for fastbootd on GKI 1.0", isGKI10);
83 
84         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild());
85         File file = buildHelper.getTestFile("fuzzy_fastboot", getAbi());
86         Assert.assertNotNull(file);
87         mFuzzyFastbootPath = file.getAbsolutePath();
88         CLog.d("Locate `fuzzy_fastboot` at %s", mFuzzyFastbootPath);
89     }
90 
91     @AfterClassWithInfo
tearDownClass(TestInformation testInfo)92     public static void tearDownClass(TestInformation testInfo) throws Exception {
93         if (!isGKI10) {
94             testInfo.getDevice().reboot(); // back to adb.
95         }
96     }
97 
98     /* Runs fuzzy_fastboot gtest to verify slot operations in fastbootd implementation. */
99     @Ignore("b/146589281")
100     @Test
testFastbootdSlotOperations()101     public void testFastbootdSlotOperations() throws Exception {
102         CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath,
103                 String.format("--serial=%s", sDevice.getFastbootSerialNumber()),
104                 "--gtest_filter=Conformance.Slots:Conformance.SetActive");
105         Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus());
106     }
107 
108     /* Runs fuzzy_fastboot to verify getvar commands related to logical partitions. */
109     @Test
testLogicalPartitionCommands()110     public void testLogicalPartitionCommands() throws Exception {
111         CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath,
112                 String.format("--serial=%s", sDevice.getFastbootSerialNumber()),
113                 "--gtest_filter=LogicalPartitionCompliance.GetVarIsLogical:LogicalPartitionCompliance.SuperPartition");
114         Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus());
115     }
116 
117     /* Devices launching with DAP must have a super partition named "super". */
118     @Test
testSuperPartitionName()119     public void testSuperPartitionName() throws Exception {
120         String superPartitionName = sDevice.getFastbootVariable("super-partition-name");
121         Assert.assertEquals("super", superPartitionName);
122     }
123 
124     /* Runs fuzzy_fastboot to verify the commands to reboot into fastbootd and bootloader. */
125     @Test
testFastbootReboot()126     public void testFastbootReboot() throws Exception {
127         CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath,
128                 String.format("--serial=%s", sDevice.getFastbootSerialNumber()),
129                 "--gtest_filter=LogicalPartitionCompliance.FastbootRebootTest");
130         Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus());
131     }
132 
133     /* Runs fuzzy_fastboot to verify the commands to reboot into fastbootd and bootloader. */
134     @Test
testLogicalPartitionFlashing()135     public void testLogicalPartitionFlashing() throws Exception {
136         CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath,
137                 String.format("--serial=%s", sDevice.getFastbootSerialNumber()),
138                 "--gtest_filter=LogicalPartitionCompliance.CreateResizeDeleteLP");
139         Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus());
140     }
141 }
142