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 
17 package com.android.server.cts;
18 
19 import com.android.server.am.ActiveInstrumentationProto;
20 import com.android.server.am.ActiveServicesProto;
21 import com.android.server.am.ActiveServicesProto.ServicesByUser;
22 import com.android.server.am.ActivityManagerServiceDumpBroadcastsProto;
23 import com.android.server.am.ActivityManagerServiceDumpProcessesProto;
24 import com.android.server.am.ActivityManagerServiceDumpProcessesProto.LruProcesses;
25 import com.android.server.am.ActivityManagerServiceDumpServicesProto;
26 import com.android.server.am.AppErrorsProto;
27 import com.android.server.am.AppTimeTrackerProto;
28 import com.android.server.am.BroadcastQueueProto;
29 import com.android.server.am.BroadcastQueueProto.BroadcastSummary;
30 import com.android.server.am.BroadcastRecordProto;
31 import com.android.server.am.ConnectionRecordProto;
32 import com.android.server.am.GrantUriProto;
33 import com.android.server.am.ImportanceTokenProto;
34 import com.android.server.am.NeededUriGrantsProto;
35 import com.android.server.am.ProcessRecordProto;
36 import com.android.server.am.ServiceRecordProto;
37 import com.android.server.am.UidRecordProto;
38 import com.android.server.am.UriPermissionOwnerProto;
39 import com.android.server.am.VrControllerProto;
40 
41 /**
42  * Test to check that the activity manager service properly outputs its dump state.
43  *
44  * make -j32 CtsIncidentHostTestCases
45  * cts-tradefed run singleCommand cts-dev -d --module CtsIncidentHostTestCases
46  */
47 public class ActivityManagerIncidentTest extends ProtoDumpTestCase {
48 
49     private static final String TEST_BROADCAST = "com.android.mybroadcast";
50     private static final String SYSTEM_PROC = "system";
51     private static final int SYSTEM_UID = 1000;
52 
53     /**
54      * Tests activity manager dumps broadcasts.
55      */
testDumpBroadcasts()56     public void testDumpBroadcasts() throws Exception {
57         getDevice().executeShellCommand("am broadcast -a " + TEST_BROADCAST);
58         Thread.sleep(100);
59         final ActivityManagerServiceDumpBroadcastsProto dump = getDump(
60                 ActivityManagerServiceDumpBroadcastsProto.parser(),
61                 "dumpsys activity --proto broadcasts");
62 
63         assertTrue(dump.getReceiverListCount() > 0);
64         assertTrue(dump.getBroadcastQueueCount() > 0);
65         assertTrue(dump.getStickyBroadcastsCount() > 0);
66 
67         boolean found = false;
68 mybroadcast:
69         for (BroadcastQueueProto queue : dump.getBroadcastQueueList()) {
70             for (BroadcastRecordProto record : queue.getHistoricalBroadcastsList()) {
71                 if (record.getIntentAction().equals(TEST_BROADCAST)) {
72                     found = true;
73                     break mybroadcast;
74                 }
75             }
76             for (BroadcastSummary summary : queue.getHistoricalBroadcastsSummaryList()) {
77                 if (summary.getIntent().getAction().equals(TEST_BROADCAST)) {
78                     found = true;
79                     break mybroadcast;
80                 }
81             }
82         }
83         assertTrue(found);
84         ActivityManagerServiceDumpBroadcastsProto.MainHandler mainHandler = dump.getHandler();
85         assertTrue(mainHandler.getHandler().contains(
86             "com.android.server.am.ActivityManagerService"));
87     }
88 
89     /**
90      * Tests activity manager dumps services.
91      */
testDumpServices()92     public void testDumpServices() throws Exception {
93         final ActivityManagerServiceDumpServicesProto dump = getDump(
94                 ActivityManagerServiceDumpServicesProto.parser(),
95                 "dumpsys activity --proto service");
96         ActiveServicesProto activeServices = dump.getActiveServices();
97         assertTrue(activeServices.getServicesByUsersCount() > 0);
98 
99         for (ServicesByUser perUserServices : activeServices.getServicesByUsersList()) {
100             assertTrue(perUserServices.getServiceRecordsCount() >= 0);
101             for (ServiceRecordProto service : perUserServices.getServiceRecordsList()) {
102                 assertFalse(service.getShortName().isEmpty());
103                 assertFalse(service.getPackageName().isEmpty());
104                 assertFalse(service.getProcessName().isEmpty());
105                 assertFalse(service.getAppinfo().getBaseDir().isEmpty());
106                 assertFalse(service.getAppinfo().getDataDir().isEmpty());
107             }
108         }
109 
110         verifyActivityManagerServiceDumpServicesProto(dump, PRIVACY_NONE);
111     }
112 
verifyActivityManagerServiceDumpServicesProto(ActivityManagerServiceDumpServicesProto dump, final int filterLevel)113     static void verifyActivityManagerServiceDumpServicesProto(ActivityManagerServiceDumpServicesProto dump, final int filterLevel) throws Exception {
114         for (ServicesByUser sbu : dump.getActiveServices().getServicesByUsersList()) {
115             for (ServiceRecordProto srp : sbu.getServiceRecordsList()) {
116                 verifyServiceRecordProto(srp, filterLevel);
117             }
118         }
119     }
120 
verifyServiceRecordProto(ServiceRecordProto srp, final int filterLevel)121     private static void verifyServiceRecordProto(ServiceRecordProto srp, final int filterLevel) throws Exception {
122         if (filterLevel == PRIVACY_AUTO) {
123             assertTrue(srp.getAppinfo().getBaseDir().isEmpty());
124             assertTrue(srp.getAppinfo().getResDir().isEmpty());
125             assertTrue(srp.getAppinfo().getDataDir().isEmpty());
126         } else {
127             assertFalse(srp.getAppinfo().getBaseDir().isEmpty());
128             assertFalse(srp.getAppinfo().getDataDir().isEmpty());
129         }
130         for (ServiceRecordProto.StartItem si : srp.getDeliveredStartsList()) {
131             verifyServiceRecordProtoStartItem(si, filterLevel);
132         }
133         for (ServiceRecordProto.StartItem si : srp.getPendingStartsList()) {
134             verifyServiceRecordProtoStartItem(si, filterLevel);
135         }
136         for (ConnectionRecordProto crp : srp.getConnectionsList()) {
137             verifyConnectionRecordProto(crp, filterLevel);
138         }
139     }
140 
verifyServiceRecordProtoStartItem(ServiceRecordProto.StartItem si, final int filterLevel)141     private static void verifyServiceRecordProtoStartItem(ServiceRecordProto.StartItem si, final int filterLevel) throws Exception {
142         verifyNeededUriGrantsProto(si.getNeededGrants(), filterLevel);
143         verifyUriPermissionOwnerProto(si.getUriPermissions(), filterLevel);
144     }
145 
verifyNeededUriGrantsProto(NeededUriGrantsProto nugp, final int filterLevel)146     private static void verifyNeededUriGrantsProto(NeededUriGrantsProto nugp, final int filterLevel) throws Exception {
147         for (GrantUriProto gup : nugp.getGrantsList()) {
148             verifyGrantUriProto(gup, filterLevel);
149         }
150     }
151 
verifyUriPermissionOwnerProto(UriPermissionOwnerProto upop, final int filterLevel)152     private static void verifyUriPermissionOwnerProto(UriPermissionOwnerProto upop, final int filterLevel) throws Exception {
153         if (filterLevel == PRIVACY_AUTO) {
154             assertTrue(upop.getOwner().isEmpty());
155         }
156         for (GrantUriProto gup : upop.getReadPermsList()) {
157             verifyGrantUriProto(gup, filterLevel);
158         }
159         for (GrantUriProto gup : upop.getWritePermsList()) {
160             verifyGrantUriProto(gup, filterLevel);
161         }
162     }
163 
verifyGrantUriProto(GrantUriProto gup, final int filterLevel)164     private static void verifyGrantUriProto(GrantUriProto gup, final int filterLevel) throws Exception {
165         if (filterLevel == PRIVACY_AUTO) {
166             assertTrue(gup.getUri().isEmpty());
167         }
168     }
169 
verifyConnectionRecordProto(ConnectionRecordProto crp, final int filterLevel)170     private static void verifyConnectionRecordProto(ConnectionRecordProto crp, final int filterLevel) throws Exception {
171         for (ConnectionRecordProto.Flag f : crp.getFlagsList()) {
172             assertTrue(ConnectionRecordProto.Flag.getDescriptor().getValues().contains(f.getValueDescriptor()));
173         }
174     }
175 
176     /**
177      * Tests activity manager dumps processes.
178      */
testDumpProcesses()179     public void testDumpProcesses() throws Exception {
180         final ActivityManagerServiceDumpProcessesProto dump = getDump(
181                 ActivityManagerServiceDumpProcessesProto.parser(),
182                 "dumpsys activity --proto processes");
183 
184         assertTrue(dump.getProcsCount() > 0);
185         boolean procFound = false;
186         for (ProcessRecordProto proc : dump.getProcsList()) {
187             if (proc.getProcessName().equals(SYSTEM_PROC) && proc.getUid() == SYSTEM_UID) {
188                 procFound = true;
189                 break;
190             }
191         }
192         assertTrue(procFound);
193 
194         assertTrue(dump.getActiveUidsCount() > 0);
195         boolean uidFound = false;
196         for (UidRecordProto uid : dump.getActiveUidsList()) {
197             if (uid.getUid() == SYSTEM_UID) {
198                 uidFound = true;
199                 break;
200             }
201         }
202         assertTrue(uidFound);
203 
204         LruProcesses lruProcs = dump.getLruProcs();
205         assertTrue(lruProcs.getSize() == lruProcs.getListCount());
206         assertTrue(dump.getUidObserversCount() > 0);
207         assertTrue(dump.getAdjSeq() > 0);
208         assertTrue(dump.getLruSeq() > 0);
209 
210         verifyActivityManagerServiceDumpProcessesProto(dump, PRIVACY_NONE);
211     }
212 
verifyActivityManagerServiceDumpProcessesProto(ActivityManagerServiceDumpProcessesProto dump, final int filterLevel)213     static void verifyActivityManagerServiceDumpProcessesProto(ActivityManagerServiceDumpProcessesProto dump, final int filterLevel) throws Exception {
214         for (ActiveInstrumentationProto aip : dump.getActiveInstrumentationsList()) {
215             verifyActiveInstrumentationProto(aip, filterLevel);
216         }
217         for (UidRecordProto urp : dump.getActiveUidsList()) {
218             verifyUidRecordProto(urp, filterLevel);
219         }
220         for (UidRecordProto urp : dump.getValidateUidsList()) {
221             verifyUidRecordProto(urp, filterLevel);
222         }
223         for (ImportanceTokenProto itp : dump.getImportantProcsList()) {
224             verifyImportanceTokenProto(itp, filterLevel);
225         }
226         verifyAppErrorsProto(dump.getAppErrors(), filterLevel);
227         verifyVrControllerProto(dump.getVrController(), filterLevel);
228         verifyAppTimeTrackerProto(dump.getCurrentTracker(), filterLevel);
229         if (filterLevel == PRIVACY_AUTO) {
230             assertTrue(dump.getMemWatchProcesses().getDump().getUri().isEmpty());
231         }
232     }
233 
verifyActiveInstrumentationProto(ActiveInstrumentationProto aip, final int filterLevel)234     private static void verifyActiveInstrumentationProto(ActiveInstrumentationProto aip, final int filterLevel) throws Exception {
235     }
236 
verifyUidRecordProto(UidRecordProto urp, final int filterLevel)237     private static void verifyUidRecordProto(UidRecordProto urp, final int filterLevel) throws Exception {
238         for (UidRecordProto.Change c : urp.getLastReportedChangesList()) {
239             assertTrue(UidRecordProto.Change.getDescriptor().getValues().contains(c.getValueDescriptor()));
240         }
241         assertTrue(urp.getNumProcs() >= 0);
242     }
243 
verifyImportanceTokenProto(ImportanceTokenProto itp, final int filterLevel)244     private static void verifyImportanceTokenProto(ImportanceTokenProto itp, final int filterLevel) throws Exception {
245         if (filterLevel == PRIVACY_AUTO) {
246             // The entire message is tagged as EXPLICIT, so even the pid should be stripped out.
247             assertTrue(itp.getPid() == 0);
248             assertTrue(itp.getToken().isEmpty());
249             assertTrue(itp.getReason().isEmpty());
250         }
251     }
252 
verifyAppErrorsProto(AppErrorsProto aep, final int filterLevel)253     private static void verifyAppErrorsProto(AppErrorsProto aep, final int filterLevel) throws Exception {
254         assertTrue(aep.getNowUptimeMs() >= 0);
255         if (filterLevel == PRIVACY_AUTO) {
256             for (AppErrorsProto.BadProcess bp : aep.getBadProcessesList()) {
257                 for (AppErrorsProto.BadProcess.Entry e : bp.getEntriesList()) {
258                     assertTrue(e.getLongMsg().isEmpty());
259                     assertTrue(e.getStack().isEmpty());
260                 }
261             }
262         }
263     }
264 
verifyVrControllerProto(VrControllerProto vcp, final int filterLevel)265     private static void verifyVrControllerProto(VrControllerProto vcp, final int filterLevel) throws Exception {
266         for (VrControllerProto.VrMode vm : vcp.getVrModeList()) {
267             assertTrue(VrControllerProto.VrMode.getDescriptor().getValues().contains(vm.getValueDescriptor()));
268         }
269     }
270 
verifyAppTimeTrackerProto(AppTimeTrackerProto attp, final int filterLevel)271     private static void verifyAppTimeTrackerProto(AppTimeTrackerProto attp, final int filterLevel) throws Exception {
272         assertTrue(attp.getTotalDurationMs() >= 0);
273         for (AppTimeTrackerProto.PackageTime pt : attp.getPackageTimesList()) {
274             assertTrue(pt.getDurationMs() >= 0);
275         }
276     }
277 }
278