diff options
author | Amin Shaikh <ashaikh@google.com> | 2016-12-05 12:21:29 -0800 |
---|---|---|
committer | Amin Shaikh <ashaikh@google.com> | 2016-12-12 11:40:34 -0800 |
commit | d0258ee4816148ff4ab9ac6b854fc5c51ea53be3 (patch) | |
tree | 90a4666e3e2a725fafa4b91da28dac87c4324a4c /tests | |
parent | 3b363f83de09574fe91de54e44e56a967237f4fd (diff) |
Implement RecommendedNetworkEvaluator.
- Gate usage on NETWORK_RECOMMENDATIONS_ENABLED flag
- Delegate WifiConfiguration selection to NetworkScoreManager
- Added RecommendedNetworkEvaluatorTest
Test: runtest frameworks-wifi
Bug: 33158114
Change-Id: I10b870df891269fd474daa2db53d43ac5050af2c
Diffstat (limited to 'tests')
3 files changed, 266 insertions, 3 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java new file mode 100644 index 000000000..97614ae30 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java @@ -0,0 +1,233 @@ +/* + * 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 junit.framework.Assert.assertNull; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import android.net.NetworkKey; +import android.net.NetworkScoreManager; +import android.net.RecommendationRequest; +import android.net.RecommendationResult; +import android.net.WifiKey; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiSsid; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.LocalLog; + +import com.android.server.wifi.util.ScanResultUtil; + +import com.google.android.collect.Lists; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; + +/** + * Unit tests for {@link RecommendedNetworkEvaluator}. + */ +@SmallTest +public class RecommendedNetworkEvaluatorTest { + private static final ScanDetail TRUSTED_SCAN_DETAIL = buildScanDetail("ssid"); + private static final ScanDetail UNTRUSTED_SCAN_DETAIL = buildScanDetail("ssid1"); + private static final WifiConfiguration TRUSTED_WIFI_CONFIGURATION = new WifiConfiguration(); + static { + TRUSTED_WIFI_CONFIGURATION.networkId = 5; + TRUSTED_WIFI_CONFIGURATION.SSID = TRUSTED_SCAN_DETAIL.getSSID(); + TRUSTED_WIFI_CONFIGURATION.getNetworkSelectionStatus().setCandidate( + TRUSTED_SCAN_DETAIL.getScanResult()); + } + + @Mock private NetworkScoreManager mNetworkScoreManager; + @Mock private WifiConfigManager mWifiConfigManager; + @Mock private NetworkScoreManager mScoreManager; + @Mock private WifiNetworkScoreCache mNetworkScoreCache; + + @Captor private ArgumentCaptor<NetworkKey[]> mNetworkKeyArrayCaptor; + @Captor private ArgumentCaptor<RecommendationRequest> mRecommendationRequestCaptor; + + private RecommendedNetworkEvaluator mRecommendedNetworkEvaluator; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mRecommendedNetworkEvaluator = new RecommendedNetworkEvaluator( + mNetworkScoreCache, mNetworkScoreManager, mWifiConfigManager, new LocalLog(0)); + + when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(TRUSTED_SCAN_DETAIL)) + .thenReturn(TRUSTED_WIFI_CONFIGURATION); + when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(UNTRUSTED_SCAN_DETAIL)) + .thenReturn(null); + } + + @Test + public void testUpdate_emptyScanList() { + mRecommendedNetworkEvaluator.update(new ArrayList<ScanDetail>()); + + verifyZeroInteractions(mNetworkScoreManager); + } + + @Test + public void testUpdate_allNetworksScored() { + when(mNetworkScoreCache.isScoredNetwork(TRUSTED_SCAN_DETAIL.getScanResult())) + .thenReturn(true); + when(mNetworkScoreCache.isScoredNetwork(UNTRUSTED_SCAN_DETAIL.getScanResult())) + .thenReturn(true); + + mRecommendedNetworkEvaluator.update(Lists.newArrayList(TRUSTED_SCAN_DETAIL, + UNTRUSTED_SCAN_DETAIL)); + + verifyZeroInteractions(mNetworkScoreManager); + } + + @Test + public void testUpdate_oneScored_oneUnscored() { + when(mNetworkScoreCache.isScoredNetwork(TRUSTED_SCAN_DETAIL.getScanResult())) + .thenReturn(true); + when(mNetworkScoreCache.isScoredNetwork(UNTRUSTED_SCAN_DETAIL.getScanResult())) + .thenReturn(false); + + mRecommendedNetworkEvaluator.update(Lists.newArrayList(TRUSTED_SCAN_DETAIL, + UNTRUSTED_SCAN_DETAIL)); + + verify(mNetworkScoreManager).requestScores(mNetworkKeyArrayCaptor.capture()); + + assertEquals(1, mNetworkKeyArrayCaptor.getValue().length); + NetworkKey expectedNetworkKey = new NetworkKey(new WifiKey(ScanResultUtil.createQuotedSSID( + UNTRUSTED_SCAN_DETAIL.getSSID()), UNTRUSTED_SCAN_DETAIL.getBSSIDString())); + assertEquals(expectedNetworkKey, mNetworkKeyArrayCaptor.getValue()[0]); + } + + @Test + public void testEvaluateNetworks_emptyScanList() { + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + new ArrayList<ScanDetail>(), null, null, false, + false /* untrustedNetworkAllowed */, null); + + assertNull(result); + verifyZeroInteractions(mWifiConfigManager, mNetworkScoreManager); + } + + @Test + public void testEvaluateNetworks_onlyTrustedNetworksAllowed_noTrustedInScanList() { + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(UNTRUSTED_SCAN_DETAIL), null, null, false, + false /* untrustedNetworkAllowed */, null); + + assertNull(result); + verifyZeroInteractions(mNetworkScoreManager); + } + + @Test + public void testEvaluateNetworks_untrustedNetworksAllowed_onlyDeletedEphemeral() { + when(mWifiConfigManager.wasEphemeralNetworkDeleted(ScanResultUtil + .createQuotedSSID(UNTRUSTED_SCAN_DETAIL.getScanResult().SSID))) + .thenReturn(true); + + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(UNTRUSTED_SCAN_DETAIL), null, null, false, + true /* untrustedNetworkAllowed */, null); + + assertNull(result); + verifyZeroInteractions(mNetworkScoreManager); + } + + @Test + public void testEvaluateNetworks_recommendation_onlyTrustedNetworkAllowed() { + when(mWifiConfigManager.wasEphemeralNetworkDeleted(anyString())).thenReturn(false); + when(mNetworkScoreManager.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(new RecommendationResult(TRUSTED_WIFI_CONFIGURATION)); + + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(TRUSTED_SCAN_DETAIL, UNTRUSTED_SCAN_DETAIL), + null, null, false, false /* untrustedNetworkAllowed */, null); + + assertEquals(TRUSTED_WIFI_CONFIGURATION, result); + verify(mNetworkScoreManager).requestRecommendation(mRecommendationRequestCaptor.capture()); + assertEquals(1, mRecommendationRequestCaptor.getValue().getScanResults().length); + assertEquals(TRUSTED_SCAN_DETAIL.getScanResult(), + mRecommendationRequestCaptor.getValue().getScanResults()[0]); + } + + @Test + public void testEvaluateNetworks_recommendation_untrustedNetworksAllowed() { + when(mWifiConfigManager.wasEphemeralNetworkDeleted(anyString())).thenReturn(false); + when(mNetworkScoreManager.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(new RecommendationResult(TRUSTED_WIFI_CONFIGURATION)); + + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(TRUSTED_SCAN_DETAIL, UNTRUSTED_SCAN_DETAIL), + null, null, false, true /* untrustedNetworkAllowed */, null); + + assertEquals(TRUSTED_WIFI_CONFIGURATION, result); + verify(mNetworkScoreManager).requestRecommendation(mRecommendationRequestCaptor.capture()); + assertEquals(2, mRecommendationRequestCaptor.getValue().getScanResults().length); + assertEquals(TRUSTED_SCAN_DETAIL.getScanResult(), + mRecommendationRequestCaptor.getValue().getScanResults()[0]); + assertEquals(UNTRUSTED_SCAN_DETAIL.getScanResult(), + mRecommendationRequestCaptor.getValue().getScanResults()[1]); + } + + @Test + public void testEvaluateNetworks_noRecommendation_untrustedNetworksAllowed() { + when(mWifiConfigManager.wasEphemeralNetworkDeleted(anyString())).thenReturn(false); + when(mNetworkScoreManager.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(new RecommendationResult(null)); + + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(TRUSTED_SCAN_DETAIL, UNTRUSTED_SCAN_DETAIL), + null, null, false, true /* untrustedNetworkAllowed */, null); + + assertNull(result); + verify(mNetworkScoreManager).requestRecommendation(mRecommendationRequestCaptor.capture()); + assertEquals(2, mRecommendationRequestCaptor.getValue().getScanResults().length); + assertEquals(TRUSTED_SCAN_DETAIL.getScanResult(), + mRecommendationRequestCaptor.getValue().getScanResults()[0]); + assertEquals(UNTRUSTED_SCAN_DETAIL.getScanResult(), + mRecommendationRequestCaptor.getValue().getScanResults()[1]); + } + + @Test + public void testEvaluateNetworks_nullRecommendation() { + when(mWifiConfigManager.wasEphemeralNetworkDeleted(anyString())).thenReturn(false); + when(mNetworkScoreManager.requestRecommendation(any(RecommendationRequest.class))) + .thenReturn(null); + + WifiConfiguration result = mRecommendedNetworkEvaluator.evaluateNetworks( + Lists.newArrayList(TRUSTED_SCAN_DETAIL, UNTRUSTED_SCAN_DETAIL), + null, null, false, false /* untrustedNetworkAllowed */, null); + + assertNull(result); + verify(mNetworkScoreManager).requestRecommendation(any(RecommendationRequest.class)); + } + + private static ScanDetail buildScanDetail(String ssid) { + return new ScanDetail(WifiSsid.createFromAsciiEncoded(ssid), + "00:00:00:00:00:00", "", 0, 0, 0, 0); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java index fe290cd8a..d323533fb 100644 --- a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -147,6 +147,32 @@ public class SavedNetworkEvaluatorTest { return wifiConfigManager; } + /** + * Do not evaluate networks that {@link WifiConfiguration#useExternalScores}. + */ + @Test + public void ignoreNetworksIfUseExternalScores() { + 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(); + for (WifiConfiguration wifiConfiguration : savedConfigs) { + wifiConfiguration.useExternalScores = true; + } + + WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + null, null, true, false, null); + + assertNull(candidate); + } /** * Between two 2G networks, choose the one with stronger RSSI value if other conditions diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index cb50471d2..f70c09b16 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -26,6 +26,7 @@ import android.app.test.MockAnswerUtil.AnswerWithArguments; import android.app.test.TestAlarmManager; import android.content.Context; import android.content.res.Resources; +import android.net.NetworkScoreManager; import android.net.wifi.ScanResult; import android.net.wifi.ScanResult.InformationElement; import android.net.wifi.SupplicantState; @@ -62,7 +63,6 @@ import java.util.HashSet; */ @SmallTest public class WifiConnectivityManagerTest { - /** * Called before each test */ @@ -92,6 +92,7 @@ public class WifiConnectivityManagerTest { } private Resources mResource; + private Context mContext; private TestAlarmManager mAlarmManager; private TestLooper mLooper = new TestLooper(); @@ -102,6 +103,8 @@ public class WifiConnectivityManagerTest { private ScanData mScanData; private WifiConfigManager mWifiConfigManager; private WifiInfo mWifiInfo; + @Mock private FrameworkFacade mFrameworkFacade; + @Mock private NetworkScoreManager mNetworkScoreManager; @Mock private Clock mClock; @Mock private WifiLastResortWatchdog mWifiLastResortWatchdog; @Mock private WifiMetrics mWifiMetrics; @@ -253,8 +256,9 @@ public class WifiConnectivityManagerTest { WifiConnectivityManager createConnectivityManager() { return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, - mWifiConfigManager, mWifiInfo, mWifiNS, mScoreCache, mWifiLastResortWatchdog, - mWifiMetrics, mLooper.getLooper(), mClock, true); + mWifiConfigManager, mWifiInfo, mWifiNS, + mWifiLastResortWatchdog, mWifiMetrics, mLooper.getLooper(), mClock, true, + mFrameworkFacade, null, null, null); } /** |