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