blob: 99ae027f4b16389de7e0cf61840628e7c9e10484 [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.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700275 expectCurrentTime();
276 expectDefaultSettings();
277 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700278 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700279 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700280
281 // talk with zombie service to assert stats have gone; and assert that
282 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700283 expectDefaultSettings();
284 replay();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700285 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700286 verifyAndReset();
287
288 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700289
290 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700291 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700292 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700293 expectNetworkStatsSummary(buildEmptyStats());
294 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700295 expectSystemReady();
296
297 // catch INetworkManagementEventObserver during systemReady()
298 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
299 INetworkManagementEventObserver>();
300 mNetManager.registerObserver(capture(networkObserver));
301 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700302
303 replay();
304 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700305
306 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700307
308 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700309 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700310 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
311 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
312 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
313 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700314 verifyAndReset();
315
316 }
317
318 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700319 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700320
321 assertStatsFilesExist(false);
322
323 // pretend that wifi network comes online; service should ask about full
324 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700325 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700326 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
327 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700328 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700329 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700330
331 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700332 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700333 verifyAndReset();
334
335 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700336 incrementCurrentTime(2 * HOUR_IN_MILLIS);
337 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700338 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700339 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
340 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
341 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700342 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700343
344 replay();
345 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
346
347 // verify service recorded history
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700348 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700349 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700350 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
351 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700352 verifyAndReset();
353
354 // now change bucket duration setting and trigger another poll with
355 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700356 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700357 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700358 expectNetworkStatsSummary(buildEmptyStats());
359 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700360 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700361
362 replay();
363 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
364
365 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700366 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700367 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700368 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
369 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700370 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700371
372 }
373
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700374 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700375 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700376 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700377 expectDefaultSettings();
378 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700379 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700380 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700381
382 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700383 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700384 verifyAndReset();
385
386 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700387 incrementCurrentTime(HOUR_IN_MILLIS);
388 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700389 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700390 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
391 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
392 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
393 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
394 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
395 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700396 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700397
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700398 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700399
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700400 replay();
401 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
402
403 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700404 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
405 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700406 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
407 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700408 verifyAndReset();
409
410 // now switch networks; this also tests that we're okay with interfaces
411 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700412 incrementCurrentTime(HOUR_IN_MILLIS);
413 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700414 expectDefaultSettings();
415 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700416 expectNetworkStatsSummary(buildEmptyStats());
417 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700418 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700419
420 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700421 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700422 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
423 verifyAndReset();
424
425 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700426 incrementCurrentTime(HOUR_IN_MILLIS);
427 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700428 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700429 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
430 .addIfaceValues(TEST_IFACE, 128L, 1L, 1024L, 8L));
431 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
432 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 1024L, 8L, 0L)
433 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700434 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700435
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700436 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700437
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700438 replay();
439 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
440
441 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700442 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700443 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
444 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700445
446 // and verify new history also recorded under different template, which
447 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700448 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
449 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
450 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700451 verifyAndReset();
452
453 }
454
455 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700456 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700457 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700458 expectDefaultSettings();
459 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700460 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700461 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700462
463 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700464 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700465 verifyAndReset();
466
467 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700468 incrementCurrentTime(HOUR_IN_MILLIS);
469 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700470 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700471 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
472 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
473 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
474 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
475 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
476 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
477 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700478 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700479
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700480 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700481
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700482 replay();
483 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
484
485 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700486 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
487 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700488 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
489 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700490 verifyAndReset();
491
492 // now pretend two UIDs are uninstalled, which should migrate stats to
493 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700494 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700495 expectDefaultSettings();
496 replay();
497 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700498 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700499 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700500 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700501 mServiceContext.sendBroadcast(intent);
502
503 // existing uid and total should remain unchanged; but removed UID
504 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700505 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
506 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
507 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700508 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
509 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700510 verifyAndReset();
511
512 }
513
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700514 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700515 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700516 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700517 expectDefaultSettings();
518 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700519 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700520 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700521
522 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700523 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700524 verifyAndReset();
525
526 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700527 incrementCurrentTime(HOUR_IN_MILLIS);
528 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700529 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700530 expectNetworkStatsSummary(buildEmptyStats());
531 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
532 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
533 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700534 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700535
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700536 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
537
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700538 replay();
539 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
540
541 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700542 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700543 verifyAndReset();
544
545 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700546 incrementCurrentTime(HOUR_IN_MILLIS);
547 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700548 expectDefaultSettings();
549 expectNetworkState(buildMobile4gState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700550 expectNetworkStatsSummary(buildEmptyStats());
551 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700552 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700553
554 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700555 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700556 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
557 verifyAndReset();
558
559 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700560 incrementCurrentTime(HOUR_IN_MILLIS);
561 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700562 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700563 expectNetworkStatsSummary(buildEmptyStats());
564 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
565 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
566 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700567 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700568
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700569 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700570
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700571 replay();
572 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
573
574 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700575 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700576
577 verifyAndReset();
578
579 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700580
581 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700582 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700583 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700584 expectDefaultSettings();
585 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700586 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700587 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700588
589 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700590 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700591 verifyAndReset();
592
593 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700594 incrementCurrentTime(HOUR_IN_MILLIS);
595 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700596 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700597 expectNetworkStatsSummary(buildEmptyStats());
598 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
599 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
600 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
601 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700602 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700603
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700604 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700605
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700606 replay();
607 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
608
609 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700610 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
611 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700612 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700613
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700614 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700615 incrementCurrentTime(HOUR_IN_MILLIS);
616 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700617 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700618 expectNetworkStatsSummary(buildEmptyStats());
619 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
620 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700621 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700622
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700623 replay();
624 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
625
626 // first verify entire history present
627 NetworkStats stats = mService.getSummaryForAllUid(
628 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700629 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700630 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
631 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
632 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700633
634 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700635 final long currentTime = currentTimeMillis();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700636 stats = mService.getSummaryForAllUid(
637 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700638 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700639 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
640
641 verifyAndReset();
642 }
643
644 public void testForegroundBackground() throws Exception {
645 // pretend that network comes online
646 expectCurrentTime();
647 expectDefaultSettings();
648 expectNetworkState(buildWifiState());
649 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700650 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700651
652 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700653 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700654 verifyAndReset();
655
656 // create some initial traffic
657 incrementCurrentTime(HOUR_IN_MILLIS);
658 expectCurrentTime();
659 expectDefaultSettings();
660 expectNetworkStatsSummary(buildEmptyStats());
661 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
662 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
663 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700664 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700665
666 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
667
668 replay();
669 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
670
671 // verify service recorded history
672 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
673 verifyAndReset();
674
675 // now switch to foreground
676 incrementCurrentTime(HOUR_IN_MILLIS);
677 expectCurrentTime();
678 expectDefaultSettings();
679 expectNetworkStatsSummary(buildEmptyStats());
680 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
681 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
682 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
683 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
684 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700685 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700686
687 mService.setUidForeground(UID_RED, true);
688 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
689
690 replay();
691 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
692
693 // test that we combined correctly
694 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
695
696 // verify entire history present
697 final NetworkStats stats = mService.getSummaryForAllUid(
698 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
699 assertEquals(4, stats.size());
700 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
701 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
702 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
703 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700704
705 verifyAndReset();
706 }
707
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700708 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
709 long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700710 final NetworkStatsHistory history = mService.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700711 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
712 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700713 }
714
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700715 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
716 long txBytes, long txPackets, int operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700717 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
718 }
719
720 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
721 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700722 final NetworkStatsHistory history = mService.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700723 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700724 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
725 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700726 }
727
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700728 private void expectSystemReady() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700729 mAlarmManager.remove(isA(PendingIntent.class));
730 expectLastCall().anyTimes();
731
732 mAlarmManager.setInexactRepeating(
733 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
734 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700735
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700736 mNetManager.setGlobalAlert(anyLong());
737 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700738 }
739
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700740 private void expectNetworkState(NetworkState... state) throws Exception {
741 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
742 }
743
744 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
745 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
746 }
747
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700748 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
749 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700750 }
751
752 private void expectDefaultSettings() throws Exception {
753 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
754 }
755
756 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
757 throws Exception {
758 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
759 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
760 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
761 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
762 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
763 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700764 expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700765 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
766 }
767
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700768 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700769 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
770 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700771 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700772 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
773 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
774 }
775
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700776 private void expectNetworkStatsPoll() throws Exception {
777 mNetManager.setGlobalAlert(anyLong());
778 expectLastCall().anyTimes();
779 }
780
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700781 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700782 final File networkFile = new File(mStatsDir, "netstats.bin");
783 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700784 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700785 assertTrue(networkFile.exists());
786 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700787 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700788 assertFalse(networkFile.exists());
789 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700790 }
791 }
792
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700793 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
794 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
795 final int i = stats.findIndex(iface, uid, set, tag);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700796 final NetworkStats.Entry entry = stats.getValues(i, null);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700797 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
798 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
799 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
800 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
801 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700802 }
803
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700804 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
805 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700806 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
807 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700808 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700809 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700810 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
811 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700812 }
813
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700814 private static NetworkState buildWifiState() {
815 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
816 info.setDetailedState(DetailedState.CONNECTED, null, null);
817 final LinkProperties prop = new LinkProperties();
818 prop.setInterfaceName(TEST_IFACE);
819 return new NetworkState(info, prop, null);
820 }
821
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700822 private static NetworkState buildMobile3gState(String subscriberId) {
823 final NetworkInfo info = new NetworkInfo(
824 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
825 info.setDetailedState(DetailedState.CONNECTED, null, null);
826 final LinkProperties prop = new LinkProperties();
827 prop.setInterfaceName(TEST_IFACE);
828 return new NetworkState(info, prop, null, subscriberId);
829 }
830
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700831 private static NetworkState buildMobile4gState() {
832 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
833 info.setDetailedState(DetailedState.CONNECTED, null, null);
834 final LinkProperties prop = new LinkProperties();
835 prop.setInterfaceName(TEST_IFACE);
836 return new NetworkState(info, prop, null);
837 }
838
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700839 private NetworkStats buildEmptyStats() {
840 return new NetworkStats(getElapsedRealtime(), 0);
841 }
842
843 private long getElapsedRealtime() {
844 return mElapsedRealtime;
845 }
846
847 private long startTimeMillis() {
848 return TEST_START;
849 }
850
851 private long currentTimeMillis() {
852 return startTimeMillis() + mElapsedRealtime;
853 }
854
855 private void incrementCurrentTime(long duration) {
856 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700857 }
858
Jeff Sharkey3f391352011-06-05 17:42:53 -0700859 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700860 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700861 }
862
863 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700864 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
865 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700866 }
867}