blob: 6138490781a0f5d153902113d4f6983d3ef5c14d [file] [log] [blame]
Jeff Sharkey3f391352011-06-05 17:42:53 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070019import static android.content.Intent.ACTION_UID_REMOVED;
20import static android.content.Intent.EXTRA_UID;
Jeff Sharkey3f391352011-06-05 17:42:53 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070022import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070023import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070024import static android.net.ConnectivityManager.TYPE_WIMAX;
25import static android.net.NetworkStats.IFACE_ALL;
Jeff 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;
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.eq;
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.expectLastCall;
45import static org.easymock.EasyMock.isA;
46
47import android.app.AlarmManager;
48import android.app.IAlarmManager;
49import android.app.PendingIntent;
50import android.content.Intent;
51import android.net.IConnectivityManager;
52import android.net.LinkProperties;
53import android.net.NetworkInfo;
54import android.net.NetworkInfo.DetailedState;
55import android.net.NetworkState;
56import android.net.NetworkStats;
57import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070058import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070059import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070060import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070061import android.test.AndroidTestCase;
62import android.test.suitebuilder.annotation.LargeTest;
63import android.util.TrustedTime;
64
65import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070066import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070067
68import org.easymock.EasyMock;
69
70import java.io.File;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070071import java.util.concurrent.Future;
72
73import libcore.io.IoUtils;
Jeff Sharkey3f391352011-06-05 17:42:53 -070074
75/**
76 * Tests for {@link NetworkStatsService}.
77 */
78@LargeTest
79public class NetworkStatsServiceTest extends AndroidTestCase {
80 private static final String TAG = "NetworkStatsServiceTest";
81
82 private static final String TEST_IFACE = "test0";
83 private static final long TEST_START = 1194220800000L;
84
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070085 private static final String IMSI_1 = "310004";
86 private static final String IMSI_2 = "310260";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070087
Jeff Sharkey4e814c32011-07-14 20:37:37 -070088 private static NetworkTemplate sTemplateWifi = buildTemplateWifi();
89 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
90 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070091
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070092 private static final int UID_RED = 1001;
93 private static final int UID_BLUE = 1002;
94 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070095
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070096 private long mElapsedRealtime;
97
Jeff Sharkey3f391352011-06-05 17:42:53 -070098 private BroadcastInterceptingContext mServiceContext;
99 private File mStatsDir;
100
101 private INetworkManagementService mNetManager;
102 private IAlarmManager mAlarmManager;
103 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700104 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700105 private IConnectivityManager mConnManager;
106
107 private NetworkStatsService mService;
108
109 @Override
110 public void setUp() throws Exception {
111 super.setUp();
112
113 mServiceContext = new BroadcastInterceptingContext(getContext());
114 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700115 if (mStatsDir.exists()) {
116 IoUtils.deleteContents(mStatsDir);
117 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700118
119 mNetManager = createMock(INetworkManagementService.class);
120 mAlarmManager = createMock(IAlarmManager.class);
121 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700122 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700123 mConnManager = createMock(IConnectivityManager.class);
124
125 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700126 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700127 mService.bindConnectivityManager(mConnManager);
128
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700129 mElapsedRealtime = 0L;
130
131 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700132 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700133 expectNetworkStatsSummary(buildEmptyStats());
134 expectNetworkStatsUidDetail(buildEmptyStats());
135 final Future<?> firstPoll = expectSystemReady();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700136
137 replay();
138 mService.systemReady();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700139 firstPoll.get();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700140 verifyAndReset();
141
142 }
143
144 @Override
145 public void tearDown() throws Exception {
146 for (File file : mStatsDir.listFiles()) {
147 file.delete();
148 }
149
150 mServiceContext = null;
151 mStatsDir = null;
152
153 mNetManager = null;
154 mAlarmManager = null;
155 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700156 mSettings = null;
157 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700158
159 mService = null;
160
161 super.tearDown();
162 }
163
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700164 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700165 // pretend that wifi network comes online; service should ask about full
166 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700167 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700168 expectDefaultSettings();
169 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700170 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700171
172 replay();
173 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700174
175 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700176 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700177 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700178
179 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700180 incrementCurrentTime(HOUR_IN_MILLIS);
181 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700182 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700183 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
184 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
185 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700186
187 replay();
188 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700189
190 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700191 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700192 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700193
194 // and bump forward again, with counters going higher. this is
195 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700196 incrementCurrentTime(DAY_IN_MILLIS);
197 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700198 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700199 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
200 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
201 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700202
203 replay();
204 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700205
206 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700207 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700208 verifyAndReset();
209
Jeff Sharkey3f391352011-06-05 17:42:53 -0700210 }
211
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700212 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700213 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700214
215 // pretend that wifi network comes online; service should ask about full
216 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700217 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700218 expectDefaultSettings();
219 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700220 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700221
222 replay();
223 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700224
225 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700226 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700227 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700228
229 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700230 incrementCurrentTime(HOUR_IN_MILLIS);
231 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700232 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700233 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
234 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
235 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
236 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
237 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
238 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
239 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
240 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700241
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700242 mService.setUidForeground(UID_RED, false);
243 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
244 mService.setUidForeground(UID_RED, true);
245 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700246
Jeff Sharkey3f391352011-06-05 17:42:53 -0700247 replay();
248 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700249
250 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700251 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700252 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
253 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
254 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
255 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700256 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700257
258 // graceful shutdown system, which should trigger persist of stats, and
259 // clear any values in memory.
260 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
261
262 // talk with zombie service to assert stats have gone; and assert that
263 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700264 expectDefaultSettings();
265 replay();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700266 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700267 verifyAndReset();
268
269 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700270
271 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700272 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700273 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700274 expectNetworkStatsSummary(buildEmptyStats());
275 expectNetworkStatsUidDetail(buildEmptyStats());
276 final Future<?> firstPoll = expectSystemReady();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700277
278 replay();
279 mService.systemReady();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700280 firstPoll.get();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700281
282 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700283 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700284 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
285 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
286 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
287 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700288 verifyAndReset();
289
290 }
291
292 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700293 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700294
295 assertStatsFilesExist(false);
296
297 // pretend that wifi network comes online; service should ask about full
298 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700299 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700300 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
301 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700302 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700303
304 replay();
305 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
306 verifyAndReset();
307
308 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700309 incrementCurrentTime(2 * HOUR_IN_MILLIS);
310 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700311 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700312 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
313 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
314 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700315
316 replay();
317 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
318
319 // verify service recorded history
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700320 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700321 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700322 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
323 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700324 verifyAndReset();
325
326 // now change bucket duration setting and trigger another poll with
327 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700328 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700329 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700330 expectNetworkStatsSummary(buildEmptyStats());
331 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700332
333 replay();
334 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
335
336 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700337 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700338 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700339 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
340 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700341 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700342
343 }
344
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700345 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700346 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700347 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700348 expectDefaultSettings();
349 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700350 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700351
352 replay();
353 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
354 verifyAndReset();
355
356 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700357 incrementCurrentTime(HOUR_IN_MILLIS);
358 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700359 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700360 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
361 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
362 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
363 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
364 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
365 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700366
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700367 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700368
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700369 replay();
370 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
371
372 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700373 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
374 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700375 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
376 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700377 verifyAndReset();
378
379 // now switch networks; this also tests that we're okay with interfaces
380 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700381 incrementCurrentTime(HOUR_IN_MILLIS);
382 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700383 expectDefaultSettings();
384 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700385 expectNetworkStatsSummary(buildEmptyStats());
386 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700387
388 replay();
389 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
390 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
391 verifyAndReset();
392
393 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700394 incrementCurrentTime(HOUR_IN_MILLIS);
395 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700396 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700397 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
398 .addIfaceValues(TEST_IFACE, 128L, 1L, 1024L, 8L));
399 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
400 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 1024L, 8L, 0L)
401 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700402
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700403 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700404
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700405 replay();
406 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
407
408 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700409 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700410 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
411 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700412
413 // and verify new history also recorded under different template, which
414 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700415 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
416 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
417 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700418 verifyAndReset();
419
420 }
421
422 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700423 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700424 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700425 expectDefaultSettings();
426 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700427 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700428
429 replay();
430 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
431 verifyAndReset();
432
433 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700434 incrementCurrentTime(HOUR_IN_MILLIS);
435 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700436 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700437 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
438 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
439 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
440 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
441 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
442 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
443 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700444
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700445 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700446
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700447 replay();
448 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
449
450 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700451 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
452 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700453 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
454 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700455 verifyAndReset();
456
457 // now pretend two UIDs are uninstalled, which should migrate stats to
458 // special "removed" bucket.
459 expectDefaultSettings();
460 replay();
461 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700462 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700463 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700464 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700465 mServiceContext.sendBroadcast(intent);
466
467 // existing uid and total should remain unchanged; but removed UID
468 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700469 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
470 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
471 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700472 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
473 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700474 verifyAndReset();
475
476 }
477
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700478 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700479 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700480 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700481 expectDefaultSettings();
482 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700483 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700484
485 replay();
486 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
487 verifyAndReset();
488
489 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700490 incrementCurrentTime(HOUR_IN_MILLIS);
491 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700492 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700493 expectNetworkStatsSummary(buildEmptyStats());
494 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
495 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
496 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700497
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700498 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
499
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700500 replay();
501 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
502
503 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700504 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700505 verifyAndReset();
506
507 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700508 incrementCurrentTime(HOUR_IN_MILLIS);
509 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700510 expectDefaultSettings();
511 expectNetworkState(buildMobile4gState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700512 expectNetworkStatsSummary(buildEmptyStats());
513 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700514
515 replay();
516 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
517 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
518 verifyAndReset();
519
520 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700521 incrementCurrentTime(HOUR_IN_MILLIS);
522 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700523 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700524 expectNetworkStatsSummary(buildEmptyStats());
525 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
526 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
527 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700528
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700529 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700530
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700531 replay();
532 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
533
534 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700535 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700536
537 verifyAndReset();
538
539 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700540
541 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700542 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700543 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700544 expectDefaultSettings();
545 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700546 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700547
548 replay();
549 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
550 verifyAndReset();
551
552 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700553 incrementCurrentTime(HOUR_IN_MILLIS);
554 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700555 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700556 expectNetworkStatsSummary(buildEmptyStats());
557 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
558 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
559 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
560 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700561
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700562 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700563
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700564 replay();
565 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
566
567 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700568 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
569 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700570 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700571
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700572 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700573 incrementCurrentTime(HOUR_IN_MILLIS);
574 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700575 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700576 expectNetworkStatsSummary(buildEmptyStats());
577 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
578 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700579
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700580 replay();
581 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
582
583 // first verify entire history present
584 NetworkStats stats = mService.getSummaryForAllUid(
585 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700586 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700587 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
588 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
589 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700590
591 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700592 final long currentTime = currentTimeMillis();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700593 stats = mService.getSummaryForAllUid(
594 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700595 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700596 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
597
598 verifyAndReset();
599 }
600
601 public void testForegroundBackground() throws Exception {
602 // pretend that network comes online
603 expectCurrentTime();
604 expectDefaultSettings();
605 expectNetworkState(buildWifiState());
606 expectNetworkStatsSummary(buildEmptyStats());
607
608 replay();
609 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
610 verifyAndReset();
611
612 // create some initial traffic
613 incrementCurrentTime(HOUR_IN_MILLIS);
614 expectCurrentTime();
615 expectDefaultSettings();
616 expectNetworkStatsSummary(buildEmptyStats());
617 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
618 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
619 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
620
621 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
622
623 replay();
624 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
625
626 // verify service recorded history
627 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
628 verifyAndReset();
629
630 // now switch to foreground
631 incrementCurrentTime(HOUR_IN_MILLIS);
632 expectCurrentTime();
633 expectDefaultSettings();
634 expectNetworkStatsSummary(buildEmptyStats());
635 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
636 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
637 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
638 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
639 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
640
641 mService.setUidForeground(UID_RED, true);
642 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
643
644 replay();
645 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
646
647 // test that we combined correctly
648 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
649
650 // verify entire history present
651 final NetworkStats stats = mService.getSummaryForAllUid(
652 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
653 assertEquals(4, stats.size());
654 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
655 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
656 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
657 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700658
659 verifyAndReset();
660 }
661
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700662 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
663 long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700664 final NetworkStatsHistory history = mService.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700665 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
666 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700667 }
668
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700669 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
670 long txBytes, long txPackets, int operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700671 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
672 }
673
674 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
675 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700676 final NetworkStatsHistory history = mService.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700677 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700678 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
679 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700680 }
681
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700682 private Future<?> expectSystemReady() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700683 mAlarmManager.remove(isA(PendingIntent.class));
684 expectLastCall().anyTimes();
685
686 mAlarmManager.setInexactRepeating(
687 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
688 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700689
690 return mServiceContext.nextBroadcastIntent(
691 NetworkStatsService.ACTION_NETWORK_STATS_UPDATED);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700692 }
693
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700694 private void expectNetworkState(NetworkState... state) throws Exception {
695 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
696 }
697
698 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
699 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
700 }
701
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700702 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
703 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700704 }
705
706 private void expectDefaultSettings() throws Exception {
707 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
708 }
709
710 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
711 throws Exception {
712 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
713 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
714 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
715 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
716 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
717 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700718 expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700719 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
720 }
721
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700722 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700723 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
724 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700725 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700726 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
727 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
728 }
729
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700730 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700731 final File networkFile = new File(mStatsDir, "netstats.bin");
732 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700733 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700734 assertTrue(networkFile.exists());
735 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700736 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700737 assertFalse(networkFile.exists());
738 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700739 }
740 }
741
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700742 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
743 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
744 final int i = stats.findIndex(iface, uid, set, tag);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700745 final NetworkStats.Entry entry = stats.getValues(i, null);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700746 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
747 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
748 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
749 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
750 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700751 }
752
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700753 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
754 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700755 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
756 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700757 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700758 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700759 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
760 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700761 }
762
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700763 private static NetworkState buildWifiState() {
764 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
765 info.setDetailedState(DetailedState.CONNECTED, null, null);
766 final LinkProperties prop = new LinkProperties();
767 prop.setInterfaceName(TEST_IFACE);
768 return new NetworkState(info, prop, null);
769 }
770
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700771 private static NetworkState buildMobile3gState(String subscriberId) {
772 final NetworkInfo info = new NetworkInfo(
773 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
774 info.setDetailedState(DetailedState.CONNECTED, null, null);
775 final LinkProperties prop = new LinkProperties();
776 prop.setInterfaceName(TEST_IFACE);
777 return new NetworkState(info, prop, null, subscriberId);
778 }
779
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700780 private static NetworkState buildMobile4gState() {
781 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
782 info.setDetailedState(DetailedState.CONNECTED, null, null);
783 final LinkProperties prop = new LinkProperties();
784 prop.setInterfaceName(TEST_IFACE);
785 return new NetworkState(info, prop, null);
786 }
787
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700788 private NetworkStats buildEmptyStats() {
789 return new NetworkStats(getElapsedRealtime(), 0);
790 }
791
792 private long getElapsedRealtime() {
793 return mElapsedRealtime;
794 }
795
796 private long startTimeMillis() {
797 return TEST_START;
798 }
799
800 private long currentTimeMillis() {
801 return startTimeMillis() + mElapsedRealtime;
802 }
803
804 private void incrementCurrentTime(long duration) {
805 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700806 }
807
Jeff Sharkey3f391352011-06-05 17:42:53 -0700808 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700809 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700810 }
811
812 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700813 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
814 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700815 }
816}