xref: /aosp_15_r20/system/gsid/tests/DsuGsiToolTest.java (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker /*
2*4e2b41f1SAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker  *
4*4e2b41f1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker  *
8*4e2b41f1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker  *
10*4e2b41f1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker  * limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker  */
16*4e2b41f1SAndroid Build Coastguard Worker 
17*4e2b41f1SAndroid Build Coastguard Worker package com.android.tests.dsu;
18*4e2b41f1SAndroid Build Coastguard Worker 
19*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertFalse;
20*4e2b41f1SAndroid Build Coastguard Worker import static org.junit.Assert.assertTrue;
21*4e2b41f1SAndroid Build Coastguard Worker 
22*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.device.DeviceNotAvailableException;
23*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.log.LogUtil.CLog;
24*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25*4e2b41f1SAndroid Build Coastguard Worker import com.android.tradefed.util.CommandStatus;
26*4e2b41f1SAndroid Build Coastguard Worker 
27*4e2b41f1SAndroid Build Coastguard Worker import org.junit.After;
28*4e2b41f1SAndroid Build Coastguard Worker import org.junit.Before;
29*4e2b41f1SAndroid Build Coastguard Worker import org.junit.Test;
30*4e2b41f1SAndroid Build Coastguard Worker import org.junit.runner.RunWith;
31*4e2b41f1SAndroid Build Coastguard Worker 
32*4e2b41f1SAndroid Build Coastguard Worker @RunWith(DeviceJUnit4ClassRunner.class)
33*4e2b41f1SAndroid Build Coastguard Worker public class DsuGsiToolTest extends DsuTestBase {
34*4e2b41f1SAndroid Build Coastguard Worker     private static final long DSU_MAX_WAIT_SEC = 10 * 60;
35*4e2b41f1SAndroid Build Coastguard Worker 
getDsuInstallCommand(String slotName)36*4e2b41f1SAndroid Build Coastguard Worker     private String getDsuInstallCommand(String slotName) {
37*4e2b41f1SAndroid Build Coastguard Worker         return String.format("am start-activity"
38*4e2b41f1SAndroid Build Coastguard Worker                 + " -n com.android.dynsystem/com.android.dynsystem.VerificationActivity"
39*4e2b41f1SAndroid Build Coastguard Worker                 + " -a android.os.image.action.START_INSTALL"
40*4e2b41f1SAndroid Build Coastguard Worker                 + " --el KEY_USERDATA_SIZE 2147483648"
41*4e2b41f1SAndroid Build Coastguard Worker                 + " --ez KEY_ENABLE_WHEN_COMPLETED true"
42*4e2b41f1SAndroid Build Coastguard Worker                 + " --es KEY_DSU_SLOT %s", slotName);
43*4e2b41f1SAndroid Build Coastguard Worker     }
44*4e2b41f1SAndroid Build Coastguard Worker 
45*4e2b41f1SAndroid Build Coastguard Worker     @Before
setUp()46*4e2b41f1SAndroid Build Coastguard Worker     public void setUp() throws DeviceNotAvailableException {
47*4e2b41f1SAndroid Build Coastguard Worker         if (isDsuRunning()) {
48*4e2b41f1SAndroid Build Coastguard Worker             assertAdbRoot();
49*4e2b41f1SAndroid Build Coastguard Worker             CLog.i("Wipe existing DSU installation");
50*4e2b41f1SAndroid Build Coastguard Worker             assertShellCommand("gsi_tool wipe");
51*4e2b41f1SAndroid Build Coastguard Worker             getDevice().reboot();
52*4e2b41f1SAndroid Build Coastguard Worker             assertDsuNotRunning();
53*4e2b41f1SAndroid Build Coastguard Worker         }
54*4e2b41f1SAndroid Build Coastguard Worker         getDevice().disableAdbRoot();
55*4e2b41f1SAndroid Build Coastguard Worker     }
56*4e2b41f1SAndroid Build Coastguard Worker 
57*4e2b41f1SAndroid Build Coastguard Worker     @After
tearDown()58*4e2b41f1SAndroid Build Coastguard Worker     public void tearDown() throws DeviceNotAvailableException {
59*4e2b41f1SAndroid Build Coastguard Worker         if (isDsuRunning()) {
60*4e2b41f1SAndroid Build Coastguard Worker             getDevice().reboot();
61*4e2b41f1SAndroid Build Coastguard Worker         }
62*4e2b41f1SAndroid Build Coastguard Worker         getDevice().executeShellCommand("gsi_tool wipe");
63*4e2b41f1SAndroid Build Coastguard Worker     }
64*4e2b41f1SAndroid Build Coastguard Worker 
65*4e2b41f1SAndroid Build Coastguard Worker     @Test
testNonLockedDsu()66*4e2b41f1SAndroid Build Coastguard Worker     public void testNonLockedDsu() throws DeviceNotAvailableException {
67*4e2b41f1SAndroid Build Coastguard Worker         final String slotName = "foo";
68*4e2b41f1SAndroid Build Coastguard Worker         assertShellCommand(getDsuInstallCommand(slotName));
69*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("Wait for DSU installation complete and reboot");
70*4e2b41f1SAndroid Build Coastguard Worker         assertTrue(
71*4e2b41f1SAndroid Build Coastguard Worker                 "Timed out waiting for DSU installation complete",
72*4e2b41f1SAndroid Build Coastguard Worker                 getDevice().waitForDeviceNotAvailable(DSU_MAX_WAIT_SEC * 1000));
73*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("DSU installation is complete and device is disconnected");
74*4e2b41f1SAndroid Build Coastguard Worker 
75*4e2b41f1SAndroid Build Coastguard Worker         getDevice().waitForDeviceAvailable();
76*4e2b41f1SAndroid Build Coastguard Worker         assertDsuRunning();
77*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("Successfully booted with DSU");
78*4e2b41f1SAndroid Build Coastguard Worker 
79*4e2b41f1SAndroid Build Coastguard Worker         // These commands should run without any error
80*4e2b41f1SAndroid Build Coastguard Worker         assertShellCommand("gsi_tool enable");
81*4e2b41f1SAndroid Build Coastguard Worker         assertShellCommand("gsi_tool disable");
82*4e2b41f1SAndroid Build Coastguard Worker         assertShellCommand("gsi_tool wipe");
83*4e2b41f1SAndroid Build Coastguard Worker     }
84*4e2b41f1SAndroid Build Coastguard Worker 
85*4e2b41f1SAndroid Build Coastguard Worker     @Test
testLockedDsu()86*4e2b41f1SAndroid Build Coastguard Worker     public void testLockedDsu() throws DeviceNotAvailableException {
87*4e2b41f1SAndroid Build Coastguard Worker         final String slotName = "foo.lock";
88*4e2b41f1SAndroid Build Coastguard Worker         assertShellCommand(getDsuInstallCommand(slotName));
89*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("Wait for DSU installation complete and reboot");
90*4e2b41f1SAndroid Build Coastguard Worker         assertTrue(
91*4e2b41f1SAndroid Build Coastguard Worker                 "Timed out waiting for DSU installation complete",
92*4e2b41f1SAndroid Build Coastguard Worker                 getDevice().waitForDeviceNotAvailable(DSU_MAX_WAIT_SEC * 1000));
93*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("DSU installation is complete and device is disconnected");
94*4e2b41f1SAndroid Build Coastguard Worker 
95*4e2b41f1SAndroid Build Coastguard Worker         getDevice().waitForDeviceAvailable();
96*4e2b41f1SAndroid Build Coastguard Worker         assertDsuRunning();
97*4e2b41f1SAndroid Build Coastguard Worker         CLog.i("Successfully booted with DSU");
98*4e2b41f1SAndroid Build Coastguard Worker 
99*4e2b41f1SAndroid Build Coastguard Worker         // These commands should fail on a locked DSU
100*4e2b41f1SAndroid Build Coastguard Worker         var result = getDevice().executeShellV2Command("gsi_tool enable");
101*4e2b41f1SAndroid Build Coastguard Worker         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
102*4e2b41f1SAndroid Build Coastguard Worker         result = getDevice().executeShellV2Command("gsi_tool disable");
103*4e2b41f1SAndroid Build Coastguard Worker         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
104*4e2b41f1SAndroid Build Coastguard Worker         result = getDevice().executeShellV2Command("gsi_tool wipe");
105*4e2b41f1SAndroid Build Coastguard Worker         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
106*4e2b41f1SAndroid Build Coastguard Worker     }
107*4e2b41f1SAndroid Build Coastguard Worker }
108