blob: 94c6711da9ae09480a86151b8dc224d50dc895e6 [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
Antonio Cansadocd42acd2016-02-17 13:03:38 -080017package com.android.server.net;
Jeff Sharkey3f391352011-06-05 17:42:53 -070018
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;
Jeff Davidson1f7e05e2016-03-10 13:21:38 -080026import static android.net.NetworkStats.ROAMING_NO;
27import static android.net.NetworkStats.ROAMING_YES;
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;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080046import static org.easymock.EasyMock.anyObject;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070047import static org.easymock.EasyMock.capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070048import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.eq;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.expectLastCall;
52import static org.easymock.EasyMock.isA;
53
54import android.app.AlarmManager;
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -080055import android.app.IAlarmListener;
Jeff Sharkey3f391352011-06-05 17:42:53 -070056import android.app.IAlarmManager;
57import android.app.PendingIntent;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080058import android.app.usage.NetworkStatsManager;
59import android.content.Context;
Jeff Sharkey3f391352011-06-05 17:42:53 -070060import android.content.Intent;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080061import android.net.DataUsageRequest;
Jeff Sharkey3f391352011-06-05 17:42:53 -070062import android.net.IConnectivityManager;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070063import android.net.INetworkManagementEventObserver;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -070064import android.net.INetworkStatsSession;
Jeff Sharkey3f391352011-06-05 17:42:53 -070065import android.net.LinkProperties;
66import android.net.NetworkInfo;
67import android.net.NetworkInfo.DetailedState;
68import android.net.NetworkState;
69import android.net.NetworkStats;
70import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070071import android.net.NetworkTemplate;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080072import android.os.ConditionVariable;
73import android.os.Handler;
74import android.os.HandlerThread;
Jeff Sharkey3f391352011-06-05 17:42:53 -070075import android.os.INetworkManagementService;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080076import android.os.IBinder;
77import android.os.Looper;
78import android.os.Messenger;
79import android.os.MessageQueue;
80import android.os.MessageQueue.IdleHandler;
81import android.os.Message;
82import android.os.PowerManager;
David Christiedf2cab22013-08-02 10:54:55 -070083import android.os.WorkSource;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070084import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070085import android.test.AndroidTestCase;
86import android.test.suitebuilder.annotation.LargeTest;
Jeff Sharkey63abc372012-01-11 18:38:16 -080087import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey3f391352011-06-05 17:42:53 -070088import android.util.TrustedTime;
89
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -080090import com.android.internal.net.VpnInfo;
Antonio Cansadocd42acd2016-02-17 13:03:38 -080091import com.android.server.BroadcastInterceptingContext;
Jeff Sharkey3f391352011-06-05 17:42:53 -070092import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070093import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey63abc372012-01-11 18:38:16 -080094import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
Jeff Sharkey3f391352011-06-05 17:42:53 -070095
Jeff Sharkeye4984be2013-09-10 21:03:27 -070096import libcore.io.IoUtils;
97
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070098import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070099import org.easymock.EasyMock;
100
101import java.io.File;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800102import java.util.ArrayList;
Antonio Cansado6965c182016-03-30 11:37:18 -0700103import java.util.Objects;
Jeff Davidsona6a78072016-01-11 16:02:17 -0800104import java.util.List;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700105
Jeff Sharkey3f391352011-06-05 17:42:53 -0700106/**
107 * Tests for {@link NetworkStatsService}.
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800108 *
109 * TODO: This test is really brittle, largely due to overly-strict use of Easymock.
110 * Rewrite w/ Mockito.
Jeff Sharkey3f391352011-06-05 17:42:53 -0700111 */
112@LargeTest
113public class NetworkStatsServiceTest extends AndroidTestCase {
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700114 private static final String TAG = "NetworkStatsServiceTest";
Jeff Sharkey3f391352011-06-05 17:42:53 -0700115
116 private static final String TEST_IFACE = "test0";
Jeff Sharkey163e6442011-10-31 16:37:52 -0700117 private static final String TEST_IFACE2 = "test1";
Jeff Sharkey3f391352011-06-05 17:42:53 -0700118 private static final long TEST_START = 1194220800000L;
119
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700120 private static final String IMSI_1 = "310004";
121 private static final String IMSI_2 = "310260";
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700122 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700123
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700124 private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700125 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
126 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700127
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700128 private static final int UID_RED = 1001;
129 private static final int UID_BLUE = 1002;
130 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700131
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800132 private static final long WAIT_TIMEOUT = 2 * 1000; // 2 secs
133 private static final int INVALID_TYPE = -1;
134
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700135 private long mElapsedRealtime;
136
Jeff Sharkey3f391352011-06-05 17:42:53 -0700137 private BroadcastInterceptingContext mServiceContext;
138 private File mStatsDir;
139
140 private INetworkManagementService mNetManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700141 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700142 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700143 private IConnectivityManager mConnManager;
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800144 private IdleableHandlerThread mHandlerThread;
145 private Handler mHandler;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700146
147 private NetworkStatsService mService;
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700148 private INetworkStatsSession mSession;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700149 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700150
151 @Override
152 public void setUp() throws Exception {
153 super.setUp();
154
155 mServiceContext = new BroadcastInterceptingContext(getContext());
156 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700157 if (mStatsDir.exists()) {
158 IoUtils.deleteContents(mStatsDir);
159 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700160
161 mNetManager = createMock(INetworkManagementService.class);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800162
163 // TODO: Mock AlarmManager when migrating this test to Mockito.
164 AlarmManager alarmManager = (AlarmManager) mServiceContext
165 .getSystemService(Context.ALARM_SERVICE);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700166 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700167 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700168 mConnManager = createMock(IConnectivityManager.class);
169
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800170 PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(
171 Context.POWER_SERVICE);
172 PowerManager.WakeLock wakeLock =
173 powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
174
Jeff Sharkey3f391352011-06-05 17:42:53 -0700175 mService = new NetworkStatsService(
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800176 mServiceContext, mNetManager, alarmManager, wakeLock, mTime,
177 TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(),
178 mStatsDir, getBaseDir(mStatsDir));
179 mHandlerThread = new IdleableHandlerThread("HandlerThread");
180 mHandlerThread.start();
181 Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
182 mHandler = new Handler(mHandlerThread.getLooper(), callback);
183 mService.setHandler(mHandler, callback);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700184 mService.bindConnectivityManager(mConnManager);
185
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700186 mElapsedRealtime = 0L;
187
188 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700189 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700190 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700191 expectSystemReady();
192
193 // catch INetworkManagementEventObserver during systemReady()
194 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
195 INetworkManagementEventObserver>();
196 mNetManager.registerObserver(capture(networkObserver));
197 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700198
199 replay();
200 mService.systemReady();
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700201 mSession = mService.openSession();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700202 verifyAndReset();
203
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700204 mNetworkObserver = networkObserver.getValue();
205
Jeff Sharkey3f391352011-06-05 17:42:53 -0700206 }
207
208 @Override
209 public void tearDown() throws Exception {
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -0700210 IoUtils.deleteContents(mStatsDir);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700211
212 mServiceContext = null;
213 mStatsDir = null;
214
215 mNetManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700216 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700217 mSettings = null;
218 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700219
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700220 mSession.close();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700221 mService = null;
222
223 super.tearDown();
224 }
225
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700226 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700227 // pretend that wifi network comes online; service should ask about full
228 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700229 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700230 expectDefaultSettings();
231 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700232 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700233 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700234 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800235 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700236
237 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800238 mService.forceUpdateIfaces();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700239
240 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700241 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700242 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700243
244 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700245 incrementCurrentTime(HOUR_IN_MILLIS);
246 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700247 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700248 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
249 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
250 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700251 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700252
253 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800254 forcePollAndWaitForIdle();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700255
256 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700257 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700258 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700259
260 // and bump forward again, with counters going higher. this is
261 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700262 incrementCurrentTime(DAY_IN_MILLIS);
263 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700264 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700265 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
266 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
267 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700268 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700269
270 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800271 forcePollAndWaitForIdle();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700272
273 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700274 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700275 verifyAndReset();
276
Jeff Sharkey3f391352011-06-05 17:42:53 -0700277 }
278
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700279 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700280 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700281
282 // pretend that wifi network comes online; service should ask about full
283 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700284 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700285 expectDefaultSettings();
286 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700287 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700288 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700289 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800290 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700291
292 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800293 mService.forceUpdateIfaces();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700294
295 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700296 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700297 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700298
299 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700300 incrementCurrentTime(HOUR_IN_MILLIS);
301 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700302 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700303 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
304 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
305 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
306 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
307 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
308 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
309 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
310 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700311 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700312
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700313 mService.setUidForeground(UID_RED, false);
314 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
315 mService.setUidForeground(UID_RED, true);
316 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700317
Jeff Sharkey3f391352011-06-05 17:42:53 -0700318 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800319 forcePollAndWaitForIdle();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700320
321 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700322 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700323 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800324 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
325 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800326 6);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700327 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700328 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700329
330 // graceful shutdown system, which should trigger persist of stats, and
331 // clear any values in memory.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700332 expectCurrentTime();
333 expectDefaultSettings();
334 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700335 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700336 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700337
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700338 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700339
340 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700341 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700342 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700343 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700344 expectSystemReady();
345
346 // catch INetworkManagementEventObserver during systemReady()
347 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
348 INetworkManagementEventObserver>();
349 mNetManager.registerObserver(capture(networkObserver));
350 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700351
352 replay();
353 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700354
355 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700356
357 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700358 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700359 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800360 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
361 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800362 6);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700363 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700364 verifyAndReset();
365
366 }
367
Jeff Sharkey63abc372012-01-11 18:38:16 -0800368 // TODO: simulate reboot to test bucket resize
369 @Suppress
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700370 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700371 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700372
373 assertStatsFilesExist(false);
374
375 // pretend that wifi network comes online; service should ask about full
376 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700377 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700378 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
379 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700380 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700381 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700382 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800383 expectBandwidthControlCheck();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700384
385 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800386 mService.forceUpdateIfaces();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700387 verifyAndReset();
388
389 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700390 incrementCurrentTime(2 * HOUR_IN_MILLIS);
391 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700392 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700393 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
394 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
395 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700396 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700397
398 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800399 forcePollAndWaitForIdle();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700400
401 // verify service recorded history
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700402 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700403 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700404 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
405 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700406 verifyAndReset();
407
408 // now change bucket duration setting and trigger another poll with
409 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700410 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700411 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700412 expectNetworkStatsSummary(buildEmptyStats());
413 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700414 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700415
416 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800417 forcePollAndWaitForIdle();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700418
419 // verify identical stats, but spread across 4 buckets now
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700420 history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700421 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700422 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
423 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700424 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700425
426 }
427
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700428 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700429 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700430 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700431 expectDefaultSettings();
432 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700433 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700434 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700435 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800436 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700437
438 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800439 mService.forceUpdateIfaces();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700440 verifyAndReset();
441
442 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700443 incrementCurrentTime(HOUR_IN_MILLIS);
444 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700445 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700446 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
447 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
448 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
449 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
450 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
451 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700452 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700453
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700454 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700455
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700456 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800457 forcePollAndWaitForIdle();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700458
459 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700460 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
461 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700462 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
463 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700464 verifyAndReset();
465
466 // now switch networks; this also tests that we're okay with interfaces
467 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700468 incrementCurrentTime(HOUR_IN_MILLIS);
469 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700470 expectDefaultSettings();
471 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkey163e6442011-10-31 16:37:52 -0700472 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
473 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
474 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
475 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
476 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
477 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700478 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800479 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700480
481 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800482 mService.forceUpdateIfaces();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800483 forcePollAndWaitForIdle();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700484 verifyAndReset();
485
486 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700487 incrementCurrentTime(HOUR_IN_MILLIS);
488 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700489 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700490 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700491 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700492 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700493 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
494 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
495 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700496 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700497 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700498
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700499 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700500
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700501 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800502 forcePollAndWaitForIdle();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700503
504 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700505 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700506 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
507 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700508
509 // and verify new history also recorded under different template, which
510 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700511 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
512 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
513 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700514 verifyAndReset();
515
516 }
517
518 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700519 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700520 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700521 expectDefaultSettings();
522 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700523 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700524 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700525 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800526 expectBandwidthControlCheck();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700527
528 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800529 mService.forceUpdateIfaces();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700530 verifyAndReset();
531
532 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700533 incrementCurrentTime(HOUR_IN_MILLIS);
534 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700535 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700536 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
537 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
538 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
539 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
540 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
541 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
542 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700543 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700544
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700545 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700546
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700547 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800548 forcePollAndWaitForIdle();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700549
550 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700551 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
552 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700553 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
554 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700555 verifyAndReset();
556
557 // now pretend two UIDs are uninstalled, which should migrate stats to
558 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700559 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700560 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700561 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
562 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
563 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
564 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
565 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
566 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
567 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
568 expectNetworkStatsPoll();
569
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700570 replay();
571 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700572 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700573 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700574 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700575 mServiceContext.sendBroadcast(intent);
576
577 // existing uid and total should remain unchanged; but removed UID
578 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700579 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
580 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
581 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700582 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
583 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700584 verifyAndReset();
585
586 }
587
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700588 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700589 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700590 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700591 expectDefaultSettings();
592 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700593 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700594 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700595 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800596 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700597
598 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800599 mService.forceUpdateIfaces();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700600 verifyAndReset();
601
602 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700603 incrementCurrentTime(HOUR_IN_MILLIS);
604 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700605 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700606 expectNetworkStatsSummary(buildEmptyStats());
607 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
608 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
609 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700610 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700611
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700612 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
613
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700614 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800615 forcePollAndWaitForIdle();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700616
617 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700618 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700619 verifyAndReset();
620
621 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700622 incrementCurrentTime(HOUR_IN_MILLIS);
623 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700624 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700625 expectNetworkState(buildMobile4gState(TEST_IFACE2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700626 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey163e6442011-10-31 16:37:52 -0700627 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
628 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
629 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700630 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800631 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700632
633 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800634 mService.forceUpdateIfaces();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800635 forcePollAndWaitForIdle();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700636 verifyAndReset();
637
638 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700639 incrementCurrentTime(HOUR_IN_MILLIS);
640 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700641 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700642 expectNetworkStatsSummary(buildEmptyStats());
643 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700644 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
645 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
646 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
647 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700648 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700649
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700650 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700651
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700652 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800653 forcePollAndWaitForIdle();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700654
655 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700656 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700657
658 verifyAndReset();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700659 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700660
661 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700662 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700663 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700664 expectDefaultSettings();
665 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700666 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700667 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700668 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800669 expectBandwidthControlCheck();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700670
671 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800672 mService.forceUpdateIfaces();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700673 verifyAndReset();
674
675 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700676 incrementCurrentTime(HOUR_IN_MILLIS);
677 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700678 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700679 expectNetworkStatsSummary(buildEmptyStats());
680 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
681 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
682 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
683 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700684 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700685
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700686 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700687
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700688 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800689 forcePollAndWaitForIdle();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700690
691 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700692 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
693 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700694 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700695
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700696 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700697 incrementCurrentTime(HOUR_IN_MILLIS);
698 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700699 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700700 expectNetworkStatsSummary(buildEmptyStats());
701 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700702 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
703 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700704 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700705 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700706
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700707 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800708 forcePollAndWaitForIdle();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700709
710 // first verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700711 NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700712 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700713 assertEquals(3, stats.size());
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800714 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 50L, 5L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800715 50L, 5L, 1);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800716 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 10L, 1L, 10L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800717 1L, 1);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800718 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_NO, 2048L, 16L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800719 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700720
721 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700722 final long currentTime = currentTimeMillis();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700723 stats = mSession.getSummaryForAllUid(
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700724 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700725 assertEquals(1, stats.size());
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800726 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_NO, 1024L, 8L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800727 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700728
729 verifyAndReset();
730 }
731
732 public void testForegroundBackground() throws Exception {
733 // pretend that network comes online
734 expectCurrentTime();
735 expectDefaultSettings();
736 expectNetworkState(buildWifiState());
737 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700738 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700739 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800740 expectBandwidthControlCheck();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700741
742 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800743 mService.forceUpdateIfaces();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700744 verifyAndReset();
745
746 // create some initial traffic
747 incrementCurrentTime(HOUR_IN_MILLIS);
748 expectCurrentTime();
749 expectDefaultSettings();
750 expectNetworkStatsSummary(buildEmptyStats());
751 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
752 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
753 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700754 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700755
756 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
757
758 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800759 forcePollAndWaitForIdle();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700760
761 // verify service recorded history
762 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
763 verifyAndReset();
764
765 // now switch to foreground
766 incrementCurrentTime(HOUR_IN_MILLIS);
767 expectCurrentTime();
768 expectDefaultSettings();
769 expectNetworkStatsSummary(buildEmptyStats());
770 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
771 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
772 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
773 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
774 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700775 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700776
777 mService.setUidForeground(UID_RED, true);
778 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
779
780 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800781 forcePollAndWaitForIdle();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700782
783 // test that we combined correctly
784 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
785
786 // verify entire history present
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700787 final NetworkStats stats = mSession.getSummaryForAllUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700788 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
789 assertEquals(4, stats.size());
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800790 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 128L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800791 128L, 2L, 1);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800792 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 64L, 1L, 64L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800793 1L, 1);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800794 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, ROAMING_NO, 32L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800795 32L, 2L, 1);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800796 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, ROAMING_NO, 1L, 1L, 1L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800797 1L, 1);
798
799 verifyAndReset();
800 }
801
802 public void testRoaming() throws Exception {
803 // pretend that network comes online
804 expectCurrentTime();
805 expectDefaultSettings();
806 expectNetworkState(buildMobile3gState(IMSI_1, true /* isRoaming */));
807 expectNetworkStatsSummary(buildEmptyStats());
808 expectNetworkStatsUidDetail(buildEmptyStats());
809 expectNetworkStatsPoll();
810 expectBandwidthControlCheck();
811
812 replay();
813 mService.forceUpdateIfaces();
814 verifyAndReset();
815
816 // Create some traffic
817 incrementCurrentTime(HOUR_IN_MILLIS);
818 expectCurrentTime();
819 expectDefaultSettings();
820 expectNetworkStatsSummary(buildEmptyStats());
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800821 // Note that all traffic from NetworkManagementService is tagged as ROAMING_NO, because
Jeff Davidsona6a78072016-01-11 16:02:17 -0800822 // roaming isn't tracked at that layer. We layer it on top by inspecting the iface
823 // properties.
824 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800825 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 128L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800826 128L, 2L, 0L)
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800827 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 64L, 1L, 64L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800828 1L, 0L));
829 expectNetworkStatsPoll();
830
831 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800832 forcePollAndWaitForIdle();
Jeff Davidsona6a78072016-01-11 16:02:17 -0800833
834 // verify service recorded history
835 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
836
837 // verify entire history present
838 final NetworkStats stats = mSession.getSummaryForAllUid(
839 sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true);
840 assertEquals(2, stats.size());
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800841 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_YES, 128L, 2L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800842 128L, 2L, 0);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -0800843 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_YES, 64L, 1L, 64L,
Jeff Davidsona6a78072016-01-11 16:02:17 -0800844 1L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700845
846 verifyAndReset();
847 }
848
Jeff Sharkey905b5892011-09-30 15:19:49 -0700849 public void testTethering() throws Exception {
850 // pretend first mobile network comes online
851 expectCurrentTime();
852 expectDefaultSettings();
853 expectNetworkState(buildMobile3gState(IMSI_1));
854 expectNetworkStatsSummary(buildEmptyStats());
855 expectNetworkStatsUidDetail(buildEmptyStats());
856 expectNetworkStatsPoll();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800857 expectBandwidthControlCheck();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700858
859 replay();
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -0800860 mService.forceUpdateIfaces();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700861 verifyAndReset();
862
863 // create some tethering traffic
864 incrementCurrentTime(HOUR_IN_MILLIS);
865 expectCurrentTime();
866 expectDefaultSettings();
867 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
868 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
Jeff Sharkey63abc372012-01-11 18:38:16 -0800869
870 final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
871 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
Jeff Sharkey905b5892011-09-30 15:19:49 -0700872 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
Jeff Sharkey63abc372012-01-11 18:38:16 -0800873 final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
874 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
875
876 expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
877 expectNetworkStatsPoll();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700878
879 replay();
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800880 forcePollAndWaitForIdle();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700881
882 // verify service recorded history
883 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
884 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
885 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
886 verifyAndReset();
887
888 }
889
Antonio Cansado6965c182016-03-30 11:37:18 -0700890 public void testRegisterUsageCallback() throws Exception {
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800891 // pretend that wifi network comes online; service should ask about full
892 // network state, and poll any existing interfaces before updating.
893 expectCurrentTime();
894 expectDefaultSettings();
895 expectNetworkState(buildWifiState());
896 expectNetworkStatsSummary(buildEmptyStats());
897 expectNetworkStatsUidDetail(buildEmptyStats());
898 expectNetworkStatsPoll();
899 expectBandwidthControlCheck();
900
901 replay();
902 mService.forceUpdateIfaces();
903
904 // verify service has empty history for wifi
905 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
906 verifyAndReset();
907
908 String callingPackage = "the.calling.package";
909 long thresholdInBytes = 1L; // very small; should be overriden by framework
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800910 DataUsageRequest inputRequest = new DataUsageRequest(
Antonio Cansado6965c182016-03-30 11:37:18 -0700911 DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800912
913 // Create a messenger that waits for callback activity
914 ConditionVariable cv = new ConditionVariable(false);
915 LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
916 Messenger messenger = new Messenger(latchedHandler);
917
918 // Allow binder to connect
919 IBinder mockBinder = createMock(IBinder.class);
920 mockBinder.linkToDeath((IBinder.DeathRecipient) anyObject(), anyInt());
921 EasyMock.replay(mockBinder);
922
923 // Force poll
924 expectCurrentTime();
925 expectDefaultSettings();
926 expectNetworkStatsSummary(buildEmptyStats());
927 expectNetworkStatsUidDetail(buildEmptyStats());
928 expectNetworkStatsPoll();
929 replay();
930
931 // Register and verify request and that binder was called
932 DataUsageRequest request =
Antonio Cansado6965c182016-03-30 11:37:18 -0700933 mService.registerUsageCallback(callingPackage, inputRequest,
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800934 messenger, mockBinder);
935 assertTrue(request.requestId > 0);
Antonio Cansado6965c182016-03-30 11:37:18 -0700936 assertTrue(Objects.equals(sTemplateWifi, request.template));
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800937 long minThresholdInBytes = 2 * 1024 * 1024; // 2 MB
938 assertEquals(minThresholdInBytes, request.thresholdInBytes);
939
940 // Send dummy message to make sure that any previous message has been handled
941 mHandler.sendMessage(mHandler.obtainMessage(-1));
942 mHandlerThread.waitForIdle(WAIT_TIMEOUT);
943
944 verifyAndReset();
945
946 // Make sure that the caller binder gets connected
947 EasyMock.verify(mockBinder);
948 EasyMock.reset(mockBinder);
949
950 // modify some number on wifi, and trigger poll event
951 // not enough traffic to call data usage callback
952 incrementCurrentTime(HOUR_IN_MILLIS);
953 expectCurrentTime();
954 expectDefaultSettings();
955 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
956 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
957 expectNetworkStatsUidDetail(buildEmptyStats());
958 expectNetworkStatsPoll();
959
960 replay();
961 forcePollAndWaitForIdle();
962
963 // verify service recorded history
964 verifyAndReset();
965 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
966
967 // make sure callback has not being called
968 assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
969
970 // and bump forward again, with counters going higher. this is
971 // important, since it will trigger the data usage callback
972 incrementCurrentTime(DAY_IN_MILLIS);
973 expectCurrentTime();
974 expectDefaultSettings();
975 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
976 .addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
977 expectNetworkStatsUidDetail(buildEmptyStats());
978 expectNetworkStatsPoll();
979
980 replay();
981 forcePollAndWaitForIdle();
982
983 // verify service recorded history
984 assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
985 verifyAndReset();
986
987 // Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
988 assertTrue(cv.block(WAIT_TIMEOUT));
989 assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
990 cv.close();
991
992 // Allow binder to disconnect
993 expect(mockBinder.unlinkToDeath((IBinder.DeathRecipient) anyObject(), anyInt()))
994 .andReturn(true);
995 EasyMock.replay(mockBinder);
996
997 // Unregister request
Antonio Cansado6965c182016-03-30 11:37:18 -0700998 mService.unregisterUsageRequest(request);
Antonio Cansadocd42acd2016-02-17 13:03:38 -0800999
1000 // Wait for the caller to ack receipt of CALLBACK_RELEASED
1001 assertTrue(cv.block(WAIT_TIMEOUT));
1002 assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
1003
1004 // Make sure that the caller binder gets disconnected
1005 EasyMock.verify(mockBinder);
1006 }
1007
Antonio Cansado6965c182016-03-30 11:37:18 -07001008 public void testUnregisterUsageCallback_unknown_noop() throws Exception {
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001009 String callingPackage = "the.calling.package";
1010 long thresholdInBytes = 10 * 1024 * 1024; // 10 MB
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001011 DataUsageRequest unknownRequest = new DataUsageRequest(
Antonio Cansado6965c182016-03-30 11:37:18 -07001012 2 /* requestId */, sTemplateImsi1, thresholdInBytes);
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001013
Antonio Cansado6965c182016-03-30 11:37:18 -07001014 mService.unregisterUsageRequest(unknownRequest);
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001015 }
1016
1017 private static File getBaseDir(File statsDir) {
1018 File baseDir = new File(statsDir, "netstats");
1019 baseDir.mkdirs();
1020 return baseDir;
1021 }
1022
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001023 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -07001024 long txBytes, long txPackets, int operations) throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -07001025 assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
1026 txPackets, operations);
1027 }
1028
1029 private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
1030 long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
1031 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -07001032 final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkey70c70532012-05-16 14:51:19 -07001033 assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
1034
1035 // verify summary API
1036 final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001037 assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, ROAMING_NO, rxBytes,
Jeff Davidsona6a78072016-01-11 16:02:17 -08001038 rxPackets, txBytes, txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001039 }
1040
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001041 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -07001042 long txBytes, long txPackets, int operations) throws Exception {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001043 assertUidTotal(template, uid, SET_ALL, ROAMING_ALL, rxBytes, rxPackets, txBytes, txPackets,
1044 operations);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001045 }
1046
Jeff Davidsona6a78072016-01-11 16:02:17 -08001047 private void assertUidTotal(NetworkTemplate template, int uid, int set, int roaming,
1048 long rxBytes, long rxPackets, long txBytes, long txPackets, int operations)
1049 throws Exception {
Jeff Sharkey70c70532012-05-16 14:51:19 -07001050 // verify history API
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -07001051 final NetworkStatsHistory history = mSession.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001052 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001053 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
1054 txPackets, operations);
Jeff Sharkey70c70532012-05-16 14:51:19 -07001055
1056 // verify summary API
1057 final NetworkStats stats = mSession.getSummaryForAllUid(
1058 template, Long.MIN_VALUE, Long.MAX_VALUE, false);
Jeff Davidsona6a78072016-01-11 16:02:17 -08001059 assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, roaming, rxBytes, rxPackets, txBytes,
1060 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001061 }
1062
Jeff Sharkey8e9992a2011-08-23 18:37:23 -07001063 private void expectSystemReady() throws Exception {
Jeff Sharkey8e9992a2011-08-23 18:37:23 -07001064 mNetManager.setGlobalAlert(anyLong());
1065 expectLastCall().atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -08001066
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -08001067 expectNetworkStatsSummary(buildEmptyStats());
1068 expectBandwidthControlCheck();
Jeff Sharkey3f391352011-06-05 17:42:53 -07001069 }
1070
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001071 private void expectNetworkState(NetworkState... state) throws Exception {
1072 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -08001073
1074 final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
1075 expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001076 }
1077
1078 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -08001079 expect(mConnManager.getAllVpnInfo()).andReturn(new VpnInfo[0]).atLeastOnce();
1080
Jeff Sharkey70c70532012-05-16 14:51:19 -07001081 expectNetworkStatsSummaryDev(summary);
1082 expectNetworkStatsSummaryXt(summary);
1083 }
1084
1085 private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001086 expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
Jeff Sharkey70c70532012-05-16 14:51:19 -07001087 }
1088
1089 private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001090 expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001091 }
1092
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001093 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
Jeff Sharkey63abc372012-01-11 18:38:16 -08001094 expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
1095 }
1096
1097 private void expectNetworkStatsUidDetail(
1098 NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
1099 throws Exception {
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001100 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey63abc372012-01-11 18:38:16 -08001101
1102 // also include tethering details, since they are folded into UID
Jeff Sharkeye4984be2013-09-10 21:03:27 -07001103 expect(mNetManager.getNetworkStatsTethering())
Jeff Sharkey63abc372012-01-11 18:38:16 -08001104 .andReturn(tetherStats).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001105 }
1106
1107 private void expectDefaultSettings() throws Exception {
1108 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
1109 }
1110
Jeff Sharkey63abc372012-01-11 18:38:16 -08001111 private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001112 throws Exception {
1113 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001114 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -08001115 expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
1116
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -07001117 final Config config = new Config(bucketDuration, deleteAge, deleteAge);
Jeff Sharkey63abc372012-01-11 18:38:16 -08001118 expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
Jeff Sharkeye8914c32012-05-01 16:26:09 -07001119 expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
Jeff Sharkey63abc372012-01-11 18:38:16 -08001120 expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
1121 expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
Jeff Sharkeyac3fcb12012-05-02 18:11:52 -07001122
1123 expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1124 expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1125 expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1126 expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1127 expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001128 }
1129
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001130 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -07001131 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
1132 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001133 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -07001134 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
1135 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
1136 }
1137
Jeff Sharkey8e9992a2011-08-23 18:37:23 -07001138 private void expectNetworkStatsPoll() throws Exception {
1139 mNetManager.setGlobalAlert(anyLong());
1140 expectLastCall().anyTimes();
1141 }
1142
Jeff Davidsonb7e3fd82015-12-14 13:02:54 -08001143 private void expectBandwidthControlCheck() throws Exception {
1144 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
1145 }
1146
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001147 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -08001148 final File basePath = new File(mStatsDir, "netstats");
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001149 if (exist) {
Jeff Sharkey63abc372012-01-11 18:38:16 -08001150 assertTrue(basePath.list().length > 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001151 } else {
Jeff Sharkey63abc372012-01-11 18:38:16 -08001152 assertTrue(basePath.list().length == 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001153 }
1154 }
1155
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001156 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
Jeff Davidsona6a78072016-01-11 16:02:17 -08001157 int tag, int roaming, long rxBytes, long rxPackets, long txBytes, long txPackets,
1158 int operations) {
Jeff Sharkey70c70532012-05-16 14:51:19 -07001159 final NetworkStats.Entry entry = new NetworkStats.Entry();
Jeff Davidsona6a78072016-01-11 16:02:17 -08001160 List<Integer> sets = new ArrayList<>();
Jeff Sharkey70c70532012-05-16 14:51:19 -07001161 if (set == SET_DEFAULT || set == SET_ALL) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001162 sets.add(SET_DEFAULT);
Jeff Sharkey70c70532012-05-16 14:51:19 -07001163 }
1164 if (set == SET_FOREGROUND || set == SET_ALL) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001165 sets.add(SET_FOREGROUND);
1166 }
1167
1168 List<Integer> roamings = new ArrayList<>();
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001169 if (roaming == ROAMING_NO || roaming == ROAMING_ALL) {
1170 roamings.add(ROAMING_NO);
Jeff Davidsona6a78072016-01-11 16:02:17 -08001171 }
Jeff Davidson1f7e05e2016-03-10 13:21:38 -08001172 if (roaming == ROAMING_YES || roaming == ROAMING_ALL) {
1173 roamings.add(ROAMING_YES);
Jeff Davidsona6a78072016-01-11 16:02:17 -08001174 }
1175
1176 for (int s : sets) {
1177 for (int r : roamings) {
1178 final int i = stats.findIndex(iface, uid, s, tag, r);
1179 if (i != -1) {
1180 entry.add(stats.getValues(i, null));
1181 }
Jeff Sharkey70c70532012-05-16 14:51:19 -07001182 }
1183 }
1184
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001185 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
1186 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
1187 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
1188 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1189 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001190 }
1191
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001192 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
1193 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -07001194 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
1195 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001196 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001197 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -07001198 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1199 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -07001200 }
1201
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001202 private static NetworkState buildWifiState() {
1203 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1204 info.setDetailedState(DetailedState.CONNECTED, null, null);
1205 final LinkProperties prop = new LinkProperties();
1206 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001207 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001208 }
1209
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001210 private static NetworkState buildMobile3gState(String subscriberId) {
Jeff Davidsona6a78072016-01-11 16:02:17 -08001211 return buildMobile3gState(subscriberId, false /* isRoaming */);
1212 }
1213
1214 private static NetworkState buildMobile3gState(String subscriberId, boolean isRoaming) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001215 final NetworkInfo info = new NetworkInfo(
1216 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1217 info.setDetailedState(DetailedState.CONNECTED, null, null);
Jeff Davidsona6a78072016-01-11 16:02:17 -08001218 info.setRoaming(isRoaming);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001219 final LinkProperties prop = new LinkProperties();
1220 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -08001221 return new NetworkState(info, prop, null, null, subscriberId, null);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -07001222 }
1223
Jeff Sharkey163e6442011-10-31 16:37:52 -07001224 private static NetworkState buildMobile4gState(String iface) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001225 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1226 info.setDetailedState(DetailedState.CONNECTED, null, null);
1227 final LinkProperties prop = new LinkProperties();
Jeff Sharkey163e6442011-10-31 16:37:52 -07001228 prop.setInterfaceName(iface);
Jeff Sharkey32566012014-12-02 18:30:14 -08001229 return new NetworkState(info, prop, null, null, null, null);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -07001230 }
1231
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -07001232 private NetworkStats buildEmptyStats() {
1233 return new NetworkStats(getElapsedRealtime(), 0);
1234 }
1235
1236 private long getElapsedRealtime() {
1237 return mElapsedRealtime;
1238 }
1239
1240 private long startTimeMillis() {
1241 return TEST_START;
1242 }
1243
1244 private long currentTimeMillis() {
1245 return startTimeMillis() + mElapsedRealtime;
1246 }
1247
1248 private void incrementCurrentTime(long duration) {
1249 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -07001250 }
1251
Jeff Sharkey3f391352011-06-05 17:42:53 -07001252 private void replay() {
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001253 EasyMock.replay(mNetManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001254 }
1255
1256 private void verifyAndReset() {
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001257 EasyMock.verify(mNetManager, mTime, mSettings, mConnManager);
1258 EasyMock.reset(mNetManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -07001259 }
Antonio Cansadocd42acd2016-02-17 13:03:38 -08001260
1261 private void forcePollAndWaitForIdle() {
1262 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
1263 // Send dummy message to make sure that any previous message has been handled
1264 mHandler.sendMessage(mHandler.obtainMessage(-1));
1265 mHandlerThread.waitForIdle(WAIT_TIMEOUT);
1266 }
1267
1268 static class LatchedHandler extends Handler {
1269 private final ConditionVariable mCv;
1270 int mLastMessageType = INVALID_TYPE;
1271
1272 LatchedHandler(Looper looper, ConditionVariable cv) {
1273 super(looper);
1274 mCv = cv;
1275 }
1276
1277 @Override
1278 public void handleMessage(Message msg) {
1279 mLastMessageType = msg.what;
1280 mCv.open();
1281 super.handleMessage(msg);
1282 }
1283 }
1284
1285 /**
1286 * A subclass of HandlerThread that allows callers to wait for it to become idle. waitForIdle
1287 * will return immediately if the handler is already idle.
1288 */
1289 static class IdleableHandlerThread extends HandlerThread {
1290 private IdleHandler mIdleHandler;
1291
1292 public IdleableHandlerThread(String name) {
1293 super(name);
1294 }
1295
1296 public void waitForIdle(long timeoutMs) {
1297 final ConditionVariable cv = new ConditionVariable();
1298 final MessageQueue queue = getLooper().getQueue();
1299
1300 synchronized (queue) {
1301 if (queue.isIdle()) {
1302 return;
1303 }
1304
1305 assertNull("BUG: only one idle handler allowed", mIdleHandler);
1306 mIdleHandler = new IdleHandler() {
1307 public boolean queueIdle() {
1308 cv.open();
1309 mIdleHandler = null;
1310 return false; // Remove the handler.
1311 }
1312 };
1313 queue.addIdleHandler(mIdleHandler);
1314 }
1315
1316 if (!cv.block(timeoutMs)) {
1317 fail("HandlerThread " + getName() + " did not become idle after " + timeoutMs
1318 + " ms");
1319 queue.removeIdleHandler(mIdleHandler);
1320 }
1321 }
1322 }
1323
Jeff Sharkey3f391352011-06-05 17:42:53 -07001324}