From d8385620730a483b98a8db3b86b1a150450cf105 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Wed, 27 May 2020 12:48:29 -0700 Subject: WifiNetworkSuggestionsManager: Only use approved suggestions externally Only expose approved network suggestions to external modules, unapproved suggestions by apps should not considered by platform for any behavior changes. Bug: 157579285 Test: atest com.android.server.wifi Change-Id: I09925b4498d435f35115a1680562c3765c067ca3 --- .../com/android/server/wifi/WakeupController.java | 2 +- .../server/wifi/WifiConnectivityManager.java | 2 +- .../server/wifi/WifiNetworkSuggestionsManager.java | 13 ++++++ .../android/server/wifi/WakeupControllerTest.java | 6 +-- .../server/wifi/WifiConnectivityManagerTest.java | 6 +-- .../wifi/WifiNetworkSuggestionsManagerTest.java | 47 ++++++++++++++++++++++ 6 files changed, 68 insertions(+), 8 deletions(-) diff --git a/service/java/com/android/server/wifi/WakeupController.java b/service/java/com/android/server/wifi/WakeupController.java index d9882dbf3..a92bdf7f1 100644 --- a/service/java/com/android/server/wifi/WakeupController.java +++ b/service/java/com/android/server/wifi/WakeupController.java @@ -347,7 +347,7 @@ public class WakeupController { } Set networkSuggestions = - mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions(); for (WifiNetworkSuggestion suggestion : networkSuggestions) { // TODO(b/127799111): Do we need to filter the list similar to saved networks above? goodNetworks.add( diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 8470d0c7d..bcf162275 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -1635,7 +1635,7 @@ public class WifiConnectivityManager { } Set suggestionsNetworks = - mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions(); // If total size not equal to 1, then no need to proceed if (passpointNetworks.size() + savedNetworks.size() + suggestionsNetworks.size() != 1) { return false; diff --git a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java index f470039a7..5d5a7d782 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java +++ b/service/java/com/android/server/wifi/WifiNetworkSuggestionsManager.java @@ -1253,6 +1253,19 @@ public class WifiNetworkSuggestionsManager { .collect(Collectors.toSet()); } + /** + * Returns a set of all network suggestions across all apps that have been approved by user. + */ + public Set getAllApprovedNetworkSuggestions() { + final String activeScorerPackage = mNetworkScoreManager.getActiveScorerPackage(); + return mActiveNetworkSuggestionsPerApp.values() + .stream() + .filter(e -> e.isApproved(activeScorerPackage)) + .flatMap(e -> convertToWnsSet(e.extNetworkSuggestions) + .stream()) + .collect(Collectors.toSet()); + } + /** * Get all user approved, non-passpoint networks from suggestion. */ diff --git a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java index 9824c5b23..3ac4adc3b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java @@ -390,7 +390,7 @@ public class WakeupControllerTest extends WifiBaseTest { WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); WifiNetworkSuggestion wepNetworkSuggestion = new WifiNetworkSuggestion(wepNetwork, null, false, false, true, true); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(new HashSet<>(Arrays.asList( openNetworkSuggestion, wepNetworkSuggestion))); @@ -432,7 +432,7 @@ public class WakeupControllerTest extends WifiBaseTest { WifiConfiguration oweNetwork = WifiConfigurationTestUtil.createOweNetwork(quotedSsid2); WifiNetworkSuggestion oweNetworkSuggestion = new WifiNetworkSuggestion(oweNetwork, null, false, false, true, true); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(new HashSet<>(Arrays.asList(oweNetworkSuggestion))); // scan results from most recent scan @@ -530,7 +530,7 @@ public class WakeupControllerTest extends WifiBaseTest { .createOpenNetwork(ScanResultUtil.createQuotedSSID(SAVED_SSID)); WifiNetworkSuggestion openNetworkSuggestion = new WifiNetworkSuggestion(openNetwork, null, false, false, true, true); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(new HashSet<>(Collections.singletonList(openNetworkSuggestion))); initializeWakeupController(true /* enabled */); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index b01f2beb1..fe16b2833 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -109,7 +109,7 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); when(mWifiNetworkSuggestionsManager.retrieveHiddenNetworkList()) .thenReturn(new ArrayList<>()); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(new HashSet<>()); when(mWifiInjector.getBssidBlocklistMonitor()).thenReturn(mBssidBlocklistMonitor); when(mWifiInjector.getWifiChannelUtilizationScan()).thenReturn(mWifiChannelUtilization); @@ -1497,7 +1497,7 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { when(mWifiNetworkSuggestion.getWifiConfiguration()).thenReturn(mSuggestionConfig); Set suggestionNetworks = new HashSet(); suggestionNetworks.add(mWifiNetworkSuggestion); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(suggestionNetworks); // Prepare for no saved networks @@ -1548,7 +1548,7 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { when(mWifiNetworkSuggestion.getWifiConfiguration()).thenReturn(mSuggestionConfig); Set suggestionNetworks = new HashSet(); suggestionNetworks.add(mWifiNetworkSuggestion); - when(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions()) + when(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()) .thenReturn(suggestionNetworks); // Prepare for a single passpoint network diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index 897dca341..e7e866084 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -4017,6 +4017,53 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { listenerIdentifier, TEST_PACKAGE_1); } + /** + * Verify that we only return user approved suggestions. + */ + @Test + public void testGetApprovedNetworkSuggestions() { + WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + wifiConfiguration, null, false, false, true, true); + // Reuse the same network credentials to ensure they both match. + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + wifiConfiguration, null, false, false, true, true); + + List networkSuggestionList1 = + new ArrayList() {{ + add(networkSuggestion1); + }}; + List networkSuggestionList2 = + new ArrayList() {{ + add(networkSuggestion2); + }}; + + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, + TEST_PACKAGE_1, TEST_FEATURE)); + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, + TEST_PACKAGE_2, TEST_FEATURE)); + + // nothing approved, return empty. + assertTrue(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions().isEmpty()); + + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); + // only app 1 approved. + assertEquals(new HashSet() {{ + add(networkSuggestion1); + }}, + mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()); + + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_2); + // both app 1 & 2 approved. + assertEquals(new HashSet() {{ + add(networkSuggestion1); + add(networkSuggestion2); + }}, + mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()); + } + /** * Helper function for creating a test configuration with user credential. * -- cgit v1.2.3