blob: a889c02e13b7c72fb9b075b2716885d20dd6ce01 [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-Ban8ba93402015-09-18 12:28:58 +010023import android.content.pm.PackageManager;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010024import android.net.ConnectivityManager;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010025import android.net.Network;
26import android.net.NetworkCapabilities;
27import android.net.NetworkInfo;
28import android.net.NetworkRequest;
29import android.os.ParcelFileDescriptor;
30import android.os.Process;
31import android.os.RemoteException;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010032import android.telephony.TelephonyManager;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010033import android.test.InstrumentationTestCase;
34import android.util.Log;
35
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010036import java.io.FileInputStream;
37import java.io.IOException;
38import java.io.InputStream;
39import java.io.InputStreamReader;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010040import java.net.URL;
41import java.text.MessageFormat;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010042import java.util.Scanner;
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010043import java.net.HttpURLConnection;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010044
45import libcore.io.IoUtils;
46import libcore.io.Streams;
47
48public class NetworkUsageStatsTest extends InstrumentationTestCase {
49 private static final String LOG_TAG = "NetworkUsageStatsTest";
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010050 private static final String APPOPS_SET_SHELL_COMMAND = "appops set {0} {1} {2}";
51 private static final String APPOPS_GET_SHELL_COMMAND = "appops get {0} {1}";
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010052
53 private static final long MINUTE = 1000 * 60;
54
55 private static final int[] sNetworkTypesToTest = new int[] {
56 ConnectivityManager.TYPE_WIFI,
57 ConnectivityManager.TYPE_MOBILE,
58 };
59
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010060 private static final String[] sSystemFeaturesToTest = new String[] {
61 PackageManager.FEATURE_WIFI,
62 PackageManager.FEATURE_TELEPHONY,
63 };
64
65 private static final String[] sFeatureNotConnectedCause = new String[] {
66 " Please make sure you are connected to a WiFi access point.",
67 " Please make sure you have added a SIM card with data plan to your phone, have enabled " +
68 "data over cellular and in case of dual SIM devices, have selected the right SIM " +
69 "for data connection."
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010070 };
71
72 private NetworkStatsManager mNsm;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010073 private ConnectivityManager mCm;
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010074 private PackageManager mPm;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010075 private long mStartTime;
76 private long mEndTime;
77
78 private long mBytesRead;
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +010079 private String mWriteSettingsMode;
80 private String mUsageStatsMode;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010081
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010082 private void exerciseRemoteHost(Network network) throws Exception {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +010083 final int timeout = 15000;
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010084 NetworkInfo networkInfo = mCm.getNetworkInfo(network);
85 if (networkInfo == null) {
86 Log.w(LOG_TAG, "Network info is null");
87 } else {
88 Log.w(LOG_TAG, "Network: " + networkInfo.toString());
89 }
90 InputStreamReader in = null;
91 HttpURLConnection urlc = null;
92 String originalKeepAlive = System.getProperty("http.keepAlive");
93 System.setProperty("http.keepAlive", "false");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +010094 try {
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +010095 urlc = (HttpURLConnection) network.openConnection(new URL(
96 "http://www.265.com/"));
97 urlc.setConnectTimeout(timeout);
98 urlc.setUseCaches(false);
99 urlc.connect();
100 boolean ping = urlc.getResponseCode() == 200;
101 if (ping) {
102 in = new InputStreamReader(
103 (InputStream) urlc.getContent());
104
105 mBytesRead = 0;
106 while (in.read() != -1) ++mBytesRead;
107 }
108 } catch (Exception e) {
109 Log.i(LOG_TAG, "Badness during exercising remote server: " + e);
110 } finally {
111 if (in != null) {
112 try {
113 in.close();
114 } catch (IOException e) {
115 // don't care
116 }
117 }
118 if (urlc != null) {
119 urlc.disconnect();
120 }
121 if (originalKeepAlive == null) {
122 System.clearProperty("http.keepAlive");
123 } else {
124 System.setProperty("http.keepAlive", originalKeepAlive);
125 }
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100126 }
127 }
128
129 @Override
130 protected void setUp() throws Exception {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100131 super.setUp();
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100132 mNsm = (NetworkStatsManager) getInstrumentation().getContext()
133 .getSystemService(Context.NETWORK_STATS_SERVICE);
134
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100135 mCm = (ConnectivityManager) getInstrumentation().getContext()
136 .getSystemService(Context.CONNECTIVITY_SERVICE);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100137
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +0100138 mPm = getInstrumentation().getContext().getPackageManager();
139
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100140 mWriteSettingsMode = getAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS);
141 setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, "allow");
142 mUsageStatsMode = getAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100143 }
144
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100145 @Override
146 protected void tearDown() throws Exception {
147 if (mWriteSettingsMode != null) {
148 setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, mWriteSettingsMode);
149 }
150 if (mUsageStatsMode != null) {
151 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, mUsageStatsMode);
152 }
153 super.tearDown();
154 }
155
156 private void setAppOpsMode(String appop, String mode) throws Exception {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100157 final String command = MessageFormat.format(APPOPS_SET_SHELL_COMMAND,
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100158 getInstrumentation().getContext().getPackageName(), appop, mode);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100159 ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
160 .executeShellCommand(command);
161 try {
162 Streams.readFully(new FileInputStream(pfd.getFileDescriptor()));
163 } finally {
164 IoUtils.closeQuietly(pfd.getFileDescriptor());
165 }
166 }
167
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100168 private String getAppOpsMode(String appop) throws Exception {
169 String result;
170 final String command = MessageFormat.format(APPOPS_GET_SHELL_COMMAND,
171 getInstrumentation().getContext().getPackageName(), appop);
172 ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
173 .executeShellCommand(command);
174 try {
175 result = convertStreamToString(new FileInputStream(pfd.getFileDescriptor()));
176 } finally {
177 IoUtils.closeQuietly(pfd.getFileDescriptor());
178 }
179 if (result == null) {
180 Log.w(LOG_TAG, "App op " + appop + " could not be read.");
181 }
182 return result;
183 }
184
185 private static String convertStreamToString(InputStream is) {
186 try (Scanner scanner = new Scanner(is).useDelimiter("\\A")) {
187 return scanner.hasNext() ? scanner.next() : null;
188 }
189 }
190
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100191 private boolean shouldTestThisNetworkType(int networkTypeIndex, long tolerance)
192 throws Exception {
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +0100193 Network[] networks = mCm.getAllNetworks();
194 for (Network network : networks) {
195 NetworkInfo networkInfo = mCm.getNetworkInfo(network);
196 if (networkInfo != null && networkInfo.isConnected() &&
197 networkInfo.getType() == sNetworkTypesToTest[networkTypeIndex]) {
198 NetworkCapabilities capabilities = mCm.getNetworkCapabilities(network);
199 if (capabilities != null && capabilities.hasCapability(
200 NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
201 mStartTime = System.currentTimeMillis() - tolerance;
202 exerciseRemoteHost(network);
203 mEndTime = System.currentTimeMillis() + tolerance;
204 return true;
205 }
206 }
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100207 }
Zoltan Szatmary-Ban8ba93402015-09-18 12:28:58 +0100208 assertFalse (sSystemFeaturesToTest[networkTypeIndex] + " is a reported system feature, " +
209 "however no corresponding connected network interface was found. " +
210 sFeatureNotConnectedCause[networkTypeIndex],
211 mPm.hasSystemFeature(sSystemFeaturesToTest[networkTypeIndex]));
212 return false;
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100213 }
214
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100215 private String getSubscriberId(int networkType) {
216 if (ConnectivityManager.TYPE_MOBILE == networkType) {
217 TelephonyManager tm = (TelephonyManager) getInstrumentation().getContext()
218 .getSystemService(Context.TELEPHONY_SERVICE);
219 return tm.getSubscriberId();
220 }
221 return "";
222 }
223
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100224 public void testDeviceSummary() throws Exception {
225 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100226 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100227 continue;
228 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100229 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100230 NetworkStats.Bucket bucket = null;
231 try {
232 bucket = mNsm.querySummaryForDevice(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100233 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
234 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100235 } catch (RemoteException | SecurityException e) {
236 fail("testDeviceSummary fails with exception: " + e.toString());
237 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100238 assertNotNull(bucket);
239 assertTimestamps(bucket);
240 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
241 assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100242 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100243 try {
244 bucket = mNsm.querySummaryForDevice(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100245 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
246 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100247 fail("negative testDeviceSummary fails: no exception thrown.");
248 } catch (RemoteException e) {
249 fail("testDeviceSummary fails with exception: " + e.toString());
250 } catch (SecurityException e) {
251 // expected outcome
252 }
253 }
254 }
255
256 public void testUserSummary() throws Exception {
257 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100258 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100259 continue;
260 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100261 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100262 NetworkStats.Bucket bucket = null;
263 try {
264 bucket = mNsm.querySummaryForUser(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100265 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
266 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100267 } catch (RemoteException | SecurityException e) {
268 fail("testUserSummary fails with exception: " + e.toString());
269 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100270 assertNotNull(bucket);
271 assertTimestamps(bucket);
272 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
273 assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL);
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100274 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100275 try {
276 bucket = mNsm.querySummaryForUser(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100277 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
278 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100279 fail("negative testUserSummary fails: no exception thrown.");
280 } catch (RemoteException e) {
281 fail("testUserSummary fails with exception: " + e.toString());
282 } catch (SecurityException e) {
283 // expected outcome
284 }
285 }
286 }
287
288 public void testAppSummary() throws Exception {
289 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100290 if (!shouldTestThisNetworkType(i, MINUTE/2)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100291 continue;
292 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100293 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100294 NetworkStats result = null;
295 try {
296 result = mNsm.querySummary(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100297 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
298 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100299 assertTrue(result != null);
300 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
301 long totalTxPackets = 0;
302 long totalRxPackets = 0;
303 long totalTxBytes = 0;
304 long totalRxBytes = 0;
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100305 while (result.hasNextBucket()) {
306 assertTrue(result.getNextBucket(bucket));
307 assertTimestamps(bucket);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100308 if (bucket.getUid() == Process.myUid()) {
309 totalTxPackets += bucket.getTxPackets();
310 totalRxPackets += bucket.getRxPackets();
311 totalTxBytes += bucket.getTxBytes();
312 totalRxBytes += bucket.getRxBytes();
313 }
314 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100315 assertFalse(result.getNextBucket(bucket));
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100316 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
317 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
318 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
319 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
320 } catch (RemoteException | SecurityException e) {
321 fail("testAppSummary fails with exception: " + e.toString());
322 } finally {
323 if (result != null) {
324 result.close();
325 }
326 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100327 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100328 try {
329 result = mNsm.querySummary(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100330 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
331 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100332 fail("negative testAppSummary fails: no exception thrown.");
333 } catch (RemoteException e) {
334 fail("testAppSummary fails with exception: " + e.toString());
335 } catch (SecurityException e) {
336 // expected outcome
337 }
338 }
339 }
340
341 public void testAppDetails() throws Exception {
342 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100343 // Relatively large tolerance to accommodate for history bucket size.
344 if (!shouldTestThisNetworkType(i, MINUTE * 120)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100345 continue;
346 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100347 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100348 NetworkStats result = null;
349 try {
350 result = mNsm.queryDetails(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100351 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
352 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100353 assertTrue(result != null);
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100354 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
355 long totalTxPackets = 0;
356 long totalRxPackets = 0;
357 long totalTxBytes = 0;
358 long totalRxBytes = 0;
359 while (result.hasNextBucket()) {
360 assertTrue(result.getNextBucket(bucket));
361 assertTimestamps(bucket);
362 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
363 if (bucket.getUid() == Process.myUid()) {
364 totalTxPackets += bucket.getTxPackets();
365 totalRxPackets += bucket.getRxPackets();
366 totalTxBytes += bucket.getTxBytes();
367 totalRxBytes += bucket.getRxBytes();
368 }
369 }
370 assertFalse(result.getNextBucket(bucket));
371 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
372 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
373 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
374 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100375 } catch (RemoteException | SecurityException e) {
376 fail("testAppDetails fails with exception: " + e.toString());
377 } finally {
378 if (result != null) {
379 result.close();
380 }
381 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100382 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100383 try {
384 result = mNsm.queryDetails(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100385 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
386 mStartTime, mEndTime);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100387 fail("negative testAppDetails fails: no exception thrown.");
388 } catch (RemoteException e) {
389 fail("testAppDetails fails with exception: " + e.toString());
390 } catch (SecurityException e) {
391 // expected outcome
392 }
393 }
394 }
395
396 public void testUidDetails() throws Exception {
397 for (int i = 0; i < sNetworkTypesToTest.length; ++i) {
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100398 // Relatively large tolerance to accommodate for history bucket size.
399 if (!shouldTestThisNetworkType(i, MINUTE * 120)) {
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100400 continue;
401 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100402 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100403 NetworkStats result = null;
404 try {
405 result = mNsm.queryDetailsForUid(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100406 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
407 mStartTime, mEndTime, Process.myUid());
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100408 assertTrue(result != null);
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100409 NetworkStats.Bucket bucket = new NetworkStats.Bucket();
410 long totalTxPackets = 0;
411 long totalRxPackets = 0;
412 long totalTxBytes = 0;
413 long totalRxBytes = 0;
414 while (result.hasNextBucket()) {
415 assertTrue(result.getNextBucket(bucket));
416 assertTimestamps(bucket);
417 assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL);
418 assertEquals(bucket.getUid(), Process.myUid());
419 totalTxPackets += bucket.getTxPackets();
420 totalRxPackets += bucket.getRxPackets();
421 totalTxBytes += bucket.getTxBytes();
422 totalRxBytes += bucket.getRxBytes();
423 }
424 assertFalse(result.getNextBucket(bucket));
425 assertTrue("No Rx bytes usage for uid " + Process.myUid(), totalRxBytes > 0);
426 assertTrue("No Rx packets usage for uid " + Process.myUid(), totalRxPackets > 0);
427 assertTrue("No Tx bytes usage for uid " + Process.myUid(), totalTxBytes > 0);
428 assertTrue("No Tx packets usage for uid " + Process.myUid(), totalTxPackets > 0);
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100429 } catch (RemoteException | SecurityException e) {
430 fail("testUidDetails fails with exception: " + e.toString());
431 } finally {
432 if (result != null) {
433 result.close();
434 }
435 }
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100436 setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny");
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100437 try {
438 result = mNsm.queryDetailsForUid(
Zoltan Szatmary-Ban72755922015-08-13 11:26:11 +0100439 sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]),
440 mStartTime, mEndTime, Process.myUid());
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100441 fail("negative testUidDetails fails: no exception thrown.");
442 } catch (RemoteException e) {
443 fail("testUidDetails fails with exception: " + e.toString());
444 } catch (SecurityException e) {
445 // expected outcome
446 }
447 }
448 }
Zoltan Szatmary-Ban3f6dec12015-06-16 15:52:11 +0100449
450 private void assertTimestamps(final NetworkStats.Bucket bucket) {
451 assertTrue("Start timestamp " + bucket.getStartTimeStamp() + " is less than " +
452 mStartTime, bucket.getStartTimeStamp() >= mStartTime);
453 assertTrue("End timestamp " + bucket.getEndTimeStamp() + " is greater than " +
454 mEndTime, bucket.getEndTimeStamp() <= mEndTime);
455 }
Zoltan Szatmary-Ban8f002252015-05-06 09:33:58 +0100456}