NetworkStatsServiceTest.java revision bc668ac4b7c54278ed20d8329eb80e0b54892dc7
1f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi/*
2f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi * Copyright (C) 2011 The Android Open Source Project
37b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia *
47b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia * Licensed under the Apache License, Version 2.0 (the "License");
57b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia * you may not use this file except in compliance with the License.
67b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia * You may obtain a copy of the License at
77b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia *
87b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia *      http://www.apache.org/licenses/LICENSE-2.0
97b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia *
107b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia * Unless required by applicable law or agreed to in writing, software
11f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi * distributed under the License is distributed on an "AS IS" BASIS,
12f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi * See the License for the specific language governing permissions and
14f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi * limitations under the License.
157b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia */
167b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia
17f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesipackage com.android.server;
187b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia
197b5e903f71127867950ba87f363a491ba5e8306dDiego Garciaimport static android.content.Intent.ACTION_UID_REMOVED;
20f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesiimport static android.content.Intent.EXTRA_UID;
21f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesiimport static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
22f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesiimport static android.net.ConnectivityManager.TYPE_MOBILE;
2340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.ConnectivityManager.TYPE_WIFI;
2440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.ConnectivityManager.TYPE_WIMAX;
2540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.IFACE_ALL;
2640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.SET_ALL;
2740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.SET_DEFAULT;
2840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.SET_FOREGROUND;
2940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.TAG_NONE;
3040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStats.UID_ALL;
3140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkStatsHistory.FIELD_ALL;
3240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkTemplate.buildTemplateMobileAll;
3340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.NetworkTemplate.buildTemplateWifiWildcard;
3440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.TrafficStats.MB_IN_BYTES;
3540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.TrafficStats.UID_REMOVED;
3640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.net.TrafficStats.UID_TETHERING;
3740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.text.format.DateUtils.DAY_IN_MILLIS;
3840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.text.format.DateUtils.HOUR_IN_MILLIS;
3940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.text.format.DateUtils.MINUTE_IN_MILLIS;
4040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static android.text.format.DateUtils.WEEK_IN_MILLIS;
4140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
4240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport static org.easymock.EasyMock.anyLong;
430b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.aryEq;
440b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.capture;
450b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.createMock;
460b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.eq;
470b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.expect;
480b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.expectLastCall;
490b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport static org.easymock.EasyMock.isA;
500b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowham
510b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.app.AlarmManager;
5240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.app.IAlarmManager;
5340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.app.PendingIntent;
540b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.content.Intent;
550b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.net.IConnectivityManager;
560b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.net.INetworkManagementEventObserver;
570b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.net.INetworkStatsSession;
580b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.net.LinkProperties;
590b1cf2dae9f688eaf17eee6ba853ebf5f9b21689adriancowhamimport android.net.NetworkInfo;
6040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.net.NetworkInfo.DetailedState;
6140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.net.NetworkState;
6240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.net.NetworkStats;
6340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.net.NetworkStatsHistory;
6440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.net.NetworkTemplate;
6540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.os.INetworkManagementService;
6640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.telephony.TelephonyManager;
6740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.test.AndroidTestCase;
6840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.test.suitebuilder.annotation.LargeTest;
6940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.test.suitebuilder.annotation.Suppress;
7040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport android.util.TrustedTime;
7140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
7240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport com.android.server.net.NetworkStatsService;
7340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport com.android.server.net.NetworkStatsService.NetworkStatsSettings;
7440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
7540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
7640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport org.easymock.Capture;
7740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport org.easymock.EasyMock;
7840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
7940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport java.io.File;
8040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
8140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamsimport libcore.io.IoUtils;
8240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
8340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams/**
8440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams * Tests for {@link NetworkStatsService}.
8540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams */
8640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams@LargeTest
8740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williamspublic class NetworkStatsServiceTest extends AndroidTestCase {
8840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String TAG = "NetworkStatsServiceTest";
8940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
9040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String TEST_IFACE = "test0";
9140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String TEST_IFACE2 = "test1";
9240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final long TEST_START = 1194220800000L;
9340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
9440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String IMSI_1 = "310004";
9540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String IMSI_2 = "310260";
9640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final String TEST_SSID = "AndroidAP";
9740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
9840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static NetworkTemplate sTemplateWifi = buildTemplateWifiWildcard();
9940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
10040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
10140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
10240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final int UID_RED = 1001;
10340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private static final int UID_BLUE = 1002;
104bf6a1c2145b187c23b06243ceaa6f85c24498643Lenny Turetsky & Phil Plante    private static final int UID_GREEN = 1003;
10509e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
10640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private long mElapsedRealtime;
107bf6a1c2145b187c23b06243ceaa6f85c24498643Lenny Turetsky & Phil Plante
10809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken    private BroadcastInterceptingContext mServiceContext;
10940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private File mStatsDir;
11040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
11140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private INetworkManagementService mNetManager;
11240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private IAlarmManager mAlarmManager;
11340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private TrustedTime mTime;
11440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private NetworkStatsSettings mSettings;
11540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private IConnectivityManager mConnManager;
11640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
11740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private NetworkStatsService mService;
11809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken    private INetworkStatsSession mSession;
11940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    private INetworkManagementEventObserver mNetworkObserver;
12040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
12140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    @Override
12240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams    public void setUp() throws Exception {
12340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        super.setUp();
12440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
12540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mServiceContext = new BroadcastInterceptingContext(getContext());
12640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mStatsDir = getContext().getFilesDir();
12740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        if (mStatsDir.exists()) {
1287bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi            IoUtils.deleteContents(mStatsDir);
1297bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        }
1307bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1317bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mNetManager = createMock(INetworkManagementService.class);
1327bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mAlarmManager = createMock(IAlarmManager.class);
1337bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mTime = createMock(TrustedTime.class);
1347bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mSettings = createMock(NetworkStatsSettings.class);
1357bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mConnManager = createMock(IConnectivityManager.class);
1367bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1377bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mService = new NetworkStatsService(
1387bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi                mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
1397bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mService.bindConnectivityManager(mConnManager);
1407bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1417bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mElapsedRealtime = 0L;
1427bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1437bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectCurrentTime();
1447bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectDefaultSettings();
1457bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectNetworkStatsSummary(buildEmptyStats());
1467bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectNetworkStatsUidDetail(buildEmptyStats());
1477bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectSystemReady();
1487bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1497bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        // catch INetworkManagementEventObserver during systemReady()
1507bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
1517bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi                INetworkManagementEventObserver>();
1527bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mNetManager.registerObserver(capture(networkObserver));
1537bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        expectLastCall().atLeastOnce();
1547bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1557bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        replay();
1567bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mService.systemReady();
1577bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mSession = mService.openSession();
1587bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        verifyAndReset();
1597bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1607bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mNetworkObserver = networkObserver.getValue();
1617bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1627bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi    }
16340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
1647bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi    @Override
1657bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi    public void tearDown() throws Exception {
1667bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        IoUtils.deleteContents(mStatsDir);
1677bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1687bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mServiceContext = null;
1697bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mStatsDir = null;
1707bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1717bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mNetManager = null;
1727bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mAlarmManager = null;
1737bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mTime = null;
1747bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mSettings = null;
17539d72cd52c261ec6df86c2ef6a65c977c72738e0Michael Portuesi        mConnManager = null;
1767bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi
1777bf3f1f842b448cd4a422fa0d7e4469d50deb279Michael Portuesi        mSession.close();
17809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        mService = null;
17909e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
18009e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        super.tearDown();
18109e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken    }
18209e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
18309e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken    public void testNetworkStatsWifi() throws Exception {
18409e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        // pretend that wifi network comes online; service should ask about full
18509e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        // network state, and poll any existing interfaces before updating.
18609e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectCurrentTime();
18709e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectDefaultSettings();
18809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkState(buildWifiState());
18909e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsSummary(buildEmptyStats());
19009e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsUidDetail(buildEmptyStats());
19109e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsPoll();
19209e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
19309e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        replay();
19409e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
19509e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
19609e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        // verify service has empty history for wifi
19709e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
19809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        verifyAndReset();
19909e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
20009e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        // modify some number on wifi, and trigger poll event
20109e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        incrementCurrentTime(HOUR_IN_MILLIS);
20209e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectCurrentTime();
20309e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectDefaultSettings();
20409e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
20509e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken                .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
20609e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsUidDetail(buildEmptyStats());
20709e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        expectNetworkStatsPoll();
208bf6a1c2145b187c23b06243ceaa6f85c24498643Lenny Turetsky & Phil Plante
20909e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        replay();
21009e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
21109e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken
2127b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        // verify service recorded history
2137b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
2147b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        verifyAndReset();
2157b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia
2167b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        // and bump forward again, with counters going higher. this is
2177b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        // important, since polling should correctly subtract last snapshot.
2187b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        incrementCurrentTime(DAY_IN_MILLIS);
2197b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        expectCurrentTime();
2207b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        expectDefaultSettings();
2217b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
222605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis                .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
223605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis        expectNetworkStatsUidDetail(buildEmptyStats());
224605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis        expectNetworkStatsPoll();
225605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis
226605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis        replay();
227605fe2e0d79665282551b03424c4552bf9157060Savvas Dalkitsis        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
2287b5e903f71127867950ba87f363a491ba5e8306dDiego Garcia
22940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        // verify service recorded history
23040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
23140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        verifyAndReset();
23240c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
233f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi    }
234f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi
235f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi    public void testStatsRebootPersist() throws Exception {
236f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi        assertStatsFilesExist(false);
237f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi
23840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        // pretend that wifi network comes online; service should ask about full
23940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        // network state, and poll any existing interfaces before updating.
2403f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        expectCurrentTime();
24140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectDefaultSettings();
242b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi        expectNetworkState(buildWifiState());
24340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectNetworkStatsSummary(buildEmptyStats());
24440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectNetworkStatsUidDetail(buildEmptyStats());
2453f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        expectNetworkStatsPoll();
24640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
247b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi        replay();
24840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
24940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
2503f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        // verify service has empty history for wifi
25140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
252b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi        verifyAndReset();
25340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
25440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        // modify some number on wifi, and trigger poll event
2553f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        incrementCurrentTime(HOUR_IN_MILLIS);
25640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectCurrentTime();
257b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi        expectDefaultSettings();
25840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
25940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams                .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
2603f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
26140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
262b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
26340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
26440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
2653f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
26640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        expectNetworkStatsPoll();
267b574da97569d2d098d0aaebbb947bffa8514a4f7Michael Portuesi
26840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mService.setUidForeground(UID_RED, false);
26940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
2703f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        mService.setUidForeground(UID_RED, true);
27140c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
272f2ff212e318b6bd939734d35be9307d79300fd8dMichael Portuesi
27340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        replay();
27440c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
2753f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz
27640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams        // verify service recorded history
2773f9ddcc54cfbf81115466d6cab8a4287b2eae67fPhil Goodwin & Tyler Schultz        assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
27809e5078f4b638fae60c67b2e6b8f43cc796574a3Chris Van Vranken        assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
279        assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
280        assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
281        assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
282        verifyAndReset();
283
284        // graceful shutdown system, which should trigger persist of stats, and
285        // clear any values in memory.
286        expectCurrentTime();
287        expectDefaultSettings();
288        replay();
289        mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
290        verifyAndReset();
291
292        assertStatsFilesExist(true);
293
294        // boot through serviceReady() again
295        expectCurrentTime();
296        expectDefaultSettings();
297        expectNetworkStatsSummary(buildEmptyStats());
298        expectNetworkStatsUidDetail(buildEmptyStats());
299        expectSystemReady();
300
301        // catch INetworkManagementEventObserver during systemReady()
302        final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
303                INetworkManagementEventObserver>();
304        mNetManager.registerObserver(capture(networkObserver));
305        expectLastCall().atLeastOnce();
306
307        replay();
308        mService.systemReady();
309
310        mNetworkObserver = networkObserver.getValue();
311
312        // after systemReady(), we should have historical stats loaded again
313        assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
314        assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
315        assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
316        assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
317        assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
318        verifyAndReset();
319
320    }
321
322    // TODO: simulate reboot to test bucket resize
323    @Suppress
324    public void testStatsBucketResize() throws Exception {
325        NetworkStatsHistory history = null;
326
327        assertStatsFilesExist(false);
328
329        // pretend that wifi network comes online; service should ask about full
330        // network state, and poll any existing interfaces before updating.
331        expectCurrentTime();
332        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
333        expectNetworkState(buildWifiState());
334        expectNetworkStatsSummary(buildEmptyStats());
335        expectNetworkStatsUidDetail(buildEmptyStats());
336        expectNetworkStatsPoll();
337
338        replay();
339        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
340        verifyAndReset();
341
342        // modify some number on wifi, and trigger poll event
343        incrementCurrentTime(2 * HOUR_IN_MILLIS);
344        expectCurrentTime();
345        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
346        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
347                .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
348        expectNetworkStatsUidDetail(buildEmptyStats());
349        expectNetworkStatsPoll();
350
351        replay();
352        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
353
354        // verify service recorded history
355        history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
356        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
357        assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
358        assertEquals(2, history.size());
359        verifyAndReset();
360
361        // now change bucket duration setting and trigger another poll with
362        // exact same values, which should resize existing buckets.
363        expectCurrentTime();
364        expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
365        expectNetworkStatsSummary(buildEmptyStats());
366        expectNetworkStatsUidDetail(buildEmptyStats());
367        expectNetworkStatsPoll();
368
369        replay();
370        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
371
372        // verify identical stats, but spread across 4 buckets now
373        history = mSession.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
374        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
375        assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
376        assertEquals(4, history.size());
377        verifyAndReset();
378
379    }
380
381    public void testUidStatsAcrossNetworks() throws Exception {
382        // pretend first mobile network comes online
383        expectCurrentTime();
384        expectDefaultSettings();
385        expectNetworkState(buildMobile3gState(IMSI_1));
386        expectNetworkStatsSummary(buildEmptyStats());
387        expectNetworkStatsUidDetail(buildEmptyStats());
388        expectNetworkStatsPoll();
389
390        replay();
391        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
392        verifyAndReset();
393
394        // create some traffic on first network
395        incrementCurrentTime(HOUR_IN_MILLIS);
396        expectCurrentTime();
397        expectDefaultSettings();
398        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
399                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
400        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
401                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
402                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
403                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
404        expectNetworkStatsPoll();
405
406        mService.incrementOperationCount(UID_RED, 0xF00D, 10);
407
408        replay();
409        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
410
411        // verify service recorded history
412        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
413        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
414        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
415        assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
416        verifyAndReset();
417
418        // now switch networks; this also tests that we're okay with interfaces
419        // disappearing, to verify we don't count backwards.
420        incrementCurrentTime(HOUR_IN_MILLIS);
421        expectCurrentTime();
422        expectDefaultSettings();
423        expectNetworkState(buildMobile3gState(IMSI_2));
424        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
425                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
426        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
427                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
428                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
429                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
430        expectNetworkStatsPoll();
431
432        replay();
433        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
434        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
435        verifyAndReset();
436
437        // create traffic on second network
438        incrementCurrentTime(HOUR_IN_MILLIS);
439        expectCurrentTime();
440        expectDefaultSettings();
441        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
442                .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
443        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
444                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
445                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
446                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
447                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
448        expectNetworkStatsPoll();
449
450        mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
451
452        replay();
453        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
454
455        // verify original history still intact
456        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
457        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
458        assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
459
460        // and verify new history also recorded under different template, which
461        // verifies that we didn't cross the streams.
462        assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
463        assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
464        assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
465        verifyAndReset();
466
467    }
468
469    public void testUidRemovedIsMoved() throws Exception {
470        // pretend that network comes online
471        expectCurrentTime();
472        expectDefaultSettings();
473        expectNetworkState(buildWifiState());
474        expectNetworkStatsSummary(buildEmptyStats());
475        expectNetworkStatsUidDetail(buildEmptyStats());
476        expectNetworkStatsPoll();
477
478        replay();
479        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
480        verifyAndReset();
481
482        // create some traffic
483        incrementCurrentTime(HOUR_IN_MILLIS);
484        expectCurrentTime();
485        expectDefaultSettings();
486        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
487                .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
488        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
489                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
490                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
491                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
492                .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
493        expectNetworkStatsPoll();
494
495        mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
496
497        replay();
498        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
499
500        // verify service recorded history
501        assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
502        assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
503        assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
504        assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
505        verifyAndReset();
506
507        // now pretend two UIDs are uninstalled, which should migrate stats to
508        // special "removed" bucket.
509        expectCurrentTime();
510        expectDefaultSettings();
511        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
512                .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
513        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
514                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
515                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
516                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
517                .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
518        expectNetworkStatsPoll();
519
520        replay();
521        final Intent intent = new Intent(ACTION_UID_REMOVED);
522        intent.putExtra(EXTRA_UID, UID_BLUE);
523        mServiceContext.sendBroadcast(intent);
524        intent.putExtra(EXTRA_UID, UID_RED);
525        mServiceContext.sendBroadcast(intent);
526
527        // existing uid and total should remain unchanged; but removed UID
528        // should be gone completely.
529        assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
530        assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
531        assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
532        assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
533        assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
534        verifyAndReset();
535
536    }
537
538    public void testUid3g4gCombinedByTemplate() throws Exception {
539        // pretend that network comes online
540        expectCurrentTime();
541        expectDefaultSettings();
542        expectNetworkState(buildMobile3gState(IMSI_1));
543        expectNetworkStatsSummary(buildEmptyStats());
544        expectNetworkStatsUidDetail(buildEmptyStats());
545        expectNetworkStatsPoll();
546
547        replay();
548        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
549        verifyAndReset();
550
551        // create some traffic
552        incrementCurrentTime(HOUR_IN_MILLIS);
553        expectCurrentTime();
554        expectDefaultSettings();
555        expectNetworkStatsSummary(buildEmptyStats());
556        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
557                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
558                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
559        expectNetworkStatsPoll();
560
561        mService.incrementOperationCount(UID_RED, 0xF00D, 5);
562
563        replay();
564        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
565
566        // verify service recorded history
567        assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
568        verifyAndReset();
569
570        // now switch over to 4g network
571        incrementCurrentTime(HOUR_IN_MILLIS);
572        expectCurrentTime();
573        expectDefaultSettings();
574        expectNetworkState(buildMobile4gState(TEST_IFACE2));
575        expectNetworkStatsSummary(buildEmptyStats());
576        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
577                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
578                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
579        expectNetworkStatsPoll();
580
581        replay();
582        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
583        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
584        verifyAndReset();
585
586        // create traffic on second network
587        incrementCurrentTime(HOUR_IN_MILLIS);
588        expectCurrentTime();
589        expectDefaultSettings();
590        expectNetworkStatsSummary(buildEmptyStats());
591        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
592                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
593                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
594                .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
595                .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
596        expectNetworkStatsPoll();
597
598        mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
599
600        replay();
601        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
602
603        // verify that ALL_MOBILE template combines both
604        assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
605
606        verifyAndReset();
607    }
608
609    public void testSummaryForAllUid() throws Exception {
610        // pretend that network comes online
611        expectCurrentTime();
612        expectDefaultSettings();
613        expectNetworkState(buildWifiState());
614        expectNetworkStatsSummary(buildEmptyStats());
615        expectNetworkStatsUidDetail(buildEmptyStats());
616        expectNetworkStatsPoll();
617
618        replay();
619        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
620        verifyAndReset();
621
622        // create some traffic for two apps
623        incrementCurrentTime(HOUR_IN_MILLIS);
624        expectCurrentTime();
625        expectDefaultSettings();
626        expectNetworkStatsSummary(buildEmptyStats());
627        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
628                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
629                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
630                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
631        expectNetworkStatsPoll();
632
633        mService.incrementOperationCount(UID_RED, 0xF00D, 1);
634
635        replay();
636        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
637
638        // verify service recorded history
639        assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
640        assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
641        verifyAndReset();
642
643        // now create more traffic in next hour, but only for one app
644        incrementCurrentTime(HOUR_IN_MILLIS);
645        expectCurrentTime();
646        expectDefaultSettings();
647        expectNetworkStatsSummary(buildEmptyStats());
648        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
649                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
650                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
651                .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
652        expectNetworkStatsPoll();
653
654        replay();
655        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
656
657        // first verify entire history present
658        NetworkStats stats = mSession.getSummaryForAllUid(
659                sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
660        assertEquals(3, stats.size());
661        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
662        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
663        assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
664
665        // now verify that recent history only contains one uid
666        final long currentTime = currentTimeMillis();
667        stats = mSession.getSummaryForAllUid(
668                sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
669        assertEquals(1, stats.size());
670        assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
671
672        verifyAndReset();
673    }
674
675    public void testForegroundBackground() throws Exception {
676        // pretend that network comes online
677        expectCurrentTime();
678        expectDefaultSettings();
679        expectNetworkState(buildWifiState());
680        expectNetworkStatsSummary(buildEmptyStats());
681        expectNetworkStatsUidDetail(buildEmptyStats());
682        expectNetworkStatsPoll();
683
684        replay();
685        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
686        verifyAndReset();
687
688        // create some initial traffic
689        incrementCurrentTime(HOUR_IN_MILLIS);
690        expectCurrentTime();
691        expectDefaultSettings();
692        expectNetworkStatsSummary(buildEmptyStats());
693        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
694                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
695                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
696        expectNetworkStatsPoll();
697
698        mService.incrementOperationCount(UID_RED, 0xF00D, 1);
699
700        replay();
701        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
702
703        // verify service recorded history
704        assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
705        verifyAndReset();
706
707        // now switch to foreground
708        incrementCurrentTime(HOUR_IN_MILLIS);
709        expectCurrentTime();
710        expectDefaultSettings();
711        expectNetworkStatsSummary(buildEmptyStats());
712        expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
713                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
714                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
715                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
716                .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
717        expectNetworkStatsPoll();
718
719        mService.setUidForeground(UID_RED, true);
720        mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
721
722        replay();
723        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
724
725        // test that we combined correctly
726        assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
727
728        // verify entire history present
729        final NetworkStats stats = mSession.getSummaryForAllUid(
730                sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
731        assertEquals(4, stats.size());
732        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
733        assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
734        assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
735        assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
736
737        verifyAndReset();
738    }
739
740    public void testTethering() throws Exception {
741        // pretend first mobile network comes online
742        expectCurrentTime();
743        expectDefaultSettings();
744        expectNetworkState(buildMobile3gState(IMSI_1));
745        expectNetworkStatsSummary(buildEmptyStats());
746        expectNetworkStatsUidDetail(buildEmptyStats());
747        expectNetworkStatsPoll();
748
749        replay();
750        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
751        verifyAndReset();
752
753        // create some tethering traffic
754        incrementCurrentTime(HOUR_IN_MILLIS);
755        expectCurrentTime();
756        expectDefaultSettings();
757        expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
758                .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
759
760        final NetworkStats uidStats = new NetworkStats(getElapsedRealtime(), 1)
761                .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L);
762        final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
763        final NetworkStats tetherStats = new NetworkStats(getElapsedRealtime(), 1)
764                .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L);
765
766        expectNetworkStatsUidDetail(uidStats, tetherIfacePairs, tetherStats);
767        expectNetworkStatsPoll();
768
769        replay();
770        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
771
772        // verify service recorded history
773        assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
774        assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
775        assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
776        verifyAndReset();
777
778    }
779
780    public void testReportXtOverDev() throws Exception {
781        // bring mobile network online
782        expectCurrentTime();
783        expectDefaultSettings();
784        expectNetworkState(buildMobile3gState(IMSI_1));
785        expectNetworkStatsSummary(buildEmptyStats());
786        expectNetworkStatsUidDetail(buildEmptyStats());
787        expectNetworkStatsPoll();
788
789        replay();
790        mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
791        verifyAndReset();
792
793        // create some traffic, but only for DEV, and across 1.5 buckets
794        incrementCurrentTime(90 * MINUTE_IN_MILLIS);
795        expectCurrentTime();
796        expectDefaultSettings();
797        expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
798                .addIfaceValues(TEST_IFACE, 6000L, 60L, 3000L, 30L));
799        expectNetworkStatsSummaryXt(buildEmptyStats());
800        expectNetworkStatsUidDetail(buildEmptyStats());
801        expectNetworkStatsPoll();
802
803        replay();
804        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
805
806        // verify service recorded history:
807        // 4000(dev) + 2000(dev)
808        assertNetworkTotal(sTemplateImsi1, 6000L, 60L, 3000L, 30L, 0);
809        verifyAndReset();
810
811        // create traffic on both DEV and XT, across two buckets
812        incrementCurrentTime(2 * HOUR_IN_MILLIS);
813        expectCurrentTime();
814        expectDefaultSettings();
815        expectNetworkStatsSummaryDev(new NetworkStats(getElapsedRealtime(), 1)
816                .addIfaceValues(TEST_IFACE, 6004L, 64L, 3004L, 34L));
817        expectNetworkStatsSummaryXt(new NetworkStats(getElapsedRealtime(), 1)
818                .addIfaceValues(TEST_IFACE, 10240L, 0L, 0L, 0L));
819        expectNetworkStatsUidDetail(buildEmptyStats());
820        expectNetworkStatsPoll();
821
822        replay();
823        mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
824
825        // verify that we switching reporting at the first atomic XT bucket,
826        // which should give us:
827        // 4000(dev) + 2000(dev) + 1(dev) + 5120(xt) + 2560(xt)
828        assertNetworkTotal(sTemplateImsi1, 13681L, 61L, 3001L, 31L, 0);
829
830        // also test pure-DEV and pure-XT ranges
831        assertNetworkTotal(sTemplateImsi1, startTimeMillis(),
832                startTimeMillis() + 2 * HOUR_IN_MILLIS, 6001L, 61L, 3001L, 31L, 0);
833        assertNetworkTotal(sTemplateImsi1, startTimeMillis() + 2 * HOUR_IN_MILLIS,
834                startTimeMillis() + 4 * HOUR_IN_MILLIS, 7680L, 0L, 0L, 0L, 0);
835
836        verifyAndReset();
837    }
838
839    private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
840            long txBytes, long txPackets, int operations) throws Exception {
841        assertNetworkTotal(template, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
842                txPackets, operations);
843    }
844
845    private void assertNetworkTotal(NetworkTemplate template, long start, long end, long rxBytes,
846            long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
847        // verify history API
848        final NetworkStatsHistory history = mSession.getHistoryForNetwork(template, FIELD_ALL);
849        assertValues(history, start, end, rxBytes, rxPackets, txBytes, txPackets, operations);
850
851        // verify summary API
852        final NetworkStats stats = mSession.getSummaryForNetwork(template, start, end);
853        assertValues(stats, IFACE_ALL, UID_ALL, SET_DEFAULT, TAG_NONE, rxBytes, rxPackets, txBytes,
854                txPackets, operations);
855    }
856
857    private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
858            long txBytes, long txPackets, int operations) throws Exception {
859        assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
860    }
861
862    private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
863            long rxPackets, long txBytes, long txPackets, int operations) throws Exception {
864        // verify history API
865        final NetworkStatsHistory history = mSession.getHistoryForUid(
866                template, uid, set, TAG_NONE, FIELD_ALL);
867        assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
868                txPackets, operations);
869
870        // verify summary API
871        final NetworkStats stats = mSession.getSummaryForAllUid(
872                template, Long.MIN_VALUE, Long.MAX_VALUE, false);
873        assertValues(stats, IFACE_ALL, uid, set, TAG_NONE, rxBytes, rxPackets, txBytes, txPackets,
874                operations);
875    }
876
877    private void expectSystemReady() throws Exception {
878        mAlarmManager.remove(isA(PendingIntent.class));
879        expectLastCall().anyTimes();
880
881        mAlarmManager.set(eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), anyLong(), isA(PendingIntent.class));
882        expectLastCall().atLeastOnce();
883
884        mNetManager.setGlobalAlert(anyLong());
885        expectLastCall().atLeastOnce();
886
887        expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
888    }
889
890    private void expectNetworkState(NetworkState... state) throws Exception {
891        expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
892
893        final LinkProperties linkProp = state.length > 0 ? state[0].linkProperties : null;
894        expect(mConnManager.getActiveLinkProperties()).andReturn(linkProp).atLeastOnce();
895    }
896
897    private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
898        expectNetworkStatsSummaryDev(summary);
899        expectNetworkStatsSummaryXt(summary);
900    }
901
902    private void expectNetworkStatsSummaryDev(NetworkStats summary) throws Exception {
903        expect(mNetManager.getNetworkStatsSummaryDev()).andReturn(summary).atLeastOnce();
904    }
905
906    private void expectNetworkStatsSummaryXt(NetworkStats summary) throws Exception {
907        expect(mNetManager.getNetworkStatsSummaryXt()).andReturn(summary).atLeastOnce();
908    }
909
910    private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
911        expectNetworkStatsUidDetail(detail, new String[0], new NetworkStats(0L, 0));
912    }
913
914    private void expectNetworkStatsUidDetail(
915            NetworkStats detail, String[] tetherIfacePairs, NetworkStats tetherStats)
916            throws Exception {
917        expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
918
919        // also include tethering details, since they are folded into UID
920        expect(mConnManager.getTetheredIfacePairs()).andReturn(tetherIfacePairs).atLeastOnce();
921        expect(mNetManager.getNetworkStatsTethering(aryEq(tetherIfacePairs)))
922                .andReturn(tetherStats).atLeastOnce();
923    }
924
925    private void expectDefaultSettings() throws Exception {
926        expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
927    }
928
929    private void expectSettings(long persistBytes, long bucketDuration, long deleteAge)
930            throws Exception {
931        expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
932        expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
933        expect(mSettings.getSampleEnabled()).andReturn(true).anyTimes();
934        expect(mSettings.getReportXtOverDev()).andReturn(true).anyTimes();
935
936        final Config config = new Config(bucketDuration, deleteAge, deleteAge);
937        expect(mSettings.getDevConfig()).andReturn(config).anyTimes();
938        expect(mSettings.getXtConfig()).andReturn(config).anyTimes();
939        expect(mSettings.getUidConfig()).andReturn(config).anyTimes();
940        expect(mSettings.getUidTagConfig()).andReturn(config).anyTimes();
941
942        expect(mSettings.getGlobalAlertBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
943        expect(mSettings.getDevPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
944        expect(mSettings.getXtPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
945        expect(mSettings.getUidPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
946        expect(mSettings.getUidTagPersistBytes(anyLong())).andReturn(MB_IN_BYTES).anyTimes();
947    }
948
949    private void expectCurrentTime() throws Exception {
950        expect(mTime.forceRefresh()).andReturn(false).anyTimes();
951        expect(mTime.hasCache()).andReturn(true).anyTimes();
952        expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
953        expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
954        expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
955    }
956
957    private void expectNetworkStatsPoll() throws Exception {
958        mNetManager.setGlobalAlert(anyLong());
959        expectLastCall().anyTimes();
960    }
961
962    private void assertStatsFilesExist(boolean exist) {
963        final File basePath = new File(mStatsDir, "netstats");
964        if (exist) {
965            assertTrue(basePath.list().length > 0);
966        } else {
967            assertTrue(basePath.list().length == 0);
968        }
969    }
970
971    private static void assertValues(NetworkStats stats, String iface, int uid, int set,
972            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
973        final NetworkStats.Entry entry = new NetworkStats.Entry();
974        if (set == SET_DEFAULT || set == SET_ALL) {
975            final int i = stats.findIndex(iface, uid, SET_DEFAULT, tag);
976            if (i != -1) {
977                entry.add(stats.getValues(i, null));
978            }
979        }
980        if (set == SET_FOREGROUND || set == SET_ALL) {
981            final int i = stats.findIndex(iface, uid, SET_FOREGROUND, tag);
982            if (i != -1) {
983                entry.add(stats.getValues(i, null));
984            }
985        }
986
987        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
988        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
989        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
990        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
991        assertEquals("unexpected operations", operations, entry.operations);
992    }
993
994    private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
995            long rxPackets, long txBytes, long txPackets, int operations) {
996        final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
997        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
998        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
999        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
1000        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
1001        assertEquals("unexpected operations", operations, entry.operations);
1002    }
1003
1004    private static NetworkState buildWifiState() {
1005        final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
1006        info.setDetailedState(DetailedState.CONNECTED, null, null);
1007        final LinkProperties prop = new LinkProperties();
1008        prop.setInterfaceName(TEST_IFACE);
1009        return new NetworkState(info, prop, null, null, TEST_SSID);
1010    }
1011
1012    private static NetworkState buildMobile3gState(String subscriberId) {
1013        final NetworkInfo info = new NetworkInfo(
1014                TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
1015        info.setDetailedState(DetailedState.CONNECTED, null, null);
1016        final LinkProperties prop = new LinkProperties();
1017        prop.setInterfaceName(TEST_IFACE);
1018        return new NetworkState(info, prop, null, subscriberId, null);
1019    }
1020
1021    private static NetworkState buildMobile4gState(String iface) {
1022        final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
1023        info.setDetailedState(DetailedState.CONNECTED, null, null);
1024        final LinkProperties prop = new LinkProperties();
1025        prop.setInterfaceName(iface);
1026        return new NetworkState(info, prop, null);
1027    }
1028
1029    private NetworkStats buildEmptyStats() {
1030        return new NetworkStats(getElapsedRealtime(), 0);
1031    }
1032
1033    private long getElapsedRealtime() {
1034        return mElapsedRealtime;
1035    }
1036
1037    private long startTimeMillis() {
1038        return TEST_START;
1039    }
1040
1041    private long currentTimeMillis() {
1042        return startTimeMillis() + mElapsedRealtime;
1043    }
1044
1045    private void incrementCurrentTime(long duration) {
1046        mElapsedRealtime += duration;
1047    }
1048
1049    private void replay() {
1050        EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1051    }
1052
1053    private void verifyAndReset() {
1054        EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1055        EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
1056    }
1057}
1058