aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hwsim/test_ap_ht.py
blob: 81c39409ba8a651bdb0b095f83f1c427d2ad961a (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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# Test cases for HT operations with hostapd
# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import time
import logging
logger = logging.getLogger()
import struct
import subprocess

import hostapd
from utils import HwsimSkip, alloc_fail
import hwsim_utils
from test_ap_csa import csa_supported

def clear_scan_cache(ifname):
    subprocess.call(['ifconfig', ifname, 'up'])
    subprocess.call(['iw', ifname, 'scan', 'trigger', 'freq', '2412', 'flush'])
    time.sleep(0.1)
    subprocess.call(['ifconfig', ifname, 'down'])

def test_ap_ht40_scan(dev, apdev):
    """HT40 co-ex scan"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2432":
        raise Exception("Unexpected frequency")
    pri = hapd.get_status_field("channel")
    if pri != "5":
        raise Exception("Unexpected primary channel")
    sec = hapd.get_status_field("secondary_channel")
    if sec != "-1":
        raise Exception("Unexpected secondary channel")

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_conflict(dev, apdev):
    """HT40 co-ex scan conflict"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "test-ht40",
               "channel": "6",
               "ht_capab": "[HT40+]"}
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2432":
        raise Exception("Unexpected frequency")
    pri = hapd.get_status_field("channel")
    if pri != "5":
        raise Exception("Unexpected primary channel")
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_conflict2(dev, apdev):
    """HT40 co-ex scan conflict (HT40-)"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "test-ht40",
               "channel": "11",
               "ht_capab": "[HT40-]"}
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "1",
               "ht_capab": "[HT40+]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2412":
        raise Exception("Unexpected frequency")
    pri = hapd.get_status_field("channel")
    if pri != "1":
        raise Exception("Unexpected primary channel")
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_not_affected(dev, apdev):
    """HT40 co-ex scan and other BSS not affected"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "test-ht20",
               "channel": "11" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    subprocess.call(['ifconfig', apdev[0]['ifname'], 'up'])
    subprocess.call(['iw', apdev[0]['ifname'], 'scan', 'trigger', 'freq', '2462'])
    time.sleep(0.5)
    subprocess.call(['iw', apdev[0]['ifname'], 'scan', 'dump'],
                    stdout=open('/dev/null', 'w'))
    time.sleep(0.1)
    subprocess.call(['ifconfig', apdev[0]['ifname'], 'down'])

    params = { "ssid": "test-ht40",
               "channel": "1",
               "ht_capab": "[HT40+]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2412":
        raise Exception("Unexpected frequency")
    pri = hapd.get_status_field("channel")
    if pri != "1":
        raise Exception("Unexpected primary channel")
    sec = hapd.get_status_field("secondary_channel")
    if sec != "1":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_legacy_conflict(dev, apdev):
    """HT40 co-ex scan conflict with legacy 20 MHz AP"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "legacy-20",
               "channel": "7", "ieee80211n": "0" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2432":
        raise Exception("Unexpected frequency: " + freq)
    pri = hapd.get_status_field("channel")
    if pri != "5":
        raise Exception("Unexpected primary channel: " + pri)
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_ht20_conflict(dev, apdev):
    """HT40 co-ex scan conflict with HT 20 MHz AP"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "ht-20",
               "channel": "7", "ieee80211n": "1" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2432":
        raise Exception("Unexpected frequency: " + freq)
    pri = hapd.get_status_field("channel")
    if pri != "5":
        raise Exception("Unexpected primary channel: " + pri)
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_intolerant(dev, apdev):
    """HT40 co-ex scan finding an AP advertising 40 MHz intolerant"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "another-bss",
               "channel": "1",
               "ht_capab": "[40-INTOLERANT]" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "1",
               "ht_capab": "[HT40+]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2412":
        raise Exception("Unexpected frequency: " + freq)
    pri = hapd.get_status_field("channel")
    if pri != "1":
        raise Exception("Unexpected primary channel: " + pri)
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_scan_match(dev, apdev):
    """HT40 co-ex scan matching configuration"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hostapd.add_ap(apdev[1]['ifname'], params)

    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    state = hapd.get_status_field("state")
    if state != "HT_SCAN":
        time.sleep(0.1)
        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            raise Exception("Unexpected interface state - expected HT_SCAN")

    ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
    if not ev:
        raise Exception("AP setup timed out")

    state = hapd.get_status_field("state")
    if state != "ENABLED":
        raise Exception("Unexpected interface state - expected ENABLED")

    freq = hapd.get_status_field("freq")
    if freq != "2432":
        raise Exception("Unexpected frequency")
    pri = hapd.get_status_field("channel")
    if pri != "5":
        raise Exception("Unexpected primary channel")
    sec = hapd.get_status_field("secondary_channel")
    if sec != "-1":
        raise Exception("Unexpected secondary channel: " + sec)

    dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)

