Search in sources :

Example 1 with INetworkManagementEventObserver

use of android.net.INetworkManagementEventObserver in project android_frameworks_base by ParanoidAndroid.

the class NetworkStatsServiceTest method testStatsRebootPersist.

public void testStatsRebootPersist() throws Exception {
    assertStatsFilesExist(false);
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    verifyAndReset();
    // modify some number on wifi, and trigger poll event
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
    expectNetworkStatsPoll();
    mService.setUidForeground(UID_RED, false);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
    mService.setUidForeground(UID_RED, true);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
    replay();
    mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
    // graceful shutdown system, which should trigger persist of stats, and
    // clear any values in memory.
    expectCurrentTime();
    expectDefaultSettings();
    replay();
    mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
    verifyAndReset();
    assertStatsFilesExist(true);
    // boot through serviceReady() again
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    replay();
    mService.systemReady();
    mNetworkObserver = networkObserver.getValue();
    // after systemReady(), we should have historical stats loaded again
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Capture(org.easymock.Capture)

Example 2 with INetworkManagementEventObserver

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

the class NetworkPolicyManagerServiceTest method callSystemReady.

@Before
public void callSystemReady() throws Exception {
    MockitoAnnotations.initMocks(this);
    final Context context = InstrumentationRegistry.getContext();
    setCurrentTimeMillis(TEST_START);
    // intercept various broadcasts, and pretend that uids have packages
    mServiceContext = new BroadcastInterceptingContext(context) {

        @Override
        public PackageManager getPackageManager() {
            return mPackageManager;
        }

        @Override
        public void startActivity(Intent intent) {
        // ignored
        }
    };
    mPolicyDir = context.getFilesDir();
    if (mPolicyDir.exists()) {
        IoUtils.deleteContents(mPolicyDir);
    }
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            mUidObserver = (IUidObserver) invocation.getArguments()[0];
            Log.d(TAG, "set mUidObserver to " + mUidObserver);
            return null;
        }
    }).when(mActivityManager).registerUidObserver(any(), anyInt());
    mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
    mService.bindConnectivityManager(mConnManager);
    mService.bindNotificationManager(mNotifManager);
    mPolicyListener = new NetworkPolicyListenerAnswer(mService);
    // Sets some common expectations.
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenAnswer(new Answer<PackageInfo>() {

        @Override
        public PackageInfo answer(InvocationOnMock invocation) throws Throwable {
            final String packageName = (String) invocation.getArguments()[0];
            final PackageInfo info = new PackageInfo();
            final Signature signature;
            if ("android".equals(packageName)) {
                signature = new Signature("F00D");
            } else {
                signature = new Signature("DEAD");
            }
            info.signatures = new Signature[] { signature };
            return info;
        }
    });
    when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn(new ApplicationInfo());
    when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] { PKG_NAME_A });
    when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true);
    expectCurrentTime();
    // Prepare NPMS.
    mService.systemReady();
    // catch INetworkManagementEventObserver during systemReady()
    ArgumentCaptor<INetworkManagementEventObserver> networkObserver = ArgumentCaptor.forClass(INetworkManagementEventObserver.class);
    verify(mNetworkManager).registerObserver(networkObserver.capture());
    mNetworkObserver = networkObserver.getValue();
}
Also used : BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) BroadcastInterceptingContext(com.android.internal.util.test.BroadcastInterceptingContext) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) IUidObserver(android.app.IUidObserver) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Signature(android.content.pm.Signature) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) Before(org.junit.Before)

Example 3 with INetworkManagementEventObserver

