WificondControlTest.java revision 160c5fd3f82eeed7271d0efc44b6a409c9176326
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import static org.junit.Assert.assertArrayEquals;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertTrue;
24import static org.mockito.Matchers.argThat;
25import static org.mockito.Mockito.any;
26import static org.mockito.Mockito.mock;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
29
30import android.net.wifi.IApInterface;
31import android.net.wifi.IClientInterface;
32import android.net.wifi.IPnoScanEvent;
33import android.net.wifi.IScanEvent;
34import android.net.wifi.IWifiScannerImpl;
35import android.net.wifi.IWificond;
36import android.net.wifi.WifiScanner;
37import android.test.suitebuilder.annotation.SmallTest;
38
39import com.android.server.wifi.util.NativeUtil;
40import com.android.server.wifi.wificond.ChannelSettings;
41import com.android.server.wifi.wificond.HiddenNetwork;
42import com.android.server.wifi.wificond.NativeScanResult;
43import com.android.server.wifi.wificond.PnoSettings;
44import com.android.server.wifi.wificond.SingleScanSettings;
45
46import org.junit.Before;
47import org.junit.Test;
48import org.mockito.ArgumentCaptor;
49import org.mockito.compat.ArgumentMatcher;
50
51import java.util.ArrayList;
52import java.util.BitSet;
53import java.util.HashSet;
54import java.util.Set;
55
56/**
57 * Unit tests for {@link com.android.server.wifi.WificondControl}.
58 */
59@SmallTest
60public class WificondControlTest {
61    private WifiInjector mWifiInjector;
62    private WifiMonitor mWifiMonitor;
63    private WificondControl mWificondControl;
64    private static final byte[] TEST_SSID =
65            new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
66    private static final byte[] TEST_BSSID =
67            new byte[] {(byte) 0x12, (byte) 0xef, (byte) 0xa1,
68                        (byte) 0x2c, (byte) 0x97, (byte) 0x8b};
69    // This the IE buffer which is consistent with TEST_SSID.
70    private static final byte[] TEST_INFO_ELEMENT =
71            new byte[] {
72                    // Element ID for SSID.
73                    (byte) 0x00,
74                    // Length of the SSID: 0x0b or 11.
75                    (byte) 0x0b,
76                    // This is string "GoogleGuest"
77                    'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
78
79    private static final int TEST_FREQUENCY = 2456;
80    private static final int TEST_SIGNAL_MBM = -4500;
81    private static final long TEST_TSF = 34455441;
82    private static final BitSet TEST_CAPABILITY = new BitSet(16) {{ set(2); set(5); }};
83    private static final boolean TEST_ASSOCIATED = true;
84    private static final NativeScanResult MOCK_NATIVE_SCAN_RESULT =
85            new NativeScanResult() {{
86                ssid = TEST_SSID;
87                bssid = TEST_BSSID;
88                infoElement = TEST_INFO_ELEMENT;
89                frequency = TEST_FREQUENCY;
90                signalMbm = TEST_SIGNAL_MBM;
91                capability = TEST_CAPABILITY;
92                associated = TEST_ASSOCIATED;
93            }};
94
95    private static final Set<Integer> SCAN_FREQ_SET =
96            new HashSet<Integer>() {{
97                add(2410);
98                add(2450);
99                add(5050);
100                add(5200);
101            }};
102    private static final String TEST_QUOTED_SSID_1 = "\"testSsid1\"";
103    private static final String TEST_QUOTED_SSID_2 = "\"testSsid2\"";
104
105    private static final Set<String> SCAN_HIDDEN_NETWORK_SSID_SET =
106            new HashSet<String>() {{
107                add(TEST_QUOTED_SSID_1);
108                add(TEST_QUOTED_SSID_2);
109            }};
110
111
112    private static final WifiNative.PnoSettings TEST_PNO_SETTINGS =
113            new WifiNative.PnoSettings() {{
114                isConnected = false;
115                periodInMs = 6000;
116                networkList = new WifiNative.PnoNetwork[2];
117                networkList[0] = new WifiNative.PnoNetwork();
118                networkList[1] = new WifiNative.PnoNetwork();
119                networkList[0].ssid = TEST_QUOTED_SSID_1;
120                networkList[0].flags = WifiScanner.PnoSettings.PnoNetwork.FLAG_DIRECTED_SCAN;
121                networkList[1].ssid = TEST_QUOTED_SSID_2;
122                networkList[1].flags = 0;
123            }};
124
125    @Before
126    public void setUp() throws Exception {
127        mWifiInjector = mock(WifiInjector.class);
128        mWifiMonitor = mock(WifiMonitor.class);
129        mWificondControl = new WificondControl(mWifiInjector, mWifiMonitor);
130    }
131
132    /**
133     * Verifies that setupDriverForClientMode() calls Wificond.
134     */
135    @Test
136    public void testSetupDriverForClientMode() throws Exception {
137        IWificond wificond = mock(IWificond.class);
138        IClientInterface clientInterface = getMockClientInterface();
139
140        when(mWifiInjector.makeWificond()).thenReturn(wificond);
141        when(wificond.createClientInterface()).thenReturn(clientInterface);
142
143        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
144        assertEquals(clientInterface, returnedClientInterface);
145        verify(wificond).createClientInterface();
146    }
147
148    /**
149     * Verifies that setupDriverForClientMode() calls subscribeScanEvents().
150     */
151    @Test
152    public void testSetupDriverForClientModeCallsScanEventSubscripiton() throws Exception {
153        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
154
155        verify(scanner).subscribeScanEvents(any(IScanEvent.class));
156    }
157
158    /**
159     * Verifies that setupDriverForClientMode() returns null when wificond is not started.
160     */
161    @Test
162    public void testSetupDriverForClientModeErrorWhenWificondIsNotStarted() throws Exception {
163        when(mWifiInjector.makeWificond()).thenReturn(null);
164
165        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
166        assertEquals(null, returnedClientInterface);
167    }
168
169    /**
170     * Verifies that setupDriverForClientMode() returns null when wificond failed to setup client
171     * interface.
172     */
173    @Test
174    public void testSetupDriverForClientModeErrorWhenWificondFailedToSetupInterface()
175            throws Exception {
176        IWificond wificond = mock(IWificond.class);
177
178        when(mWifiInjector.makeWificond()).thenReturn(wificond);
179        when(wificond.createClientInterface()).thenReturn(null);
180
181        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
182        assertEquals(null, returnedClientInterface);
183    }
184
185    /**
186     * Verifies that setupDriverForSoftApMode() calls wificond.
187     */
188    @Test
189    public void testSetupDriverForSoftApMode() throws Exception {
190        IWificond wificond = mock(IWificond.class);
191        IApInterface apInterface = mock(IApInterface.class);
192
193        when(mWifiInjector.makeWificond()).thenReturn(wificond);
194        when(wificond.createApInterface()).thenReturn(apInterface);
195
196        IApInterface returnedApInterface = mWificondControl.setupDriverForSoftApMode();
197        assertEquals(apInterface, returnedApInterface);
198        verify(wificond).createApInterface();
199    }
200
201    /**
202     * Verifies that setupDriverForSoftAp() returns null when wificond is not started.
203     */
204    @Test
205    public void testSetupDriverForSoftApModeErrorWhenWificondIsNotStarted() throws Exception {
206        when(mWifiInjector.makeWificond()).thenReturn(null);
207
208        IApInterface returnedApInterface = mWificondControl.setupDriverForSoftApMode();
209
210        assertEquals(null, returnedApInterface);
211    }
212
213    /**
214     * Verifies that setupDriverForSoftApMode() returns null when wificond failed to setup
215     * AP interface.
216     */
217    @Test
218    public void testSetupDriverForSoftApModeErrorWhenWificondFailedToSetupInterface()
219            throws Exception {
220        IWificond wificond = mock(IWificond.class);
221
222        when(mWifiInjector.makeWificond()).thenReturn(wificond);
223        when(wificond.createApInterface()).thenReturn(null);
224
225        IApInterface returnedApInterface = mWificondControl.setupDriverForSoftApMode();
226        assertEquals(null, returnedApInterface);
227    }
228
229    /**
230     * Verifies that enableSupplicant() calls wificond.
231     */
232    @Test
233    public void testEnableSupplicant() throws Exception {
234        IWificond wificond = mock(IWificond.class);
235        IClientInterface clientInterface = getMockClientInterface();
236
237        when(mWifiInjector.makeWificond()).thenReturn(wificond);
238        when(wificond.createClientInterface()).thenReturn(clientInterface);
239        when(clientInterface.enableSupplicant()).thenReturn(true);
240
241        mWificondControl.setupDriverForClientMode();
242        assertTrue(mWificondControl.enableSupplicant());
243        verify(clientInterface).enableSupplicant();
244    }
245
246    /**
247     * Verifies that enableSupplicant() returns false when there is no configured
248     * client interface.
249     */
250    @Test
251    public void testEnableSupplicantErrorWhenNoClientInterfaceConfigured() throws Exception {
252        IWificond wificond = mock(IWificond.class);
253        IClientInterface clientInterface = getMockClientInterface();
254
255        when(mWifiInjector.makeWificond()).thenReturn(wificond);
256        when(wificond.createClientInterface()).thenReturn(clientInterface);
257
258        // Configure client interface.
259        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
260        assertEquals(clientInterface, returnedClientInterface);
261
262        // Tear down interfaces.
263        assertTrue(mWificondControl.tearDownInterfaces());
264
265        // Enabling supplicant should fail.
266        assertFalse(mWificondControl.enableSupplicant());
267    }
268
269    /**
270     * Verifies that disableSupplicant() calls wificond.
271     */
272    @Test
273    public void testDisableSupplicant() throws Exception {
274        IWificond wificond = mock(IWificond.class);
275        IClientInterface clientInterface = getMockClientInterface();
276
277        when(mWifiInjector.makeWificond()).thenReturn(wificond);
278        when(wificond.createClientInterface()).thenReturn(clientInterface);
279        when(clientInterface.disableSupplicant()).thenReturn(true);
280
281        mWificondControl.setupDriverForClientMode();
282        assertTrue(mWificondControl.disableSupplicant());
283        verify(clientInterface).disableSupplicant();
284    }
285
286    /**
287     * Verifies that disableSupplicant() returns false when there is no configured
288     * client interface.
289     */
290    @Test
291    public void testDisableSupplicantErrorWhenNoClientInterfaceConfigured() throws Exception {
292        IWificond wificond = mock(IWificond.class);
293        IClientInterface clientInterface = getMockClientInterface();
294
295        when(mWifiInjector.makeWificond()).thenReturn(wificond);
296        when(wificond.createClientInterface()).thenReturn(clientInterface);
297
298        // Configure client interface.
299        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
300        assertEquals(clientInterface, returnedClientInterface);
301
302        // Tear down interfaces.
303        assertTrue(mWificondControl.tearDownInterfaces());
304
305        // Disabling supplicant should fail.
306        assertFalse(mWificondControl.disableSupplicant());
307    }
308
309    /**
310     * Verifies that tearDownInterfaces() calls wificond.
311     */
312    @Test
313    public void testTearDownInterfaces() throws Exception {
314        IWificond wificond = mock(IWificond.class);
315
316        when(mWifiInjector.makeWificond()).thenReturn(wificond);
317        assertTrue(mWificondControl.tearDownInterfaces());
318
319        verify(wificond).tearDownInterfaces();
320    }
321
322    /**
323     * Verifies that tearDownInterfaces() calls unsubscribeScanEvents() when there was
324     * a configured client interface.
325     */
326    @Test
327    public void testTearDownInterfacesRemovesScanEventSubscription() throws Exception {
328        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
329
330        assertTrue(mWificondControl.tearDownInterfaces());
331
332        verify(scanner).unsubscribeScanEvents();
333    }
334
335
336    /**
337     * Verifies that tearDownInterfaces() returns false when wificond is not started.
338     */
339    @Test
340    public void testTearDownInterfacesErrorWhenWificondIsNotStarterd() throws Exception {
341        when(mWifiInjector.makeWificond()).thenReturn(null);
342
343        assertFalse(mWificondControl.tearDownInterfaces());
344    }
345
346    /**
347     * Verifies that signalPoll() calls wificond.
348     */
349    @Test
350    public void testSignalPoll() throws Exception {
351        IWificond wificond = mock(IWificond.class);
352        IClientInterface clientInterface = getMockClientInterface();
353
354        when(mWifiInjector.makeWificond()).thenReturn(wificond);
355        when(wificond.createClientInterface()).thenReturn(clientInterface);
356
357        mWificondControl.setupDriverForClientMode();
358        mWificondControl.signalPoll();
359        verify(clientInterface).signalPoll();
360    }
361
362    /**
363     * Verifies that signalPoll() returns null when there is no configured client interface.
364     */
365    @Test
366    public void testSignalPollErrorWhenNoClientInterfaceConfigured() throws Exception {
367        IWificond wificond = mock(IWificond.class);
368        IClientInterface clientInterface = getMockClientInterface();
369
370        when(mWifiInjector.makeWificond()).thenReturn(wificond);
371        when(wificond.createClientInterface()).thenReturn(clientInterface);
372
373        // Configure client interface.
374        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
375        assertEquals(clientInterface, returnedClientInterface);
376
377        // Tear down interfaces.
378        assertTrue(mWificondControl.tearDownInterfaces());
379
380        // Signal poll should fail.
381        assertEquals(null, mWificondControl.signalPoll());
382    }
383
384    /**
385     * Verifies that getTxPacketCounters() calls wificond.
386     */
387    @Test
388    public void testGetTxPacketCounters() throws Exception {
389        IWificond wificond = mock(IWificond.class);
390        IClientInterface clientInterface = getMockClientInterface();
391
392        when(mWifiInjector.makeWificond()).thenReturn(wificond);
393        when(wificond.createClientInterface()).thenReturn(clientInterface);
394
395        mWificondControl.setupDriverForClientMode();
396        mWificondControl.getTxPacketCounters();
397        verify(clientInterface).getPacketCounters();
398    }
399
400    /**
401     * Verifies that getTxPacketCounters() returns null when there is no configured client
402     * interface.
403     */
404    @Test
405    public void testGetTxPacketCountersErrorWhenNoClientInterfaceConfigured() throws Exception {
406        IWificond wificond = mock(IWificond.class);
407        IClientInterface clientInterface = getMockClientInterface();
408
409        when(mWifiInjector.makeWificond()).thenReturn(wificond);
410        when(wificond.createClientInterface()).thenReturn(clientInterface);
411
412        // Configure client interface.
413        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
414        assertEquals(clientInterface, returnedClientInterface);
415
416        // Tear down interfaces.
417        assertTrue(mWificondControl.tearDownInterfaces());
418
419        // Signal poll should fail.
420        assertEquals(null, mWificondControl.getTxPacketCounters());
421    }
422
423    /**
424     * Verifies that getScanResults() returns null when there is no configured client
425     * interface.
426     */
427    @Test
428    public void testGetScanResultsErrorWhenNoClientInterfaceConfigured() throws Exception {
429        IWificond wificond = mock(IWificond.class);
430        IClientInterface clientInterface = getMockClientInterface();
431
432        when(mWifiInjector.makeWificond()).thenReturn(wificond);
433        when(wificond.createClientInterface()).thenReturn(clientInterface);
434
435        // Configure client interface.
436        IClientInterface returnedClientInterface = mWificondControl.setupDriverForClientMode();
437        assertEquals(clientInterface, returnedClientInterface);
438
439        // Tear down interfaces.
440        assertTrue(mWificondControl.tearDownInterfaces());
441
442        // getScanResults should fail.
443        assertEquals(0, mWificondControl.getScanResults().size());
444    }
445
446    /**
447     * Verifies that getScanResults() can parse NativeScanResult from wificond correctly,
448     */
449    @Test
450    public void testGetScanResults() throws Exception {
451        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
452        assertNotNull(scanner);
453
454        // Mock the returned array of NativeScanResult.
455        NativeScanResult[] mockScanResults = {MOCK_NATIVE_SCAN_RESULT};
456        when(scanner.getScanResults()).thenReturn(mockScanResults);
457
458        ArrayList<ScanDetail> returnedScanResults = mWificondControl.getScanResults();
459        assertEquals(mockScanResults.length, returnedScanResults.size());
460        // Since NativeScanResult is organized differently from ScanResult, this only checks
461        // a few fields.
462        for (int i = 0; i < mockScanResults.length; i++) {
463            assertArrayEquals(mockScanResults[i].ssid,
464                              returnedScanResults.get(i).getScanResult().SSID.getBytes());
465            assertEquals(mockScanResults[i].frequency,
466                         returnedScanResults.get(i).getScanResult().frequency);
467            assertEquals(mockScanResults[i].tsf,
468                         returnedScanResults.get(i).getScanResult().timestamp);
469        }
470    }
471
472    /**
473     * Verifies that Scan() can convert input parameters to SingleScanSettings correctly.
474     */
475    @Test
476    public void testScan() throws Exception {
477        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
478
479        when(scanner.scan(any(SingleScanSettings.class))).thenReturn(true);
480
481        assertTrue(mWificondControl.scan(SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET));
482        verify(scanner).scan(argThat(new ScanMatcher(
483                SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET)));
484    }
485
486    /**
487     * Verifies that Scan() can handle null input parameters correctly.
488     */
489    @Test
490    public void testScanNullParameters() throws Exception {
491        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
492
493        when(scanner.scan(any(SingleScanSettings.class))).thenReturn(true);
494
495        assertTrue(mWificondControl.scan(null, null));
496        verify(scanner).scan(argThat(new ScanMatcher(null, null)));
497    }
498
499    /**
500     * Verifies that Scan() can handle wificond scan failure.
501     */
502    @Test
503    public void testScanFailure() throws Exception {
504        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
505
506        when(scanner.scan(any(SingleScanSettings.class))).thenReturn(false);
507        assertFalse(mWificondControl.scan(SCAN_FREQ_SET, SCAN_HIDDEN_NETWORK_SSID_SET));
508        verify(scanner).scan(any(SingleScanSettings.class));
509    }
510
511    /**
512     * Verifies that startPnoScan() can convert input parameters to PnoSettings correctly.
513     */
514    @Test
515    public void testStartPnoScan() throws Exception {
516        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
517
518        when(scanner.startPnoScan(any(PnoSettings.class))).thenReturn(true);
519        assertTrue(mWificondControl.startPnoScan(TEST_PNO_SETTINGS));
520        verify(scanner).startPnoScan(argThat(new PnoScanMatcher(TEST_PNO_SETTINGS)));
521    }
522
523    /**
524     * Verifies that stopPnoScan() calls underlying wificond.
525     */
526    @Test
527    public void testStopPnoScan() throws Exception {
528        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
529
530        when(scanner.stopPnoScan()).thenReturn(true);
531        assertTrue(mWificondControl.stopPnoScan());
532        verify(scanner).stopPnoScan();
533    }
534
535    /**
536     * Verifies that stopPnoScan() can handle wificond failure.
537     */
538    @Test
539    public void testStopPnoScanFailure() throws Exception {
540        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
541
542        when(scanner.stopPnoScan()).thenReturn(false);
543        assertFalse(mWificondControl.stopPnoScan());
544        verify(scanner).stopPnoScan();
545    }
546
547    /**
548     * Verifies that WificondControl can invoke WifiMonitor broadcast methods upon scan
549     * reuslt event.
550     */
551    @Test
552    public void testScanResultEvent() throws Exception {
553        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
554
555        ArgumentCaptor<IScanEvent> messageCaptor = ArgumentCaptor.forClass(IScanEvent.class);
556        verify(scanner).subscribeScanEvents(messageCaptor.capture());
557        IScanEvent scanEvent = messageCaptor.getValue();
558        assertNotNull(scanEvent);
559        scanEvent.OnScanResultReady();
560
561        verify(mWifiMonitor).broadcastScanResultEvent(any(String.class));
562    }
563
564    /**
565     * Verifies that WificondControl can invoke WifiMonitor broadcast methods upon scan
566     * failed event.
567     */
568    @Test
569    public void testScanFailedEvent() throws Exception {
570        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
571
572        ArgumentCaptor<IScanEvent> messageCaptor = ArgumentCaptor.forClass(IScanEvent.class);
573        verify(scanner).subscribeScanEvents(messageCaptor.capture());
574        IScanEvent scanEvent = messageCaptor.getValue();
575        assertNotNull(scanEvent);
576        scanEvent.OnScanFailed();
577
578        verify(mWifiMonitor).broadcastScanFailedEvent(any(String.class));
579    }
580
581    /**
582     * Verifies that WificondControl can invoke WifiMonitor broadcast methods upon pno scan
583     * reuslt event.
584     */
585    @Test
586    public void testPnoScanResultEvent() throws Exception {
587        IWifiScannerImpl scanner = setupClientInterfaceAndCreateMockWificondScanner();
588
589        ArgumentCaptor<IPnoScanEvent> messageCaptor = ArgumentCaptor.forClass(IPnoScanEvent.class);
590        verify(scanner).subscribePnoScanEvents(messageCaptor.capture());
591        IPnoScanEvent pnoScanEvent = messageCaptor.getValue();
592        assertNotNull(pnoScanEvent);
593        pnoScanEvent.OnPnoNetworkFound();
594
595        verify(mWifiMonitor).broadcastScanResultEvent(any(String.class));
596    }
597
598    /**
599     * Helper method: create a mock IClientInterface which mocks all neccessary operations.
600     * Returns a mock IClientInterface.
601     */
602    private IClientInterface getMockClientInterface() throws Exception {
603        IClientInterface clientInterface = mock(IClientInterface.class);
604        IWifiScannerImpl scanner = mock(IWifiScannerImpl.class);
605
606        when(clientInterface.getWifiScannerImpl()).thenReturn(scanner);
607
608        return clientInterface;
609    }
610
611    /**
612     * Helper method: Setup interface to client mode for mWificondControl.
613     * Returns a mock IWifiScannerImpl.
614     */
615    private IWifiScannerImpl setupClientInterfaceAndCreateMockWificondScanner() throws Exception {
616        IWificond wificond = mock(IWificond.class);
617        IClientInterface clientInterface = mock(IClientInterface.class);
618        IWifiScannerImpl scanner = mock(IWifiScannerImpl.class);
619
620        when(mWifiInjector.makeWificond()).thenReturn(wificond);
621        when(wificond.createClientInterface()).thenReturn(clientInterface);
622        when(clientInterface.getWifiScannerImpl()).thenReturn(scanner);
623
624        assertEquals(clientInterface, mWificondControl.setupDriverForClientMode());
625
626        return scanner;
627    }
628
629    // Create a ArgumentMatcher which captures a SingleScanSettings parameter and checks if it
630    // matches the provided frequency set and ssid set.
631    private class ScanMatcher extends ArgumentMatcher<SingleScanSettings> {
632        private final Set<Integer> mExpectedFreqs;
633        private final Set<String> mExpectedSsids;
634        ScanMatcher(Set<Integer> expectedFreqs, Set<String> expectedSsids) {
635            this.mExpectedFreqs = expectedFreqs;
636            this.mExpectedSsids = expectedSsids;
637        }
638
639        @Override
640        public boolean matchesObject(Object argument) {
641            SingleScanSettings settings = (SingleScanSettings) argument;
642            ArrayList<ChannelSettings> channelSettings = settings.channelSettings;
643            ArrayList<HiddenNetwork> hiddenNetworks = settings.hiddenNetworks;
644            if (mExpectedFreqs != null) {
645                Set<Integer> freqSet = new HashSet<Integer>();
646                for (ChannelSettings channel : channelSettings) {
647                    freqSet.add(channel.frequency);
648                }
649                if (!mExpectedFreqs.equals(freqSet)) {
650                    return false;
651                }
652            } else {
653                if (channelSettings != null && channelSettings.size() > 0) {
654                    return false;
655                }
656            }
657
658            if (mExpectedSsids != null) {
659                Set<String> ssidSet = new HashSet<String>();
660                for (HiddenNetwork network : hiddenNetworks) {
661                    ssidSet.add(NativeUtil.encodeSsid(
662                            NativeUtil.byteArrayToArrayList(network.ssid)));
663                }
664                if (!mExpectedSsids.equals(ssidSet)) {
665                    return false;
666                }
667
668            } else {
669                if (hiddenNetworks != null && hiddenNetworks.size() > 0) {
670                    return false;
671                }
672            }
673            return true;
674        }
675    }
676
677    // Create a ArgumentMatcher which captures a PnoSettings parameter and checks if it
678    // matches the WifiNative.PnoSettings;
679    private class PnoScanMatcher extends ArgumentMatcher<PnoSettings> {
680        private final WifiNative.PnoSettings mExpectedPnoSettings;
681        PnoScanMatcher(WifiNative.PnoSettings expectedPnoSettings) {
682            this.mExpectedPnoSettings = expectedPnoSettings;
683        }
684        @Override
685        public boolean matchesObject(Object argument) {
686            PnoSettings settings = (PnoSettings) argument;
687            if (mExpectedPnoSettings == null) {
688                return false;
689            }
690            if (settings.intervalMs != mExpectedPnoSettings.periodInMs
691                    || settings.min2gRssi != mExpectedPnoSettings.min24GHzRssi
692                    || settings.min5gRssi != mExpectedPnoSettings.min5GHzRssi) {
693                return false;
694            }
695            if (settings.pnoNetworks == null || mExpectedPnoSettings.networkList == null) {
696                return false;
697            }
698            if (settings.pnoNetworks.size() != mExpectedPnoSettings.networkList.length) {
699                return false;
700            }
701
702            for (int i = 0; i < settings.pnoNetworks.size(); i++) {
703                if (!mExpectedPnoSettings.networkList[i].ssid.equals(NativeUtil.encodeSsid(
704                         NativeUtil.byteArrayToArrayList(settings.pnoNetworks.get(i).ssid)))) {
705                    return false;
706                }
707                boolean isNetworkHidden = (mExpectedPnoSettings.networkList[i].flags
708                        & WifiScanner.PnoSettings.PnoNetwork.FLAG_DIRECTED_SCAN) != 0;
709                if (isNetworkHidden != settings.pnoNetworks.get(i).isHidden) {
710                    return false;
711                }
712
713            }
714            return true;
715        }
716
717    }
718}
719