blob: c12f978d02286b5e2935c87fc6a69eb60516621b [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;
Erik Klinef851d6d2015-04-20 16:03:48 +090021import 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;
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;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070042import static org.easymock.EasyMock.anyInt;
Jeff Sharkey3f391352011-06-05 17:42:53 -070043import static org.easymock.EasyMock.anyLong;
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;
David Christiedf2cab22013-08-02 10:54:55 -070066import android.os.WorkSource;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070067import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070068import android.test.AndroidTestCase;
69import android.test.suitebuilder.annotation.LargeTest;
Jeff Sharkey63abc372012-01-11 18:38:16 -080070import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey3f391352011-06-05 17:42:53 -070071import android.util.TrustedTime;
72
73import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070074import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey63abc372012-01-11 18:38:16 -080075import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
Jeff Sharkey3f391352011-06-05 17:42:53 -070076
Jeff Sharkeye4984be2013-09-10 21:03:27 -070077import libcore.io.IoUtils;
78
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070079import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070080import org.easymock.EasyMock;
81
82import java.io.File;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070083
Jeff Sharkey3f391352011-06-05 17:42:53 -070084/**
85 * Tests for {@link NetworkStatsService}.
86 */
87@LargeTest
88public class NetworkStatsServiceTest extends AndroidTestCase {
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070089 private static final String TAG = "NetworkStatsServiceTest";
Jeff Sharkey3f391352011-06-05 17:42:53 -070090
91 private static final String TEST_IFACE = "test0";
Jeff Sharkey163e6442011-10-31 16:37:52 -070092 private static final String TEST_IFACE2 = "test1";
Jeff Sharkey3f391352011-06-05 17:42:53 -070093 private static final long TEST_START = 1194220800000L;
94
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070095 private static final String IMSI_1 = "310004";
96 private static final String IMSI_2 = "310260";
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -070097 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070098
Jeff Sharkeye8914c32012-05-01 16:26:09 -070099 private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700100 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
101 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700102
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700103 private static final int UID_RED = 1001;
104 private static final int UID_BLUE = 1002;
105 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700106
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700107 private long mElapsedRealtime;
108
Jeff Sharkey3f391352011-06-05 17:42:53 -0700109 private BroadcastInterceptingContext mServiceContext;
110 private File mStatsDir;
111
112 private INetworkManagementService mNetManager;
113 private IAlarmManager mAlarmManager;
114 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700115 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700116 private IConnectivityManager mConnManager;
117
118 private NetworkStatsService mService;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700119 private INetworkStatsSession mSession;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700120 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700121
122 @Override
123 public void setUp() throws Exception {
124 super.setUp();
125
126 mServiceContext = new BroadcastInterceptingContext(getContext());
127 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700128 if (mStatsDir.exists()) {
129 IoUtils.deleteContents(mStatsDir);
130 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700131
132 mNetManager = createMock(INetworkManagementService.class);
133 mAlarmManager = createMock(IAlarmManager.class);
134 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700135 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700136 mConnManager = createMock(IConnectivityManager.class);
137
138 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700139 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700140 mService.bindConnectivityManager(mConnManager);
141
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700142 mElapsedRealtime = 0L;
143
144 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700145 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700146 expectNetworkStatsSummary(buildEmptyStats());
147 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700148 expectSystemReady();
149
150 // catch INetworkManagementEventObserver during systemReady()
151 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
152 INetworkManagementEventObserver>();
153 mNetManager.registerObserver(capture(networkObserver));
154 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700155
156 replay();
157 mService.systemReady();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700158 mSession = mService.openSession();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700159 verifyAndReset();
160
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700161 mNetworkObserver = networkObserver.getValue();
162
Jeff Sharkey3f391352011-06-05 17:42:53 -0700163 }
164
165 @Override
166 public void tearDown() throws Exception {
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700167 IoUtils.deleteContents(mStatsDir);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700168
169 mServiceContext = null;
170 mStatsDir = null;
171
172 mNetManager = null;
173 mAlarmManager = null;
174 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700175 mSettings = null;
176 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700177
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700178 mSession.close();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700179 mService = null;
180
181 super.tearDown();
182 }
183
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700184 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700185 // pretend that wifi network comes online; service should ask about full
186 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700187 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700188 expectDefaultSettings();
189 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700190 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700191 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700192 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700193
194 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900195 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700196
197 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700198 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700199 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700200
201 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700202 incrementCurrentTime(HOUR_IN_MILLIS);
203 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700204 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700205 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
206 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
207 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700208 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700209
210 replay();
211 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700212
213 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700214 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700215 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700216
217 // and bump forward again, with counters going higher. this is
218 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700219 incrementCurrentTime(DAY_IN_MILLIS);
220 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700221 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700222 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
223 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
224 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700225 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700226
227 replay();
228 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700229
230 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700231 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700232 verifyAndReset();
233
Jeff Sharkey3f391352011-06-05 17:42:53 -0700234 }
235
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700236 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700237 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700238
239 // pretend that wifi network comes online; service should ask about full
240 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700241 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700242 expectDefaultSettings();
243 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700244 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700245 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700246 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700247
248 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900249 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700250
251 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700252 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700253 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700254
255 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700256 incrementCurrentTime(HOUR_IN_MILLIS);
257 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700258 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700259 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
260 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
261 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
262 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
263 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
264 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
265 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
266 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700267 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700268
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700269 mService.setUidForeground(UID_RED, false);
270 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
271 mService.setUidForeground(UID_RED, true);
272 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700273
Jeff Sharkey3f391352011-06-05 17:42:53 -0700274 replay();
275 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700276
277 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700278 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700279 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
280 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
281 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
282 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700283 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700284
285 // graceful shutdown system, which should trigger persist of stats, and
286 // clear any values in memory.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700287 expectCurrentTime();
288 expectDefaultSettings();
289 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700290 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700291 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700292
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700293 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700294
295 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700296 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700297 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700298 expectNetworkStatsSummary(buildEmptyStats());
299 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700300 expectSystemReady();
301
302 // catch INetworkManagementEventObserver during systemReady()
303 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
304 INetworkManagementEventObserver>();
305 mNetManager.registerObserver(capture(networkObserver));
306 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700307
308 replay();
309 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700310
311 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700312
313 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700314 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700315 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
316 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
317 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
318 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700319 verifyAndReset();
320
321 }
322
Jeff Sharkey63abc372012-01-11 18:38:16 -0800323 // TODO: simulate reboot to test bucket resize
324 @Suppress
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700325 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700326 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700327
328 assertStatsFilesExist(false);
329
330 // pretend that wifi network comes online; service should ask about full
331 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700332 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700333 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
334 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700335 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700336 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700337 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700338
339 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900340 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700341 verifyAndReset();
342
343 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700344 incrementCurrentTime(2 * HOUR_IN_MILLIS);
345 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700346 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700347 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
348 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
349 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700350 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700351
352 replay();
353 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
354
355 // verify service recorded history
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700356 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700357 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700358 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
359 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700360 verifyAndReset();
361
362 // now change bucket duration setting and trigger another poll with
363 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700364 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700365 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700366 expectNetworkStatsSummary(buildEmptyStats());
367 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700368 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700369
370 replay();
371 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
372
373 // verify identical stats, but spread across 4 buckets now
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700374 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700375 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700376 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
377 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700378 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700379
380 }
381
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700382 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700383 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700384 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700385 expectDefaultSettings();
386 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700387 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700388 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700389 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700390
391 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900392 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700393 verifyAndReset();
394
395 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700396 incrementCurrentTime(HOUR_IN_MILLIS);
397 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700398 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700399 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
400 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
401 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
402 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
403 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
404 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700405 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700406
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700407 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700408
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700409 replay();
410 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
411
412 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700413 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
414 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700415 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
416 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700417 verifyAndReset();
418
419 // now switch networks; this also tests that we're okay with interfaces
420 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700421 incrementCurrentTime(HOUR_IN_MILLIS);
422 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700423 expectDefaultSettings();
424 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkey163e6442011-10-31 16:37:52 -0700425 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
426 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
427 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
428 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
429 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
430 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700431 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700432
433 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900434 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700435 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
436 verifyAndReset();
437
438 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700439 incrementCurrentTime(HOUR_IN_MILLIS);
440 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700441 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700442 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700443 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700444 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700445 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
446 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
447 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700448 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700449 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700450
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700451 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700452
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700453 replay();
454 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
455
456 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700457 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700458 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
459 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700460
461 // and verify new history also recorded under different template, which
462 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700463 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
464 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
465 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700466 verifyAndReset();
467
468 }
469
470 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700471 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700472 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700473 expectDefaultSettings();
474 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700475 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700476 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700477 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700478
479 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900480 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700481 verifyAndReset();
482
483 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700484 incrementCurrentTime(HOUR_IN_MILLIS);
485 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700486 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700487 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
488 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
489 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
490 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
491 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
492 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
493 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700494 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700495
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700496 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700497
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700498 replay();
499 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
500
501 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700502 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
503 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700504 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
505 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700506 verifyAndReset();
507
508 // now pretend two UIDs are uninstalled, which should migrate stats to
509 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700510 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700511 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700512 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
513 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
514 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
515 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
516 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
517 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
518 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
519 expectNetworkStatsPoll();
520
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700521 replay();
522 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700523 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700524 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700525 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700526 mServiceContext.sendBroadcast(intent);
527
528 // existing uid and total should remain unchanged; but removed UID
529 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700530 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
531 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
532 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700533 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
534 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700535 verifyAndReset();
536
537 }
538
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700539 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700540 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700541 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700542 expectDefaultSettings();
543 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700544 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700545 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700546 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700547
548 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900549 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700550 verifyAndReset();
551
552 // create some traffic
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, 1024L, 8L, 1024L, 8L, 0L)
559 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700560 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700561
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700562 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
563
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(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700569 verifyAndReset();
570
571 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700572 incrementCurrentTime(HOUR_IN_MILLIS);
573 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700574 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700575 expectNetworkState(buildMobile4gState(TEST_IFACE2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700576 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey163e6442011-10-31 16:37:52 -0700577 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
578 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
579 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700580 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700581
582 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900583 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700584 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
585 verifyAndReset();
586
587 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700588 incrementCurrentTime(HOUR_IN_MILLIS);
589 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700590 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700591 expectNetworkStatsSummary(buildEmptyStats());
592 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700593 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
594 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
595 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
596 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700597 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700598
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700599 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700600
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700601 replay();
602 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
603
604 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700605 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700606
607 verifyAndReset();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700608 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700609
610 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700611 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700612 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700613 expectDefaultSettings();
614 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700615 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700616 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700617 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700618
619 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900620 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700621 verifyAndReset();
622
623 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700624 incrementCurrentTime(HOUR_IN_MILLIS);
625 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700626 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700627 expectNetworkStatsSummary(buildEmptyStats());
628 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
629 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
630 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
631 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700632 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700633
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700634 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700635
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700636 replay();
637 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
638
639 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700640 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
641 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700642 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700643
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700644 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700645 incrementCurrentTime(HOUR_IN_MILLIS);
646 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700647 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700648 expectNetworkStatsSummary(buildEmptyStats());
649 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700650 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
651 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700652 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700653 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700654
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700655 replay();
656 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
657
658 // first verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700659 NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700660 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700661 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700662 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
663 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
664 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700665
666 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700667 final long currentTime = currentTimeMillis();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700668 stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700669 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700670 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700671 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
672
673 verifyAndReset();
674 }
675
676 public void testForegroundBackground() throws Exception {
677 // pretend that network comes online
678 expectCurrentTime();
679 expectDefaultSettings();
680 expectNetworkState(buildWifiState());
681 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700682 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700683 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700684
685 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900686 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700687 verifyAndReset();
688
689 // create some initial traffic
690 incrementCurrentTime(HOUR_IN_MILLIS);
691 expectCurrentTime();
692 expectDefaultSettings();
693 expectNetworkStatsSummary(buildEmptyStats());
694 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
695 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
696 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700697 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700698
699 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
700
701 replay();
702 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
703
704 // verify service recorded history
705 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
706 verifyAndReset();
707
708 // now switch to foreground
709 incrementCurrentTime(HOUR_IN_MILLIS);
710 expectCurrentTime();
711 expectDefaultSettings();
712 expectNetworkStatsSummary(buildEmptyStats());
713 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
714 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
715 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
716 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
717 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700718 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700719
720 mService.setUidForeground(UID_RED, true);
721 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
722
723 replay();
724 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
725
726 // test that we combined correctly
727 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
728
729 // verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700730 final NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700731 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
732 assertEquals(4, stats.size());
733 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
734 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
735 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
736 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700737
738 verifyAndReset();
739 }
740
Jeff Sharkey905b5892011-09-30 15:19:49 -0700741 public void testTethering() throws Exception {
742 // pretend first mobile network comes online
743 expectCurrentTime();
744 expectDefaultSettings();
745 expectNetworkState(buildMobile3gState(IMSI_1));
746 expectNetworkStatsSummary(buildEmptyStats());
747 expectNetworkStatsUidDetail(buildEmptyStats());
748 expectNetworkStatsPoll();
749
750 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900751 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey905b5892011-09-30 15:19:49 -0700752 verifyAndReset();
753
754 // create some tethering traffic
755 incrementCurrentTime(HOUR_IN_MILLIS);
756 expectCurrentTime();
757 expectDefaultSettings();
758 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
759 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
Jeff Sharkey63abc372012-01-11 18:38:16 -0800760
761 final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
762 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
Jeff Sharkey905b5892011-09-30 15:19:49 -0700763 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
Jeff Sharkey63abc372012-01-11 18:38:16 -0800764 final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
765 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
766
767 expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
768 expectNetworkStatsPoll();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700769
770 replay();
771 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
772
773 // verify service recorded history
774 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
775 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
776 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
777 verifyAndReset();
778
779 }
780
Jeff Sharkey70c70532012-05-16 14:51:19 -0700781 public void testReportXtOverDev() throws Exception {
782 // bring mobile network online
783 expectCurrentTime();
784 expectDefaultSettings();
785 expectNetworkState(buildMobile3gState(IMSI_1));
786 expectNetworkStatsSummary(buildEmptyStats());
787 expectNetworkStatsUidDetail(buildEmptyStats());
788 expectNetworkStatsPoll();
789
790 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900791 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey70c70532012-05-16 14:51:19 -0700792 verifyAndReset();
793
794 // create some traffic, but only for DEV, and across 1.5 buckets
795 incrementCurrentTime(90 * MINUTE_IN_MILLIS);
796 expectCurrentTime();
797 expectDefaultSettings();
798 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
799 .addIfaceValues(TEST_IFACE, 6000L, 60L, 3000L, 30L));
800 expectNetworkStatsSummaryXt(buildEmptyStats());
801 expectNetworkStatsUidDetail(buildEmptyStats());
802 expectNetworkStatsPoll();
803
804 replay();
805 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
806
807 // verify service recorded history:
808 // 4000(dev) + 2000(dev)
809 assertNetworkTotal(sTemplateImsi1, 6000L, 60L, 3000L, 30L, 0);
810 verifyAndReset();
811
812 // create traffic on both DEV and XT, across two buckets
813 incrementCurrentTime(2 * HOUR_IN_MILLIS);
814 expectCurrentTime();
815 expectDefaultSettings();
816 expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
817 .addIfaceValues(TEST_IFACE, 6004L, 64L, 3004L, 34L));
818 expectNetworkStatsSummaryXt(new NetworkStats(getElapsedRealtime(), 1)
819 .addIfaceValues(TEST_IFACE, 10240L, 0L, 0L, 0L));
820 expectNetworkStatsUidDetail(buildEmptyStats());
821 expectNetworkStatsPoll();
822
823 replay();
824 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
825
826 // verify that we switching reporting at the first atomic XT bucket,
827 // which should give us:
828 // 4000(dev) + 2000(dev) + 1(dev) + 5120(xt) + 2560(xt)
829 assertNetworkTotal(sTemplateImsi1, 13681L, 61L, 3001L, 31L, 0);
830
831 // also test pure-DEV and pure-XT ranges
832 assertNetworkTotal(sTemplateImsi1, startTimeMillis(),
833 startTimeMillis() + 2 * HOUR_IN_MILLIS, 6001L, 61L, 3001L, 31L, 0);
834 assertNetworkTotal(sTemplateImsi1, startTimeMillis() + 2 * HOUR_IN_MILLIS,
835 startTimeMillis() + 4 * HOUR_IN_MILLIS, 7680L, 0L, 0L, 0L, 0);
836
837 verifyAndReset();
838 }
839
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700840 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700841 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700842 assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
843 txPackets, operations);
844 }
845
846 private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
847 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
848 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700849 final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700850 assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
851
852 // verify summary API
853 final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
854 assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes,
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700855 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700856 }
857
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700858 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700859 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700860 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
861 }
862
863 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700864 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700865 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700866 final NetworkStatsHistory history = mSession.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700867 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700868 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
869 txPackets, operations);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700870
871 // verify summary API
872 final NetworkStats stats = mSession.getSummaryForAllUid(
873 template, Long.MIN_VALUE, Long.MAX_VALUE, false);
874 assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
875 operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700876 }
877
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700878 private void expectSystemReady() throws Exception {
Christopher Tate14a7bb02015-10-01 10:24:31 -0700879 mAlarmManager.remove(isA(PendingIntent.class), null);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700880 expectLastCall().anyTimes();
881
Christopher Tate14a7bb02015-10-01 10:24:31 -0700882 mAlarmManager.set(getContext().getPackageName(),
883 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), anyLong(),
884 anyInt(), isA(PendingIntent.class), null, null, isA(WorkSource.class),
Jose Lima235510e2014-08-13 12:50:01 -0700885 isA(AlarmManager.AlarmClockInfo.class));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700886 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700887
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700888 mNetManager.setGlobalAlert(anyLong());
889 expectLastCall().atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800890
891 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700892 }
893
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700894 private void expectNetworkState(NetworkState... state) throws Exception {
895 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800896
897 final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
898 expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700899 }
900
901 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700902 expectNetworkStatsSummaryDev(summary);
903 expectNetworkStatsSummaryXt(summary);
904 }
905
906 private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700907 expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
Jeff Sharkey70c70532012-05-16 14:51:19 -0700908 }
909
910 private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700911 expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700912 }
913
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700914 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800915 expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
916 }
917
918 private void expectNetworkStatsUidDetail(
919 NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
920 throws Exception {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700921 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800922
923 // also include tethering details, since they are folded into UID
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700924 expect(mNetManager.getNetworkStatsTethering())
Jeff Sharkey63abc372012-01-11 18:38:16 -0800925 .andReturn(tetherStats).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700926 }
927
928 private void expectDefaultSettings() throws Exception {
929 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
930 }
931
Jeff Sharkey63abc372012-01-11 18:38:16 -0800932 private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700933 throws Exception {
934 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700935 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800936 expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
937
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700938 final Config config = new Config(bucketDuration, deleteAge, deleteAge);
Jeff Sharkey63abc372012-01-11 18:38:16 -0800939 expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700940 expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800941 expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
942 expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700943
944 expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
945 expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
946 expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
947 expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
948 expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700949 }
950
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700951 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700952 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
953 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700954 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700955 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
956 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
957 }
958
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700959 private void expectNetworkStatsPoll() throws Exception {
960 mNetManager.setGlobalAlert(anyLong());
961 expectLastCall().anyTimes();
962 }
963
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700964 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800965 final File basePath = new File(mStatsDir, "netstats");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700966 if (exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800967 assertTrue(basePath.list().length > 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700968 } else {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800969 assertTrue(basePath.list().length == 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700970 }
971 }
972
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700973 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
974 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700975 final NetworkStats.Entry entry = new NetworkStats.Entry();
976 if (set == SET_DEFAULT || set == SET_ALL) {
977 final int i = stats.findIndex(iface, uid, SET_DEFAULT, tag);
978 if (i != -1) {
979 entry.add(stats.getValues(i, null));
980 }
981 }
982 if (set == SET_FOREGROUND || set == SET_ALL) {
983 final int i = stats.findIndex(iface, uid, SET_FOREGROUND, tag);
984 if (i != -1) {
985 entry.add(stats.getValues(i, null));
986 }
987 }
988
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700989 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
990 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
991 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
992 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
993 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700994 }
995
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700996 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
997 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700998 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
999 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001000 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001001 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001002 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1003 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001004 }
1005
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001006 private static NetworkState buildWifiState() {
1007 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1008 info.setDetailedState(DetailedState.CONNECTED, null, null);
1009 final LinkProperties prop = new LinkProperties();
1010 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001011 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001012 }
1013
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001014 private static NetworkState buildMobile3gState(String subscriberId) {
1015 final NetworkInfo info = new NetworkInfo(
1016 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1017 info.setDetailedState(DetailedState.CONNECTED, null, null);
1018 final LinkProperties prop = new LinkProperties();
1019 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001020 return new NetworkState(info, prop, null, null, subscriberId, null);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001021 }
1022
Jeff Sharkey163e6442011-10-31 16:37:52 -07001023 private static NetworkState buildMobile4gState(String iface) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001024 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1025 info.setDetailedState(DetailedState.CONNECTED, null, null);
1026 final LinkProperties prop = new LinkProperties();
Jeff Sharkey163e6442011-10-31 16:37:52 -07001027 prop.setInterfaceName(iface);
Jeff Sharkey32566012014-12-02 18:30:14 -08001028 return new NetworkState(info, prop, null, null, null, null);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001029 }
1030
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001031 private NetworkStats buildEmptyStats() {
1032 return new NetworkStats(getElapsedRealtime(), 0);
1033 }
1034
1035 private long getElapsedRealtime() {
1036 return mElapsedRealtime;
1037 }
1038
1039 private long startTimeMillis() {
1040 return TEST_START;
1041 }
1042
1043 private long currentTimeMillis() {
1044 return startTimeMillis() + mElapsedRealtime;
1045 }
1046
1047 private void incrementCurrentTime(long duration) {
1048 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001049 }
1050
Jeff Sharkey3f391352011-06-05 17:42:53 -07001051 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001052 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001053 }
1054
1055 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001056 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1057 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001058 }
1059}