use of android.net.INetworkManagementEventObserver in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    mServiceContext = new BroadcastInterceptingContext(getContext());
    mStatsDir = getContext().getFilesDir();
    if (mStatsDir.exists()) {
        IoUtils.deleteContents(mStatsDir);
    }
    mNetManager = createMock(INetworkManagementService.class);
    // TODO: Mock AlarmManager when migrating this test to Mockito.
    AlarmManager alarmManager = (AlarmManager) mServiceContext.getSystemService(Context.ALARM_SERVICE);
    mTime = createMock(TrustedTime.class);
    mSettings = createMock(NetworkStatsSettings.class);
    mConnManager = createMock(IConnectivityManager.class);
    PowerManager powerManager = (PowerManager) mServiceContext.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mService = new NetworkStatsService(mServiceContext, mNetManager, alarmManager, wakeLock, mTime, TelephonyManager.getDefault(), mSettings, new NetworkStatsObservers(), mStatsDir, getBaseDir(mStatsDir));
    mHandlerThread = new IdleableHandlerThread("HandlerThread");
    mHandlerThread.start();
    Handler.Callback callback = new NetworkStatsService.HandlerCallback(mService);
    mHandler = new Handler(mHandlerThread.getLooper(), callback);
    mService.setHandler(mHandler, callback);
    mService.bindConnectivityManager(mConnManager);
    mElapsedRealtime = 0L;
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    replay();
    mService.systemReady();
    mSession = mService.openSession();
    verifyAndReset();
    mNetworkObserver = networkObserver.getValue();
}
Also used : TrustedTime(android.util.TrustedTime) INetworkManagementService(android.os.INetworkManagementService) BroadcastInterceptingContext(com.android.server.BroadcastInterceptingContext) Handler(android.os.Handler) IdleHandler(android.os.MessageQueue.IdleHandler) Capture(org.easymock.Capture) NetworkStatsSettings(com.android.server.net.NetworkStatsService.NetworkStatsSettings) PowerManager(android.os.PowerManager) NetworkStatsService(com.android.server.net.NetworkStatsService) IAlarmManager(android.app.IAlarmManager) AlarmManager(android.app.AlarmManager) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) IConnectivityManager(android.net.IConnectivityManager)

Example 4 with INetworkManagementEventObserver

use of android.net.INetworkManagementEventObserver in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method testStatsRebootPersist.

public void testStatsRebootPersist() throws Exception {
    assertStatsFilesExist(false);
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    verifyAndReset();
    // modify some number on wifi, and trigger poll event
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
    expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L).addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L).addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
    expectNetworkStatsPoll();
    mService.setUidForeground(UID_RED, false);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
    mService.setUidForeground(UID_RED, true);
    mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
    // graceful shutdown system, which should trigger persist of stats, and
    // clear any values in memory.
    expectCurrentTime();
    expectDefaultSettings();
    replay();
    mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
    verifyAndReset();
    assertStatsFilesExist(true);
    // boot through serviceReady() again
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectSystemReady();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    replay();
    mService.systemReady();
    mNetworkObserver = networkObserver.getValue();
    // after systemReady(), we should have historical stats loaded again
    assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
    assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
    assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, ROAMING_NO, 512L, 4L, 256L, 2L, 4);
    assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, ROAMING_NO, 512L, 4L, 256L, 2L, 6);
    assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
    verifyAndReset();
}
Also used : NetworkStats(android.net.NetworkStats) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Capture(org.easymock.Capture)

Example 5 with INetworkManagementEventObserver

use of android.net.INetworkManagementEventObserver in project android_frameworks_base by DirtyUnicorns.

the class NetworkPolicyManagerServiceTest method _setUp.

