• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.cts.install.lib.host.InstallUtilsHost;
24 
25 import com.android.internal.util.test.SystemPreparer;
26 import com.android.modules.utils.build.testing.DeviceSdkLevel;
27 import com.android.tradefed.device.DeviceNotAvailableException;
28 import com.android.tradefed.device.ITestDevice;
29 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
30 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.RuleChain;
38 import org.junit.rules.TemporaryFolder;
39 import org.junit.runner.RunWith;
40 
41 import java.util.Objects;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
44 
45 @RunWith(DeviceJUnit4ClassRunner.class)
46 public class ApexSystemServicesTestCases extends BaseHostJUnit4Test {
47 
48     private static final int REBOOT_TIMEOUT = 1 * 60 * 1000;
49 
50     private final InstallUtilsHost mHostUtils = new InstallUtilsHost(this);
51     private final TemporaryFolder mTemporaryFolder = new TemporaryFolder();
52     private final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder, this::getDevice);
53 
54     @Rule
55     public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer);
56 
57     private DeviceSdkLevel mDeviceSdkLevel;
58     private ITestDevice mDevice;
59 
60     @Before
setup()61     public void setup() throws Exception {
62         mDevice = getDevice();
63         mDeviceSdkLevel = new DeviceSdkLevel(getDevice());
64 
65         assumeTrue(mDeviceSdkLevel.isDeviceAtLeastT());
66 
67         assertThat(mDevice.enableAdbRoot()).isTrue();
68         assertThat(mHostUtils.isApexUpdateSupported()).isTrue();
69     }
70 
71     @After
tearDown()72     public void tearDown() throws Exception {
73         mDevice.disableAdbRoot();
74     }
75 
76     @Test
testNoApexSystemServiceStartsWithoutApex()77     public void testNoApexSystemServiceStartsWithoutApex() throws Exception {
78         mPreparer.reboot();
79 
80         assertThat(getFakeApexSystemServiceLogcat())
81                 .doesNotContain("FakeApexSystemService onStart");
82     }
83 
84     @Ignore
85     @Test
testApexSystemServiceStarts()86     public void testApexSystemServiceStarts() throws Exception {
87         // Pre-install the apex
88         String apex = "test_com.android.server.apex";
89         mPreparer.pushResourceFile(apex, "/system/apex/" + apex);
90         // Reboot activates the apex
91         mPreparer.reboot();
92 
93         mDevice.waitForBootComplete(REBOOT_TIMEOUT);
94 
95         assertThat(getFakeApexSystemServiceLogcat())
96                 .contains("FakeApexSystemService onStart");
97     }
98 
99     @Ignore
100     @Test
testInitOrder()101     public void testInitOrder() throws Exception {
102         // Pre-install the apex
103         String apex = "test_com.android.server.apex";
104         mPreparer.pushResourceFile(apex, "/system/apex/" + apex);
105         // Reboot activates the apex
106         mPreparer.reboot();
107 
108         mDevice.waitForBootComplete(REBOOT_TIMEOUT);
109 
110         assertThat(getFakeApexSystemServiceLogcat().lines()
111                 .map(ApexSystemServicesTestCases::getDebugMessage)
112                 .filter(Objects::nonNull)
113                 .collect(Collectors.toList()))
114                 .containsExactly(
115                         // Second service has a higher initOrder and must be started first
116                         "FakeApexSystemService2 onStart",
117                         "FakeApexSystemService onStart"
118                 )
119                 .inOrder();
120     }
121 
getFakeApexSystemServiceLogcat()122     private String getFakeApexSystemServiceLogcat() throws DeviceNotAvailableException {
123         return mDevice.executeAdbCommand("logcat", "-v", "brief", "-d", "FakeApexSystemService:D",
124                 "*:S");
125     }
126 
127     private static final Pattern DEBUG_MESSAGE =
128             Pattern.compile("(FakeApexSystemService[0-9]* onStart)");
129 
getDebugMessage(String logcatLine)130     private static String getDebugMessage(String logcatLine) {
131         return DEBUG_MESSAGE.matcher(logcatLine)
132                 .results()
133                 .map(m -> m.group(1))
134                 .findFirst()
135                 .orElse(null);
136     }
137 
138 }
139