diff options
author | Roshan Pius <rpius@google.com> | 2018-09-24 13:04:47 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2018-10-31 09:22:35 -0700 |
commit | 0c4c806bcd0990fce252a6c6f0a413faf8e197f3 (patch) | |
tree | 6cb5f63897a2726479f58786af82ec268b12c700 /tests | |
parent | d461ed97902a08f6f2a7014cbeafb288b590539e (diff) |
WifiNetworkFactory: Implement |acceptRequest|
The wifi network factory will accept network requests for the following
cases:
a) Request is for a generic wifi network (legacy mechanism).
b) Specific Request from a foreground app.
c) Specific Request from a foreground service when we're not already
processing another specific request from a foreground app.
Also, modified the |needNetworkFor| & |releaseNetworkFor| to store &
clear the active network request. Further processing in these methods
will be added in further CL.
Also, added MatchAllNetworkSpecifier in the capabilities used
by WifiNetworkFactory.
Bug: 113878056
Test: Unit tests
Change-Id: I69a206cffe4e8b6387d2df71526142f89b95c767
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java | 167 |
1 files changed, 164 insertions, 3 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java index 46519e519..fdb2b107d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java @@ -16,12 +16,18 @@ package com.android.server.wifi; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; + import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import android.app.ActivityManager; import android.content.Context; +import android.content.pm.PackageManager; import android.net.NetworkCapabilities; import android.net.NetworkRequest; +import android.net.wifi.WifiNetworkSpecifier; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; @@ -36,8 +42,15 @@ import org.mockito.MockitoAnnotations; */ @SmallTest public class WifiNetworkFactoryTest { + private static final int TEST_UID_1 = 10423; + private static final String TEST_PACKAGE_NAME_1 = "com.test.networkrequest.1"; + private static final int TEST_UID_2 = 10424; + private static final String TEST_PACKAGE_NAME_2 = "com.test.networkrequest.2"; + @Mock WifiConnectivityManager mWifiConnectivityManager; @Mock Context mContext; + @Mock ActivityManager mActivityManager; + @Mock PackageManager mPackageManager; NetworkCapabilities mNetworkCapabilities; TestLooper mLooper; NetworkRequest mNetworkRequest; @@ -55,8 +68,16 @@ public class WifiNetworkFactoryTest { mNetworkCapabilities = new NetworkCapabilities(); mNetworkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + when(mPackageManager.getNameForUid(TEST_UID_1)).thenReturn(TEST_PACKAGE_NAME_1); + when(mPackageManager.getNameForUid(TEST_UID_2)).thenReturn(TEST_PACKAGE_NAME_2); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + mWifiNetworkFactory = new WifiNetworkFactory(mLooper.getLooper(), mContext, - mNetworkCapabilities, mWifiConnectivityManager); + mNetworkCapabilities, mActivityManager, mWifiConnectivityManager); mNetworkRequest = new NetworkRequest.Builder() .setCapabilities(mNetworkCapabilities) @@ -75,7 +96,7 @@ public class WifiNetworkFactoryTest { * Validates handling of needNetworkFor. */ @Test - public void testHandleNetworkRequest() { + public void testHandleNetworkRequestWithNoSpecifier() { assertFalse(mWifiNetworkFactory.hasConnectionRequests()); mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); @@ -92,7 +113,7 @@ public class WifiNetworkFactoryTest { * Validates handling of releaseNetwork. */ @Test - public void testHandleNetworkRelease() { + public void testHandleNetworkReleaseWithNoSpecifier() { // Release network with out a corresponding request should be ignored. mWifiNetworkFactory.releaseNetworkFor(mNetworkRequest); assertFalse(mWifiNetworkFactory.hasConnectionRequests()); @@ -105,4 +126,144 @@ public class WifiNetworkFactoryTest { assertFalse(mWifiNetworkFactory.hasConnectionRequests()); verify(mWifiConnectivityManager).setTrustedConnectionAllowed(false); } + + /** + * Validates handling of acceptNetwork for requests with no network specifier. + */ + @Test + public void testHandleAcceptNetworkRequestWithNoSpecifier() { + assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a non foreground + * app/service. + */ + @Test + public void testHandleAcceptNetworkRequestFromNonFgAppOrSvcWithSpecifier() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE + 1); + + WifiNetworkSpecifier specifier = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier); + + assertFalse(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a foreground + * app. + */ + @Test + public void testHandleAcceptNetworkRequestFromFgAppWithSpecifier() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND); + + WifiNetworkSpecifier specifier = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier); + + assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a foreground + * app. + */ + @Test + public void testHandleAcceptNetworkRequestFromFgAppWithSpecifierWithPendingRequestFromFgSvc() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2)) + .thenReturn(IMPORTANCE_FOREGROUND); + + // Handle request 1. + WifiNetworkSpecifier specifier1 = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // Make request 2 which will be accepted because a fg app request can + // override a fg service request. + WifiNetworkSpecifier specifier2 = + new WifiNetworkSpecifier(TEST_UID_2); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2); + assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a foreground + * app. + */ + @Test + public void testHandleAcceptNetworkRequestFromFgSvcWithSpecifierWithPendingRequestFromFgSvc() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + + // Handle request 1. + WifiNetworkSpecifier specifier1 = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // Make request 2 which will be accepted because a fg service request can + // override an existing fg service request. + WifiNetworkSpecifier specifier2 = + new WifiNetworkSpecifier(TEST_UID_2); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2); + assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a foreground + * app. + */ + @Test + public void testHandleAcceptNetworkRequestFromFgAppWithSpecifierWithPendingRequestFromFgApp() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2)) + .thenReturn(IMPORTANCE_FOREGROUND); + + // Handle request 1. + WifiNetworkSpecifier specifier1 = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // Make request 2 which will be accepted because a fg app request can + // override an existing fg app request. + WifiNetworkSpecifier specifier2 = + new WifiNetworkSpecifier(TEST_UID_2); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2); + assertTrue(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } + + /** + * Validates handling of acceptNetwork with a network specifier from a foreground + * service when we're in the midst of processing a request from a foreground app. + */ + @Test + public void testHandleAcceptNetworkRequestFromFgSvcWithSpecifierWithPendingRequestFromFgApp() { + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_1)) + .thenReturn(IMPORTANCE_FOREGROUND); + when(mActivityManager.getPackageImportance(TEST_PACKAGE_NAME_2)) + .thenReturn(IMPORTANCE_FOREGROUND_SERVICE); + + // Handle request 1. + WifiNetworkSpecifier specifier1 = + new WifiNetworkSpecifier(TEST_UID_1); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier1); + mWifiNetworkFactory.needNetworkFor(mNetworkRequest, 0); + + // Make request 2 which will be rejected because a fg service request cannot + // override a fg app request. + WifiNetworkSpecifier specifier2 = + new WifiNetworkSpecifier(TEST_UID_2); + mNetworkRequest.networkCapabilities.setNetworkSpecifier(specifier2); + assertFalse(mWifiNetworkFactory.acceptRequest(mNetworkRequest, 0)); + } } |