Home | History | Annotate | Download | only in uidmap
      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 org.junit.Assert.assertTrue;
     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.
     35     public void testUidSnapshotIncluded() throws Exception {
     36         if (statsdDisabled()) {
     37             return;
     38         }
     39         // There should be at least the test app installed during the test setup.
     40         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
     41 
     42         ConfigMetricsReportList reports = getReportList();
     43         assertTrue(reports.getReportsCount() > 0);
     44 
     45         for (ConfigMetricsReport report : reports.getReportsList()) {
     46             UidMapping uidmap = report.getUidMap();
     47             assertTrue(uidmap.getSnapshotsCount() > 0);
     48             for (PackageInfoSnapshot snapshot : uidmap.getSnapshotsList()) {
     49                 // There must be at least one element in each snapshot (at least one package is
     50                 // installed).
     51                 assertTrue(snapshot.getPackageInfoCount() > 0);
     52             }
     53         }
     54     }
     55 
     56     private boolean hasMatchingChange(UidMapping uidmap, int uid, boolean expectDeletion) {
     57         LogUtil.CLog.d("The uid we are looking for is " + uid);
     58         for (UidMapping.Change change : uidmap.getChangesList()) {
     59             if (change.getAppHash() == DEVICE_SIDE_TEST_PKG_HASH && change.getUid() == uid) {
     60                 if (change.getDeletion() == expectDeletion) {
     61                     return true;
     62                 }
     63             }
     64         }
     65         return false;
     66     }
     67 
     68     // Tests that delta event included during app installation.
     69     public void testChangeFromInstallation() throws Exception {
     70         if (statsdDisabled()) {
     71             return;
     72         }
     73         getDevice().uninstallPackage(DEVICE_SIDE_TEST_PACKAGE);
     74         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
     75         // Install the package after the config is sent to statsd. The uid map is not guaranteed to
     76         // be updated if there's no config in statsd.
     77         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
     78         final String result = getDevice().installPackage(
     79                 buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), false, true);
     80 
     81         Thread.sleep(WAIT_TIME_SHORT);
     82 
     83         ConfigMetricsReportList reports = getReportList();
     84         assertTrue(reports.getReportsCount() > 0);
     85 
     86         boolean found = false;
     87         int uid = getUid();
     88         for (ConfigMetricsReport report : reports.getReportsList()) {
     89             LogUtil.CLog.d("Got the following report: \n" + report.toString());
     90             if (hasMatchingChange(report.getUidMap(), uid, false)) {
     91                 found = true;
     92             }
     93         }
     94         assertTrue(found);
     95     }
     96 
     97     // We check that a re-installation gives a change event (similar to an app upgrade).
     98     public void testChangeFromReinstall() throws Exception {
     99         if (statsdDisabled()) {
    100             return;
    101         }
    102         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
    103         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), false, true);
    104         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
    105         // Now enable re-installation.
    106         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), true, true);
    107 
    108         Thread.sleep(WAIT_TIME_SHORT);
    109 
    110         ConfigMetricsReportList reports = getReportList();
    111         assertTrue(reports.getReportsCount() > 0);
    112 
    113         boolean found = false;
    114         int uid = getUid();
    115         for (ConfigMetricsReport report : reports.getReportsList()) {
    116             LogUtil.CLog.d("Got the following report: \n" + report.toString());
    117             if (hasMatchingChange(report.getUidMap(), uid, false)) {
    118                 found = true;
    119             }
    120         }
    121         assertTrue(found);
    122     }
    123 
    124     public void testChangeFromUninstall() throws Exception {
    125         if (statsdDisabled()) {
    126             return;
    127         }
    128         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mCtsBuild);
    129         getDevice().installPackage(buildHelper.getTestFile(DEVICE_SIDE_TEST_APK), true, true);
    130         createAndUploadConfig(AtomsProto.Atom.UID_PROCESS_STATE_CHANGED_FIELD_NUMBER);
    131         int uid = getUid();
    132         getDevice().uninstallPackage(DEVICE_SIDE_TEST_PACKAGE);
    133 
    134         Thread.sleep(WAIT_TIME_SHORT);
    135 
    136         ConfigMetricsReportList reports = getReportList();
    137         assertTrue(reports.getReportsCount() > 0);
    138 
    139         boolean found = false;
    140         for (ConfigMetricsReport report : reports.getReportsList()) {
    141             LogUtil.CLog.d("Got the following report: \n" + report.toString());
    142             if (hasMatchingChange(report.getUidMap(), uid, true)) {
    143                 found = true;
    144             }
    145         }
    146         assertTrue(found);
    147     }
    148 }
    149