NetworkStatsServiceTest.java revision 0ca981fe42c43a4b7c345f4a0a2b3b2a519be5da
1/* 2 * Copyright (C) 2011 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 17package com.android.server; 18 19import static android.content.Intent.ACTION_UID_REMOVED; 20import static android.content.Intent.EXTRA_UID; 21import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; 22import static android.net.ConnectivityManager.TYPE_MOBILE; 23import static android.net.ConnectivityManager.TYPE_WIFI; 24import static android.net.ConnectivityManager.TYPE_WIMAX; 25import static android.net.NetworkStats.IFACE_ALL; 26import static android.net.NetworkStats.SET_ALL; 27import static android.net.NetworkStats.SET_DEFAULT; 28import static android.net.NetworkStats.SET_FOREGROUND; 29import static android.net.NetworkStats.TAG_NONE; 30import static android.net.NetworkStats.UID_ALL; 31import static android.net.NetworkStatsHistory.FIELD_ALL; 32import static android.net.NetworkTemplate.buildTemplateMobileAll; 33import static android.net.NetworkTemplate.buildTemplateWifiWildcard; 34import static android.net.TrafficStats.MB_IN_BYTES; 35import static android.net.TrafficStats.UID_REMOVED; 36import static android.net.TrafficStats.UID_TETHERING; 37import static android.text.format.DateUtils.DAY_IN_MILLIS; 38import static android.text.format.DateUtils.HOUR_IN_MILLIS; 39import static android.text.format.DateUtils.MINUTE_IN_MILLIS; 40import static android.text.format.DateUtils.WEEK_IN_MILLIS; 41import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL; 42import static org.easymock.EasyMock.anyLong; 43import static org.easymock.EasyMock.anyObject; 44import static org.easymock.EasyMock.aryEq; 45import static org.easymock.EasyMock.capture; 46import static org.easymock.EasyMock.createMock; 47import static org.easymock.EasyMock.eq; 48import static org.easymock.EasyMock.expect; 49import static org.easymock.EasyMock.expectLastCall; 50import static org.easymock.EasyMock.isA; 51 52import android.app.AlarmManager; 53import android.app.IAlarmManager; 54import android.app.PendingIntent; 55import android.content.Intent; 56import android.net.IConnectivityManager; 57import android.net.INetworkManagementEventObserver; 58import android.net.INetworkStatsSession; 59import android.net.LinkProperties; 60import android.net.NetworkInfo; 61import android.net.NetworkInfo.DetailedState; 62import android.net.NetworkState; 63import android.net.NetworkStats; 64import android.net.NetworkStatsHistory; 65import android.net.NetworkTemplate; 66import android.os.INetworkManagementService; 67import android.os.WorkSource; 68import android.telephony.TelephonyManager; 69import android.test.AndroidTestCase; 70import android.test.suitebuilder.annotation.LargeTest; 71import android.test.suitebuilder.annotation.Suppress; 72import android.util.TrustedTime; 73 74import com.android.server.net.NetworkStatsService; 75import com.android.server.net.NetworkStatsService.NetworkStatsSettings; 76import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config; 77 78import org.easymock.Capture; 79import org.easymock.EasyMock; 80 81import java.io.File; 82 83import libcore.io.IoUtils; 84 85/** 86 * Tests for {@link NetworkStatsService}. 87 */ 88@LargeTest 89public class NetworkStatsServiceTest extends AndroidTestCase { 90 private static final String TAG = "NetworkStatsServiceTest"; 91 92 private static final String TEST_IFACE = "test0"; 93 private static final String TEST_IFACE2 = "test1"; 94 private static final long TEST_START = 1194220800000L; 95 96 private static final String IMSI_1 = "310004"; 97 private static final String IMSI_2 = "310260"; 98 private static final String TEST_SSID = "AndroidAP"; 99 100 private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard(); 101 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1); 102 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2); 103 104 private static final int UID_RED = 1001; 105 private static final int UID_BLUE = 1002; 106 private static final int UID_GREEN = 1003; 107 108 private long mElapsedRealtime; 109 110 private BroadcastInterceptingContext mServiceContext; 111 private File mStatsDir; 112 113 private INetworkManagementService mNetManager; 114 private IAlarmManager mAlarmManager; 115 private TrustedTime mTime; 116 private NetworkStatsSettings mSettings; 117 private IConnectivityManager mConnManager; 118 119 private NetworkStatsService mService; 120 private INetworkStatsSession mSession; 121 private INetworkManagementEventObserver mNetworkObserver; 122 123 @Override 124 public void setUp() throws Exception { 125 super.setUp(); 126 127 mServiceContext = new BroadcastInterceptingContext(getContext()); 128 mStatsDir = getContext().getFilesDir(); 129 if (mStatsDir.exists()) { 130 IoUtils.deleteContents(mStatsDir); 131 } 132 133 mNetManager = createMock(INetworkManagementService.class); 134 mAlarmManager = createMock(IAlarmManager.class); 135 mTime = createMock(TrustedTime.class); 136 mSettings = createMock(NetworkStatsSettings.class); 137 mConnManager = createMock(IConnectivityManager.class); 138 139 mService = new NetworkStatsService( 140 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings); 141 mService.bindConnectivityManager(mConnManager); 142 143 mElapsedRealtime = 0L; 144 145 expectCurrentTime(); 146 expectDefaultSettings(); 147 expectNetworkStatsSummary(buildEmptyStats()); 148 expectNetworkStatsUidDetail(buildEmptyStats()); 149 expectSystemReady(); 150 151 // catch INetworkManagementEventObserver during systemReady() 152 final Capture<INetworkManagementEventObserver> networkObserver = new Capture< 153 INetworkManagementEventObserver>(); 154 mNetManager.registerObserver(capture(networkObserver)); 155 expectLastCall().atLeastOnce(); 156 157 replay(); 158 mService.systemReady(); 159 mSession = mService.openSession(); 160 verifyAndReset(); 161 162 mNetworkObserver = networkObserver.getValue(); 163 164 } 165 166 @Override 167 public void tearDown() throws Exception { 168 IoUtils.deleteContents(mStatsDir); 169 170 mServiceContext = null; 171 mStatsDir = null; 172 173 mNetManager = null; 174 mAlarmManager = null; 175 mTime = null; 176 mSettings = null; 177 mConnManager = null; 178 179 mSession.close(); 180 mService = null; 181 182 super.tearDown(); 183 } 184 185 public void testNetworkStatsWifi() throws Exception { 186 // pretend that wifi network comes online; service should ask about full 187 // network state, and poll any existing interfaces before updating. 188 expectCurrentTime(); 189 expectDefaultSettings(); 190 expectNetworkState(buildWifiState()); 191 expectNetworkStatsSummary(buildEmptyStats()); 192 expectNetworkStatsUidDetail(buildEmptyStats()); 193 expectNetworkStatsPoll(); 194 195 replay(); 196 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 197 198 // verify service has empty history for wifi 199 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); 200 verifyAndReset(); 201 202 // modify some number on wifi, and trigger poll event 203 incrementCurrentTime(HOUR_IN_MILLIS); 204 expectCurrentTime(); 205 expectDefaultSettings(); 206 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 207 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L)); 208 expectNetworkStatsUidDetail(buildEmptyStats()); 209 expectNetworkStatsPoll(); 210 211 replay(); 212 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 213 214 // verify service recorded history 215 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0); 216 verifyAndReset(); 217 218 // and bump forward again, with counters going higher. this is 219 // important, since polling should correctly subtract last snapshot. 220 incrementCurrentTime(DAY_IN_MILLIS); 221 expectCurrentTime(); 222 expectDefaultSettings(); 223 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 224 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L)); 225 expectNetworkStatsUidDetail(buildEmptyStats()); 226 expectNetworkStatsPoll(); 227 228 replay(); 229 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 230 231 // verify service recorded history 232 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0); 233 verifyAndReset(); 234 235 } 236 237 public void testStatsRebootPersist() throws Exception { 238 assertStatsFilesExist(false); 239 240 // pretend that wifi network comes online; service should ask about full 241 // network state, and poll any existing interfaces before updating. 242 expectCurrentTime(); 243 expectDefaultSettings(); 244 expectNetworkState(buildWifiState()); 245 expectNetworkStatsSummary(buildEmptyStats()); 246 expectNetworkStatsUidDetail(buildEmptyStats()); 247 expectNetworkStatsPoll(); 248 249 replay(); 250 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 251 252 // verify service has empty history for wifi 253 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); 254 verifyAndReset(); 255 256 // modify some number on wifi, and trigger poll event 257 incrementCurrentTime(HOUR_IN_MILLIS); 258 expectCurrentTime(); 259 expectDefaultSettings(); 260 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 261 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L)); 262 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2) 263 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L) 264 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L) 265 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L) 266 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L) 267 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L)); 268 expectNetworkStatsPoll(); 269 270 mService.setUidForeground(UID_RED, false); 271 mService.incrementOperationCount(UID_RED, 0xFAAD, 4); 272 mService.setUidForeground(UID_RED, true); 273 mService.incrementOperationCount(UID_RED, 0xFAAD, 6); 274 275 replay(); 276 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 277 278 // verify service recorded history 279 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0); 280 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10); 281 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4); 282 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6); 283 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0); 284 verifyAndReset(); 285 286 // graceful shutdown system, which should trigger persist of stats, and 287 // clear any values in memory. 288 expectCurrentTime(); 289 expectDefaultSettings(); 290 replay(); 291 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN)); 292 verifyAndReset(); 293 294 assertStatsFilesExist(true); 295 296 // boot through serviceReady() again 297 expectCurrentTime(); 298 expectDefaultSettings(); 299 expectNetworkStatsSummary(buildEmptyStats()); 300 expectNetworkStatsUidDetail(buildEmptyStats()); 301 expectSystemReady(); 302 303 // catch INetworkManagementEventObserver during systemReady() 304 final Capture<INetworkManagementEventObserver> networkObserver = new Capture< 305 INetworkManagementEventObserver>(); 306 mNetManager.registerObserver(capture(networkObserver)); 307 expectLastCall().atLeastOnce(); 308 309 replay(); 310 mService.systemReady(); 311 312 mNetworkObserver = networkObserver.getValue(); 313 314 // after systemReady(), we should have historical stats loaded again 315 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0); 316 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10); 317 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4); 318 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6); 319 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0); 320 verifyAndReset(); 321 322 } 323 324 // TODO: simulate reboot to test bucket resize 325 @Suppress 326 public void testStatsBucketResize() throws Exception { 327 NetworkStatsHistory history = null; 328 329 assertStatsFilesExist(false); 330 331 // pretend that wifi network comes online; service should ask about full 332 // network state, and poll any existing interfaces before updating. 333 expectCurrentTime(); 334 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); 335 expectNetworkState(buildWifiState()); 336 expectNetworkStatsSummary(buildEmptyStats()); 337 expectNetworkStatsUidDetail(buildEmptyStats()); 338 expectNetworkStatsPoll(); 339 340 replay(); 341 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 342 verifyAndReset(); 343 344 // modify some number on wifi, and trigger poll event 345 incrementCurrentTime(2 * HOUR_IN_MILLIS); 346 expectCurrentTime(); 347 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); 348 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 349 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L)); 350 expectNetworkStatsUidDetail(buildEmptyStats()); 351 expectNetworkStatsPoll(); 352 353 replay(); 354 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 355 356 // verify service recorded history 357 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL); 358 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0); 359 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration()); 360 assertEquals(2, history.size()); 361 verifyAndReset(); 362 363 // now change bucket duration setting and trigger another poll with 364 // exact same values, which should resize existing buckets. 365 expectCurrentTime(); 366 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS); 367 expectNetworkStatsSummary(buildEmptyStats()); 368 expectNetworkStatsUidDetail(buildEmptyStats()); 369 expectNetworkStatsPoll(); 370 371 replay(); 372 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 373 374 // verify identical stats, but spread across 4 buckets now 375 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL); 376 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0); 377 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration()); 378 assertEquals(4, history.size()); 379 verifyAndReset(); 380 381 } 382 383 public void testUidStatsAcrossNetworks() throws Exception { 384 // pretend first mobile network comes online 385 expectCurrentTime(); 386 expectDefaultSettings(); 387 expectNetworkState(buildMobile3gState(IMSI_1)); 388 expectNetworkStatsSummary(buildEmptyStats()); 389 expectNetworkStatsUidDetail(buildEmptyStats()); 390 expectNetworkStatsPoll(); 391 392 replay(); 393 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 394 verifyAndReset(); 395 396 // create some traffic on first network 397 incrementCurrentTime(HOUR_IN_MILLIS); 398 expectCurrentTime(); 399 expectDefaultSettings(); 400 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 401 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L)); 402 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) 403 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L) 404 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L) 405 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L)); 406 expectNetworkStatsPoll(); 407 408 mService.incrementOperationCount(UID_RED, 0xF00D, 10); 409 410 replay(); 411 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 412 413 // verify service recorded history 414 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0); 415 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); 416 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10); 417 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0); 418 verifyAndReset(); 419 420 // now switch networks; this also tests that we're okay with interfaces 421 // disappearing, to verify we don't count backwards. 422 incrementCurrentTime(HOUR_IN_MILLIS); 423 expectCurrentTime(); 424 expectDefaultSettings(); 425 expectNetworkState(buildMobile3gState(IMSI_2)); 426 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 427 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L)); 428 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) 429 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L) 430 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L) 431 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L)); 432 expectNetworkStatsPoll(); 433 434 replay(); 435 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 436 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 437 verifyAndReset(); 438 439 // create traffic on second network 440 incrementCurrentTime(HOUR_IN_MILLIS); 441 expectCurrentTime(); 442 expectDefaultSettings(); 443 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 444 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L)); 445 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 446 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L) 447 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L) 448 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L) 449 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L)); 450 expectNetworkStatsPoll(); 451 452 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10); 453 454 replay(); 455 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 456 457 // verify original history still intact 458 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0); 459 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10); 460 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0); 461 462 // and verify new history also recorded under different template, which 463 // verifies that we didn't cross the streams. 464 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0); 465 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); 466 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10); 467 verifyAndReset(); 468 469 } 470 471 public void testUidRemovedIsMoved() throws Exception { 472 // pretend that network comes online 473 expectCurrentTime(); 474 expectDefaultSettings(); 475 expectNetworkState(buildWifiState()); 476 expectNetworkStatsSummary(buildEmptyStats()); 477 expectNetworkStatsUidDetail(buildEmptyStats()); 478 expectNetworkStatsPoll(); 479 480 replay(); 481 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 482 verifyAndReset(); 483 484 // create some traffic 485 incrementCurrentTime(HOUR_IN_MILLIS); 486 expectCurrentTime(); 487 expectDefaultSettings(); 488 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 489 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L)); 490 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 491 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L) 492 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L) 493 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L) 494 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)); 495 expectNetworkStatsPoll(); 496 497 mService.incrementOperationCount(UID_RED, 0xFAAD, 10); 498 499 replay(); 500 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 501 502 // verify service recorded history 503 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0); 504 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10); 505 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0); 506 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0); 507 verifyAndReset(); 508 509 // now pretend two UIDs are uninstalled, which should migrate stats to 510 // special "removed" bucket. 511 expectCurrentTime(); 512 expectDefaultSettings(); 513 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 514 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L)); 515 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 516 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L) 517 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L) 518 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L) 519 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)); 520 expectNetworkStatsPoll(); 521 522 replay(); 523 final Intent intent = new Intent(ACTION_UID_REMOVED); 524 intent.putExtra(EXTRA_UID, UID_BLUE); 525 mServiceContext.sendBroadcast(intent); 526 intent.putExtra(EXTRA_UID, UID_RED); 527 mServiceContext.sendBroadcast(intent); 528 529 // existing uid and total should remain unchanged; but removed UID 530 // should be gone completely. 531 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0); 532 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0); 533 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0); 534 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0); 535 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10); 536 verifyAndReset(); 537 538 } 539 540 public void testUid3g4gCombinedByTemplate() throws Exception { 541 // pretend that network comes online 542 expectCurrentTime(); 543 expectDefaultSettings(); 544 expectNetworkState(buildMobile3gState(IMSI_1)); 545 expectNetworkStatsSummary(buildEmptyStats()); 546 expectNetworkStatsUidDetail(buildEmptyStats()); 547 expectNetworkStatsPoll(); 548 549 replay(); 550 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 551 verifyAndReset(); 552 553 // create some traffic 554 incrementCurrentTime(HOUR_IN_MILLIS); 555 expectCurrentTime(); 556 expectDefaultSettings(); 557 expectNetworkStatsSummary(buildEmptyStats()); 558 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 559 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L) 560 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)); 561 expectNetworkStatsPoll(); 562 563 mService.incrementOperationCount(UID_RED, 0xF00D, 5); 564 565 replay(); 566 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 567 568 // verify service recorded history 569 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5); 570 verifyAndReset(); 571 572 // now switch over to 4g network 573 incrementCurrentTime(HOUR_IN_MILLIS); 574 expectCurrentTime(); 575 expectDefaultSettings(); 576 expectNetworkState(buildMobile4gState(TEST_IFACE2)); 577 expectNetworkStatsSummary(buildEmptyStats()); 578 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 579 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L) 580 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)); 581 expectNetworkStatsPoll(); 582 583 replay(); 584 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 585 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 586 verifyAndReset(); 587 588 // create traffic on second network 589 incrementCurrentTime(HOUR_IN_MILLIS); 590 expectCurrentTime(); 591 expectDefaultSettings(); 592 expectNetworkStatsSummary(buildEmptyStats()); 593 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 594 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L) 595 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L) 596 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L) 597 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L)); 598 expectNetworkStatsPoll(); 599 600 mService.incrementOperationCount(UID_RED, 0xFAAD, 5); 601 602 replay(); 603 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 604 605 // verify that ALL_MOBILE template combines both 606 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10); 607 608 verifyAndReset(); 609 } 610 611 public void testSummaryForAllUid() throws Exception { 612 // pretend that network comes online 613 expectCurrentTime(); 614 expectDefaultSettings(); 615 expectNetworkState(buildWifiState()); 616 expectNetworkStatsSummary(buildEmptyStats()); 617 expectNetworkStatsUidDetail(buildEmptyStats()); 618 expectNetworkStatsPoll(); 619 620 replay(); 621 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 622 verifyAndReset(); 623 624 // create some traffic for two apps 625 incrementCurrentTime(HOUR_IN_MILLIS); 626 expectCurrentTime(); 627 expectDefaultSettings(); 628 expectNetworkStatsSummary(buildEmptyStats()); 629 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 630 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L) 631 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L) 632 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L)); 633 expectNetworkStatsPoll(); 634 635 mService.incrementOperationCount(UID_RED, 0xF00D, 1); 636 637 replay(); 638 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 639 640 // verify service recorded history 641 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1); 642 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0); 643 verifyAndReset(); 644 645 // now create more traffic in next hour, but only for one app 646 incrementCurrentTime(HOUR_IN_MILLIS); 647 expectCurrentTime(); 648 expectDefaultSettings(); 649 expectNetworkStatsSummary(buildEmptyStats()); 650 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 651 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L) 652 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L) 653 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L)); 654 expectNetworkStatsPoll(); 655 656 replay(); 657 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 658 659 // first verify entire history present 660 NetworkStats stats = mSession.getSummaryForAllUid( 661 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); 662 assertEquals(3, stats.size()); 663 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1); 664 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1); 665 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0); 666 667 // now verify that recent history only contains one uid 668 final long currentTime = currentTimeMillis(); 669 stats = mSession.getSummaryForAllUid( 670 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true); 671 assertEquals(1, stats.size()); 672 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0); 673 674 verifyAndReset(); 675 } 676 677 public void testForegroundBackground() throws Exception { 678 // pretend that network comes online 679 expectCurrentTime(); 680 expectDefaultSettings(); 681 expectNetworkState(buildWifiState()); 682 expectNetworkStatsSummary(buildEmptyStats()); 683 expectNetworkStatsUidDetail(buildEmptyStats()); 684 expectNetworkStatsPoll(); 685 686 replay(); 687 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 688 verifyAndReset(); 689 690 // create some initial traffic 691 incrementCurrentTime(HOUR_IN_MILLIS); 692 expectCurrentTime(); 693 expectDefaultSettings(); 694 expectNetworkStatsSummary(buildEmptyStats()); 695 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 696 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L) 697 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)); 698 expectNetworkStatsPoll(); 699 700 mService.incrementOperationCount(UID_RED, 0xF00D, 1); 701 702 replay(); 703 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 704 705 // verify service recorded history 706 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1); 707 verifyAndReset(); 708 709 // now switch to foreground 710 incrementCurrentTime(HOUR_IN_MILLIS); 711 expectCurrentTime(); 712 expectDefaultSettings(); 713 expectNetworkStatsSummary(buildEmptyStats()); 714 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) 715 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L) 716 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L) 717 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L) 718 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L)); 719 expectNetworkStatsPoll(); 720 721 mService.setUidForeground(UID_RED, true); 722 mService.incrementOperationCount(UID_RED, 0xFAAD, 1); 723 724 replay(); 725 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 726 727 // test that we combined correctly 728 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2); 729 730 // verify entire history present 731 final NetworkStats stats = mSession.getSummaryForAllUid( 732 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); 733 assertEquals(4, stats.size()); 734 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1); 735 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1); 736 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1); 737 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1); 738 739 verifyAndReset(); 740 } 741 742 public void testTethering() throws Exception { 743 // pretend first mobile network comes online 744 expectCurrentTime(); 745 expectDefaultSettings(); 746 expectNetworkState(buildMobile3gState(IMSI_1)); 747 expectNetworkStatsSummary(buildEmptyStats()); 748 expectNetworkStatsUidDetail(buildEmptyStats()); 749 expectNetworkStatsPoll(); 750 751 replay(); 752 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 753 verifyAndReset(); 754 755 // create some tethering traffic 756 incrementCurrentTime(HOUR_IN_MILLIS); 757 expectCurrentTime(); 758 expectDefaultSettings(); 759 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) 760 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L)); 761 762 final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1) 763 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L); 764 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" }; 765 final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1) 766 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L); 767 768 expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats); 769 expectNetworkStatsPoll(); 770 771 replay(); 772 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 773 774 // verify service recorded history 775 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0); 776 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0); 777 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0); 778 verifyAndReset(); 779 780 } 781 782 public void testReportXtOverDev() throws Exception { 783 // bring mobile network online 784 expectCurrentTime(); 785 expectDefaultSettings(); 786 expectNetworkState(buildMobile3gState(IMSI_1)); 787 expectNetworkStatsSummary(buildEmptyStats()); 788 expectNetworkStatsUidDetail(buildEmptyStats()); 789 expectNetworkStatsPoll(); 790 791 replay(); 792 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); 793 verifyAndReset(); 794 795 // create some traffic, but only for DEV, and across 1.5 buckets 796 incrementCurrentTime(90 * MINUTE_IN_MILLIS); 797 expectCurrentTime(); 798 expectDefaultSettings(); 799 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1) 800 .addIfaceValues(TEST_IFACE, 6000L, 60L, 3000L, 30L)); 801 expectNetworkStatsSummaryXt(buildEmptyStats()); 802 expectNetworkStatsUidDetail(buildEmptyStats()); 803 expectNetworkStatsPoll(); 804 805 replay(); 806 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 807 808 // verify service recorded history: 809 // 4000(dev) + 2000(dev) 810 assertNetworkTotal(sTemplateImsi1, 6000L, 60L, 3000L, 30L, 0); 811 verifyAndReset(); 812 813 // create traffic on both DEV and XT, across two buckets 814 incrementCurrentTime(2 * HOUR_IN_MILLIS); 815 expectCurrentTime(); 816 expectDefaultSettings(); 817 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1) 818 .addIfaceValues(TEST_IFACE, 6004L, 64L, 3004L, 34L)); 819 expectNetworkStatsSummaryXt(new NetworkStats(getElapsedRealtime(), 1) 820 .addIfaceValues(TEST_IFACE, 10240L, 0L, 0L, 0L)); 821 expectNetworkStatsUidDetail(buildEmptyStats()); 822 expectNetworkStatsPoll(); 823 824 replay(); 825 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); 826 827 // verify that we switching reporting at the first atomic XT bucket, 828 // which should give us: 829 // 4000(dev) + 2000(dev) + 1(dev) + 5120(xt) + 2560(xt) 830 assertNetworkTotal(sTemplateImsi1, 13681L, 61L, 3001L, 31L, 0); 831 832 // also test pure-DEV and pure-XT ranges 833 assertNetworkTotal(sTemplateImsi1, startTimeMillis(), 834 startTimeMillis() + 2 * HOUR_IN_MILLIS, 6001L, 61L, 3001L, 31L, 0); 835 assertNetworkTotal(sTemplateImsi1, startTimeMillis() + 2 * HOUR_IN_MILLIS, 836 startTimeMillis() + 4 * HOUR_IN_MILLIS, 7680L, 0L, 0L, 0L, 0); 837 838 verifyAndReset(); 839 } 840 841 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets, 842 long txBytes, long txPackets, int operations) throws Exception { 843 assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes, 844 txPackets, operations); 845 } 846 847 private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes, 848 long rxPackets, long txBytes, long txPackets, int operations) throws Exception { 849 // verify history API 850 final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL); 851 assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations); 852 853 // verify summary API 854 final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end); 855 assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes, 856 txPackets, operations); 857 } 858 859 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets, 860 long txBytes, long txPackets, int operations) throws Exception { 861 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations); 862 } 863 864 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes, 865 long rxPackets, long txBytes, long txPackets, int operations) throws Exception { 866 // verify history API 867 final NetworkStatsHistory history = mSession.getHistoryForUid( 868 template, uid, set, TAG_NONE, FIELD_ALL); 869 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes, 870 txPackets, operations); 871 872 // verify summary API 873 final NetworkStats stats = mSession.getSummaryForAllUid( 874 template, Long.MIN_VALUE, Long.MAX_VALUE, false); 875 assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets, 876 operations); 877 } 878 879 private void expectSystemReady() throws Exception { 880 mAlarmManager.remove(isA(PendingIntent.class)); 881 expectLastCall().anyTimes(); 882 883 mAlarmManager.set(eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), anyLong(), 884 isA(PendingIntent.class), anyObject(WorkSource.class)); 885 expectLastCall().atLeastOnce(); 886 887 mNetManager.setGlobalAlert(anyLong()); 888 expectLastCall().atLeastOnce(); 889 890 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce(); 891 } 892 893 private void expectNetworkState(NetworkState... state) throws Exception { 894 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); 895 896 final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null; 897 expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce(); 898 } 899 900 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception { 901 expectNetworkStatsSummaryDev(summary); 902 expectNetworkStatsSummaryXt(summary); 903 } 904 905 private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception { 906 expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce(); 907 } 908 909 private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception { 910 expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce(); 911 } 912 913 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception { 914 expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0)); 915 } 916 917 private void expectNetworkStatsUidDetail( 918 NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats) 919 throws Exception { 920 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce(); 921 922 // also include tethering details, since they are folded into UID 923 expect(mConnManager.getTetheredIfacePairs()).andReturn(tetherIfacePairs).atLeastOnce(); 924 expect(mNetManager.getNetworkStatsTethering(aryEq(tetherIfacePairs))) 925 .andReturn(tetherStats).atLeastOnce(); 926 } 927 928 private void expectDefaultSettings() throws Exception { 929 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); 930 } 931 932 private void expectSettings(long persistBytes, long bucketDuration, long deleteAge) 933 throws Exception { 934 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes(); 935 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes(); 936 expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes(); 937 expect(mSettings.getReportXtOverDev()).andReturn(true).anyTimes(); 938 939 final Config config = new Config(bucketDuration, deleteAge, deleteAge); 940 expect(mSettings.getDevConfig()).andReturn(config).anyTimes(); 941 expect(mSettings.getXtConfig()).andReturn(config).anyTimes(); 942 expect(mSettings.getUidConfig()).andReturn(config).anyTimes(); 943 expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes(); 944 945 expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes(); 946 expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes(); 947 expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes(); 948 expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes(); 949 expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes(); 950 } 951 952 private void expectCurrentTime() throws Exception { 953 expect(mTime.forceRefresh()).andReturn(false).anyTimes(); 954 expect(mTime.hasCache()).andReturn(true).anyTimes(); 955 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes(); 956 expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); 957 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); 958 } 959 960 private void expectNetworkStatsPoll() throws Exception { 961 mNetManager.setGlobalAlert(anyLong()); 962 expectLastCall().anyTimes(); 963 } 964 965 private void assertStatsFilesExist(boolean exist) { 966 final File basePath = new File(mStatsDir, "netstats"); 967 if (exist) { 968 assertTrue(basePath.list().length > 0); 969 } else { 970 assertTrue(basePath.list().length == 0); 971 } 972 } 973 974 private static void assertValues(NetworkStats stats, String iface, int uid, int set, 975 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) { 976 final NetworkStats.Entry entry = new NetworkStats.Entry(); 977 if (set == SET_DEFAULT || set == SET_ALL) { 978 final int i = stats.findIndex(iface, uid, SET_DEFAULT, tag); 979 if (i != -1) { 980 entry.add(stats.getValues(i, null)); 981 } 982 } 983 if (set == SET_FOREGROUND || set == SET_ALL) { 984 final int i = stats.findIndex(iface, uid, SET_FOREGROUND, tag); 985 if (i != -1) { 986 entry.add(stats.getValues(i, null)); 987 } 988 } 989 990 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 991 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 992 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 993 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 994 assertEquals("unexpected operations", operations, entry.operations); 995 } 996 997 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes, 998 long rxPackets, long txBytes, long txPackets, int operations) { 999 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null); 1000 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 1001 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 1002 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 1003 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 1004 assertEquals("unexpected operations", operations, entry.operations); 1005 } 1006 1007 private static NetworkState buildWifiState() { 1008 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); 1009 info.setDetailedState(DetailedState.CONNECTED, null, null); 1010 final LinkProperties prop = new LinkProperties(); 1011 prop.setInterfaceName(TEST_IFACE); 1012 return new NetworkState(info, prop, null, null, TEST_SSID); 1013 } 1014 1015 private static NetworkState buildMobile3gState(String subscriberId) { 1016 final NetworkInfo info = new NetworkInfo( 1017 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null); 1018 info.setDetailedState(DetailedState.CONNECTED, null, null); 1019 final LinkProperties prop = new LinkProperties(); 1020 prop.setInterfaceName(TEST_IFACE); 1021 return new NetworkState(info, prop, null, subscriberId, null); 1022 } 1023 1024 private static NetworkState buildMobile4gState(String iface) { 1025 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null); 1026 info.setDetailedState(DetailedState.CONNECTED, null, null); 1027 final LinkProperties prop = new LinkProperties(); 1028 prop.setInterfaceName(iface); 1029 return new NetworkState(info, prop, null); 1030 } 1031 1032 private NetworkStats buildEmptyStats() { 1033 return new NetworkStats(getElapsedRealtime(), 0); 1034 } 1035 1036 private long getElapsedRealtime() { 1037 return mElapsedRealtime; 1038 } 1039 1040 private long startTimeMillis() { 1041 return TEST_START; 1042 } 1043 1044 private long currentTimeMillis() { 1045 return startTimeMillis() + mElapsedRealtime; 1046 } 1047 1048 private void incrementCurrentTime(long duration) { 1049 mElapsedRealtime += duration; 1050 } 1051 1052 private void replay() { 1053 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); 1054 } 1055 1056 private void verifyAndReset() { 1057 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); 1058 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); 1059 } 1060} 1061