xref: /aosp_15_r20/cts/hostsidetests/os/src/android/os/cts/OsHostTests.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 android.os.cts;
18 
19 import com.android.tradefed.util.RunUtil;
20 import android.platform.test.annotations.AppModeFull;
21 
22 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
23 import com.android.tradefed.build.IBuildInfo;
24 import com.android.tradefed.device.CollectingOutputReceiver;
25 import com.android.tradefed.device.ITestDevice;
26 import com.android.tradefed.result.InputStreamSource;
27 import com.android.tradefed.testtype.DeviceTestCase;
28 import com.android.tradefed.testtype.IAbi;
29 import com.android.tradefed.testtype.IAbiReceiver;
30 import com.android.tradefed.testtype.IBuildReceiver;
31 
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.FileNotFoundException;
35 import java.io.InputStreamReader;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 
39 public class OsHostTests extends DeviceTestCase implements IBuildReceiver, IAbiReceiver {
40     private static final String TEST_APP_PACKAGE = "android.os.app";
41 
42     private static final String TEST_NON_EXPORTED_ACTIVITY_CLASS = "TestNonExported";
43     private static final String START_NON_EXPORTED_ACTIVITY_COMMAND = String.format(
44             "am start -n %s/%s.%s",
45             TEST_APP_PACKAGE, TEST_APP_PACKAGE, TEST_NON_EXPORTED_ACTIVITY_CLASS);
46 
47     private static final String TEST_FG_SERVICE_CLASS = "TestFgService";
48     private static final String START_FG_SERVICE_COMMAND = String.format(
49             "am start-foreground-service -n %s/%s.%s",
50             TEST_APP_PACKAGE, TEST_APP_PACKAGE, TEST_FG_SERVICE_CLASS);
51     private static final String FILTER_FG_SERVICE_REGEXP =
52             "TestFgService starting foreground: pid=([0-9]*)";
53 
54     /**
55      * A reference to the device under test.
56      */
57     private ITestDevice mDevice;
58     private IAbi mAbi;
59     private IBuildInfo mCtsBuild;
60 
61     @Override
setAbi(IAbi abi)62     public void setAbi(IAbi abi) {
63         mAbi = abi;
64     }
65 
66     @Override
setBuild(IBuildInfo buildInfo)67     public void setBuild(IBuildInfo buildInfo) {
68         mCtsBuild = buildInfo;
69     }
70 
71     @Override
setUp()72     protected void setUp() throws Exception {
73         super.setUp();
74 
75         // Get the device, this gives a handle to run commands and install APKs.
76         mDevice = getDevice();
77     }
78 
79     /**
80      * Test whether non-exported activities are properly not launchable.
81      *
82      * @throws Exception
83      */
84     @AppModeFull(reason = "Error message is different for instant app (Activity does not exist)")
testNonExportedActivities()85     public void testNonExportedActivities() throws Exception {
86         // Run as unroot
87         boolean wasRoot = mDevice.isAdbRoot();
88         try {
89             mDevice.disableAdbRoot();
90             // Attempt to launch the non-exported activity in the test app
91             CollectingOutputReceiver outputReceiver = new CollectingOutputReceiver();
92             mDevice.executeShellCommand(START_NON_EXPORTED_ACTIVITY_COMMAND, outputReceiver);
93             final String output = outputReceiver.getOutput();
94 
95             assertTrue(output.contains("Permission Denial") && output.contains(" not exported"));
96         } finally {
97             // Restore back to original root state
98             if (wasRoot) {
99                 mDevice.enableAdbRoot();
100             }
101         }
102     }
103 
104     /**
105      * Test behavior of malformed Notifications w.r.t. foreground services
106      * @throws Exception
107      */
108     @AppModeFull(reason = "Instant apps may not start foreground services")
testForegroundServiceBadNotification()109     public void testForegroundServiceBadNotification() throws Exception {
110         final Pattern pattern = Pattern.compile(FILTER_FG_SERVICE_REGEXP);
111 
112         mDevice.clearLogcat();
113         mDevice.executeShellCommand(START_FG_SERVICE_COMMAND);
114 
115         int maxRetries = 5;
116         String pid = null;
117         while (pid == null && maxRetries > 0) {
118             RunUtil.getDefault().sleep(1000);
119             maxRetries--;
120 
121             try (InputStreamSource logSource = mDevice.getLogcat()) {
122                 InputStreamReader streamReader =
123                         new InputStreamReader(logSource.createInputStream());
124                 BufferedReader logReader = new BufferedReader(streamReader);
125 
126                 String line;
127                 while ((line = logReader.readLine()) != null) {
128                     Matcher matcher = pattern.matcher(line);
129                     if (matcher.find()) {
130                         pid = matcher.group(1);
131                         break;
132                     }
133                 }
134             }
135         }
136         assertTrue("Didn't find test service statement in logcat", pid != null);
137 
138         final String procStr = "/proc/" + pid;
139         final String lsOut = mDevice.executeShellCommand("ls -d " + procStr).trim();
140         assertTrue("Looking for nonexistence of service process " + pid,
141                 lsOut.contains("No such file"));
142     }
143 }
144