summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/HostapdHalTest.java
blob: 2cf8f222fd1258ade120bb454145994c5e98a5b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 * Copyright (C) 2017 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.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

import android.app.test.MockAnswerUtil;
import android.content.Context;
import android.hardware.wifi.hostapd.V1_0.HostapdStatus;
import android.hardware.wifi.hostapd.V1_0.HostapdStatusCode;
import android.hardware.wifi.hostapd.V1_0.IHostapd;
import android.hardware.wifi.hostapd.V1_1.IHostapdCallback;
import android.hidl.manager.V1_0.IServiceManager;
import android.hidl.manager.V1_0.IServiceNotification;
import android.net.wifi.WifiConfiguration;
import android.os.IHwBinder;
import android.os.RemoteException;
import android.os.test.TestLooper;

import androidx.test.filters.SmallTest;

import com.android.internal.R;
import com.android.server.wifi.util.NativeUtil;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;

/**
 * Unit tests for HostapdHal
 */
@SmallTest
public class HostapdHalTest {
    private static final String IFACE_NAME = "mock-wlan0";
    private static final String NETWORK_SSID = "test-ssid";
    private static final String NETWORK_PSK = "test-psk";

    private @Mock Context mContext;
    private @Mock IServiceManager mServiceManagerMock;
    private @Mock IHostapd mIHostapdMock;
    private @Mock WifiNative.HostapdDeathEventHandler mHostapdHalDeathHandler;
    private @Mock WifiNative.SoftApListener mSoftApListener;
    private android.hardware.wifi.hostapd.V1_1.IHostapd mIHostapdMockV1_1;
    private IHostapdCallback mIHostapdCallback;
    private MockResources mResources;
    HostapdStatus mStatusSuccess;
    HostapdStatus mStatusFailure;
    private TestLooper mLooper = new TestLooper();
    private HostapdHal mHostapdHal;
    private ArgumentCaptor<IHwBinder.DeathRecipient> mServiceManagerDeathCaptor =
            ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class);
    private ArgumentCaptor<IHwBinder.DeathRecipient> mHostapdDeathCaptor =
            ArgumentCaptor.forClass(IHwBinder.DeathRecipient.class);
    private ArgumentCaptor<IServiceNotification.Stub> mServiceNotificationCaptor =
            ArgumentCaptor.forClass(IServiceNotification.Stub.class);
    private ArgumentCaptor<IHostapd.IfaceParams> mIfaceParamsCaptor =
            ArgumentCaptor.forClass(IHostapd.IfaceParams.class);
    private ArgumentCaptor<android.hardware.wifi.hostapd.V1_1.IHostapd.IfaceParams>
            mIfaceParamsCaptorV1_1 =
            ArgumentCaptor.forClass(android.hardware.wifi.hostapd.V1_1.IHostapd.IfaceParams.class);
    private ArgumentCaptor<IHostapd.NetworkParams> mNetworkParamsCaptor =
            ArgumentCaptor.forClass(IHostapd.NetworkParams.class);
    private ArgumentCaptor<Long> mDeathRecipientCookieCaptor = ArgumentCaptor.forClass(Long.class);
    private InOrder mInOrder;

    private class HostapdHalSpy extends HostapdHal {
        HostapdHalSpy() {
            super(mContext, mLooper.getLooper());
        }

        @Override
        protected IServiceManager getServiceManagerMockable() throws RemoteException {
            return mServiceManagerMock;
        }

        @Override
        protected IHostapd getHostapdMockable() throws RemoteException {
            return mIHostapdMock;
        }

        @Override
        protected android.hardware.wifi.hostapd.V1_1.IHostapd getHostapdMockableV1_1()
                throws RemoteException {
            return mIHostapdMockV1_1;
        }
    }

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mResources = new MockResources();
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, false);
        mResources.setBoolean(R.bool.config_wifi_softap_ieee80211ac_supported, false);
        mResources.setString(R.string.config_wifi_softap_acs_supported_channel_list, "");

        mStatusSuccess = createHostapdStatus(HostapdStatusCode.SUCCESS);
        mStatusFailure = createHostapdStatus(HostapdStatusCode.FAILURE_UNKNOWN);

        when(mContext.getResources()).thenReturn(mResources);
        when(mServiceManagerMock.linkToDeath(any(IHwBinder.DeathRecipient.class),
                anyLong())).thenReturn(true);
        when(mServiceManagerMock.registerForNotifications(anyString(), anyString(),
                any(IServiceNotification.Stub.class))).thenReturn(true);
        when(mServiceManagerMock.getTransport(anyString(), anyString()))
                .thenReturn(IServiceManager.Transport.EMPTY);
        when(mIHostapdMock.linkToDeath(any(IHwBinder.DeathRecipient.class),
                anyLong())).thenReturn(true);
        when(mIHostapdMock.linkToDeath(any(IHwBinder.DeathRecipient.class),
                anyLong())).thenReturn(true);
        when(mIHostapdMock.addAccessPoint(
                mIfaceParamsCaptor.capture(), mNetworkParamsCaptor.capture()))
                .thenReturn(mStatusSuccess);
        when(mIHostapdMock.removeAccessPoint(any())).thenReturn(mStatusSuccess);
        mHostapdHal = new HostapdHalSpy();
    }

    /**
     * Sunny day scenario for HostapdHal initialization
     * Asserts successful initialization
     */
    @Test
    public void testInitialize_success() throws Exception {
        executeAndValidateInitializationSequence(false, false);
    }

    /**
     * Failure scenario for HostapdHal initialization
     */
    @Test
    public void testInitialize_registerException() throws Exception {
        executeAndValidateInitializationSequence(true, false);
    }

    /**
     * Failure scenario for HostapdHal initialization
     */
    @Test
    public void testInitialize_registerFailure() throws Exception {
        executeAndValidateInitializationSequence(false, true);
    }

    /**
     * Sunny day scenario for V1.1 HostapdHal initialization
     * Asserts successful initialization
     */
    @Test
    public void testInitialize_successV1_1() throws Exception {
        when(mServiceManagerMock.getTransport(anyString(), anyString()))
                .thenReturn(IServiceManager.Transport.HWBINDER);
        mIHostapdMockV1_1 = mock(android.hardware.wifi.hostapd.V1_1.IHostapd.class);
        executeAndValidateInitializationSequenceV1_1(false);
    }

    /**
     * Failure scenario for V1.1 HostapdHal initialization
     */
    @Test
    public void testInitialize_registerCallbackFailureV1_1() throws Exception {
        when(mServiceManagerMock.getTransport(anyString(), anyString()))
                .thenReturn(IServiceManager.Transport.HWBINDER);
        mIHostapdMockV1_1 = mock(android.hardware.wifi.hostapd.V1_1.IHostapd.class);
        executeAndValidateInitializationSequenceV1_1(true);
    }

    /**
     * Verifies the hostapd death handling.
     */
    @Test
    public void testDeathHandling() throws Exception {
        executeAndValidateInitializationSequence();

        mHostapdHal.registerDeathHandler(mHostapdHalDeathHandler);
        mHostapdDeathCaptor.getValue().serviceDied(mDeathRecipientCookieCaptor.getValue());
        mLooper.dispatchAll();
        verify(mHostapdHalDeathHandler).onDeath();
    }

    /**
     * Verifies the hostapd death handling.
     */
    @Test
    public void testStaleDeathHandling() throws Exception {
        executeAndValidateInitializationSequence();

        mHostapdHal.registerDeathHandler(mHostapdHalDeathHandler);
        mHostapdDeathCaptor.getValue().serviceDied(mDeathRecipientCookieCaptor.getValue() - 1);
        mLooper.dispatchAll();
        verify(mHostapdHalDeathHandler, never()).onDeath();
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_Band2G() throws Exception {
        executeAndValidateInitializationSequence();
        final int apChannel = 6;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_2GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);
        assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Open_Band5G() throws Exception {
        executeAndValidateInitializationSequence();
        final int apChannel = 18;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = true;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_5GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_5_GHZ, mIfaceParamsCaptor.getValue().channelParams.band);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);
        assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertTrue(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.NONE, mNetworkParamsCaptor.getValue().encryptionType);
        assertNotNull(mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_Band5G_Hidden() throws Exception {
        executeAndValidateInitializationSequence();
        final int apChannel = 18;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = true;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_5GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_5_GHZ, mIfaceParamsCaptor.getValue().channelParams.band);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);
        assertEquals(apChannel, mIfaceParamsCaptor.getValue().channelParams.channel);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertTrue(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_Band2G_WithACS() throws Exception {
        // Enable ACS in the config.
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true);
        mHostapdHal = new HostapdHalSpy();

        executeAndValidateInitializationSequence();
        final int apChannel = 6;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_2GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_Band2G_WithIeee80211AC() throws Exception {
        // Enable ACS & 80211AC in the config.
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true);
        mResources.setBoolean(R.bool.config_wifi_softap_ieee80211ac_supported, true);
        mHostapdHal = new HostapdHalSpy();

        executeAndValidateInitializationSequence();
        final int apChannel = 6;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_2GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_2_4_GHZ, mIfaceParamsCaptor.getValue().channelParams.band);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_BandAny_WithACS() throws Exception {
        // Enable ACS in the config.
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true);
        mHostapdHal = new HostapdHalSpy();

        executeAndValidateInitializationSequence();
        final int apChannel = 6;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_ANY;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_ANY, mIfaceParamsCaptor.getValue().channelParams.band);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.enableAcs);
        assertTrue(mIfaceParamsCaptor.getValue().channelParams.acsShouldExcludeDfs);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     * Verifies that BAND_ANY is downgraded to 2.4GHz if ACS is disabled.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_BandAny_Downgraded_WithoutACS() throws Exception {
        // Disable ACS in the config.
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, false);
        mHostapdHal = new HostapdHalSpy();

        executeAndValidateInitializationSequence();
        final int apChannel = 6;

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apChannel = apChannel;
        configuration.apBand = WifiConfiguration.AP_BAND_ANY;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptor.getValue().ifaceName);
        assertTrue(mIfaceParamsCaptor.getValue().hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptor.getValue().hwModeParams.enable80211AC);
        // Verify the band is downgraded to 2.4GHz.
        assertEquals(IHostapd.Band.BAND_2_4_GHZ,
                mIfaceParamsCaptor.getValue().channelParams.band);
        assertFalse(mIfaceParamsCaptor.getValue().channelParams.enableAcs);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the successful addition of access point.
     * Verifies that channel info for ACS is handled.
     */
    @Test
    public void testAddAccessPointSuccess_Psk_BandAny_WithACS_AcsChannels() throws Exception {
        when(mServiceManagerMock.getTransport(anyString(), anyString()))
                .thenReturn(IServiceManager.Transport.HWBINDER);
        mIHostapdMockV1_1 = mock(android.hardware.wifi.hostapd.V1_1.IHostapd.class);
        // Enable ACS and set available channels in the config.
        final String acsChannelStr = "1,6,11-13,40";
        android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange channelRange1 =
                new android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange();
        android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange channelRange2 =
                new android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange();
        android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange channelRange3 =
                new android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange();
        android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange channelRange4 =
                new android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange();
        channelRange1.start = channelRange1.end = 1;
        channelRange2.start = channelRange2.end = 6;
        channelRange3.start = 11;
        channelRange3.end = 13;
        channelRange4.start = channelRange4.end = 40;
        ArrayList<android.hardware.wifi.hostapd.V1_1.IHostapd.AcsChannelRange> acsChannelRanges =
                new ArrayList<>();
        acsChannelRanges.add(channelRange1);
        acsChannelRanges.add(channelRange2);
        acsChannelRanges.add(channelRange3);
        acsChannelRanges.add(channelRange4);
        mResources.setBoolean(R.bool.config_wifi_softap_acs_supported, true);
        mResources.setString(R.string.config_wifi_softap_acs_supported_channel_list, acsChannelStr);
        mHostapdHal = new HostapdHalSpy();

        when(mIHostapdMockV1_1.addAccessPoint_1_1(
                mIfaceParamsCaptorV1_1.capture(), mNetworkParamsCaptor.capture()))
                .thenReturn(mStatusSuccess);

        executeAndValidateInitializationSequenceV1_1(false);

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = false;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
        configuration.preSharedKey = NETWORK_PSK;
        configuration.apBand = WifiConfiguration.AP_BAND_ANY;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMockV1_1).addAccessPoint_1_1(any(), any());

        assertEquals(IFACE_NAME, mIfaceParamsCaptorV1_1.getValue().V1_0.ifaceName);
        assertTrue(mIfaceParamsCaptorV1_1.getValue().V1_0.hwModeParams.enable80211N);
        assertFalse(mIfaceParamsCaptorV1_1.getValue().V1_0.hwModeParams.enable80211AC);
        assertEquals(IHostapd.Band.BAND_ANY,
                mIfaceParamsCaptorV1_1.getValue().V1_0.channelParams.band);
        assertTrue(mIfaceParamsCaptorV1_1.getValue().V1_0.channelParams.enableAcs);
        assertTrue(mIfaceParamsCaptorV1_1.getValue().V1_0.channelParams.acsShouldExcludeDfs);
        assertEquals(acsChannelRanges,
                mIfaceParamsCaptorV1_1.getValue().channelParams.acsChannelRanges);

        assertEquals(NativeUtil.stringToByteArrayList(NETWORK_SSID),
                mNetworkParamsCaptor.getValue().ssid);
        assertFalse(mNetworkParamsCaptor.getValue().isHidden);
        assertEquals(IHostapd.EncryptionType.WPA2, mNetworkParamsCaptor.getValue().encryptionType);
        assertEquals(NETWORK_PSK, mNetworkParamsCaptor.getValue().pskPassphrase);
    }

    /**
     * Verifies the failure handling in addition of access point with an invalid band.
     */
    @Test
    public void testAddAccessPointInvalidBandFailure() throws Exception {
        executeAndValidateInitializationSequence();

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = true;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        configuration.apBand = WifiConfiguration.AP_BAND_5GHZ + 1;

        assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock, never()).addAccessPoint(any(), any());
    }

    /**
     * Verifies the failure handling in addition of access point.
     */
    @Test
    public void testAddAccessPointFailure() throws Exception {
        executeAndValidateInitializationSequence();
        when(mIHostapdMock.addAccessPoint(any(), any())).thenReturn(mStatusFailure);

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = true;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        configuration.apChannel = 6;
        configuration.apBand = WifiConfiguration.AP_BAND_2GHZ;

        assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());
    }

    /**
     * Verifies the failure handling in addition of access point.
     */
    @Test
    public void testAddAccessPointRemoteException() throws Exception {
        executeAndValidateInitializationSequence();
        doThrow(new RemoteException()).when(mIHostapdMock).addAccessPoint(any(), any());

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.hiddenSSID = true;
        configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        configuration.apChannel = 6;

        assertFalse(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMock).addAccessPoint(any(), any());
    }

    /**
     * Verifies the successful removal of access point.
     */
    @Test
    public void testRemoveAccessPointSuccess() throws Exception {
        executeAndValidateInitializationSequence();

        assertTrue(mHostapdHal.removeAccessPoint(IFACE_NAME));
        verify(mIHostapdMock).removeAccessPoint(any());
    }

    /**
     * Verifies the failure handling in removal of access point.
     */
    @Test
    public void testRemoveAccessPointFailure() throws Exception {
        executeAndValidateInitializationSequence();
        when(mIHostapdMock.removeAccessPoint(any())).thenReturn(mStatusFailure);

        assertFalse(mHostapdHal.removeAccessPoint(IFACE_NAME));
        verify(mIHostapdMock).removeAccessPoint(any());
    }

    /**
     * Verifies the failure handling in addition of access point.
     */
    @Test
    public void testRemoveAccessPointRemoteException() throws Exception {
        executeAndValidateInitializationSequence();
        doThrow(new RemoteException()).when(mIHostapdMock).removeAccessPoint(any());

        assertFalse(mHostapdHal.removeAccessPoint(IFACE_NAME));
        verify(mIHostapdMock).removeAccessPoint(any());
    }

    /**
     * Verifies the handling of onFailure callback from hostapd.
     */
    @Test
    public void testOnFailureCallbackHandling() throws Exception {
        when(mServiceManagerMock.getTransport(anyString(), anyString()))
                .thenReturn(IServiceManager.Transport.HWBINDER);
        mIHostapdMockV1_1 = mock(android.hardware.wifi.hostapd.V1_1.IHostapd.class);
        when(mIHostapdMockV1_1.addAccessPoint_1_1(
                mIfaceParamsCaptorV1_1.capture(), mNetworkParamsCaptor.capture()))
                .thenReturn(mStatusSuccess);
        executeAndValidateInitializationSequenceV1_1(false);

        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = NETWORK_SSID;
        configuration.apBand = WifiConfiguration.AP_BAND_2GHZ;

        assertTrue(mHostapdHal.addAccessPoint(IFACE_NAME, configuration, mSoftApListener));
        verify(mIHostapdMockV1_1).addAccessPoint_1_1(any(), any());

        // Trigger on failure.
        mIHostapdCallback.onFailure(IFACE_NAME);
        verify(mSoftApListener).onFailure();

        // Now remove the access point and ensure that the callback is no longer handled.
        reset(mSoftApListener);
        assertTrue(mHostapdHal.removeAccessPoint(IFACE_NAME));
        mIHostapdCallback.onFailure(IFACE_NAME);
        verify(mSoftApListener, never()).onFailure();
    }

    private void executeAndValidateInitializationSequence() throws Exception {
        executeAndValidateInitializationSequence(false, false);
    }

    /**
     * Calls.initialize(), mocking various callback answers and verifying flow, asserting for the
     * expected result. Verifies if IHostapd manager is initialized or reset.
     */
    private void executeAndValidateInitializationSequence(
            boolean causeRegisterRemoteException, boolean causeRegisterFailure) throws Exception {
        boolean shouldSucceed = !causeRegisterRemoteException && !causeRegisterFailure;
        mInOrder = inOrder(mServiceManagerMock, mIHostapdMock);
        if (causeRegisterFailure) {
            when(mServiceManagerMock.registerForNotifications(anyString(), anyString(),
                    any(IServiceNotification.Stub.class))).thenReturn(false);
        } else if (causeRegisterRemoteException) {
            doThrow(new RemoteException()).when(mServiceManagerMock)
                    .registerForNotifications(
                            anyString(), anyString(), any(IServiceNotification.Stub.class));
        }
        // Initialize HostapdHal, should call serviceManager.registerForNotifications
        assertEquals(shouldSucceed, mHostapdHal.initialize());
        // verify: service manager initialization sequence
        mInOrder.verify(mServiceManagerMock).linkToDeath(mServiceManagerDeathCaptor.capture(),
                anyLong());
        mInOrder.verify(mServiceManagerMock).registerForNotifications(
                eq(IHostapd.kInterfaceName), eq(""), mServiceNotificationCaptor.capture());
        if (shouldSucceed) {
            // act: cause the onRegistration(...) callback to execute
            mServiceNotificationCaptor.getValue().onRegistration(IHostapd.kInterfaceName, "", true);
            assertTrue(mHostapdHal.isInitializationComplete());
            mInOrder.verify(mIHostapdMock).linkToDeath(mHostapdDeathCaptor.capture(),
                    mDeathRecipientCookieCaptor.capture());
        } else {
            assertFalse(mHostapdHal.isInitializationComplete());
            mInOrder.verify(mIHostapdMock, never()).linkToDeath(
                    mHostapdDeathCaptor.capture(), anyLong());
        }
    }

    /**
     * Calls.initialize(), mocking various callback answers and verifying flow, asserting for the
     * expected result. Verifies if IHostapd manager is initialized or reset.
     */
    private void executeAndValidateInitializationSequenceV1_1(
            boolean causeCallbackFailure) throws Exception {
        boolean shouldSucceed = !causeCallbackFailure;
        mInOrder = inOrder(mServiceManagerMock, mIHostapdMock);
        if (causeCallbackFailure) {
            doAnswer(new MockAnswerUtil.AnswerWithArguments() {
                public HostapdStatus answer(IHostapdCallback cb)
                        throws RemoteException {
                    return mStatusFailure;
                }
            }).when(mIHostapdMockV1_1).registerCallback(any(IHostapdCallback.class));
        } else {
            doAnswer(new MockAnswerUtil.AnswerWithArguments() {
                public HostapdStatus answer(IHostapdCallback cb)
                        throws RemoteException {
                    mIHostapdCallback = cb;
                    return mStatusSuccess;
                }
            }).when(mIHostapdMockV1_1).registerCallback(any(IHostapdCallback.class));
        }
        // Initialize HostapdHal, should call serviceManager.registerForNotifications
        assertTrue(mHostapdHal.initialize());
        // verify: service manager initialization sequence
        mInOrder.verify(mServiceManagerMock).linkToDeath(mServiceManagerDeathCaptor.capture(),
                anyLong());
        mInOrder.verify(mServiceManagerMock).registerForNotifications(
                eq(IHostapd.kInterfaceName), eq(""), mServiceNotificationCaptor.capture());
        // act: cause the onRegistration(...) callback to execute
        mServiceNotificationCaptor.getValue().onRegistration(IHostapd.kInterfaceName, "", true);
        assertEquals(shouldSucceed, mHostapdHal.isInitializationComplete());
        mInOrder.verify(mIHostapdMock).linkToDeath(mHostapdDeathCaptor.capture(), anyLong());
        verify(mIHostapdMockV1_1).registerCallback(any(IHostapdCallback.class));
    }

    private HostapdStatus createHostapdStatus(int code) {
        HostapdStatus status = new HostapdStatus();
        status.code = code;
        return status;
    }
}