From e6d7f23569585f8f0fb02adbef992d3f1430db44 Mon Sep 17 00:00:00 2001 From: Ningyuan Wang Date: Tue, 14 Feb 2017 16:56:26 -0800 Subject: Request single shot scan via wificond Bug: 34715459 Bug: 33398008 Test: compile, unit tests, integration tests Change-Id: Ie44ae50d9d498bd164bd407259f013dd367e4ed2 --- .../android/server/wifi/WificondControlTest.java | 142 +++++++++++++++++++-- 1 file changed, 132 insertions(+), 10 deletions(-) (limited to 'tests/wifitests/src/com/android/server/wifi/WificondControlTest.java') diff --git a/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java b/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java index ea366d850..ec0bb20fb 100644 --- a/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WificondControlTest.java @@ -19,7 +19,10 @@ package com.android.server.wifi; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -30,13 +33,20 @@ import android.net.wifi.IWifiScannerImpl; import android.net.wifi.IWificond; import android.test.suitebuilder.annotation.SmallTest; +import com.android.server.wifi.util.NativeUtil; +import com.android.server.wifi.wificond.ChannelSettings; +import com.android.server.wifi.wificond.HiddenNetwork; import com.android.server.wifi.wificond.NativeScanResult; +import com.android.server.wifi.wificond.SingleScanSettings; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatcher; import java.util.ArrayList; import java.util.BitSet; +import java.util.HashSet; +import java.util.Set; /** * Unit tests for {@link com.android.server.wifi.WificondControl}. @@ -76,6 +86,20 @@ public class WificondControlTest { associated = TEST_ASSOCIATED; }}; + private static final Set SCAN_FREQ_SET = + new HashSet() {{ + add(2410); + add(2450); + add(5050); + add(5200); + }}; + private static final Set SCAN_HIDDEN_NETWORK_SSID_SET = + new HashSet() {{ + // These SSIDs should be quoted. + add("\"hiddenAP1\""); + add("\"hiddenAP2\""); + }}; + @Before public void setUp() throws Exception { mWifiInjector = mock(WifiInjector.class); @@ -377,21 +401,13 @@ public class WificondControlTest { */ @Test public void testGetScanResults() throws Exception { - IWificond wificond = mock(IWificond.class); - IClientInterface clientInterface = mock(IClientInterface.class); - IWifiScannerImpl scanner = mock(IWifiScannerImpl.class); - - when(mWifiInjector.makeWificond()).thenReturn(wificond); - when(wificond.createClientInterface()).thenReturn(clientInterface); - when(clientInterface.getWifiScannerImpl()).thenReturn(scanner); + IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner(); + assertNotNull(scanner); // Mock the returned array of NativeScanResult. NativeScanResult[] mockScanResults = {MOCK_NATIVE_SCAN_RESULT}; when(scanner.getScanResults()).thenReturn(mockScanResults); - // Configure client interface. - IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode(); - assertEquals(clientInterface, returnedClientInterface); ArrayList returnedScanResults = mWificondControl.getScanResults(); assertEquals(mockScanResults.length, returnedScanResults.size()); // Since NativeScanResult is organized differently from ScanResult, this only checks @@ -405,4 +421,110 @@ public class WificondControlTest { returnedScanResults.get(i).getScanResult().timestamp); } } + + /** + * Verifies that Scan() can convert input parameters to SingleScanSettings correctly. + */ + @Test + public void testScan() throws Exception { + IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner(); + + when(scanner.scan(any(SingleScanSettings.class))).thenReturn(true); + + assertTrue(mWificondControl.scan(SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET)); + verify(scanner).scan(argThat(new ScanMatcher( + SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET))); + } + + /** + * Verifies that Scan() can handle null input parameters correctly. + */ + @Test + public void testScanNullParameters() throws Exception { + IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner(); + + when(scanner.scan(any(SingleScanSettings.class))).thenReturn(true); + + assertTrue(mWificondControl.scan(null, null)); + verify(scanner).scan(argThat(new ScanMatcher(null, null))); + } + + /** + * Verifies that Scan() can handle wificond scan failure. + */ + @Test + public void testScanFailure() throws Exception { + IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner(); + + when(scanner.scan(any(SingleScanSettings.class))).thenReturn(false); + assertFalse(mWificondControl.scan(SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET)); + verify(scanner).scan(any(SingleScanSettings.class)); + } + + /** + * Helper method: Setup interface to client mode for mWificondControl. + * Returns a mock IWifiScannerImpl. + */ + private IWifiScannerImpl setupClientInterfaceAndCreateMockWificondScanner() throws Exception { + IWificond wificond = mock(IWificond.class); + IClientInterface clientInterface = mock(IClientInterface.class); + IWifiScannerImpl scanner = mock(IWifiScannerImpl.class); + + when(mWifiInjector.makeWificond()).thenReturn(wificond); + when(wificond.createClientInterface()).thenReturn(clientInterface); + when(clientInterface.getWifiScannerImpl()).thenReturn(scanner); + + assertEquals(clientInterface, mWificondControl.setupDriverForClientMode()); + + return scanner; + } + + // Create a ArgumentMatcher which captures a SingleScanSettings parameter and checks if it + // matches the provided frequency set and ssid set. + private class ScanMatcher extends ArgumentMatcher { + private final Set mExpectedFreqs; + private final Set mExpectedSsids; + ScanMatcher(Set expectedFreqs, Set expectedSsids) { + this.mExpectedFreqs = expectedFreqs; + this.mExpectedSsids = expectedSsids; + } + + @Override + public boolean matches(Object argument) { + SingleScanSettings settings = (SingleScanSettings) argument; + ArrayList channelSettings = settings.channelSettings; + ArrayList hiddenNetworks = settings.hiddenNetworks; + if (mExpectedFreqs != null) { + Set freqSet = new HashSet(); + for (ChannelSettings channel : channelSettings) { + freqSet.add(channel.frequency); + } + if (!mExpectedFreqs.equals(freqSet)) { + return false; + } + } else { + if (channelSettings != null && channelSettings.size() > 0) { + return false; + } + } + + if (mExpectedSsids != null) { + Set ssidSet = new HashSet(); + for (HiddenNetwork network : hiddenNetworks) { + ssidSet.add(NativeUtil.encodeSsid( + NativeUtil.byteArrayToArrayList(network.ssid))); + } + if (!mExpectedSsids.equals(ssidSet)) { + return false; + } + + } else { + if (hiddenNetworks != null && hiddenNetworks.size() > 0) { + return false; + } + } + return true; + } + } + } -- cgit v1.2.3