blob: 2c6dbbf345d4d2f7104db936b9c2f0bcc7addaed [file] [log] [blame]
Jeff Sharkey3f391352011-06-05 17:42:53 -07001/*
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
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070019import static android.content.Intent.ACTION_UID_REMOVED;
20import static android.content.Intent.EXTRA_UID;
Jeff Sharkey3f391352011-06-05 17:42:53 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070022import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070023import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070025import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070026import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070027import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070028import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey3f391352011-06-05 17:42:53 -070029import static android.text.format.DateUtils.DAY_IN_MILLIS;
30import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070031import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
32import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070033import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
34import static org.easymock.EasyMock.anyLong;
35import static org.easymock.EasyMock.createMock;
36import static org.easymock.EasyMock.eq;
37import static org.easymock.EasyMock.expect;
38import static org.easymock.EasyMock.expectLastCall;
39import static org.easymock.EasyMock.isA;
40
41import android.app.AlarmManager;
42import android.app.IAlarmManager;
43import android.app.PendingIntent;
44import android.content.Intent;
45import android.net.IConnectivityManager;
46import android.net.LinkProperties;
47import android.net.NetworkInfo;
48import android.net.NetworkInfo.DetailedState;
49import android.net.NetworkState;
50import android.net.NetworkStats;
51import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070052import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070053import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070054import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070055import android.test.AndroidTestCase;
56import android.test.suitebuilder.annotation.LargeTest;
57import android.util.TrustedTime;
58
59import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070060import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070061
62import org.easymock.EasyMock;
63
64import java.io.File;
65
66/**
67 * Tests for {@link NetworkStatsService}.
68 */
69@LargeTest
70public class NetworkStatsServiceTest extends AndroidTestCase {
71 private static final String TAG = "NetworkStatsServiceTest";
72
73 private static final String TEST_IFACE = "test0";
74 private static final long TEST_START = 1194220800000L;
75
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070076 private static final String IMSI_1 = "310004";
77 private static final String IMSI_2 = "310260";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070078
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070079 private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
80 private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1);
81 private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2);
82
83 private static final int TEST_UID_RED = 1001;
84 private static final int TEST_UID_BLUE = 1002;
85 private static final int TEST_UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070086
Jeff Sharkey3f391352011-06-05 17:42:53 -070087 private BroadcastInterceptingContext mServiceContext;
88 private File mStatsDir;
89
90 private INetworkManagementService mNetManager;
91 private IAlarmManager mAlarmManager;
92 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070093 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070094 private IConnectivityManager mConnManager;
95
96 private NetworkStatsService mService;
97
98 @Override
99 public void setUp() throws Exception {
100 super.setUp();
101
102 mServiceContext = new BroadcastInterceptingContext(getContext());
103 mStatsDir = getContext().getFilesDir();
104
105 mNetManager = createMock(INetworkManagementService.class);
106 mAlarmManager = createMock(IAlarmManager.class);
107 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700108 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700109 mConnManager = createMock(IConnectivityManager.class);
110
111 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700112 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700113 mService.bindConnectivityManager(mConnManager);
114
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700115 expectDefaultSettings();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700116 expectSystemReady();
117
118 replay();
119 mService.systemReady();
120 verifyAndReset();
121
122 }
123
124 @Override
125 public void tearDown() throws Exception {
126 for (File file : mStatsDir.listFiles()) {
127 file.delete();
128 }
129
130 mServiceContext = null;
131 mStatsDir = null;
132
133 mNetManager = null;
134 mAlarmManager = null;
135 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700136 mSettings = null;
137 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700138
139 mService = null;
140
141 super.tearDown();
142 }
143
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700144 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700145 long elapsedRealtime = 0;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700146
147 // pretend that wifi network comes online; service should ask about full
148 // network state, and poll any existing interfaces before updating.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700149 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700150 expectDefaultSettings();
151 expectNetworkState(buildWifiState());
152 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700153
154 replay();
155 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700156
157 // verify service has empty history for wifi
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700158 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700159 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700160
161 // modify some number on wifi, and trigger poll event
162 elapsedRealtime += HOUR_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700163 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700164 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700165 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700166 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700167 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700168
169 replay();
170 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700171
172 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700173 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700174 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700175
176 // and bump forward again, with counters going higher. this is
177 // important, since polling should correctly subtract last snapshot.
178 elapsedRealtime += DAY_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700179 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700180 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700181 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700182 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700183 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700184
185 replay();
186 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700187
188 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700189 assertNetworkTotal(sTemplateWifi, 4096L, 8192L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700190 verifyAndReset();
191
Jeff Sharkey3f391352011-06-05 17:42:53 -0700192 }
193
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700194 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700195 long elapsedRealtime = 0;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700196 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700197
198 // pretend that wifi network comes online; service should ask about full
199 // network state, and poll any existing interfaces before updating.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700200 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700201 expectDefaultSettings();
202 expectNetworkState(buildWifiState());
203 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700204
205 replay();
206 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700207
208 // verify service has empty history for wifi
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700209 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700210 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700211
212 // modify some number on wifi, and trigger poll event
213 elapsedRealtime += HOUR_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700214 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700215 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700216 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700217 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L));
Jeff Sharkey4a971222011-06-11 22:16:55 -0700218 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2)
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700219 .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 512L, 256L)
220 .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 128L, 128L));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700221
222 replay();
223 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700224
225 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700226 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700227 assertUidTotal(sTemplateWifi, TEST_UID_RED, 512L, 256L);
228 assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 128L, 128L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700229 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700230
231 // graceful shutdown system, which should trigger persist of stats, and
232 // clear any values in memory.
233 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
234
235 // talk with zombie service to assert stats have gone; and assert that
236 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700237 expectDefaultSettings();
238 replay();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700239 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700240 verifyAndReset();
241
242 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700243
244 // boot through serviceReady() again
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700245 expectDefaultSettings();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700246 expectSystemReady();
247
248 replay();
249 mService.systemReady();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700250
251 // after systemReady(), we should have historical stats loaded again
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700252 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700253 assertUidTotal(sTemplateWifi, TEST_UID_RED, 512L, 256L);
254 assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 128L, 128L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700255 verifyAndReset();
256
257 }
258
259 public void testStatsBucketResize() throws Exception {
260 long elapsedRealtime = 0;
261 NetworkStatsHistory history = null;
262 long[] total = null;
263
264 assertStatsFilesExist(false);
265
266 // pretend that wifi network comes online; service should ask about full
267 // network state, and poll any existing interfaces before updating.
268 expectTime(TEST_START + elapsedRealtime);
269 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
270 expectNetworkState(buildWifiState());
271 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
272
273 replay();
274 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
275 verifyAndReset();
276
277 // modify some number on wifi, and trigger poll event
278 elapsedRealtime += 2 * HOUR_IN_MILLIS;
279 expectTime(TEST_START + elapsedRealtime);
280 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkey4a971222011-06-11 22:16:55 -0700281 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700282 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700283 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
284
285 replay();
286 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
287
288 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700289 history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700290 total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
291 assertEquals(512L, total[0]);
292 assertEquals(512L, total[1]);
293 assertEquals(HOUR_IN_MILLIS, history.bucketDuration);
294 assertEquals(2, history.bucketCount);
295 verifyAndReset();
296
297 // now change bucket duration setting and trigger another poll with
298 // exact same values, which should resize existing buckets.
299 expectTime(TEST_START + elapsedRealtime);
300 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
301 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
302 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
303
304 replay();
305 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
306
307 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700308 history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700309 total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
310 assertEquals(512L, total[0]);
311 assertEquals(512L, total[1]);
312 assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration);
313 assertEquals(4, history.bucketCount);
314 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700315
316 }
317
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700318 public void testUidStatsAcrossNetworks() throws Exception {
319 long elapsedRealtime = 0;
320
321 // pretend first mobile network comes online
322 expectTime(TEST_START + elapsedRealtime);
323 expectDefaultSettings();
324 expectNetworkState(buildMobile3gState(IMSI_1));
325 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
326
327 replay();
328 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
329 verifyAndReset();
330
331 // create some traffic on first network
332 elapsedRealtime += HOUR_IN_MILLIS;
333 expectTime(TEST_START + elapsedRealtime);
334 expectDefaultSettings();
335 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
336 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 512L));
337 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 3)
338 .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 1024L, 0L)
339 .addEntry(TEST_IFACE, TEST_UID_RED, 0xF00D, 512L, 512L)
340 .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 512L, 0L));
341
342 replay();
343 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
344
345 // verify service recorded history
346 assertNetworkTotal(sTemplateImsi1, 2048L, 512L);
347 assertNetworkTotal(sTemplateWifi, 0L, 0L);
348 assertUidTotal(sTemplateImsi1, TEST_UID_RED, 1536L, 512L);
349 assertUidTotal(sTemplateImsi1, TEST_UID_BLUE, 512L, 0L);
350 verifyAndReset();
351
352 // now switch networks; this also tests that we're okay with interfaces
353 // disappearing, to verify we don't count backwards.
354 elapsedRealtime += HOUR_IN_MILLIS;
355 expectTime(TEST_START + elapsedRealtime);
356 expectDefaultSettings();
357 expectNetworkState(buildMobile3gState(IMSI_2));
358 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
359 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
360
361 replay();
362 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
363 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
364 verifyAndReset();
365
366 // create traffic on second network
367 elapsedRealtime += HOUR_IN_MILLIS;
368 expectTime(TEST_START + elapsedRealtime);
369 expectDefaultSettings();
370 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
371 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1024L));
372 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
373 .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 128L, 1024L));
374
375 replay();
376 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
377
378 // verify original history still intact
379 assertNetworkTotal(sTemplateImsi1, 2048L, 512L);
380 assertUidTotal(sTemplateImsi1, TEST_UID_RED, 1536L, 512L);
381 assertUidTotal(sTemplateImsi1, TEST_UID_BLUE, 512L, 0L);
382
383 // and verify new history also recorded under different template, which
384 // verifies that we didn't cross the streams.
385 assertNetworkTotal(sTemplateImsi2, 128L, 1024L);
386 assertNetworkTotal(sTemplateWifi, 0L, 0L);
387 assertUidTotal(sTemplateImsi2, TEST_UID_BLUE, 128L, 1024L);
388 verifyAndReset();
389
390 }
391
392 public void testUidRemovedIsMoved() throws Exception {
393 long elapsedRealtime = 0;
394
395 // pretend that network comes online
396 expectTime(TEST_START + elapsedRealtime);
397 expectDefaultSettings();
398 expectNetworkState(buildWifiState());
399 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
400
401 replay();
402 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
403 verifyAndReset();
404
405 // create some traffic
406 elapsedRealtime += HOUR_IN_MILLIS;
407 expectTime(TEST_START + elapsedRealtime);
408 expectDefaultSettings();
409 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
410 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 544L));
411 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
412 .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 16L, 16L)
413 .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 4096L, 512L)
414 .addEntry(TEST_IFACE, TEST_UID_GREEN, TAG_NONE, 16L, 16L));
415
416 replay();
417 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
418
419 // verify service recorded history
420 assertNetworkTotal(sTemplateWifi, 4128L, 544L);
421 assertUidTotal(sTemplateWifi, TEST_UID_RED, 16L, 16L);
422 assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 4096L, 512L);
423 assertUidTotal(sTemplateWifi, TEST_UID_GREEN, 16L, 16L);
424 verifyAndReset();
425
426 // now pretend two UIDs are uninstalled, which should migrate stats to
427 // special "removed" bucket.
428 expectDefaultSettings();
429 replay();
430 final Intent intent = new Intent(ACTION_UID_REMOVED);
431 intent.putExtra(EXTRA_UID, TEST_UID_BLUE);
432 mServiceContext.sendBroadcast(intent);
433 intent.putExtra(EXTRA_UID, TEST_UID_RED);
434 mServiceContext.sendBroadcast(intent);
435
436 // existing uid and total should remain unchanged; but removed UID
437 // should be gone completely.
438 assertNetworkTotal(sTemplateWifi, 4128L, 544L);
439 assertUidTotal(sTemplateWifi, TEST_UID_RED, 0L, 0L);
440 assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 0L, 0L);
441 assertUidTotal(sTemplateWifi, TEST_UID_GREEN, 16L, 16L);
442 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 528L);
443 verifyAndReset();
444
445 }
446
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700447 private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700448 final NetworkStatsHistory history = mService.getHistoryForNetwork(template);
449 final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
450 assertEquals(rx, total[0]);
451 assertEquals(tx, total[1]);
452 }
453
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700454 private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) {
455 final NetworkStatsHistory history = mService.getHistoryForUid(template, uid);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700456 final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
457 assertEquals(rx, total[0]);
458 assertEquals(tx, total[1]);
459 }
460
Jeff Sharkey3f391352011-06-05 17:42:53 -0700461 private void expectSystemReady() throws Exception {
462 mAlarmManager.remove(isA(PendingIntent.class));
463 expectLastCall().anyTimes();
464
465 mAlarmManager.setInexactRepeating(
466 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
467 expectLastCall().atLeastOnce();
468 }
469
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700470 private void expectNetworkState(NetworkState... state) throws Exception {
471 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
472 }
473
474 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
475 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
476 }
477
478 private void expectNetworkStatsDetail(NetworkStats detail) throws Exception {
479 expect(mNetManager.getNetworkStatsDetail()).andReturn(detail).atLeastOnce();
480 }
481
482 private void expectDefaultSettings() throws Exception {
483 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
484 }
485
486 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
487 throws Exception {
488 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
489 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
490 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
491 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
492 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
493 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
494 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
495 }
496
497 private void expectTime(long currentTime) throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700498 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
499 expect(mTime.hasCache()).andReturn(true).anyTimes();
500 expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes();
501 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
502 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
503 }
504
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700505 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700506 final File networkFile = new File(mStatsDir, "netstats.bin");
507 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700508 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700509 assertTrue(networkFile.exists());
510 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700511 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700512 assertFalse(networkFile.exists());
513 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700514 }
515 }
516
517 private static NetworkState buildWifiState() {
518 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
519 info.setDetailedState(DetailedState.CONNECTED, null, null);
520 final LinkProperties prop = new LinkProperties();
521 prop.setInterfaceName(TEST_IFACE);
522 return new NetworkState(info, prop, null);
523 }
524
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700525 private static NetworkState buildMobile3gState(String subscriberId) {
526 final NetworkInfo info = new NetworkInfo(
527 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
528 info.setDetailedState(DetailedState.CONNECTED, null, null);
529 final LinkProperties prop = new LinkProperties();
530 prop.setInterfaceName(TEST_IFACE);
531 return new NetworkState(info, prop, null, subscriberId);
532 }
533
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700534 private static NetworkStats buildEmptyStats(long elapsedRealtime) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700535 return new NetworkStats(elapsedRealtime, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700536 }
537
Jeff Sharkey3f391352011-06-05 17:42:53 -0700538 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700539 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700540 }
541
542 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700543 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
544 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700545 }
546}