1 /*
2  * Copyright (C) 2018 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 package android.cts.statsd.uidmap;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.cts.statsd.atom.DeviceAtomTestCase;
21 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
22 import com.android.internal.os.StatsdConfigProto;
23 import com.android.os.AtomsProto;
24 import com.android.os.StatsLog.ConfigMetricsReportList;
25 import com.android.os.StatsLog.ConfigMetricsReport;
26 import com.android.os.StatsLog.UidMapping;
27 import com.android.os.StatsLog.UidMapping.PackageInfoSnapshot;
28 import com.android.tradefed.log.LogUtil;
29 
30 import java.util.List;
31 
32 public class UidMapTests extends DeviceAtomTestCase {
33 
34     // Tests that every report has at least one snapshot.
testUidSnapshotIncluded()35     public void testUidSnapshotIncluded() throws Exception {
36         // There should be at least the test app installed during the test setup.
37         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
38 
39         ConfigMetricsReportList reports = getReportList();
40         assertThat(reports.getReportsCount()).isGreaterThan(0);
41 
42         for (ConfigMetricsReport report : reports.getReportsList()) {
43             UidMapping uidmap = report.getUidMap();
44             assertThat(uidmap.getSnapshotsCount()).isGreaterThan(0);
45             for (PackageInfoSnapshot snapshot : uidmap.getSnapshotsList()) {
46                 // There must be at least one element in each snapshot (at least one package is
47                 // installed).
48                 assertThat(snapshot.getPackageInfoCount()).isGreaterThan(0);
49             }
50         }
51     }
52 
hasMatchingChange(UidMapping uidmap, int uid, boolean expectDeletion)53     private boolean hasMatchingChange(UidMapping uidmap, int uid, boolean expectDeletion) {
54         LogUtil.CLog.d("The uid we are looking for is " + uid);
55         for (UidMapping.Change change : uidmap.getChangesList()) {
56             if (change.getAppHash() == DEVICE_SIDE_TEST_PKG_HASH && change.getUid() == uid) {
57                 if (change.getDeletion() == expectDeletion) {
58                     return true;
59                 }
60             }
61         }
62         return false;
63     }
64 
65     // Tests that delta event included during app installation.
testChangeFromInstallation()66     public void testChangeFromInstallation() throws Exception {
67         getDevice().uninstallPackage(DEVICE_SIDE_TEST_PACKAGE);
68         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
69         // Install the package after the config is sent to statsd. The uid map is not guaranteed to
70         // be updated if there's no config in statsd.
71         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
72         final String result = getDevice().installPackage(
73                 buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), false, true);
74 
75         Thread.sleep(WAIT_TIME_LONG);
76 
77         ConfigMetricsReportList reports = getReportList();
78         assertThat(reports.getReportsCount()).isGreaterThan(0);
79 
80         boolean found = false;
81         int uid = getUid();
82         for (ConfigMetricsReport report : reports.getReportsList()) {
83             LogUtil.CLog.d("Got the following report: \n" + report.toString());
84             if (hasMatchingChange(report.getUidMap(), uid, false)) {
85                 found = true;
86             }
87         }
88         assertThat(found).isTrue();
89     }
90 
91     // We check that a re-installation gives a change event (similar to an app upgrade).
testChangeFromReinstall()92     public void testChangeFromReinstall() throws Exception {
93         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
94         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), false, true);
95         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
96         // Now enable re-installation.
97         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), true, true);
98 
99         Thread.sleep(WAIT_TIME_LONG);
100 
101         ConfigMetricsReportList reports = getReportList();
102         assertThat(reports.getReportsCount()).isGreaterThan(0);
103 
104         boolean found = false;
105         int uid = getUid();
106         for (ConfigMetricsReport report : reports.getReportsList()) {
107             LogUtil.CLog.d("Got the following report: \n" + report.toString());
108             if (hasMatchingChange(report.getUidMap(), uid, false)) {
109                 found = true;
110             }
111         }
112         assertThat(found).isTrue();
113     }
114 
testChangeFromUninstall()115     public void testChangeFromUninstall() throws Exception {
116         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
117         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), true, true);
118         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
119         int uid = getUid();
120         getDevice().uninstallPackage(DEVICE_SIDE_TEST_PACKAGE);
121 
122         Thread.sleep(WAIT_TIME_LONG);
123 
124         ConfigMetricsReportList reports = getReportList();
125         assertThat(reports.getReportsCount()).isGreaterThan(0);
126 
127         boolean found = false;
128         for (ConfigMetricsReport report : reports.getReportsList()) {
129             LogUtil.CLog.d("Got the following report: \n" + report.toString());
130             if (hasMatchingChange(report.getUidMap(), uid, true)) {
131                 found = true;
132             }
133         }
134         assertThat(found).isTrue();
135     }
136 }
137