Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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 com.android.server.cts;
     17 
     18 import android.service.pm.PackageProto;
     19 import android.service.pm.PackageServiceDumpProto;
     20 
     21 import java.util.regex.Matcher;
     22 import java.util.regex.Pattern;
     23 
     24 /** Test for "dumpsys package --proto" */
     25 public class PackageIncidentTest extends ProtoDumpTestCase {
     26     // Use the test apk from the NetstatsIncidentTest
     27     private static final String DEVICE_SIDE_TEST_APK = "CtsNetStatsApp.apk";
     28     private static final String DEVICE_SIDE_TEST_PACKAGE = "com.android.server.cts.netstats";
     29 
     30     @Override
     31     protected void tearDown() throws Exception {
     32         getDevice().uninstallPackage(DEVICE_SIDE_TEST_PACKAGE);
     33 
     34         super.tearDown();
     35     }
     36 
     37     private static void assertPositive(String name, long value) {
     38         if (value > 0) return;
     39         fail(name + " expected to be positive, but was: " + value);
     40     }
     41 
     42     private static void assertNotNegative(String name, long value) {
     43         if (value >= 0) return;
     44         fail(name + " expected to be zero or positive, but was: " + value);
     45     }
     46 
     47     /** Parse the output of "dumpsys package --proto" and make sure the values are probable. */
     48     public void testPackageServiceDump() throws Exception {
     49         final long st = System.currentTimeMillis();
     50 
     51         installPackage(DEVICE_SIDE_TEST_APK, /* grantPermissions= */ true);
     52 
     53         // Find the package UID, version code, and version string.
     54         final Matcher matcher =
     55                 execCommandAndFind(
     56                         "dumpsys package " + DEVICE_SIDE_TEST_PACKAGE,
     57                         "userId=(\\d+).*versionCode=(\\d+).*versionName=([^\\n]*)",
     58                         Pattern.DOTALL);
     59         final int uid = Integer.parseInt(matcher.group(1));
     60         final int versionCode = Integer.parseInt(matcher.group(2));
     61         final String versionString = matcher.group(3).trim();
     62 
     63         final PackageServiceDumpProto dump =
     64                 getDump(PackageServiceDumpProto.parser(), "dumpsys package --proto");
     65 
     66         PackageProto testPackage = null;
     67         for (PackageProto pkg : dump.getPackagesList()) {
     68             if (pkg.getName().equals(DEVICE_SIDE_TEST_PACKAGE)) {
     69                 testPackage = pkg;
     70                 break;
     71             }
     72         }
     73         assertNotNull(testPackage);
     74         assertEquals(testPackage.getName(), DEVICE_SIDE_TEST_PACKAGE);
     75         assertEquals(testPackage.getUid(), uid);
     76         assertEquals(testPackage.getVersionCode(), versionCode);
     77         assertEquals(testPackage.getVersionString(), versionString);
     78         assertPositive("install_time_ms", testPackage.getInstallTimeMs());
     79         assertEquals(testPackage.getInstallTimeMs(), testPackage.getUpdateTimeMs());
     80         assertEquals(testPackage.getSplits(0).getName(), "base");
     81         assertEquals(testPackage.getSplits(0).getRevisionCode(), 0);
     82         assertEquals(testPackage.getUsers(0).getId(), 0);
     83         assertEquals(
     84                 testPackage.getUsers(0).getInstallType(),
     85                 PackageProto.UserInfoProto.InstallType.FULL_APP_INSTALL);
     86         assertFalse(testPackage.getUsers(0).getIsHidden());
     87         assertFalse(testPackage.getUsers(0).getIsLaunched());
     88         assertFalse(
     89                 testPackage.getUsers(0).getEnabledState()
     90                         == PackageProto.UserInfoProto.EnabledState
     91                                 .COMPONENT_ENABLED_STATE_DISABLED_USER);
     92 
     93         verifyPackageServiceDumpProto(dump, PRIVACY_NONE);
     94     }
     95 
     96     static void verifyPackageServiceDumpProto(PackageServiceDumpProto dump, final int filterLevel) throws Exception {
     97         assertNotNull(dump.getVerifierPackage().getName());
     98         assertNotNull(dump.getSharedLibraries(0).getName());
     99         if (dump.getSharedLibraries(0).getIsJar()) {
    100             assertNotNull(dump.getSharedLibraries(0).getPath());
    101         } else {
    102             assertNotNull(dump.getSharedLibraries(0).getApk());
    103         }
    104         assertNotNull(dump.getFeatures(0).getName());
    105 
    106         PackageServiceDumpProto.SharedUserProto systemUser = null;
    107         for (PackageServiceDumpProto.SharedUserProto user : dump.getSharedUsersList()) {
    108             if (user.getUserId() == 1000) {
    109                 systemUser = user;
    110                 break;
    111             }
    112         }
    113         assertNotNull(systemUser);
    114         assertEquals("android.uid.system", systemUser.getName());
    115 
    116         if (filterLevel == PRIVACY_AUTO) {
    117             for (String msg : dump.getMessagesList()) {
    118                 assertTrue(msg.isEmpty());
    119             }
    120         }
    121     }
    122 }
    123