blob: 636d0593e9f8bb322a643f263190b63c7a2d8641 [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 Sharkeyd03fd3f2011-06-19 20:55:09 -070024import static android.net.ConnectivityManager.TYPE_WIMAX;
25import static android.net.NetworkStats.IFACE_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070026import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070027import static android.net.NetworkStats.UID_ALL;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070028import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070029import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070030import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey3f391352011-06-05 17:42:53 -070031import static android.text.format.DateUtils.DAY_IN_MILLIS;
32import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070033import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
34import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070035import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070036import static com.android.server.net.NetworkStatsService.packUidAndTag;
37import static com.android.server.net.NetworkStatsService.unpackTag;
38import static com.android.server.net.NetworkStatsService.unpackUid;
Jeff Sharkey3f391352011-06-05 17:42:53 -070039import static org.easymock.EasyMock.anyLong;
40import static org.easymock.EasyMock.createMock;
41import static org.easymock.EasyMock.eq;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.expectLastCall;
44import static org.easymock.EasyMock.isA;
45
46import android.app.AlarmManager;
47import android.app.IAlarmManager;
48import android.app.PendingIntent;
49import android.content.Intent;
50import android.net.IConnectivityManager;
51import android.net.LinkProperties;
52import android.net.NetworkInfo;
53import android.net.NetworkInfo.DetailedState;
54import android.net.NetworkState;
55import android.net.NetworkStats;
56import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070057import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070058import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070059import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070060import android.test.AndroidTestCase;
61import android.test.suitebuilder.annotation.LargeTest;
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070062import android.util.Log;
Jeff Sharkey3f391352011-06-05 17:42:53 -070063import android.util.TrustedTime;
64
65import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070066import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070067
68import org.easymock.EasyMock;
69
70import java.io.File;
71
72/**
73 * Tests for {@link NetworkStatsService}.
74 */
75@LargeTest
76public class NetworkStatsServiceTest extends AndroidTestCase {
77 private static final String TAG = "NetworkStatsServiceTest";
78
79 private static final String TEST_IFACE = "test0";
80 private static final long TEST_START = 1194220800000L;
81
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070082 private static final String IMSI_1 = "310004";
83 private static final String IMSI_2 = "310260";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070084
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070085 private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null);
86 private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1);
87 private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2);
88
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070089 private static final int UID_RED = 1001;
90 private static final int UID_BLUE = 1002;
91 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070092
Jeff Sharkey3f391352011-06-05 17:42:53 -070093 private BroadcastInterceptingContext mServiceContext;
94 private File mStatsDir;
95
96 private INetworkManagementService mNetManager;
97 private IAlarmManager mAlarmManager;
98 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070099 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700100 private IConnectivityManager mConnManager;
101
102 private NetworkStatsService mService;
103
104 @Override
105 public void setUp() throws Exception {
106 super.setUp();
107
108 mServiceContext = new BroadcastInterceptingContext(getContext());
109 mStatsDir = getContext().getFilesDir();
110
111 mNetManager = createMock(INetworkManagementService.class);
112 mAlarmManager = createMock(IAlarmManager.class);
113 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700114 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700115 mConnManager = createMock(IConnectivityManager.class);
116
117 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700118 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700119 mService.bindConnectivityManager(mConnManager);
120
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700121 expectDefaultSettings();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700122 expectSystemReady();
123
124 replay();
125 mService.systemReady();
126 verifyAndReset();
127
128 }
129
130 @Override
131 public void tearDown() throws Exception {
132 for (File file : mStatsDir.listFiles()) {
133 file.delete();
134 }
135
136 mServiceContext = null;
137 mStatsDir = null;
138
139 mNetManager = null;
140 mAlarmManager = null;
141 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700142 mSettings = null;
143 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700144
145 mService = null;
146
147 super.tearDown();
148 }
149
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700150 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700151 long elapsedRealtime = 0;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700152
153 // pretend that wifi network comes online; service should ask about full
154 // network state, and poll any existing interfaces before updating.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700155 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700156 expectDefaultSettings();
157 expectNetworkState(buildWifiState());
158 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700159
160 replay();
161 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700162
163 // verify service has empty history for wifi
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700164 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700165 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700166
167 // modify some number on wifi, and trigger poll event
168 elapsedRealtime += HOUR_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700169 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700170 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700171 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700172 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700173 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700174
175 replay();
176 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700177
178 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700179 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700180 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700181
182 // and bump forward again, with counters going higher. this is
183 // important, since polling should correctly subtract last snapshot.
184 elapsedRealtime += DAY_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700185 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700186 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700187 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700188 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700189 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700190
191 replay();
192 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700193
194 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700195 assertNetworkTotal(sTemplateWifi, 4096L, 8192L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700196 verifyAndReset();
197
Jeff Sharkey3f391352011-06-05 17:42:53 -0700198 }
199
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700200 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700201 long elapsedRealtime = 0;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700202 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700203
204 // pretend that wifi network comes online; service should ask about full
205 // network state, and poll any existing interfaces before updating.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700206 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700207 expectDefaultSettings();
208 expectNetworkState(buildWifiState());
209 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700210
211 replay();
212 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700213
214 // verify service has empty history for wifi
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700215 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700216 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700217
218 // modify some number on wifi, and trigger poll event
219 elapsedRealtime += HOUR_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700220 expectTime(TEST_START + elapsedRealtime);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700221 expectDefaultSettings();
Jeff Sharkey4a971222011-06-11 22:16:55 -0700222 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700223 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L));
Jeff Sharkey4a971222011-06-11 22:16:55 -0700224 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2)
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700225 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L)
226 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 128L));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700227
228 replay();
229 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700230
231 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700232 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700233 assertUidTotal(sTemplateWifi, UID_RED, 512L, 256L);
234 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 128L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700235 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700236
237 // graceful shutdown system, which should trigger persist of stats, and
238 // clear any values in memory.
239 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
240
241 // talk with zombie service to assert stats have gone; and assert that
242 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700243 expectDefaultSettings();
244 replay();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700245 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700246 verifyAndReset();
247
248 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700249
250 // boot through serviceReady() again
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700251 expectDefaultSettings();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700252 expectSystemReady();
253
254 replay();
255 mService.systemReady();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700256
257 // after systemReady(), we should have historical stats loaded again
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700258 assertNetworkTotal(sTemplateWifi, 1024L, 2048L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700259 assertUidTotal(sTemplateWifi, UID_RED, 512L, 256L);
260 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 128L);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700261 verifyAndReset();
262
263 }
264
265 public void testStatsBucketResize() throws Exception {
266 long elapsedRealtime = 0;
267 NetworkStatsHistory history = null;
268 long[] total = null;
269
270 assertStatsFilesExist(false);
271
272 // pretend that wifi network comes online; service should ask about full
273 // network state, and poll any existing interfaces before updating.
274 expectTime(TEST_START + elapsedRealtime);
275 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
276 expectNetworkState(buildWifiState());
277 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
278
279 replay();
280 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
281 verifyAndReset();
282
283 // modify some number on wifi, and trigger poll event
284 elapsedRealtime += 2 * HOUR_IN_MILLIS;
285 expectTime(TEST_START + elapsedRealtime);
286 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkey4a971222011-06-11 22:16:55 -0700287 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700288 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700289 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
290
291 replay();
292 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
293
294 // verify service recorded history
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700295 history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700296 total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
297 assertEquals(512L, total[0]);
298 assertEquals(512L, total[1]);
299 assertEquals(HOUR_IN_MILLIS, history.bucketDuration);
300 assertEquals(2, history.bucketCount);
301 verifyAndReset();
302
303 // now change bucket duration setting and trigger another poll with
304 // exact same values, which should resize existing buckets.
305 expectTime(TEST_START + elapsedRealtime);
306 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
307 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
308 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
309
310 replay();
311 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
312
313 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700314 history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700315 total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
316 assertEquals(512L, total[0]);
317 assertEquals(512L, total[1]);
318 assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration);
319 assertEquals(4, history.bucketCount);
320 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700321
322 }
323
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700324 public void testUidStatsAcrossNetworks() throws Exception {
325 long elapsedRealtime = 0;
326
327 // pretend first mobile network comes online
328 expectTime(TEST_START + elapsedRealtime);
329 expectDefaultSettings();
330 expectNetworkState(buildMobile3gState(IMSI_1));
331 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
332
333 replay();
334 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
335 verifyAndReset();
336
337 // create some traffic on first network
338 elapsedRealtime += HOUR_IN_MILLIS;
339 expectTime(TEST_START + elapsedRealtime);
340 expectDefaultSettings();
341 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
342 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 512L));
343 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 3)
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700344 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1536L, 512L)
345 .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L)
346 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 512L, 0L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700347
348 replay();
349 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
350
351 // verify service recorded history
352 assertNetworkTotal(sTemplateImsi1, 2048L, 512L);
353 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700354 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 512L);
355 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 0L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700356 verifyAndReset();
357
358 // now switch networks; this also tests that we're okay with interfaces
359 // disappearing, to verify we don't count backwards.
360 elapsedRealtime += HOUR_IN_MILLIS;
361 expectTime(TEST_START + elapsedRealtime);
362 expectDefaultSettings();
363 expectNetworkState(buildMobile3gState(IMSI_2));
364 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
365 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
366
367 replay();
368 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
369 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
370 verifyAndReset();
371
372 // create traffic on second network
373 elapsedRealtime += HOUR_IN_MILLIS;
374 expectTime(TEST_START + elapsedRealtime);
375 expectDefaultSettings();
376 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
377 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1024L));
378 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700379 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 1024L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700380
381 replay();
382 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
383
384 // verify original history still intact
385 assertNetworkTotal(sTemplateImsi1, 2048L, 512L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700386 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 512L);
387 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 0L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700388
389 // and verify new history also recorded under different template, which
390 // verifies that we didn't cross the streams.
391 assertNetworkTotal(sTemplateImsi2, 128L, 1024L);
392 assertNetworkTotal(sTemplateWifi, 0L, 0L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700393 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1024L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700394 verifyAndReset();
395
396 }
397
398 public void testUidRemovedIsMoved() throws Exception {
399 long elapsedRealtime = 0;
400
401 // pretend that network comes online
402 expectTime(TEST_START + elapsedRealtime);
403 expectDefaultSettings();
404 expectNetworkState(buildWifiState());
405 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
406
407 replay();
408 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
409 verifyAndReset();
410
411 // create some traffic
412 elapsedRealtime += HOUR_IN_MILLIS;
413 expectTime(TEST_START + elapsedRealtime);
414 expectDefaultSettings();
415 expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1)
416 .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 544L));
417 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700418 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 16L, 16L)
419 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 4096L, 512L)
420 .addEntry(TEST_IFACE, UID_GREEN, TAG_NONE, 16L, 16L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700421
422 replay();
423 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
424
425 // verify service recorded history
426 assertNetworkTotal(sTemplateWifi, 4128L, 544L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700427 assertUidTotal(sTemplateWifi, UID_RED, 16L, 16L);
428 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 512L);
429 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 16L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700430 verifyAndReset();
431
432 // now pretend two UIDs are uninstalled, which should migrate stats to
433 // special "removed" bucket.
434 expectDefaultSettings();
435 replay();
436 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700437 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700438 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700439 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700440 mServiceContext.sendBroadcast(intent);
441
442 // existing uid and total should remain unchanged; but removed UID
443 // should be gone completely.
444 assertNetworkTotal(sTemplateWifi, 4128L, 544L);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700445 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L);
446 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L);
447 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 16L);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700448 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 528L);
449 verifyAndReset();
450
451 }
452
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700453 public void testUid3g4gCombinedByTemplate() throws Exception {
454 long elapsedRealtime = 0;
455
456 // pretend that network comes online
457 expectTime(TEST_START + elapsedRealtime);
458 expectDefaultSettings();
459 expectNetworkState(buildMobile3gState(IMSI_1));
460 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
461
462 replay();
463 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
464 verifyAndReset();
465
466 // create some traffic
467 elapsedRealtime += HOUR_IN_MILLIS;
468 expectTime(TEST_START + elapsedRealtime);
469 expectDefaultSettings();
470 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
471 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
472 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1024L, 1024L)
473 .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L));
474
475 replay();
476 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
477
478 // verify service recorded history
479 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 1024L);
480 verifyAndReset();
481
482 // now switch over to 4g network
483 elapsedRealtime += HOUR_IN_MILLIS;
484 expectTime(TEST_START + elapsedRealtime);
485 expectDefaultSettings();
486 expectNetworkState(buildMobile4gState());
487 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
488 expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime));
489
490 replay();
491 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
492 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
493 verifyAndReset();
494
495 // create traffic on second network
496 elapsedRealtime += HOUR_IN_MILLIS;
497 expectTime(TEST_START + elapsedRealtime);
498 expectDefaultSettings();
499 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
500 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
501 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L));
502
503 replay();
504 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
505
506 // verify that ALL_MOBILE template combines both
507 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 1280L);
508
509 verifyAndReset();
510
511 }
512
513 public void testPackedUidAndTag() throws Exception {
514 assertEquals(0x0000000000000000L, packUidAndTag(0, 0x0));
515 assertEquals(0x000003E900000000L, packUidAndTag(1001, 0x0));
516 assertEquals(0x000003E90000F00DL, packUidAndTag(1001, 0xF00D));
517
518 long packed;
519 packed = packUidAndTag(Integer.MAX_VALUE, Integer.MIN_VALUE);
520 assertEquals(Integer.MAX_VALUE, unpackUid(packed));
521 assertEquals(Integer.MIN_VALUE, unpackTag(packed));
522
523 packed = packUidAndTag(Integer.MIN_VALUE, Integer.MAX_VALUE);
524 assertEquals(Integer.MIN_VALUE, unpackUid(packed));
525 assertEquals(Integer.MAX_VALUE, unpackTag(packed));
526
527 packed = packUidAndTag(10005, 0xFFFFFFFF);
528 assertEquals(10005, unpackUid(packed));
529 assertEquals(0xFFFFFFFF, unpackTag(packed));
530
531 }
532
533 public void testSummaryForAllUid() throws Exception {
534 long elapsedRealtime = 0;
535
536 // pretend that network comes online
537 expectTime(TEST_START + elapsedRealtime);
538 expectDefaultSettings();
539 expectNetworkState(buildWifiState());
540 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
541
542 replay();
543 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
544 verifyAndReset();
545
546 // create some traffic for two apps
547 elapsedRealtime += HOUR_IN_MILLIS;
548 expectTime(TEST_START + elapsedRealtime);
549 expectDefaultSettings();
550 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
551 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
552 .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 50L, 50L)
553 .addEntry(TEST_IFACE, UID_RED, 0xF00D, 10L, 10L)
554 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 1024L, 512L));
555
556 replay();
557 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
558
559 // verify service recorded history
560 assertUidTotal(sTemplateWifi, UID_RED, 50L, 50L);
561 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 512L);
562 verifyAndReset();
563
564 // now create more traffic in next hour, but only for one app
565 elapsedRealtime += HOUR_IN_MILLIS;
566 expectTime(TEST_START + elapsedRealtime);
567 expectDefaultSettings();
568 expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime));
569 expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1)
570 .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 2048L, 1024L));
571
572 replay();
573 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
574
575 // first verify entire history present
576 NetworkStats stats = mService.getSummaryForAllUid(
577 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
578 assertEquals(3, stats.size);
579 assertStatsEntry(stats, 0, IFACE_ALL, UID_RED, TAG_NONE, 50L, 50L);
580 assertStatsEntry(stats, 1, IFACE_ALL, UID_RED, 0xF00D, 10L, 10L);
581 assertStatsEntry(stats, 2, IFACE_ALL, UID_BLUE, TAG_NONE, 2048L, 1024L);
582
583 // now verify that recent history only contains one uid
584 final long currentTime = TEST_START + elapsedRealtime;
585 stats = mService.getSummaryForAllUid(
586 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
587 assertEquals(1, stats.size);
588 assertStatsEntry(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 512L);
589
590 verifyAndReset();
591 }
592
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700593 private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700594 final NetworkStatsHistory history = mService.getHistoryForNetwork(template);
595 final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
596 assertEquals(rx, total[0]);
597 assertEquals(tx, total[1]);
598 }
599
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700600 private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700601 final NetworkStatsHistory history = mService.getHistoryForUid(template, uid, TAG_NONE);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700602 final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null);
603 assertEquals(rx, total[0]);
604 assertEquals(tx, total[1]);
605 }
606
Jeff Sharkey3f391352011-06-05 17:42:53 -0700607 private void expectSystemReady() throws Exception {
608 mAlarmManager.remove(isA(PendingIntent.class));
609 expectLastCall().anyTimes();
610
611 mAlarmManager.setInexactRepeating(
612 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
613 expectLastCall().atLeastOnce();
614 }
615
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700616 private void expectNetworkState(NetworkState... state) throws Exception {
617 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
618 }
619
620 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
621 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
622 }
623
624 private void expectNetworkStatsDetail(NetworkStats detail) throws Exception {
625 expect(mNetManager.getNetworkStatsDetail()).andReturn(detail).atLeastOnce();
626 }
627
628 private void expectDefaultSettings() throws Exception {
629 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
630 }
631
632 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
633 throws Exception {
634 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
635 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
636 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
637 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
638 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
639 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700640 expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700641 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
642 }
643
644 private void expectTime(long currentTime) throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700645 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
646 expect(mTime.hasCache()).andReturn(true).anyTimes();
647 expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes();
648 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
649 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
650 }
651
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700652 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700653 final File networkFile = new File(mStatsDir, "netstats.bin");
654 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700655 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700656 assertTrue(networkFile.exists());
657 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700658 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700659 assertFalse(networkFile.exists());
660 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700661 }
662 }
663
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700664 private static void assertStatsEntry(
665 NetworkStats stats, int i, String iface, int uid, int tag, long rx, long tx) {
666 assertEquals(iface, stats.iface[i]);
667 assertEquals(uid, stats.uid[i]);
668 assertEquals(tag, stats.tag[i]);
669 assertEquals(rx, stats.rx[i]);
670 assertEquals(tx, stats.tx[i]);
671 }
672
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700673 private static NetworkState buildWifiState() {
674 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
675 info.setDetailedState(DetailedState.CONNECTED, null, null);
676 final LinkProperties prop = new LinkProperties();
677 prop.setInterfaceName(TEST_IFACE);
678 return new NetworkState(info, prop, null);
679 }
680
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700681 private static NetworkState buildMobile3gState(String subscriberId) {
682 final NetworkInfo info = new NetworkInfo(
683 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
684 info.setDetailedState(DetailedState.CONNECTED, null, null);
685 final LinkProperties prop = new LinkProperties();
686 prop.setInterfaceName(TEST_IFACE);
687 return new NetworkState(info, prop, null, subscriberId);
688 }
689
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700690 private static NetworkState buildMobile4gState() {
691 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
692 info.setDetailedState(DetailedState.CONNECTED, null, null);
693 final LinkProperties prop = new LinkProperties();
694 prop.setInterfaceName(TEST_IFACE);
695 return new NetworkState(info, prop, null);
696 }
697
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700698 private static NetworkStats buildEmptyStats(long elapsedRealtime) {
Jeff Sharkey4a971222011-06-11 22:16:55 -0700699 return new NetworkStats(elapsedRealtime, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700700 }
701
Jeff Sharkey3f391352011-06-05 17:42:53 -0700702 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700703 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700704 }
705
706 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700707 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
708 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700709 }
710}