1 /*
2  * Copyright (C) 2015 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.cts.cpptools;
18 
19 import com.android.tradefed.testtype.DeviceTestCase;
20 
21 import java.util.regex.Pattern;
22 import java.util.regex.Matcher;
23 
24 /**
25  * Test to check the host can execute commands via "adb shell run-as".
26  */
27 public class RunAsHostTest extends DeviceTestCase {
28 
29     /**
30      * Tests that host can execute shell commands as a debuggable app via adb.
31      *
32      * @throws Exception
33      */
testRunAs()34     public void testRunAs() throws Exception {
35         String runAsResult = getDevice().executeShellCommand("run-as android.cpptools.app id -u");
36         assertNotNull("adb shell command failed", runAsResult);
37         runAsResult = runAsResult.trim();
38         Matcher appIdMatcher = Pattern.compile("^([0-9]+)$").matcher(runAsResult);
39         assertTrue("unexpected result returned by adb shell command: \"" + runAsResult + "\"",
40                    appIdMatcher.matches());
41         String appIdString = appIdMatcher.group(1);
42         assertTrue("invalid app id " + appIdString, Integer.parseInt(appIdString) > 10000);
43     }
44 }
45