Search in sources :

Example 21 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.

the class NetworkPolicyManagerServiceTest method testNetworkPolicyAppliedCycleLastMonth.

@Suppress
public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
    NetworkState[] state = null;
    NetworkStats stats = null;
    Future<Void> future;
    final long TIME_FEB_15 = 1171497600000L;
    final long TIME_MAR_10 = 1173484800000L;
    final int CYCLE_DAY = 15;
    setCurrentTimeMillis(TIME_MAR_10);
    // first, pretend that wifi network comes online. no policy active,
    // which means we shouldn't push limit to interface.
    state = new NetworkState[] { buildWifi() };
    expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
    expectCurrentTime();
    expectClearNotifications();
    expectAdvisePersistThreshold();
    future = expectMeteredIfacesChanged();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
    future.get();
    verifyAndReset();
    // now change cycle to be on 15th, and test in early march, to verify we
    // pick cycle day in previous month.
    expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
    expectCurrentTime();
    // pretend that 512 bytes total have happened
    stats = new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
    expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)).andReturn(stats.getTotalBytes()).atLeastOnce();
    expectPolicyDataEnable(TYPE_WIFI, true);
    // TODO: consider making strongly ordered mock
    expectRemoveInterfaceQuota(TEST_IFACE);
    expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
    expectClearNotifications();
    expectAdvisePersistThreshold();
    future = expectMeteredIfacesChanged(TEST_IFACE);
    replay();
    setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
    future.get();
    verifyAndReset();
}
Also used : NetworkPolicy(android.net.NetworkPolicy) NetworkStats(android.net.NetworkStats) Intent(android.content.Intent) NetworkState(android.net.NetworkState) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 22 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.

the class NetworkPolicyManagerServiceTest method testNextCycleTimezoneAfterUtc.

public void testNextCycleTimezoneAfterUtc() throws Exception {
    // US/Central is UTC-6
    final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 10, "US/Central", 1024L, 1024L, false);
    assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"), computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 23 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.

the class NetworkPolicyManagerServiceTest method testNextCycleSane.

public void testNextCycleSane() throws Exception {
    final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
    final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
    // walk forwards, ensuring that cycle boundaries don't get stuck
    long currentCycle = computeNextCycleBoundary(parseTime("2011-08-01T00:00:00.000Z"), policy);
    for (int i = 0; i < 128; i++) {
        long nextCycle = computeNextCycleBoundary(currentCycle, policy);
        assertEqualsFuzzy(DAY_IN_MILLIS * 30, nextCycle - currentCycle, DAY_IN_MILLIS * 3);
        assertUnique(seen, nextCycle);
        currentCycle = nextCycle;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NetworkPolicy(android.net.NetworkPolicy) EasyMock.anyLong(org.easymock.EasyMock.anyLong)

Example 24 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.

the class NetworkPolicyManagerServiceTest method testNextCycleTimezoneBeforeUtc.

public void testNextCycleTimezoneBeforeUtc() throws Exception {
    // Israel is UTC+2
    final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
    assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"), computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
}
Also used : NetworkPolicy(android.net.NetworkPolicy)

Example 25 with NetworkPolicy

use of android.net.NetworkPolicy in project android_frameworks_base by ResurrectionRemix.

the class SettingsBackupAgent method restoreNetworkPolicies.

private void restoreNetworkPolicies(byte[] data) {
    NetworkPolicyManager networkPolicyManager = (NetworkPolicyManager) getSystemService(NETWORK_POLICY_SERVICE);
    if (data != null && data.length != 0) {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
        try {
            int version = in.readInt();
            if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
                throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
            }
            int length = in.readInt();
            NetworkPolicy[] policies = new NetworkPolicy[length];
            for (int i = 0; i < length; i++) {
                byte isNull = in.readByte();
                if (isNull == BackupUtils.NULL)
                    continue;
                int byteLength = in.readInt();
                byte[] policyData = new byte[byteLength];
                in.read(policyData, 0, byteLength);
                policies[i] = NetworkPolicy.getNetworkPolicyFromBackup(new DataInputStream(new ByteArrayInputStream(policyData)));
            }
            // Only set the policies if there was no error in the restore operation
            networkPolicyManager.setNetworkPolicies(policies);
        } catch (NullPointerException | IOException | BackupUtils.BadVersionException e) {
            // NPE can be thrown when trying to instantiate a NetworkPolicy
            Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NetworkPolicy(android.net.NetworkPolicy) NetworkPolicyManager(android.net.NetworkPolicyManager) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Aggregations

NetworkPolicy (android.net.NetworkPolicy)275 Test (org.junit.Test)57 NetworkTemplate (android.net.NetworkTemplate)40 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)30 NetworkIdentity (android.net.NetworkIdentity)24 IOException (java.io.IOException)22 NetworkState (android.net.NetworkState)21 Time (android.text.format.Time)16 NetworkStats (android.net.NetworkStats)15 RemoteException (android.os.RemoteException)11 Intent (android.content.Intent)10 NetworkPolicyManager (android.net.NetworkPolicyManager)10 WifiConfiguration (android.net.wifi.WifiConfiguration)10 ArraySet (android.util.ArraySet)10 PrintWriter (java.io.PrintWriter)10 ArrayList (java.util.ArrayList)10 LinkedHashSet (java.util.LinkedHashSet)10 Suppress (android.test.suitebuilder.annotation.Suppress)9 View (android.view.View)8 AdapterView (android.widget.AdapterView)8