diff options
Diffstat (limited to 'tests')
6 files changed, 1616 insertions, 2315 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/ExternalScoreEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/ExternalScoreEvaluatorTest.java new file mode 100644 index 000000000..16bd4f8d8 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/ExternalScoreEvaluatorTest.java @@ -0,0 +1,419 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.app.test.MockAnswerUtil.AnswerWithArguments; +import android.content.Context; +import android.content.res.Resources; +import android.net.INetworkScoreCache; +import android.net.NetworkScoreManager; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; +import android.os.SystemClock; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.internal.R; +import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.ExternalScoreEvaluator}. + */ +@SmallTest +public class ExternalScoreEvaluatorTest { + + /** Sets up test. */ + @Before + public void setUp() throws Exception { + mResource = getResource(); + mScoreManager = getScoreManager(); + mContext = getContext(); + mWifiConfigManager = getWifiConfigManager(); + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); + + mThresholdQualifiedRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); + mThresholdQualifiedRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); + + mExternalScoreEvaluator = new ExternalScoreEvaluator(mContext, mWifiConfigManager, + mClock, null); + } + + /** Cleans up test. */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + private ExternalScoreEvaluator mExternalScoreEvaluator; + private WifiConfigManager mWifiConfigManager; + private Context mContext; + private Resources mResource; + private NetworkScoreManager mScoreManager; + private WifiNetworkScoreCache mScoreCache; + private Clock mClock = mock(Clock.class); + private int mThresholdQualifiedRssi2G; + private int mThresholdQualifiedRssi5G; + private static final String TAG = "External Score Evaluator Unit Test"; + + NetworkScoreManager getScoreManager() { + NetworkScoreManager scoreManager = mock(NetworkScoreManager.class); + + doAnswer(new AnswerWithArguments() { + public void answer(int networkType, INetworkScoreCache scoreCache) { + mScoreCache = (WifiNetworkScoreCache) scoreCache; + }}).when(scoreManager).registerNetworkScoreCache(anyInt(), anyObject()); + + return scoreManager; + } + + Context getContext() { + Context context = mock(Context.class); + + when(context.getResources()).thenReturn(mResource); + when(context.getSystemService(Context.NETWORK_SCORE_SERVICE)).thenReturn(mScoreManager); + + return context; + } + + Resources getResource() { + Resources resource = mock(Resources.class); + + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz)) + .thenReturn(-70); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz)) + .thenReturn(-73); + + return resource; + } + + WifiConfigManager getWifiConfigManager() { + WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); + when(wifiConfigManager.getLastSelectedNetwork()) + .thenReturn(WifiConfiguration.INVALID_NETWORK_ID); + return wifiConfigManager; + } + + + /** + * When no saved networks available, choose the available ephemeral networks + * if untrusted networks are allowed. + */ + @Test + public void chooseEphemeralNetworkBecauseOfNoSavedNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + Integer[] scores = {null, 120}; + boolean[] meteredHints = {false, true}; + + List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( + ssids, bssids, freqs, caps, levels, mClock); + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + // No saved networks. + when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(any(ScanDetail.class))) + .thenReturn(null); + + ScanResult scanResult = scanDetails.get(1).getScanResult(); + WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil + .setupEphemeralNetwork(mWifiConfigManager, 1, scanResult, meteredHints[1]); + + // Untrusted networks allowed. + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfig, candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + scanResult, candidate); + assertEquals(meteredHints[1], candidate.meteredHint); + } + + /** + * When no saved networks available, choose the highest scored ephemeral networks + * if untrusted networks are allowed. + */ + @Test + public void chooseHigherScoredEphemeralNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[ESS]", "[ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; + Integer[] scores = {100, 120}; + boolean[] meteredHints = {true, true}; + ScanResult[] scanResults = new ScanResult[2]; + WifiConfiguration[] ephemeralNetworkConfigs = new WifiConfiguration[2]; + + List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( + ssids, bssids, freqs, caps, levels, mClock); + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + // No saved networks. + when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(any(ScanDetail.class))) + .thenReturn(null); + + for (int i = 0; i < 2; i++) { + scanResults[i] = scanDetails.get(i).getScanResult(); + ephemeralNetworkConfigs[i] = WifiNetworkSelectorTestUtil + .setupEphemeralNetwork(mWifiConfigManager, i, scanResults[i], meteredHints[i]); + } + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + scanResults[1], candidate); + assertEquals(meteredHints[1], candidate.meteredHint); + } + + /** + * Don't choose available ephemeral networks if no saved networks and untrusted networks + * are not allowed. + */ + @Test + public void noEphemeralNetworkWhenUntrustedNetworksNotAllowed() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + Integer[] scores = {null, 120}; + boolean[] meteredHints = {false, true}; + + List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( + ssids, bssids, freqs, caps, levels, mClock); + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + // No saved networks. + when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(any(ScanDetail.class))) + .thenReturn(null); + + ScanResult scanResult = scanDetails.get(1).getScanResult(); + WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil + .setupEphemeralNetwork(mWifiConfigManager, 1, scanResult, meteredHints[1]); + + // Untursted networks not allowed. + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, false, null); + + assertEquals("Expect null configuration", null, candidate); + } + + + /** + * Choose externally scored saved network. + */ + @Test + public void chooseSavedNetworkWithExternalScore() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {5200}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]"}; + int[] securities = {SECURITY_PSK}; + int[] levels = {mThresholdQualifiedRssi5G + 8}; + Integer[] scores = {120}; + boolean[] meteredHints = {false}; + + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].useExternalScores = true; + + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + scanDetails.get(0).getScanResult(), candidate); + } + + /** + * Choose externally scored saved network with higher score. + */ + @Test + public void chooseSavedNetworkWithHigherExternalScore() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; + Integer[] scores = {100, 120}; + boolean[] meteredHints = {false, false}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].useExternalScores = savedConfigs[1].useExternalScores = true; + + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + scanDetails.get(1).getScanResult(), candidate); + } + + /** + * Prefer externally scored saved network over untrusted network when they have + * the same score. + */ + @Test + public void chooseExternallyScoredSavedNetworkOverUntrustedNetworksWithSameScore() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; + int[] securities = {SECURITY_PSK, SECURITY_NONE}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; + Integer[] scores = {120, 120}; + boolean[] meteredHints = {false, true}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].useExternalScores = true; + + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + scanDetails.get(0).getScanResult(), candidate); + } + + /** + * Choose untrusted network when it has higher score than the externally scored + * saved network. + */ + @Test + public void chooseUntrustedNetworkWithHigherScoreThanExternallyScoredSavedNetwork() { + // Saved network. + String[] savedSsids = {"\"test1\""}; + String[] savedBssids = {"6c:f3:7f:ae:8c:f3"}; + int[] savedFreqs = {2470}; + String[] savedCaps = {"[WPA2-EAP-CCMP][ESS]"}; + int[] savedSecurities = {SECURITY_PSK}; + int[] savedLevels = {mThresholdQualifiedRssi2G + 8}; + // Ephemeral network. + String[] ephemeralSsids = {"\"test2\""}; + String[] ephemeralBssids = {"6c:f3:7f:ae:8c:f4"}; + int[] ephemeralFreqs = {2437}; + String[] ephemeralCaps = {"[ESS]"}; + int[] ephemeralLevels = {mThresholdQualifiedRssi2G + 8}; + // Ephemeral network has higher score than the saved network. + Integer[] scores = {100, 120}; + boolean[] meteredHints = {false, true}; + + // Set up the saved network. + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(savedSsids, + savedBssids, savedFreqs, savedCaps, savedLevels, savedSecurities, + mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].useExternalScores = true; + + // Set up the ephemeral network. + scanDetails.addAll(WifiNetworkSelectorTestUtil.buildScanDetails( + ephemeralSsids, ephemeralBssids, ephemeralFreqs, + ephemeralCaps, ephemeralLevels, mClock)); + ScanResult ephemeralScanResult = scanDetails.get(1).getScanResult(); + WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil + .setupEphemeralNetwork(mWifiConfigManager, 1, ephemeralScanResult, + meteredHints[1]); + + // Set up score cache for both the saved network and the ephemeral network. + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfig, candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + ephemeralScanResult, candidate); + } + + /** + * Prefer externally scored saved network over untrusted network when they have + * the same score. + */ + @Test + public void nullScoredNetworks() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; + int[] securities = {SECURITY_PSK, SECURITY_NONE}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; + Integer[] scores = {null, null}; + boolean[] meteredHints = {false, true}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + savedConfigs[0].useExternalScores = true; + + WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, + scanDetails, scores, meteredHints); + + WifiConfiguration candidate = mExternalScoreEvaluator.evaluateNetworks(scanDetails, + null, null, false, true, null); + + assertEquals("Expect null configuration", null, candidate); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java new file mode 100644 index 000000000..fe290cd8a --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -0,0 +1,323 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.content.Context; +import android.content.res.Resources; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; +import android.os.SystemClock; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.internal.R; +import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.SavedNetworkEvaluator}. + */ +@SmallTest +public class SavedNetworkEvaluatorTest { + + /** Sets up test. */ + @Before + public void setUp() throws Exception { + mResource = getResource(); + mContext = getContext(); + mWifiConfigManager = getWifiConfigManager(); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); + + mThresholdMinimumRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); + mThresholdMinimumRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); + mThresholdQualifiedRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); + mThresholdQualifiedRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); + mThresholdSaturatedRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz); + mThresholdSaturatedRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz); + + mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mWifiConfigManager, + mClock, null); + } + + /** Cleans up test. */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + private SavedNetworkEvaluator mSavedNetworkEvaluator; + private WifiConfigManager mWifiConfigManager; + private Context mContext; + private Resources mResource; + private Clock mClock = mock(Clock.class); + private int mThresholdMinimumRssi2G; + private int mThresholdMinimumRssi5G; + private int mThresholdQualifiedRssi2G; + private int mThresholdQualifiedRssi5G; + private int mThresholdSaturatedRssi2G; + private int mThresholdSaturatedRssi5G; + private static final String TAG = "Saved Network Evaluator Unit Test"; + + Context getContext() { + Context context = mock(Context.class); + Resources resource = mock(Resources.class); + + when(context.getResources()).thenReturn(mResource); + return context; + } + + Resources getResource() { + Resources resource = mock(Resources.class); + + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz)) + .thenReturn(-70); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz)) + .thenReturn(-73); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz)) + .thenReturn(-70); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz)) + .thenReturn(-73); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz)) + .thenReturn(-82); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz)) + .thenReturn(-85); + when(resource.getInteger( + R.integer.config_wifi_framework_RSSI_SCORE_SLOPE)) + .thenReturn(4); + when(resource.getInteger( + R.integer.config_wifi_framework_RSSI_SCORE_OFFSET)) + .thenReturn(85); + when(resource.getInteger( + R.integer.config_wifi_framework_SAME_BSSID_AWARD)) + .thenReturn(24); + when(resource.getInteger( + R.integer.config_wifi_framework_SECURITY_AWARD)) + .thenReturn(80); + when(resource.getInteger( + R.integer.config_wifi_framework_5GHz_preference_boost_factor)) + .thenReturn(16); + when(resource.getInteger( + R.integer.config_wifi_framework_current_network_boost)) + .thenReturn(16); + + return resource; + } + + WifiConfigManager getWifiConfigManager() { + WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); + when(wifiConfigManager.getLastSelectedNetwork()) + .thenReturn(WifiConfiguration.INVALID_NETWORK_ID); + return wifiConfigManager; + } + + + /** + * Between two 2G networks, choose the one with stronger RSSI value if other conditions + * are the same and the RSSI values are not satuarted. + */ + @Test + public void chooseStrongerRssi2GNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2470, 2437}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, null); + + ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * Between two 5G networks, choose the one with stronger RSSI value if other conditions + * are the same and the RSSI values are not satuarted. + */ + @Test + public void chooseStrongerRssi5GNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {5200, 5240}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi5G + 8, mThresholdQualifiedRssi5G + 10}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, null); + + ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * Choose secure network over open network if other conditions are the same. + */ + @Test + public void chooseSecureNetworkOverOpenNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {5200, 5240}; + String[] caps = {"[ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi5G, mThresholdQualifiedRssi5G}; + int[] securities = {SECURITY_NONE, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, null); + + ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * Choose 5G network over 2G network if other conditions are the same. + */ + @Test + public void choose5GNetworkOver2GNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2437, 5240}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G, mThresholdQualifiedRssi5G}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, null); + + ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * Verify that we stick to the currently connected network if the other one is + * just slightly better scored. + */ + @Test + public void stickToCurrentNetwork() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {5200, 5240}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + // test2 has slightly stronger RSSI value than test1 + int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 4}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + // Simuluate we are connected to SSID test1 already. + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + savedConfigs[0], null, true, false, null); + + // Even though test2 has higher RSSI value, test1 is chosen because of the + // currently connected network bonus. + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * Verify that we stick to the currently connected BSSID if the other one is + * just slightly better scored. + */ + @Test + public void stickToCurrentBSSID() { + // Same SSID + String[] ssids = {"\"test1\"", "\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {5200, 5240}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + // test2 has slightly stronger RSSI value than test1 + int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 6}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + // Simuluate we are connected to BSSID "6c:f3:7f:ae:8c:f3" already + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, bssids[0], true, false, null); + + // Even though test2 has higher RSSI value, test1 is chosen because of the + // currently connected BSSID bonus. + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 84133acb7..c952f65ff 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -72,7 +72,7 @@ public class WifiConnectivityManagerTest { mWifiConfigManager = mockWifiConfigManager(); mWifiInfo = getWifiInfo(); mWifiScanner = mockWifiScanner(); - mWifiQNS = mockWifiQualifiedNetworkSelector(); + mWifiNS = mockWifiNetworkSelector(); mWifiConnectivityManager = createConnectivityManager(); mWifiConnectivityManager.setWifiEnabled(true); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); @@ -91,7 +91,7 @@ public class WifiConnectivityManagerTest { private TestAlarmManager mAlarmManager; private TestLooper mLooper = new TestLooper(); private WifiConnectivityManager mWifiConnectivityManager; - private WifiQualifiedNetworkSelector mWifiQNS; + private WifiNetworkSelector mWifiNS; private WifiStateMachine mWifiStateMachine; private WifiScanner mWifiScanner; private WifiConfigManager mWifiConfigManager; @@ -117,10 +117,10 @@ public class WifiConnectivityManagerTest { 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(WifiQualifiedNetworkSelector.RSSI_SATURATION_2G_BAND); + .thenReturn(-60); when(resource.getInteger( R.integer.config_wifi_framework_current_network_boost)) - .thenReturn(WifiQualifiedNetworkSelector.SAME_NETWORK_AWARD); + .thenReturn(16); return resource; } @@ -197,8 +197,8 @@ public class WifiConnectivityManagerTest { return stateMachine; } - WifiQualifiedNetworkSelector mockWifiQualifiedNetworkSelector() { - WifiQualifiedNetworkSelector qns = mock(WifiQualifiedNetworkSelector.class); + WifiNetworkSelector mockWifiNetworkSelector() { + WifiNetworkSelector ns = mock(WifiNetworkSelector.class); WifiConfiguration candidate = generateWifiConfig( 0, CANDIDATE_NETWORK_ID, CANDIDATE_SSID, false, true, null, null); @@ -208,9 +208,9 @@ public class WifiConnectivityManagerTest { candidateScanResult.BSSID = CANDIDATE_BSSID; candidate.getNetworkSelectionStatus().setCandidate(candidateScanResult); - when(qns.selectQualifiedNetwork(anyBoolean(), anyBoolean(), anyBoolean(), - anyBoolean(), anyBoolean(), anyBoolean(), anyObject())).thenReturn(candidate); - return qns; + when(ns.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + anyBoolean())).thenReturn(candidate); + return ns; } WifiInfo getWifiInfo() { @@ -250,7 +250,7 @@ public class WifiConnectivityManagerTest { WifiConnectivityManager createConnectivityManager() { return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, - mWifiConfigManager, mWifiInfo, mWifiQNS, mWifiInjector, mLooper.getLooper(), true); + mWifiConfigManager, mWifiInfo, mWifiNS, mWifiInjector, mLooper.getLooper(), true); } /** @@ -490,9 +490,9 @@ public class WifiConnectivityManagerTest { * because of their low RSSI values. */ @Test - public void PnoRetryForLowRssiNetwork() { - when(mWifiQNS.selectQualifiedNetwork(anyBoolean(), anyBoolean(), anyBoolean(), - anyBoolean(), anyBoolean(), anyBoolean(), anyObject())).thenReturn(null); + public void pnoRetryForLowRssiNetwork() { + when(mWifiNS.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + anyBoolean())).thenReturn(null); // Set screen to off mWifiConnectivityManager.handleScreenStateChanged(false); @@ -511,7 +511,7 @@ public class WifiConnectivityManagerTest { .getLowRssiNetworkRetryDelay(); assertEquals(lowRssiNetworkRetryDelayStartValue * 2, - lowRssiNetworkRetryDelayAfterPnoValue); + lowRssiNetworkRetryDelayAfterPnoValue); } /** @@ -546,8 +546,8 @@ public class WifiConnectivityManagerTest { @Test public void watchdogBitePnoGoodIncrementsMetrics() { // Qns returns no candidate after watchdog single scan. - when(mWifiQNS.selectQualifiedNetwork(anyBoolean(), anyBoolean(), anyBoolean(), - anyBoolean(), anyBoolean(), anyBoolean(), anyObject())).thenReturn(null); + when(mWifiNS.selectNetwork(anyObject(), anyBoolean(), anyBoolean(), + anyBoolean())).thenReturn(null); // Set screen to off mWifiConnectivityManager.handleScreenStateChanged(false); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java new file mode 100644 index 000000000..a03e17457 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java @@ -0,0 +1,491 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; +import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.content.Context; +import android.content.res.Resources; +import android.net.NetworkScoreManager; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import android.os.SystemClock; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.Pair; + +import com.android.internal.R; +import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.WifiNetworkSelector}. + */ +@SmallTest +public class WifiNetworkSelectorTest { + + /** Sets up test. */ + @Before + public void setUp() throws Exception { + mResource = getResource(); + mContext = getContext(); + mWifiConfigManager = getWifiConfigManager(); + mWifiInfo = getWifiInfo(); + + mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, + mWifiInfo, mClock); + mWifiNetworkSelector.registerNetworkEvaluator(mDummyEvaluator, 1); + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); + + mThresholdMinimumRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); + mThresholdMinimumRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); + mThresholdQualifiedRssi2G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); + mThresholdQualifiedRssi5G = mResource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); + } + + /** Cleans up test. */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + /** + * All this dummy network evaluator does is to pick the very first network + * in the scan results. + */ + public class DummyNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluator { + private static final String NAME = "DummyNetworkEvaluator"; + private WifiConfigManager mConfigManager; + + /** + * Get the evaluator name. + */ + public String getName() { + return NAME; + } + + /** + * Update thee evaluator. + */ + public void update(List<ScanDetail> scanDetails) { + } + + /** + * Always return the first network in the scan results for connection. + */ + public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + WifiConfiguration currentNetwork, String currentBssid, boolean connected, + boolean untrustedNetworkAllowed, + List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks) { + ScanDetail scanDetail = scanDetails.get(0); + mWifiConfigManager.setNetworkCandidateScanResult(0, scanDetail.getScanResult(), 100); + + return mWifiConfigManager.getSavedNetworkForScanDetailAndCache(scanDetail); + } + } + + private WifiNetworkSelector mWifiNetworkSelector = null; + private DummyNetworkEvaluator mDummyEvaluator = new DummyNetworkEvaluator(); + private WifiConfigManager mWifiConfigManager = null; + private Context mContext; + private Resources mResource; + private WifiInfo mWifiInfo; + private Clock mClock = mock(Clock.class); + private int mThresholdMinimumRssi2G; + private int mThresholdMinimumRssi5G; + private int mThresholdQualifiedRssi2G; + private int mThresholdQualifiedRssi5G; + + Context getContext() { + Context context = mock(Context.class); + Resources resource = mock(Resources.class); + + when(context.getResources()).thenReturn(mResource); + return context; + } + + Resources getResource() { + 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_low_rssi_threshold_5GHz)) + .thenReturn(-70); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz)) + .thenReturn(-73); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz)) + .thenReturn(-82); + when(resource.getInteger( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz)) + .thenReturn(-85); + return resource; + } + + NetworkScoreManager getNetworkScoreManager() { + NetworkScoreManager networkScoreManager = mock(NetworkScoreManager.class); + + return networkScoreManager; + } + + WifiInfo getWifiInfo() { + WifiInfo wifiInfo = mock(WifiInfo.class); + + // simulate a disconnected state + when(wifiInfo.is24GHz()).thenReturn(true); + when(wifiInfo.is5GHz()).thenReturn(false); + when(wifiInfo.getRssi()).thenReturn(-70); + when(wifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID); + when(wifiInfo.getBSSID()).thenReturn(null); + return wifiInfo; + } + + WifiConfigManager getWifiConfigManager() { + WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); + when(wifiConfigManager.getLastSelectedNetwork()) + .thenReturn(WifiConfiguration.INVALID_NETWORK_ID); + return wifiConfigManager; + } + + + /** + * No network selection if scan result is empty. + * + * WifiStateMachine is in disconnected state. + * scanDetails is empty. + * + * Expected behavior: no network recommended by Network Selector + */ + @Test + public void emptyScanResults() { + String[] ssids = new String[0]; + String[] bssids = new String[0]; + int[] freqs = new int[0]; + String[] caps = new String[0]; + int[] levels = new int[0]; + int[] securities = new int[0]; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + false, true, false); + assertEquals("Expect null configuration", null, candidate); + } + + + /** + * No network selection if the RSSI values in scan result are too low. + * + * WifiStateMachine is in disconnected state. + * scanDetails contains a 2.4GHz and a 5GHz network, but both with RSSI lower than + * the threshold + * + * Expected behavior: no network recommended by Network Selector + */ + @Test + public void verifyMinimumRssiThreshold() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2437, 5180}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdMinimumRssi2G - 1, mThresholdMinimumRssi5G - 1}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + false, true, false); + assertEquals("Expect null configuration", null, candidate); + } + + /** + * No network selection if WiFi is connected and it is too short from last + * network selection. + * + * WifiStateMachine is in connected state. + * scanDetails contains two valid networks. + * Perform a network seletion right after the first one. + * + * Expected behavior: no network recommended by Network Selector + */ + @Test + public void verifyMinimumTimeGapWhenConnected() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2437, 5180}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + // Make a network selection. + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + false, true, false); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS - 2000); + + // Do another network selection with WSM in CONNECTED state. + candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + true, false, false); + + assertEquals("Expect null configuration", null, candidate); + } + + /** + * Perform network selection if WiFi is disconnected even if it is too short from last + * network selection. + * + * WifiStateMachine is in disconnected state. + * scanDetails contains two valid networks. + * Perform a network seletion right after the first one. + * + * Expected behavior: the first network is recommended by Network Selector + */ + @Test + public void verifyNoMinimumTimeGapWhenDisconnected() { + String[] ssids = {"\"test1\"", "\"test2\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; + int[] freqs = {2437, 5180}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdMinimumRssi2G + 1, mThresholdMinimumRssi5G + 1}; + int[] securities = {SECURITY_PSK, SECURITY_PSK}; + + // Make a network selection. + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + false, true, false); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS - 2000); + + // Do another network selection with WSM in DISCONNECTED state. + candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + false, true, false); + + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * No network selection if the currently connected on is already sufficient. + * + * WifiStateMachine is connected to a qualified (5G, secure, good RSSI) network. + * scanDetails contains a valid network. + * Perform a network seletion after the first one. + * + * Expected behavior: no network recommended by Network Selector + */ + @Test + public void noNetworkSelectionWhenCurrentOneIsSufficient() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {5180}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi5G + 8}; + int[] securities = {SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + + // connect to test1 + mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + when(mWifiInfo.getNetworkId()).thenReturn(0); + when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); + when(mWifiInfo.is24GHz()).thenReturn(false); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); + + levels[0] = mThresholdQualifiedRssi5G + 20; + scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + scanDetails = scanDetailsAndConfigs.getScanDetails(); + + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + true, false, false); + assertEquals("Expect null configuration", null, candidate); + } + + + /** + * New network selection is performed if the currently connected network + * band is 2G. + * + * WifiStateMachine is connected to a 2G network. + * scanDetails contains a valid networks. + * Perform a network seletion after the first one. + * + * Expected behavior: the first network is recommended by Network Selector + */ + @Test + public void band2GNetworkIsNotSufficient() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {2470}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi2G + 8}; + int[] securities = {SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + // connect to test1 + mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + when(mWifiInfo.getNetworkId()).thenReturn(0); + when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); + when(mWifiInfo.is24GHz()).thenReturn(true); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); + + // Do another network selection. + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + true, false, false); + + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + + /** + * New network selection is performed if the currently connected network + * is a open one. + * + * WifiStateMachine is connected to a open network. + * scanDetails contains a valid networks. + * Perform a network seletion after the first one. + * + * Expected behavior: the first network is recommended by Network Selector + */ + @Test + public void openNetworkIsNotSufficient() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {5180}; + String[] caps = {"[ESS]"}; + int[] levels = {mThresholdQualifiedRssi5G + 8}; + int[] securities = {SECURITY_NONE}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + // connect to test1 + mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + when(mWifiInfo.getNetworkId()).thenReturn(0); + when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); + when(mWifiInfo.is24GHz()).thenReturn(false); + when(mWifiInfo.is5GHz()).thenReturn(true); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); + + // Do another network selection. + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + true, false, false); + + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } + + /** + * New network selection is performed if the currently connected network + * has low RSSI value. + * + * WifiStateMachine is connected to a low RSSI 5GHz network. + * scanDetails contains a valid networks. + * Perform a network seletion after the first one. + * + * Expected behavior: the first network is recommended by Network Selector + */ + @Test + public void lowRssi5GNetworkIsNotSufficient() { + String[] ssids = {"\"test1\""}; + String[] bssids = {"6c:f3:7f:ae:8c:f3"}; + int[] freqs = {5180}; + String[] caps = {"[WPA2-EAP-CCMP][ESS]"}; + int[] levels = {mThresholdQualifiedRssi5G - 2}; + int[] securities = {SECURITY_PSK}; + + ScanDetailsAndWifiConfigs scanDetailsAndConfigs = + WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, + freqs, caps, levels, securities, mWifiConfigManager, mClock); + List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); + WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); + + // connect to test1 + mWifiNetworkSelector.selectNetwork(scanDetails, false, true, false); + when(mWifiInfo.getNetworkId()).thenReturn(0); + when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); + when(mWifiInfo.is24GHz()).thenReturn(false); + when(mWifiInfo.is5GHz()).thenReturn(true); + when(mWifiInfo.getRssi()).thenReturn(levels[0]); + + when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); + + // Do another network selection. + WifiConfiguration candidate = mWifiNetworkSelector.selectNetwork(scanDetails, + true, false, false); + + ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); + WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, + chosenScanResult, candidate); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java new file mode 100644 index 000000000..db3aff14c --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTestUtil.java @@ -0,0 +1,367 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static com.android.server.wifi.WifiConfigurationTestUtil.generateWifiConfig; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.app.test.MockAnswerUtil.AnswerWithArguments; +import android.net.NetworkKey; +import android.net.RssiCurve; +import android.net.ScoredNetwork; +import android.net.WifiKey; +import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; +import android.net.wifi.WifiSsid; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.TextUtils; + +import com.android.server.wifi.util.ScanResultUtil; + +import java.util.ArrayList; +import java.util.List; + +/** + * Helper for WifiNetworkSelector unit tests. + */ +@SmallTest +public class WifiNetworkSelectorTestUtil { + + /** + * A class that holds a list of scanDetail and their associated WifiConfiguration. + */ + public static class ScanDetailsAndWifiConfigs { + List<ScanDetail> mScanDetails; + WifiConfiguration[] mWifiConfigs; + + ScanDetailsAndWifiConfigs(List<ScanDetail> scanDetails, WifiConfiguration[] configs) { + mScanDetails = scanDetails; + mWifiConfigs = configs; + } + + List<ScanDetail> getScanDetails() { + return mScanDetails; + } + + WifiConfiguration[] getWifiConfigs() { + return mWifiConfigs; + } + } + + /** + * Build a list of ScanDetail based on the caller supplied network SSID, BSSID, + * frequency, capability and RSSI level information. Create the corresponding + * WifiConfiguration for these networks and set up the mocked WifiConfigManager. + * + * @param ssids an array of SSIDs + * @param bssids an array of BSSIDs + * @param freqs an array of the network's frequency + * @param caps an array of the network's capability + * @param levels an array of the network's RSSI levels + * @param securities an array of the network's security setting + * @param wifiConfigManager the mocked WifiConfigManager + * @return the constructed ScanDetail list and WifiConfiguration array + */ + public static ScanDetailsAndWifiConfigs setupScanDetailsAndConfigStore(String[] ssids, + String[] bssids, int[] freqs, String[] caps, int[] levels, int[] securities, + WifiConfigManager wifiConfigManager, Clock clock) { + List<ScanDetail> scanDetails = buildScanDetails(ssids, bssids, freqs, caps, levels, clock); + WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, securities); + prepareConfigStore(wifiConfigManager, savedConfigs); + scanResultLinkConfiguration(wifiConfigManager, savedConfigs, scanDetails); + + return new ScanDetailsAndWifiConfigs(scanDetails, savedConfigs); + } + + /** + * Verify whether the WifiConfiguration chosen by WifiNetworkSelector matches + * with the chosen scan result. + * + * @param chosenScanResult the chosen scan result + * @param chosenCandidate the chosen configuration + */ + public static void verifySelectedScanResult(WifiConfigManager wifiConfigManager, + ScanResult chosenScanResult, WifiConfiguration chosenCandidate) { + verify(wifiConfigManager, atLeastOnce()).setNetworkCandidateScanResult( + eq(chosenCandidate.networkId), eq(chosenScanResult), anyInt()); + } + + + /** + * Build a list of scanDetails based on the caller supplied network SSID, BSSID, + * frequency, capability and RSSI level information. + * + * @param ssids an array of SSIDs + * @param bssids an array of BSSIDs + * @param freqs an array of the network's frequency + * @param caps an array of the network's capability + * @param levels an array of the network's RSSI levels + * @return the constructed list of ScanDetail + */ + public static List<ScanDetail> buildScanDetails(String[] ssids, String[] bssids, int[] freqs, + String[] caps, int[] levels, Clock clock) { + List<ScanDetail> scanDetailList = new ArrayList<ScanDetail>(); + + long timeStamp = clock.getElapsedSinceBootMillis(); + for (int index = 0; index < ssids.length; index++) { + ScanDetail scanDetail = new ScanDetail(WifiSsid.createFromAsciiEncoded(ssids[index]), + bssids[index], caps[index], levels[index], freqs[index], timeStamp, 0); + scanDetailList.add(scanDetail); + } + return scanDetailList; + } + + + /** + * Generate an array of {@link android.net.wifi.WifiConfiguration} based on the caller + * supplied network SSID and sencurity information. + * + * @param ssids an array of SSIDs + * @param securities an array of the network's security setting + * @return the constructed array of {@link android.net.wifi.WifiConfiguration} + */ + public static WifiConfiguration[] generateWifiConfigurations(String[] ssids, + int[] securities) { + if (ssids == null || securities == null || ssids.length != securities.length + || ssids.length == 0) { + return null; + } + + WifiConfiguration[] configs = new WifiConfiguration[ssids.length]; + for (int index = 0; index < ssids.length; index++) { + configs[index] = generateWifiConfig(index, 0, ssids[index], false, true, null, null, + securities[index]); + } + + return configs; + } + + /** + * Add the Configurations to WifiConfigManager (WifiConfigureStore can take them out according + * to the networkd ID) and setup the WifiConfigManager mocks for these networks. + * This simulates the WifiConfigManager class behaviour. + * + * @param wifiConfigManager the mocked WifiConfigManager + * @param configs input configuration need to be added to WifiConfigureStore + */ + private static void prepareConfigStore(WifiConfigManager wifiConfigManager, + final WifiConfiguration[] configs) { + when(wifiConfigManager.getConfiguredNetwork(anyInt())) + .then(new AnswerWithArguments() { + public WifiConfiguration answer(int netId) { + if (netId >= 0 && netId < configs.length) { + return new WifiConfiguration(configs[netId]); + } else { + return null; + } + } + }); + when(wifiConfigManager.getConfiguredNetwork(anyString())) + .then(new AnswerWithArguments() { + public WifiConfiguration answer(String configKey) { + for (int netId = 0; netId < configs.length; netId++) { + if (TextUtils.equals(configs[netId].configKey(), configKey)) { + return new WifiConfiguration(configs[netId]); + } + } + return null; + } + }); + when(wifiConfigManager.getSavedNetworks()) + .then(new AnswerWithArguments() { + public List<WifiConfiguration> answer() { + List<WifiConfiguration> savedNetworks = new ArrayList<>(); + for (int netId = 0; netId < configs.length; netId++) { + savedNetworks.add(new WifiConfiguration(configs[netId])); + } + return savedNetworks; + } + }); + when(wifiConfigManager.clearNetworkCandidateScanResult(anyInt())) + .then(new AnswerWithArguments() { + public boolean answer(int netId) { + if (netId >= 0 && netId < configs.length) { + configs[netId].getNetworkSelectionStatus().setCandidate(null); + configs[netId].getNetworkSelectionStatus() + .setCandidateScore(Integer.MIN_VALUE); + configs[netId].getNetworkSelectionStatus() + .setSeenInLastQualifiedNetworkSelection(false); + return true; + } else { + return false; + } + } + }); + when(wifiConfigManager.setNetworkCandidateScanResult( + anyInt(), any(ScanResult.class), anyInt())) + .then(new AnswerWithArguments() { + public boolean answer(int netId, ScanResult scanResult, int score) { + if (netId >= 0 && netId < configs.length) { + configs[netId].getNetworkSelectionStatus().setCandidate(scanResult); + configs[netId].getNetworkSelectionStatus().setCandidateScore(score); + configs[netId].getNetworkSelectionStatus() + .setSeenInLastQualifiedNetworkSelection(true); + return true; + } else { + return false; + } + } + }); + when(wifiConfigManager.clearNetworkConnectChoice(anyInt())) + .then(new AnswerWithArguments() { + public boolean answer(int netId) { + if (netId >= 0 && netId < configs.length) { + configs[netId].getNetworkSelectionStatus().setConnectChoice(null); + configs[netId].getNetworkSelectionStatus() + .setConnectChoiceTimestamp( + NetworkSelectionStatus + .INVALID_NETWORK_SELECTION_DISABLE_TIMESTAMP); + return true; + } else { + return false; + } + } + }); + when(wifiConfigManager.setNetworkConnectChoice(anyInt(), anyString(), anyLong())) + .then(new AnswerWithArguments() { + public boolean answer(int netId, String configKey, long timestamp) { + if (netId >= 0 && netId < configs.length) { + configs[netId].getNetworkSelectionStatus().setConnectChoice(configKey); + configs[netId].getNetworkSelectionStatus().setConnectChoiceTimestamp( + timestamp); + return true; + } else { + return false; + } + } + }); + } + + + /** + * Link scan results to the saved configurations. + * + * The shorter of the 2 input params will be used to loop over so the inputs don't + * need to be of equal length. If there are more scan details then configs the remaining scan + * details will be associated with a NULL config. + * + * @param wifiConfigManager the mocked WifiConfigManager + * @param configs saved configurations + * @param scanDetails come in scan results + */ + private static void scanResultLinkConfiguration(WifiConfigManager wifiConfigManager, + WifiConfiguration[] configs, List<ScanDetail> scanDetails) { + if (configs == null || scanDetails == null) { + return; + } + + if (scanDetails.size() <= configs.length) { + for (int i = 0; i < scanDetails.size(); i++) { + ScanDetail scanDetail = scanDetails.get(i); + when(wifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetail))) + .thenReturn(configs[i]); + } + } else { + for (int i = 0; i < configs.length; i++) { + ScanDetail scanDetail = scanDetails.get(i); + when(wifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetail))) + .thenReturn(configs[i]); + } + + // associated the remaining scan details with a NULL config. + for (int i = configs.length; i < scanDetails.size(); i++) { + when(wifiConfigManager.getSavedNetworkForScanDetailAndCache( + eq(scanDetails.get(i)))).thenReturn(null); + } + } + } + + + + /** + * Configure the score cache for externally scored networks + * + * @param scoreCache Wifi network score cache to be configured + * @param scanDetails a list of ScanDetail + * @param scores scores of the networks + * @param meteredHints hints of if the networks are metered + */ + public static void configureScoreCache(WifiNetworkScoreCache scoreCache, + List<ScanDetail> scanDetails, Integer[] scores, boolean[] meteredHints) { + List<ScoredNetwork> networks = new ArrayList<>(); + + for (int i = 0; i < scanDetails.size(); i++) { + ScanDetail scanDetail = scanDetails.get(i); + byte rssiScore; + Integer score = scores[i]; + ScanResult scanResult = scanDetail.getScanResult(); + WifiKey wifiKey = new WifiKey("\"" + scanResult.SSID + "\"", scanResult.BSSID); + NetworkKey ntwkKey = new NetworkKey(wifiKey); + if (scores[i] == null) { + rssiScore = WifiNetworkScoreCache.INVALID_NETWORK_SCORE; + } else { + rssiScore = scores[i].byteValue(); + } + RssiCurve rssiCurve = new RssiCurve(-100, 100, new byte[] {rssiScore}); + ScoredNetwork scoredNetwork = new ScoredNetwork(ntwkKey, rssiCurve, meteredHints[i]); + + networks.add(scoredNetwork); + } + + scoreCache.updateScores(networks); + } + + /** + * Setup WifiConfigManager mock for ephemeral networks. + * + * @param wifiConfigManager WifiConfigManager mock + * @param networkId ID of the ephemeral network + * @param scanResult scanResult of the ephemeral network + * @param meteredHint flag to indidate if the network has meteredHint + */ + public static WifiConfiguration setupEphemeralNetwork(WifiConfigManager wifiConfigManager, + int networkId, ScanResult scanResult, boolean meteredHint) { + // Return the correct networkID for ephemeral network addition. + when(wifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) + .thenReturn(new NetworkUpdateResult(networkId)); + final WifiConfiguration config = ScanResultUtil.createNetworkFromScanResult(scanResult); + config.networkId = networkId; + config.meteredHint = meteredHint; + + when(wifiConfigManager.getConfiguredNetwork(eq(networkId))) + .then(new AnswerWithArguments() { + public WifiConfiguration answer(int netId) { + return new WifiConfiguration(config); + } + }); + when(wifiConfigManager.setNetworkCandidateScanResult( + eq(networkId), any(ScanResult.class), anyInt())) + .then(new AnswerWithArguments() { + public boolean answer(int netId, ScanResult scanResult, int score) { + config.getNetworkSelectionStatus().setCandidate(scanResult); + config.getNetworkSelectionStatus().setCandidateScore(score); + config.getNetworkSelectionStatus() + .setSeenInLastQualifiedNetworkSelection(true); + return true; + } + }); + return config; + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java deleted file mode 100644 index c83400b93..000000000 --- a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java +++ /dev/null @@ -1,2299 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.server.wifi; - -import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_EAP; -import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; -import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; -import static com.android.server.wifi.WifiConfigurationTestUtil.generateWifiConfig; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import android.app.test.MockAnswerUtil.AnswerWithArguments; -import android.content.Context; -import android.content.res.Resources; -import android.net.NetworkScoreManager; -import android.net.wifi.ScanResult; -import android.net.wifi.WifiConfiguration; -import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; -import android.net.wifi.WifiEnterpriseConfig; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiSsid; -import android.os.SystemClock; -import android.test.suitebuilder.annotation.SmallTest; -import android.text.TextUtils; -import android.util.LocalLog; - -import com.android.internal.R; -import com.android.server.wifi.util.ScanResultUtil; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; - -/** - * Unit tests for {@link com.android.server.wifi.WifiQualifiedNetworkSelector}. - */ -@SmallTest -public class WifiQualifiedNetworkSelectorTest { - - @Before - public void setUp() throws Exception { - mResource = getResource(); - mScoreManager = getNetworkScoreManager(); - mScoreCache = getScoreCache(); - mContext = getContext(); - mWifiConfigManager = getWifiConfigManager(); - mWifiInfo = getWifiInfo(); - mLocalLog = getLocalLog(); - - mWifiQualifiedNetworkSelector = new WifiQualifiedNetworkSelector(mWifiConfigManager, - mContext, mWifiInfo, mClock); - mWifiQualifiedNetworkSelector.enableVerboseLogging(1); - mWifiQualifiedNetworkSelector.setWifiNetworkScoreCache(mScoreCache); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); - } - - @After - public void cleanup() { - validateMockitoUsage(); - } - - private WifiQualifiedNetworkSelector mWifiQualifiedNetworkSelector = null; - private WifiConfigManager mWifiConfigManager = null; - private Context mContext; - private Resources mResource; - private NetworkScoreManager mScoreManager; - private WifiNetworkScoreCache mScoreCache; - private WifiInfo mWifiInfo; - private LocalLog mLocalLog; - private Clock mClock = mock(Clock.class); - private static final String[] DEFAULT_SSIDS = {"\"test1\"", "\"test2\""}; - private static final String[] DEFAULT_BSSIDS = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - private static final String TAG = "QNS Unit Test"; - - private List<ScanDetail> getScanDetails(String[] ssids, String[] bssids, int[] frequencies, - String[] caps, int[] levels) { - List<ScanDetail> scanDetailList = new ArrayList<ScanDetail>(); - long timeStamp = mClock.getElapsedSinceBootMillis(); - for (int index = 0; index < ssids.length; index++) { - ScanDetail scanDetail = new ScanDetail(WifiSsid.createFromAsciiEncoded(ssids[index]), - bssids[index], caps[index], levels[index], frequencies[index], timeStamp, 0); - scanDetailList.add(scanDetail); - } - return scanDetailList; - } - - Context getContext() { - Context context = mock(Context.class); - Resources resource = mock(Resources.class); - - when(context.getResources()).thenReturn(mResource); - when(context.getSystemService(Context.NETWORK_SCORE_SERVICE)).thenReturn(mScoreManager); - return context; - } - - Resources getResource() { - Resources resource = mock(Resources.class); - - when(resource.getInteger(R.integer.config_wifi_framework_SECURITY_AWARD)).thenReturn(80); - when(resource.getInteger(R.integer.config_wifi_framework_RSSI_SCORE_OFFSET)).thenReturn(85); - when(resource.getInteger(R.integer.config_wifi_framework_SAME_BSSID_AWARD)).thenReturn(24); - when(resource.getInteger(R.integer.config_wifi_framework_LAST_SELECTION_AWARD)) - .thenReturn(480); - when(resource.getInteger(R.integer.config_wifi_framework_PASSPOINT_SECURITY_AWARD)) - .thenReturn(40); - when(resource.getInteger(R.integer.config_wifi_framework_SECURITY_AWARD)).thenReturn(80); - when(resource.getInteger(R.integer.config_wifi_framework_RSSI_SCORE_SLOPE)).thenReturn(4); - 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(WifiQualifiedNetworkSelector.RSSI_SATURATION_2G_BAND); - when(resource.getInteger( - R.integer.config_wifi_framework_current_network_boost)) - .thenReturn(WifiQualifiedNetworkSelector.SAME_NETWORK_AWARD); - when(resource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz)) - .thenReturn(WifiQualifiedNetworkSelector.RSSI_SATURATION_2G_BAND); - when(resource.getInteger( - R.integer.config_wifi_framework_5GHz_preference_boost_factor)) - .thenReturn(WifiQualifiedNetworkSelector.BAND_AWARD_5GHz); - when(resource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz)) - .thenReturn(WifiQualifiedNetworkSelector.QUALIFIED_RSSI_5G_BAND); - when(resource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz)) - .thenReturn(WifiQualifiedNetworkSelector.MINIMUM_5G_ACCEPT_RSSI); - when(resource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz)) - .thenReturn(WifiQualifiedNetworkSelector.MINIMUM_2G_ACCEPT_RSSI); - return resource; - } - - NetworkScoreManager getNetworkScoreManager() { - NetworkScoreManager networkScoreManager = mock(NetworkScoreManager.class); - - return networkScoreManager; - } - - WifiNetworkScoreCache getScoreCache() { - return mock(WifiNetworkScoreCache.class); - } - - LocalLog getLocalLog() { - return new LocalLog(0); - } - - WifiInfo getWifiInfo() { - WifiInfo wifiInfo = mock(WifiInfo.class); - - //simulate a disconnected state - when(wifiInfo.is24GHz()).thenReturn(true); - when(wifiInfo.is5GHz()).thenReturn(false); - when(wifiInfo.getRssi()).thenReturn(-70); - when(wifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID); - when(wifiInfo.getBSSID()).thenReturn(null); - when(wifiInfo.getNetworkId()).thenReturn(-1); - return wifiInfo; - } - - WifiConfigManager getWifiConfigManager() { - WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); - when(wifiConfigManager.getLastSelectedNetwork()) - .thenReturn(WifiConfiguration.INVALID_NETWORK_ID); - return wifiConfigManager; - } - - /** - * This API is used to generate multiple simulated saved configurations used for test - * - * @param ssid array of SSID of saved configuration - * @param security array of securities of saved configuration - * @return generated new array of configurations based on input - */ - private WifiConfiguration[] generateWifiConfigurations(String[] ssid, int[] security) { - if (ssid == null || security == null || ssid.length != security.length - || ssid.length == 0) { - return null; - } - - WifiConfiguration[] configs = new WifiConfiguration[ssid.length]; - for (int index = 0; index < ssid.length; index++) { - configs[index] = generateWifiConfig(index, 0, ssid[index], false, true, null, null, - security[index]); - } - - return configs; - } - - /** - * set configuration to a passpoint configuration - * - * @param config The configuration need to be set as a passipoint configuration - */ - private void setConfigPasspoint(WifiConfiguration config) { - config.FQDN = "android.qns.unitTest"; - config.providerFriendlyName = "android.qns.unitTest"; - WifiEnterpriseConfig enterpriseConfig = mock(WifiEnterpriseConfig.class); - when(enterpriseConfig.getEapMethod()).thenReturn(WifiEnterpriseConfig.Eap.PEAP); - - } - - /** - * Add the Configurations to WifiConfigManager (WifiConfigureStore can take them out according - * to the networkd ID) and setup the WifiConfigManager mocks for these networks. - * This simulates the WifiConfigManager class behaviour. - * - * @param configs input configuration need to be added to WifiConfigureStore - */ - private void prepareConfigStore(final WifiConfiguration[] configs) { - when(mWifiConfigManager.getConfiguredNetwork(anyInt())) - .then(new AnswerWithArguments() { - public WifiConfiguration answer(int netId) { - if (netId >= 0 && netId < configs.length) { - return new WifiConfiguration(configs[netId]); - } else { - return null; - } - } - }); - when(mWifiConfigManager.getConfiguredNetwork(anyString())) - .then(new AnswerWithArguments() { - public WifiConfiguration answer(String configKey) { - for (int netId = 0; netId < configs.length; netId++) { - if (TextUtils.equals(configs[netId].configKey(), configKey)) { - return new WifiConfiguration(configs[netId]); - } - } - return null; - } - }); - when(mWifiConfigManager.getSavedNetworks()) - .then(new AnswerWithArguments() { - public List<WifiConfiguration> answer() { - List<WifiConfiguration> savedNetworks = new ArrayList<>(); - for (int netId = 0; netId < configs.length; netId++) { - savedNetworks.add(new WifiConfiguration(configs[netId])); - } - return savedNetworks; - } - }); - when(mWifiConfigManager.clearNetworkCandidateScanResult(anyInt())) - .then(new AnswerWithArguments() { - public boolean answer(int netId) { - if (netId >= 0 && netId < configs.length) { - configs[netId].getNetworkSelectionStatus().setCandidate(null); - configs[netId].getNetworkSelectionStatus() - .setCandidateScore(Integer.MIN_VALUE); - configs[netId].getNetworkSelectionStatus() - .setSeenInLastQualifiedNetworkSelection(false); - return true; - } else { - return false; - } - } - }); - when(mWifiConfigManager.setNetworkCandidateScanResult( - anyInt(), any(ScanResult.class), anyInt())) - .then(new AnswerWithArguments() { - public boolean answer(int netId, ScanResult scanResult, int score) { - if (netId >= 0 && netId < configs.length) { - configs[netId].getNetworkSelectionStatus().setCandidate(scanResult); - configs[netId].getNetworkSelectionStatus().setCandidateScore(score); - configs[netId].getNetworkSelectionStatus() - .setSeenInLastQualifiedNetworkSelection(true); - return true; - } else { - return false; - } - } - }); - when(mWifiConfigManager.clearNetworkConnectChoice(anyInt())) - .then(new AnswerWithArguments() { - public boolean answer(int netId) { - if (netId >= 0 && netId < configs.length) { - configs[netId].getNetworkSelectionStatus().setConnectChoice(null); - configs[netId].getNetworkSelectionStatus() - .setConnectChoiceTimestamp( - NetworkSelectionStatus - .INVALID_NETWORK_SELECTION_DISABLE_TIMESTAMP); - return true; - } else { - return false; - } - } - }); - when(mWifiConfigManager.setNetworkConnectChoice(anyInt(), anyString(), anyLong())) - .then(new AnswerWithArguments() { - public boolean answer(int netId, String configKey, long timestamp) { - if (netId >= 0 && netId < configs.length) { - configs[netId].getNetworkSelectionStatus().setConnectChoice(configKey); - configs[netId].getNetworkSelectionStatus().setConnectChoiceTimestamp( - timestamp); - return true; - } else { - return false; - } - } - }); - } - - /** - * Setup WifiConfigManager mock for the ephemeral network addition and getter/setter methods. - */ - private WifiConfiguration setupEphemeralNetwork( - int networkId, ScanResult scanResult, boolean meteredHint) { - // Return the correct networkID for ephemeral network addition. - when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) - .thenReturn(new NetworkUpdateResult(networkId)); - final WifiConfiguration config = ScanResultUtil.createNetworkFromScanResult(scanResult); - config.networkId = networkId; - config.meteredHint = meteredHint; - -// when(mWifiConfigManager.getConfiguredNetwork(networkId)) - // .thenReturn(new WifiConfiguration(config)); - - when(mWifiConfigManager.getConfiguredNetwork(eq(networkId))) - .then(new AnswerWithArguments() { - public WifiConfiguration answer(int netId) { - return new WifiConfiguration(config); - } - }); - when(mWifiConfigManager.setNetworkCandidateScanResult( - eq(networkId), any(ScanResult.class), anyInt())) - .then(new AnswerWithArguments() { - public boolean answer(int netId, ScanResult scanResult, int score) { - config.getNetworkSelectionStatus().setCandidate(scanResult); - config.getNetworkSelectionStatus().setCandidateScore(score); - config.getNetworkSelectionStatus() - .setSeenInLastQualifiedNetworkSelection(true); - return true; - } - }); - return config; - } - - /** - * Link scan results to the saved configurations. - * - * The shorter of the 2 input params will be used to loop over so the inputs don't - * need to be of equal length. If there are more scan details then configs the remaining scan - * details will be associated with a NULL config. - * - * @param configs saved configurations - * @param scanDetails come in scan results - */ - private void scanResultLinkConfiguration(WifiConfiguration[] configs, - List<ScanDetail> scanDetails) { - if (scanDetails.size() <= configs.length) { - for (int i = 0; i < scanDetails.size(); i++) { - ScanDetail scanDetail = scanDetails.get(i); - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetail))) - .thenReturn(configs[i]); - } - } else { - for (int i = 0; i < configs.length; i++) { - ScanDetail scanDetail = scanDetails.get(i); - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetail))) - .thenReturn(configs[i]); - } - - // associated the remaining scan details with a NULL config. - for (int i = configs.length; i < scanDetails.size(); i++) { - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache( - eq(scanDetails.get(i)))).thenReturn(null); - } - } - } - - private void configureScoreCache(List<ScanDetail> scanDetails, Integer[] scores, - boolean[] meteredHints) { - for (int i = 0; i < scanDetails.size(); i++) { - ScanDetail scanDetail = scanDetails.get(i); - Integer score = scores[i]; - ScanResult scanResult = scanDetail.getScanResult(); - if (score != null) { - when(mScoreCache.isScoredNetwork(scanResult)).thenReturn(true); - when(mScoreCache.hasScoreCurve(scanResult)).thenReturn(true); - when(mScoreCache.getNetworkScore(eq(scanResult), anyBoolean())).thenReturn(score); - when(mScoreCache.getNetworkScore(scanResult)).thenReturn(score); - } else { - when(mScoreCache.isScoredNetwork(scanResult)).thenReturn(false); - when(mScoreCache.hasScoreCurve(scanResult)).thenReturn(false); - when(mScoreCache.getNetworkScore(eq(scanResult), anyBoolean())).thenReturn( - WifiNetworkScoreCache.INVALID_NETWORK_SCORE); - when(mScoreCache.getNetworkScore(scanResult)).thenReturn( - WifiNetworkScoreCache.INVALID_NETWORK_SCORE); - } - when(mScoreCache.getMeteredHint(scanResult)).thenReturn(meteredHints[i]); - } - } - - /** - * Verify whether the chosen configuration matched with the expected chosen scan result - * - * @param chosenScanResult the expected chosen scan result - * @param chosenCandidate the chosen configuration - */ - private void verifySelectedResult( - ScanResult chosenScanResult, WifiConfiguration chosenCandidate) { - verify(mWifiConfigManager, atLeastOnce()).setNetworkCandidateScanResult( - eq(chosenCandidate.networkId), eq(chosenScanResult), anyInt()); - } - - // QNS test under disconnected State - - /** - * Case #1 choose 2GHz stronger RSSI test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled, encrypted and at 2.4 GHz - * test1 is with RSSI -70 test2 is with RSSI -60 - * - * Expected behavior: test2 is chosen - */ - @Test - public void chooseNetworkDisconnected2GHighestRssi() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2417}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-70, -60}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(scanDetails.size() - 1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual( - savedConfigs[scanDetails.size() - 1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #2 choose 5GHz Stronger RSSI Test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled, encrypted and at 5 GHz - * test1 is with RSSI -70 test2 is with RSSI -60 - * - * Expected behavior: test2 is chosen - */ - @Test - public void chooseNetworkDisconnected5GHighestRssi() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5180, 5610}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-70, -60}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(scanDetails.size() - 1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual( - savedConfigs[scanDetails.size() - 1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #3 5GHz over 2GHz bonus Test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled - * test1 is @ 2GHz with RSSI -60 - * test2 is @ 5Ghz with RSSI -65 - * - * Expected behavior: test2 is chosen due to 5GHz bonus - */ - @Test - public void chooseNetworkDisconnect5GOver2GTest() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-60, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(scanDetails.size() - 1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual( - savedConfigs[scanDetails.size() - 1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #4 2GHz over 5GHz dur to 5GHz signal too weak test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled - * test1 is @ 2GHz with RSSI -60 - * test2 is @ 5Ghz with RSSI -75 - * - * Expected behavior: test1 is chosen due to 5GHz signal is too weak (5GHz bonus can not - * compensate) - */ - @Test - public void chooseNetworkDisconnect2GOver5GTest() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-60, -75}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #5 2GHz signal Saturation test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled - * test1 is @ 2GHz with RSSI -50 - * test2 is @ 5Ghz with RSSI -65 - * - * Expected behavior: test2 is chosen. Although the RSSI delta here is 15 too, because 2GHz RSSI - * saturates at -60, the real RSSI delta is only 5, which is less than 5GHz bonus - */ - @Test - public void chooseNetworkDisconnect2GRssiSaturationTest() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(scanDetails.size() - 1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual( - savedConfigs[scanDetails.size() - 1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #6 Minimum RSSI test - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network - * Both network are enabled - * test1 is @ 2GHz with RSSI -86 - * test2 is @ 5Ghz with RSSI -83 - * - * Expected behavior: no QNS is made because both network are below the minimum threshold, null - */ - @Test - public void chooseNetworkMinimumRssiTest() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {WifiQualifiedNetworkSelector.MINIMUM_2G_ACCEPT_RSSI - 1, - WifiQualifiedNetworkSelector.MINIMUM_5G_ACCEPT_RSSI - 1}; - int[] security = {SECURITY_EAP, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - assertEquals("choose the wrong SSID", null, candidate); - } - - /** - * Case #7 encrypted network over passpoint network - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1 is secured network, test2 are passpoint network - * Both network are enabled and at 2.4 GHz. Both have RSSI of -70 - * - * Expected behavior: test1 is chosen since secured network has higher priority than passpoint - * network - */ - @Test - public void chooseNetworkSecurityOverPassPoint() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] levels = {-70, -70}; - int[] security = {SECURITY_EAP, SECURITY_NONE}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - setConfigPasspoint(savedConfigs[1]); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #8 passpoint network over open network - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1 is passpoint network, test2 is open network - * Both network are enabled and at 2.4 GHz. Both have RSSI of -70 - * - * Expected behavior: test1 is chosen since passpoint network has higher priority than open - * network - */ - @Test - public void chooseNetworkPasspointOverOpen() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f8", "6c:f3:7f:ae:8c:f4"}; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-70, -70}; - int[] security = {SECURITY_NONE, SECURITY_NONE}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - setConfigPasspoint(savedConfigs[0]); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #9 secure network over open network - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * Two networks test1 is secure network, test2 is open network - * Both network are enabled and at 2.4 GHz. Both have RSSI of -70 - * - * Expected behavior: test1 is chosen since secured network has higher priority than open - * network - */ - @Test - public void chooseNetworkSecureOverOpen() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-70, -70}; - int[] security = {SECURITY_PSK, SECURITY_NONE}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #10 first time user select a network - * - * In this test. we simulate following scenario - * There are three saved networks: test1, test2 and test3. Now user select the network test3 - * check test3 has been saved in test1's and test2's ConnectChoice - * - * Expected behavior: test1's and test2's ConnectChoice should be test3, test3's ConnectChoice - * should be null - */ - @Test - public void userSelectsNetworkForFirstTime() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\""}; - int[] security = {SECURITY_PSK, SECURITY_PSK, SECURITY_NONE}; - - final WifiConfiguration[] configs = generateWifiConfigurations(ssids, security); - for (WifiConfiguration network : configs) { - NetworkSelectionStatus status = network.getNetworkSelectionStatus(); - status.setSeenInLastQualifiedNetworkSelection(true); - } - prepareConfigStore(configs); - - mWifiQualifiedNetworkSelector.setUserConnectChoice(configs.length - 1); - String key = configs[configs.length - 1].configKey(); - for (int index = 0; index < configs.length; index++) { - WifiConfiguration config = configs[index]; - NetworkSelectionStatus status = config.getNetworkSelectionStatus(); - if (index == configs.length - 1) { - assertEquals("User selected network should not have prefernce over it", null, - status.getConnectChoice()); - } else { - assertEquals("Wrong user preference", key, status.getConnectChoice()); - } - } - } - - /** - * Case #11 choose user selected network - * - * In this test, we simulate following scenario: - * WifiStateMachine is under disconnected state - * There are three networks: test1, test2, test3 and test3 is the user preference - * All three networks are enabled - * test1 is @ 2.4GHz with RSSI -50 PSK - * test2 is @ 5Ghz with RSSI -65 PSK - * test3 is @ 2.4GHz with RSSI -55 open - * - * Expected behavior: test3 is chosen since it is user selected network. It overcome all other - * priorities - */ - @Test - public void chooseUserPreferredNetwork() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\""}; - int[] security = {SECURITY_PSK, SECURITY_PSK, SECURITY_NONE}; - - final WifiConfiguration[] configs = generateWifiConfigurations(ssids, security); - for (WifiConfiguration network : configs) { - NetworkSelectionStatus status = network.getNetworkSelectionStatus(); - status.setSeenInLastQualifiedNetworkSelection(true); - } - prepareConfigStore(configs); - - //set user preference - mWifiQualifiedNetworkSelector.setUserConnectChoice(ssids.length - 1); - //Generate mocked recent scan results - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "6c:f3:7f:ae:8c:f5"}; - int[] frequencies = {2437, 5180, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]", "NONE"}; - int[] levels = {-50, -65, -55}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - scanResultLinkConfiguration(configs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(scanDetails.size() - 1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual( - configs[scanDetails.size() - 1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #12 enable a blacklisted BSSID - * - * In this test, we simulate following scenario: - * For two Aps, BSSIDA and BSSIDB. Disable BSSIDA, then check whether BSSIDA is disabled and - * BSSIDB is enabled. Then enable BSSIDA, check whether both BSSIDs are enabled. - */ - @Test - public void enableBssidTest() { - String bssidA = "6c:f3:7f:ae:8c:f3"; - String bssidB = "6c:f3:7f:ae:8c:f4"; - //check by default these two BSSIDs should be enabled - assertEquals("bssidA should be enabled by default", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidA), false); - assertEquals("bssidB should be enabled by default", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidB), false); - - //disable bssidA 3 times, check whether A is dsiabled and B is still enabled - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssidA, false); - assertEquals("bssidA should be disabled", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidA), false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssidA, false); - assertEquals("bssidA should be disabled", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidA), false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssidA, false); - assertEquals("bssidA should be disabled", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidA), true); - assertEquals("bssidB should still be enabled", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidB), false); - - //re-enable bssidA, check whether A is dsiabled and B is still enabled - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssidA, true); - assertEquals("bssidA should be enabled by default", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidA), false); - assertEquals("bssidB should be enabled by default", - mWifiQualifiedNetworkSelector.isBssidDisabled(bssidB), false); - - //make sure illegal input will not cause crash - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(null, false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(null, true); - } - - /** - * Case #13 do not choose the BSSID has been disabled - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network and found in scan results - * Both network are enabled - * test1 is @ 2GHz with RSSI -65 - * test2 is @ 5Ghz with RSSI -50 - * test2's BSSID is disabled - * - * expected return test1 since test2's BSSID has been disabled - */ - @Test - public void networkChooseWithOneBssidDisabled() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-65, -50}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #14 re-choose the disabled BSSID after it is re-enabled - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network and found in scan results - * Both network are enabled - * test1 is @ 2GHz with RSSI -65 - * test2 is @ 5Ghz with RSSI -50 - * test2's BSSID is disabled - * - * expected return test2 since test2's BSSID has been enabled again - */ - @Test - public void networkChooseWithOneBssidReenaabled() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-65, -50}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - //re-enable it - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], true); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #15 re-choose the disabled BSSID after its disability has expired - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network and found in scan results - * Both network are enabled - * test1 is @ 2GHz with RSSI -65 - * test2 is @ 5Ghz with RSSI -50 - * test2's BSSID is disabled - * - * expected return test2 since test2's BSSID has been enabled again - */ - @Test - public void networkChooseWithOneBssidDisableExpire() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "6c:f3:7f:ae:8c:f5"}; - int[] frequencies = {2437, 5180, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]", - "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-65, -50, -60}; - int[] security = {SECURITY_PSK, SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - for (int index = 0; index < WifiQualifiedNetworkSelector.BSSID_BLACKLIST_THRESHOLD; - index++) { - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[1], false); - mWifiQualifiedNetworkSelector.enableBssidForQualityNetworkSelection(bssids[2], false); - } - - //re-enable it - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() - + WifiQualifiedNetworkSelector.BSSID_BLACKLIST_EXPIRE_TIME_MS); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - /** - * Case #16 do not choose the SSID has been disabled - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state - * Two networks test1, test2 are secured network and found in scan results - * Both network are enabled - * test1 is @ 2GHz with RSSI -65 - * test2 is @ 5Ghz with RSSI -50 - * test2's SSID is disabled - * - * expected return test1 since test2's SSID has been disabled - */ - @Test - public void networkChooseWithOneSsidDisabled() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-65, -50}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - when(mWifiConfigManager.tryEnableNetwork(anyInt())).thenReturn(true); - savedConfigs[1].getNetworkSelectionStatus().setNetworkSelectionStatus( - NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #17 do not make QNS is link is bouncing now - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state and currently is under link bouncing - * Two networks test1, test2 are secured network and found in scan results - * Both network are enabled - * test1 is @ 2GHz with RSSI -50 - * test2 is @ 5Ghz with RSSI -50 - * - * expected return null - */ - @Test - public void noQNSWhenLinkBouncingDisconnected() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {WifiQualifiedNetworkSelector.MINIMUM_2G_ACCEPT_RSSI - 1, - WifiQualifiedNetworkSelector.MINIMUM_5G_ACCEPT_RSSI - 1}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, true, false, true, false, scanDetails); - - assertEquals("choose the wrong network", null, candidate); - } - - /** - * Case #18 QNS with very short gap - * - * In this test. we simulate following scenario: - * WifiStateMachine is under disconnected state - * If last QNS is made in less than MINIMUM_QUALIFIED_NETWORK_SELECTION_INTERVAL, we - * still should make new QNS since it is disconnected now - * - * expect return test1 because of band bonus - */ - @Test - public void networkSelectionInShortGap() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - //first QNS - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - //immediately second QNS - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - //Unit test for Connected State - - /** - * Case #19 no QNS with very short gap when connected - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and test2 is connected - * When WifiStateMachine is already in connected state, if last QNS is made in less than - * MINIMUM_QUALIFIED_NETWORK_SELECTION_INTERVAL, no new QNS should be made - * - * expect return NULL - */ - @Test - public void noNetworkSelectionDueToShortGap() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - //first QNS - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - //immediately second QNS - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - assertEquals("choose the wrong BSSID", null, candidate); - } - - /** - * Case #20 force QNS with very short gap under connection - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and test2 is connected - * When WifiStateMachine is already in connected state, if last QNS is made in less than - * MINIMUM_QUALIFIED_NETWORK_SELECTION_INTERVAL, no new QNS should be made. However, if we force - * to make new QNS, QNS still will be made - * - * expect return test2 since it is the current connected one (bonus) - */ - @Test - public void forceNetworkSelectionInShortGap() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - //first QNS - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - //immediately second QNS - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(true, - false, false, true, false, false, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #21 no QNS when connected and user do not allow switch when connected - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and test2 is connected - * if user does not allow switch network when connected, do not make new QNS when connected - * - * expect return NULL - */ - @Test - public void noNewNetworkSelectionDuetoUserDisableSwitchWhenConnected() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - assertEquals("choose the wrong BSSID", null, candidate); - assertEquals("Should receive zero filteredScanDetails", 0, - mWifiQualifiedNetworkSelector.getFilteredScanDetails().size()); - } - - /** - * Case #22 no new QNS if current network is qualified already - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and test2 is connected - * If current connected network is Qualified already, do not make new QNS - * simulated current connected network as: - * 5GHz, RSSI = WifiQualifiedNetworkSelector.QUALIFIED_RSSI_5G_BAND, secured - * - * expected return null - */ - @Test - public void noNewQNSCurrentNetworkQualified() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-65, WifiQualifiedNetworkSelector.QUALIFIED_RSSI_5G_BAND}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - //first time, connect to test2 due to 5GHz bonus - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - - levels[0] = -50; // if there is QNS, test1 will be chosen - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - assertEquals("choose the wrong BSSID", null, candidate); - } - - /** - * Case #23 No new QNS when link bouncing when connected - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and test2 is connected - * no new QNS when link is bouncing - * - * expected return null - */ - @Test - public void noNewQNSLinkBouncing() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-70, -75}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - //first connect to test2 due to 5GHz bonus - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, true, true, false, false, scanDetails); - assertEquals("choose the wrong BSSID", null, candidate); - } - - /** - * Case #24 Qualified network need to be on 5GHz - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and connected to test2 - * if current connected network is not 5GHz, then it is not qualified. We should make new QNS - * - * expected result: return test1 - */ - @Test - public void currentNetworkNotQualifiedDueToBandMismatch() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-50, -65}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(0); - when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); - when(mWifiInfo.is24GHz()).thenReturn(true); - //connect to config2 first - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #25 Qualified network need to be secured - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connects to test2. If current connected - * network is open network, then it is not qualified. We should make new QNS selection and - * ensure that we switch to the secure one if the RSSI is sufficiently high. - * - * expected result: return test1 since test1 has higher RSSI - */ - @Test - public void currentNetworkNotQualifiedDueToOpenNetwork() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5400, 5400}; - String[] caps = {"[WPA2-EAP-CCMP][ESS][ESS]", "[ESS]"}; - int[] levels = {-80, -45}; - int[] security = {SECURITY_PSK, SECURITY_NONE}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - //first connect to test2 because of RSSI - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - when(mWifiInfo.is5GHz()).thenReturn(true); - when(mClock.getElapsedSinceBootMillis()) - .thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - - // Now increase RSSI of test1 - levels[0] = -60; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #26 ephemeral network can not be qualified network - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * if current connected network is ephemeral network, then it is not qualified. We should make - * new QNS - * - * expected result: return test1 (since test2 is ephemeral) - */ - @Test - public void currentNetworkNotQualifiedDueToEphemeral() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, -50}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - savedConfigs[1].ephemeral = true; - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - //first connect to test2 since test1's RSSI is negligible - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - - levels[0] = -70; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - scanResultLinkConfiguration(savedConfigs, scanDetails); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #27 low signal network can not be Qualified network (5GHz) - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * if current connected network's rssi is too low, then it is not qualified. We should - * make new QNS - * - * expected result: return test1 - */ - @Test - public void currentNetworkNotQualifiedDueToLow5GRssi() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-80, WifiQualifiedNetworkSelector.QUALIFIED_RSSI_5G_BAND - 1}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.getRssi()).thenReturn(levels[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - when(mWifiInfo.is5GHz()).thenReturn(true); - - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - levels[0] = -60; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #28 low signal network can not be Qualified network (2.4GHz) - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * if current connected network's rssi is too low, then it is not qualified. We should - * make new QNS - * - * expected result: return test1 - */ - @Test - public void currentNetworkNotQualifiedDueToLow2GRssi() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, WifiQualifiedNetworkSelector.QUALIFIED_RSSI_24G_BAND - 1}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.getRssi()).thenReturn(levels[1]); - when(mWifiInfo.is24GHz()).thenReturn(false); - when(mWifiInfo.is5GHz()).thenReturn(true); - - levels[0] = -60; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #29 Choose current network due to current network bonus - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * To connect to a network which is not linked to current connected network, unless this network - * is more than 10 db higher than current network, we should not switch. So although test2 has a - * lower signal, we still choose test2 - * - * expected result: return test2 - */ - @Test - public void currentNetworkStayDueToSameNetworkBonus() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, -80}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(true); - - levels[0] = -80 + WifiQualifiedNetworkSelector.SAME_BSSID_AWARD / 4 - + WifiQualifiedNetworkSelector.SAME_NETWORK_AWARD / 4 - 1; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #30 choose another network due to current network's signal is too low - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * To connect to a network which is not linked to current connected network, if this network - * is more than 10 db higher than current network, we should switch - * - * expected new result: return test1 - */ - @Test - public void switchNetworkStayDueToCurrentNetworkRssiLow() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, -80}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(true); - - levels[0] = -80 + WifiQualifiedNetworkSelector.SAME_BSSID_AWARD / 4 - + WifiQualifiedNetworkSelector.SAME_NETWORK_AWARD / 4 + 1; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #31 Choose current BSSID due to current BSSID bonus - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * Linked network will be treated as same network. To connect to a network which is linked to - * current connected network, unless this network is more than 6 db higher than current network, - * we should not switch AP and stick to current BSSID - * - * expected result: return test2 - */ - @Test - public void currentBssidStayDueToSameBSSIDBonus() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, -80}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - //link two configuration - savedConfigs[0].linkedConfigurations = new HashMap<String, Integer>(); - savedConfigs[1].linkedConfigurations = new HashMap<String, Integer>(); - savedConfigs[0].linkedConfigurations.put(savedConfigs[1].configKey(), 1); - savedConfigs[1].linkedConfigurations.put(savedConfigs[0].configKey(), 1); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(true); - - levels[0] = -80 + WifiQualifiedNetworkSelector.SAME_NETWORK_AWARD / 4 - 1; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #32 Choose another BSSID due to current BSSID's rssi is too low - * - * In this test. we simulate following scenario: - * WifiStateMachine is under connected state and current connected to test2 - * Linked network will be treated as same network. To connect to a network which is linked to - * current connected network, unless this network is more than 6 db higher than current network, - * we should not switch AP and stick to current BSSID - * - * expected result: return test2 - */ - @Test - public void swithBssidDueToLowRssi() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS][ESS]"}; - int[] levels = {-100, -80}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - //link two configuration - savedConfigs[0].linkedConfigurations = new HashMap<String, Integer>(); - savedConfigs[1].linkedConfigurations = new HashMap<String, Integer>(); - savedConfigs[0].linkedConfigurations.put(savedConfigs[1].configKey(), 1); - savedConfigs[1].linkedConfigurations.put(savedConfigs[0].configKey(), 1); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, false, false, - false, true, false, scanDetails); - - when(mWifiInfo.getNetworkId()).thenReturn(1); - when(mWifiInfo.getBSSID()).thenReturn(bssids[1]); - when(mWifiInfo.is24GHz()).thenReturn(true); - - levels[0] = -80 + WifiQualifiedNetworkSelector.SAME_BSSID_AWARD / 4 + 1; - scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + 11 * 1000); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, true, false, false, scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #33 Choose an ephemeral network with a good score because no saved networks - * are available. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is an enterprise network w/o a score. - * test2 is an open network with a good score. Additionally it's a metered network. - * isUntrustedConnectionsAllowed is set to true. - * - * expected result: return test2 with meteredHint set to True. - */ - @Test - public void selectQualifiedNetworkChoosesEphemeral() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] levels = {-70, -70}; - Integer[] scores = {null, 120}; - boolean[] meteredHints = {false, true}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - // No saved networks. - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(any(ScanDetail.class))) - .thenReturn(null); - - ScanResult untrustedScanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration unTrustedNetworkCandidate = - setupEphemeralNetwork(1, untrustedScanResult, meteredHints[1]); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - true /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(unTrustedNetworkCandidate, candidate); - verifySelectedResult(untrustedScanResult, candidate); - assertEquals(meteredHints[1], candidate.meteredHint); - } - - /** - * Case #34 Test Filtering of potential candidate scanDetails (Untrusted allowed) - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * test1 is @ 2GHz with RSSI -60 - * test2 is @ 5Ghz with RSSI -86, (below minimum threshold) - * test3 is @ 5Ghz with RSSI -50, however it has no associated saved config - * test4 is @ 2Ghz with RSSI -62, no associated config, but is Ephemeral - * - * Expected behavior: test1 is chosen due to 5GHz signal is too weak (5GHz bonus can not - * compensate). - * test1 & test4's scanDetails are returned by 'getFilteredScanDetail()' - */ - @Test - public void testGetFilteredScanDetailsReturnsOnlyConsideredScanDetails_untrustedAllowed() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\"", "\"test4\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "de:ad:ba:b1:e5:55", - "c0:ff:ee:ee:e3:ee"}; - int[] frequencies = {2437, 5180, 5180, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]", - "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {-60, -86, -50, -62}; - int[] security = {SECURITY_PSK, SECURITY_PSK, SECURITY_PSK, SECURITY_PSK}; - boolean[] meteredHints = {false, false, false, true}; - Integer[] scores = {null, null, null, 120}; - - //Create all 4 scanDetails - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - - //Setup NetworkScoreCache for detecting ephemeral networks ("test4") - configureScoreCache(scanDetails, scores, meteredHints); - ScanResult untrustedScanResult = scanDetails.get(3).getScanResult(); - - //Set up associated configs for test1 & test2 - WifiConfiguration[] savedConfigs = generateWifiConfigurations( - Arrays.copyOfRange(ssids, 0, 2), Arrays.copyOfRange(security, 0, 2)); - prepareConfigStore(savedConfigs); - List<ScanDetail> savedScanDetails = new ArrayList<ScanDetail>(scanDetails.subList(0, 2)); - scanResultLinkConfiguration(savedConfigs, savedScanDetails); - - setupEphemeralNetwork(2, scanDetails.get(2).getScanResult(), meteredHints[2]); - setupEphemeralNetwork(3, scanDetails.get(3).getScanResult(), meteredHints[3]); - - //Force mock ConfigManager to return null (and not an empty list) for "test3" & "test4" - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetails.get(2)))) - .thenReturn(null); - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetails.get(3)))) - .thenReturn(null); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - true /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - - verifySelectedResult(chosenScanResult, candidate); - //Verify two scanDetails returned in the filteredScanDetails - assertEquals(2, mWifiQualifiedNetworkSelector.getFilteredScanDetails().size()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).first.toString(), - scanDetails.get(0).toString()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(1).first.toString(), - scanDetails.get(3).toString()); - } - - - /** - * Case #35 Test Filtering of potential candidate scanDetails (Untrusted disallowed) - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * test1 is @ 2GHz with RSSI -60 - * test2 is @ 5Ghz with RSSI -86, (below minimum threshold) - * test3 is @ 5Ghz with RSSI -50, however it has no associated saved config - * test4 is @ 2Ghz with RSSI -62, no associated config, but is Ephemeral - * - * Expected behavior: test1 is chosen due to 5GHz signal is too weak (5GHz bonus can not - * compensate). - * test1 & test4's scanDetails are returned by 'getFilteredScanDetail()' - */ - @Test - public void testGetFilteredScanDetailsReturnsOnlyConsideredScanDetails_untrustedDisallowed() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\"", "\"test4\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "de:ad:ba:b1:e5:55", - "c0:ff:ee:ee:e3:ee"}; - int[] frequencies = {2437, 5180, 5180, 2437}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]", - "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {-60, -86, -50, -62}; - int[] security = {SECURITY_PSK, SECURITY_PSK, SECURITY_PSK, SECURITY_PSK}; - boolean[] meteredHints = {false, false, false, true}; - Integer[] scores = {null, null, null, 120}; - - //Create all 4 scanDetails - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - - //Setup NetworkScoreCache for detecting ephemeral networks ("test4") - configureScoreCache(scanDetails, scores, meteredHints); - ScanResult untrustedScanResult = scanDetails.get(3).getScanResult(); - WifiConfiguration unTrustedNetworkCandidate = - ScanResultUtil.createNetworkFromScanResult(untrustedScanResult); - - //Set up associated configs for test1 & test2 - WifiConfiguration[] savedConfigs = generateWifiConfigurations( - Arrays.copyOfRange(ssids, 0, 2), Arrays.copyOfRange(security, 0, 2)); - prepareConfigStore(savedConfigs); - List<ScanDetail> savedScanDetails = new ArrayList<ScanDetail>(scanDetails.subList(0, 2)); - scanResultLinkConfiguration(savedConfigs, savedScanDetails); - - setupEphemeralNetwork(2, scanDetails.get(2).getScanResult(), meteredHints[2]); - setupEphemeralNetwork(3, scanDetails.get(3).getScanResult(), meteredHints[3]); - - //Force mock ConfigManager to return null (and not an empty list) for "test3" & "test4" - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetails.get(2)))) - .thenReturn(null); - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(eq(scanDetails.get(3)))) - .thenReturn(null); - - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - false /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - - verifySelectedResult(chosenScanResult, candidate); - //Verify two scanDetails returned in the filteredScanDetails - assertEquals(1, mWifiQualifiedNetworkSelector.getFilteredScanDetails().size()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).first.toString(), - scanDetails.get(0).toString()); - } - - /** - * Case #36 Ignore an ephemeral network if it was previously deleted. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is an open network with a low score. Additionally it's a metered network. - * test2 is an open network with a good score but was previously deleted. - * isUntrustedConnectionsAllowed is set to true. - * - * expected result: return test1 with meteredHint set to True. - */ - @Test - public void selectQualifiedNetworkDoesNotChooseDeletedEphemeral() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] levels = {-70, -70}; - Integer[] scores = {20, 120}; - boolean[] meteredHints = {true, false}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - // No saved networks. - when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(any(ScanDetail.class))) - .thenReturn(null); - - ScanResult untrustedScanResult = scanDetails.get(0).getScanResult(); - WifiConfiguration unTrustedNetworkCandidate = - setupEphemeralNetwork(0, untrustedScanResult, meteredHints[0]); - - // The second scan result is for an ephemeral network which was previously deleted - when(mWifiConfigManager - .wasEphemeralNetworkDeleted(scanDetails.get(0).getScanResult().SSID)) - .thenReturn(false); - when(mWifiConfigManager - .wasEphemeralNetworkDeleted(scanDetails.get(1).getScanResult().SSID)) - .thenReturn(true); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - true /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(unTrustedNetworkCandidate, candidate); - verifySelectedResult(untrustedScanResult, candidate); - assertEquals(meteredHints[0], candidate.meteredHint); - } - - /** - * Case #37 Choose the saved config that doesn't qualify for external scoring. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is a saved network. - * test2 is a saved network with useExternalScores set to true and a very high score. - * - * expected result: return test1 because saved networks that don't request external scoring - * have a higher priority. - */ - @Test - public void selectQualifiedNetworkPrefersSavedWithoutExternalScores() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {-70, -70}; - Integer[] scores = {null, 120}; - boolean[] meteredHints = {false, true}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - WifiConfiguration[] savedConfigs = generateWifiConfigurations(DEFAULT_SSIDS, security); - savedConfigs[1].useExternalScores = true; // test2 is set to use external scores. - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - false /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(scanDetails.get(0).getScanResult(), candidate); - } - - /** - * Case #38 Choose the saved config that does qualify for external scoring when other saved - * networks are not available. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is a saved network with useExternalScores set to true and a very high score. - * test2 is a saved network but not in range (not included in the scan results). - * - * expected result: return test1 because there are no better saved networks within range. - */ - @Test - public void selectQualifiedNetworkSelectsSavedWithExternalScores() { - String[] ssids = {"\"test1\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3"}; - int[] frequencies = {5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]"}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {-70}; - Integer[] scores = {120}; - boolean[] meteredHints = {false}; - - // Scan details only contains 1 ssid, test1. - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - // The saved config contains 2 ssids, test1 & test2. - WifiConfiguration[] savedConfigs = generateWifiConfigurations(DEFAULT_SSIDS, security); - savedConfigs[0].useExternalScores = true; // test1 is set to use external scores. - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - false /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(scanDetails.get(0).getScanResult(), candidate); - } - - /** - * Case #39 Choose the saved config that does qualify for external scoring over the - * untrusted network with the same score. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is a saved network with useExternalScores set to true and the same score as test2. - * test2 is NOT saved network but in range with a good external score. - * - * expected result: return test1 because the tie goes to the saved network. - */ - @Test - public void selectQualifiedNetworkPrefersSavedWithExternalScoresOverUntrusted() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {-70, -70}; - Integer[] scores = {120, 120}; - boolean[] meteredHints = {false, true}; - - // Both networks are in the scan results. - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - // Set up the associated configs only for test1 - WifiConfiguration[] savedConfigs = generateWifiConfigurations( - Arrays.copyOfRange(ssids, 0, 1), Arrays.copyOfRange(security, 0, 1)); - savedConfigs[0].useExternalScores = true; // test1 is set to use external scores. - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - setupEphemeralNetwork(1, scanDetails.get(1).getScanResult(), meteredHints[1]); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - true /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - verifySelectedResult(scanDetails.get(0).getScanResult(), candidate); - } - - /** - * Case #40 Choose the ephemeral config over the saved config that does qualify for external - * scoring because the untrusted network has a higher score. - * - * In this test. we simulate following scenario: - * WifiStateMachine is not connected to any network. - * selectQualifiedNetwork() is called with 2 scan results, test1 and test2. - * test1 is a saved network with useExternalScores set to true and a low score. - * test2 is NOT saved network but in range with a good external score. - * - * expected result: return test2 because it has a better score. - */ - @Test - public void selectQualifiedNetworkPrefersUntrustedOverScoredSaved() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {5200, 5200}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[ESS]"}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {-70, -70}; - Integer[] scores = {10, 120}; - boolean[] meteredHints = {false, true}; - - // Both networks are in the scan results. - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - configureScoreCache(scanDetails, scores, meteredHints); - - // Set up the associated configs only for test1 - WifiConfiguration[] savedConfigs = generateWifiConfigurations( - Arrays.copyOfRange(ssids, 0, 1), Arrays.copyOfRange(security, 0, 1)); - savedConfigs[0].useExternalScores = true; // test1 is set to use external scores. - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult untrustedScanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration unTrustedNetworkCandidate = - setupEphemeralNetwork(1, untrustedScanResult, meteredHints[1]); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork( - false /* forceSelectNetwork */, - true /* isUntrustedConnectionsAllowed */, - false, /* isLinkDebouncing */ - false, /* isConnected */ - true, /* isDisconnected */ - false, /* isSupplicantTransient */ - scanDetails); - WifiConfigurationTestUtil.assertConfigurationEqual(unTrustedNetworkCandidate, candidate); - verifySelectedResult(untrustedScanResult, candidate); - } - - - /** - * Case #41 Ensure the ExternalScoreEvaluator correctly selects the untrusted network. - * - * In this test. we simulate following scenario: - * The ExternalScoreEvaluator is asked to evaluate 1 untrusted network and 1 saved network. - * The untrusted network has the higher score. - * - * expected result: The untrusted network is determined to be the best network. - */ - @Test - public void externalScoreEvaluator_untrustedIsBest() { - WifiQualifiedNetworkSelector.ExternalScoreEvaluator evaluator = - new WifiQualifiedNetworkSelector.ExternalScoreEvaluator(mLocalLog, true); - ScanResult untrustedScanResult = new ScanResult(); - int untrustedScore = 100; - evaluator.evalUntrustedCandidate(untrustedScore, untrustedScanResult); - - ScanResult savedScanResult = new ScanResult(); - int savedScore = 50; - WifiConfiguration savedConfig = new WifiConfiguration(); - evaluator.evalSavedCandidate(savedScore, savedConfig, savedScanResult); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.UNTRUSTED_NETWORK, evaluator.getBestCandidateType()); - assertEquals(untrustedScore, evaluator.getHighScore()); - assertSame(untrustedScanResult, evaluator.getScanResultCandidate()); - } - - /** - * Case #42 Ensure the ExternalScoreEvaluator correctly selects the saved network. - * - * In this test. we simulate following scenario: - * The ExternalScoreEvaluator is asked to evaluate 1 untrusted network and 1 saved network. - * The saved network has the higher score. - * - * expected result: The saved network is determined to be the best network. - */ - @Test - public void externalScoreEvaluator_savedIsBest() { - WifiQualifiedNetworkSelector.ExternalScoreEvaluator evaluator = - new WifiQualifiedNetworkSelector.ExternalScoreEvaluator(mLocalLog, true); - ScanResult untrustedScanResult = new ScanResult(); - int untrustedScore = 50; - evaluator.evalUntrustedCandidate(untrustedScore, untrustedScanResult); - - ScanResult savedScanResult = new ScanResult(); - int savedScore = 100; - WifiConfiguration savedConfig = new WifiConfiguration(); - evaluator.evalSavedCandidate(savedScore, savedConfig, savedScanResult); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.SAVED_NETWORK, evaluator.getBestCandidateType()); - assertEquals(savedScore, evaluator.getHighScore()); - assertSame(savedScanResult, evaluator.getScanResultCandidate()); - } - - /** - * Case #43 Ensure the ExternalScoreEvaluator correctly selects the saved network if a - * tie occurs. - * - * In this test. we simulate following scenario: - * The ExternalScoreEvaluator is asked to evaluate 1 untrusted network and 1 saved network. - * Both networks have the same score. - * - * expected result: The saved network is determined to be the best network. - */ - @Test - public void externalScoreEvaluator_tieScores() { - WifiQualifiedNetworkSelector.ExternalScoreEvaluator evaluator = - new WifiQualifiedNetworkSelector.ExternalScoreEvaluator(mLocalLog, true); - ScanResult untrustedScanResult = new ScanResult(); - int untrustedScore = 100; - evaluator.evalUntrustedCandidate(untrustedScore, untrustedScanResult); - - ScanResult savedScanResult = new ScanResult(); - int savedScore = 100; - WifiConfiguration savedConfig = new WifiConfiguration(); - evaluator.evalSavedCandidate(savedScore, savedConfig, savedScanResult); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.SAVED_NETWORK, evaluator.getBestCandidateType()); - assertEquals(savedScore, evaluator.getHighScore()); - assertSame(savedScanResult, evaluator.getScanResultCandidate()); - } - - /** - * Case #44 Ensure the ExternalScoreEvaluator correctly selects the saved network out of - * multiple options. - * - * In this test. we simulate following scenario: - * The ExternalScoreEvaluator is asked to evaluate 2 untrusted networks and 2 saved networks. - * The high scores are equal and the low scores differ. - * - * expected result: The saved network is determined to be the best network. - */ - @Test - public void externalScoreEvaluator_multipleScores() { - WifiQualifiedNetworkSelector.ExternalScoreEvaluator evaluator = - new WifiQualifiedNetworkSelector.ExternalScoreEvaluator(mLocalLog, true); - ScanResult untrustedScanResult = new ScanResult(); - int untrustedScore = 100; - evaluator.evalUntrustedCandidate(untrustedScore, untrustedScanResult); - evaluator.evalUntrustedCandidate(80, new ScanResult()); - - ScanResult savedScanResult = new ScanResult(); - int savedScore = 100; - WifiConfiguration savedConfig = new WifiConfiguration(); - evaluator.evalSavedCandidate(savedScore, savedConfig, savedScanResult); - evaluator.evalSavedCandidate(90, new WifiConfiguration(), new ScanResult()); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.SAVED_NETWORK, evaluator.getBestCandidateType()); - assertEquals(savedScore, evaluator.getHighScore()); - assertSame(savedScanResult, evaluator.getScanResultCandidate()); - } - - /** - * Case #45 Ensure the ExternalScoreEvaluator correctly handles NULL score inputs. - * - * In this test we simulate following scenario: - * The ExternalScoreEvaluator is asked to evaluate both types of candidates with NULL scores. - * - * expected result: No crashes. The best candidate type is returned as NONE. - */ - @Test - public void externalScoreEvaluator_nullScores() { - WifiQualifiedNetworkSelector.ExternalScoreEvaluator evaluator = - new WifiQualifiedNetworkSelector.ExternalScoreEvaluator(mLocalLog, true); - evaluator.evalUntrustedCandidate(null, new ScanResult()); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.NONE, evaluator.getBestCandidateType()); - evaluator.evalSavedCandidate(null, new WifiConfiguration(), new ScanResult()); - assertEquals(WifiQualifiedNetworkSelector.ExternalScoreEvaluator - .BestCandidateType.NONE, evaluator.getBestCandidateType()); - } - - /** - * Case #48 5GHz due to user band preference is set to AUTO - * - * In this test. we simulate following scenario - * WifiStateMachine is under disconnected state - * User band preference is set to AUTO - * Two networks test1, test2 are secured network - * Both network are enabled - * test1 is @ 2GHz with RSSI -50 - * test2 is @ 5Ghz with RSSI -50 - * - * Expected behavior: test2 is chosen due to 5G bonus and user band preference - * is set to AUTO. - */ - @Test - public void chooseNetworkDisconnectUserPreferAutoBandTest() { - String[] ssids = DEFAULT_SSIDS; - String[] bssids = DEFAULT_BSSIDS; - int[] frequencies = {2437, 5180}; - String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"}; - int[] levels = {-50, -50}; - int[] security = {SECURITY_PSK, SECURITY_PSK}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - - scanResultLinkConfiguration(savedConfigs, scanDetails); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - verifySelectedResult(chosenScanResult, candidate); - } - - /** - * Case #49 Choose 2.4GHz BSSID with stronger RSSI value over - * 5GHz BSSID with weaker RSSI value - * - * In this test. we simulate following scenario: - * Two APs are found in scan results - * BSSID1 is @ 5GHz with RSSI -82 - * BSSID2 is @ 2Ghz with RSSI -72 - * These two BSSIDs get exactly the same QNS score - * - * expect BSSID2 to be chosen as it has stronger RSSI value - */ - @Test - public void chooseStrongerRssiOver5GHz() { - String[] ssids = {"\"test1\"", "\"test1\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] frequencies = {5220, 2437}; - String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {-82, -72}; - int[] security = {SECURITY_NONE, SECURITY_NONE}; - - List<ScanDetail> scanDetails = getScanDetails(ssids, bssids, frequencies, caps, levels); - WifiConfiguration[] savedConfigs = generateWifiConfigurations(ssids, security); - prepareConfigStore(savedConfigs); - scanResultLinkConfiguration(savedConfigs, scanDetails); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - - WifiConfiguration candidate = mWifiQualifiedNetworkSelector.selectQualifiedNetwork(false, - false, false, false, true, false, scanDetails); - - verifySelectedResult(chosenScanResult, candidate); - } -} |