def test_ap_ht40_5ghz_match(dev, apdev):
    """HT40 co-ex scan on 5 GHz with matching pri/sec channel"""
    clear_scan_cache(apdev[0]['ifname'])
    try:
        hapd = None
        hapd2 = None
        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "36",
                   "country_code": "US",
                   "ht_capab": "[HT40+]"}
        hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)

        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "36",
                   "ht_capab": "[HT40+]"}
        hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            time.sleep(0.1)
            state = hapd.get_status_field("state")
            if state != "HT_SCAN":
                raise Exception("Unexpected interface state - expected HT_SCAN")

        ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
        if not ev:
            raise Exception("AP setup timed out")

        state = hapd.get_status_field("state")
        if state != "ENABLED":
            raise Exception("Unexpected interface state - expected ENABLED")

        freq = hapd.get_status_field("freq")
        if freq != "5180":
            raise Exception("Unexpected frequency")
        pri = hapd.get_status_field("channel")
        if pri != "36":
            raise Exception("Unexpected primary channel")
        sec = hapd.get_status_field("secondary_channel")
        if sec != "1":
            raise Exception("Unexpected secondary channel: " + sec)

        dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        if hapd2:
            hapd2.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()

def test_ap_ht40_5ghz_switch(dev, apdev):
    """HT40 co-ex scan on 5 GHz switching pri/sec channel"""
    clear_scan_cache(apdev[0]['ifname'])
    try:
        hapd = None
        hapd2 = None
        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "36",
                   "country_code": "US",
                   "ht_capab": "[HT40+]"}
        hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)

        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "40",
                   "ht_capab": "[HT40-]"}
        hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            time.sleep(0.1)
            state = hapd.get_status_field("state")
            if state != "HT_SCAN":
                raise Exception("Unexpected interface state - expected HT_SCAN")

        ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
        if not ev:
            raise Exception("AP setup timed out")

        state = hapd.get_status_field("state")
        if state != "ENABLED":
            raise Exception("Unexpected interface state - expected ENABLED")

        freq = hapd.get_status_field("freq")
        if freq != "5180":
            raise Exception("Unexpected frequency: " + freq)
        pri = hapd.get_status_field("channel")
        if pri != "36":
            raise Exception("Unexpected primary channel: " + pri)
        sec = hapd.get_status_field("secondary_channel")
        if sec != "1":
            raise Exception("Unexpected secondary channel: " + sec)

        dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        if hapd2:
            hapd2.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])

def test_ap_ht40_5ghz_switch2(dev, apdev):
    """HT40 co-ex scan on 5 GHz switching pri/sec channel (2)"""
    clear_scan_cache(apdev[0]['ifname'])
    try:
        hapd = None
        hapd2 = None
        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "36",
                   "country_code": "US",
                   "ht_capab": "[HT40+]"}
        hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)

        id = dev[0].add_network()
        dev[0].set_network(id, "mode", "2")
        dev[0].set_network_quoted(id, "ssid", "wpas-ap-open")
        dev[0].set_network(id, "key_mgmt", "NONE")
        dev[0].set_network(id, "frequency", "5200")
        dev[0].set_network(id, "scan_freq", "5200")
        dev[0].select_network(id)
        time.sleep(1)

        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "40",
                   "ht_capab": "[HT40-]"}
        hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

        state = hapd.get_status_field("state")
        if state != "HT_SCAN":
            time.sleep(0.1)
            state = hapd.get_status_field("state")
            if state != "HT_SCAN":
                raise Exception("Unexpected interface state - expected HT_SCAN")

        ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
        if not ev:
            raise Exception("AP setup timed out")

        state = hapd.get_status_field("state")
        if state != "ENABLED":
            raise Exception("Unexpected interface state - expected ENABLED")

        freq = hapd.get_status_field("freq")
        if freq != "5180":
            raise Exception("Unexpected frequency: " + freq)
        pri = hapd.get_status_field("channel")
        if pri != "36":
            raise Exception("Unexpected primary channel: " + pri)
        sec = hapd.get_status_field("secondary_channel")
        if sec != "1":
            raise Exception("Unexpected secondary channel: " + sec)

        dev[0].connect("test-ht40", key_mgmt="NONE", scan_freq=freq)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        if hapd2:
            hapd2.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()

