blob: b6fef4a2e009b6b5c03b37b4acec8675ad924dd5 [file] [log] [blame]
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +01001/**
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package android.app.usage.cts;
18
19import android.app.AppOpsManager;
20import android.app.usage.NetworkStatsManager;
21import android.app.usage.NetworkStats;
22import android.content.Context;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010023import android.net.ConnectivityManager;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010024import android.net.Network;
25import android.net.NetworkCapabilities;
26import android.net.NetworkInfo;
27import android.net.NetworkRequest;
28import android.os.ParcelFileDescriptor;
29import android.os.Process;
30import android.os.RemoteException;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010031import android.telephony.TelephonyManager;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010032import android.test.InstrumentationTestCase;
33import android.util.Log;
34
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010035import java.io.FileInputStream;
36import java.io.IOException;
37import java.io.InputStream;
38import java.io.InputStreamReader;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010039import java.net.URL;
40import java.text.MessageFormat;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010041import java.util.Scanner;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010042import javax.net.ssl.HttpsURLConnection;
43
44import libcore.io.IoUtils;
45import libcore.io.Streams;
46
47public class NetworkUsageStatsTest extends InstrumentationTestCase {
48 private static final String LOG_TAG = "NetworkUsageStatsTest";
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010049 private static final String APPOPS_SET_SHELL_COMMAND = "appops set {0} {1} {2}";
50 private static final String APPOPS_GET_SHELL_COMMAND = "appops get {0} {1}";
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010051
52 private static final long MINUTE = 1000 * 60;
53
54 private static final int[] sNetworkTypesToTest = new int[] {
55 ConnectivityManager.TYPE_WIFI,
56 ConnectivityManager.TYPE_MOBILE,
57 };
58
59 // Order corresponds to sNetworkTypesToTest
60 private static final int[] sTransportTypesToTest = new int[] {
61 NetworkCapabilities.TRANSPORT_WIFI,
62 NetworkCapabilities.TRANSPORT_CELLULAR,
63 };
64
65 private NetworkStatsManager mNsm;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010066 private ConnectivityManager mCm;
67 private long mStartTime;
68 private long mEndTime;
69
70 private long mBytesRead;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010071 private String mWriteSettingsMode;
72 private String mUsageStatsMode;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010073
74 private void exerciseRemoteHost(int transportType) throws Exception {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +010075 final int timeout = 15000;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010076 mCm.requestNetwork(new NetworkRequest.Builder()
77 .addTransportType(transportType)
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010078 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010079 .build(), new ConnectivityManager.NetworkCallback() {
80 @Override
81 public void onAvailable(Network network) {
82 NetworkInfo networkInfo = mCm.getNetworkInfo(network);
83 if (networkInfo == null) {
84 Log.w(LOG_TAG, "Network info is null");
85 } else {
86 Log.w(LOG_TAG, "Network: " + networkInfo.toString());
87 }
88 InputStreamReader in = null;
89 HttpsURLConnection urlc = null;
90 String originalKeepAlive = System.getProperty("http.keepAlive");
91 System.setProperty("http.keepAlive", "false");
92 try {
93 urlc = (HttpsURLConnection) network.openConnection(new URL(
94 "https://www.google.com"));
95 urlc.setConnectTimeout(timeout);
96 urlc.setUseCaches(false);
97 urlc.connect();
98 boolean ping = urlc.getResponseCode() == 200;
99 if (ping) {
100 in = new InputStreamReader(
101 (InputStream) urlc.getContent());
102
103 mBytesRead = 0;
104 while (in.read() != -1) ++mBytesRead;
105 }
106 } catch (Exception e) {
107 Log.i(LOG_TAG, "Badness during exercising remote server: " + e);
108 } finally {
109 if (in != null) {
110 try {
111 in.close();
112 } catch (IOException e) {
113 // don't care
114 }
115 }
116 if (urlc != null) {
117 urlc.disconnect();
118 }
119 if (originalKeepAlive == null) {
120 System.clearProperty("http.keepAlive");
121 } else {
122 System.setProperty("http.keepAlive", originalKeepAlive);
123 }
124 }
125 }
126 });
127 try {
128 Thread.sleep(timeout);
129 } catch (InterruptedException e) {
130 }
131 }
132
133 @Override
134 protected void setUp() throws Exception {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100135 super.setUp();
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100136 mNsm = (NetworkStatsManager) getInstrumentation().getContext()
137 .getSystemService(Context.NETWORK_STATS_SERVICE);
138
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100139 mCm = (ConnectivityManager) getInstrumentation().getContext()
140 .getSystemService(Context.CONNECTIVITY_SERVICE);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100141
142 mWriteSettingsMode = getAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS);
143 setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, "allow");
144 mUsageStatsMode = getAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100145 }
146
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100147 @Override
148 protected void tearDown() throws Exception {
149 if (mWriteSettingsMode != null) {
150 setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, mWriteSettingsMode);
151 }
152 if (mUsageStatsMode != null) {
153 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, mUsageStatsMode);
154 }
155 super.tearDown();
156 }
157
158 private void setAppOpsMode(String appop, String mode) throws Exception {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100159 final String command = MessageFormat.format(APPOPS_SET_SHELL_COMMAND,
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100160 getInstrumentation().getContext().getPackageName(), appop, mode);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100161 ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
162 .executeShellCommand(command);
163 try {
164 Streams.readFully(new FileInputStream(pfd.getFileDescriptor()));
165 } finally {
166 IoUtils.closeQuietly(pfd.getFileDescriptor());
167 }
168 }
169
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100170 private String getAppOpsMode(String appop) throws Exception {
171 String result;
172 final String command = MessageFormat.format(APPOPS_GET_SHELL_COMMAND,
173 getInstrumentation().getContext().getPackageName(), appop);
174 ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
175 .executeShellCommand(command);
176 try {
177 result = convertStreamToString(new FileInputStream(pfd.getFileDescriptor()));
178 } finally {
179 IoUtils.closeQuietly(pfd.getFileDescriptor());
180 }
181 if (result == null) {
182 Log.w(LOG_TAG, "App op " + appop + " could not be read.");
183 }
184 return result;
185 }
186
187 private static String convertStreamToString(InputStream is) {
188 try (Scanner scanner = new Scanner(is).useDelimiter("\\A")) {
189 return scanner.hasNext() ? scanner.next() : null;
190 }
191 }
192
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100193 private boolean shouldTestThisNetworkType(int networkTypeIndex, long tolerance)
194 throws Exception {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100195 NetworkInfo networkInfo = mCm.getNetworkInfo(sNetworkTypesToTest[networkTypeIndex]);
196 if (networkInfo == null || !networkInfo.isAvailable()) {
197 return false;
198 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100199 mStartTime = System.currentTimeMillis() - tolerance;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100200 exerciseRemoteHost(sTransportTypesToTest[networkTypeIndex]);
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100201 mEndTime = System.currentTimeMillis() + tolerance;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100202 return true;
203 }
204
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100205 private String getSubscriberId(int networkType) {
206 if (ConnectivityManager.TYPE_MOBILE == networkType) {
207 TelephonyManager tm = (TelephonyManager) getInstrumentation().getContext()
208 .getSystemService(Context.TELEPHONY_SERVICE);
209 return tm.getSubscriberId();
210 }
211 return "";
212 }
213
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100214 public void testDeviceSummary() throws Exception {
215 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100216 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100217 continue;
218 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100219 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100220 NetworkStats.Bucket bucket = null;
221 try {
222 bucket = mNsm.querySummaryForDevice(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100223 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
224 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100225 } catch (RemoteException | SecurityException e) {
226 fail("testDeviceSummary fails with exception: " + e.toString());
227 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100228 assertNotNull(bucket);
229 assertTimestamps(bucket);
230 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
231 assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100232 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100233 try {
234 bucket = mNsm.querySummaryForDevice(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100235 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
236 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100237 fail("negative testDeviceSummary fails: no exception thrown.");
238 } catch (RemoteException e) {
239 fail("testDeviceSummary fails with exception: " + e.toString());
240 } catch (SecurityException e) {
241 // expected outcome
242 }
243 }
244 }
245
246 public void testUserSummary() throws Exception {
247 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100248 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100249 continue;
250 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100251 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100252 NetworkStats.Bucket bucket = null;
253 try {
254 bucket = mNsm.querySummaryForUser(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100255 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
256 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100257 } catch (RemoteException | SecurityException e) {
258 fail("testUserSummary fails with exception: " + e.toString());
259 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100260 assertNotNull(bucket);
261 assertTimestamps(bucket);
262 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
263 assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100264 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100265 try {
266 bucket = mNsm.querySummaryForUser(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100267 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
268 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100269 fail("negative testUserSummary fails: no exception thrown.");
270 } catch (RemoteException e) {
271 fail("testUserSummary fails with exception: " + e.toString());
272 } catch (SecurityException e) {
273 // expected outcome
274 }
275 }
276 }
277
278 public void testAppSummary() throws Exception {
279 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100280 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100281 continue;
282 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100283 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100284 NetworkStats result = null;
285 try {
286 result = mNsm.querySummary(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100287 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
288 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100289 assertTrue(result != null);
290 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
291 long totalTxPackets = 0;
292 long totalRxPackets = 0;
293 long totalTxBytes = 0;
294 long totalRxBytes = 0;
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100295 while (result.hasNextBucket()) {
296 assertTrue(result.getNextBucket(bucket));
297 assertTimestamps(bucket);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100298 if (bucket.getUid() == Process.myUid()) {
299 totalTxPackets += bucket.getTxPackets();
300 totalRxPackets += bucket.getRxPackets();
301 totalTxBytes += bucket.getTxBytes();
302 totalRxBytes += bucket.getRxBytes();
303 }
304 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100305 assertFalse(result.getNextBucket(bucket));
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100306 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
307 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
308 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
309 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
310 } catch (RemoteException | SecurityException e) {
311 fail("testAppSummary fails with exception: " + e.toString());
312 } finally {
313 if (result != null) {
314 result.close();
315 }
316 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100317 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100318 try {
319 result = mNsm.querySummary(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100320 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
321 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100322 fail("negative testAppSummary fails: no exception thrown.");
323 } catch (RemoteException e) {
324 fail("testAppSummary fails with exception: " + e.toString());
325 } catch (SecurityException e) {
326 // expected outcome
327 }
328 }
329 }
330
331 public void testAppDetails() throws Exception {
332 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100333 // Relatively large tolerance to accommodate for history bucket size.
334 if (!shouldTestThisNetworkType(i, MINUTE * 120)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100335 continue;
336 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100337 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100338 NetworkStats result = null;
339 try {
340 result = mNsm.queryDetails(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100341 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
342 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100343 assertTrue(result != null);
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100344 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
345 long totalTxPackets = 0;
346 long totalRxPackets = 0;
347 long totalTxBytes = 0;
348 long totalRxBytes = 0;
349 while (result.hasNextBucket()) {
350 assertTrue(result.getNextBucket(bucket));
351 assertTimestamps(bucket);
352 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
353 if (bucket.getUid() == Process.myUid()) {
354 totalTxPackets += bucket.getTxPackets();
355 totalRxPackets += bucket.getRxPackets();
356 totalTxBytes += bucket.getTxBytes();
357 totalRxBytes += bucket.getRxBytes();
358 }
359 }
360 assertFalse(result.getNextBucket(bucket));
361 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
362 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
363 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
364 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100365 } catch (RemoteException | SecurityException e) {
366 fail("testAppDetails fails with exception: " + e.toString());
367 } finally {
368 if (result != null) {
369 result.close();
370 }
371 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100372 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100373 try {
374 result = mNsm.queryDetails(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100375 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
376 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100377 fail("negative testAppDetails fails: no exception thrown.");
378 } catch (RemoteException e) {
379 fail("testAppDetails fails with exception: " + e.toString());
380 } catch (SecurityException e) {
381 // expected outcome
382 }
383 }
384 }
385
386 public void testUidDetails() throws Exception {
387 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100388 // Relatively large tolerance to accommodate for history bucket size.
389 if (!shouldTestThisNetworkType(i, MINUTE * 120)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100390 continue;
391 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100392 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100393 NetworkStats result = null;
394 try {
395 result = mNsm.queryDetailsForUid(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100396 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
397 mStartTime, mEndTime, Process.myUid());
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100398 assertTrue(result != null);
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100399 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
400 long totalTxPackets = 0;
401 long totalRxPackets = 0;
402 long totalTxBytes = 0;
403 long totalRxBytes = 0;
404 while (result.hasNextBucket()) {
405 assertTrue(result.getNextBucket(bucket));
406 assertTimestamps(bucket);
407 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
408 assertEquals(bucket.getUid(), Process.myUid());
409 totalTxPackets += bucket.getTxPackets();
410 totalRxPackets += bucket.getRxPackets();
411 totalTxBytes += bucket.getTxBytes();
412 totalRxBytes += bucket.getRxBytes();
413 }
414 assertFalse(result.getNextBucket(bucket));
415 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
416 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
417 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
418 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100419 } catch (RemoteException | SecurityException e) {
420 fail("testUidDetails fails with exception: " + e.toString());
421 } finally {
422 if (result != null) {
423 result.close();
424 }
425 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100426 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100427 try {
428 result = mNsm.queryDetailsForUid(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100429 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
430 mStartTime, mEndTime, Process.myUid());
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100431 fail("negative testUidDetails fails: no exception thrown.");
432 } catch (RemoteException e) {
433 fail("testUidDetails fails with exception: " + e.toString());
434 } catch (SecurityException e) {
435 // expected outcome
436 }
437 }
438 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100439
440 private void assertTimestamps(final NetworkStats.Bucket bucket) {
441 assertTrue("Start timestamp " + bucket.getStartTimeStamp() + " is less than " +
442 mStartTime, bucket.getStartTimeStamp() >= mStartTime);
443 assertTrue("End timestamp " + bucket.getEndTimeStamp() + " is greater than " +
444 mEndTime, bucket.getEndTimeStamp() <= mEndTime);
445 }
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100446}