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 android.platform.test.annotations.RequiresDevice; 20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 21 import com.android.tradefed.device.IManagedTestDevice; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.device.TestDeviceState; 24 import com.android.tradefed.invoker.TestInformation; 25 import com.android.tradefed.log.LogUtil.CLog; 26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 27 import com.android.tradefed.testtype.junit4.AfterClassWithInfo; 28 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 29 import com.android.tradefed.testtype.junit4.BeforeClassWithInfo; 30 import com.android.tradefed.util.CommandResult; 31 import com.android.tradefed.util.CommandStatus; 32 import com.android.tradefed.util.IRunUtil; 33 import com.android.tradefed.util.RunUtil; 34 import java.io.File; 35 import java.lang.Thread; 36 import java.util.Arrays; 37 import java.util.HashSet; 38 import java.util.concurrent.ConcurrentHashMap; 39 import java.util.regex.Matcher; 40 import java.util.regex.Pattern; 41 import org.junit.Assert; 42 import org.junit.Assume; 43 import org.junit.Before; 44 import org.junit.Ignore; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.junit.runners.JUnit4; 48 49 /* VTS test to verify userspace fastboot implementation. */ 50 @RequiresDevice 51 @RunWith(DeviceJUnit4ClassRunner.class) 52 public class FastbootVerifyUserspaceTest extends BaseHostJUnit4Test { 53 // Default maximum command run time is set to 90 seconds. 54 private static final long MAX_CMD_RUN_TIME = 90000L; 55 private static String executeShellKernelARM64 = 56 "cat /proc/config.gz | gzip -d | grep CONFIG_ARM64=y"; 57 58 private IRunUtil mRunUtil = RunUtil.getDefault(); 59 private String mFuzzyFastbootPath; 60 61 // IMPORTANT: Multiple instances of this class will be created while sharding 62 // the tests across multiple devices. So it needs to use ConcurrentHashMap to 63 // make these static variables thread-safe. 64 private static ConcurrentHashMap<ITestDevice, Boolean> sDeviceIsGKI10 = 65 new ConcurrentHashMap<>(); 66 67 @BeforeClassWithInfo setUpClass(TestInformation testInfo)68 public static void setUpClass(TestInformation testInfo) throws Exception { 69 // Collects information while adb is available, prior to rebooting into fastbootd. 70 boolean isKernelARM64 = testInfo.getDevice() 71 .executeShellCommand(executeShellKernelARM64) 72 .contains("CONFIG_ARM64"); 73 boolean isGKI10 = false; 74 if (isKernelARM64) { 75 String output = testInfo.getDevice().executeShellCommand("uname -r"); 76 Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)"); 77 Matcher m1 = p.matcher(output); 78 Assert.assertTrue(m1.find()); 79 isGKI10 = (Integer.parseInt(m1.group(1)) == 5 && Integer.parseInt(m1.group(2)) == 4); 80 } 81 82 // Saves the local variable to the static variable for later use, because adb 83 // is not available in the following tests. 84 sDeviceIsGKI10.put(testInfo.getDevice(), isGKI10); 85 86 // Transfers from adb to fastbootd. 87 if (!isGKI10) { 88 testInfo.getDevice().rebootIntoFastbootd(); 89 } 90 } 91 92 @Before setUp()93 public void setUp() throws Exception { 94 Assume.assumeFalse("Skipping test for fastbootd on GKI 1.0", 95 sDeviceIsGKI10.get(getTestInformation().getDevice())); 96 97 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild()); 98 File file = buildHelper.getTestFile("fuzzy_fastboot", getAbi()); 99 Assert.assertNotNull(file); 100 mFuzzyFastbootPath = file.getAbsolutePath(); 101 CLog.d("Locate `fuzzy_fastboot` at %s", mFuzzyFastbootPath); 102 } 103 104 @AfterClassWithInfo tearDownClass(TestInformation testInfo)105 public static void tearDownClass(TestInformation testInfo) throws Exception { 106 if (!sDeviceIsGKI10.get(testInfo.getDevice())) { 107 // Make sure the device state still is FASTBOOTD after the test class have 108 // been executed, because fastboot commands in the tests will disrupt 109 // device state 110 if (!TestDeviceState.FASTBOOTD.equals(testInfo.getDevice().getDeviceState())) { 111 ((IManagedTestDevice) testInfo.getDevice()) 112 .setDeviceState(TestDeviceState.FASTBOOTD); 113 } 114 testInfo.getDevice().reboot(); // reboot from fastbootd to adb. 115 } 116 sDeviceIsGKI10.remove(testInfo.getDevice()); 117 } 118 119 /* Runs fuzzy_fastboot gtest to verify slot operations in fastbootd implementation. */ 120 @Ignore("b/146589281") 121 @Test testFastbootdSlotOperations()122 public void testFastbootdSlotOperations() throws Exception { 123 CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath, 124 String.format( 125 "--serial=%s", getTestInformation().getDevice().getFastbootSerialNumber()), 126 "--gtest_filter=Conformance.Slots:Conformance.SetActive"); 127 Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus()); 128 } 129 130 /* Runs fuzzy_fastboot to verify getvar commands related to logical partitions. */ 131 @Test testLogicalPartitionCommands()132 public void testLogicalPartitionCommands() throws Exception { 133 CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath, 134 String.format( 135 "--serial=%s", getTestInformation().getDevice().getFastbootSerialNumber()), 136 "--gtest_filter=LogicalPartitionCompliance.GetVarIsLogical:LogicalPartitionCompliance.SuperPartition"); 137 Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus()); 138 } 139 140 /* Devices launching with DAP must have a super partition named "super". */ 141 @Test testSuperPartitionName()142 public void testSuperPartitionName() throws Exception { 143 String superPartitionName = 144 getTestInformation().getDevice().getFastbootVariable("super-partition-name"); 145 Assert.assertEquals("super", superPartitionName); 146 } 147 148 /* Runs fuzzy_fastboot to verify the commands to reboot into fastbootd and bootloader. */ 149 @Test testFastbootReboot()150 public void testFastbootReboot() throws Exception { 151 CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath, 152 String.format( 153 "--serial=%s", getTestInformation().getDevice().getFastbootSerialNumber()), 154 "--gtest_filter=LogicalPartitionCompliance.FastbootRebootTest"); 155 Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus()); 156 } 157 158 /* Runs fuzzy_fastboot to verify the commands to reboot into fastbootd and bootloader. */ 159 @Test testLogicalPartitionFlashing()160 public void testLogicalPartitionFlashing() throws Exception { 161 CommandResult result = mRunUtil.runTimedCmd(MAX_CMD_RUN_TIME, mFuzzyFastbootPath, 162 String.format( 163 "--serial=%s", getTestInformation().getDevice().getFastbootSerialNumber()), 164 "--gtest_filter=LogicalPartitionCompliance.CreateResizeDeleteLP"); 165 Assert.assertEquals(CommandStatus.SUCCESS, result.getStatus()); 166 } 167 } 168