diff options
author | Nate Jiang <qiangjiang@google.com> | 2020-02-25 00:22:59 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-02-25 00:22:59 +0000 |
commit | a41a8b4bace719172bb2721e0b6323bc3830e11d (patch) | |
tree | a0b931b3ed354b7d7e41fa03a1afac40631c4564 /tests | |
parent | 861798ec8b5c8770bb883a101ceed513ef7954c4 (diff) | |
parent | 34abfd0239ba324b4f2ec50a7296973b08417e82 (diff) |
Merge changes from topic "addSuggestionPno-rvc-dev" into rvc-dev
* changes:
Move create intenal wificonfig into ExtendedWifiNetworkSuggestion
Add suggestion network to PNO scan list
Diffstat (limited to 'tests')
3 files changed, 240 insertions, 424 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index 233d1a234..3ff4aa239 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -110,8 +110,6 @@ public class WifiConfigManagerTest extends WifiBaseTest { private static final String TEST_PAC_PROXY_LOCATION_2 = "http://blah"; private static final int TEST_RSSI = -50; private static final int TEST_FREQUENCY_1 = 2412; - private static final int TEST_FREQUENCY_2 = 5180; - private static final int TEST_FREQUENCY_3 = 5240; private static final int MAX_BLOCKED_BSSID_PER_NETWORK = 10; private static final MacAddress TEST_RANDOMIZED_MAC = MacAddress.fromString("d2:11:19:34:a5:20"); @@ -142,7 +140,6 @@ public class WifiConfigManagerTest extends WifiBaseTest { @Mock private WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; @Mock private WifiScoreCard mWifiScoreCard; @Mock private PerNetwork mPerNetwork; - @Mock private PerNetwork mPerNetwork1; private MockResources mResources; private InOrder mContextConfigStoreMockOrder; @@ -2371,367 +2368,6 @@ public class WifiConfigManagerTest extends WifiBaseTest { } /** - * Verifies the ordering of network list generated using - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoList() { - // Create and add 3 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - verifyAddNetworkToWifiConfigManager(network2); - verifyAddNetworkToWifiConfigManager(network3); - - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - // Now set scan results in 2 of them to set the corresponding - // {@link NetworkSelectionStatus#mSeenInLastQualifiedNetworkSelection} field. - assertTrue(mWifiConfigManager.setNetworkCandidateScanResult( - network1.networkId, createScanDetailForNetwork(network1).getScanResult(), 54)); - assertTrue(mWifiConfigManager.setNetworkCandidateScanResult( - network3.networkId, createScanDetailForNetwork(network3).getScanResult(), 54)); - - // Now increment |network3|'s association count. This should ensure that this network - // is preferred over |network1|. - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network3.networkId)); - - // Retrieve the Pno network list & verify the order of the networks returned. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(3, pnoNetworks.size()); - assertEquals(network3.SSID, pnoNetworks.get(0).ssid); - assertEquals(network1.SSID, pnoNetworks.get(1).ssid); - assertEquals(network2.SSID, pnoNetworks.get(2).ssid); - - // Now permanently disable |network3|. This should remove network 3 from the list. - assertTrue(mWifiConfigManager.disableNetwork( - network3.networkId, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - // Retrieve the Pno network list again & verify the order of the networks returned. - pnoNetworks = mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(2, pnoNetworks.size()); - assertEquals(network1.SSID, pnoNetworks.get(0).ssid); - assertEquals(network2.SSID, pnoNetworks.get(1).ssid); - - // Now set network1 autojoin disabled. This should remove network 1 from the list. - assertTrue(mWifiConfigManager.allowAutojoin(network1.networkId, false)); - // Retrieve the Pno network list again & verify the order of the networks returned. - pnoNetworks = mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(1, pnoNetworks.size()); - assertEquals(network2.SSID, pnoNetworks.get(0).ssid); - } - - /** - * Verifies frequencies are populated correctly for pno networks. - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListFrequencies() { - mResources.setBoolean(R.bool.config_wifiPnoFrequencyCullingEnabled, true); - // Create and add 3 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - verifyAddNetworkToWifiConfigManager(network2); - when(mWifiScoreCard.lookupNetwork(network1.SSID)).thenReturn(mPerNetwork); - when(mWifiScoreCard.lookupNetwork(network2.SSID)).thenReturn(mPerNetwork1); - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - - // Retrieve the Pno network list & verify the order of the networks returned. - // Frequencies should be empty since no scan results have been received yet. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(2, pnoNetworks.size()); - assertEquals(network1.SSID, pnoNetworks.get(0).ssid); - assertEquals(network2.SSID, pnoNetworks.get(1).ssid); - assertTrue("frequencies should be empty", pnoNetworks.get(0).frequencies.length == 0); - assertTrue("frequencies should be empty", pnoNetworks.get(1).frequencies.length == 0); - - // Add frequencies to |network1| - ScanDetail scanDetail1 = createScanDetailForNetwork(network1, TEST_BSSID + "1", - TEST_RSSI, TEST_FREQUENCY_1); - ScanDetail scanDetail2 = createScanDetailForNetwork(network1, TEST_BSSID + "2", - TEST_RSSI, TEST_FREQUENCY_1); - ScanDetail scanDetail3 = createScanDetailForNetwork(network1, TEST_BSSID + "3", - TEST_RSSI, TEST_FREQUENCY_2); - ScanDetail scanDetail4 = createScanDetailForNetwork(network1, TEST_BSSID + "4", - TEST_RSSI, TEST_FREQUENCY_3); - - mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(scanDetail1); - mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(scanDetail2); - mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(scanDetail3); - mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(scanDetail4); - ArgumentCaptor<Integer> argumentCaptor = ArgumentCaptor.forClass(Integer.class); - verify(mPerNetwork, times(4)).addFrequency(argumentCaptor.capture()); - verify(mPerNetwork1, never()).addFrequency(anyInt()); - - Set<Integer> channelSet = new HashSet<>(); - channelSet.addAll(argumentCaptor.getAllValues()); - assertEquals(3, channelSet.size()); - when(mPerNetwork.getFrequencies()).thenReturn(new ArrayList<>(channelSet)); - when(mPerNetwork1.getFrequencies()).thenReturn(new ArrayList<>()); - pnoNetworks = mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(2, pnoNetworks.size()); - assertEquals(network1.SSID, pnoNetworks.get(0).ssid); - assertEquals(network2.SSID, pnoNetworks.get(1).ssid); - assertEquals(3, pnoNetworks.get(0).frequencies.length); - Arrays.sort(pnoNetworks.get(0).frequencies); - assertEquals(TEST_FREQUENCY_1, pnoNetworks.get(0).frequencies[0]); - assertEquals(TEST_FREQUENCY_2, pnoNetworks.get(0).frequencies[1]); - assertEquals(TEST_FREQUENCY_3, pnoNetworks.get(0).frequencies[2]); - assertTrue("frequencies should be empty", pnoNetworks.get(1).frequencies.length == 0); - } - - /** - * Verify that pno frequency culling is disabled by the flag properly. - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListFrequenciesFlagDisabled() { - mResources.setBoolean(R.bool.config_wifiPnoFrequencyCullingEnabled, false); - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - ScanDetail scanDetail1 = createScanDetailForNetwork(network1, TEST_BSSID + "1", - TEST_RSSI, TEST_FREQUENCY_1); - mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(scanDetail1); - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(1, pnoNetworks.size()); - assertEquals(network1.SSID, pnoNetworks.get(0).ssid); - assertEquals(0, pnoNetworks.get(0).frequencies.length); - } - - /** - * Verifies the ordering of network list generated using - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListPrefersLastConnectedNetwork() { - mResources.setBoolean(R.bool.config_wifiPnoRecencySortingEnabled, true); - // Create and add 3 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - verifyAddNetworkToWifiConfigManager(network2); - verifyAddNetworkToWifiConfigManager(network3); - - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - long firstConnectionTimeMillis = 45677; - long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; - - // Now simulate first connection to |network1| at |firstConnectionTimeMillis|. - when(mClock.getWallClockMillis()).thenReturn(firstConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - - // Now simulate second connection to |network3| at |secondConnectionTimeMillis|. - when(mClock.getWallClockMillis()).thenReturn(secondConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network3.networkId)); - - // Retrieve the Pno network list & verify the order of the networks returned. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(3, pnoNetworks.size()); - assertEquals(network3.SSID, pnoNetworks.get(0).ssid); - assertEquals(network1.SSID, pnoNetworks.get(1).ssid); - assertEquals(network2.SSID, pnoNetworks.get(2).ssid); - } - - /** - * Verifies the ordering of network list generated using - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListPrefersLastConnectedNetworkThenMostConnectedNetworks() { - mResources.setBoolean(R.bool.config_wifiPnoRecencySortingEnabled, true); - // Create and add 3 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - verifyAddNetworkToWifiConfigManager(network2); - verifyAddNetworkToWifiConfigManager(network3); - - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - long firstConnectionTimeMillis = 45677; - long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; - long thirdConnectionTimeMillis = secondConnectionTimeMillis + 45; - long fourthConnectionTimeMillis = thirdConnectionTimeMillis + 45; - long fifthConnectionTimeMillis = fourthConnectionTimeMillis + 45; - long sixthConnectionTimeMillis = fifthConnectionTimeMillis + 45; - - // Simulate 3 connections to |network2| - when(mClock.getWallClockMillis()).thenReturn(firstConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network2.networkId)); - when(mClock.getWallClockMillis()).thenReturn(secondConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network2.networkId)); - when(mClock.getWallClockMillis()).thenReturn(thirdConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network2.networkId)); - - // Simulate 2 connections to |network1| - when(mClock.getWallClockMillis()).thenReturn(fourthConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - when(mClock.getWallClockMillis()).thenReturn(fifthConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - - // Simulate last connection to |network3| - when(mClock.getWallClockMillis()).thenReturn(sixthConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network3.networkId)); - - // Retrieve the Pno network list & verify the order of the networks returned. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(3, pnoNetworks.size()); - assertEquals(network3.SSID, pnoNetworks.get(0).ssid); - assertEquals(network2.SSID, pnoNetworks.get(1).ssid); - assertEquals(network1.SSID, pnoNetworks.get(2).ssid); - } - - /** - * Verify that pno recency consideration feature is disabled by the flag. - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListRecencyFlagDisabled() { - mResources.setBoolean(R.bool.config_wifiPnoRecencySortingEnabled, false); - // Create and add 3 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); - verifyAddNetworkToWifiConfigManager(network1); - verifyAddNetworkToWifiConfigManager(network2); - verifyAddNetworkToWifiConfigManager(network3); - - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - network1.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network2.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - network3.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - long firstConnectionTimeMillis = 45677; - long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; - long thridConnectionTimeMillis = secondConnectionTimeMillis + 45; - - // Simulate connecting to network1 2 times - when(mClock.getWallClockMillis()).thenReturn(firstConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - when(mClock.getWallClockMillis()).thenReturn(secondConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network1.networkId)); - - // Simulate connecting to network2 once with the newest timestamp - when(mClock.getWallClockMillis()).thenReturn(thridConnectionTimeMillis); - assertTrue(mWifiConfigManager.updateNetworkAfterConnect(network2.networkId)); - - // Retrieve the Pno network list & verify the order of the networks returned. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(3, pnoNetworks.size()); - assertEquals(network1.SSID, pnoNetworks.get(0).ssid); - assertEquals(network2.SSID, pnoNetworks.get(1).ssid); - assertEquals(network3.SSID, pnoNetworks.get(2).ssid); - } - - /** - * Verifies that the list of PNO networks does not contain ephemeral or passpoint networks - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListDoesNotContainEphemeralOrPasspointNetworks() throws Exception { - WifiConfiguration savedOpenNetwork = WifiConfigurationTestUtil.createOpenNetwork(); - WifiConfiguration ephemeralNetwork = WifiConfigurationTestUtil.createEphemeralNetwork(); - WifiConfiguration passpointNetwork = WifiConfigurationTestUtil.createPasspointNetwork(); - - verifyAddNetworkToWifiConfigManager(savedOpenNetwork); - verifyAddEphemeralNetworkToWifiConfigManager(ephemeralNetwork); - verifyAddPasspointNetworkToWifiConfigManager(passpointNetwork); - - // Enable all of them. - assertTrue(mWifiConfigManager.enableNetwork( - savedOpenNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - ephemeralNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - assertTrue(mWifiConfigManager.enableNetwork( - passpointNetwork.networkId, false, TEST_CREATOR_UID, TEST_CREATOR_NAME)); - - // Retrieve the Pno network list & verify the order of the networks returned. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(1, pnoNetworks.size()); - assertEquals(savedOpenNetwork.SSID, pnoNetworks.get(0).ssid); - } - - /** - * Verifies that the list of PNO networks does not contain any permanently or temporarily - * disabled networks. - * {@link WifiConfigManager#retrievePnoNetworkList()}. - */ - @Test - public void testRetrievePnoListDoesNotContainDisabledNetworks() throws Exception { - // Create and add 2 networks. - WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); - WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); - - NetworkUpdateResult result1 = verifyAddNetworkToWifiConfigManager(network1); - NetworkUpdateResult result2 = verifyAddNetworkToWifiConfigManager(network2); - - // Enable all of them. - verifyUpdateNetworkSelectionStatus( - result1.getNetworkId(), NetworkSelectionStatus.DISABLED_NONE, 0); - verifyUpdateNetworkSelectionStatus( - result2.getNetworkId(), NetworkSelectionStatus.DISABLED_NONE, 0); - - // Set network1 to temporarily disabled. The threshold for association rejection is 5, so - // disable it 5 times to actually mark it temporarily disabled. - int assocRejectReason = NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION; - int assocRejectThreshold = - WifiConfigManager.getNetworkSelectionDisableThreshold(assocRejectReason); - for (int i = 1; i <= assocRejectThreshold; i++) { - verifyUpdateNetworkSelectionStatus(result1.getNetworkId(), assocRejectReason, i); - } - - // Set network 2 to permanently disabled. - verifyUpdateNetworkSelectionStatus( - result2.getNetworkId(), NetworkSelectionStatus.DISABLED_BY_WIFI_MANAGER, 0); - - // Retrieve the Pno network list & verify both networks are not included. - List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = - mWifiConfigManager.retrievePnoNetworkList(); - assertEquals(0, pnoNetworks.size()); - } - - /** * Verifies the linking of networks when they have the same default GW Mac address in * {@link WifiConfigManager#getOrCreateScanDetailCacheForNetwork(WifiConfiguration)}. */ diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 435577b52..2f231519b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -26,7 +26,6 @@ import android.app.test.MockAnswerUtil.AnswerWithArguments; import android.app.test.TestAlarmManager; import android.content.Context; import android.content.pm.PackageManager; -import android.content.res.Resources; import android.net.NetworkScoreManager; import android.net.wifi.ScanResult; import android.net.wifi.ScanResult.InformationElement; @@ -66,6 +65,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -83,7 +83,8 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - mResource = mockResource(); + mResources = new MockResources(); + setUpResources(mResources); mAlarmManager = new TestAlarmManager(); mContext = mockContext(); mLocalLog = new LocalLog(512); @@ -101,16 +102,37 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { .thenReturn(new ArrayList<>()); when(mWifiInjector.getBssidBlocklistMonitor()).thenReturn(mBssidBlocklistMonitor); when(mWifiInjector.getWifiChannelUtilizationScan()).thenReturn(mWifiChannelUtilization); + when(mWifiInjector.getWifiScoreCard()).thenReturn(mWifiScoreCard); + when(mWifiInjector.getWifiNetworkSuggestionsManager()) + .thenReturn(mWifiNetworkSuggestionsManager); mWifiConnectivityManager = createConnectivityManager(); verify(mWifiConfigManager).addOnNetworkUpdateListener(anyObject()); mWifiConnectivityManager.setTrustedConnectionAllowed(true); mWifiConnectivityManager.setWifiEnabled(true); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); - mMinPacketRateActiveTraffic = mResource.getInteger( + mMinPacketRateActiveTraffic = mResources.getInteger( R.integer.config_wifiFrameworkMinPacketPerSecondActiveTraffic); when(mWifiLastResortWatchdog.shouldIgnoreBssidUpdate(anyString())).thenReturn(false); } + private void setUpResources(MockResources resources) { + resources.setBoolean( + R.bool.config_wifi_framework_enable_associated_network_selection, true); + resources.setInteger( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz, -60); + resources.setInteger( + R.integer.config_wifiFrameworkMinPacketPerSecondActiveTraffic, 16); + resources.setIntArray( + R.array.config_wifiConnectedScanIntervalScheduleSec, + VALID_CONNECTED_SINGLE_SCAN_SCHEDULE); + resources.setIntArray( + R.array.config_wifiDisconnectedScanIntervalScheduleSec, + VALID_DISCONNECTED_SINGLE_SCAN_SCHEDULE); + resources.setIntArray( + R.array.config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec, + SCHEDULE_EMPTY); + } + /** * Called after each test */ @@ -121,8 +143,6 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { validateMockitoUsage(); } - private Resources mResource; - private Context mContext; private TestAlarmManager mAlarmManager; private TestLooper mLooper = new TestLooper(); @@ -146,6 +166,10 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { @Mock private BssidBlocklistMonitor mBssidBlocklistMonitor; @Mock private WifiChannelUtilization mWifiChannelUtilization; @Mock private ScoringParams mScoringParams; + @Mock private WifiScoreCard mWifiScoreCard; + @Mock private WifiScoreCard.PerNetwork mPerNetwork; + @Mock private WifiScoreCard.PerNetwork mPerNetwork1; + @Captor ArgumentCaptor<ScanResult> mCandidateScanResultCaptor; @Captor ArgumentCaptor<ArrayList<String>> mBssidBlacklistCaptor; @Captor ArgumentCaptor<ArrayList<String>> mSsidWhitelistCaptor; @@ -169,33 +193,14 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { private static final int MAX_SCAN_INTERVAL_IN_SCHEDULE = 60; private static final int[] DEFAULT_SINGLE_SCAN_SCHEDULE = {20, 40, 80, 160}; private static final int MAX_SCAN_INTERVAL_IN_DEFAULT_SCHEDULE = 160; - - Resources mockResource() { - Resources resource = mock(Resources.class); - - when(resource.getBoolean( - R.bool.config_wifi_framework_enable_associated_network_selection)).thenReturn(true); - when(resource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz)) - .thenReturn(-60); - when(resource.getInteger( - R.integer.config_wifiFrameworkMinPacketPerSecondActiveTraffic)).thenReturn(16); - when(resource.getIntArray( - R.array.config_wifiConnectedScanIntervalScheduleSec)) - .thenReturn(VALID_CONNECTED_SINGLE_SCAN_SCHEDULE); - when(resource.getIntArray( - R.array.config_wifiDisconnectedScanIntervalScheduleSec)) - .thenReturn(VALID_DISCONNECTED_SINGLE_SCAN_SCHEDULE); - when(resource.getIntArray(R.array - .config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec)) - .thenReturn(SCHEDULE_EMPTY); - return resource; - } + private static final int TEST_FREQUENCY_1 = 2412; + private static final int TEST_FREQUENCY_2 = 5180; + private static final int TEST_FREQUENCY_3 = 5240; Context mockContext() { Context context = mock(Context.class); - when(context.getResources()).thenReturn(mResource); + when(context.getResources()).thenReturn(mResources); when(context.getSystemService(Context.ALARM_SERVICE)).thenReturn( mAlarmManager.getAlarmManager()); when(context.getPackageManager()).thenReturn(mock(PackageManager.class)); @@ -314,15 +319,11 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { WifiConfigManager mockWifiConfigManager() { WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); - + WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); + List<WifiConfiguration> networkList = new ArrayList<>(); + networkList.add(config); when(wifiConfigManager.getConfiguredNetwork(anyInt())).thenReturn(null); - - // Pass dummy pno network list, otherwise Pno scan requests will not be triggered. - PnoSettings.PnoNetwork pnoNetwork = new PnoSettings.PnoNetwork(CANDIDATE_SSID); - ArrayList<PnoSettings.PnoNetwork> pnoNetworkList = new ArrayList<>(); - pnoNetworkList.add(pnoNetwork); - when(wifiConfigManager.retrievePnoNetworkList()).thenReturn(pnoNetworkList); - when(wifiConfigManager.retrievePnoNetworkList()).thenReturn(pnoNetworkList); + when(wifiConfigManager.getSavedNetworks(anyInt())).thenReturn(networkList); doNothing().when(wifiConfigManager).addOnNetworkUpdateListener( mNetworkUpdateListenerCaptor.capture()); @@ -430,9 +431,8 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { @Test public void turnScreenOnWhenWifiInConnectedStateRoamingDisabled() { // Turn off auto roaming - when(mResource.getBoolean( - R.bool.config_wifi_framework_enable_associated_network_selection)) - .thenReturn(false); + mResources.setBoolean( + R.bool.config_wifi_framework_enable_associated_network_selection, false); mWifiConnectivityManager = createConnectivityManager(); mWifiConnectivityManager.setTrustedConnectionAllowed(true); @@ -771,8 +771,8 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { */ @Test public void checkPeriodicScanIntervalWhenDisconnectedWithEmptySchedule() throws Exception { - when(mResource.getIntArray(R.array.config_wifiDisconnectedScanIntervalScheduleSec)) - .thenReturn(SCHEDULE_EMPTY); + mResources.setIntArray(R.array.config_wifiDisconnectedScanIntervalScheduleSec, + SCHEDULE_EMPTY); checkWorkingWithDefaultSchedule(); } @@ -783,8 +783,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { */ @Test public void checkPeriodicScanIntervalWhenDisconnectedWithZeroValuesSchedule() { - when(mResource.getIntArray(R.array.config_wifiDisconnectedScanIntervalScheduleSec)) - .thenReturn(INVALID_SCHEDULE_ZERO_VALUES); + mResources.setIntArray( + R.array.config_wifiDisconnectedScanIntervalScheduleSec, + INVALID_SCHEDULE_ZERO_VALUES); checkWorkingWithDefaultSchedule(); } @@ -795,8 +796,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { */ @Test public void checkPeriodicScanIntervalWhenDisconnectedWithNegativeValuesSchedule() { - when(mResource.getIntArray(R.array.config_wifiDisconnectedScanIntervalScheduleSec)) - .thenReturn(INVALID_SCHEDULE_NEGATIVE_VALUES); + mResources.setIntArray( + R.array.config_wifiDisconnectedScanIntervalScheduleSec, + INVALID_SCHEDULE_NEGATIVE_VALUES); checkWorkingWithDefaultSchedule(); } @@ -1006,9 +1008,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { currentTimeStamp += MAX_SCAN_INTERVAL_IN_SCHEDULE * 1000; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); - when(mResource.getIntArray(R.array - .config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec)) - .thenReturn(VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); + mResources.setIntArray( + R.array.config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec, + VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); @@ -1050,9 +1052,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { currentTimeStamp += MAX_SCAN_INTERVAL_IN_SCHEDULE * 1000; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); - when(mResource.getIntArray(R.array - .config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec)) - .thenReturn(VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); + mResources.setIntArray( + R.array.config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec, + VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.status = WifiConfiguration.Status.CURRENT; @@ -1092,9 +1094,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { currentTimeStamp += MAX_SCAN_INTERVAL_IN_SCHEDULE * 1000; when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp); - when(mResource.getIntArray(R.array - .config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec)) - .thenReturn(VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); + mResources.setIntArray( + R.array.config_wifiSingleSavedNetworkConnectedScanIntervalScheduleSec, + VALID_CONNECTED_SINGLE_SAVED_NETWORK_SCHEDULE); // Set firmware roaming to enabled when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(true); @@ -1919,9 +1921,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { */ @Test public void filterScanResultsWithOneRadioChainInfoForNetworkSelectionIfConfigDisabled() { - when(mResource.getBoolean( - R.bool.config_wifi_framework_use_single_radio_chain_scan_results_network_selection)) - .thenReturn(false); + mResources.setBoolean( + R.bool.config_wifi_framework_use_single_radio_chain_scan_results_network_selection, + false); when(mWifiNS.selectNetwork(any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean())) .thenReturn(null); mWifiConnectivityManager = createConnectivityManager(); @@ -1975,9 +1977,9 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { */ @Test public void dontFilterScanResultsWithOneRadioChainInfoForNetworkSelectionIfConfigEnabled() { - when(mResource.getBoolean( - R.bool.config_wifi_framework_use_single_radio_chain_scan_results_network_selection)) - .thenReturn(true); + mResources.setBoolean( + R.bool.config_wifi_framework_use_single_radio_chain_scan_results_network_selection, + true); when(mWifiNS.selectNetwork(any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean())) .thenReturn(null); mWifiConnectivityManager = createConnectivityManager(); @@ -2326,4 +2328,150 @@ public class WifiConnectivityManagerTest extends WifiBaseTest { mWifiConnectivityManager.forceConnectivityScan(WIFI_WORK_SOURCE); verify(mWifiScanner, times(2)).startScan(any(), any(), any(), any()); } + + /** + * Verify no network is network selection disabled, auto-join disabled using. + * {@link WifiConnectivityManager#retrievePnoNetworkList()}. + */ + @Test + public void testRetrievePnoList() { + // Create and add 3 networks. + WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); + WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); + WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); + List<WifiConfiguration> networkList = new ArrayList<>(); + networkList.add(network1); + networkList.add(network2); + networkList.add(network3); + when(mWifiConfigManager.getSavedNetworks(anyInt())).thenReturn(networkList); + // Retrieve the Pno network list & verify. + List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = + mWifiConnectivityManager.retrievePnoNetworkList(); + verify(mWifiNetworkSuggestionsManager).getAllPnoAvailableSuggestionNetworks(); + assertEquals(3, pnoNetworks.size()); + assertEquals(network1.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + assertEquals(network3.SSID, pnoNetworks.get(2).ssid); + + // Now permanently disable |network3|. This should remove network 3 from the list. + network3.getNetworkSelectionStatus().setNetworkSelectionStatus( + WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); + + // Retrieve the Pno network list & verify. + pnoNetworks = mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(2, pnoNetworks.size()); + assertEquals(network1.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + + // Now set network1 autojoin disabled. This should remove network 1 from the list. + network1.allowAutojoin = false; + // Retrieve the Pno network list & verify. + pnoNetworks = mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(1, pnoNetworks.size()); + assertEquals(network2.SSID, pnoNetworks.get(0).ssid); + } + + /** + * Verifies frequencies are populated correctly for pno networks. + * {@link WifiConnectivityManager#retrievePnoNetworkList()}. + */ + @Test + public void testRetrievePnoListFrequencies() { + // Create 2 networks. + WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); + WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); + List<WifiConfiguration> networkList = new ArrayList<>(); + networkList.add(network1); + networkList.add(network2); + when(mWifiConfigManager.getSavedNetworks(anyInt())).thenReturn(networkList); + + // Retrieve the Pno network list and verify. + // Frequencies should be empty since no scan results have been received yet. + List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = + mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(2, pnoNetworks.size()); + assertEquals(network1.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + assertEquals("frequencies should be empty", 0, pnoNetworks.get(0).frequencies.length); + assertEquals("frequencies should be empty", 0, pnoNetworks.get(1).frequencies.length); + + //Set up wifiScoreCard to get frequency. + List<Integer> channelList = new ArrayList<>(); + channelList.add(TEST_FREQUENCY_1); + channelList.add(TEST_FREQUENCY_2); + channelList.add(TEST_FREQUENCY_3); + when(mWifiScoreCard.lookupNetwork(network1.SSID)).thenReturn(mPerNetwork); + when(mWifiScoreCard.lookupNetwork(network2.SSID)).thenReturn(mPerNetwork1); + when(mPerNetwork.getFrequencies()).thenReturn(channelList); + when(mPerNetwork1.getFrequencies()).thenReturn(new ArrayList<>()); + + //Set config_wifiPnoFrequencyCullingEnabled false, should ignore get frequency. + mResources.setBoolean(R.bool.config_wifiPnoFrequencyCullingEnabled, false); + pnoNetworks = mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(2, pnoNetworks.size()); + assertEquals(network1.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + assertEquals("frequencies should be empty", 0, pnoNetworks.get(0).frequencies.length); + assertEquals("frequencies should be empty", 0, pnoNetworks.get(1).frequencies.length); + + ////Set config_wifiPnoFrequencyCullingEnabled false, should get the right frequency. + mResources.setBoolean(R.bool.config_wifiPnoFrequencyCullingEnabled, true); + pnoNetworks = mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(2, pnoNetworks.size()); + assertEquals(network1.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + assertEquals(3, pnoNetworks.get(0).frequencies.length); + Arrays.sort(pnoNetworks.get(0).frequencies); + assertEquals(TEST_FREQUENCY_1, pnoNetworks.get(0).frequencies[0]); + assertEquals(TEST_FREQUENCY_2, pnoNetworks.get(0).frequencies[1]); + assertEquals(TEST_FREQUENCY_3, pnoNetworks.get(0).frequencies[2]); + assertEquals("frequencies should be empty", 0, pnoNetworks.get(1).frequencies.length); + } + + + /** + * Verifies the ordering of network list generated using + * {@link WifiConnectivityManager#retrievePnoNetworkList()}. + */ + @Test + public void testRetrievePnoListOrder() { + //Create 3 networks. + WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(); + WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); + WifiConfiguration network3 = WifiConfigurationTestUtil.createOpenHiddenNetwork(); + List<WifiConfiguration> networkList = new ArrayList<>(); + networkList.add(network1); + networkList.add(network2); + networkList.add(network3); + when(mWifiConfigManager.getSavedNetworks(anyInt())).thenReturn(networkList); + // Set up network as: + // Network2 has the highest association number. + // Network3 is most recently connected. + // Network1 the second for both association number and most recently. + long firstConnectionTimeMillis = 45677; + long secondConnectionTimeMillis = firstConnectionTimeMillis + 45; + network1.numAssociation = 2; + network2.numAssociation = 3; + network3.numAssociation = 1; + network1.lastConnected = firstConnectionTimeMillis; + network3.lastConnected = secondConnectionTimeMillis; + //When config_wifiPnoRecencySortingEnabled is false, will prefer most connected network. + mResources.setBoolean(R.bool.config_wifiPnoRecencySortingEnabled, false); + List<WifiScanner.PnoSettings.PnoNetwork> pnoNetworks = + mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(3, pnoNetworks.size()); + assertEquals(network2.SSID, pnoNetworks.get(0).ssid); + assertEquals(network1.SSID, pnoNetworks.get(1).ssid); + assertEquals(network3.SSID, pnoNetworks.get(2).ssid); + + //When config_wifiPnoRecencySortingEnabled is true, after prefer most connected network. + //The most recently connected network will move the first. + mResources.setBoolean(R.bool.config_wifiPnoRecencySortingEnabled, true); + pnoNetworks = + mWifiConnectivityManager.retrievePnoNetworkList(); + assertEquals(3, pnoNetworks.size()); + assertEquals(network3.SSID, pnoNetworks.get(0).ssid); + assertEquals(network2.SSID, pnoNetworks.get(1).ssid); + assertEquals(network1.SSID, pnoNetworks.get(2).ssid); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index 6c19cb38b..468781f6e 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -3317,6 +3317,38 @@ public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { } /** + * Verify getAllPnoAvailableSuggestionNetworks will only return user approved, + * non-passpoint network. + */ + @Test + public void testGetPnoAvailableSuggestions() { + WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); + WifiConfiguration network2 = WifiConfigurationTestUtil.createOpenNetwork(); + PasspointConfiguration passpointConfiguration = + createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); + WifiConfiguration dummyConfig = new WifiConfiguration(); + dummyConfig.FQDN = TEST_FQDN; + WifiNetworkSuggestion networkSuggestion = + new WifiNetworkSuggestion(network1, null, false, false, true, true, false); + WifiNetworkSuggestion passpointSuggestion = new WifiNetworkSuggestion(dummyConfig, + passpointConfiguration, false, false, true, true, false); + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + add(passpointSuggestion); + }}; + assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, + mWifiNetworkSuggestionsManager + .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); + assertTrue(mWifiNetworkSuggestionsManager.getAllPnoAvailableSuggestionNetworks().isEmpty()); + mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_PACKAGE_1); + List<WifiConfiguration> pnoNetwork = + mWifiNetworkSuggestionsManager.getAllPnoAvailableSuggestionNetworks(); + assertEquals(1, pnoNetwork.size()); + assertEquals(network1.SSID, pnoNetwork.get(0).SSID); + } + + /** * Helper function for creating a test configuration with user credential. * * @return {@link PasspointConfiguration} |