def test_obss_scan(dev, apdev):
    """Overlapping BSS scan request"""
    params = { "ssid": "obss-scan",
               "channel": "6",
               "ht_capab": "[HT40-]",
               "obss_interval": "10" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)

    params = { "ssid": "another-bss",
               "channel": "9",
               "ieee80211n": "0" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    dev[0].connect("obss-scan", key_mgmt="NONE", scan_freq="2437")
    hapd.set("ext_mgmt_frame_handling", "1")
    logger.info("Waiting for OBSS scan to occur")
    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
    if ev is None:
        raise Exception("Timed out while waiting for OBSS scan to start")
    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
    if ev is None:
        raise Exception("Timed out while waiting for OBSS scan results")
    received = False
    for i in range(0, 4):
        frame = hapd.mgmt_rx(timeout=5)
        if frame is None:
            raise Exception("MGMT RX wait timed out")
        if frame['subtype'] != 13:
            continue
        payload = frame['payload']
        if len(payload) < 3:
            continue
        (category, action, ie) = struct.unpack('BBB', payload[0:3])
        if category != 4:
            continue
        if action != 0:
            continue
        if ie == 72:
            logger.info("20/40 BSS Coexistence report received")
            received = True
            break
    if not received:
        raise Exception("20/40 BSS Coexistence report not seen")

def test_obss_scan_40_intolerant(dev, apdev):
    """Overlapping BSS scan request with 40 MHz intolerant AP"""
    params = { "ssid": "obss-scan",
               "channel": "6",
               "ht_capab": "[HT40-]",
               "obss_interval": "10" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)

    params = { "ssid": "another-bss",
               "channel": "7",
               "ht_capab": "[40-INTOLERANT]" }
    hostapd.add_ap(apdev[1]['ifname'], params)

    dev[0].connect("obss-scan", key_mgmt="NONE", scan_freq="2437")
    hapd.set("ext_mgmt_frame_handling", "1")
    logger.info("Waiting for OBSS scan to occur")
    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
    if ev is None:
        raise Exception("Timed out while waiting for OBSS scan to start")
    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
    if ev is None:
        raise Exception("Timed out while waiting for OBSS scan results")
    received = False
    for i in range(0, 4):
        frame = hapd.mgmt_rx(timeout=5)
        if frame is None:
            raise Exception("MGMT RX wait timed out")
        if frame['subtype'] != 13:
            continue
        payload = frame['payload']
        if len(payload) < 3:
            continue
        (category, action, ie) = struct.unpack('BBB', payload[0:3])
        if category != 4:
            continue
        if action != 0:
            continue
        if ie == 72:
            logger.info("20/40 BSS Coexistence report received")
            received = True
            break
    if not received:
        raise Exception("20/40 BSS Coexistence report not seen")

def test_obss_coex_report_handling(dev, apdev):
    """Overlapping BSS scan report handling with obss_interval=0"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "obss-scan",
               "channel": "6",
               "ht_capab": "[HT40-]" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    bssid = apdev[0]['bssid']
    dev[0].connect("obss-scan", key_mgmt="NONE", scan_freq="2437")

    sec = hapd.get_status_field("secondary_channel")
    if sec != "-1":
        raise Exception("AP is not using 40 MHz channel")

    # 20/40 MHz co-ex report tests: number of invalid reports and a valid report
    # that forces 20 MHz channel.
    tests = [ '0400', '040048', '04004801', '0400480000', '0400490100',
              '040048ff0000', '04004801ff49ff00', '04004801004900',
              '0400480100490101', '0400480100490201ff',
              '040048010449020005' ]
    for msg in tests:
        req = "MGMT_TX {} {} freq=2437 action={}".format(bssid, bssid, msg)
        if "OK" not in dev[0].request(req):
            raise Exception("Could not send management frame")
    time.sleep(0.5)
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("AP did not move to 20 MHz channel")

def test_obss_coex_report_handling1(dev, apdev):
    """Overlapping BSS scan report handling with obss_interval=1"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "obss-scan",
               "channel": "6",
               "ht_capab": "[HT40+]",
               "obss_interval": "1" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    bssid = apdev[0]['bssid']
    dev[0].connect("obss-scan", key_mgmt="NONE", scan_freq="2437")

    sec = hapd.get_status_field("secondary_channel")
    if sec != "1":
        raise Exception("AP is not using 40 MHz channel")

    # 20/40 MHz co-ex report forcing 20 MHz channel
    msg = '040048010449020005'
    req = "MGMT_TX {} {} freq=2437 action={}".format(bssid, bssid, msg)
    if "OK" not in dev[0].request(req):
        raise Exception("Could not send management frame")
    time.sleep(0.5)
    sec = hapd.get_status_field("secondary_channel")
    if sec != "0":
        raise Exception("AP did not move to 20 MHz channel")

    # No 20/40 MHz co-ex reports forcing 20 MHz channel during next interval
    for i in range(20):
        sec = hapd.get_status_field("secondary_channel")
        if sec == "1":
            break
        time.sleep(0.5)
    if sec != "1":
        raise Exception("AP did not return to 40 MHz channel")

def test_olbc(dev, apdev):
    """OLBC detection"""
    params = { "ssid": "test-olbc",
               "channel": "6",
               "ht_capab": "[HT40-]",
               "ap_table_expiration_time": "2" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    status = hapd.get_status()
    if status['olbc'] != '0' or status['olbc_ht'] != '0':
        raise Exception("Unexpected OLBC information")

    params = { "ssid": "olbc-ap",
               "hw_mode": "b",
               "channel": "6",
               "wmm_enabled": "0" }
    hostapd.add_ap(apdev[1]['ifname'], params)
    time.sleep(0.5)
    status = hapd.get_status()
    if status['olbc'] != '1' or status['olbc_ht'] != '1':
        raise Exception("Missing OLBC information")

    hapd_global = hostapd.HostapdGlobal()
    hapd_global.remove(apdev[1]['ifname'])

    logger.info("Waiting for OLBC state to time out")
    cleared = False
    for i in range(0, 15):
        time.sleep(1)
        status = hapd.get_status()
        if status['olbc'] == '0' and status['olbc_ht'] == '0':
            cleared = True
            break
    if not cleared:
        raise Exception("OLBC state did nto time out")

def test_olbc_table_limit(dev, apdev):
    """OLBC AP table size limit"""
    ifname1 = apdev[0]['ifname']
    ifname2 = apdev[0]['ifname'] + '-2'
    ifname3 = apdev[0]['ifname'] + '-3'
    hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
    hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
    hostapd.add_bss('phy3', ifname3, 'bss-3.conf')

    params = { "ssid": "test-olbc",
               "channel": "1",
               "ap_table_max_size": "2" }
    hapd = hostapd.add_ap(apdev[1]['ifname'], params)

    time.sleep(0.3)
    with alloc_fail(hapd, 1, "ap_list_process_beacon"):
        time.sleep(0.3)
    hapd.set("ap_table_max_size", "1")
    time.sleep(0.3)
    hapd.set("ap_table_max_size", "0")
    time.sleep(0.3)

def test_olbc_5ghz(dev, apdev):
    """OLBC detection on 5 GHz"""
    try:
        hapd = None
        hapd2 = None
        params = { "ssid": "test-olbc",
                   "country_code": "FI",
                   "hw_mode": "a",
                   "channel": "36",
                   "ht_capab": "[HT40+]" }
        hapd = hostapd.add_ap(apdev[0]['ifname'], params)
        status = hapd.get_status()
        if status['olbc'] != '0' or status['olbc_ht'] != '0':
            raise Exception("Unexpected OLBC information")

        params = { "ssid": "olbc-ap",
                   "country_code": "FI",
                   "hw_mode": "a",
                   "channel": "36",
                   "ieee80211n": "0",
                   "wmm_enabled": "0" }
        hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
        found = False
        for i in range(20):
            time.sleep(0.1)
            status = hapd.get_status()
            logger.debug('olbc_ht: ' + status['olbc_ht'])
            if status['olbc_ht'] == '1':
                found = True
                break
        if not found:
            raise Exception("Missing OLBC information")
    finally:
        if hapd:
            hapd.request("DISABLE")
        if hapd2:
            hapd2.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])

