blob: e7f1d9ad374530f49d232293de89a98c4c55d168 [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 Sharkey961e3042011-08-29 16:02:57 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
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 Sharkeyb5d55e32011-08-10 17:53:27 -070026import static android.net.NetworkStats.SET_ALL;
27import static android.net.NetworkStats.SET_DEFAULT;
28import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070029import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070030import static android.net.NetworkStats.UID_ALL;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070031import static android.net.NetworkStatsHistory.FIELD_ALL;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070032import static android.net.NetworkTemplate.buildTemplateMobileAll;
33import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070034import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey3f391352011-06-05 17:42:53 -070035import static android.text.format.DateUtils.DAY_IN_MILLIS;
36import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070037import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
38import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070039import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
40import static org.easymock.EasyMock.anyLong;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070041import static org.easymock.EasyMock.capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070042import static org.easymock.EasyMock.createMock;
43import static org.easymock.EasyMock.eq;
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.expectLastCall;
46import static org.easymock.EasyMock.isA;
47
48import android.app.AlarmManager;
49import android.app.IAlarmManager;
50import android.app.PendingIntent;
51import android.content.Intent;
52import android.net.IConnectivityManager;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070053import android.net.INetworkManagementEventObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -070054import android.net.LinkProperties;
55import android.net.NetworkInfo;
56import android.net.NetworkInfo.DetailedState;
57import android.net.NetworkState;
58import android.net.NetworkStats;
59import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070060import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070061import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070062import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070063import android.test.AndroidTestCase;
64import android.test.suitebuilder.annotation.LargeTest;
65import android.util.TrustedTime;
66
67import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070068import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070069
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070070import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070071import org.easymock.EasyMock;
72
73import java.io.File;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070074
75import libcore.io.IoUtils;
Jeff Sharkey3f391352011-06-05 17:42:53 -070076
77/**
78 * Tests for {@link NetworkStatsService}.
79 */
80@LargeTest
81public class NetworkStatsServiceTest extends AndroidTestCase {
82 private static final String TAG = "NetworkStatsServiceTest";
83
84 private static final String TEST_IFACE = "test0";
85 private static final long TEST_START = 1194220800000L;
86
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070087 private static final String IMSI_1 = "310004";
88 private static final String IMSI_2 = "310260";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070089
Jeff Sharkey4e814c32011-07-14 20:37:37 -070090 private static NetworkTemplate sTemplateWifi = buildTemplateWifi();
91 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
92 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070093
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070094 private static final int UID_RED = 1001;
95 private static final int UID_BLUE = 1002;
96 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070097
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070098 private long mElapsedRealtime;
99
Jeff Sharkey3f391352011-06-05 17:42:53 -0700100 private BroadcastInterceptingContext mServiceContext;
101 private File mStatsDir;
102
103 private INetworkManagementService mNetManager;
104 private IAlarmManager mAlarmManager;
105 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700106 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700107 private IConnectivityManager mConnManager;
108
109 private NetworkStatsService mService;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700110 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700111
112 @Override
113 public void setUp() throws Exception {
114 super.setUp();
115
116 mServiceContext = new BroadcastInterceptingContext(getContext());
117 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700118 if (mStatsDir.exists()) {
119 IoUtils.deleteContents(mStatsDir);
120 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700121
122 mNetManager = createMock(INetworkManagementService.class);
123 mAlarmManager = createMock(IAlarmManager.class);
124 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700125 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700126 mConnManager = createMock(IConnectivityManager.class);
127
128 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700129 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700130 mService.bindConnectivityManager(mConnManager);
131
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700132 mElapsedRealtime = 0L;
133
134 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700135 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700136 expectNetworkStatsSummary(buildEmptyStats());
137 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700138 expectSystemReady();
139
140 // catch INetworkManagementEventObserver during systemReady()
141 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
142 INetworkManagementEventObserver>();
143 mNetManager.registerObserver(capture(networkObserver));
144 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700145
146 replay();
147 mService.systemReady();
148 verifyAndReset();
149
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700150 mNetworkObserver = networkObserver.getValue();
151
Jeff Sharkey3f391352011-06-05 17:42:53 -0700152 }
153
154 @Override
155 public void tearDown() throws Exception {
156 for (File file : mStatsDir.listFiles()) {
157 file.delete();
158 }
159
160 mServiceContext = null;
161 mStatsDir = null;
162
163 mNetManager = null;
164 mAlarmManager = null;
165 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700166 mSettings = null;
167 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700168
169 mService = null;
170
171 super.tearDown();
172 }
173
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700174 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700175 // pretend that wifi network comes online; service should ask about full
176 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700177 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700178 expectDefaultSettings();
179 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700180 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700181 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700182
183 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700184 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700185
186 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700187 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700188 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700189
190 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700191 incrementCurrentTime(HOUR_IN_MILLIS);
192 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700193 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700194 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
195 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
196 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700197 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700198
199 replay();
200 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700201
202 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700203 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700204 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700205
206 // and bump forward again, with counters going higher. this is
207 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700208 incrementCurrentTime(DAY_IN_MILLIS);
209 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700210 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700211 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
212 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
213 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700214 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700215
216 replay();
217 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700218
219 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700220 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700221 verifyAndReset();
222
Jeff Sharkey3f391352011-06-05 17:42:53 -0700223 }
224
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700225 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700226 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700227
228 // pretend that wifi network comes online; service should ask about full
229 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700230 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700231 expectDefaultSettings();
232 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700233 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700234 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700235
236 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700237 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700238
239 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700240 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700241 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700242
243 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700244 incrementCurrentTime(HOUR_IN_MILLIS);
245 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700246 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700247 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
248 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
249 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
250 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
251 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
252 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
253 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
254 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700255 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700256
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700257 mService.setUidForeground(UID_RED, false);
258 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
259 mService.setUidForeground(UID_RED, true);
260 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700261
Jeff Sharkey3f391352011-06-05 17:42:53 -0700262 replay();
263 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700264
265 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700266 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700267 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
268 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
269 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
270 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700271 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700272
273 // graceful shutdown system, which should trigger persist of stats, and
274 // clear any values in memory.
275 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
276
277 // talk with zombie service to assert stats have gone; and assert that
278 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700279 expectDefaultSettings();
280 replay();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700281 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700282 verifyAndReset();
283
284 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700285
286 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700287 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700288 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700289 expectNetworkStatsSummary(buildEmptyStats());
290 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700291 expectSystemReady();
292
293 // catch INetworkManagementEventObserver during systemReady()
294 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
295 INetworkManagementEventObserver>();
296 mNetManager.registerObserver(capture(networkObserver));
297 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700298
299 replay();
300 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700301
302 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700303
304 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700305 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700306 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
307 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
308 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
309 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700310 verifyAndReset();
311
312 }
313
314 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700315 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700316
317 assertStatsFilesExist(false);
318
319 // pretend that wifi network comes online; service should ask about full
320 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700321 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700322 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
323 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700324 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700325 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700326
327 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700328 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700329 verifyAndReset();
330
331 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700332 incrementCurrentTime(2 * HOUR_IN_MILLIS);
333 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700334 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700335 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
336 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
337 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700338 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700339
340 replay();
341 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
342
343 // verify service recorded history
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700344 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700345 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700346 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
347 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700348 verifyAndReset();
349
350 // now change bucket duration setting and trigger another poll with
351 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700352 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700353 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700354 expectNetworkStatsSummary(buildEmptyStats());
355 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700356 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700357
358 replay();
359 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
360
361 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700362 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700363 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700364 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
365 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700366 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700367
368 }
369
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700370 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700371 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700372 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700373 expectDefaultSettings();
374 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700375 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700376 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700377
378 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700379 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700380 verifyAndReset();
381
382 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700383 incrementCurrentTime(HOUR_IN_MILLIS);
384 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700385 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700386 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
387 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
388 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
389 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
390 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
391 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700392 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700393
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700394 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700395
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700396 replay();
397 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
398
399 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700400 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
401 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700402 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
403 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700404 verifyAndReset();
405
406 // now switch networks; this also tests that we're okay with interfaces
407 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700408 incrementCurrentTime(HOUR_IN_MILLIS);
409 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700410 expectDefaultSettings();
411 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700412 expectNetworkStatsSummary(buildEmptyStats());
413 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700414 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700415
416 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700417 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700418 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
419 verifyAndReset();
420
421 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700422 incrementCurrentTime(HOUR_IN_MILLIS);
423 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700424 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700425 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
426 .addIfaceValues(TEST_IFACE, 128L, 1L, 1024L, 8L));
427 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
428 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 1024L, 8L, 0L)
429 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700430 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700431
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700432 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700433
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700434 replay();
435 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
436
437 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700438 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700439 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
440 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700441
442 // and verify new history also recorded under different template, which
443 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700444 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
445 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
446 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700447 verifyAndReset();
448
449 }
450
451 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700452 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700453 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700454 expectDefaultSettings();
455 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700456 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700457 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700458
459 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700460 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700461 verifyAndReset();
462
463 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700464 incrementCurrentTime(HOUR_IN_MILLIS);
465 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700466 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700467 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
468 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
469 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
470 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
471 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
472 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
473 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700474 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700475
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700476 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700477
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700478 replay();
479 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
480
481 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700482 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
483 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700484 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
485 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700486 verifyAndReset();
487
488 // now pretend two UIDs are uninstalled, which should migrate stats to
489 // special "removed" bucket.
490 expectDefaultSettings();
491 replay();
492 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700493 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700494 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700495 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700496 mServiceContext.sendBroadcast(intent);
497
498 // existing uid and total should remain unchanged; but removed UID
499 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700500 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
501 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
502 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700503 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
504 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700505 verifyAndReset();
506
507 }
508
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700509 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700510 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700511 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700512 expectDefaultSettings();
513 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700514 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700515 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700516
517 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700518 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700519 verifyAndReset();
520
521 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700522 incrementCurrentTime(HOUR_IN_MILLIS);
523 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700524 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700525 expectNetworkStatsSummary(buildEmptyStats());
526 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
527 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
528 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700529 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700530
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700531 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
532
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700533 replay();
534 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
535
536 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700537 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700538 verifyAndReset();
539
540 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700541 incrementCurrentTime(HOUR_IN_MILLIS);
542 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700543 expectDefaultSettings();
544 expectNetworkState(buildMobile4gState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700545 expectNetworkStatsSummary(buildEmptyStats());
546 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700547 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700548
549 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700550 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700551 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
552 verifyAndReset();
553
554 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700555 incrementCurrentTime(HOUR_IN_MILLIS);
556 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700557 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700558 expectNetworkStatsSummary(buildEmptyStats());
559 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
560 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
561 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700562 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700563
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700564 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700565
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700566 replay();
567 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
568
569 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700570 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700571
572 verifyAndReset();
573
574 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700575
576 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700577 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700578 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700579 expectDefaultSettings();
580 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700581 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700582 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700583
584 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700585 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700586 verifyAndReset();
587
588 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700589 incrementCurrentTime(HOUR_IN_MILLIS);
590 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700591 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700592 expectNetworkStatsSummary(buildEmptyStats());
593 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
594 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
595 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
596 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700597 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700598
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700599 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700600
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700601 replay();
602 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
603
604 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700605 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
606 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700607 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700608
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700609 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700610 incrementCurrentTime(HOUR_IN_MILLIS);
611 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700612 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700613 expectNetworkStatsSummary(buildEmptyStats());
614 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
615 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700616 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700617
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700618 replay();
619 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
620
621 // first verify entire history present
622 NetworkStats stats = mService.getSummaryForAllUid(
623 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700624 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700625 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
626 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
627 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700628
629 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700630 final long currentTime = currentTimeMillis();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700631 stats = mService.getSummaryForAllUid(
632 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700633 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700634 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
635
636 verifyAndReset();
637 }
638
639 public void testForegroundBackground() throws Exception {
640 // pretend that network comes online
641 expectCurrentTime();
642 expectDefaultSettings();
643 expectNetworkState(buildWifiState());
644 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700645 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700646
647 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700648 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700649 verifyAndReset();
650
651 // create some initial traffic
652 incrementCurrentTime(HOUR_IN_MILLIS);
653 expectCurrentTime();
654 expectDefaultSettings();
655 expectNetworkStatsSummary(buildEmptyStats());
656 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
657 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
658 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700659 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700660
661 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
662
663 replay();
664 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
665
666 // verify service recorded history
667 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
668 verifyAndReset();
669
670 // now switch to foreground
671 incrementCurrentTime(HOUR_IN_MILLIS);
672 expectCurrentTime();
673 expectDefaultSettings();
674 expectNetworkStatsSummary(buildEmptyStats());
675 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
676 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
677 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
678 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
679 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700680 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700681
682 mService.setUidForeground(UID_RED, true);
683 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
684
685 replay();
686 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
687
688 // test that we combined correctly
689 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
690
691 // verify entire history present
692 final NetworkStats stats = mService.getSummaryForAllUid(
693 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
694 assertEquals(4, stats.size());
695 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
696 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
697 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
698 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700699
700 verifyAndReset();
701 }
702
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700703 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
704 long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700705 final NetworkStatsHistory history = mService.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700706 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
707 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700708 }
709
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700710 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
711 long txBytes, long txPackets, int operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700712 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
713 }
714
715 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
716 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700717 final NetworkStatsHistory history = mService.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700718 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700719 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
720 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700721 }
722
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700723 private void expectSystemReady() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700724 mAlarmManager.remove(isA(PendingIntent.class));
725 expectLastCall().anyTimes();
726
727 mAlarmManager.setInexactRepeating(
728 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
729 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700730
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700731 mNetManager.setGlobalAlert(anyLong());
732 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700733 }
734
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700735 private void expectNetworkState(NetworkState... state) throws Exception {
736 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
737 }
738
739 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
740 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
741 }
742
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700743 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
744 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700745 }
746
747 private void expectDefaultSettings() throws Exception {
748 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
749 }
750
751 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
752 throws Exception {
753 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
754 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
755 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
756 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
757 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
758 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700759 expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700760 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
761 }
762
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700763 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700764 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
765 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700766 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700767 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
768 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
769 }
770
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700771 private void expectNetworkStatsPoll() throws Exception {
772 mNetManager.setGlobalAlert(anyLong());
773 expectLastCall().anyTimes();
774 }
775
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700776 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700777 final File networkFile = new File(mStatsDir, "netstats.bin");
778 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700779 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700780 assertTrue(networkFile.exists());
781 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700782 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700783 assertFalse(networkFile.exists());
784 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700785 }
786 }
787
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700788 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
789 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
790 final int i = stats.findIndex(iface, uid, set, tag);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700791 final NetworkStats.Entry entry = stats.getValues(i, null);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700792 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
793 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
794 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
795 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
796 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700797 }
798
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700799 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
800 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700801 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
802 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700803 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700804 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700805 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
806 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700807 }
808
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700809 private static NetworkState buildWifiState() {
810 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
811 info.setDetailedState(DetailedState.CONNECTED, null, null);
812 final LinkProperties prop = new LinkProperties();
813 prop.setInterfaceName(TEST_IFACE);
814 return new NetworkState(info, prop, null);
815 }
816
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700817 private static NetworkState buildMobile3gState(String subscriberId) {
818 final NetworkInfo info = new NetworkInfo(
819 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
820 info.setDetailedState(DetailedState.CONNECTED, null, null);
821 final LinkProperties prop = new LinkProperties();
822 prop.setInterfaceName(TEST_IFACE);
823 return new NetworkState(info, prop, null, subscriberId);
824 }
825
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700826 private static NetworkState buildMobile4gState() {
827 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
828 info.setDetailedState(DetailedState.CONNECTED, null, null);
829 final LinkProperties prop = new LinkProperties();
830 prop.setInterfaceName(TEST_IFACE);
831 return new NetworkState(info, prop, null);
832 }
833
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700834 private NetworkStats buildEmptyStats() {
835 return new NetworkStats(getElapsedRealtime(), 0);
836 }
837
838 private long getElapsedRealtime() {
839 return mElapsedRealtime;
840 }
841
842 private long startTimeMillis() {
843 return TEST_START;
844 }
845
846 private long currentTimeMillis() {
847 return startTimeMillis() + mElapsedRealtime;
848 }
849
850 private void incrementCurrentTime(long duration) {
851 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700852 }
853
Jeff Sharkey3f391352011-06-05 17:42:53 -0700854 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700855 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700856 }
857
858 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700859 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
860 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700861 }
862}