1 /*
2  * Copyright (C) 2020 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.appsecurity.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.cts.statsdatom.lib.AtomTestUtils;
22 import android.cts.statsdatom.lib.ConfigUtils;
23 import android.cts.statsdatom.lib.DeviceUtils;
24 import android.cts.statsdatom.lib.ReportUtils;
25 
26 import com.android.os.AtomsProto;
27 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
28 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
29 import com.android.tradefed.util.RunUtil;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 
37 /**
38  * Set of tests that verify atoms are correctly sent to statsd.
39  */
40 @RunWith(DeviceJUnit4ClassRunner.class)
41 public class StatsdAppSecurityAtomTest extends BaseHostJUnit4Test {
42     private static final String STATSD_APP_APK = "CtsStatsSecurityApp.apk";
43     private static final String STATSD_APP_PKG = "com.android.cts.statsdsecurityapp";
44 
45     @Before
setUp()46     public void setUp() throws Exception {
47         ConfigUtils.removeConfig(getDevice());
48         ReportUtils.clearReports(getDevice());
49         installPackage(STATSD_APP_APK);
50         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG);
51     }
52 
53     @After
tearDown()54     public void tearDown() throws Exception {
55         ConfigUtils.removeConfig(getDevice());
56         ReportUtils.clearReports(getDevice());
57         getDevice().uninstallPackage(STATSD_APP_PKG);
58     }
59 
60     @Test
testRoleHolder()61     public void testRoleHolder() throws Exception {
62         // Make device side test package a role holder
63         String callScreenAppRole = "android.app.role.CALL_SCREENING";
64         getDevice().executeShellCommand(
65                 "cmd role add-role-holder " + callScreenAppRole + " "
66                         + STATSD_APP_PKG);
67 
68         // Set up what to collect
69         ConfigUtils.uploadConfigForPulledAtom(getDevice(), STATSD_APP_PKG,
70                 AtomsProto.Atom.ROLE_HOLDER_FIELD_NUMBER);
71 
72         boolean verifiedKnowRoleState = false;
73 
74         // Pull a report
75         AtomTestUtils.sendAppBreadcrumbReportedAtom(getDevice());
76         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
77 
78         int testAppId = getAppId(DeviceUtils.getAppUid(getDevice(), STATSD_APP_PKG));
79 
80         for (AtomsProto.Atom atom : ReportUtils.getGaugeMetricAtoms(getDevice())) {
81             AtomsProto.RoleHolder roleHolder = atom.getRoleHolder();
82 
83             assertThat(roleHolder.getPackageName()).isNotNull();
84             assertThat(roleHolder.getRole()).isNotNull();
85 
86             // Verify that only empty roles have the special -1 UID
87             if (roleHolder.getPackageName().equals("")) {
88                 assertThat(roleHolder.getUid()).isEqualTo(-1);
89             } else {
90                 assertThat(roleHolder.getUid()).isAtLeast(0);
91             }
92 
93             if (roleHolder.getPackageName().equals(STATSD_APP_PKG)) {
94                 assertThat(getAppId(roleHolder.getUid())).isEqualTo(testAppId);
95                 assertThat(roleHolder.getPackageName()).isEqualTo(STATSD_APP_PKG);
96                 assertThat(roleHolder.getRole()).isEqualTo(callScreenAppRole);
97 
98                 verifiedKnowRoleState = true;
99             }
100         }
101 
102         assertThat(verifiedKnowRoleState).isTrue();
103     }
104 
105     /**
106      * The app id from a uid.
107      *
108      * @param uid The uid of the app
109      * @return The app id of the app
110      * @see android.os.UserHandle#getAppId
111      */
getAppId(int uid)112     private static int getAppId(int uid) {
113         return uid % 100000;
114     }
115 }
116