From 6f39981ba5abf52c8c71ba3d9f5abef2fc6498fa Mon Sep 17 00:00:00 2001 From: Varun Anand Date: Fri, 3 Apr 2020 14:15:04 -0700 Subject: Exempt active scorer from user approval for its suggestions. Note, I'm adding a method to PerAppInfo that encapsulates all the exemptions (currently based on carrier privileges and active scorer). This is to keep the surface area of changes to a minimum (in future) whenever exemptions need to be modified. Fixes: 152335546 Test: atest com.android.server.wifi Test: manually verified that active scorer is exempted. Change-Id: Iffe5edfd1824d0c1fbbb93430714a9f372be3643 --- .../wifi/WifiNetworkSuggestionsManagerTest.java | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index e9bb940be..5bc4d3366 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -16,8 +16,11 @@ package com.android.server.wifi; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED; import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; +import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE; import static android.app.AppOpsManager.MODE_ALLOWED; import static android.app.AppOpsManager.MODE_IGNORED; import static android.app.AppOpsManager.OPSTR_CHANGE_WIFI_STATE; @@ -50,6 +53,7 @@ import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; +import android.net.NetworkScoreManager; import android.net.util.MacAddressUtils; import android.net.wifi.EAPConstants; import android.net.wifi.ISuggestionConnectionStatusListener; @@ -128,6 +132,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { private @Mock Resources mResources; private @Mock AppOpsManager mAppOpsManager; private @Mock NotificationManager mNotificationManger; + private @Mock NetworkScoreManager mNetworkScoreManager; private @Mock PackageManager mPackageManager; private @Mock WifiPermissionsUtil mWifiPermissionsUtil; private @Mock WifiInjector mWifiInjector; @@ -206,6 +211,7 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) .thenReturn(mNotificationManger); + when(mContext.getSystemService(NetworkScoreManager.class)).thenReturn(mNetworkScoreManager); when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mContext.getSystemService(ActivityManager.class)).thenReturn(mActivityManager); when(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) @@ -2587,6 +2593,50 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { validateUserApprovalNotification(TEST_APP_NAME_1); } + @Test + public void testAddNetworkSuggestions_activeFgScorer_doesNotRequestForApproval() { + // Fg app + when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_FOREGROUND); + // Active scorer + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List networkSuggestionList = + new ArrayList() {{ + add(networkSuggestion); + }}; + + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + + @Test + public void testAddNetworkSuggestions_activeBgScorer_doesNotRequestForApproval() { + // Bg app + when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_SERVICE); + // Active scorer + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List networkSuggestionList = + new ArrayList() {{ + add(networkSuggestion); + }}; + + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + /** * Verify handling of user clicking allow on the user approval notification when first time * add suggestions. @@ -2621,6 +2671,26 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { verifyNoMoreInteractions(mNotificationManger); } + @Test + public void getNetworkSuggestionsForScanDetail_exemptsActiveScorerFromUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true); + List networkSuggestionList = + new ArrayList() {{ + add(networkSuggestion); + }}; + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( + createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); + + verifyZeroInteractions(mNotificationManger); + verifyZeroInteractions(mAlertDialog); + } + /** * Verify handling of user clicking Disallow on the user approval notification when first time * add suggestions. @@ -2917,6 +2987,30 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertNull(ewns); } + @Test + public void getNetworkSuggestionsForFqdn_activeScorer_doesNotRequestForUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + PasspointConfiguration passpointConfiguration = + createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); + WifiConfiguration dummyConfiguration = createDummyWifiConfigurationForPasspoint(TEST_FQDN); + dummyConfiguration.FQDN = TEST_FQDN; + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion(dummyConfiguration, + passpointConfiguration, true, false, true, true); + List networkSuggestionList = Arrays.asList(networkSuggestion); + dummyConfiguration.creatorUid = TEST_UID_1; + when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), + anyInt(), anyString(), eq(true), eq(true))).thenReturn(true); + assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); + + Set ewns = + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); + + assertEquals(1, ewns.size()); + verifyZeroInteractions(mAlertDialog); + verifyZeroInteractions(mNotificationManger); + } + /** * Verify return true when allow user manually connect and user approved the app */ @@ -3622,6 +3716,24 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { assertEquals(network1.SSID, pnoNetwork.get(0).SSID); } + @Test + public void getAllScanOptimizationSuggestionNetworks_returnsActiveScorerWithoutUserApproval() { + when(mNetworkScoreManager.getActiveScorerPackage()).thenReturn(TEST_PACKAGE_1); + WifiConfiguration network = WifiConfigurationTestUtil.createOpenNetwork(); + WifiNetworkSuggestion networkSuggestion = + new WifiNetworkSuggestion(network, null, false, false, true, true); + List networkSuggestionList = Arrays.asList(networkSuggestion); + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager + .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_PACKAGE_1); + + List networks = + mWifiNetworkSuggestionsManager.getAllScanOptimizationSuggestionNetworks(); + + assertEquals(1, networks.size()); + } + /** * Verify if a suggestion is mostRecently connected, flag will be persist. */ -- cgit v1.2.3