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 android.cts.statsd.apex;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.android.apex.ApexInfo;
22 import com.android.apex.XmlParser;
23 import com.android.compatibility.common.util.ApiLevelUtil;
24 import com.android.tradefed.log.LogUtil;
25 import com.android.tradefed.testtype.DeviceTestCase;
26 
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.util.List;
30 
31 /**
32  * Verify statsd is not in the bootstrap apexes
33  */
34 public class BootstrapApexTests extends DeviceTestCase {
35     private static final String TAG = "Statsd.BootstrapApexTests";
36 
37     // Constants for the paths to apex-info-list.xml
38     // - new location
39     private static final String BOOTSTRAP_APEX_FILE1 = "/bootstrap-apex/apex-info-list.xml";
40     // - legacy location
41     private static final String BOOTSTRAP_APEX_FILE2 = "/apex/.bootstrap-apex-info-list.xml";
42 
43 
sdkLevelAtLeast(int sdkLevel, String codename)44     private boolean sdkLevelAtLeast(int sdkLevel, String codename) throws Exception {
45         return ApiLevelUtil.isAtLeast(getDevice(), sdkLevel)
46                 || ApiLevelUtil.codenameEquals(getDevice(), codename);
47     }
48 
49     // TODO: use InstallUtilsHost#isApexUpdateSupported after migrating to JUnit4
isApexUpdateSupported()50     private final boolean isApexUpdateSupported() throws Exception {
51         return getDevice().getBooleanProperty("ro.apex.updatable", false);
52     }
53 
readBootstrapApexes()54     private List<ApexInfo> readBootstrapApexes() throws Exception {
55         File file = getDevice().pullFile(BOOTSTRAP_APEX_FILE1);
56         if (file == null) {
57             file = getDevice().pullFile(BOOTSTRAP_APEX_FILE2);
58         }
59         try (FileInputStream stream = new FileInputStream(file)) {
60             return XmlParser.readApexInfoList(stream).getApexInfo();
61         }
62     }
63 
testStatsdNotPresent()64     public void testStatsdNotPresent() throws Exception {
65         if (!sdkLevelAtLeast(31, "S")) {
66             return;
67         }
68         if (!isApexUpdateSupported()) {
69             return;
70         }
71 
72         List<ApexInfo> apexInfoList = readBootstrapApexes();
73         LogUtil.CLog.d(TAG + " Received " + apexInfoList.size() + " apexes in bootstrap apexes");
74         assertThat(apexInfoList.size()).isGreaterThan(0);
75         for (ApexInfo apexInfo : apexInfoList) {
76             LogUtil.CLog.d(TAG + " APEX name is " + apexInfo.getModuleName());
77             assertThat(apexInfo.getModuleName()).isNotEqualTo("com.android.os.statsd");
78         }
79     }
80 }
81