def test_ap_require_ht(dev, apdev):
    """Require HT"""
    params = { "ssid": "require-ht",
               "require_ht": "1" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    dev[1].connect("require-ht", key_mgmt="NONE", scan_freq="2412",
                   disable_ht="1", wait_connect=False)
    dev[0].connect("require-ht", key_mgmt="NONE", scan_freq="2412")
    ev = dev[1].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
    dev[1].request("DISCONNECT")
    if ev is None:
        raise Exception("Association rejection timed out")
    if "status_code=27" not in ev:
        raise Exception("Unexpected rejection status code")
    dev[2].connect("require-ht", key_mgmt="NONE", scan_freq="2412",
                   ht_mcs="0x01 00 00 00 00 00 00 00 00 00",
                   disable_max_amsdu="1", ampdu_factor="2",
                   ampdu_density="1", disable_ht40="1", disable_sgi="1",
                   disable_ldpc="1")

def test_ap_require_ht_limited_rates(dev, apdev):
    """Require HT with limited supported rates"""
    params = { "ssid": "require-ht",
               "supported_rates": "60 120 240 360 480 540",
               "require_ht": "1" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)

    dev[1].connect("require-ht", key_mgmt="NONE", scan_freq="2412",
                   disable_ht="1", wait_connect=False)
    dev[0].connect("require-ht", key_mgmt="NONE", scan_freq="2412")
    ev = dev[1].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
    dev[1].request("DISCONNECT")
    if ev is None:
        raise Exception("Association rejection timed out")
    if "status_code=27" not in ev:
        raise Exception("Unexpected rejection status code")

def test_ap_ht_capab_not_supported(dev, apdev):
    """HT configuration with driver not supporting all ht_capab entries"""
    params = { "ssid": "test-ht40",
               "channel": "5",
               "ht_capab": "[HT40-][LDPC][SMPS-STATIC][SMPS-DYNAMIC][GF][SHORT-GI-20][SHORT-GI-40][TX-STBC][RX-STBC1][RX-STBC12][RX-STBC123][DELAYED-BA][MAX-AMSDU-7935][DSSS_CCK-40][LSIG-TXOP-PROT]"}
    hapd = hostapd.add_ap(apdev[0]['ifname'], params, no_enable=True)
    if "FAIL" not in hapd.request("ENABLE"):
        raise Exception("Unexpected ENABLE success")

def test_ap_ht_40mhz_intolerant_sta(dev, apdev):
    """Associated STA indicating 40 MHz intolerant"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "intolerant",
               "channel": "6",
               "ht_capab": "[HT40-]" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    if hapd.get_status_field("num_sta_ht40_intolerant") != "0":
        raise Exception("Unexpected num_sta_ht40_intolerant value")
    if hapd.get_status_field("secondary_channel") != "-1":
        raise Exception("Unexpected secondary_channel")

    dev[0].connect("intolerant", key_mgmt="NONE", scan_freq="2437")
    if hapd.get_status_field("num_sta_ht40_intolerant") != "0":
        raise Exception("Unexpected num_sta_ht40_intolerant value")
    if hapd.get_status_field("secondary_channel") != "-1":
        raise Exception("Unexpected secondary_channel")

    dev[2].connect("intolerant", key_mgmt="NONE", scan_freq="2437",
                   ht40_intolerant="1")
    time.sleep(1)
    if hapd.get_status_field("num_sta_ht40_intolerant") != "1":
        raise Exception("Unexpected num_sta_ht40_intolerant value (expected 1)")
    if hapd.get_status_field("secondary_channel") != "0":
        raise Exception("Unexpected secondary_channel (did not disable 40 MHz)")

    dev[2].request("DISCONNECT")
    time.sleep(1)
    if hapd.get_status_field("num_sta_ht40_intolerant") != "0":
        raise Exception("Unexpected num_sta_ht40_intolerant value (expected 0)")
    if hapd.get_status_field("secondary_channel") != "-1":
        raise Exception("Unexpected secondary_channel (did not re-enable 40 MHz)")

def test_ap_ht_40mhz_intolerant_ap(dev, apdev):
    """Associated STA reports 40 MHz intolerant AP after association"""
    clear_scan_cache(apdev[0]['ifname'])
    params = { "ssid": "ht",
               "channel": "6",
               "ht_capab": "[HT40-]",
               "obss_interval": "3" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)

    dev[0].connect("ht", key_mgmt="NONE", scan_freq="2437")

    if hapd.get_status_field("secondary_channel") != "-1":
        raise Exception("Unexpected secondary channel information")

    logger.info("Start 40 MHz intolerant AP")
    params = { "ssid": "intolerant",
               "channel": "5",
               "ht_capab": "[40-INTOLERANT]" }
    hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)

    logger.info("Waiting for co-ex report from STA")
    ok = False
    for i in range(0, 20):
        time.sleep(1)
        if hapd.get_status_field("secondary_channel") == "0":
            logger.info("AP moved to 20 MHz channel")
            ok = True
            break
    if not ok:
        raise Exception("AP did not move to 20 MHz channel")

    if "OK" not in hapd2.request("DISABLE"):
        raise Exception("Failed to disable 40 MHz intolerant AP")

    # make sure the intolerant AP disappears from scan results more quickly
    dev[0].scan(type="ONLY", freq="2432", only_new=True)
    dev[0].scan(type="ONLY", freq="2432", only_new=True)
    dev[0].dump_monitor()

    logger.info("Waiting for AP to move back to 40 MHz channel")
    ok = False
    for i in range(0, 30):
        time.sleep(1)
        if hapd.get_status_field("secondary_channel") == "-1":
            logger.info("AP moved to 40 MHz channel")
            ok = True
            break
    if not ok:
        raise Exception("AP did not move to 40 MHz channel")

def test_ap_ht40_csa(dev, apdev):
    """HT with 40 MHz channel width and CSA"""
    csa_supported(dev[0])
    try:
        hapd = None
        params = { "ssid": "ht",
                   "country_code": "US",
                   "hw_mode": "a",
                   "channel": "36",
                   "ht_capab": "[HT40+]",
                   "ieee80211n": "1" }
        hapd = hostapd.add_ap(apdev[0]['ifname'], params)

        dev[0].connect("ht", key_mgmt="NONE", scan_freq="5180")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5200 ht sec_channel_offset=-1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5200" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5180 ht sec_channel_offset=1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5180" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()

def test_ap_ht40_csa2(dev, apdev):
    """HT with 40 MHz channel width and CSA"""
    csa_supported(dev[0])
    try:
        hapd = None
        params = { "ssid": "ht",
                   "country_code": "US",
                   "hw_mode": "a",
                   "channel": "36",
                   "ht_capab": "[HT40+]",
                   "ieee80211n": "1" }
        hapd = hostapd.add_ap(apdev[0]['ifname'], params)

        dev[0].connect("ht", key_mgmt="NONE", scan_freq="5180")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5220 ht sec_channel_offset=1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5220" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5180 ht sec_channel_offset=1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5180" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()

def test_ap_ht40_csa3(dev, apdev):
    """HT with 40 MHz channel width and CSA"""
    csa_supported(dev[0])
    try:
        hapd = None
        params = { "ssid": "ht",
                   "country_code": "US",
                   "hw_mode": "a",
                   "channel": "36",
                   "ht_capab": "[HT40+]",
                   "ieee80211n": "1" }
        hapd = hostapd.add_ap(apdev[0]['ifname'], params)

        dev[0].connect("ht", key_mgmt="NONE", scan_freq="5180")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5240 ht sec_channel_offset=-1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5240" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)

        hapd.request("CHAN_SWITCH 5 5180 ht sec_channel_offset=1 bandwidth=40")
        ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
        if ev is None:
            raise Exception("CSA finished event timed out")
        if "freq=5180" not in ev:
            raise Exception("Unexpected channel in CSA finished event")
        ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
        if ev is not None:
            raise Exception("Unexpected STA disconnection during CSA")
        hwsim_utils.test_connectivity(dev[0], hapd)
    finally:
        dev[0].request("DISCONNECT")
        if hapd:
            hapd.request("DISABLE")
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()

def test_ap_ht_smps(dev, apdev):
    """SMPS AP configuration options"""
    params = { "ssid": "ht1", "ht_capab": "[SMPS-STATIC]" }
    try:
        hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    except:
        raise HwsimSkip("Assume mac80211_hwsim was not recent enough to support SMPS")
    params = { "ssid": "ht2", "ht_capab": "[SMPS-DYNAMIC]" }
    hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)

    dev[0].connect("ht1", key_mgmt="NONE", scan_freq="2412")
    dev[1].connect("ht2", key_mgmt="NONE", scan_freq="2412")
    hwsim_utils.test_connectivity(dev[0], hapd)
    hwsim_utils.test_connectivity(dev[1], hapd2)

def test_prefer_ht20(dev, apdev):
    """Preference on HT20 over no-HT"""
    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "0" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    bssid = apdev[0]['bssid']
    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "1" }
    hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid, freq=2412)
    dev[0].scan_for_bss(bssid2, freq=2412)
    dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
    if dev[0].get_status_field('bssid') != bssid2:
        raise Exception("Unexpected BSS selected")

    est = dev[0].get_bss(bssid)['est_throughput']
    if est != "54000":
        raise Exception("Unexpected BSS0 est_throughput: " + est)

    est = dev[0].get_bss(bssid2)['est_throughput']
    if est != "65000":
        raise Exception("Unexpected BSS1 est_throughput: " + est)

def test_prefer_ht40(dev, apdev):
    """Preference on HT40 over HT20"""
    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "1" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    bssid = apdev[0]['bssid']
    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "1",
               "ht_capab": "[HT40+]" }
    hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid, freq=2412)
    dev[0].scan_for_bss(bssid2, freq=2412)
    dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
    if dev[0].get_status_field('bssid') != bssid2:
        raise Exception("Unexpected BSS selected")

    est = dev[0].get_bss(bssid)['est_throughput']
    if est != "65000":
        raise Exception("Unexpected BSS0 est_throughput: " + est)

    est = dev[0].get_bss(bssid2)['est_throughput']
    if est != "135000":
        raise Exception("Unexpected BSS1 est_throughput: " + est)

def test_prefer_ht20_during_roam(dev, apdev):
    """Preference on HT20 over no-HT in roaming consideration"""
    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "0" }
    hapd = hostapd.add_ap(apdev[0]['ifname'], params)
    bssid = apdev[0]['bssid']

    dev[0].scan_for_bss(bssid, freq=2412)
    dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")

    params = { "ssid": "test",
               "channel": "1",
               "ieee80211n": "1" }
    hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
    bssid2 = apdev[1]['bssid']
    dev[0].scan_for_bss(bssid2, freq=2412)
    dev[0].scan(freq=2412)
    dev[0].wait_connected()
    
    if dev[0].get_status_field('bssid') != bssid2:
        raise Exception("Unexpected BSS selected")

def test_ap_ht40_5ghz_invalid_pair(dev, apdev):
    """HT40 on 5 GHz with invalid channel pair"""
    clear_scan_cache(apdev[0]['ifname'])
    try:
        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "40",
                   "country_code": "US",
                   "ht_capab": "[HT40+]"}
        hapd = hostapd.add_ap(apdev[1]['ifname'], params, wait_enabled=False)
        ev = hapd.wait_event(["AP-DISABLED", "AP-ENABLED"], timeout=10)
        if not ev:
            raise Exception("AP setup failure timed out")
        if "AP-ENABLED" in ev:
            sec = hapd.get_status_field("secondary_channel")
            if sec != "0":
                raise Exception("Invalid 40 MHz channel accepted")
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def test_ap_ht40_5ghz_disabled_sec(dev, apdev):
    """HT40 on 5 GHz with disabled secondary channel"""
    clear_scan_cache(apdev[0]['ifname'])
    try:
        params = { "ssid": "test-ht40",
                   "hw_mode": "a",
                   "channel": "48",
                   "country_code": "US",
                   "ht_capab": "[HT40+]"}
        hapd = hostapd.add_ap(apdev[1]['ifname'], params, wait_enabled=False)
        ev = hapd.wait_event(["AP-DISABLED", "AP-ENABLED"], timeout=10)
        if not ev:
            raise Exception("AP setup failure timed out")
        if "AP-ENABLED" in ev:
            sec = hapd.get_status_field("secondary_channel")
            if sec != "0":
                raise Exception("Invalid 40 MHz channel accepted")
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])