public void _setUp() throws Exception {
    super.setUp();
    setCurrentTimeMillis(TEST_START);
    // intercept various broadcasts, and pretend that uids have packages
    mServiceContext = new BroadcastInterceptingContext(getContext()) {

        @Override
        public PackageManager getPackageManager() {
            return new MockPackageManager() {

                @Override
                public String[] getPackagesForUid(int uid) {
                    return new String[] { "com.example" };
                }

                @Override
                public PackageInfo getPackageInfo(String packageName, int flags) {
                    final PackageInfo info = new PackageInfo();
                    final Signature signature;
                    if ("android".equals(packageName)) {
                        signature = new Signature("F00D");
                    } else {
                        signature = new Signature("DEAD");
                    }
                    info.signatures = new Signature[] { signature };
                    return info;
                }
            };
        }

        @Override
        public void startActivity(Intent intent) {
        // ignored
        }
    };
    mPolicyDir = getContext().getFilesDir();
    if (mPolicyDir.exists()) {
        IoUtils.deleteContents(mPolicyDir);
    }
    mActivityManager = createMock(IActivityManager.class);
    mStatsService = createMock(INetworkStatsService.class);
    mNetworkManager = createMock(INetworkManagementService.class);
    mPolicyListener = createMock(INetworkPolicyListener.class);
    mTime = createMock(TrustedTime.class);
    mConnManager = createMock(IConnectivityManager.class);
    mNotifManager = createMock(INotificationManager.class);
    mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, mNetworkManager, mTime, mPolicyDir, true);
    mService.bindConnectivityManager(mConnManager);
    mService.bindNotificationManager(mNotifManager);
    // RemoteCallbackList needs a binder to use as key
    expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
    replay();
    mService.registerListener(mPolicyListener);
    verifyAndReset();
    // catch IProcessObserver during systemReady()
    final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
    mActivityManager.registerProcessObserver(capture(processObserver));
    expectLastCall().atLeastOnce();
    // catch INetworkManagementEventObserver during systemReady()
    final Capture<INetworkManagementEventObserver> networkObserver = new Capture<INetworkManagementEventObserver>();
    mNetworkManager.registerObserver(capture(networkObserver));
    expectLastCall().atLeastOnce();
    expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
    expectCurrentTime();
    replay();
    mService.systemReady();
    verifyAndReset();
    mProcessObserver = processObserver.getValue();
    mNetworkObserver = networkObserver.getValue();
}
Also used : PackageInfo(android.content.pm.PackageInfo) TrustedTime(android.util.TrustedTime) INetworkManagementService(android.os.INetworkManagementService) MockPackageManager(android.test.mock.MockPackageManager) Intent(android.content.Intent) INetworkPolicyListener(android.net.INetworkPolicyListener) Capture(org.easymock.Capture) NetworkPolicyManagerService(com.android.server.net.NetworkPolicyManagerService) PackageManager(android.content.pm.PackageManager) MockPackageManager(android.test.mock.MockPackageManager) INotificationManager(android.app.INotificationManager) Signature(android.content.pm.Signature) INetworkStatsService(android.net.INetworkStatsService) INetworkManagementEventObserver(android.net.INetworkManagementEventObserver) IConnectivityManager(android.net.IConnectivityManager) IProcessObserver(android.app.IProcessObserver) IActivityManager(android.app.IActivityManager)

Aggregations

INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)14 Capture (org.easymock.Capture)12 Intent (android.content.Intent)9 IConnectivityManager (android.net.IConnectivityManager)8 INetworkManagementService (android.os.INetworkManagementService)8 TrustedTime (android.util.TrustedTime)8 PackageInfo (android.content.pm.PackageInfo)5 PackageManager (android.content.pm.PackageManager)5 Signature (android.content.pm.Signature)5 NetworkPolicyManagerService (com.android.server.net.NetworkPolicyManagerService)5 NetworkStatsService (com.android.server.net.NetworkStatsService)5 IActivityManager (android.app.IActivityManager)4 IAlarmManager (android.app.IAlarmManager)4 INotificationManager (android.app.INotificationManager)4 IProcessObserver (android.app.IProcessObserver)4 PendingIntent (android.app.PendingIntent)4 INetworkPolicyListener (android.net.INetworkPolicyListener)4 INetworkStatsService (android.net.INetworkStatsService)4 NetworkStats (android.net.NetworkStats)4 Handler (android.os.Handler)4