• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.sdksandbox.host;
18 
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
23 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
24 import com.android.tradefed.util.CommandResult;
25 import com.android.tradefed.util.CommandStatus;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.HashSet;
34 
35 @Ignore("b/242829515,b/242829475,b/242830221,b/242829965")
36 @RunWith(DeviceJUnit4ClassRunner.class)
37 public final class SdkSandboxShellHostTest extends BaseHostJUnit4Test {
38 
39     private static final String DEBUGGABLE_APP_PACKAGE = "com.android.sdksandbox.debuggable";
40     private static final String DEBUGGABLE_APP_ACTIVITY = "SdkSandboxTestDebuggableActivity";
41 
42     private static final String APP_PACKAGE = "com.android.sdksandbox.app";
43     private static final String APP_ACTIVITY = "SdkSandboxTestActivity";
44 
45     private static final String DEBUGGABLE_APP_SANDBOX_NAME = DEBUGGABLE_APP_PACKAGE
46       + "_sdk_sandbox";
47     private static final String APP_SANDBOX_NAME = APP_PACKAGE + "_sdk_sandbox";
48 
49     private HashSet<Integer> mOriginalUsers;
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         assertThat(getBuild()).isNotNull();
54         assertThat(getDevice()).isNotNull();
55 
56         // Ensure neither app is currently running
57         for (String pkg : new String[]{APP_PACKAGE, DEBUGGABLE_APP_PACKAGE}) {
58             clearProcess(pkg);
59         }
60 
61         mOriginalUsers = new HashSet<>(getDevice().listUsers());
62 
63         assertThat(getDevice().enableAdbRoot()).isTrue();
64     }
65 
66     @After
tearDown()67     public void tearDown() throws Exception {
68         for (Integer userId : getDevice().listUsers()) {
69             if (!mOriginalUsers.contains(userId)) {
70                 getDevice().removeUser(userId);
71             }
72         }
73         getDevice().disableAdbRoot();
74     }
75 
76     @Test
testStartAndStopSdkSandboxSucceedsForDebuggableApp()77     public void testStartAndStopSdkSandboxSucceedsForDebuggableApp() throws Exception {
78         CommandResult output = getDevice().executeShellV2Command(
79                 String.format("cmd sdk_sandbox start %s", DEBUGGABLE_APP_PACKAGE));
80         assertThat(output.getStderr()).isEmpty();
81         assertThat(output.getStatus()).isEqualTo(CommandStatus.SUCCESS);
82 
83         String processDump = getDevice().executeShellCommand("ps -A");
84         assertThat(processDump).contains(DEBUGGABLE_APP_SANDBOX_NAME);
85 
86         output = getDevice().executeShellV2Command(
87                 String.format("cmd sdk_sandbox stop %s", DEBUGGABLE_APP_PACKAGE));
88         assertThat(output.getStderr()).isEmpty();
89         assertThat(output.getStatus()).isEqualTo(CommandStatus.SUCCESS);
90 
91         processDump = getDevice().executeShellCommand("ps -A");
92         assertThat(processDump).doesNotContain(DEBUGGABLE_APP_SANDBOX_NAME);
93     }
94 
95     @Test
testStartSdkSandboxFailsForNonDebuggableApp()96     public void testStartSdkSandboxFailsForNonDebuggableApp() throws Exception {
97         CommandResult output = getDevice().executeShellV2Command(
98                 String.format("cmd sdk_sandbox start %s", APP_PACKAGE));
99         assertThat(output.getStatus()).isEqualTo(CommandStatus.FAILED);
100 
101         String processDump = getDevice().executeShellCommand("ps -A");
102         assertThat(processDump).doesNotContain(APP_SANDBOX_NAME);
103     }
104 
105     @Test
testStartSdkSandboxFailsForIncorrectUser()106     public void testStartSdkSandboxFailsForIncorrectUser() throws Exception {
107         int otherUserId = getDevice().createUser("TestUser_" + System.currentTimeMillis());
108         CommandResult output = getDevice().executeShellV2Command(
109                 String.format("cmd sdk_sandbox start --user %s %s",
110                         otherUserId, DEBUGGABLE_APP_PACKAGE));
111         assertThat(output.getStatus()).isEqualTo(CommandStatus.FAILED);
112 
113         String processDump = getDevice().executeShellCommand("ps -A");
114         assertThat(processDump).doesNotContain(DEBUGGABLE_APP_SANDBOX_NAME);
115     }
116 
117     @Test
testStopSdkSandboxSucceedsForRunningDebuggableApp()118     public void testStopSdkSandboxSucceedsForRunningDebuggableApp() throws Exception {
119         startActivity(DEBUGGABLE_APP_PACKAGE, DEBUGGABLE_APP_ACTIVITY);
120 
121         CommandResult output = getDevice().executeShellV2Command(
122                 String.format("cmd sdk_sandbox stop %s", DEBUGGABLE_APP_PACKAGE));
123         assertThat(output.getStderr()).isEmpty();
124         assertThat(output.getStatus()).isEqualTo(CommandStatus.SUCCESS);
125 
126         String processDump = getDevice().executeShellCommand("ps -A");
127         assertThat(processDump).doesNotContain(DEBUGGABLE_APP_SANDBOX_NAME);
128     }
129 
130     @Test
testStartSdkSandboxFailsForInvalidPackage()131     public void testStartSdkSandboxFailsForInvalidPackage() throws Exception {
132         String invalidPackage = "com.android.sdksandbox.nonexistent";
133         CommandResult output = getDevice().executeShellV2Command(
134                 String.format("cmd sdk_sandbox start %s", invalidPackage));
135         assertThat(output.getStatus()).isEqualTo(CommandStatus.FAILED);
136     }
137 
138     @Test
testStopSdkSandboxFailsForNonDebuggableApp()139     public void testStopSdkSandboxFailsForNonDebuggableApp() throws Exception {
140         startActivity(APP_PACKAGE, APP_ACTIVITY);
141 
142         CommandResult output = getDevice().executeShellV2Command(
143                 String.format("cmd sdk_sandbox stop %s", APP_PACKAGE));
144         assertThat(output.getStatus()).isEqualTo(CommandStatus.FAILED);
145 
146         String processDump = getDevice().executeShellCommand("ps -A");
147         assertThat(processDump).contains(APP_SANDBOX_NAME);
148     }
149 
150     @Test
testStopSdkSandboxFailsForIncorrectUser()151     public void testStopSdkSandboxFailsForIncorrectUser() throws Exception {
152         startActivity(DEBUGGABLE_APP_PACKAGE, DEBUGGABLE_APP_ACTIVITY);
153 
154         int otherUserId = getDevice().createUser("TestUser_" + System.currentTimeMillis());
155         CommandResult output = getDevice().executeShellV2Command(String.format(
156                 "cmd sdk_sandbox stop --user %s %s", otherUserId, DEBUGGABLE_APP_PACKAGE));
157         assertThat(output.getStatus()).isEqualTo(CommandStatus.FAILED);
158 
159         String processDump = getDevice().executeShellCommand("ps -A");
160         assertThat(processDump).contains(DEBUGGABLE_APP_SANDBOX_NAME);
161     }
162 
clearProcess(String pkg)163     private void clearProcess(String pkg) throws Exception {
164         getDevice().executeShellCommand(String.format("pm clear %s", pkg));
165     }
166 
startActivity(String pkg, String activity)167     private void startActivity(String pkg, String activity) throws Exception {
168         getDevice().executeShellCommand(String.format("am start -W -n %s/.%s", pkg, activity));
169     }
170 }
171