diff options
author | Glen Kuhne <kuh@google.com> | 2016-03-30 11:42:02 -0700 |
---|---|---|
committer | Glen Kuhne <kuh@google.com> | 2016-04-14 11:08:50 -0700 |
commit | 09abbe29be6e552a2531b0367bd6d29647d33767 (patch) | |
tree | a7f7d26414491754e55a4176f74e9a9358921841 /tests | |
parent | 42396f489e485671100353beb6102d315ff3a542 (diff) |
Created LastResortWatchdog class
Created the WifiLastResortWatchdog class, that will handle restarting
the wifi stack as a last resort when wifi is failing.
Implemented the logic that tracks failure counts for all available
networks, and the maintaining of that list of available networks.
BUG=27856523
Change-Id: I8c944d033955ad7e5cc9484cce8f20cd3755312e
Diffstat (limited to 'tests')
3 files changed, 265 insertions, 22 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 307614f4a..4b2f79f24 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -52,17 +52,17 @@ public class WifiConnectivityManagerTest { */ @Before public void setUp() throws Exception { - mResource = getResource(); - mAlarmManager = getAlarmManager(); - mContext = getContext(); - mWifiStateMachine = getWifiStateMachine(); - mWifiConfigManager = getWifiConfigManager(); - mWifiInfo = getWifiInfo(); - mWifiScanner = getWifiScanner(); - mWifiQNS = getWifiQualifiedNetworkSelector(); - + mWifiInjector = mockWifiInjector(); + mResource = mockResource(); + mAlarmManager = mockAlarmManager(); + mContext = mockContext(); + mWifiStateMachine = mockWifiStateMachine(); + mWifiConfigManager = mockWifiConfigManager(); + mWifiInfo = mockWifiInfo(); + mWifiScanner = mockWifiScanner(); + mWifiQNS = mockWifiQualifiedNetworkSelector(); mWifiConnectivityManager = new WifiConnectivityManager(mContext, mWifiStateMachine, - mWifiScanner, mWifiConfigManager, mWifiInfo, mWifiQNS); + mWifiScanner, mWifiConfigManager, mWifiInfo, mWifiQNS, mWifiInjector); mWifiConnectivityManager.setWifiEnabled(true); } @@ -83,13 +83,15 @@ public class WifiConnectivityManagerTest { private WifiScanner mWifiScanner; private WifiConfigManager mWifiConfigManager; private WifiInfo mWifiInfo; + private WifiLastResortWatchdog mWifiLastResortWatchdog; + private WifiInjector mWifiInjector; private static final int CANDIDATE_NETWORK_ID = 0; private static final String CANDIDATE_SSID = "\"AnSsid\""; private static final String CANDIDATE_BSSID = "6c:f3:7f:ae:8c:f3"; private static final String TAG = "WifiConnectivityManager Unit Test"; - Resources getResource() { + Resources mockResource() { Resources resource = mock(Resources.class); when(resource.getInteger(R.integer.config_wifi_framework_SECURITY_AWARD)).thenReturn(80); @@ -98,13 +100,13 @@ public class WifiConnectivityManagerTest { return resource; } - AlarmManager getAlarmManager() { + AlarmManager mockAlarmManager() { AlarmManager alarmManager = mock(AlarmManager.class); return alarmManager; } - Context getContext() { + Context mockContext() { Context context = mock(Context.class); when(context.getResources()).thenReturn(mResource); @@ -114,7 +116,7 @@ public class WifiConnectivityManagerTest { return context; } - WifiScanner getWifiScanner() { + WifiScanner mockWifiScanner() { WifiScanner scanner = mock(WifiScanner.class); // dummy scan results. QNS PeriodicScanListener bulids scanDetails from @@ -137,7 +139,7 @@ public class WifiConnectivityManagerTest { return scanner; } - WifiStateMachine getWifiStateMachine() { + WifiStateMachine mockWifiStateMachine() { WifiStateMachine stateMachine = mock(WifiStateMachine.class); when(stateMachine.getFrequencyBand()).thenReturn(1); @@ -149,7 +151,7 @@ public class WifiConnectivityManagerTest { return stateMachine; } - WifiQualifiedNetworkSelector getWifiQualifiedNetworkSelector() { + WifiQualifiedNetworkSelector mockWifiQualifiedNetworkSelector() { WifiQualifiedNetworkSelector qns = mock(WifiQualifiedNetworkSelector.class); WifiConfiguration candidate = generateWifiConfig( @@ -162,11 +164,10 @@ public class WifiConnectivityManagerTest { when(qns.selectQualifiedNetwork(anyBoolean(), anyBoolean(), anyObject(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(candidate); - return qns; } - WifiInfo getWifiInfo() { + WifiInfo mockWifiInfo() { WifiInfo wifiInfo = mock(WifiInfo.class); when(wifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID); @@ -175,7 +176,7 @@ public class WifiConnectivityManagerTest { return wifiInfo; } - WifiConfigManager getWifiConfigManager() { + WifiConfigManager mockWifiConfigManager() { WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class); when(wifiConfigManager.getWifiConfiguration(anyInt())).thenReturn(null); @@ -187,6 +188,12 @@ public class WifiConnectivityManagerTest { return wifiConfigManager; } + WifiInjector mockWifiInjector() { + WifiInjector wifiInjector = mock(WifiInjector.class); + mWifiLastResortWatchdog = mock(WifiLastResortWatchdog.class); + when(wifiInjector.getWifiLastResortWatchdog()).thenReturn(mWifiLastResortWatchdog); + return wifiInjector; + } /** * Wifi enters disconnected state while screen is on. diff --git a/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java new file mode 100644 index 000000000..fc38e62ab --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java @@ -0,0 +1,236 @@ +/* + * 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 org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiSsid; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.Pair; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.WifiLastResortWatchdog}. + */ +@SmallTest +public class WifiLastResortWatchdogTest { + WifiLastResortWatchdog mLastResortWatchdog; + + @Before + public void setUp() throws Exception { + mLastResortWatchdog = new WifiLastResortWatchdog(); + } + + private List<Pair<ScanDetail, WifiConfiguration>> createFilteredQnsCandidates(String[] ssids, + String[] bssids, int[] frequencies, String[] caps, int[] levels, + boolean[] isEphemeral) { + List<Pair<ScanDetail, WifiConfiguration>> candidates = new ArrayList<>(); + long timeStamp = System.currentTimeMillis(); + 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); + WifiConfiguration config = null; + if (!isEphemeral[index]) { + config = mock(WifiConfiguration.class); + WifiConfiguration.NetworkSelectionStatus networkSelectionStatus = + mock(WifiConfiguration.NetworkSelectionStatus.class); + when(config.getNetworkSelectionStatus()).thenReturn(networkSelectionStatus); + when(networkSelectionStatus.getHasEverConnected()).thenReturn(true); + } + candidates.add(Pair.create(scanDetail, config)); + } + return candidates; + } + + /** + * Case #1: Test aging works in available network buffering + * This test simulates 4 networks appearing in a scan result, and then only the first 2 + * appearing in successive scans results. + * Expected Behavior: + * 4 networks appear in recentAvailalbeNetworks, after N=MAX_BSSID_AGE scans, only 2 remain + */ + @Test + public void testAvailableNetworkBuffering_ageCullingWorks() throws Exception { + 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}; + boolean[] isEphemeral = {false, false, false, false}; + + // Buffer potential candidates 1,2,3 & 4 + List<Pair<ScanDetail, WifiConfiguration>> candidates = createFilteredQnsCandidates(ssids, + bssids, frequencies, caps, levels, isEphemeral); + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 4); + + // Repeatedly buffer candidates 1 & 2, MAX_BSSID_AGE - 1 times + candidates = createFilteredQnsCandidates(Arrays.copyOfRange(ssids, 0, 2), + Arrays.copyOfRange(bssids, 0, 2), + Arrays.copyOfRange(frequencies, 0, 2), + Arrays.copyOfRange(caps, 0, 2), + Arrays.copyOfRange(levels, 0, 2), + Arrays.copyOfRange(isEphemeral, 0, 2)); + for (int i = 0; i < WifiLastResortWatchdog.MAX_BSSID_AGE - 1; i++) { + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[0]).age, 0); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[1]).age, 0); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[2]).age, i+1); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[3]).age, i+1); + } + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 4); + + // One more buffering should age and cull candidates 2 & 3 + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 2); + }; + + /** + * Case #2: Culling of old networks + * Part 1: + * This test starts with 4 networks seen, it then buffers N=MAX_BSSID_AGE empty scans + * Expected behaviour: All networks are culled from recentAvailableNetworks + * + * Part 2: + * Buffer some more empty scans just to make sure nothing breaks + */ + @Test + public void testAvailableNetworkBuffering_emptyBufferWithEmptyScanResults() throws Exception { + 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}; + boolean[] isEphemeral = {false, false, false, false}; + + // Buffer potential candidates 1,2,3 & 4 + List<Pair<ScanDetail, WifiConfiguration>> candidates = createFilteredQnsCandidates(ssids, + bssids, frequencies, caps, levels, isEphemeral); + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 4); + + // Repeatedly buffer with no candidates + candidates = createFilteredQnsCandidates(Arrays.copyOfRange(ssids, 0, 0), + Arrays.copyOfRange(bssids, 0, 0), + Arrays.copyOfRange(frequencies, 0, 0), + Arrays.copyOfRange(caps, 0, 0), + Arrays.copyOfRange(levels, 0, 0), + Arrays.copyOfRange(isEphemeral, 0, 0)); + for (int i = 0; i < WifiLastResortWatchdog.MAX_BSSID_AGE; i++) { + mLastResortWatchdog.updateAvailableNetworks(candidates); + } + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 0); + for (int i = 0; i < WifiLastResortWatchdog.MAX_BSSID_AGE; i++) { + mLastResortWatchdog.updateAvailableNetworks(candidates); + } + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 0); + }; + + /** + * Case 3: Adding more networks over time + * In this test, each successive (4 total) scan result buffers one more network. + * Expected behavior: recentAvailableNetworks grows with number of scan results + */ + @Test + public void testAvailableNetworkBuffering_addNewNetworksOverTime() throws Exception { + 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}; + boolean[] isEphemeral = {false, false, false, false}; + List<Pair<ScanDetail, WifiConfiguration>> candidates; + // Buffer (i) scan results with each successive scan result + for (int i = 1; i <= ssids.length; i++) { + candidates = createFilteredQnsCandidates(Arrays.copyOfRange(ssids, 0, i), + Arrays.copyOfRange(bssids, 0, i), + Arrays.copyOfRange(frequencies, 0, i), + Arrays.copyOfRange(caps, 0, i), + Arrays.copyOfRange(levels, 0, i), + Arrays.copyOfRange(isEphemeral, 0, i)); + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), i); + for (int j = 0; j < i; j++) { + assertEquals( + mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[j]).age, 0); + } + } + }; + + /** + * Case 4: Test buffering with ephemeral networks & toString() + * This test is the same as Case 1, but it also includes ephemeral networks. toString is also + * smoke tested at various places in this test + * Expected behaviour: 4 networks added initially (2 ephemeral). After MAX_BSSID_AGE more + * bufferings, 2 are culled (leaving 1 ephemeral, one normal). toString method should execute + * without breaking anything. + */ + @Test + public void testAvailableNetworkBuffering_multipleNetworksSomeEphemeral() throws Exception { + 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}; + boolean[] isEphemeral = {true, false, true, false}; + + // Buffer potential candidates 1,2,3 & 4 + List<Pair<ScanDetail, WifiConfiguration>> candidates = createFilteredQnsCandidates(ssids, + bssids, frequencies, caps, levels, isEphemeral); + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 4); + + // Repeatedly buffer candidates 1 & 2, MAX_BSSID_AGE - 1 times + candidates = createFilteredQnsCandidates(Arrays.copyOfRange(ssids, 0, 2), + Arrays.copyOfRange(bssids, 0, 2), + Arrays.copyOfRange(frequencies, 0, 2), + Arrays.copyOfRange(caps, 0, 2), + Arrays.copyOfRange(levels, 0, 2), + Arrays.copyOfRange(isEphemeral, 0, 2)); + for (int i = 0; i < WifiLastResortWatchdog.MAX_BSSID_AGE - 1; i++) { + mLastResortWatchdog.updateAvailableNetworks(candidates); + mLastResortWatchdog.toString(); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[0]).age, 0); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[1]).age, 0); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[2]).age, i+1); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().get(bssids[3]).age, i+1); + } + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 4); + + // One more buffering should age and cull candidates 2 & 3 + mLastResortWatchdog.updateAvailableNetworks(candidates); + assertEquals(mLastResortWatchdog.getRecentAvailableNetworks().size(), 2); + mLastResortWatchdog.toString(); + }; +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java index 92e16b392..5af267b19 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiQualifiedNetworkSelectorTest.java @@ -1726,9 +1726,9 @@ public class WifiQualifiedNetworkSelectorTest { verifySelectedResult(chosenScanResult, candidate); //Verify two scanDetails returned in the filteredScanDetails assertEquals(2, mWifiQualifiedNetworkSelector.getFilteredScanDetails().size()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).toString(), + assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).first.toString(), scanDetails.get(0).toString()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(1).toString(), + assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(1).first.toString(), scanDetails.get(3).toString()); } @@ -1805,7 +1805,7 @@ public class WifiQualifiedNetworkSelectorTest { verifySelectedResult(chosenScanResult, candidate); //Verify two scanDetails returned in the filteredScanDetails assertEquals(1, mWifiQualifiedNetworkSelector.getFilteredScanDetails().size()); - assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).toString(), + assertEquals(mWifiQualifiedNetworkSelector.getFilteredScanDetails().get(0).first.toString(), scanDetails.get(0).toString()); } } |