• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.dsu;
18 
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 import com.android.tradefed.util.CommandStatus;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(DeviceJUnit4ClassRunner.class)
33 public class DsuGsiToolTest extends DsuTestBase {
34     private static final long DSU_MAX_WAIT_SEC = 10 * 60;
35 
getDsuInstallCommand(String slotName)36     private String getDsuInstallCommand(String slotName) {
37         return String.format("am start-activity"
38                 + " -n com.android.dynsystem/com.android.dynsystem.VerificationActivity"
39                 + " -a android.os.image.action.START_INSTALL"
40                 + " --el KEY_USERDATA_SIZE 2147483648"
41                 + " --ez KEY_ENABLE_WHEN_COMPLETED true"
42                 + " --es KEY_DSU_SLOT %s", slotName);
43     }
44 
45     @Before
setUp()46     public void setUp() throws DeviceNotAvailableException {
47         if (isDsuRunning()) {
48             assertAdbRoot();
49             CLog.i("Wipe existing DSU installation");
50             assertShellCommand("gsi_tool wipe");
51             getDevice().reboot();
52             assertDsuNotRunning();
53         }
54         getDevice().disableAdbRoot();
55     }
56 
57     @After
tearDown()58     public void tearDown() throws DeviceNotAvailableException {
59         if (isDsuRunning()) {
60             getDevice().reboot();
61         }
62         getDevice().executeShellCommand("gsi_tool wipe");
63     }
64 
65     @Test
testNonLockedDsu()66     public void testNonLockedDsu() throws DeviceNotAvailableException {
67         final String slotName = "foo";
68         assertShellCommand(getDsuInstallCommand(slotName));
69         CLog.i("Wait for DSU installation complete and reboot");
70         assertTrue(
71                 "Timed out waiting for DSU installation complete",
72                 getDevice().waitForDeviceNotAvailable(DSU_MAX_WAIT_SEC * 1000));
73         CLog.i("DSU installation is complete and device is disconnected");
74 
75         getDevice().waitForDeviceAvailable();
76         assertDsuRunning();
77         CLog.i("Successfully booted with DSU");
78 
79         // These commands should run without any error
80         assertShellCommand("gsi_tool enable");
81         assertShellCommand("gsi_tool disable");
82         assertShellCommand("gsi_tool wipe");
83     }
84 
85     @Test
testLockedDsu()86     public void testLockedDsu() throws DeviceNotAvailableException {
87         final String slotName = "foo.lock";
88         assertShellCommand(getDsuInstallCommand(slotName));
89         CLog.i("Wait for DSU installation complete and reboot");
90         assertTrue(
91                 "Timed out waiting for DSU installation complete",
92                 getDevice().waitForDeviceNotAvailable(DSU_MAX_WAIT_SEC * 1000));
93         CLog.i("DSU installation is complete and device is disconnected");
94 
95         getDevice().waitForDeviceAvailable();
96         assertDsuRunning();
97         CLog.i("Successfully booted with DSU");
98 
99         // These commands should fail on a locked DSU
100         var result = getDevice().executeShellV2Command("gsi_tool enable");
101         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
102         result = getDevice().executeShellV2Command("gsi_tool disable");
103         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
104         result = getDevice().executeShellV2Command("gsi_tool wipe");
105         assertFalse(result.getStatus() == CommandStatus.SUCCESS);
106     }
107 }
108