blob: 8cbd32d2bc653ef8f9d56260b13111442081ce0b [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 Sharkeyb09540f2011-06-19 01:08:12 -070021import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070022import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070023import static android.net.ConnectivityManager.TYPE_WIMAX;
24import static android.net.NetworkStats.IFACE_ALL;
Jeff Davidsona6a78072016-01-11 16:02:17 -080025import static android.net.NetworkStats.ROAMING_ALL;
26import static android.net.NetworkStats.ROAMING_DEFAULT;
27import static android.net.NetworkStats.ROAMING_ROAMING;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070028import static android.net.NetworkStats.SET_ALL;
29import static android.net.NetworkStats.SET_DEFAULT;
30import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070031import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070032import static android.net.NetworkStats.UID_ALL;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070033import static android.net.NetworkStatsHistory.FIELD_ALL;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070034import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkeye8914c32012-05-01 16:26:09 -070035import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey241dde22012-02-03 14:50:07 -080036import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070037import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey905b5892011-09-30 15:19:49 -070038import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey3f391352011-06-05 17:42:53 -070039import static android.text.format.DateUtils.DAY_IN_MILLIS;
40import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070041import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
42import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070043import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070044import static org.easymock.EasyMock.anyInt;
Jeff Sharkey3f391352011-06-05 17:42:53 -070045import static org.easymock.EasyMock.anyLong;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070046import static org.easymock.EasyMock.capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070047import static org.easymock.EasyMock.createMock;
48import static org.easymock.EasyMock.eq;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.expectLastCall;
51import static org.easymock.EasyMock.isA;
52
53import android.app.AlarmManager;
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -080054import android.app.IAlarmListener;
Jeff Sharkey3f391352011-06-05 17:42:53 -070055import android.app.IAlarmManager;
56import android.app.PendingIntent;
57import android.content.Intent;
58import android.net.IConnectivityManager;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070059import android.net.INetworkManagementEventObserver;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070060import android.net.INetworkStatsSession;
Jeff Sharkey3f391352011-06-05 17:42:53 -070061import android.net.LinkProperties;
62import android.net.NetworkInfo;
63import android.net.NetworkInfo.DetailedState;
64import android.net.NetworkState;
65import android.net.NetworkStats;
66import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070067import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070068import android.os.INetworkManagementService;
David Christiedf2cab22013-08-02 10:54:55 -070069import android.os.WorkSource;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070070import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070071import android.test.AndroidTestCase;
72import android.test.suitebuilder.annotation.LargeTest;
Jeff Sharkey63abc372012-01-11 18:38:16 -080073import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey3f391352011-06-05 17:42:53 -070074import android.util.TrustedTime;
75
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -080076import com.android.internal.net.VpnInfo;
Jeff Sharkey3f391352011-06-05 17:42:53 -070077import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070078import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey63abc372012-01-11 18:38:16 -080079import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
Jeff Sharkey3f391352011-06-05 17:42:53 -070080
Jeff Sharkeye4984be2013-09-10 21:03:27 -070081import libcore.io.IoUtils;
82
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070083import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070084import org.easymock.EasyMock;
85
86import java.io.File;
Jeff Davidsona6a78072016-01-11 16:02:17 -080087import java.util.ArrayList;
88import java.util.List;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070089
Jeff Sharkey3f391352011-06-05 17:42:53 -070090/**
91 * Tests for {@link NetworkStatsService}.
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -080092 *
93 * TODO: This test is really brittle, largely due to overly-strict use of Easymock.
94 * Rewrite w/ Mockito.
Jeff Sharkey3f391352011-06-05 17:42:53 -070095 */
96@LargeTest
97public class NetworkStatsServiceTest extends AndroidTestCase {
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070098 private static final String TAG = "NetworkStatsServiceTest";
Jeff Sharkey3f391352011-06-05 17:42:53 -070099
100 private static final String TEST_IFACE = "test0";
Jeff Sharkey163e6442011-10-31 16:37:52 -0700101 private static final String TEST_IFACE2 = "test1";
Jeff Sharkey3f391352011-06-05 17:42:53 -0700102 private static final long TEST_START = 1194220800000L;
103
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700104 private static final String IMSI_1 = "310004";
105 private static final String IMSI_2 = "310260";
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700106 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700107
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700108 private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700109 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
110 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700111
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700112 private static final int UID_RED = 1001;
113 private static final int UID_BLUE = 1002;
114 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700115
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700116 private long mElapsedRealtime;
117
Jeff Sharkey3f391352011-06-05 17:42:53 -0700118 private BroadcastInterceptingContext mServiceContext;
119 private File mStatsDir;
120
121 private INetworkManagementService mNetManager;
122 private IAlarmManager mAlarmManager;
123 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700124 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700125 private IConnectivityManager mConnManager;
126
127 private NetworkStatsService mService;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700128 private INetworkStatsSession mSession;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700129 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700130
131 @Override
132 public void setUp() throws Exception {
133 super.setUp();
134
135 mServiceContext = new BroadcastInterceptingContext(getContext());
136 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700137 if (mStatsDir.exists()) {
138 IoUtils.deleteContents(mStatsDir);
139 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700140
141 mNetManager = createMock(INetworkManagementService.class);
142 mAlarmManager = createMock(IAlarmManager.class);
143 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700144 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700145 mConnManager = createMock(IConnectivityManager.class);
146
147 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700148 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700149 mService.bindConnectivityManager(mConnManager);
150
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700151 mElapsedRealtime = 0L;
152
153 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700154 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700155 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700156 expectSystemReady();
157
158 // catch INetworkManagementEventObserver during systemReady()
159 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
160 INetworkManagementEventObserver>();
161 mNetManager.registerObserver(capture(networkObserver));
162 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700163
164 replay();
165 mService.systemReady();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700166 mSession = mService.openSession();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700167 verifyAndReset();
168
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700169 mNetworkObserver = networkObserver.getValue();
170
Jeff Sharkey3f391352011-06-05 17:42:53 -0700171 }
172
173 @Override
174 public void tearDown() throws Exception {
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700175 IoUtils.deleteContents(mStatsDir);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700176
177 mServiceContext = null;
178 mStatsDir = null;
179
180 mNetManager = null;
181 mAlarmManager = null;
182 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700183 mSettings = null;
184 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700185
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700186 mSession.close();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700187 mService = null;
188
189 super.tearDown();
190 }
191
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700192 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700193 // pretend that wifi network comes online; service should ask about full
194 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700195 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700196 expectDefaultSettings();
197 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700198 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700199 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700200 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800201 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700202
203 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800204 mService.forceUpdateIfaces();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700205
206 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700207 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700208 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700209
210 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700211 incrementCurrentTime(HOUR_IN_MILLIS);
212 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700213 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700214 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
215 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
216 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700217 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700218
219 replay();
220 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700221
222 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700223 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700224 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700225
226 // and bump forward again, with counters going higher. this is
227 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700228 incrementCurrentTime(DAY_IN_MILLIS);
229 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700230 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700231 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
232 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
233 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700234 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700235
236 replay();
237 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700238
239 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700240 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700241 verifyAndReset();
242
Jeff Sharkey3f391352011-06-05 17:42:53 -0700243 }
244
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700245 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700246 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700247
248 // pretend that wifi network comes online; service should ask about full
249 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700250 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700251 expectDefaultSettings();
252 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700253 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700254 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700255 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800256 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700257
258 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800259 mService.forceUpdateIfaces();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700260
261 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700262 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700263 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700264
265 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700266 incrementCurrentTime(HOUR_IN_MILLIS);
267 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700268 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700269 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
270 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
271 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
272 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
273 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
274 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
275 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
276 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700277 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700278
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700279 mService.setUidForeground(UID_RED, false);
280 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
281 mService.setUidForeground(UID_RED, true);
282 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700283
Jeff Sharkey3f391352011-06-05 17:42:53 -0700284 replay();
285 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700286
287 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700288 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700289 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800290 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_DEFAULT, 512L, 4L, 256L, 2L, 4);
291 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_DEFAULT, 512L, 4L, 256L, 2L,
292 6);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700293 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700294 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700295
296 // graceful shutdown system, which should trigger persist of stats, and
297 // clear any values in memory.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700298 expectCurrentTime();
299 expectDefaultSettings();
300 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700301 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700302 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700303
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700304 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700305
306 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700307 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700308 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700309 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700310 expectSystemReady();
311
312 // catch INetworkManagementEventObserver during systemReady()
313 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
314 INetworkManagementEventObserver>();
315 mNetManager.registerObserver(capture(networkObserver));
316 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700317
318 replay();
319 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700320
321 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700322
323 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700324 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700325 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800326 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_DEFAULT, 512L, 4L, 256L, 2L, 4);
327 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_DEFAULT, 512L, 4L, 256L, 2L,
328 6);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700329 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700330 verifyAndReset();
331
332 }
333
Jeff Sharkey63abc372012-01-11 18:38:16 -0800334 // TODO: simulate reboot to test bucket resize
335 @Suppress
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700336 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700337 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700338
339 assertStatsFilesExist(false);
340
341 // pretend that wifi network comes online; service should ask about full
342 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700343 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700344 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
345 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700346 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700347 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700348 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800349 expectBandwidthControlCheck();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700350
351 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800352 mService.forceUpdateIfaces();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700353 verifyAndReset();
354
355 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700356 incrementCurrentTime(2 * HOUR_IN_MILLIS);
357 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700358 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700359 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
360 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
361 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700362 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700363
364 replay();
365 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
366
367 // verify service recorded history
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700368 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700369 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700370 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
371 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700372 verifyAndReset();
373
374 // now change bucket duration setting and trigger another poll with
375 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700376 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700377 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700378 expectNetworkStatsSummary(buildEmptyStats());
379 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700380 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700381
382 replay();
383 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
384
385 // verify identical stats, but spread across 4 buckets now
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700386 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700387 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700388 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
389 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700390 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700391
392 }
393
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700394 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700395 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700396 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700397 expectDefaultSettings();
398 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700399 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700400 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700401 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800402 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700403
404 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800405 mService.forceUpdateIfaces();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700406 verifyAndReset();
407
408 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700409 incrementCurrentTime(HOUR_IN_MILLIS);
410 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700411 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700412 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
413 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
414 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
415 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
416 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
417 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700418 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700419
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700420 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700421
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700422 replay();
423 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
424
425 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700426 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
427 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700428 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
429 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700430 verifyAndReset();
431
432 // now switch networks; this also tests that we're okay with interfaces
433 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700434 incrementCurrentTime(HOUR_IN_MILLIS);
435 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700436 expectDefaultSettings();
437 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkey163e6442011-10-31 16:37:52 -0700438 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
439 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
440 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
441 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
442 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
443 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700444 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800445 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700446
447 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800448 mService.forceUpdateIfaces();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700449 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
450 verifyAndReset();
451
452 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700453 incrementCurrentTime(HOUR_IN_MILLIS);
454 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700455 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700456 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700457 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700458 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700459 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
460 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
461 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700462 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700463 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700464
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700465 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700466
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700467 replay();
468 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
469
470 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700471 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700472 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
473 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700474
475 // and verify new history also recorded under different template, which
476 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700477 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
478 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
479 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700480 verifyAndReset();
481
482 }
483
484 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700485 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700486 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700487 expectDefaultSettings();
488 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700489 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700490 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700491 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800492 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700493
494 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800495 mService.forceUpdateIfaces();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700496 verifyAndReset();
497
498 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700499 incrementCurrentTime(HOUR_IN_MILLIS);
500 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700501 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700502 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
503 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
504 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
505 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
506 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
507 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
508 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700509 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700510
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700511 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700512
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700513 replay();
514 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
515
516 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700517 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
518 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700519 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
520 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700521 verifyAndReset();
522
523 // now pretend two UIDs are uninstalled, which should migrate stats to
524 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700525 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700526 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700527 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
528 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
529 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
530 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
531 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
532 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
533 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
534 expectNetworkStatsPoll();
535
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700536 replay();
537 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700538 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700539 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700540 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700541 mServiceContext.sendBroadcast(intent);
542
543 // existing uid and total should remain unchanged; but removed UID
544 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700545 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
546 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
547 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700548 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
549 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700550 verifyAndReset();
551
552 }
553
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700554 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700555 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700556 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700557 expectDefaultSettings();
558 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700559 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700560 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700561 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800562 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700563
564 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800565 mService.forceUpdateIfaces();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700566 verifyAndReset();
567
568 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700569 incrementCurrentTime(HOUR_IN_MILLIS);
570 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700571 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700572 expectNetworkStatsSummary(buildEmptyStats());
573 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
574 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
575 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700576 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700577
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700578 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
579
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700580 replay();
581 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
582
583 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700584 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700585 verifyAndReset();
586
587 // now switch over to 4g 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 Sharkey163e6442011-10-31 16:37:52 -0700591 expectNetworkState(buildMobile4gState(TEST_IFACE2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700592 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey163e6442011-10-31 16:37:52 -0700593 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
594 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
595 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700596 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800597 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700598
599 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800600 mService.forceUpdateIfaces();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700601 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
602 verifyAndReset();
603
604 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700605 incrementCurrentTime(HOUR_IN_MILLIS);
606 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700607 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700608 expectNetworkStatsSummary(buildEmptyStats());
609 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700610 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
611 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
612 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
613 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700614 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700615
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700616 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700617
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700618 replay();
619 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
620
621 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700622 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700623
624 verifyAndReset();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700625 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700626
627 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700628 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700629 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700630 expectDefaultSettings();
631 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700632 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700633 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700634 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800635 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700636
637 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800638 mService.forceUpdateIfaces();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700639 verifyAndReset();
640
641 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700642 incrementCurrentTime(HOUR_IN_MILLIS);
643 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700644 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700645 expectNetworkStatsSummary(buildEmptyStats());
646 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
647 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
648 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
649 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700650 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700651
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700652 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
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 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700658 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
659 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700660 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700661
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700662 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700663 incrementCurrentTime(HOUR_IN_MILLIS);
664 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700665 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700666 expectNetworkStatsSummary(buildEmptyStats());
667 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700668 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
669 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700670 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700671 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700672
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700673 replay();
674 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
675
676 // first verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700677 NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700678 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700679 assertEquals(3, stats.size());
Jeff Davidsona6a78072016-01-11 16:02:17 -0800680 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, 50L, 5L,
681 50L, 5L, 1);
682 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_DEFAULT, 10L, 1L, 10L,
683 1L, 1);
684 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, 2048L, 16L,
685 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700686
687 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700688 final long currentTime = currentTimeMillis();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700689 stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700690 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700691 assertEquals(1, stats.size());
Jeff Davidsona6a78072016-01-11 16:02:17 -0800692 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, 1024L, 8L,
693 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700694
695 verifyAndReset();
696 }
697
698 public void testForegroundBackground() throws Exception {
699 // pretend that network comes online
700 expectCurrentTime();
701 expectDefaultSettings();
702 expectNetworkState(buildWifiState());
703 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700704 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700705 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800706 expectBandwidthControlCheck();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700707
708 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800709 mService.forceUpdateIfaces();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700710 verifyAndReset();
711
712 // create some initial traffic
713 incrementCurrentTime(HOUR_IN_MILLIS);
714 expectCurrentTime();
715 expectDefaultSettings();
716 expectNetworkStatsSummary(buildEmptyStats());
717 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
718 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
719 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700720 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700721
722 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
723
724 replay();
725 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
726
727 // verify service recorded history
728 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
729 verifyAndReset();
730
731 // now switch to foreground
732 incrementCurrentTime(HOUR_IN_MILLIS);
733 expectCurrentTime();
734 expectDefaultSettings();
735 expectNetworkStatsSummary(buildEmptyStats());
736 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
737 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
738 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
739 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
740 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700741 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700742
743 mService.setUidForeground(UID_RED, true);
744 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
745
746 replay();
747 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
748
749 // test that we combined correctly
750 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
751
752 // verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700753 final NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700754 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
755 assertEquals(4, stats.size());
Jeff Davidsona6a78072016-01-11 16:02:17 -0800756 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, 128L, 2L,
757 128L, 2L, 1);
758 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_DEFAULT, 64L, 1L, 64L,
759 1L, 1);
760 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, ROAMING_DEFAULT, 32L, 2L,
761 32L, 2L, 1);
762 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, ROAMING_DEFAULT, 1L, 1L, 1L,
763 1L, 1);
764
765 verifyAndReset();
766 }
767
768 public void testRoaming() throws Exception {
769 // pretend that network comes online
770 expectCurrentTime();
771 expectDefaultSettings();
772 expectNetworkState(buildMobile3gState(IMSI_1, true /* isRoaming */));
773 expectNetworkStatsSummary(buildEmptyStats());
774 expectNetworkStatsUidDetail(buildEmptyStats());
775 expectNetworkStatsPoll();
776 expectBandwidthControlCheck();
777
778 replay();
779 mService.forceUpdateIfaces();
780 verifyAndReset();
781
782 // Create some traffic
783 incrementCurrentTime(HOUR_IN_MILLIS);
784 expectCurrentTime();
785 expectDefaultSettings();
786 expectNetworkStatsSummary(buildEmptyStats());
787 // Note that all traffic from NetworkManagementService is tagged as ROAMING_DEFAULT, because
788 // roaming isn't tracked at that layer. We layer it on top by inspecting the iface
789 // properties.
790 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
791 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, 128L, 2L,
792 128L, 2L, 0L)
793 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_DEFAULT, 64L, 1L, 64L,
794 1L, 0L));
795 expectNetworkStatsPoll();
796
797 replay();
798 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
799
800 // verify service recorded history
801 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
802
803 // verify entire history present
804 final NetworkStats stats = mSession.getSummaryForAllUid(
805 sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true);
806 assertEquals(2, stats.size());
807 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_ROAMING, 128L, 2L,
808 128L, 2L, 0);
809 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_ROAMING, 64L, 1L, 64L,
810 1L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700811
812 verifyAndReset();
813 }
814
Jeff Sharkey905b5892011-09-30 15:19:49 -0700815 public void testTethering() throws Exception {
816 // pretend first mobile network comes online
817 expectCurrentTime();
818 expectDefaultSettings();
819 expectNetworkState(buildMobile3gState(IMSI_1));
820 expectNetworkStatsSummary(buildEmptyStats());
821 expectNetworkStatsUidDetail(buildEmptyStats());
822 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800823 expectBandwidthControlCheck();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700824
825 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800826 mService.forceUpdateIfaces();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700827 verifyAndReset();
828
829 // create some tethering traffic
830 incrementCurrentTime(HOUR_IN_MILLIS);
831 expectCurrentTime();
832 expectDefaultSettings();
833 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
834 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
Jeff Sharkey63abc372012-01-11 18:38:16 -0800835
836 final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
837 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
Jeff Sharkey905b5892011-09-30 15:19:49 -0700838 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
Jeff Sharkey63abc372012-01-11 18:38:16 -0800839 final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
840 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
841
842 expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
843 expectNetworkStatsPoll();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700844
845 replay();
846 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
847
848 // verify service recorded history
849 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
850 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
851 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
852 verifyAndReset();
853
854 }
855
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700856 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700857 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700858 assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
859 txPackets, operations);
860 }
861
862 private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
863 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
864 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700865 final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700866 assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
867
868 // verify summary API
869 final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800870 assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, ROAMING_DEFAULT, rxBytes,
871 rxPackets, txBytes, txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700872 }
873
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700874 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700875 long txBytes, long txPackets, int operations) throws Exception {
Jeff Davidsona6a78072016-01-11 16:02:17 -0800876 assertUidTotal(template, uid, SET_ALL, ROAMING_ALL, rxBytes, rxPackets, txBytes, txPackets,
877 operations);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700878 }
879
Jeff Davidsona6a78072016-01-11 16:02:17 -0800880 private void assertUidTotal(NetworkTemplate template, int uid, int set, int roaming,
881 long rxBytes, long rxPackets, long txBytes, long txPackets, int operations)
882 throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700883 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700884 final NetworkStatsHistory history = mSession.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700885 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700886 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
887 txPackets, operations);
Jeff Sharkey70c70532012-05-16 14:51:19 -0700888
889 // verify summary API
890 final NetworkStats stats = mSession.getSummaryForAllUid(
891 template, Long.MIN_VALUE, Long.MAX_VALUE, false);
Jeff Davidsona6a78072016-01-11 16:02:17 -0800892 assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, roaming, rxBytes, rxPackets, txBytes,
893 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700894 }
895
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700896 private void expectSystemReady() throws Exception {
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800897 mAlarmManager.remove(isA(PendingIntent.class), EasyMock.<IAlarmListener>isNull());
Jeff Sharkey3f391352011-06-05 17:42:53 -0700898 expectLastCall().anyTimes();
899
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800900 mAlarmManager.set(eq(getContext().getPackageName()),
Christopher Tate14a7bb02015-10-01 10:24:31 -0700901 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), anyLong(),
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800902 anyInt(), isA(PendingIntent.class), EasyMock.<IAlarmListener>isNull(),
903 EasyMock.<String>isNull(), EasyMock.<WorkSource>isNull(),
904 EasyMock.<AlarmManager.AlarmClockInfo>isNull());
905 expectLastCall().anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700906
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700907 mNetManager.setGlobalAlert(anyLong());
908 expectLastCall().atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800909
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800910 expectNetworkStatsSummary(buildEmptyStats());
911 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700912 }
913
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700914 private void expectNetworkState(NetworkState... state) throws Exception {
915 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800916
917 final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
918 expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700919 }
920
921 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800922 expect(mConnManager.getAllVpnInfo()).andReturn(new VpnInfo[0]).atLeastOnce();
923
Jeff Sharkey70c70532012-05-16 14:51:19 -0700924 expectNetworkStatsSummaryDev(summary);
925 expectNetworkStatsSummaryXt(summary);
926 }
927
928 private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700929 expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
Jeff Sharkey70c70532012-05-16 14:51:19 -0700930 }
931
932 private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700933 expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700934 }
935
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700936 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800937 expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
938 }
939
940 private void expectNetworkStatsUidDetail(
941 NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
942 throws Exception {
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700943 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800944
945 // also include tethering details, since they are folded into UID
Jeff Sharkeye4984be2013-09-10 21:03:27 -0700946 expect(mNetManager.getNetworkStatsTethering())
Jeff Sharkey63abc372012-01-11 18:38:16 -0800947 .andReturn(tetherStats).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700948 }
949
950 private void expectDefaultSettings() throws Exception {
951 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
952 }
953
Jeff Sharkey63abc372012-01-11 18:38:16 -0800954 private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700955 throws Exception {
956 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700957 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800958 expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
959
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700960 final Config config = new Config(bucketDuration, deleteAge, deleteAge);
Jeff Sharkey63abc372012-01-11 18:38:16 -0800961 expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700962 expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -0800963 expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
964 expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700965
966 expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
967 expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
968 expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
969 expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
970 expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700971 }
972
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700973 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700974 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
975 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700976 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700977 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
978 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
979 }
980
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700981 private void expectNetworkStatsPoll() throws Exception {
982 mNetManager.setGlobalAlert(anyLong());
983 expectLastCall().anyTimes();
984 }
985
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800986 private void expectBandwidthControlCheck() throws Exception {
987 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
988 }
989
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700990 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800991 final File basePath = new File(mStatsDir, "netstats");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700992 if (exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800993 assertTrue(basePath.list().length > 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700994 } else {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800995 assertTrue(basePath.list().length == 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700996 }
997 }
998
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700999 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
Jeff Davidsona6a78072016-01-11 16:02:17 -08001000 int tag, int roaming, long rxBytes, long rxPackets, long txBytes, long txPackets,
1001 int operations) {
Jeff Sharkey70c70532012-05-16 14:51:19 -07001002 final NetworkStats.Entry entry = new NetworkStats.Entry();
Jeff Davidsona6a78072016-01-11 16:02:17 -08001003 List<Integer> sets = new ArrayList<>();
Jeff Sharkey70c70532012-05-16 14:51:19 -07001004 if (set == SET_DEFAULT || set == SET_ALL) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001005 sets.add(SET_DEFAULT);
Jeff Sharkey70c70532012-05-16 14:51:19 -07001006 }
1007 if (set == SET_FOREGROUND || set == SET_ALL) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001008 sets.add(SET_FOREGROUND);
1009 }
1010
1011 List<Integer> roamings = new ArrayList<>();
1012 if (roaming == ROAMING_DEFAULT || roaming == ROAMING_ALL) {
1013 roamings.add(ROAMING_DEFAULT);
1014 }
1015 if (roaming == ROAMING_ROAMING || roaming == ROAMING_ALL) {
1016 roamings.add(ROAMING_ROAMING);
1017 }
1018
1019 for (int s : sets) {
1020 for (int r : roamings) {
1021 final int i = stats.findIndex(iface, uid, s, tag, r);
1022 if (i != -1) {
1023 entry.add(stats.getValues(i, null));
1024 }
Jeff Sharkey70c70532012-05-16 14:51:19 -07001025 }
1026 }
1027
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001028 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
1029 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
1030 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
1031 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1032 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001033 }
1034
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001035 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
1036 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -07001037 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
1038 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001039 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001040 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001041 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1042 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001043 }
1044
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001045 private static NetworkState buildWifiState() {
1046 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1047 info.setDetailedState(DetailedState.CONNECTED, null, null);
1048 final LinkProperties prop = new LinkProperties();
1049 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001050 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001051 }
1052
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001053 private static NetworkState buildMobile3gState(String subscriberId) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001054 return buildMobile3gState(subscriberId, false /* isRoaming */);
1055 }
1056
1057 private static NetworkState buildMobile3gState(String subscriberId, boolean isRoaming) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001058 final NetworkInfo info = new NetworkInfo(
1059 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1060 info.setDetailedState(DetailedState.CONNECTED, null, null);
Jeff Davidsona6a78072016-01-11 16:02:17 -08001061 info.setRoaming(isRoaming);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001062 final LinkProperties prop = new LinkProperties();
1063 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001064 return new NetworkState(info, prop, null, null, subscriberId, null);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001065 }
1066
Jeff Sharkey163e6442011-10-31 16:37:52 -07001067 private static NetworkState buildMobile4gState(String iface) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001068 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1069 info.setDetailedState(DetailedState.CONNECTED, null, null);
1070 final LinkProperties prop = new LinkProperties();
Jeff Sharkey163e6442011-10-31 16:37:52 -07001071 prop.setInterfaceName(iface);
Jeff Sharkey32566012014-12-02 18:30:14 -08001072 return new NetworkState(info, prop, null, null, null, null);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001073 }
1074
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001075 private NetworkStats buildEmptyStats() {
1076 return new NetworkStats(getElapsedRealtime(), 0);
1077 }
1078
1079 private long getElapsedRealtime() {
1080 return mElapsedRealtime;
1081 }
1082
1083 private long startTimeMillis() {
1084 return TEST_START;
1085 }
1086
1087 private long currentTimeMillis() {
1088 return startTimeMillis() + mElapsedRealtime;
1089 }
1090
1091 private void incrementCurrentTime(long duration) {
1092 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001093 }
1094
Jeff Sharkey3f391352011-06-05 17:42:53 -07001095 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001096 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001097 }
1098
1099 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001100 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1101 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001102 }
1103}