blob: cdc4d784cf01c8881b82993cee86a07f40d53f51 [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;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070033import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey241dde22012-02-03 14:50:07 -080034import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070035import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey905b5892011-09-30 15:19:49 -070036import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey3f391352011-06-05 17:42:53 -070037import static android.text.format.DateUtils.DAY_IN_MILLIS;
38import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070039import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
40import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070041import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
42import static org.easymock.EasyMock.anyLong;
Jeff Sharkey63abc372012-01-11 18:38:16 -080043import static org.easymock.EasyMock.aryEq;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070044import static org.easymock.EasyMock.capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070045import static org.easymock.EasyMock.createMock;
46import static org.easymock.EasyMock.eq;
47import static org.easymock.EasyMock.expect;
48import static org.easymock.EasyMock.expectLastCall;
49import static org.easymock.EasyMock.isA;
50
51import android.app.AlarmManager;
52import android.app.IAlarmManager;
53import android.app.PendingIntent;
54import android.content.Intent;
55import android.net.IConnectivityManager;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070056import android.net.INetworkManagementEventObserver;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070057import android.net.INetworkStatsSession;
Jeff Sharkey3f391352011-06-05 17:42:53 -070058import android.net.LinkProperties;
59import android.net.NetworkInfo;
60import android.net.NetworkInfo.DetailedState;
61import android.net.NetworkState;
62import android.net.NetworkStats;
63import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070064import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070065import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070066import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070067import android.test.AndroidTestCase;
68import android.test.suitebuilder.annotation.LargeTest;
Jeff Sharkey63abc372012-01-11 18:38:16 -080069import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey3f391352011-06-05 17:42:53 -070070import android.util.TrustedTime;
71
72import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070073import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey63abc372012-01-11 18:38:16 -080074import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
Jeff Sharkey3f391352011-06-05 17:42:53 -070075
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070076import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070077import org.easymock.EasyMock;
78
79import java.io.File;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070080
81import libcore.io.IoUtils;
Jeff Sharkey3f391352011-06-05 17:42:53 -070082
83/**
84 * Tests for {@link NetworkStatsService}.
85 */
86@LargeTest
87public class NetworkStatsServiceTest extends AndroidTestCase {
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070088 private static final String TAG = "NetworkStatsServiceTest";
Jeff Sharkey3f391352011-06-05 17:42:53 -070089
90 private static final String TEST_IFACE = "test0";
Jeff Sharkey163e6442011-10-31 16:37:52 -070091 private static final String TEST_IFACE2 = "test1";
Jeff Sharkey3f391352011-06-05 17:42:53 -070092 private static final long TEST_START = 1194220800000L;
93
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070094 private static final String IMSI_1 = "310004";
95 private static final String IMSI_2 = "310260";
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -070096 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070097
Jeff Sharkeye8914c32012-05-01 16:26:09 -070098 private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
Jeff Sharkey4e814c32011-07-14 20:37:37 -070099 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
100 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700101
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700102 private static final int UID_RED = 1001;
103 private static final int UID_BLUE = 1002;
104 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700105
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700106 private long mElapsedRealtime;
107
Jeff Sharkey3f391352011-06-05 17:42:53 -0700108 private BroadcastInterceptingContext mServiceContext;
109 private File mStatsDir;
110
111 private INetworkManagementService mNetManager;
112 private IAlarmManager mAlarmManager;
113 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700114 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700115 private IConnectivityManager mConnManager;
116
117 private NetworkStatsService mService;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700118 private INetworkStatsSession mSession;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700119 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700120
121 @Override
122 public void setUp() throws Exception {
123 super.setUp();
124
125 mServiceContext = new BroadcastInterceptingContext(getContext());
126 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700127 if (mStatsDir.exists()) {
128 IoUtils.deleteContents(mStatsDir);
129 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700130
131 mNetManager = createMock(INetworkManagementService.class);
132 mAlarmManager = createMock(IAlarmManager.class);
133 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700134 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700135 mConnManager = createMock(IConnectivityManager.class);
136
137 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700138 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700139 mService.bindConnectivityManager(mConnManager);
140
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700141 mElapsedRealtime = 0L;
142
143 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700144 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700145 expectNetworkStatsSummary(buildEmptyStats());
146 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700147 expectSystemReady();
148
149 // catch INetworkManagementEventObserver during systemReady()
150 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
151 INetworkManagementEventObserver>();
152 mNetManager.registerObserver(capture(networkObserver));
153 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700154
155 replay();
156 mService.systemReady();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700157 mSession = mService.openSession();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700158 verifyAndReset();
159
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700160 mNetworkObserver = networkObserver.getValue();
161
Jeff Sharkey3f391352011-06-05 17:42:53 -0700162 }
163
164 @Override
165 public void tearDown() throws Exception {
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700166 IoUtils.deleteContents(mStatsDir);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700167
168 mServiceContext = null;
169 mStatsDir = null;
170
171 mNetManager = null;
172 mAlarmManager = null;
173 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700174 mSettings = null;
175 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700176
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700177 mSession.close();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700178 mService = null;
179
180 super.tearDown();
181 }
182
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700183 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700184 // pretend that wifi network comes online; service should ask about full
185 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700186 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700187 expectDefaultSettings();
188 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700189 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700190 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700191 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700192
193 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700194 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700195
196 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700197 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700198 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700199
200 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700201 incrementCurrentTime(HOUR_IN_MILLIS);
202 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700203 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700204 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
205 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
206 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700207 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700208
209 replay();
210 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700211
212 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700213 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700214 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700215
216 // and bump forward again, with counters going higher. this is
217 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700218 incrementCurrentTime(DAY_IN_MILLIS);
219 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700220 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700221 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
222 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
223 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700224 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700225
226 replay();
227 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700228
229 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700230 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700231 verifyAndReset();
232
Jeff Sharkey3f391352011-06-05 17:42:53 -0700233 }
234
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700235 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700236 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700237
238 // pretend that wifi network comes online; service should ask about full
239 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700240 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700241 expectDefaultSettings();
242 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700243 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700244 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700245 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700246
247 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700248 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700249
250 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700251 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700252 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700253
254 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700255 incrementCurrentTime(HOUR_IN_MILLIS);
256 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700257 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700258 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
259 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
260 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
261 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
262 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
263 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
264 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
265 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700266 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700267
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700268 mService.setUidForeground(UID_RED, false);
269 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
270 mService.setUidForeground(UID_RED, true);
271 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700272
Jeff Sharkey3f391352011-06-05 17:42:53 -0700273 replay();
274 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700275
276 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700277 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700278 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
279 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
280 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
281 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700282 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700283
284 // graceful shutdown system, which should trigger persist of stats, and
285 // clear any values in memory.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700286 expectCurrentTime();
287 expectDefaultSettings();
288 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700289 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700290 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700291
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700292 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700293
294 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700295 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700296 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700297 expectNetworkStatsSummary(buildEmptyStats());
298 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700299 expectSystemReady();
300
301 // catch INetworkManagementEventObserver during systemReady()
302 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
303 INetworkManagementEventObserver>();
304 mNetManager.registerObserver(capture(networkObserver));
305 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700306
307 replay();
308 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700309
310 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700311
312 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700313 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700314 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
315 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
316 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
317 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700318 verifyAndReset();
319
320 }
321
Jeff Sharkey63abc372012-01-11 18:38:16 -0800322 // TODO: simulate reboot to test bucket resize
323 @Suppress
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700324 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700325 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700326
327 assertStatsFilesExist(false);
328
329 // pretend that wifi network comes online; service should ask about full
330 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700331 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700332 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
333 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700334 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700335 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700336 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700337
338 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700339 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700340 verifyAndReset();
341
342 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700343 incrementCurrentTime(2 * HOUR_IN_MILLIS);
344 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700345 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700346 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
347 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
348 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700349 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700350
351 replay();
352 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
353
354 // verify service recorded history
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700355 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700356 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700357 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
358 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700359 verifyAndReset();
360
361 // now change bucket duration setting and trigger another poll with
362 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700363 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700364 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700365 expectNetworkStatsSummary(buildEmptyStats());
366 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700367 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700368
369 replay();
370 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
371
372 // verify identical stats, but spread across 4 buckets now
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700373 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700374 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700375 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
376 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700377 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700378
379 }
380
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700381 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700382 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700383 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700384 expectDefaultSettings();
385 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700386 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700387 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700388 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700389
390 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700391 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700392 verifyAndReset();
393
394 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700395 incrementCurrentTime(HOUR_IN_MILLIS);
396 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700397 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700398 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
399 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
400 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
401 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
402 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
403 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700404 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700405
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700406 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700407
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700408 replay();
409 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
410
411 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700412 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
413 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700414 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
415 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700416 verifyAndReset();
417
418 // now switch networks; this also tests that we're okay with interfaces
419 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700420 incrementCurrentTime(HOUR_IN_MILLIS);
421 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700422 expectDefaultSettings();
423 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkey163e6442011-10-31 16:37:52 -0700424 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
425 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
426 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
427 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
428 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
429 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700430 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700431
432 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700433 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700434 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
435 verifyAndReset();
436
437 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700438 incrementCurrentTime(HOUR_IN_MILLIS);
439 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700440 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700441 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700442 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700443 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700444 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
445 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
446 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700447 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700448 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700449
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700450 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700451
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700452 replay();
453 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
454
455 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700456 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700457 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
458 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700459
460 // and verify new history also recorded under different template, which
461 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700462 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
463 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
464 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700465 verifyAndReset();
466
467 }
468
469 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700470 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700471 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700472 expectDefaultSettings();
473 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700474 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700475 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700476 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700477
478 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700479 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700480 verifyAndReset();
481
482 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700483 incrementCurrentTime(HOUR_IN_MILLIS);
484 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700485 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700486 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
487 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
488 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
489 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
490 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
491 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
492 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700493 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700494
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700495 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700496
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700497 replay();
498 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
499
500 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700501 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
502 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700503 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
504 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700505 verifyAndReset();
506
507 // now pretend two UIDs are uninstalled, which should migrate stats to
508 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700509 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700510 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700511 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
512 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
513 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
514 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
515 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
516 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
517 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
518 expectNetworkStatsPoll();
519
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700520 replay();
521 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700522 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700523 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700524 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700525 mServiceContext.sendBroadcast(intent);
526
527 // existing uid and total should remain unchanged; but removed UID
528 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700529 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
530 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
531 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700532 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
533 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700534 verifyAndReset();
535
536 }
537
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700538 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700539 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700540 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700541 expectDefaultSettings();
542 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700543 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700544 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700545 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700546
547 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700548 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700549 verifyAndReset();
550
551 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700552 incrementCurrentTime(HOUR_IN_MILLIS);
553 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700554 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700555 expectNetworkStatsSummary(buildEmptyStats());
556 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
557 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
558 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700559 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700560
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700561 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
562
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700563 replay();
564 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
565
566 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700567 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700568 verifyAndReset();
569
570 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700571 incrementCurrentTime(HOUR_IN_MILLIS);
572 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700573 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700574 expectNetworkState(buildMobile4gState(TEST_IFACE2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700575 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey163e6442011-10-31 16:37:52 -0700576 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
577 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
578 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700579 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700580
581 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700582 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700583 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
584 verifyAndReset();
585
586 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700587 incrementCurrentTime(HOUR_IN_MILLIS);
588 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700589 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700590 expectNetworkStatsSummary(buildEmptyStats());
591 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700592 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
593 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
594 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
595 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700596 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700597
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700598 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700599
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700600 replay();
601 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
602
603 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700604 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700605
606 verifyAndReset();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700607 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700608
609 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700610 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700611 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700612 expectDefaultSettings();
613 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700614 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700615 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700616 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700617
618 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700619 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700620 verifyAndReset();
621
622 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700623 incrementCurrentTime(HOUR_IN_MILLIS);
624 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700625 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700626 expectNetworkStatsSummary(buildEmptyStats());
627 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
628 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
629 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
630 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700631 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700632
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700633 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700634
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700635 replay();
636 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
637
638 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700639 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
640 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700641 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700642
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700643 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700644 incrementCurrentTime(HOUR_IN_MILLIS);
645 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700646 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700647 expectNetworkStatsSummary(buildEmptyStats());
648 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700649 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
650 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700651 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700652 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700653
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700654 replay();
655 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
656
657 // first verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700658 NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700659 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700660 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700661 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
662 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
663 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700664
665 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700666 final long currentTime = currentTimeMillis();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700667 stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700668 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700669 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700670 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
671
672 verifyAndReset();
673 }
674
675 public void testForegroundBackground() throws Exception {
676 // pretend that network comes online
677 expectCurrentTime();
678 expectDefaultSettings();
679 expectNetworkState(buildWifiState());
680 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700681 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700682 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700683
684 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700685 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700686 verifyAndReset();
687
688 // create some initial traffic
689 incrementCurrentTime(HOUR_IN_MILLIS);
690 expectCurrentTime();
691 expectDefaultSettings();
692 expectNetworkStatsSummary(buildEmptyStats());
693 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
694 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
695 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700696 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700697
698 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
699
700 replay();
701 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
702
703 // verify service recorded history
704 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
705 verifyAndReset();
706
707 // now switch to foreground
708 incrementCurrentTime(HOUR_IN_MILLIS);
709 expectCurrentTime();
710 expectDefaultSettings();
711 expectNetworkStatsSummary(buildEmptyStats());
712 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
713 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
714 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
715 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
716 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700717 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700718
719 mService.setUidForeground(UID_RED, true);
720 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
721
722 replay();
723 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
724
725 // test that we combined correctly
726 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
727
728 // verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700729 final NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700730 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
731 assertEquals(4, stats.size());
732 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
733 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
734 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
735 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700736
737 verifyAndReset();
738 }
739
Jeff Sharkey905b5892011-09-30 15:19:49 -0700740 public void testTethering() throws Exception {
741 // pretend first mobile network comes online
742 expectCurrentTime();
743 expectDefaultSettings();
744 expectNetworkState(buildMobile3gState(IMSI_1));
745 expectNetworkStatsSummary(buildEmptyStats());
746 expectNetworkStatsUidDetail(buildEmptyStats());
747 expectNetworkStatsPoll();
748
749 replay();
750 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
751 verifyAndReset();
752
753 // create some tethering traffic
754 incrementCurrentTime(HOUR_IN_MILLIS);
755 expectCurrentTime();
756 expectDefaultSettings();
757 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
758 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
Jeff Sharkey63abc372012-01-11 18:38:16 -0800759
760 final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
761 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
Jeff Sharkey905b5892011-09-30 15:19:49 -0700762 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
Jeff Sharkey63abc372012-01-11 18:38:16 -0800763 final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
764 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
765
766 expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
767 expectNetworkStatsPoll();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700768
769 replay();
770 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
771
772 // verify service recorded history
773 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
774 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
775 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
776 verifyAndReset();
777
778 }
779
Jeff Sharkey70c70532012-05-16 14:51:19 -0700780 public void testReportXtOverDev() throws Exception {
781 // bring mobile network online
782 expectCurrentTime();
783 expectDefaultSettings();
784 expectNetworkState(buildMobile3gState(IMSI_1));
785 expectNetworkStatsSummary(buildEmptyStats());
786 expectNetworkStatsUidDetail(buildEmptyStats());
787 expectNetworkStatsPoll();
788
789 replay();
790 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
791 verifyAndReset();
792
793 // create some traffic, but only for DEV, and across 1.5 buckets
794 incrementCurrentTime(90 * MINUTE_IN_MILLIS);
795 expectCurrentTime();
796 expectDefaultSettings();
797 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
798 .addIfaceValues(TEST_IFACE, 6000L, 60L, 3000L, 30L));
799 expectNetworkStatsSummaryXt(buildEmptyStats());
800 expectNetworkStatsUidDetail(buildEmptyStats());
801 expectNetworkStatsPoll();
802
803 replay();
804 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
805
806 // verify service recorded history:
807 // 4000(dev) + 2000(dev)
808 assertNetworkTotal(sTemplateImsi1, 6000L, 60L, 3000L, 30L, 0);
809 verifyAndReset();
810
811 // create traffic on both DEV and XT, across two buckets
812 incrementCurrentTime(2 * HOUR_IN_MILLIS);
813 expectCurrentTime();
814 expectDefaultSettings();
815 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
816 .addIfaceValues(TEST_IFACE, 6004L, 64L, 3004L, 34L));
817 expectNetworkStatsSummaryXt(new NetworkStats(getElapsedRealtime(), 1)
818 .addIfaceValues(TEST_IFACE, 10240L, 0L, 0L, 0L));
819 expectNetworkStatsUidDetail(buildEmptyStats());
820 expectNetworkStatsPoll();
821
822 replay();
823 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
824
825 // verify that we switching reporting at the first atomic XT bucket,
826 // which should give us:
827 // 4000(dev) + 2000(dev) + 1(dev) + 5120(xt) + 2560(xt)
828 assertNetworkTotal(sTemplateImsi1, 13681L, 61L, 3001L, 31L, 0);
829
830 // also test pure-DEV and pure-XT ranges
831 assertNetworkTotal(sTemplateImsi1, startTimeMillis(),
832 startTimeMillis() + 2 * HOUR_IN_MILLIS, 6001L, 61L, 3001L, 31L, 0);
833 assertNetworkTotal(sTemplateImsi1, startTimeMillis() + 2 * HOUR_IN_MILLIS,
834 startTimeMillis() + 4 * HOUR_IN_MILLIS, 7680L, 0L, 0L, 0L, 0);
835
836 verifyAndReset();
837 }
838
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700839 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700840 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700841 assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
842 txPackets, operations);
843 }
844
845 private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
846 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
847 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700848 final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700849 assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
850
851 // verify summary API
852 final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
853 assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes,
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700854 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700855 }
856
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700857 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700858 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700859 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
860 }
861
862 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700863 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700864 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700865 final NetworkStatsHistory history = mSession.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700866 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700867 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
868 txPackets, operations);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700869
870 // verify summary API
871 final NetworkStats stats = mSession.getSummaryForAllUid(
872 template, Long.MIN_VALUE, Long.MAX_VALUE, false);
873 assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
874 operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700875 }
876
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700877 private void expectSystemReady() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700878 mAlarmManager.remove(isA(PendingIntent.class));
879 expectLastCall().anyTimes();
880
881 mAlarmManager.setInexactRepeating(
882 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
883 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700884
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700885 mNetManager.setGlobalAlert(anyLong());
886 expectLastCall().atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800887
888 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700889 }
890
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700891 private void expectNetworkState(NetworkState... state) throws Exception {
892 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800893
894 final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
895 expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700896 }
897
898 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700899 expectNetworkStatsSummaryDev(summary);
900 expectNetworkStatsSummaryXt(summary);
901 }
902
903 private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700904 expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
Jeff Sharkey70c70532012-05-16 14:51:19 -0700905 }
906
907 private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700908 expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700909 }
910
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700911 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800912 expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
913 }
914
915 private void expectNetworkStatsUidDetail(
916 NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
917 throws Exception {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700918 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800919
920 // also include tethering details, since they are folded into UID
921 expect(mConnManager.getTetheredIfacePairs()).andReturn(tetherIfacePairs).atLeastOnce();
922 expect(mNetManager.getNetworkStatsTethering(aryEq(tetherIfacePairs)))
923 .andReturn(tetherStats).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700924 }
925
926 private void expectDefaultSettings() throws Exception {
927 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
928 }
929
Jeff Sharkey63abc372012-01-11 18:38:16 -0800930 private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700931 throws Exception {
932 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700933 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800934 expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
Jeff Sharkey70c70532012-05-16 14:51:19 -0700935 expect(mSettings.getReportXtOverDev()).andReturn(true).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800936
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700937 final Config config = new Config(bucketDuration, deleteAge, deleteAge);
Jeff Sharkey63abc372012-01-11 18:38:16 -0800938 expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700939 expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800940 expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
941 expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700942
943 expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
944 expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
945 expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
946 expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
947 expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700948 }
949
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700950 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700951 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
952 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700953 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700954 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
955 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
956 }
957
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700958 private void expectNetworkStatsPoll() throws Exception {
959 mNetManager.setGlobalAlert(anyLong());
960 expectLastCall().anyTimes();
961 }
962
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700963 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800964 final File basePath = new File(mStatsDir, "netstats");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700965 if (exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800966 assertTrue(basePath.list().length > 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700967 } else {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800968 assertTrue(basePath.list().length == 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700969 }
970 }
971
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700972 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
973 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700974 final NetworkStats.Entry entry = new NetworkStats.Entry();
975 if (set == SET_DEFAULT || set == SET_ALL) {
976 final int i = stats.findIndex(iface, uid, SET_DEFAULT, tag);
977 if (i != -1) {
978 entry.add(stats.getValues(i, null));
979 }
980 }
981 if (set == SET_FOREGROUND || set == SET_ALL) {
982 final int i = stats.findIndex(iface, uid, SET_FOREGROUND, tag);
983 if (i != -1) {
984 entry.add(stats.getValues(i, null));
985 }
986 }
987
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700988 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
989 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
990 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
991 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
992 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700993 }
994
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700995 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
996 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700997 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
998 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700999 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001000 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001001 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1002 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001003 }
1004
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001005 private static NetworkState buildWifiState() {
1006 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1007 info.setDetailedState(DetailedState.CONNECTED, null, null);
1008 final LinkProperties prop = new LinkProperties();
1009 prop.setInterfaceName(TEST_IFACE);
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -07001010 return new NetworkState(info, prop, null, null, TEST_SSID);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001011 }
1012
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001013 private static NetworkState buildMobile3gState(String subscriberId) {
1014 final NetworkInfo info = new NetworkInfo(
1015 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1016 info.setDetailedState(DetailedState.CONNECTED, null, null);
1017 final LinkProperties prop = new LinkProperties();
1018 prop.setInterfaceName(TEST_IFACE);
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001019 return new NetworkState(info, prop, null, subscriberId, null);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001020 }
1021
Jeff Sharkey163e6442011-10-31 16:37:52 -07001022 private static NetworkState buildMobile4gState(String iface) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001023 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1024 info.setDetailedState(DetailedState.CONNECTED, null, null);
1025 final LinkProperties prop = new LinkProperties();
Jeff Sharkey163e6442011-10-31 16:37:52 -07001026 prop.setInterfaceName(iface);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001027 return new NetworkState(info, prop, null);
1028 }
1029
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001030 private NetworkStats buildEmptyStats() {
1031 return new NetworkStats(getElapsedRealtime(), 0);
1032 }
1033
1034 private long getElapsedRealtime() {
1035 return mElapsedRealtime;
1036 }
1037
1038 private long startTimeMillis() {
1039 return TEST_START;
1040 }
1041
1042 private long currentTimeMillis() {
1043 return startTimeMillis() + mElapsedRealtime;
1044 }
1045
1046 private void incrementCurrentTime(long duration) {
1047 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001048 }
1049
Jeff Sharkey3f391352011-06-05 17:42:53 -07001050 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001051 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001052 }
1053
1054 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001055 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1056 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001057 }
1058}