Search in sources :

Example 76 with NetworkIdentity

use of android.net.NetworkIdentity in project android_frameworks_base by crdroidandroid.

the class NetworkStatsCollectionTest method testAccessLevels.

public void testAccessLevels() throws Exception {
    final NetworkStatsCollection collection = new NetworkStatsCollection(HOUR_IN_MILLIS);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    final NetworkIdentitySet identSet = new NetworkIdentitySet();
    identSet.add(new NetworkIdentity(TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, TEST_IMSI, null, false, true));
    int myUid = Process.myUid();
    int otherUidInSameUser = Process.myUid() + 1;
    int uidInDifferentUser = Process.myUid() + UserHandle.PER_USER_RANGE;
    // Record one entry for the current UID.
    entry.rxBytes = 32;
    collection.recordData(identSet, myUid, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for another UID in this user.
    entry.rxBytes = 64;
    collection.recordData(identSet, otherUidInSameUser, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for the system UID.
    entry.rxBytes = 128;
    collection.recordData(identSet, Process.SYSTEM_UID, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Record one entry for a UID in a different user.
    entry.rxBytes = 256;
    collection.recordData(identSet, uidInDifferentUser, SET_DEFAULT, TAG_NONE, 0, 60 * MINUTE_IN_MILLIS, entry);
    // Verify the set of relevant UIDs for each access level.
    MoreAsserts.assertEquals(new int[] { myUid }, collection.getRelevantUids(NetworkStatsAccess.Level.DEFAULT));
    MoreAsserts.assertEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser }, collection.getRelevantUids(NetworkStatsAccess.Level.USER));
    MoreAsserts.assertEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser, uidInDifferentUser }, collection.getRelevantUids(NetworkStatsAccess.Level.DEVICE));
    // Verify security check in getHistory.
    assertNotNull(collection.getHistory(buildTemplateMobileAll(TEST_IMSI), myUid, SET_DEFAULT, TAG_NONE, 0, NetworkStatsAccess.Level.DEFAULT));
    try {
        collection.getHistory(buildTemplateMobileAll(TEST_IMSI), otherUidInSameUser, SET_DEFAULT, TAG_NONE, 0, NetworkStatsAccess.Level.DEFAULT);
        fail("Should have thrown SecurityException for accessing different UID");
    } catch (SecurityException e) {
    // expected
    }
    // Verify appropriate aggregation in getSummary.
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32, 0, 0, 0, NetworkStatsAccess.Level.DEFAULT);
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32 + 64 + 128, 0, 0, 0, NetworkStatsAccess.Level.USER);
    assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 32 + 64 + 128 + 256, 0, 0, 0, NetworkStatsAccess.Level.DEVICE);
}
Also used : NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 77 with NetworkIdentity

use of android.net.NetworkIdentity in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_initialSample_doesNotNotify.

public void testUpdateStats_initialSample_doesNotNotify() 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);
    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 78 with NetworkIdentity

use of android.net.NetworkIdentity in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_belowThreshold_doesNotNotify.

public void testUpdateStats_belowThreshold_doesNotNotify() 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, 1).addIfaceValues(TEST_IFACE, BASE_BYTES + 1024L, 10L, BASE_BYTES + 2048L, 20L);
    mStatsObservers.updateStats(xtSnapshot, uidSnapshot, mActiveIfaces, mActiveUidIfaces, VPN_INFO, TEST_START);
    waitForObserverToIdle();
    assertTrue(mCv.block(WAIT_TIMEOUT));
    mCv.block(WAIT_TIMEOUT);
    assertEquals(INVALID_TYPE, mHandler.mLastMessageType);
}
Also used : DataUsageRequest(android.net.DataUsageRequest) NetworkIdentity(android.net.NetworkIdentity) NetworkStats(android.net.NetworkStats)

Example 79 with NetworkIdentity

use of android.net.NetworkIdentity in project android_frameworks_base by crdroidandroid.

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 80 with NetworkIdentity

use of android.net.NetworkIdentity in project android_frameworks_base by crdroidandroid.

the class NetworkStatsObserversTest method testUpdateStats_defaultAccess_usageOtherUid_doesNotNotify.

public void testUpdateStats_defaultAccess_usageOtherUid_doesNotNotify() throws Exception {
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateImsi1, THRESHOLD_BYTES);
    DataUsageRequest request = mStatsObservers.register(inputRequest, mMessenger, mockBinder, UID_BLUE, 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, 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, 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)

Aggregations

NetworkIdentity (android.net.NetworkIdentity)82 NetworkStats (android.net.NetworkStats)32 DataUsageRequest (android.net.DataUsageRequest)28 NetworkPolicy (android.net.NetworkPolicy)24 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)20 NetworkState (android.net.NetworkState)11 RemoteException (android.os.RemoteException)11 TelephonyManager (android.telephony.TelephonyManager)11 LinkProperties (android.net.LinkProperties)10 SubscriptionManager (android.telephony.SubscriptionManager)10 ArraySet (android.util.ArraySet)10 NetworkQuotaInfo (android.net.NetworkQuotaInfo)6 NetworkTemplate (android.net.NetworkTemplate)6 Time (android.text.format.Time)6 NtpTrustedTime (android.util.NtpTrustedTime)6 TrustedTime (android.util.TrustedTime)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)1 Map (java.util.Map)1