Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package android.app.usage.cts; |
| 18 | |
| 19 | import android.app.AppOpsManager; |
| 20 | import android.app.usage.NetworkStatsManager; |
| 21 | import android.app.usage.NetworkStats; |
| 22 | import android.content.Context; |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 24 | import android.net.ConnectivityManager; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 25 | import android.net.Network; |
| 26 | import android.net.NetworkCapabilities; |
| 27 | import android.net.NetworkInfo; |
| 28 | import android.net.NetworkRequest; |
| 29 | import android.os.ParcelFileDescriptor; |
| 30 | import android.os.Process; |
| 31 | import android.os.RemoteException; |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 32 | import android.telephony.TelephonyManager; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 33 | import android.test.InstrumentationTestCase; |
| 34 | import android.util.Log; |
| 35 | |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 36 | import java.io.FileInputStream; |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.io.InputStreamReader; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 40 | import java.net.URL; |
| 41 | import java.text.MessageFormat; |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 42 | import java.util.Scanner; |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 43 | import java.net.HttpURLConnection; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 44 | |
| 45 | import libcore.io.IoUtils; |
| 46 | import libcore.io.Streams; |
| 47 | |
| 48 | public class NetworkUsageStatsTest extends InstrumentationTestCase { |
| 49 | private static final String LOG_TAG = "NetworkUsageStatsTest"; |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 50 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 52 | |
| 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-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 60 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | private NetworkStatsManager mNsm; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 73 | private ConnectivityManager mCm; |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 74 | private PackageManager mPm; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 75 | private long mStartTime; |
| 76 | private long mEndTime; |
| 77 | |
| 78 | private long mBytesRead; |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 79 | private String mWriteSettingsMode; |
| 80 | private String mUsageStatsMode; |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 81 | |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 82 | private void exerciseRemoteHost(Network network) throws Exception { |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 83 | final int timeout = 15000; |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 84 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 94 | try { |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 95 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected void setUp() throws Exception { |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 131 | super.setUp(); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 132 | mNsm = (NetworkStatsManager) getInstrumentation().getContext() |
| 133 | .getSystemService(Context.NETWORK_STATS_SERVICE); |
| 134 | |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 135 | mCm = (ConnectivityManager) getInstrumentation().getContext() |
| 136 | .getSystemService(Context.CONNECTIVITY_SERVICE); |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 137 | |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 138 | mPm = getInstrumentation().getContext().getPackageManager(); |
| 139 | |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 140 | mWriteSettingsMode = getAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS); |
| 141 | setAppOpsMode(AppOpsManager.OPSTR_WRITE_SETTINGS, "allow"); |
| 142 | mUsageStatsMode = getAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 145 | @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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 157 | final String command = MessageFormat.format(APPOPS_SET_SHELL_COMMAND, |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 158 | getInstrumentation().getContext().getPackageName(), appop, mode); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 159 | 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-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 168 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 191 | private boolean shouldTestThisNetworkType(int networkTypeIndex, long tolerance) |
| 192 | throws Exception { |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 193 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 207 | } |
Zoltan Szatmary-Ban | 8ba9340 | 2015-09-18 12:28:58 +0100 | [diff] [blame] | 208 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 215 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 224 | public void testDeviceSummary() throws Exception { |
| 225 | for (int i = 0; i < sNetworkTypesToTest.length; ++i) { |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 226 | if (!shouldTestThisNetworkType(i, MINUTE/2)) { |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 227 | continue; |
| 228 | } |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 229 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 230 | NetworkStats.Bucket bucket = null; |
| 231 | try { |
| 232 | bucket = mNsm.querySummaryForDevice( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 233 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 234 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 235 | } catch (RemoteException | SecurityException e) { |
| 236 | fail("testDeviceSummary fails with exception: " + e.toString()); |
| 237 | } |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 238 | assertNotNull(bucket); |
| 239 | assertTimestamps(bucket); |
| 240 | assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL); |
| 241 | assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL); |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 242 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 243 | try { |
| 244 | bucket = mNsm.querySummaryForDevice( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 245 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 246 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 247 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 258 | if (!shouldTestThisNetworkType(i, MINUTE/2)) { |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 259 | continue; |
| 260 | } |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 261 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 262 | NetworkStats.Bucket bucket = null; |
| 263 | try { |
| 264 | bucket = mNsm.querySummaryForUser( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 265 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 266 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 267 | } catch (RemoteException | SecurityException e) { |
| 268 | fail("testUserSummary fails with exception: " + e.toString()); |
| 269 | } |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 270 | assertNotNull(bucket); |
| 271 | assertTimestamps(bucket); |
| 272 | assertEquals(bucket.getState(), NetworkStats.Bucket.STATE_ALL); |
| 273 | assertEquals(bucket.getUid(), NetworkStats.Bucket.UID_ALL); |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 274 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 275 | try { |
| 276 | bucket = mNsm.querySummaryForUser( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 277 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 278 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 279 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 290 | if (!shouldTestThisNetworkType(i, MINUTE/2)) { |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 291 | continue; |
| 292 | } |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 293 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 294 | NetworkStats result = null; |
| 295 | try { |
| 296 | result = mNsm.querySummary( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 297 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 298 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 299 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 305 | while (result.hasNextBucket()) { |
| 306 | assertTrue(result.getNextBucket(bucket)); |
| 307 | assertTimestamps(bucket); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 308 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 315 | assertFalse(result.getNextBucket(bucket)); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 316 | 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-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 327 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 328 | try { |
| 329 | result = mNsm.querySummary( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 330 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 331 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 332 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 343 | // Relatively large tolerance to accommodate for history bucket size. |
| 344 | if (!shouldTestThisNetworkType(i, MINUTE * 120)) { |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 345 | continue; |
| 346 | } |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 347 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 348 | NetworkStats result = null; |
| 349 | try { |
| 350 | result = mNsm.queryDetails( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 351 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 352 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 353 | assertTrue(result != null); |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 354 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 375 | } 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-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 382 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 383 | try { |
| 384 | result = mNsm.queryDetails( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 385 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 386 | mStartTime, mEndTime); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 387 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 398 | // Relatively large tolerance to accommodate for history bucket size. |
| 399 | if (!shouldTestThisNetworkType(i, MINUTE * 120)) { |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 400 | continue; |
| 401 | } |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 402 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "allow"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 403 | NetworkStats result = null; |
| 404 | try { |
| 405 | result = mNsm.queryDetailsForUid( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 406 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 407 | mStartTime, mEndTime, Process.myUid()); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 408 | assertTrue(result != null); |
Zoltan Szatmary-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 409 | 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 429 | } 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-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 436 | setAppOpsMode(AppOpsManager.OPSTR_GET_USAGE_STATS, "deny"); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 437 | try { |
| 438 | result = mNsm.queryDetailsForUid( |
Zoltan Szatmary-Ban | 7275592 | 2015-08-13 11:26:11 +0100 | [diff] [blame] | 439 | sNetworkTypesToTest[i], getSubscriberId(sNetworkTypesToTest[i]), |
| 440 | mStartTime, mEndTime, Process.myUid()); |
Zoltan Szatmary-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 441 | 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-Ban | 3f6dec1 | 2015-06-16 15:52:11 +0100 | [diff] [blame] | 449 | |
| 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-Ban | 8f00225 | 2015-05-06 09:33:58 +0100 | [diff] [blame] | 456 | } |