Search in sources :

Example 36 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsServiceTest method testUidStatsAcrossNetworks.

@Test
public void testUidStatsAcrossNetworks() throws Exception {
    // pretend first mobile network comes online
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_1));
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectBandwidthControlCheck();
    mService.forceUpdateIfaces();
    // create some traffic on first network
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
    mService.incrementOperationCount(UID_RED, 0xF00D, 10);
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
    assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
    // now switch networks; this also tests that we're okay with interfaces
    // disappearing, to verify we don't count backwards.
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildMobile3gState(IMSI_2));
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
    expectBandwidthControlCheck();
    mService.forceUpdateIfaces();
    forcePollAndWaitForIdle();
    // create traffic on second network
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
    mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
    forcePollAndWaitForIdle();
    // verify original history still intact
    assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
    assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
    assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
    // and verify new history also recorded under different template, which
    // verifies that we didn't cross the streams.
    assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
}
Also used : NetworkStats(android.net.NetworkStats) Test(org.junit.Test)

Example 37 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsObserversTest method testUpdateStats_userAccess_usageAnotherUser_doesNotNotify.

public void testUpdateStats_userAccess_usageAnotherUser_doesNotNotify() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_RED, NetworkStatsAccess.Level.USER);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveUidIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = null;
    NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2).addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2).addValues(TEST_IFACE, UID_ANOTHER_USER, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(INVALID_TYPE, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 38 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsObserversTest method testUpdateStats_deviceAccess_notifies.

public void testUpdateStats_deviceAccess_notifies() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, Process.SYSTEM_UID, NetworkStatsAccess.Level.DEVICE);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = new NetworkStats(TEST_START, 1).addIfaceValues(TEST_IFACE, BASE_BYTES, 8L, BASE_BYTES, 16L);
    NetworkStats uidSnapshot = null;
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    xtSnapshot = new NetworkStats(TEST_START + MINUTE_IN_MILLIS, 1).addIfaceValues(TEST_IFACE, BASE_BYTES + THRESHOLD_BYTES, 12L, BASE_BYTES + THRESHOLD_BYTES, 22L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 39 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsObserversTest method testUpdateStats_userAccess_usageSameUser_notifies.

public void testUpdateStats_userAccess_usageSameUser_notifies() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_BLUE, NetworkStatsAccess.Level.USER);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveUidIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = null;
    NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 40 with NetworkStats

use of android.net.NetworkStats in project platform_frameworks_base by android.

the class NetworkStatsObserversTest method testUpdateStats_defaultAccess_notifiesSameUid.

public void testUpdateStats_defaultAccess_notifiesSameUid() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_RED, NetworkStatsAccess.Level.DEFAULT);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateImsi1, request.template));
    assertEquals(THRESHOLD_BYTES, request.thresholdInBytes);
    NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, IMSI_1, null, /* networkId */
    false, /* roaming */
    true));
    mActiveUidIfaces.put(TEST_IFACE, identSet);
    // Baseline
    NetworkStats xtSnapshot = null;
    NetworkStats uidSnapshot = new NetworkStats(TEST_START, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES, 2L, BASE_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    // Delta
    uidSnapshot = new NetworkStats(TEST_START + 2 * MINUTE_IN_MILLIS, 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, BASE_BYTES + THRESHOLD_BYTES, 2L, BASE_BYTES + THRESHOLD_BYTES, 2L, 0L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Aggregations

NetworkStats (android.net.NetworkStats)271 File (java.io.File)49 DataUsageRequest (android.net.DataUsageRequest)32 Intent (android.content.Intent)29 NetworkIdentity (android.net.NetworkIdentity)28 NetworkStatsHistory (android.net.NetworkStatsHistory)21 Bundle (android.os.Bundle)18 StrictMode (android.os.StrictMode)18 ProcFileReader (com.android.internal.util.ProcFileReader)18 FileInputStream (java.io.FileInputStream)18 ProtocolException (java.net.ProtocolException)18 PendingIntent (android.app.PendingIntent)17 IOException (java.io.IOException)17 NetworkPolicy (android.net.NetworkPolicy)15 NetworkState (android.net.NetworkState)15 Test (org.junit.Test)14 Suppress (android.test.suitebuilder.annotation.Suppress)13 VpnInfo (com.android.internal.net.VpnInfo)10 RemoteException (android.os.RemoteException)8 NetworkTemplate (android.net.NetworkTemplate)6