NetworkStatsServiceTest.java revision c97d8b7464f0c60165ac9abb6b35323583fad2b5
1/*
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.net;
18
19import static android.content.Intent.ACTION_UID_REMOVED;
20import static android.content.Intent.EXTRA_UID;
21import static android.net.ConnectivityManager.TYPE_MOBILE;
22import static android.net.ConnectivityManager.TYPE_WIFI;
23import static android.net.ConnectivityManager.TYPE_WIMAX;
24import static android.net.NetworkStats.IFACE_ALL;
25import static android.net.NetworkStats.ROAMING_ALL;
26import static android.net.NetworkStats.ROAMING_NO;
27import static android.net.NetworkStats.ROAMING_YES;
28import static android.net.NetworkStats.SET_ALL;
29import static android.net.NetworkStats.SET_DEFAULT;
30import static android.net.NetworkStats.SET_FOREGROUND;
31import static android.net.NetworkStats.TAG_NONE;
32import static android.net.NetworkStats.UID_ALL;
33import static android.net.NetworkStatsHistory.FIELD_ALL;
34import static android.net.NetworkTemplate.buildTemplateMobileAll;
35import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
36import static android.net.TrafficStats.MB_IN_BYTES;
37import static android.net.TrafficStats.UID_REMOVED;
38import static android.net.TrafficStats.UID_TETHERING;
39import static android.text.format.DateUtils.DAY_IN_MILLIS;
40import static android.text.format.DateUtils.HOUR_IN_MILLIS;
41import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
42import static android.text.format.DateUtils.WEEK_IN_MILLIS;
43
44import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
45
46import static org.easymock.EasyMock.anyInt;
47import static org.easymock.EasyMock.anyLong;
48import static org.easymock.EasyMock.anyObject;
49import static org.easymock.EasyMock.capture;
50import static org.easymock.EasyMock.createMock;
51import static org.easymock.EasyMock.eq;
52import static org.easymock.EasyMock.expect;
53import static org.easymock.EasyMock.expectLastCall;
54import static org.junit.Assert.assertEquals;
55import static org.junit.Assert.assertNull;
56import static org.junit.Assert.assertTrue;
57import static org.junit.Assert.fail;
58
59import android.app.AlarmManager;
60import android.app.usage.NetworkStatsManager;
61import android.content.Context;
62import android.content.Intent;
63import android.net.DataUsageRequest;
64import android.net.IConnectivityManager;
65import android.net.INetworkManagementEventObserver;
66import android.net.INetworkStatsSession;
67import android.net.LinkProperties;
68import android.net.NetworkCapabilities;
69import android.net.NetworkInfo;
70import android.net.NetworkInfo.DetailedState;
71import android.net.NetworkState;
72import android.net.NetworkStats;
73import android.net.NetworkStatsHistory;
74import android.net.NetworkTemplate;
75import android.os.ConditionVariable;
76import android.os.Handler;
77import android.os.HandlerThread;
78import android.os.INetworkManagementService;
79import android.os.IBinder;
80import android.os.Looper;
81import android.os.Messenger;
82import android.os.MessageQueue;
83import android.os.MessageQueue.IdleHandler;
84import android.os.Message;
85import android.os.PowerManager;
86import android.support.test.InstrumentationRegistry;
87import android.support.test.runner.AndroidJUnit4;
88import android.telephony.TelephonyManager;
89import android.test.AndroidTestCase;
90import android.util.TrustedTime;
91
92import com.android.internal.net.VpnInfo;
93import com.android.internal.util.test.BroadcastInterceptingContext;
94import com.android.server.net.NetworkStatsService;
95import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
96import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
97
98import libcore.io.IoUtils;
99
100import org.easymock.Capture;
101import org.easymock.EasyMock;
102import org.junit.After;
103import org.junit.Before;
104import org.junit.Ignore;
105import org.junit.Test;
106import org.junit.runner.RunWith;
107
108import java.io.File;
109import java.util.ArrayList;
110import java.util.Objects;
111import java.util.List;
112
113/**
114 * Tests for {@link NetworkStatsService}.
115 *
116 * TODO: This test is really brittle, largely due to overly-strict use of Easymock.
117 * Rewrite w/ Mockito.
118 */
119@RunWith(AndroidJUnit4.class)
120public class NetworkStatsServiceTest {
121    private static final String TAG = "NetworkStatsServiceTest";
122
123    private static final String TEST_IFACE = "test0";
124    private static final String TEST_IFACE2 = "test1";
125    private static final long TEST_START = 1194220800000L;
126
127    private static final String IMSI_1 = "310004";
128    private static final String IMSI_2 = "310260";
129    private static final String TEST_SSID = "AndroidAP";
130
131    private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
132    private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
133    private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
134
135    private static final int UID_RED = 1001;
136    private static final int UID_BLUE = 1002;
137    private static final int UID_GREEN = 1003;
138
139    private static final long WAIT_TIMEOUT = 2 * 1000;  // 2 secs
140    private static final int INVALID_TYPE = -1;
141
142    private long mElapsedRealtime;
143
144    private BroadcastInterceptingContext mServiceContext;
145    private File mStatsDir;
146
147    private INetworkManagementService mNetManager;
148    private TrustedTime mTime;
149    private NetworkStatsSettings mSettings;
150    private IConnectivityManager mConnManager;
151    private IdleableHandlerThread mHandlerThread;
152    private Handler mHandler;
153
154    private NetworkStatsService mService;
155    private INetworkStatsSession mSession;
156    private INetworkManagementEventObserver mNetworkObserver;
157
158    @Before
159    public void setUp() throws Exception {
160        final Context context = InstrumentationRegistry.getContext();
161
162        mServiceContext = new BroadcastInterceptingContext(context);
163        mStatsDir = context.getFilesDir();
164        if (mStatsDir.exists()) {
165            IoUtils.deleteContents(mStatsDir);
166        }
167
168        mNetManager = createMock(INetworkManagementService.class);
169
170        // TODO: Mock AlarmManager when migrating this test to Mockito.
171        AlarmManager alarmManager = (AlarmManager) mServiceContext
172                .getSystemService(Context.ALARM_SERVICE);
173        mTime = createMock(TrustedTime.class);
174        mSettings = createMock(NetworkStatsSettings.class);
175        mConnManager = createMock(IConnectivityManager.class);
176
177        PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(
178                Context.POWER_SERVICE);
179        PowerManager.WakeLock wakeLock =
180                powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
181
182        mService = new NetworkStatsService(
183                mServiceContext, mNetManager, alarmManager, wakeLock, mTime,
184                TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(),
185                mStatsDir, getBaseDir(mStatsDir));
186        mHandlerThread = new IdleableHandlerThread("HandlerThread");
187        mHandlerThread.start();
188        Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
189        mHandler = new Handler(mHandlerThread.getLooper(), callback);
190        mService.setHandler(mHandler, callback);
191        mService.bindConnectivityManager(mConnManager);
192
193        mElapsedRealtime = 0L;
194
195        expectCurrentTime();
196        expectDefaultSettings();
197        expectNetworkStatsUidDetail(buildEmptyStats());
198        expectSystemReady();
199
200        // catch INetworkManagementEventObserver during systemReady()
201        final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
202                INetworkManagementEventObserver>();
203        mNetManager.registerObserver(capture(networkObserver));
204        expectLastCall().atLeastOnce();
205
206        replay();
207        mService.systemReady();
208        mSession = mService.openSession();
209        verifyAndReset();
210
211        mNetworkObserver = networkObserver.getValue();
212
213    }
214
215    @After
216    public void tearDown() throws Exception {
217        IoUtils.deleteContents(mStatsDir);
218
219        mServiceContext = null;
220        mStatsDir = null;
221
222        mNetManager = null;
223        mTime = null;
224        mSettings = null;
225        mConnManager = null;
226
227        mSession.close();
228        mService = null;
229    }
230
231    @Test
232    public void testNetworkStatsWifi() throws Exception {
233        // pretend that wifi network comes online; service should ask about full
234        // network state, and poll any existing interfaces before updating.
235        expectCurrentTime();
236        expectDefaultSettings();
237        expectNetworkState(buildWifiState());
238        expectNetworkStatsSummary(buildEmptyStats());
239        expectNetworkStatsUidDetail(buildEmptyStats());
240        expectNetworkStatsPoll();
241        expectBandwidthControlCheck();
242
243        replay();
244        mService.forceUpdateIfaces();
245
246        // verify service has empty history for wifi
247        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
248        verifyAndReset();
249
250        // modify some number on wifi, and trigger poll event
251        incrementCurrentTime(HOUR_IN_MILLIS);
252        expectCurrentTime();
253        expectDefaultSettings();
254        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
255                .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
256        expectNetworkStatsUidDetail(buildEmptyStats());
257        expectNetworkStatsPoll();
258
259        replay();
260        forcePollAndWaitForIdle();
261
262        // verify service recorded history
263        assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
264        verifyAndReset();
265
266        // and bump forward again, with counters going higher. this is
267        // important, since polling should correctly subtract last snapshot.
268        incrementCurrentTime(DAY_IN_MILLIS);
269        expectCurrentTime();
270        expectDefaultSettings();
271        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
272                .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
273        expectNetworkStatsUidDetail(buildEmptyStats());
274        expectNetworkStatsPoll();
275
276        replay();
277        forcePollAndWaitForIdle();
278
279        // verify service recorded history
280        assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
281        verifyAndReset();
282
283    }
284
285    @Test
286    public void testStatsRebootPersist() throws Exception {
287        assertStatsFilesExist(false);
288
289        // pretend that wifi network comes online; service should ask about full
290        // network state, and poll any existing interfaces before updating.
291        expectCurrentTime();
292        expectDefaultSettings();
293        expectNetworkState(buildWifiState());
294        expectNetworkStatsSummary(buildEmptyStats());
295        expectNetworkStatsUidDetail(buildEmptyStats());
296        expectNetworkStatsPoll();
297        expectBandwidthControlCheck();
298
299        replay();
300        mService.forceUpdateIfaces();
301
302        // verify service has empty history for wifi
303        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
304        verifyAndReset();
305
306        // modify some number on wifi, and trigger poll event
307        incrementCurrentTime(HOUR_IN_MILLIS);
308        expectCurrentTime();
309        expectDefaultSettings();
310        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
311                .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
312        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
313                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
314                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
315                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
316                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
317                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
318        expectNetworkStatsPoll();
319
320        mService.setUidForeground(UID_RED, false);
321        mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
322        mService.setUidForeground(UID_RED, true);
323        mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
324
325        replay();
326        forcePollAndWaitForIdle();
327
328        // verify service recorded history
329        assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
330        assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
331        assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
332        assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L,
333                6);
334        assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
335        verifyAndReset();
336
337        // graceful shutdown system, which should trigger persist of stats, and
338        // clear any values in memory.
339        expectCurrentTime();
340        expectDefaultSettings();
341        replay();
342        mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
343        verifyAndReset();
344
345        assertStatsFilesExist(true);
346
347        // boot through serviceReady() again
348        expectCurrentTime();
349        expectDefaultSettings();
350        expectNetworkStatsUidDetail(buildEmptyStats());
351        expectSystemReady();
352
353        // catch INetworkManagementEventObserver during systemReady()
354        final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
355                INetworkManagementEventObserver>();
356        mNetManager.registerObserver(capture(networkObserver));
357        expectLastCall().atLeastOnce();
358
359        replay();
360        mService.systemReady();
361
362        mNetworkObserver = networkObserver.getValue();
363
364        // after systemReady(), we should have historical stats loaded again
365        assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
366        assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
367        assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
368        assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L,
369                6);
370        assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
371        verifyAndReset();
372
373    }
374
375    // TODO: simulate reboot to test bucket resize
376    @Test
377    @Ignore
378    public void testStatsBucketResize() throws Exception {
379        NetworkStatsHistory history = null;
380
381        assertStatsFilesExist(false);
382
383        // pretend that wifi network comes online; service should ask about full
384        // network state, and poll any existing interfaces before updating.
385        expectCurrentTime();
386        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
387        expectNetworkState(buildWifiState());
388        expectNetworkStatsSummary(buildEmptyStats());
389        expectNetworkStatsUidDetail(buildEmptyStats());
390        expectNetworkStatsPoll();
391        expectBandwidthControlCheck();
392
393        replay();
394        mService.forceUpdateIfaces();
395        verifyAndReset();
396
397        // modify some number on wifi, and trigger poll event
398        incrementCurrentTime(2 * HOUR_IN_MILLIS);
399        expectCurrentTime();
400        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
401        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
402                .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
403        expectNetworkStatsUidDetail(buildEmptyStats());
404        expectNetworkStatsPoll();
405
406        replay();
407        forcePollAndWaitForIdle();
408
409        // verify service recorded history
410        history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
411        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
412        assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
413        assertEquals(2, history.size());
414        verifyAndReset();
415
416        // now change bucket duration setting and trigger another poll with
417        // exact same values, which should resize existing buckets.
418        expectCurrentTime();
419        expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
420        expectNetworkStatsSummary(buildEmptyStats());
421        expectNetworkStatsUidDetail(buildEmptyStats());
422        expectNetworkStatsPoll();
423
424        replay();
425        forcePollAndWaitForIdle();
426
427        // verify identical stats, but spread across 4 buckets now
428        history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
429        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
430        assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
431        assertEquals(4, history.size());
432        verifyAndReset();
433
434    }
435
436    @Test
437    public void testUidStatsAcrossNetworks() throws Exception {
438        // pretend first mobile network comes online
439        expectCurrentTime();
440        expectDefaultSettings();
441        expectNetworkState(buildMobile3gState(IMSI_1));
442        expectNetworkStatsSummary(buildEmptyStats());
443        expectNetworkStatsUidDetail(buildEmptyStats());
444        expectNetworkStatsPoll();
445        expectBandwidthControlCheck();
446
447        replay();
448        mService.forceUpdateIfaces();
449        verifyAndReset();
450
451        // create some traffic on first network
452        incrementCurrentTime(HOUR_IN_MILLIS);
453        expectCurrentTime();
454        expectDefaultSettings();
455        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
456                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
457        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
458                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
459                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
460                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
461        expectNetworkStatsPoll();
462
463        mService.incrementOperationCount(UID_RED, 0xF00D, 10);
464
465        replay();
466        forcePollAndWaitForIdle();
467
468        // verify service recorded history
469        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
470        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
471        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
472        assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
473        verifyAndReset();
474
475        // now switch networks; this also tests that we're okay with interfaces
476        // disappearing, to verify we don't count backwards.
477        incrementCurrentTime(HOUR_IN_MILLIS);
478        expectCurrentTime();
479        expectDefaultSettings();
480        expectNetworkState(buildMobile3gState(IMSI_2));
481        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
482                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
483        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
484                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
485                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
486                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
487        expectNetworkStatsPoll();
488        expectBandwidthControlCheck();
489
490        replay();
491        mService.forceUpdateIfaces();
492        forcePollAndWaitForIdle();
493        verifyAndReset();
494
495        // create traffic on second network
496        incrementCurrentTime(HOUR_IN_MILLIS);
497        expectCurrentTime();
498        expectDefaultSettings();
499        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
500                .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
501        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
502                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
503                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
504                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
505                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
506        expectNetworkStatsPoll();
507
508        mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
509
510        replay();
511        forcePollAndWaitForIdle();
512
513        // verify original history still intact
514        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
515        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
516        assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
517
518        // and verify new history also recorded under different template, which
519        // verifies that we didn't cross the streams.
520        assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
521        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
522        assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
523        verifyAndReset();
524
525    }
526
527    @Test
528    public void testUidRemovedIsMoved() throws Exception {
529        // pretend that network comes online
530        expectCurrentTime();
531        expectDefaultSettings();
532        expectNetworkState(buildWifiState());
533        expectNetworkStatsSummary(buildEmptyStats());
534        expectNetworkStatsUidDetail(buildEmptyStats());
535        expectNetworkStatsPoll();
536        expectBandwidthControlCheck();
537
538        replay();
539        mService.forceUpdateIfaces();
540        verifyAndReset();
541
542        // create some traffic
543        incrementCurrentTime(HOUR_IN_MILLIS);
544        expectCurrentTime();
545        expectDefaultSettings();
546        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
547                .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
548        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
549                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
550                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
551                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
552                .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
553        expectNetworkStatsPoll();
554
555        mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
556
557        replay();
558        forcePollAndWaitForIdle();
559
560        // verify service recorded history
561        assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
562        assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
563        assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
564        assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
565        verifyAndReset();
566
567        // now pretend two UIDs are uninstalled, which should migrate stats to
568        // special "removed" bucket.
569        expectCurrentTime();
570        expectDefaultSettings();
571        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
572                .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
573        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
574                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
575                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
576                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
577                .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
578        expectNetworkStatsPoll();
579
580        replay();
581        final Intent intent = new Intent(ACTION_UID_REMOVED);
582        intent.putExtra(EXTRA_UID, UID_BLUE);
583        mServiceContext.sendBroadcast(intent);
584        intent.putExtra(EXTRA_UID, UID_RED);
585        mServiceContext.sendBroadcast(intent);
586
587        // existing uid and total should remain unchanged; but removed UID
588        // should be gone completely.
589        assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
590        assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
591        assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
592        assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
593        assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
594        verifyAndReset();
595
596    }
597
598    @Test
599    public void testUid3g4gCombinedByTemplate() throws Exception {
600        // pretend that network comes online
601        expectCurrentTime();
602        expectDefaultSettings();
603        expectNetworkState(buildMobile3gState(IMSI_1));
604        expectNetworkStatsSummary(buildEmptyStats());
605        expectNetworkStatsUidDetail(buildEmptyStats());
606        expectNetworkStatsPoll();
607        expectBandwidthControlCheck();
608
609        replay();
610        mService.forceUpdateIfaces();
611        verifyAndReset();
612
613        // create some traffic
614        incrementCurrentTime(HOUR_IN_MILLIS);
615        expectCurrentTime();
616        expectDefaultSettings();
617        expectNetworkStatsSummary(buildEmptyStats());
618        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
619                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
620                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
621        expectNetworkStatsPoll();
622
623        mService.incrementOperationCount(UID_RED, 0xF00D, 5);
624
625        replay();
626        forcePollAndWaitForIdle();
627
628        // verify service recorded history
629        assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
630        verifyAndReset();
631
632        // now switch over to 4g network
633        incrementCurrentTime(HOUR_IN_MILLIS);
634        expectCurrentTime();
635        expectDefaultSettings();
636        expectNetworkState(buildMobile4gState(TEST_IFACE2));
637        expectNetworkStatsSummary(buildEmptyStats());
638        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
639                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
640                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
641        expectNetworkStatsPoll();
642        expectBandwidthControlCheck();
643
644        replay();
645        mService.forceUpdateIfaces();
646        forcePollAndWaitForIdle();
647        verifyAndReset();
648
649        // create traffic on second network
650        incrementCurrentTime(HOUR_IN_MILLIS);
651        expectCurrentTime();
652        expectDefaultSettings();
653        expectNetworkStatsSummary(buildEmptyStats());
654        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
655                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
656                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
657                .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
658                .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
659        expectNetworkStatsPoll();
660
661        mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
662
663        replay();
664        forcePollAndWaitForIdle();
665
666        // verify that ALL_MOBILE template combines both
667        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
668
669        verifyAndReset();
670    }
671
672    @Test
673    public void testSummaryForAllUid() throws Exception {
674        // pretend that network comes online
675        expectCurrentTime();
676        expectDefaultSettings();
677        expectNetworkState(buildWifiState());
678        expectNetworkStatsSummary(buildEmptyStats());
679        expectNetworkStatsUidDetail(buildEmptyStats());
680        expectNetworkStatsPoll();
681        expectBandwidthControlCheck();
682
683        replay();
684        mService.forceUpdateIfaces();
685        verifyAndReset();
686
687        // create some traffic for two apps
688        incrementCurrentTime(HOUR_IN_MILLIS);
689        expectCurrentTime();
690        expectDefaultSettings();
691        expectNetworkStatsSummary(buildEmptyStats());
692        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
693                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
694                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
695                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
696        expectNetworkStatsPoll();
697
698        mService.incrementOperationCount(UID_RED, 0xF00D, 1);
699
700        replay();
701        forcePollAndWaitForIdle();
702
703        // verify service recorded history
704        assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
705        assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
706        verifyAndReset();
707
708        // now create more traffic in next hour, but only for one app
709        incrementCurrentTime(HOUR_IN_MILLIS);
710        expectCurrentTime();
711        expectDefaultSettings();
712        expectNetworkStatsSummary(buildEmptyStats());
713        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
714                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
715                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
716                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
717        expectNetworkStatsPoll();
718
719        replay();
720        forcePollAndWaitForIdle();
721
722        // first verify entire history present
723        NetworkStats stats = mSession.getSummaryForAllUid(
724                sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
725        assertEquals(3, stats.size());
726        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 50L, 5L,
727                50L, 5L, 1);
728        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 10L, 1L, 10L,
729                1L, 1);
730        assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_NO, 2048L, 16L,
731                1024L, 8L, 0);
732
733        // now verify that recent history only contains one uid
734        final long currentTime = currentTimeMillis();
735        stats = mSession.getSummaryForAllUid(
736                sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
737        assertEquals(1, stats.size());
738        assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, ROAMING_NO, 1024L, 8L,
739                512L, 4L, 0);
740
741        verifyAndReset();
742    }
743
744    @Test
745    public void testForegroundBackground() throws Exception {
746        // pretend that network comes online
747        expectCurrentTime();
748        expectDefaultSettings();
749        expectNetworkState(buildWifiState());
750        expectNetworkStatsSummary(buildEmptyStats());
751        expectNetworkStatsUidDetail(buildEmptyStats());
752        expectNetworkStatsPoll();
753        expectBandwidthControlCheck();
754
755        replay();
756        mService.forceUpdateIfaces();
757        verifyAndReset();
758
759        // create some initial traffic
760        incrementCurrentTime(HOUR_IN_MILLIS);
761        expectCurrentTime();
762        expectDefaultSettings();
763        expectNetworkStatsSummary(buildEmptyStats());
764        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
765                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
766                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
767        expectNetworkStatsPoll();
768
769        mService.incrementOperationCount(UID_RED, 0xF00D, 1);
770
771        replay();
772        forcePollAndWaitForIdle();
773
774        // verify service recorded history
775        assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
776        verifyAndReset();
777
778        // now switch to foreground
779        incrementCurrentTime(HOUR_IN_MILLIS);
780        expectCurrentTime();
781        expectDefaultSettings();
782        expectNetworkStatsSummary(buildEmptyStats());
783        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
784                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
785                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
786                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
787                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
788        expectNetworkStatsPoll();
789
790        mService.setUidForeground(UID_RED, true);
791        mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
792
793        replay();
794        forcePollAndWaitForIdle();
795
796        // test that we combined correctly
797        assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
798
799        // verify entire history present
800        final NetworkStats stats = mSession.getSummaryForAllUid(
801                sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
802        assertEquals(4, stats.size());
803        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 128L, 2L,
804                128L, 2L, 1);
805        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 64L, 1L, 64L,
806                1L, 1);
807        assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, ROAMING_NO, 32L, 2L,
808                32L, 2L, 1);
809        assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, ROAMING_NO, 1L, 1L, 1L,
810                1L, 1);
811
812        verifyAndReset();
813    }
814
815    @Test
816    public void testRoaming() throws Exception {
817        // pretend that network comes online
818        expectCurrentTime();
819        expectDefaultSettings();
820        expectNetworkState(buildMobile3gState(IMSI_1, true /* isRoaming */));
821        expectNetworkStatsSummary(buildEmptyStats());
822        expectNetworkStatsUidDetail(buildEmptyStats());
823        expectNetworkStatsPoll();
824        expectBandwidthControlCheck();
825
826        replay();
827        mService.forceUpdateIfaces();
828        verifyAndReset();
829
830        // Create some traffic
831        incrementCurrentTime(HOUR_IN_MILLIS);
832        expectCurrentTime();
833        expectDefaultSettings();
834        expectNetworkStatsSummary(buildEmptyStats());
835        // Note that all traffic from NetworkManagementService is tagged as ROAMING_NO, because
836        // roaming isn't tracked at that layer. We layer it on top by inspecting the iface
837        // properties.
838        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
839                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_NO, 128L, 2L,
840                        128L, 2L, 0L)
841                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_NO, 64L, 1L, 64L,
842                        1L, 0L));
843        expectNetworkStatsPoll();
844
845        replay();
846        forcePollAndWaitForIdle();
847
848        // verify service recorded history
849        assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
850
851        // verify entire history present
852        final NetworkStats stats = mSession.getSummaryForAllUid(
853                sTemplateImsi1, Long.MIN_VALUE, Long.MAX_VALUE, true);
854        assertEquals(2, stats.size());
855        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, ROAMING_YES, 128L, 2L,
856                128L, 2L, 0);
857        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, ROAMING_YES, 64L, 1L, 64L,
858                1L, 0);
859
860        verifyAndReset();
861    }
862
863    @Test
864    public void testTethering() throws Exception {
865        // pretend first mobile network comes online
866        expectCurrentTime();
867        expectDefaultSettings();
868        expectNetworkState(buildMobile3gState(IMSI_1));
869        expectNetworkStatsSummary(buildEmptyStats());
870        expectNetworkStatsUidDetail(buildEmptyStats());
871        expectNetworkStatsPoll();
872        expectBandwidthControlCheck();
873
874        replay();
875        mService.forceUpdateIfaces();
876        verifyAndReset();
877
878        // create some tethering traffic
879        incrementCurrentTime(HOUR_IN_MILLIS);
880        expectCurrentTime();
881        expectDefaultSettings();
882        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
883                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
884
885        final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
886                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
887        final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
888        final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
889                .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
890
891        expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
892        expectNetworkStatsPoll();
893
894        replay();
895        forcePollAndWaitForIdle();
896
897        // verify service recorded history
898        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
899        assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
900        assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
901        verifyAndReset();
902
903    }
904
905    @Test
906    public void testRegisterUsageCallback() throws Exception {
907        // pretend that wifi network comes online; service should ask about full
908        // network state, and poll any existing interfaces before updating.
909        expectCurrentTime();
910        expectDefaultSettings();
911        expectNetworkState(buildWifiState());
912        expectNetworkStatsSummary(buildEmptyStats());
913        expectNetworkStatsUidDetail(buildEmptyStats());
914        expectNetworkStatsPoll();
915        expectBandwidthControlCheck();
916
917        replay();
918        mService.forceUpdateIfaces();
919
920        // verify service has empty history for wifi
921        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
922        verifyAndReset();
923
924        String callingPackage = "the.calling.package";
925        long thresholdInBytes = 1L;  // very small; should be overriden by framework
926        DataUsageRequest inputRequest = new DataUsageRequest(
927                DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
928
929        // Create a messenger that waits for callback activity
930        ConditionVariable cv = new ConditionVariable(false);
931        LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
932        Messenger messenger = new Messenger(latchedHandler);
933
934        // Allow binder to connect
935        IBinder mockBinder = createMock(IBinder.class);
936        mockBinder.linkToDeath((IBinder.DeathRecipient) anyObject(), anyInt());
937        EasyMock.replay(mockBinder);
938
939        // Force poll
940        expectCurrentTime();
941        expectDefaultSettings();
942        expectNetworkStatsSummary(buildEmptyStats());
943        expectNetworkStatsUidDetail(buildEmptyStats());
944        expectNetworkStatsPoll();
945        replay();
946
947        // Register and verify request and that binder was called
948        DataUsageRequest request =
949                mService.registerUsageCallback(callingPackage, inputRequest,
950                        messenger, mockBinder);
951        assertTrue(request.requestId > 0);
952        assertTrue(Objects.equals(sTemplateWifi, request.template));
953        long minThresholdInBytes = 2 * 1024 * 1024; // 2 MB
954        assertEquals(minThresholdInBytes, request.thresholdInBytes);
955
956        // Send dummy message to make sure that any previous message has been handled
957        mHandler.sendMessage(mHandler.obtainMessage(-1));
958        mHandlerThread.waitForIdle(WAIT_TIMEOUT);
959
960        verifyAndReset();
961
962        // Make sure that the caller binder gets connected
963        EasyMock.verify(mockBinder);
964        EasyMock.reset(mockBinder);
965
966        // modify some number on wifi, and trigger poll event
967        // not enough traffic to call data usage callback
968        incrementCurrentTime(HOUR_IN_MILLIS);
969        expectCurrentTime();
970        expectDefaultSettings();
971        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
972                .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
973        expectNetworkStatsUidDetail(buildEmptyStats());
974        expectNetworkStatsPoll();
975
976        replay();
977        forcePollAndWaitForIdle();
978
979        // verify service recorded history
980        verifyAndReset();
981        assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
982
983        // make sure callback has not being called
984        assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
985
986        // and bump forward again, with counters going higher. this is
987        // important, since it will trigger the data usage callback
988        incrementCurrentTime(DAY_IN_MILLIS);
989        expectCurrentTime();
990        expectDefaultSettings();
991        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
992                .addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
993        expectNetworkStatsUidDetail(buildEmptyStats());
994        expectNetworkStatsPoll();
995
996        replay();
997        forcePollAndWaitForIdle();
998
999        // verify service recorded history
1000        assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
1001        verifyAndReset();
1002
1003        // Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
1004        assertTrue(cv.block(WAIT_TIMEOUT));
1005        assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
1006        cv.close();
1007
1008        // Allow binder to disconnect
1009        expect(mockBinder.unlinkToDeath((IBinder.DeathRecipient) anyObject(), anyInt()))
1010                .andReturn(true);
1011        EasyMock.replay(mockBinder);
1012
1013        // Unregister request
1014        mService.unregisterUsageRequest(request);
1015
1016        // Wait for the caller to ack receipt of CALLBACK_RELEASED
1017        assertTrue(cv.block(WAIT_TIMEOUT));
1018        assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
1019
1020        // Make sure that the caller binder gets disconnected
1021        EasyMock.verify(mockBinder);
1022    }
1023
1024    @Test
1025    public void testUnregisterUsageCallback_unknown_noop() throws Exception {
1026        String callingPackage = "the.calling.package";
1027        long thresholdInBytes = 10 * 1024 * 1024;  // 10 MB
1028        DataUsageRequest unknownRequest = new DataUsageRequest(
1029                2 /* requestId */, sTemplateImsi1, thresholdInBytes);
1030
1031        mService.unregisterUsageRequest(unknownRequest);
1032    }
1033
1034    private static File getBaseDir(File statsDir) {
1035        File baseDir = new File(statsDir, "netstats");
1036        baseDir.mkdirs();
1037        return baseDir;
1038    }
1039
1040    private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
1041            long txBytes, long txPackets, int operations) throws Exception {
1042        assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
1043                txPackets, operations);
1044    }
1045
1046    private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
1047            long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
1048        // verify history API
1049        final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
1050        assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
1051
1052        // verify summary API
1053        final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
1054        assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, ROAMING_NO, rxBytes,
1055                rxPackets, txBytes, txPackets, operations);
1056    }
1057
1058    private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
1059            long txBytes, long txPackets, int operations) throws Exception {
1060        assertUidTotal(template, uid, SET_ALL, ROAMING_ALL, rxBytes, rxPackets, txBytes, txPackets,
1061                operations);
1062    }
1063
1064    private void assertUidTotal(NetworkTemplate template, int uid, int set, int roaming,
1065            long rxBytes, long rxPackets, long txBytes, long txPackets, int operations)
1066            throws Exception {
1067        // verify history API
1068        final NetworkStatsHistory history = mSession.getHistoryForUid(
1069                template, uid, set, TAG_NONE, FIELD_ALL);
1070        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
1071                txPackets, operations);
1072
1073        // verify summary API
1074        final NetworkStats stats = mSession.getSummaryForAllUid(
1075                template, Long.MIN_VALUE, Long.MAX_VALUE, false);
1076        assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, roaming, rxBytes, rxPackets, txBytes,
1077                txPackets, operations);
1078    }
1079
1080    private void expectSystemReady() throws Exception {
1081        mNetManager.setGlobalAlert(anyLong());
1082        expectLastCall().atLeastOnce();
1083
1084        expectNetworkStatsSummary(buildEmptyStats());
1085        expectBandwidthControlCheck();
1086    }
1087
1088    private void expectNetworkState(NetworkState... state) throws Exception {
1089        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
1090
1091        final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
1092        expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
1093    }
1094
1095    private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
1096        expect(mConnManager.getAllVpnInfo()).andReturn(new VpnInfo[0]).atLeastOnce();
1097
1098        expectNetworkStatsSummaryDev(summary);
1099        expectNetworkStatsSummaryXt(summary);
1100    }
1101
1102    private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
1103        expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
1104    }
1105
1106    private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
1107        expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
1108    }
1109
1110    private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
1111        expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
1112    }
1113
1114    private void expectNetworkStatsUidDetail(
1115            NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
1116            throws Exception {
1117        expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
1118
1119        // also include tethering details, since they are folded into UID
1120        expect(mNetManager.getNetworkStatsTethering())
1121                .andReturn(tetherStats).atLeastOnce();
1122    }
1123
1124    private void expectDefaultSettings() throws Exception {
1125        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
1126    }
1127
1128    private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
1129            throws Exception {
1130        expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
1131        expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
1132        expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
1133
1134        final Config config = new Config(bucketDuration, deleteAge, deleteAge);
1135        expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
1136        expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
1137        expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
1138        expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
1139
1140        expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1141        expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1142        expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1143        expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1144        expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
1145    }
1146
1147    private void expectCurrentTime() throws Exception {
1148        expect(mTime.forceRefresh()).andReturn(false).anyTimes();
1149        expect(mTime.hasCache()).andReturn(true).anyTimes();
1150        expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
1151        expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
1152        expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
1153    }
1154
1155    private void expectNetworkStatsPoll() throws Exception {
1156        mNetManager.setGlobalAlert(anyLong());
1157        expectLastCall().anyTimes();
1158    }
1159
1160    private void expectBandwidthControlCheck() throws Exception {
1161        expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
1162    }
1163
1164    private void assertStatsFilesExist(boolean exist) {
1165        final File basePath = new File(mStatsDir, "netstats");
1166        if (exist) {
1167            assertTrue(basePath.list().length > 0);
1168        } else {
1169            assertTrue(basePath.list().length == 0);
1170        }
1171    }
1172
1173    private static void assertValues(NetworkStats stats, String iface, int uid, int set,
1174            int tag, int roaming, long rxBytes, long rxPackets, long txBytes, long txPackets,
1175            int operations) {
1176        final NetworkStats.Entry entry = new NetworkStats.Entry();
1177        List<Integer> sets = new ArrayList<>();
1178        if (set == SET_DEFAULT || set == SET_ALL) {
1179            sets.add(SET_DEFAULT);
1180        }
1181        if (set == SET_FOREGROUND || set == SET_ALL) {
1182            sets.add(SET_FOREGROUND);
1183        }
1184
1185        List<Integer> roamings = new ArrayList<>();
1186        if (roaming == ROAMING_NO || roaming == ROAMING_ALL) {
1187            roamings.add(ROAMING_NO);
1188        }
1189        if (roaming == ROAMING_YES || roaming == ROAMING_ALL) {
1190            roamings.add(ROAMING_YES);
1191        }
1192
1193        for (int s : sets) {
1194            for (int r : roamings) {
1195                final int i = stats.findIndex(iface, uid, s, tag, r);
1196                if (i != -1) {
1197                    entry.add(stats.getValues(i, null));
1198                }
1199            }
1200        }
1201
1202        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
1203        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
1204        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
1205        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1206        assertEquals("unexpected operations", operations, entry.operations);
1207    }
1208
1209    private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
1210            long rxPackets, long txBytes, long txPackets, int operations) {
1211        final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
1212        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
1213        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
1214        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
1215        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1216        assertEquals("unexpected operations", operations, entry.operations);
1217    }
1218
1219    private static NetworkState buildWifiState() {
1220        final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1221        info.setDetailedState(DetailedState.CONNECTED, null, null);
1222        final LinkProperties prop = new LinkProperties();
1223        prop.setInterfaceName(TEST_IFACE);
1224        final NetworkCapabilities capabilities = new NetworkCapabilities();
1225        return new NetworkState(info, prop, capabilities, null, null, TEST_SSID);
1226    }
1227
1228    private static NetworkState buildMobile3gState(String subscriberId) {
1229        return buildMobile3gState(subscriberId, false /* isRoaming */);
1230    }
1231
1232    private static NetworkState buildMobile3gState(String subscriberId, boolean isRoaming) {
1233        final NetworkInfo info = new NetworkInfo(
1234                TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1235        info.setDetailedState(DetailedState.CONNECTED, null, null);
1236        info.setRoaming(isRoaming);
1237        final LinkProperties prop = new LinkProperties();
1238        prop.setInterfaceName(TEST_IFACE);
1239        final NetworkCapabilities capabilities = new NetworkCapabilities();
1240        return new NetworkState(info, prop, capabilities, null, subscriberId, null);
1241    }
1242
1243    private static NetworkState buildMobile4gState(String iface) {
1244        final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1245        info.setDetailedState(DetailedState.CONNECTED, null, null);
1246        final LinkProperties prop = new LinkProperties();
1247        prop.setInterfaceName(iface);
1248        final NetworkCapabilities capabilities = new NetworkCapabilities();
1249        return new NetworkState(info, prop, capabilities, null, null, null);
1250    }
1251
1252    private NetworkStats buildEmptyStats() {
1253        return new NetworkStats(getElapsedRealtime(), 0);
1254    }
1255
1256    private long getElapsedRealtime() {
1257        return mElapsedRealtime;
1258    }
1259
1260    private long startTimeMillis() {
1261        return TEST_START;
1262    }
1263
1264    private long currentTimeMillis() {
1265        return startTimeMillis() + mElapsedRealtime;
1266    }
1267
1268    private void incrementCurrentTime(long duration) {
1269        mElapsedRealtime += duration;
1270    }
1271
1272    private void replay() {
1273        EasyMock.replay(mNetManager, mTime, mSettings, mConnManager);
1274    }
1275
1276    private void verifyAndReset() {
1277        EasyMock.verify(mNetManager, mTime, mSettings, mConnManager);
1278        EasyMock.reset(mNetManager, mTime, mSettings, mConnManager);
1279    }
1280
1281    private void forcePollAndWaitForIdle() {
1282        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
1283        // Send dummy message to make sure that any previous message has been handled
1284        mHandler.sendMessage(mHandler.obtainMessage(-1));
1285        mHandlerThread.waitForIdle(WAIT_TIMEOUT);
1286    }
1287
1288    static class LatchedHandler extends Handler {
1289        private final ConditionVariable mCv;
1290        int mLastMessageType = INVALID_TYPE;
1291
1292        LatchedHandler(Looper looper, ConditionVariable cv) {
1293            super(looper);
1294            mCv = cv;
1295        }
1296
1297        @Override
1298        public void handleMessage(Message msg) {
1299            mLastMessageType = msg.what;
1300            mCv.open();
1301            super.handleMessage(msg);
1302        }
1303    }
1304
1305    /**
1306     * A subclass of HandlerThread that allows callers to wait for it to become idle. waitForIdle
1307     * will return immediately if the handler is already idle.
1308     */
1309    static class IdleableHandlerThread extends HandlerThread {
1310        private IdleHandler mIdleHandler;
1311
1312        public IdleableHandlerThread(String name) {
1313            super(name);
1314        }
1315
1316        public void waitForIdle(long timeoutMs) {
1317            final ConditionVariable cv = new ConditionVariable();
1318            final MessageQueue queue = getLooper().getQueue();
1319
1320            synchronized (queue) {
1321                if (queue.isIdle()) {
1322                    return;
1323                }
1324
1325                assertNull("BUG: only one idle handler allowed", mIdleHandler);
1326                mIdleHandler = new IdleHandler() {
1327                    public boolean queueIdle() {
1328                        cv.open();
1329                        mIdleHandler = null;
1330                        return false;  // Remove the handler.
1331                    }
1332                };
1333                queue.addIdleHandler(mIdleHandler);
1334            }
1335
1336            if (!cv.block(timeoutMs)) {
1337                fail("HandlerThread " + getName() + " did not become idle after " + timeoutMs
1338                        + " ms");
1339                queue.removeIdleHandler(mIdleHandler);
1340            }
1341        }
1342    }
1343
1344}
1345