aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hwsim/test_fst_module.py
blob: 4e182b97efa401135e6cb5fb69305828deb0cabc (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
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
# FST functionality tests
# Copyright (c) 2015, Qualcomm Atheros, Inc.
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

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

import hwsim_utils
from hwsim import HWSimRadio
import hostapd
from wpasupplicant import WpaSupplicant
import fst_test_common
import fst_module_aux
from utils import alloc_fail, HwsimSkip

#enum - bad parameter types
bad_param_none = 0
bad_param_session_add_no_params = 1
bad_param_group_id = 2
bad_param_session_set_no_params = 3
bad_param_session_set_unknown_param = 4
bad_param_session_id = 5
bad_param_old_iface = 6
bad_param_new_iface = 7
bad_param_negative_llt = 8
bad_param_zero_llt = 9
bad_param_llt_too_big = 10
bad_param_llt_nan = 11
bad_param_peer_addr = 12
bad_param_session_initiate_no_params = 13
bad_param_session_initiate_bad_session_id = 14
bad_param_session_initiate_with_no_new_iface_set = 15
bad_param_session_initiate_with_bad_peer_addr_set = 16
bad_param_session_initiate_request_with_bad_stie = 17
bad_param_session_initiate_response_with_reject = 18
bad_param_session_initiate_response_with_bad_stie = 19
bad_param_session_initiate_response_with_zero_llt = 20
bad_param_session_initiate_stt_no_response = 21
bad_param_session_initiate_concurrent_setup_request = 22
bad_param_session_transfer_no_params = 23
bad_param_session_transfer_bad_session_id = 24
bad_param_session_transfer_setup_skipped = 25
bad_param_session_teardown_no_params = 26
bad_param_session_teardown_bad_session_id = 27
bad_param_session_teardown_setup_skipped = 28
bad_param_session_teardown_bad_fsts_id = 29

bad_param_names = ("None",
                   "No params passed to session add",
                   "Group ID",
                   "No params passed to session set",
                   "Unknown param passed to session set",
                   "Session ID",
                   "Old interface name",
                   "New interface name",
                   "Negative LLT",
                   "Zero LLT",
                   "LLT too big",
                   "LLT is not a number",
                   "Peer address",
                   "No params passed to session initiate",
                   "Session ID",
                   "No new_iface was set",
                   "Peer address",
                   "Request with bad st ie",
                   "Response with reject",
                   "Response with bad st ie",
                   "Response with zero llt",
                   "No response, STT",
                   "Concurrent setup request",
                   "No params passed to session transfer",
                   "Session ID",
                   "Session setup skipped",
                   "No params passed to session teardown",
                   "Bad session",
                   "Session setup skipped",
                   "Bad fsts_id")

def fst_start_session(apdev, test_params, bad_param_type, start_on_ap,
                      peer_addr = None):
    """This function makes the necessary preparations and the adds and sets a
    session using either correct or incorrect parameters depending on the value
    of bad_param_type. If the call ends as expected (with session being
    successfully added and set in case of correct parameters or with the
    expected exception in case of incorrect parameters), the function silently
    exits. Otherwise, it throws an exception thus failing the test."""

    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if start_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        initiator.add_peer(responder, peer_addr, new_peer_addr)
        group_id = None
        if bad_param_type == bad_param_group_id:
            group_id = '-1'
        elif bad_param_type == bad_param_session_add_no_params:
            group_id = ''
        initiator.set_fst_parameters(group_id=group_id)
        sid = initiator.add_session()
        if bad_param_type == bad_param_session_set_no_params:
            res = initiator.set_session_param(None)
            if not res.startswith("OK"):
                raise Exception("Session set operation failed")
        elif bad_param_type == bad_param_session_set_unknown_param:
            res = initiator.set_session_param("bad_param=1")
            if not res.startswith("OK"):
                raise Exception("Session set operation failed")
        else:
            if bad_param_type == bad_param_session_initiate_with_no_new_iface_set:
                new_iface = None
            elif bad_param_type == bad_param_new_iface:
                new_iface = 'wlan12'
            old_iface = None if bad_param_type != bad_param_old_iface else 'wlan12'
            llt = None
            if bad_param_type == bad_param_negative_llt:
                llt = '-1'
            elif bad_param_type == bad_param_zero_llt:
                llt = '0'
            elif bad_param_type == bad_param_llt_too_big:
                llt = '4294967296'    #0x100000000
            elif bad_param_type == bad_param_llt_nan:
                llt = 'nan'
            elif bad_param_type == bad_param_session_id:
                sid = '-1'
            initiator.set_fst_parameters(llt=llt)
            initiator.configure_session(sid, new_iface, old_iface)
    except Exception, e:
        if e.args[0].startswith("Cannot add FST session with groupid"):
            if bad_param_type == bad_param_group_id or bad_param_type == bad_param_session_add_no_params:
                bad_parameter_detected = True
        elif e.args[0].startswith("Cannot set FST session new_ifname:"):
            if bad_param_type == bad_param_new_iface:
                bad_parameter_detected = True
        elif e.args[0].startswith("Session set operation failed"):
            if (bad_param_type == bad_param_session_set_no_params or
                bad_param_type == bad_param_session_set_unknown_param):
                bad_parameter_detected = True
        elif e.args[0].startswith("Cannot set FST session old_ifname:"):
            if (bad_param_type == bad_param_old_iface or
                bad_param_type == bad_param_session_id or
                bad_param_type == bad_param_session_set_no_params):
                bad_parameter_detected = True
        elif e.args[0].startswith("Cannot set FST session llt:"):
            if (bad_param_type == bad_param_negative_llt or
                bad_param_type == bad_param_llt_too_big or
                bad_param_type == bad_param_llt_nan):
                bad_parameter_detected = True
        elif e.args[0].startswith("Cannot set FST session peer address:"):
            if bad_param_type == bad_param_peer_addr:
                bad_parameter_detected = True
        if not bad_parameter_detected:
            # The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Bad parameter was detected (%s)" % bad_param_names[bad_param_type])
            else:
                if bad_param_type == bad_param_none or bad_param_type == bad_param_zero_llt:
                    logger.info("Success. Session added and set")
                else:
                    exception_text = ""
                    if bad_param_type == bad_param_peer_addr:
                        exception_text = "Failure. Bad parameter was not detected (Peer address == %s)" % ap1.get_new_peer_addr()
                    else:
                        exception_text = "Failure. Bad parameter was not detected (%s)" % bad_param_names[bad_param_type]
                    raise Exception(exception_text)
        else:
            logger.info("Failure. Unexpected exception")

def fst_initiate_session(apdev, test_params, bad_param_type, init_on_ap):
    """This function makes the necessary preparations and then adds, sets and
    initiates a session using either correct or incorrect parameters at each
    stage depending on the value of bad_param_type. If the call ends as expected
    (with session being successfully added, set and initiated in case of correct
    parameters or with the expected exception in case of incorrect parameters),
    the function silently exits. Otherwise it throws an exception thus failing
    the test."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if init_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname() if bad_param_type != bad_param_session_initiate_with_no_new_iface_set else None
            new_peer_addr = ap2.get_actual_peer_addr()
            resp_newif = sta2.ifname()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname() if bad_param_type != bad_param_session_initiate_with_no_new_iface_set else None
            new_peer_addr = sta2.get_actual_peer_addr()
            resp_newif = ap2.ifname()
        peeraddr = None if bad_param_type != bad_param_session_initiate_with_bad_peer_addr_set else '10:DE:AD:DE:AD:11'
        initiator.add_peer(responder, peeraddr, new_peer_addr)
        if bad_param_type == bad_param_session_initiate_response_with_zero_llt:
            initiator.set_fst_parameters(llt='0')
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        if bad_param_type == bad_param_session_initiate_no_params:
            sid = ''
        elif bad_param_type == bad_param_session_initiate_bad_session_id:
            sid = '-1'
        if bad_param_type == bad_param_session_initiate_request_with_bad_stie:
            actual_fsts_id = initiator.get_fsts_id_by_sid(sid)
            initiator.send_test_session_setup_request(str(actual_fsts_id), "bad_new_band")
            responder.wait_for_session_event(5)
        elif bad_param_type == bad_param_session_initiate_response_with_reject:
            initiator.send_session_setup_request(sid)
            initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            setup_event = responder.wait_for_session_event(5, [],
                                                           ['EVENT_FST_SETUP'])
            if not 'id' in setup_event:
                raise Exception("No session id in FST setup event")
            responder.send_session_setup_response(str(setup_event['id']),
                                                  "reject")
            event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            if event['new_state'] != "INITIAL" or event['reason'] != "REASON_REJECT":
                raise Exception("Response with reject not handled as expected")
            bad_parameter_detected = True
        elif bad_param_type == bad_param_session_initiate_response_with_bad_stie:
            initiator.send_session_setup_request(sid)
            initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            responder.wait_for_session_event(5, [], ['EVENT_FST_SETUP'])
            actual_fsts_id = initiator.get_fsts_id_by_sid(sid)
            responder.send_test_session_setup_response(str(actual_fsts_id),
                                                       "accept", "bad_new_band")
            event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            if event['new_state'] != "INITIAL" or event['reason'] != "REASON_ERROR_PARAMS":
                raise Exception("Response with bad STIE not handled as expected")
            bad_parameter_detected = True
        elif bad_param_type == bad_param_session_initiate_response_with_zero_llt:
            initiator.initiate_session(sid, "accept")
            event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            if event['new_state'] != "TRANSITION_DONE":
                raise Exception("Response reception for a session with llt=0 not handled as expected")
            bad_parameter_detected = True
        elif bad_param_type == bad_param_session_initiate_stt_no_response:
            initiator.send_session_setup_request(sid)
            initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            responder.wait_for_session_event(5, [], ['EVENT_FST_SETUP'])
            event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            if event['new_state'] != "INITIAL" or event['reason'] != "REASON_STT":
                raise Exception("No response scenario not handled as expected")
            bad_parameter_detected = True
        elif bad_param_type == bad_param_session_initiate_concurrent_setup_request:
            responder.add_peer(initiator)
            resp_sid = responder.add_session()
            responder.configure_session(resp_sid, resp_newif)
            initiator.send_session_setup_request(sid)
            actual_fsts_id = initiator.get_fsts_id_by_sid(sid)
            responder.send_test_session_setup_request(str(actual_fsts_id))
            event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
            initiator_addr = initiator.get_own_mac_address()
            responder_addr = responder.get_own_mac_address()
            if initiator_addr < responder_addr:
                event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
                if event['new_state'] != "INITIAL" or event['reason'] != "REASON_SETUP":
                    raise Exception("Concurrent setup scenario not handled as expected")
                event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SETUP"])
                # The incoming setup request received by the initiator has
                # priority over the one sent previously by the initiator itself
                # because the initiator's MAC address is numerically lower than
                # the one of the responder. Thus, the initiator should generate
                # an FST_SETUP event.
            else:
                event = initiator.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
                if event['new_state'] != "INITIAL" or event['reason'] != "REASON_STT":
                    raise Exception("Concurrent setup scenario not handled as expected")
                # The incoming setup request was dropped at the initiator
                # because its MAC address is numerically bigger than the one of
                # the responder. Thus, the initiator continue to wait for a
                # setup response until the STT event fires.
            bad_parameter_detected = True
        else:
            initiator.initiate_session(sid, "accept")
    except Exception, e:
        if e.args[0].startswith("Cannot initiate fst session"):
            if bad_param_type != bad_param_none:
                bad_parameter_detected = True
        elif e.args[0].startswith("No FST-EVENT-SESSION received"):
            if bad_param_type == bad_param_session_initiate_request_with_bad_stie:
                bad_parameter_detected = True
        if not bad_parameter_detected:
            #The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Bad parameter was detected (%s)" % bad_param_names[bad_param_type])
            else:
                if bad_param_type == bad_param_none:
                    logger.info("Success. Session initiated")
                else:
                    raise Exception("Failure. Bad parameter was not detected (%s)" % bad_param_names[bad_param_type])
        else:
            logger.info("Failure. Unexpected exception")

def fst_transfer_session(apdev, test_params, bad_param_type, init_on_ap,
                         rsn=False):
    """This function makes the necessary preparations and then adds, sets,
    initiates and attempts to transfer a session using either correct or
    incorrect parameters at each stage depending on the value of bad_param_type.
    If the call ends as expected the function silently exits. Otherwise, it
    throws an exception thus failing the test."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev, rsn=rsn)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2, rsn=rsn)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if init_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        initiator.add_peer(responder, new_peer_addr = new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        if bad_param_type != bad_param_session_transfer_setup_skipped:
            initiator.initiate_session(sid, "accept")
        if bad_param_type == bad_param_session_transfer_no_params:
            sid = ''
        elif bad_param_type == bad_param_session_transfer_bad_session_id:
            sid = '-1'
        initiator.transfer_session(sid)
    except Exception, e:
        if e.args[0].startswith("Cannot transfer fst session"):
            if bad_param_type != bad_param_none:
                bad_parameter_detected = True
        if not bad_parameter_detected:
            # The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Bad parameter was detected (%s)" % bad_param_names[bad_param_type])
            else:
                if bad_param_type == bad_param_none:
                    logger.info("Success. Session transferred")
                else:
                    raise Exception("Failure. Bad parameter was not detected (%s)" % bad_param_names[bad_param_type])
        else:
            logger.info("Failure. Unexpected exception")


def fst_tear_down_session(apdev, test_params, bad_param_type, init_on_ap):
    """This function makes the necessary preparations and then adds, sets, and
    initiates a session. It then issues a tear down command using either
    correct or incorrect parameters at each stage. If the call ends as expected,
    the function silently exits. Otherwise, it throws an exception thus failing
    the test."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if init_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        initiator.add_peer(responder, new_peer_addr = new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        if bad_param_type != bad_param_session_teardown_setup_skipped:
            initiator.initiate_session(sid, "accept")
        if bad_param_type == bad_param_session_teardown_bad_fsts_id:
            initiator.send_test_tear_down('-1')
            responder.wait_for_session_event(5)
        else:
            if bad_param_type == bad_param_session_teardown_no_params:
                sid = ''
            elif bad_param_type == bad_param_session_teardown_bad_session_id:
                sid = '-1'
            initiator.teardown_session(sid)
    except Exception, e:
        if e.args[0].startswith("Cannot tear down fst session"):
            if (bad_param_type == bad_param_session_teardown_no_params or
                bad_param_type == bad_param_session_teardown_bad_session_id or
                bad_param_type == bad_param_session_teardown_setup_skipped):
                bad_parameter_detected = True
        elif e.args[0].startswith("No FST-EVENT-SESSION received"):
            if bad_param_type == bad_param_session_teardown_bad_fsts_id:
                bad_parameter_detected = True
        if not bad_parameter_detected:
            # The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Bad parameter was detected (%s)" % bad_param_names[bad_param_type])
            else:
                if bad_param_type == bad_param_none:
                    logger.info("Success. Session torn down")
                else:
                    raise Exception("Failure. Bad parameter was not detected (%s)" % bad_param_names[bad_param_type])
        else:
            logger.info("Failure. Unexpected exception")


#enum - remove session scenarios
remove_scenario_no_params = 0
remove_scenario_bad_session_id = 1
remove_scenario_non_established_session = 2
remove_scenario_established_session = 3

remove_scenario_names = ("No params",
                         "Bad session id",
                         "Remove non-established session",
                         "Remove established session")


def fst_remove_session(apdev, test_params, remove_session_scenario, init_on_ap):
    """This function attempts to remove a session at various stages of its
    formation, depending on the value of remove_session_scenario. If the call
    ends as expected, the function silently exits. Otherwise, it throws an
    exception thus failing the test."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if init_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        initiator.add_peer(responder, new_peer_addr = new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        if remove_session_scenario != remove_scenario_no_params:
            if remove_session_scenario != remove_scenario_non_established_session:
                initiator.initiate_session(sid, "accept")
        if remove_session_scenario == remove_scenario_no_params:
            sid = ''
        elif remove_session_scenario == remove_scenario_bad_session_id:
            sid = '-1'
        initiator.remove_session(sid)
    except Exception, e:
        if e.args[0].startswith("Cannot remove fst session"):
            if (remove_session_scenario == remove_scenario_no_params or
                remove_session_scenario == remove_scenario_bad_session_id):
                bad_parameter_detected = True
        elif e.args[0].startswith("No FST-EVENT-SESSION received"):
            if remove_session_scenario == remove_scenario_non_established_session:
                bad_parameter_detected = True
        if not bad_parameter_detected:
            #The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Remove scenario ended as expected (%s)" % remove_scenario_names[remove_session_scenario])
            else:
                if remove_session_scenario == remove_scenario_established_session:
                    logger.info("Success. Session removed")
                else:
                    raise Exception("Failure. Remove scenario ended in an unexpected way (%s)" % remove_scenario_names[remove_session_scenario])
        else:
            logger.info("Failure. Unexpected exception")


#enum - frame types
frame_type_session_request = 0
frame_type_session_response = 1
frame_type_ack_request = 2
frame_type_ack_response = 3
frame_type_tear_down = 4

frame_type_names = ("Session request",
                    "Session Response",
                    "Ack request",
                    "Ack response",
                    "Tear down")

def fst_send_unexpected_frame(apdev, test_params, frame_type, send_from_ap, additional_param = ''):
    """This function creates two pairs of APs and stations, makes them connect
    and then causes one side to send an unexpected FST frame of the specified
    type to the other. The other side should then identify and ignore the
    frame."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    exception_already_raised = False
    frame_receive_timeout = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if send_from_ap:
            sender = ap1
            receiver = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            sender = sta1
            receiver = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        sender.add_peer(receiver, new_peer_addr = new_peer_addr)
        sid=sender.add_session()
        sender.configure_session(sid, new_iface)
        if frame_type == frame_type_session_request:
            sender.send_session_setup_request(sid)
            event = receiver.wait_for_session_event(5)
            if event['type'] != 'EVENT_FST_SETUP':
                raise Exception("Unexpected indication: " + event['type'])
        elif frame_type == frame_type_session_response:
            #fsts_id doesn't matter, no actual session exists
            sender.send_test_session_setup_response('0', additional_param)
            receiver.wait_for_session_event(5)
        elif frame_type == frame_type_ack_request:
            #fsts_id doesn't matter, no actual session exists
            sender.send_test_ack_request('0')
            receiver.wait_for_session_event(5)
        elif frame_type == frame_type_ack_response:
            #fsts_id doesn't matter, no actual session exists
            sender.send_test_ack_response('0')
            receiver.wait_for_session_event(5)
        elif frame_type == frame_type_tear_down:
            #fsts_id doesn't matter, no actual session exists
            sender.send_test_tear_down('0')
            receiver.wait_for_session_event(5)
    except Exception, e:
        if e.args[0].startswith("No FST-EVENT-SESSION received"):
            if frame_type != frame_type_session_request:
                frame_receive_timeout = True
        else:
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if frame_receive_timeout:
                logger.info("Success. Frame was ignored (%s)" % frame_type_names[frame_type])
            else:
                if frame_type == frame_type_session_request:
                    logger.info("Success. Frame received, session created")
                else:
                    raise Exception("Failure. Frame was not ignored (%s)" % frame_type_names[frame_type])
        else:
            logger.info("Failure. Unexpected exception")


#enum - bad session transfer scenarios
bad_scenario_none = 0
bad_scenario_ack_req_session_not_set_up = 1
bad_scenario_ack_req_session_not_established_init_side = 2
bad_scenario_ack_req_session_not_established_resp_side = 3
bad_scenario_ack_req_bad_fsts_id = 4
bad_scenario_ack_resp_session_not_set_up = 5
bad_scenario_ack_resp_session_not_established_init_side = 6
bad_scenario_ack_resp_session_not_established_resp_side = 7
bad_scenario_ack_resp_no_ack_req = 8
bad_scenario_ack_resp_bad_fsts_id = 9

bad_scenario_names = ("None",
                      "Ack request received before the session was set up",
                      "Ack request received on the initiator side before session was established",
                      "Ack request received on the responder side before session was established",
                      "Ack request received with bad fsts_id",
                      "Ack response received before the session was set up",
                      "Ack response received on the initiator side before session was established",
                      "Ack response received on the responder side before session was established",
                      "Ack response received before ack request was sent",
                      "Ack response received with bad fsts_id")

def fst_bad_transfer(apdev, test_params, bad_scenario_type, init_on_ap):
    """This function makes the necessary preparations and then adds and sets a
    session. It then initiates and it unless instructed otherwise) and attempts
    to send one of the frames involved in the session transfer protocol,
    skipping or distorting one of the stages according to the value of
    bad_scenario_type parameter."""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    bad_parameter_detected = False
    exception_already_raised = False
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        # This call makes sure FstHostapd singleton object is created and, as a
        # result, the global control interface is registered (this is done from
        # the constructor).
        ap1.get_global_instance()
        if init_on_ap:
            initiator = ap1
            responder = sta1
            new_iface = ap2.ifname()
            new_peer_addr = ap2.get_actual_peer_addr()
        else:
            initiator = sta1
            responder = ap1
            new_iface = sta2.ifname()
            new_peer_addr = sta2.get_actual_peer_addr()
        initiator.add_peer(responder, new_peer_addr = new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        if (bad_scenario_type != bad_scenario_ack_req_session_not_set_up and
            bad_scenario_type != bad_scenario_ack_resp_session_not_set_up):
            if (bad_scenario_type != bad_scenario_ack_req_session_not_established_init_side and
                bad_scenario_type != bad_scenario_ack_resp_session_not_established_init_side and
                bad_scenario_type != bad_scenario_ack_req_session_not_established_resp_side and
                bad_scenario_type != bad_scenario_ack_resp_session_not_established_resp_side):
                response =  "accept"
            else:
                response = ''
            initiator.initiate_session(sid, response)
        if bad_scenario_type == bad_scenario_ack_req_session_not_set_up:
            #fsts_id doesn't matter, no actual session exists
            responder.send_test_ack_request('0')
            initiator.wait_for_session_event(5)
            # We want to send the unexpected frame to the side that already has
            # a session created
        elif bad_scenario_type == bad_scenario_ack_resp_session_not_set_up:
            #fsts_id doesn't matter, no actual session exists
            responder.send_test_ack_response('0')
            initiator.wait_for_session_event(5)
            # We want to send the unexpected frame to the side that already has
            # a session created
        elif bad_scenario_type == bad_scenario_ack_req_session_not_established_init_side:
            #fsts_id doesn't matter, no actual session exists
            initiator.send_test_ack_request('0')
            responder.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_req_session_not_established_resp_side:
            #fsts_id doesn't matter, no actual session exists
            responder.send_test_ack_request('0')
            initiator.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_resp_session_not_established_init_side:
            #fsts_id doesn't matter, no actual session exists
            initiator.send_test_ack_response('0')
            responder.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_resp_session_not_established_resp_side:
            #fsts_id doesn't matter, no actual session exists
            responder.send_test_ack_response('0')
            initiator.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_req_bad_fsts_id:
            initiator.send_test_ack_request('-1')
            responder.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_resp_bad_fsts_id:
            initiator.send_test_ack_response('-1')
            responder.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        elif bad_scenario_type == bad_scenario_ack_resp_no_ack_req:
            actual_fsts_id = initiator.get_fsts_id_by_sid(sid)
            initiator.send_test_ack_response(str(actual_fsts_id))
            responder.wait_for_session_event(5, ["EVENT_FST_SESSION_STATE"])
        else:
            raise Exception("Unknown bad scenario identifier")
    except Exception, e:
        if e.args[0].startswith("No FST-EVENT-SESSION received"):
            bad_parameter_detected = True
        if not bad_parameter_detected:
            # The exception was unexpected
            logger.info(e)
            exception_already_raised = True
            raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        if not exception_already_raised:
            if bad_parameter_detected:
                logger.info("Success. Bad scenario was handled correctly (%s)" % bad_scenario_names[bad_scenario_type])
            else:
                raise Exception("Failure. Bad scenario was handled incorrectly (%s)" % bad_scenario_names[bad_scenario_type])
        else:
            logger.info("Failure. Unexpected exception")

def test_fst_sta_connect_to_non_fst_ap(dev, apdev, test_params):
    """FST STA connecting to non-FST AP"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_sta_connect_to_fst_ap(dev, apdev, test_params):
    """FST STA connecting to FST AP"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        orig_sta2_mbies = sta2.get_local_mbies()
        vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
        sta1.connect(ap1, key_mgmt="NONE",
                     scan_freq=fst_test_common.fst_test_def_freq_a)
        time.sleep(2)
        res_sta2_mbies = sta2.get_local_mbies()
        if res_sta2_mbies == orig_sta2_mbies:
            raise Exception("Failure. MB IEs have not been updated")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        sta1.disconnect()
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_ap_connect_to_fst_sta(dev, apdev, test_params):
    """FST AP connecting to FST STA"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        orig_ap_mbies = ap1.get_local_mbies()
        vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
        sta1.connect(ap1, key_mgmt="NONE",
                     scan_freq=fst_test_common.fst_test_def_freq_a)
        time.sleep(2)
        res_ap_mbies = ap1.get_local_mbies()
        if res_ap_mbies != orig_ap_mbies:
            raise Exception("Failure. MB IEs have been unexpectedly updated on the AP")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        sta1.disconnect()
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_ap_connect_to_non_fst_sta(dev, apdev, test_params):
    """FST AP connecting to non-FST STA"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        orig_ap_mbies = ap2.get_local_mbies()
        vals = dev[0].scan(None, fst_test_common.fst_test_def_freq_g)
        fst_module_aux.external_sta_connect(dev[0], ap2, key_mgmt="NONE",
                                            scan_freq=fst_test_common.fst_test_def_freq_g)
        time.sleep(2)
        res_ap_mbies = ap2.get_local_mbies()
        if res_ap_mbies != orig_ap_mbies:
            raise Exception("Failure. MB IEs have been unexpectedly updated on the AP")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        fst_module_aux.disconnect_external_sta(dev[0], ap2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_second_sta_connect_to_non_fst_ap(dev, apdev, test_params):
    """FST STA 2nd connecting to non-FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_second_sta_connect_to_fst_ap(dev, apdev, test_params):
    """FST STA 2nd connecting to FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_disconnect_1_of_2_stas_from_non_fst_ap(dev, apdev, test_params):
    """FST disconnect 1 of 2 STAs from non-FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta2.disconnect_from_external_ap()
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_disconnect_1_of_2_stas_from_fst_ap(dev, apdev, test_params):
    """FST disconnect 1 of 2 STAs from FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta1.disconnect()
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_disconnect_2_of_2_stas_from_non_fst_ap(dev, apdev, test_params):
    """FST disconnect 2 of 2 STAs from non-FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g" })
        try:
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            sta1.disconnect()
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta2.disconnect_from_external_ap()
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs must be present on the stations")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_disconnect_2_of_2_stas_from_fst_ap(dev, apdev, test_params):
    """FST disconnect 2 of 2 STAs from FST AP"""
    fst_ap1, fst_ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    with HWSimRadio() as (radio, iface):
        non_fst_ap = hostapd.add_ap(iface, { "ssid": "non_fst_11g"})
        try:
            vals = sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
            sta1.connect(fst_ap1, key_mgmt="NONE", scan_freq=fst_test_common.fst_test_def_freq_a)
            sta2.connect_to_external_ap(non_fst_ap, ssid="non_fst_11g",
                                        key_mgmt="NONE", scan_freq='2412')
            time.sleep(2)
            sta2.disconnect_from_external_ap()
            time.sleep(2)
            orig_sta1_mbies = sta1.get_local_mbies()
            orig_sta2_mbies = sta2.get_local_mbies()
            sta1.disconnect()
            time.sleep(2)
            res_sta1_mbies = sta1.get_local_mbies()
            res_sta2_mbies = sta2.get_local_mbies()
            if (orig_sta1_mbies.startswith("FAIL") or
                orig_sta2_mbies.startswith("FAIL") or
                res_sta1_mbies.startswith("FAIL") or
                res_sta2_mbies.startswith("FAIL")):
                raise Exception("Failure. MB IEs should have stayed present on both stations")
            # Mandatory part of 8.4.2.140 Multi-band element is 24 bytes = 48 hex chars
            basic_sta1_mbies = res_sta1_mbies[0:48] + res_sta1_mbies[60:108]
            basic_sta2_mbies = res_sta2_mbies[0:48] + res_sta2_mbies[60:108]
            if (basic_sta1_mbies != basic_sta2_mbies):
                raise Exception("Failure. Basic MB IEs should have become identical on both stations")
            addr_sta1_str = sta1.get_own_mac_address().replace(":", "")
            addr_sta2_str = sta2.get_own_mac_address().replace(":", "")
            # Mandatory part of 8.4.2.140 Multi-band element is followed by STA MAC Address field (6 bytes = 12 hex chars)
            addr_sta1_mbie1 = res_sta1_mbies[48:60]
            addr_sta1_mbie2 = res_sta1_mbies[108:120]
            addr_sta2_mbie1 = res_sta2_mbies[48:60]
            addr_sta2_mbie2 = res_sta2_mbies[108:120]
            if (addr_sta1_mbie1 != addr_sta1_mbie2 or
                addr_sta1_mbie1 != addr_sta2_str or
                addr_sta2_mbie1 != addr_sta2_mbie2 or
                addr_sta2_mbie1 != addr_sta1_str):
                raise Exception("Failure. STA Address in MB IEs should have been same as the other STA's")
        except Exception, e:
            logger.info(e)
            raise
        finally:
            sta1.disconnect()
            sta2.disconnect_from_external_ap()
            fst_module_aux.stop_two_ap_sta_pairs(fst_ap1, fst_ap2, sta1, sta2)
            hostapd.HostapdGlobal().remove(iface)

def test_fst_disconnect_non_fst_sta(dev, apdev, test_params):
    """FST disconnect non-FST STA"""
    ap1, ap2, fst_sta1, fst_sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    external_sta_connected = False
    try:
        vals = fst_sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
        fst_sta1.connect(ap1, key_mgmt="NONE",
                         scan_freq=fst_test_common.fst_test_def_freq_a)
        vals = dev[0].scan(None, fst_test_common.fst_test_def_freq_g)
        fst_module_aux.external_sta_connect(dev[0], ap2, key_mgmt="NONE",
                                            scan_freq=fst_test_common.fst_test_def_freq_g)
        external_sta_connected = True
        time.sleep(2)
        fst_sta1.disconnect()
        time.sleep(2)
        orig_ap_mbies = ap2.get_local_mbies()
        fst_module_aux.disconnect_external_sta(dev[0], ap2)
        external_sta_connected = False
        time.sleep(2)
        res_ap_mbies = ap2.get_local_mbies()
        if res_ap_mbies != orig_ap_mbies:
            raise Exception("Failure. MB IEs have been unexpectedly updated on the AP")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        fst_sta1.disconnect()
        if external_sta_connected:
            fst_module_aux.disconnect_external_sta(dev[0], ap2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, fst_sta1, fst_sta2)

def test_fst_disconnect_fst_sta(dev, apdev, test_params):
    """FST disconnect FST STA"""
    ap1, ap2, fst_sta1, fst_sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    external_sta_connected = False
    try:
        vals = fst_sta1.scan(freq=fst_test_common.fst_test_def_freq_a)
        fst_sta1.connect(ap1, key_mgmt="NONE",
                         scan_freq=fst_test_common.fst_test_def_freq_a)
        vals = dev[0].scan(None, fst_test_common.fst_test_def_freq_g)
        fst_module_aux.external_sta_connect(dev[0], ap2, key_mgmt="NONE",
                                            scan_freq=fst_test_common.fst_test_def_freq_g)
        external_sta_connected = True
        time.sleep(2)
        fst_module_aux.disconnect_external_sta(dev[0], ap2)
        external_sta_connected = False
        time.sleep(2)
        orig_ap_mbies = ap2.get_local_mbies()
        fst_sta1.disconnect()
        time.sleep(2)
        res_ap_mbies = ap2.get_local_mbies()
        if res_ap_mbies != orig_ap_mbies:
            raise Exception("Failure. MB IEs have been unexpectedly updated on the AP")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        fst_sta1.disconnect()
        if external_sta_connected:
            fst_module_aux.disconnect_external_sta(dev[0], ap2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, fst_sta1, fst_sta2)

def test_fst_dynamic_iface_attach(dev, apdev, test_params):
    """FST dynamic interface attach"""
    ap1 = fst_module_aux.FstAP(apdev[0]['ifname'], 'fst_11a', 'a',
                               fst_test_common.fst_test_def_chan_a,
                               fst_test_common.fst_test_def_group,
                               fst_test_common.fst_test_def_prio_low,
                               fst_test_common.fst_test_def_llt)
    ap1.start()
    ap2 = fst_module_aux.FstAP(apdev[1]['ifname'], 'fst_11g', 'b',
                               fst_test_common.fst_test_def_chan_g,
                               '', '', '')
    ap2.start()

    sta1 = fst_module_aux.FstSTA('wlan5',
                                 fst_test_common.fst_test_def_group,
                                 fst_test_common.fst_test_def_prio_low,
                                 fst_test_common.fst_test_def_llt)
    sta1.start()
    sta2 = fst_module_aux.FstSTA('wlan6', '', '', '')
    sta2.start()

    try:
        orig_sta2_mbies = sta2.get_local_mbies()
        orig_ap2_mbies = ap2.get_local_mbies()
        sta2.send_iface_attach_request(sta2.ifname(),
                                       fst_test_common.fst_test_def_group,
                                       '52', '27')
        event = sta2.wait_for_iface_event(5)
        if event['event_type'] != 'attached':
            raise Exception("Failure. Iface was not properly attached")
        ap2.send_iface_attach_request(ap2.ifname(),
                                      fst_test_common.fst_test_def_group,
                                      '102', '77')
        event = ap2.wait_for_iface_event(5)
        if event['event_type'] != 'attached':
            raise Exception("Failure. Iface was not properly attached")
        time.sleep(2)
        res_sta2_mbies = sta2.get_local_mbies()
        res_ap2_mbies = ap2.get_local_mbies()
        sta2.send_iface_detach_request(sta2.ifname())
        event = sta2.wait_for_iface_event(5)
        if event['event_type'] != 'detached':
            raise Exception("Failure. Iface was not properly detached")
        ap2.send_iface_detach_request(ap2.ifname())
        event = ap2.wait_for_iface_event(5)
        if event['event_type'] != 'detached':
            raise Exception("Failure. Iface was not properly detached")
        if (not orig_sta2_mbies.startswith("FAIL") or
            not orig_ap2_mbies.startswith("FAIL") or
            res_sta2_mbies.startswith("FAIL") or
            res_ap2_mbies.startswith("FAIL")):
            raise Exception("Failure. MB IEs should have appeared on the station and on the AP")
    except Exception, e:
        logger.info(e)
        raise
    finally:
        ap1.stop()
        ap2.stop()
        sta1.stop()
        sta2.stop()

# AP side FST module tests

def test_fst_ap_start_session(dev, apdev, test_params):
    """FST AP start session"""
    fst_start_session(apdev, test_params, bad_param_none, True)

def test_fst_ap_start_session_no_add_params(dev, apdev, test_params):
    """FST AP start session - no add params"""
    fst_start_session(apdev, test_params, bad_param_session_add_no_params, True)

def test_fst_ap_start_session_bad_group_id(dev, apdev, test_params):
    """FST AP start session - bad group id"""
    fst_start_session(apdev, test_params, bad_param_group_id, True)

def test_fst_ap_start_session_no_set_params(dev, apdev, test_params):
    """FST AP start session - no set params"""
    fst_start_session(apdev, test_params, bad_param_session_set_no_params, True)

def test_fst_ap_start_session_set_unknown_param(dev, apdev, test_params):
    """FST AP start session - set unknown param"""
    fst_start_session(apdev, test_params, bad_param_session_set_unknown_param,
                      True)

def test_fst_ap_start_session_bad_session_id(dev, apdev, test_params):
    """FST AP start session - bad session id"""
    fst_start_session(apdev, test_params, bad_param_session_id, True)

def test_fst_ap_start_session_bad_new_iface(dev, apdev, test_params):
    """FST AP start session - bad new iface"""
    fst_start_session(apdev, test_params, bad_param_new_iface, True)

def test_fst_ap_start_session_bad_old_iface(dev, apdev, test_params):
    """FST AP start session - bad old iface"""
    fst_start_session(apdev, test_params, bad_param_old_iface, True)

def test_fst_ap_start_session_negative_llt(dev, apdev, test_params):
    """FST AP start session - negative llt"""
    fst_start_session(apdev, test_params, bad_param_negative_llt, True)

def test_fst_ap_start_session_zero_llt(dev, apdev, test_params):
    """FST AP start session - zero llt"""
    fst_start_session(apdev, test_params, bad_param_zero_llt, True)

def test_fst_ap_start_session_llt_too_big(dev, apdev, test_params):
    """FST AP start session - llt too large"""
    fst_start_session(apdev, test_params, bad_param_llt_too_big, True)

def test_fst_ap_start_session_invalid_peer_addr(dev, apdev, test_params):
    """FST AP start session - invalid peer address"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, True,
                      'GG:GG:GG:GG:GG:GG')

def test_fst_ap_start_session_multicast_peer_addr(dev, apdev, test_params):
    """FST AP start session - multicast peer address"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, True,
                      '01:00:11:22:33:44')

def test_fst_ap_start_session_broadcast_peer_addr(dev, apdev, test_params):
    """FST AP start session - broadcast peer address"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, True,
                      'FF:FF:FF:FF:FF:FF')

def test_fst_ap_initiate_session(dev, apdev, test_params):
    """FST AP initiate session"""
    fst_initiate_session(apdev, test_params, bad_param_none, True)

def test_fst_ap_initiate_session_no_params(dev, apdev, test_params):
    """FST AP initiate session - no params"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_no_params, True)

def test_fst_ap_initiate_session_invalid_session_id(dev, apdev, test_params):
    """FST AP initiate session - invalid session id"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_bad_session_id, True)

def test_fst_ap_initiate_session_no_new_iface(dev, apdev, test_params):
    """FST AP initiate session - no new iface"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_with_no_new_iface_set, True)

def test_fst_ap_initiate_session_bad_peer_addr(dev, apdev, test_params):
    """FST AP initiate session - bad peer address"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_with_bad_peer_addr_set,
                         True)

def test_fst_ap_initiate_session_request_with_bad_stie(dev, apdev, test_params):
    """FST AP initiate session - request with bad stie"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_request_with_bad_stie, True)

def test_fst_ap_initiate_session_response_with_reject(dev, apdev, test_params):
    """FST AP initiate session - response with reject"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_response_with_reject, True)

def test_fst_ap_initiate_session_response_with_bad_stie(dev, apdev,
                                                        test_params):
    """FST AP initiate session - response with bad stie"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_response_with_bad_stie,
                         True)

def test_fst_ap_initiate_session_response_with_zero_llt(dev, apdev,
                                                        test_params):
    """FST AP initiate session - zero llt"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_response_with_zero_llt,
                         True)

def test_fst_ap_initiate_session_stt_no_response(dev, apdev, test_params):
    """FST AP initiate session - stt no response"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_stt_no_response, True)

def test_fst_ap_initiate_session_concurrent_setup_request(dev, apdev,
                                                          test_params):
    """FST AP initiate session - concurrent setup request"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_concurrent_setup_request,
                         True)

def test_fst_ap_session_request_with_no_session(dev, apdev, test_params):
    """FST AP session request with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_request,
                              True)

def test_fst_ap_session_response_accept_with_no_session(dev, apdev,
                                                        test_params):
    """FST AP session response accept with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_response,
                              True, "accept")

def test_fst_ap_session_response_reject_with_no_session(dev, apdev,
                                                        test_params):
    """FST AP session response reject with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_response,
                              True, "reject")

def test_fst_ap_ack_request_with_no_session(dev, apdev, test_params):
    """FST AP ack request with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_ack_request, True)

def test_fst_ap_ack_response_with_no_session(dev, apdev, test_params):
    """FST AP ack response with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_ack_response, True)

def test_fst_ap_tear_down_response_with_no_session(dev, apdev, test_params):
    """FST AP tear down response with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_tear_down, True)

def test_fst_ap_transfer_session(dev, apdev, test_params):
    """FST AP transfer session"""
    fst_transfer_session(apdev, test_params, bad_param_none, True)

def test_fst_ap_transfer_session_no_params(dev, apdev, test_params):
    """FST AP transfer session - no params"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_no_params, True)

def test_fst_ap_transfer_session_bad_session_id(dev, apdev, test_params):
    """FST AP transfer session - bad session id"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_bad_session_id, True)

def test_fst_ap_transfer_session_setup_skipped(dev, apdev, test_params):
    """FST AP transfer session - setup skipped"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_setup_skipped, True)

def test_fst_ap_ack_request_with_session_not_set_up(dev, apdev, test_params):
    """FST AP ack request with session not set up"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_set_up, True)

def test_fst_ap_ack_request_with_session_not_established_init_side(dev, apdev,
                                                                   test_params):
    """FST AP ack request with session not established init side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_established_init_side,
                     True)

def test_fst_ap_ack_request_with_session_not_established_resp_side(dev, apdev,
                                                                   test_params):
    """FST AP ack request with session not established resp side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_established_resp_side,
                     True)

def test_fst_ap_ack_request_with_bad_fsts_id(dev, apdev, test_params):
    """FST AP ack request with bad fsts id"""
    fst_bad_transfer(apdev, test_params, bad_scenario_ack_req_bad_fsts_id, True)

def test_fst_ap_ack_response_with_session_not_set_up(dev, apdev, test_params):
    """FST AP ack response with session not set up"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_set_up, True)

def test_fst_ap_ack_response_with_session_not_established_init_side(dev, apdev, test_params):
    """FST AP ack response with session not established init side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_established_init_side,
                     True)

def test_fst_ap_ack_response_with_session_not_established_resp_side(dev, apdev, test_params):
    """FST AP ack response with session not established resp side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_established_resp_side,
                     True)

def test_fst_ap_ack_response_with_no_ack_request(dev, apdev, test_params):
    """FST AP ack response with no ack request"""
    fst_bad_transfer(apdev, test_params, bad_scenario_ack_resp_no_ack_req, True)

def test_fst_ap_tear_down_session(dev, apdev, test_params):
    """FST AP tear down session"""
    fst_tear_down_session(apdev, test_params, bad_param_none, True)

def test_fst_ap_tear_down_session_no_params(dev, apdev, test_params):
    """FST AP tear down session - no params"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_no_params, True)

def test_fst_ap_tear_down_session_bad_session_id(dev, apdev, test_params):
    """FST AP tear down session - bad session id"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_bad_session_id, True)

def test_fst_ap_tear_down_session_setup_skipped(dev, apdev, test_params):
    """FST AP tear down session - setup skipped"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_setup_skipped, True)

def test_fst_ap_tear_down_session_bad_fsts_id(dev, apdev, test_params):
    """FST AP tear down session - bad fsts id"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_bad_fsts_id, True)

def test_fst_ap_remove_session_not_established(dev, apdev, test_params):
    """FST AP remove session - not established"""
    fst_remove_session(apdev, test_params,
                       remove_scenario_non_established_session, True)

def test_fst_ap_remove_session_established(dev, apdev, test_params):
    """FST AP remove session - established"""
    fst_remove_session(apdev, test_params,
                       remove_scenario_established_session, True)

def test_fst_ap_remove_session_no_params(dev, apdev, test_params):
    """FST AP remove session - no params"""
    fst_remove_session(apdev, test_params, remove_scenario_no_params, True)

def test_fst_ap_remove_session_bad_session_id(dev, apdev, test_params):
    """FST AP remove session - bad session id"""
    fst_remove_session(apdev, test_params, remove_scenario_bad_session_id, True)

def test_fst_ap_ctrl_iface(dev, apdev, test_params):
    """FST control interface behavior"""
    hglobal = hostapd.HostapdGlobal()
    start_num_groups = 0
    res = hglobal.request("FST-MANAGER LIST_GROUPS")
    del hglobal
    if "FAIL" not in res:
        start_num_groups = len(res.splitlines())

    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        initiator = ap1
        responder = sta1
        initiator.add_peer(responder, None)
        initiator.set_fst_parameters(group_id=None)
        sid = initiator.add_session()
        res = initiator.get_session_params(sid)
        logger.info("Initial session params:\n" + str(res))
        if res['state'] != 'INITIAL':
            raise Exception("Unexpected state: " + res['state'])
        initiator.set_fst_parameters(llt=None)
        initiator.configure_session(sid, ap2.ifname(), None)
        res = initiator.get_session_params(sid)
        logger.info("Session params after configuration:\n" + str(res))
        res = initiator.iface_peers(initiator.ifname())
        logger.info("Interface peers: " + str(res))
        if len(res) != 1:
            raise Exception("Unexpected number of peers")
        res = initiator.get_peer_mbies(initiator.ifname(),
                                       initiator.get_new_peer_addr())
        logger.info("Peer MB IEs: " + str(res))
        res = initiator.list_ifaces()
        logger.info("Interfaces: " + str(res))
        if len(res) != 2:
            raise Exception("Unexpected number of interfaces")
        res = initiator.list_groups()
        logger.info("Groups: " + str(res))
        if len(res) != 1 + start_num_groups:
            raise Exception("Unexpected number of groups")

        tests = [ "LIST_IFACES unknown",
                  "LIST_IFACES     unknown2",
                  "SESSION_GET 12345678",
                  "SESSION_SET " + sid + " unknown=foo",
                  "SESSION_RESPOND 12345678 foo",
                  "SESSION_RESPOND " + sid,
                  "SESSION_RESPOND " + sid + " foo",
                  "TEST_REQUEST foo",
                  "TEST_REQUEST SEND_SETUP_REQUEST",
                  "TEST_REQUEST SEND_SETUP_REQUEST foo",
                  "TEST_REQUEST SEND_SETUP_RESPONSE",
                  "TEST_REQUEST SEND_SETUP_RESPONSE foo",
                  "TEST_REQUEST SEND_ACK_REQUEST",
                  "TEST_REQUEST SEND_ACK_REQUEST foo",
                  "TEST_REQUEST SEND_ACK_RESPONSE",
                  "TEST_REQUEST SEND_ACK_RESPONSE foo",
                  "TEST_REQUEST SEND_TEAR_DOWN",
                  "TEST_REQUEST SEND_TEAR_DOWN foo",
                  "TEST_REQUEST GET_FSTS_ID",
                  "TEST_REQUEST GET_FSTS_ID foo",
                  "TEST_REQUEST GET_LOCAL_MBIES",
                  "TEST_REQUEST GET_LOCAL_MBIES foo",
                  "GET_PEER_MBIES",
                  "GET_PEER_MBIES ",
                  "GET_PEER_MBIES unknown",
                  "GET_PEER_MBIES unknown unknown",
                  "GET_PEER_MBIES unknown  " + initiator.get_new_peer_addr(),
                  "GET_PEER_MBIES " + initiator.ifname() + " 01:ff:ff:ff:ff:ff",
                  "GET_PEER_MBIES " + initiator.ifname() + " 00:ff:ff:ff:ff:ff",
                  "GET_PEER_MBIES " + initiator.ifname() + " 00:00:00:00:00:00",
                  "IFACE_PEERS",
                  "IFACE_PEERS ",
                  "IFACE_PEERS unknown",
                  "IFACE_PEERS unknown unknown",
                  "IFACE_PEERS " + initiator.fst_group,
                  "IFACE_PEERS " + initiator.fst_group + " unknown" ]
        for t in tests:
            if "FAIL" not in initiator.grequest("FST-MANAGER " + t):
                raise Exception("Unexpected response for invalid FST-MANAGER command " + t)
        if "UNKNOWN FST COMMAND" not in initiator.grequest("FST-MANAGER unknown"):
            raise Exception("Unexpected response for unknown FST-MANAGER command")

        tests = [ "FST-DETACH", "FST-DETACH ", "FST-DETACH unknown",
                  "FST-ATTACH", "FST-ATTACH ", "FST-ATTACH unknown",
                  "FST-ATTACH unknown unknown" ]
        for t in tests:
            if "FAIL" not in initiator.grequest(t):
                raise Exception("Unexpected response for invalid command " + t)

        try:
            # Trying to add same interface again needs to fail.
            ap1.send_iface_attach_request(ap1.iface, ap1.fst_group,
                                          ap1.fst_llt, ap1.fst_pri)
            raise Exception("Duplicate FST-ATTACH succeeded")
        except Exception, e:
            if not str(e).startswith("Cannot attach"):
                raise

        try:
            ap1.get_fsts_id_by_sid("123")
        except Exception, e:
            if not str(e).startswith("Cannot get fsts_id for sid"):
                raise
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_ap_start_session_oom(dev, apdev, test_params):
    """FST AP setup failing due to OOM"""
    ap1 = fst_module_aux.FstAP(apdev[0]['ifname'], 'fst_11a', 'a',
                               fst_test_common.fst_test_def_chan_a,
                               fst_test_common.fst_test_def_group,
                               fst_test_common.fst_test_def_prio_low,
                               fst_test_common.fst_test_def_llt)
    ap1.start()
    try:
        run_fst_ap_start_session_oom(apdev, ap1)
    finally:
        ap1.stop()
        fst_test_common.fst_clear_regdom()

def run_fst_ap_start_session_oom(apdev, ap1):
    with alloc_fail(ap1, 1, "fst_iface_create"):
        ap2_started = False
        try:
            ap2 = fst_module_aux.FstAP(apdev[1]['ifname'], 'fst_11g', 'b',
                                       fst_test_common.fst_test_def_chan_g,
                                       fst_test_common.fst_test_def_group,
                                       fst_test_common.fst_test_def_prio_high,
                                       fst_test_common.fst_test_def_llt)
            try:
                # This will fail in fst_iface_create() OOM
                ap2.start()
            except:
                pass
        finally:
            try:
                ap2.stop()
            except:
                pass

# STA side FST module tests

def test_fst_sta_start_session(dev, apdev, test_params):
    """FST STA start session"""
    fst_start_session(apdev, test_params, bad_param_none, False)

def test_fst_sta_start_session_no_add_params(dev, apdev, test_params):
    """FST STA start session - no add params"""
    fst_start_session(apdev, test_params, bad_param_session_add_no_params,
                      False)

def test_fst_sta_start_session_bad_group_id(dev, apdev, test_params):
    """FST STA start session - bad group id"""
    fst_start_session(apdev, test_params, bad_param_group_id, False)

def test_fst_sta_start_session_no_set_params(dev, apdev, test_params):
    """FST STA start session - no set params"""
    fst_start_session(apdev, test_params, bad_param_session_set_no_params,
                      False)

def test_fst_sta_start_session_set_unknown_param(dev, apdev, test_params):
    """FST STA start session - set unknown param"""
    fst_start_session(apdev, test_params, bad_param_session_set_unknown_param,
                      False)

def test_fst_sta_start_session_bad_session_id(dev, apdev, test_params):
    """FST STA start session - bad session id"""
    fst_start_session(apdev, test_params, bad_param_session_id, False)

def test_fst_sta_start_session_bad_new_iface(dev, apdev, test_params):
    """FST STA start session - bad new iface"""
    fst_start_session(apdev, test_params, bad_param_new_iface, False)

def test_fst_sta_start_session_bad_old_iface(dev, apdev, test_params):
    """FST STA start session - bad old iface"""
    fst_start_session(apdev, test_params, bad_param_old_iface, False)

def test_fst_sta_start_session_negative_llt(dev, apdev, test_params):
    """FST STA start session - negative llt"""
    fst_start_session(apdev, test_params, bad_param_negative_llt, False)

def test_fst_sta_start_session_zero_llt(dev, apdev, test_params):
    """FST STA start session - zero llt"""
    fst_start_session(apdev, test_params, bad_param_zero_llt, False)

def test_fst_sta_start_session_llt_too_big(dev, apdev, test_params):
    """FST STA start session - llt too large"""
    fst_start_session(apdev, test_params, bad_param_llt_too_big, False)

def test_fst_sta_start_session_invalid_peer_addr(dev, apdev, test_params):
    """FST STA start session - invalid peer address"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, False,
                      'GG:GG:GG:GG:GG:GG')

def test_fst_sta_start_session_multicast_peer_addr(dev, apdev, test_params):
    """FST STA start session - multicast peer address"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, False,
                      '11:00:11:22:33:44')

def test_fst_sta_start_session_broadcast_peer_addr(dev, apdev, test_params):
    """FST STA start session - broadcast peer addr"""
    fst_start_session(apdev, test_params, bad_param_peer_addr, False,
                      'FF:FF:FF:FF:FF:FF')

def test_fst_sta_initiate_session(dev, apdev, test_params):
    """FST STA initiate session"""
    fst_initiate_session(apdev, test_params, bad_param_none, False)

def test_fst_sta_initiate_session_no_params(dev, apdev, test_params):
    """FST STA initiate session - no params"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_no_params, False)

def test_fst_sta_initiate_session_invalid_session_id(dev, apdev, test_params):
    """FST STA initiate session - invalid session id"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_bad_session_id, False)

def test_fst_sta_initiate_session_no_new_iface(dev, apdev, test_params):
    """FST STA initiate session - no new iface"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_with_no_new_iface_set,
                         False)

def test_fst_sta_initiate_session_bad_peer_addr(dev, apdev, test_params):
    """FST STA initiate session - bad peer address"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_with_bad_peer_addr_set,
                         False)

def test_fst_sta_initiate_session_request_with_bad_stie(dev, apdev,
                                                        test_params):
    """FST STA initiate session - request with bad stie"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_request_with_bad_stie,
                         False)

def test_fst_sta_initiate_session_response_with_reject(dev, apdev, test_params):
    """FST STA initiate session - response with reject"""
    fst_initiate_session(apdev, test_params, bad_param_session_initiate_response_with_reject, False)

def test_fst_sta_initiate_session_response_with_bad_stie(dev, apdev, test_params):
    """FST STA initiate session - response with bad stie"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_response_with_bad_stie,
                         False)

def test_fst_sta_initiate_session_response_with_zero_llt(dev, apdev,
                                                         test_params):
    """FST STA initiate session - response with zero llt"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_response_with_zero_llt,
                         False)

def test_fst_sta_initiate_session_stt_no_response(dev, apdev, test_params):
    """FST STA initiate session - stt no response"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_stt_no_response, False)

def test_fst_sta_initiate_session_concurrent_setup_request(dev, apdev,
                                                           test_params):
    """FST STA initiate session - concurrent setup request"""
    fst_initiate_session(apdev, test_params,
                         bad_param_session_initiate_concurrent_setup_request,
                         False)

def test_fst_sta_session_request_with_no_session(dev, apdev, test_params):
    """FST STA session request with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_request,
                              False)

def test_fst_sta_session_response_accept_with_no_session(dev, apdev,
                                                         test_params):
    """FST STA session response accept with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_response,
                              False, "accept")

def test_fst_sta_session_response_reject_with_no_session(dev, apdev,
                                                         test_params):
    """FST STA session response reject with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_session_response,
                              False, "reject")

def test_fst_sta_ack_request_with_no_session(dev, apdev, test_params):
    """FST STA ack request with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_ack_request, False)

def test_fst_sta_ack_response_with_no_session(dev, apdev, test_params):
    """FST STA ack response with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_ack_response,
                              False)

def test_fst_sta_tear_down_response_with_no_session(dev, apdev, test_params):
    """FST STA tear down response with no session"""
    fst_send_unexpected_frame(apdev, test_params, frame_type_tear_down, False)

def test_fst_sta_transfer_session(dev, apdev, test_params):
    """FST STA transfer session"""
    fst_transfer_session(apdev, test_params, bad_param_none, False)

def test_fst_sta_transfer_session_no_params(dev, apdev, test_params):
    """FST STA transfer session - no params"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_no_params, False)

def test_fst_sta_transfer_session_bad_session_id(dev, apdev, test_params):
    """FST STA transfer session - bad session id"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_bad_session_id, False)

def test_fst_sta_transfer_session_setup_skipped(dev, apdev, test_params):
    """FST STA transfer session - setup skipped"""
    fst_transfer_session(apdev, test_params,
                         bad_param_session_transfer_setup_skipped, False)

def test_fst_sta_ack_request_with_session_not_set_up(dev, apdev, test_params):
    """FST STA ack request with session not set up"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_set_up, False)

def test_fst_sta_ack_request_with_session_not_established_init_side(dev, apdev, test_params):
    """FST STA ack request with session not established init side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_established_init_side,
                     False)

def test_fst_sta_ack_request_with_session_not_established_resp_side(dev, apdev, test_params):
    """FST STA ack request with session not established resp side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_req_session_not_established_resp_side,
                     False)

def test_fst_sta_ack_request_with_bad_fsts_id(dev, apdev, test_params):
    """FST STA ack request with bad fsts id"""
    fst_bad_transfer(apdev, test_params, bad_scenario_ack_req_bad_fsts_id,
                     False)

def test_fst_sta_ack_response_with_session_not_set_up(dev, apdev, test_params):
    """FST STA ack response with session not set up"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_set_up, False)

def test_fst_sta_ack_response_with_session_not_established_init_side(dev, apdev, test_params):
    """FST STA ack response with session not established init side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_established_init_side,
                     False)

def test_fst_sta_ack_response_with_session_not_established_resp_side(dev, apdev, test_params):
    """FST STA ack response with session not established resp side"""
    fst_bad_transfer(apdev, test_params,
                     bad_scenario_ack_resp_session_not_established_resp_side,
                     False)

def test_fst_sta_ack_response_with_no_ack_request(dev, apdev, test_params):
    """FST STA ack response with no ack request"""
    fst_bad_transfer(apdev, test_params, bad_scenario_ack_resp_no_ack_req,
                     False)

def test_fst_sta_tear_down_session(dev, apdev, test_params):
    """FST STA tear down session"""
    fst_tear_down_session(apdev, test_params, bad_param_none, False)

def test_fst_sta_tear_down_session_no_params(dev, apdev, test_params):
    """FST STA tear down session - no params"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_no_params, False)

def test_fst_sta_tear_down_session_bad_session_id(dev, apdev, test_params):
    """FST STA tear down session - bad session id"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_bad_session_id, False)

def test_fst_sta_tear_down_session_setup_skipped(dev, apdev, test_params):
    """FST STA tear down session - setup skipped"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_setup_skipped, False)

def test_fst_sta_tear_down_session_bad_fsts_id(dev, apdev, test_params):
    """FST STA tear down session - bad fsts id"""
    fst_tear_down_session(apdev, test_params,
                          bad_param_session_teardown_bad_fsts_id, False)

def test_fst_sta_remove_session_not_established(dev, apdev, test_params):
    """FST STA tear down session - not established"""
    fst_remove_session(apdev, test_params,
                       remove_scenario_non_established_session, False)

def test_fst_sta_remove_session_established(dev, apdev, test_params):
    """FST STA remove session - established"""
    fst_remove_session(apdev, test_params,
                       remove_scenario_established_session, False)

def test_fst_sta_remove_session_no_params(dev, apdev, test_params):
    """FST STA remove session - no params"""
    fst_remove_session(apdev, test_params, remove_scenario_no_params, False)

def test_fst_sta_remove_session_bad_session_id(dev, apdev, test_params):
    """FST STA remove session - bad session id"""
    fst_remove_session(apdev, test_params, remove_scenario_bad_session_id,
                       False)

def test_fst_rsn_ap_transfer_session(dev, apdev, test_params):
    """FST RSN AP transfer session"""
    fst_transfer_session(apdev, test_params, bad_param_none, True, rsn=True)

MGMT_SUBTYPE_ACTION = 13
ACTION_CATEG_FST = 18
FST_ACTION_SETUP_REQUEST = 0
FST_ACTION_SETUP_RESPONSE = 1
FST_ACTION_TEAR_DOWN = 2
FST_ACTION_ACK_REQUEST = 3
FST_ACTION_ACK_RESPONSE = 4
FST_ACTION_ON_CHANNEL_TUNNEL = 5

def hostapd_tx_and_status(hapd, msg):
    hapd.set("ext_mgmt_frame_handling", "1")
    hapd.mgmt_tx(msg)
    ev = hapd.wait_event([ "MGMT-TX-STATUS" ], timeout=1)
    if ev is None or "ok=1" not in ev:
        raise Exception("No ACK")
    hapd.set("ext_mgmt_frame_handling", "0")

def test_fst_proto(dev, apdev, test_params):
    """FST protocol testing"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        hapd = ap1.get_instance()
        sta = sta1.get_instance()
        dst = sta.own_addr()
        src = apdev[0]['bssid']

        msg = {}
        msg['fc'] = MGMT_SUBTYPE_ACTION << 4
        msg['da'] = dst
        msg['sa'] = src
        msg['bssid'] = src

        # unknown FST Action (255) received!
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST, 255)
        hostapd_tx_and_status(hapd, msg)

        # FST Request dropped: too short
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST)
        hostapd_tx_and_status(hapd, msg)

        # FST Request dropped: invalid STIE (EID)
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     163, 11, 0, 0, 0, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Request dropped: invalid STIE (Len)
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     164, 10, 0, 0, 0, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Request dropped: new and old band IDs are the same
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     164, 11, 0, 0, 0, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        ifaces = sta1.list_ifaces()
        id = int(ifaces[0]['name'].split('|')[1])
        # FST Request dropped: new iface not found (new_band_id mismatch)
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     164, 11, 0, 0, id + 1, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Action 'Setup Response' dropped: no session in progress found
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE)
        hostapd_tx_and_status(hapd, msg)

        # Create session
        initiator = ap1
        responder = sta1
        new_iface = ap2.ifname()
        new_peer_addr = ap2.get_actual_peer_addr()
        resp_newif = sta2.ifname()
        peeraddr = None
        initiator.add_peer(responder, peeraddr, new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        initiator.initiate_session(sid, "accept")

        # FST Response dropped due to wrong state: SETUP_COMPLETION
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE)
        hostapd_tx_and_status(hapd, msg)

        # Too short FST Tear Down dropped
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_TEAR_DOWN)
        hostapd_tx_and_status(hapd, msg)

        # tear down for wrong FST Setup ID (0)
        msg['payload'] = struct.pack("<BBL", ACTION_CATEG_FST,
                                     FST_ACTION_TEAR_DOWN, 0)
        hostapd_tx_and_status(hapd, msg)

        # Ack received on wrong interface
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_ACK_REQUEST)
        hostapd_tx_and_status(hapd, msg)

        # Ack Response in inappropriate session state (SETUP_COMPLETION)
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_ACK_RESPONSE)
        hostapd_tx_and_status(hapd, msg)

        # Unsupported FST Action frame (On channel tunnel)
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_ON_CHANNEL_TUNNEL)
        hostapd_tx_and_status(hapd, msg)

        # FST Request dropped: new iface not found (new_band_id match)
        # FST Request dropped due to MAC comparison
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     164, 11, 0, 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        hapd2 = ap2.get_instance()
        dst2 = sta2.get_instance().own_addr()
        src2 = apdev[1]['bssid']

        msg2 = {}
        msg2['fc'] = MGMT_SUBTYPE_ACTION << 4
        msg2['da'] = dst2
        msg2['sa'] = src2
        msg2['bssid'] = src2
        # FST Response dropped: wlan6 is not the old iface
        msg2['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                      FST_ACTION_SETUP_RESPONSE)
        hostapd_tx_and_status(hapd2, msg2)

        sta.dump_monitor()

        group = ap1.fst_group
        ap1.send_iface_detach_request(ap1.iface)

        sta.flush_scan_cache()
        sta.request("REASSOCIATE")
        sta.wait_connected()

        # FST Request dropped due to no interface connection
        msg['payload'] = struct.pack("<BBBLBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_REQUEST, 0, 0,
                                     164, 11, 0, 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        try:
            fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        except:
            pass

def test_fst_setup_response_proto(dev, apdev, test_params):
    """FST protocol testing for Setup Response"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        hapd = ap1.get_instance()
        sta = sta1.get_instance()
        dst = sta.own_addr()
        src = apdev[0]['bssid']

        sta1.add_peer(ap1, None, sta2.get_actual_peer_addr())
        sta1.set_fst_parameters(llt='0')
        sid = sta1.add_session()
        sta1.configure_session(sid, sta2.ifname())
        sta1.initiate_session(sid, "")

        msg = {}
        msg['fc'] = MGMT_SUBTYPE_ACTION << 4
        msg['da'] = dst
        msg['sa'] = src
        msg['bssid'] = src

        # Too short FST Response dropped
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE)
        hostapd_tx_and_status(hapd, msg)

        # FST Response dropped: invalid STIE (EID)
        dialog_token = 1
        status_code = 0
        id = 0
        msg['payload'] = struct.pack("<BBBBBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE, dialog_token,
                                     status_code,
                                     163, 11, 0, 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Response dropped: invalid STIE (Len)
        dialog_token = 1
        status_code = 0
        id = 0
        msg['payload'] = struct.pack("<BBBBBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE, dialog_token,
                                     status_code,
                                     164, 10, 0, 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Response dropped due to wrong dialog token
        dialog_token = 123
        status_code = 0
        id = 0
        msg['payload'] = struct.pack("<BBBBBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE, dialog_token,
                                     status_code,
                                     164, 11, 0, 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Response dropped due to wrong FST Session ID
        dialog_token = 1
        status_code = 0
        id = 1
        msg['payload'] = struct.pack("<BBBBBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE, dialog_token,
                                     status_code,
                                     164, 11, int(sid) + 123456,
                                     0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)

        # FST Response with non-zero status code
        dialog_token = 1
        status_code = 1
        id = 1
        msg['payload'] = struct.pack("<BBBBBBLBBBBBBB", ACTION_CATEG_FST,
                                     FST_ACTION_SETUP_RESPONSE, dialog_token,
                                     status_code,
                                     164, 11, int(sid), 0, id, 0, 0, 0, 0, 0)
        hostapd_tx_and_status(hapd, msg)
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_ack_response_proto(dev, apdev, test_params):
    """FST protocol testing for Ack Response"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        hapd = ap2.get_instance()
        sta = sta2.get_instance()
        dst = sta.own_addr()
        src = apdev[1]['bssid']

        sta1.add_peer(ap1, None, sta2.get_actual_peer_addr())
        sta1.set_fst_parameters(llt='0')
        sid = sta1.add_session()
        sta1.configure_session(sid, sta2.ifname())

        s = sta1.grequest("FST-MANAGER SESSION_INITIATE "+ sid)
        if not s.startswith('OK'):
            raise Exception("Cannot initiate fst session: %s" % s)
        ev = sta1.peer_obj.wait_gevent([ "FST-EVENT-SESSION" ], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION received")
        event = fst_module_aux.parse_fst_session_event(ev)
        if event == None:
            raise Exception("Unrecognized FST event: " % ev)
        if event['type'] != 'EVENT_FST_SETUP':
            raise Exception("Expected FST_SETUP event, got: " + event['type'])
        ev = sta1.peer_obj.wait_gevent(["FST-EVENT-SESSION"], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION received")
        event = fst_module_aux.parse_fst_session_event(ev)
        if event == None:
            raise Exception("Unrecognized FST event: " % ev)
        if event['type'] != 'EVENT_FST_SESSION_STATE':
            raise Exception("Expected EVENT_FST_SESSION_STATE event, got: " + event['type'])
        if event['new_state'] != "SETUP_COMPLETION":
            raise Exception("Expected new state SETUP_COMPLETION, got: " + event['new_state'])

        hapd.set("ext_mgmt_frame_handling", "1")
        s = sta1.peer_obj.grequest("FST-MANAGER SESSION_RESPOND "+ event['id'] + " accept")
        if not s.startswith('OK'):
            raise Exception("Error session_respond: %s" % s)
        req = hapd.mgmt_rx()
        if req is None:
            raise Exception("No Ack Request seen")
        msg = {}
        msg['fc'] = MGMT_SUBTYPE_ACTION << 4
        msg['da'] = dst
        msg['sa'] = src
        msg['bssid'] = src

        # Too short FST Ack Response dropped
        msg['payload'] = struct.pack("<BB", ACTION_CATEG_FST,
                                     FST_ACTION_ACK_RESPONSE)
        hapd.mgmt_tx(msg)
        ev = hapd.wait_event([ "MGMT-TX-STATUS" ], timeout=1)
        if ev is None or "ok=1" not in ev:
            raise Exception("No ACK")

        # Ack Response for wrong FSt Setup ID
        msg['payload'] = struct.pack("<BBBL", ACTION_CATEG_FST,
                                     FST_ACTION_ACK_RESPONSE,
                                     0, int(sid) + 123456)
        hostapd_tx_and_status(hapd, msg)
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_ap_config_oom(dev, apdev, test_params):
    """FST AP configuration and OOM"""
    ap1 = fst_module_aux.FstAP(apdev[0]['ifname'], 'fst_11a', 'a',
                               fst_test_common.fst_test_def_chan_a,
                               fst_test_common.fst_test_def_group,
                               fst_test_common.fst_test_def_prio_low)
    hapd = ap1.start(return_early=True)
    with alloc_fail(hapd, 1, "fst_group_create"):
        res = ap1.grequest("FST-ATTACH %s %s" % (ap1.iface, ap1.fst_group))
        if not res.startswith("FAIL"):
            raise Exception("FST-ATTACH succeeded unexpectedly")

    with alloc_fail(hapd, 1, "fst_iface_create"):
        res = ap1.grequest("FST-ATTACH %s %s" % (ap1.iface, ap1.fst_group))
        if not res.startswith("FAIL"):
            raise Exception("FST-ATTACH succeeded unexpectedly")

    with alloc_fail(hapd, 1, "fst_group_create_mb_ie"):
        res = ap1.grequest("FST-ATTACH %s %s" % (ap1.iface, ap1.fst_group))
        # This is allowed to complete currently

    ap1.stop()
    fst_test_common.fst_clear_regdom()

def test_fst_send_oom(dev, apdev, test_params):
    """FST send action OOM"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        hapd = ap1.get_instance()
        sta = sta1.get_instance()
        dst = sta.own_addr()
        src = apdev[0]['bssid']

        # Create session
        initiator = ap1
        responder = sta1
        new_iface = ap2.ifname()
        new_peer_addr = ap2.get_actual_peer_addr()
        resp_newif = sta2.ifname()
        peeraddr = None
        initiator.add_peer(responder, peeraddr, new_peer_addr)
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        with alloc_fail(hapd, 1, "fst_session_send_action"):
            res = initiator.grequest("FST-MANAGER SESSION_INITIATE " + sid)
            if not res.startswith("FAIL"):
                raise Exception("Unexpected SESSION_INITIATE result")

        res = initiator.grequest("FST-MANAGER SESSION_INITIATE " + sid)
        if not res.startswith("OK"):
            raise Exception("SESSION_INITIATE failed")

        tests = [ "", "foo", sid, sid + " foo", sid + " foo=bar" ]
        for t in tests:
            res = initiator.grequest("FST-MANAGER SESSION_SET " + t)
            if not res.startswith("FAIL"):
                raise Exception("Invalid SESSION_SET accepted")

        with alloc_fail(hapd, 1, "fst_session_send_action"):
            res = initiator.grequest("FST-MANAGER SESSION_TEARDOWN " + sid)
            if not res.startswith("FAIL"):
                raise Exception("Unexpected SESSION_TEARDOWN result")
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_session_oom(dev, apdev, test_params):
    """FST session create OOM"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        hapd = ap1.get_instance()
        sta = sta1.get_instance()
        dst = sta.own_addr()
        src = apdev[0]['bssid']

        # Create session
        initiator = ap1
        responder = sta1
        new_iface = ap2.ifname()
        new_peer_addr = ap2.get_actual_peer_addr()
        resp_newif = sta2.ifname()
        peeraddr = None
        initiator.add_peer(responder, peeraddr, new_peer_addr)
        with alloc_fail(hapd, 1, "fst_session_create"):
            sid = initiator.grequest("FST-MANAGER SESSION_ADD " + initiator.fst_group)
            if not sid.startswith("FAIL"):
                raise Exception("Unexpected SESSION_ADD success")
        sid = initiator.add_session()
        initiator.configure_session(sid, new_iface)
        with alloc_fail(sta, 1, "fst_session_create"):
            res = initiator.grequest("FST-MANAGER SESSION_INITIATE " + sid)
            if not res.startswith("OK"):
                raise Exception("Unexpected SESSION_INITIATE result")
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def test_fst_attach_zero_llt(dev, apdev):
    """FST attach with llt=0"""
    sta1 = fst_module_aux.FstSTA('wlan5', fst_test_common.fst_test_def_group,
                                 "100", "0")
    sta1.start()
    sta1.stop()

def test_fst_session_respond_fail(dev, apdev, test_params):
    """FST-MANAGER SESSION_RESPOND failure"""
    ap1, ap2, sta1, sta2 = fst_module_aux.start_two_ap_sta_pairs(apdev)
    try:
        fst_module_aux.connect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        sta1.add_peer(ap1, None, sta2.get_actual_peer_addr())
        sid = sta1.add_session()
        sta1.configure_session(sid, sta2.ifname())
        sta1.send_session_setup_request(sid)
        sta1.wait_for_session_event(5, [], ["EVENT_FST_SESSION_STATE"])
        ev = ap1.wait_for_session_event(5, [], ['EVENT_FST_SETUP'])
        if not 'id' in ev:
            raise Exception("No session id in FST setup event")
        # Disconnect STA to make SESSION_RESPOND fail due to no peer found
        sta = sta1.get_instance()
        sta.request("DISCONNECT")
        sta.wait_disconnected()
        req = "FST-MANAGER SESSION_RESPOND %s reject" % ev['id']
        s = ap1.grequest(req)
        if not s.startswith("FAIL"):
            raise Exception("SESSION_RESPOND succeeded unexpectedly")
    finally:
        fst_module_aux.disconnect_two_ap_sta_pairs(ap1, ap2, sta1, sta2)
        fst_module_aux.stop_two_ap_sta_pairs(ap1, ap2, sta1, sta2)

def fst_session_set(dev, sid, param, value):
    cmd = "FST-MANAGER SESSION_SET %s %s=%s" % (sid, param, value)
    if "OK" not in dev.global_request(cmd):
        raise Exception(cmd + " failed")

def fst_session_set_ap(dev, sid, param, value):
    cmd = "FST-MANAGER SESSION_SET %s %s=%s" % (sid, param, value)
    if "OK" not in dev.request(cmd):
        raise Exception(cmd + " failed")

def fst_attach_ap(dev, ifname, group):
    cmd = "FST-ATTACH %s %s" % (ifname, group)
    if "OK" not in dev.request(cmd):
        raise Exception("FST-ATTACH (AP) failed")
    ev = dev.wait_event(['FST-EVENT-IFACE'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-IFACE attached (AP)")
    for t in [ "attached", "ifname=" + ifname, "group=" + group ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-IFACE data (AP): " + ev)

def fst_attach_sta(dev, ifname, group):
    if "OK" not in dev.global_request("FST-ATTACH %s %s" % (ifname, group)):
        raise Exception("FST-ATTACH (STA) failed")
    ev = dev.wait_global_event(['FST-EVENT-IFACE'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-IFACE attached (STA)")
    for t in [ "attached", "ifname=" + ifname, "group=" + group ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-IFACE data (STA): " + ev)

def fst_detach_ap(dev, ifname, group):
    if "OK" not in dev.request("FST-DETACH " + ifname):
        raise Exception("FST-DETACH (AP) failed for " + ifname)
    ev = dev.wait_event(['FST-EVENT-IFACE'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-IFACE detached (AP) for " + ifname)
    for t in [ "detached", "ifname=" + ifname, "group=" + group ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-IFACE data (AP): " + ev)

def fst_detach_sta(dev, ifname, group):
    dev.dump_monitor()
    if "OK" not in dev.global_request("FST-DETACH " + ifname):
        raise Exception("FST-DETACH (STA) failed for " + ifname)
    ev = dev.wait_global_event(['FST-EVENT-IFACE'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-IFACE detached (STA) for " + ifname)
    for t in [ "detached", "ifname=" + ifname, "group=" + group ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-IFACE data (STA): " + ev)

def fst_wait_event_peer_ap(dev, event, ifname, addr):
    ev = dev.wait_event(['FST-EVENT-PEER'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-PEER connected (AP)")
    for t in [ " " + event + " ", "ifname=" + ifname, "peer_addr=" + addr ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-PEER data (AP): " + ev)

def fst_wait_event_peer_sta(dev, event, ifname, addr):
    ev = dev.wait_global_event(['FST-EVENT-PEER'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-PEER connected (STA)")
    for t in [ " " + event + " ", "ifname=" + ifname, "peer_addr=" + addr ]:
        if t not in ev:
            raise Exception("Unexpected FST-EVENT-PEER data (STA): " + ev)

def fst_setup_req(dev, hglobal, freq, dst, req, stie, mbie="", no_wait=False):
    act = req + stie + mbie
    dev.request("MGMT_TX %s %s freq=%d action=%s" % (dst, dst, freq, act))
    ev = dev.wait_event(['MGMT-TX-STATUS'], timeout=5)
    if ev is None or "result=SUCCESS" not in ev:
        raise Exception("FST Action frame not ACKed")

    if no_wait:
        return
    while True:
        ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (AP)")
        if "new_state=SETUP_COMPLETION" in ev:
            break

def fst_start_and_connect(apdev, group, sgroup):
    hglobal = hostapd.HostapdGlobal()
    if "OK" not in hglobal.request("FST-MANAGER TEST_REQUEST IS_SUPPORTED"):
        raise HwsimSkip("No FST testing support")

    params = { "ssid": "fst_11a", "hw_mode": "a", "channel": "36",
               "country_code": "US" }
    hapd = hostapd.add_ap(apdev[0], params)

    fst_attach_ap(hglobal, apdev[0]['ifname'], group)

    cmd = "FST-ATTACH %s %s" % (apdev[0]['ifname'], group)
    if "FAIL" not in hglobal.request(cmd):
        raise Exception("Duplicated FST-ATTACH (AP) accepted")

    params = { "ssid": "fst_11g", "hw_mode": "g", "channel": "1",
               "country_code": "US" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    fst_attach_ap(hglobal, apdev[1]['ifname'], group)

    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
    wpas.interface_add("wlan5")
    fst_attach_sta(wpas, wpas.ifname, sgroup)

    wpas.interface_add("wlan6", set_ifname=False)
    wpas2 = WpaSupplicant(ifname="wlan6")
    fst_attach_sta(wpas, wpas2.ifname, sgroup)

    wpas.connect("fst_11a", key_mgmt="NONE", scan_freq="5180",
                 wait_connect=False)
    wpas.wait_connected()

    fst_wait_event_peer_sta(wpas, "connected", wpas.ifname, apdev[0]['bssid'])
    fst_wait_event_peer_ap(hglobal, "connected", apdev[0]['ifname'],
                           wpas.own_addr())

    wpas2.connect("fst_11g", key_mgmt="NONE", scan_freq="2412",
                  wait_connect=False)
    wpas2.wait_connected()

    fst_wait_event_peer_sta(wpas, "connected", wpas2.ifname, apdev[1]['bssid'])
    fst_wait_event_peer_ap(hglobal, "connected", apdev[1]['ifname'],
                           wpas2.own_addr())
    return hglobal, wpas, wpas2, hapd, hapd2

def test_fst_test_setup(dev, apdev, test_params):
    """FST setup using separate commands"""
    try:
        _test_fst_test_setup(dev, apdev, test_params)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()
        dev[1].flush_scan_cache()

def _test_fst_test_setup(dev, apdev, test_params):
    group = "fstg0b"
    sgroup = "fstg1b"
    hglobal, wpas, wpas2, hapd, hapd2 = fst_start_and_connect(apdev, group, sgroup)

    sid = wpas.global_request("FST-MANAGER SESSION_ADD " + sgroup).strip()
    if "FAIL" in sid:
        raise Exception("FST-MANAGER SESSION_ADD (STA) failed")

    fst_session_set(wpas, sid, "old_ifname", wpas.ifname)
    fst_session_set(wpas, sid, "old_peer_addr", apdev[0]['bssid'])
    fst_session_set(wpas, sid, "new_ifname", wpas2.ifname)
    fst_session_set(wpas, sid, "new_peer_addr", apdev[1]['bssid'])

    if "OK" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("FST-MANAGER SESSION_INITIATE failed")

    while True:
        ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (AP)")
        if "new_state=SETUP_COMPLETION" in ev:
            f = re.search("session_id=(\d+)", ev)
            if f is None:
                raise Exception("No session_id in FST-EVENT-SESSION")
            sid_ap = f.group(1)
            cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
            if "OK" not in hglobal.request(cmd):
                raise Exception("FST-MANAGER SESSION_RESPOND failed on AP")
            break

    ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-SESSION")
    if "new_state=SETUP_COMPLETION" not in ev:
        raise Exception("Unexpected FST-EVENT-SESSION data: " + ev)

    ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-SESSION")
    if "event_type=EVENT_FST_ESTABLISHED" not in ev:
        raise Exception("Unexpected FST-EVENT-SESSION data: " + ev)

    cmd = "FST-MANAGER SESSION_REMOVE " + sid
    if "OK" not in wpas.global_request(cmd):
        raise Exception("FST-MANAGER SESSION_REMOVE failed")
    ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-SESSION")
    if "new_state=INITIAL" not in ev:
        raise Exception("Unexpected FST-EVENT-SESSION data (STA): " + ev)

    ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
    if ev is None:
        raise Exception("No FST-EVENT-SESSION (AP)")
    if "new_state=INITIAL" not in ev:
        raise Exception("Unexpected FST-EVENT-SESSION data (AP): " + ev)

    if "FAIL" not in wpas.global_request(cmd):
        raise Exception("Duplicated FST-MANAGER SESSION_REMOVE accepted")

    hglobal.request("FST-MANAGER SESSION_REMOVE " + sid_ap)

    wpas.request("DISCONNECT")
    wpas.wait_disconnected()
    fst_wait_event_peer_sta(wpas, "disconnected", wpas.ifname,
                            apdev[0]['bssid'])
    fst_wait_event_peer_ap(hglobal, "disconnected", apdev[0]['ifname'],
                           wpas.own_addr())

    wpas2.request("DISCONNECT")
    wpas2.wait_disconnected()
    fst_wait_event_peer_sta(wpas, "disconnected", wpas2.ifname,
                            apdev[1]['bssid'])
    fst_wait_event_peer_ap(hglobal, "disconnected", apdev[1]['ifname'],
                           wpas2.own_addr())

    fst_detach_ap(hglobal, apdev[0]['ifname'], group)
    if "FAIL" not in hglobal.request("FST-DETACH " + apdev[0]['ifname']):
        raise Exception("Duplicated FST-DETACH (AP) accepted")
    hapd.disable()

    fst_detach_ap(hglobal, apdev[1]['ifname'], group)
    hapd2.disable()

    fst_detach_sta(wpas, wpas.ifname, sgroup)
    fst_detach_sta(wpas, wpas2.ifname, sgroup)

def test_fst_setup_mbie_diff(dev, apdev, test_params):
    """FST setup and different MBIE in FST Setup Request"""
    try:
        _test_fst_setup_mbie_diff(dev, apdev, test_params)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()
        dev[1].flush_scan_cache()

def _test_fst_setup_mbie_diff(dev, apdev, test_params):
    group = "fstg0c"
    sgroup = "fstg1c"
    hglobal, wpas, wpas2, hapd, hapd2 = fst_start_and_connect(apdev, group, sgroup)

    # FST Setup Request: Category, FST Action, Dialog Token (non-zero),
    # LLT (32 bits, see 10.32), Session Transition (see 8.4.2.147),
    # Multi-band element (optional, see 8.4.2.140)

    # Session Transition: EID, Len, FSTS ID(4), Session Control,
    # New Band (Band ID, Setup, Operation), Old Band (Band ID, Setup, Operation)

    # Multi-band element: EID, Len, Multi-band Control, Band ID,
    # Operating Class, Channel Number, BSSID (6), Beacon Interval (2),
    # TSF Offset (8), Multi-band Connection Capability, FSTSessionTimeOut,
    # STA MAC Address (6, optional), Pairwise Cipher Suite Count (2, optional),
    # Pairwise Cipher Suite List (4xm, optional)

    # MBIE with the non-matching STA MAC Address:
    req = "1200011a060000"
    stie = "a40b0100000000020001040001"
    mbie = "9e1c0c0200010200000004000000000000000000000000ff0200000006ff"
    fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie, mbie)

    # MBIE without the STA MAC Address:
    req = "1200011a060000"
    stie = "a40b0100000000020001040001"
    mbie = "9e16040200010200000004000000000000000000000000ff"
    fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie, mbie)

    # MBIE with unsupported STA Role:
    req = "1200011a060000"
    stie = "a40b0100000000020001040001"
    mbie = "9e16070200010200000004000000000000000000000000ff"
    fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie, mbie)

    # MBIE with unsupported Band ID:
    req = "1200011a060000"
    stie = "a40b0100000000020001040001"
    mbie = "9e1604ff00010200000004000000000000000000000000ff"
    fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie, mbie)

    # FST Setup Request without MBIE (different FSTS ID):
    req = "1200011a060000"
    stie = "a40b0200000000020001040001"
    fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie)

    # MBIE update OOM on AP
    req = "1200011a060000"
    stie = "a40b0100000000020001040001"
    mbie = "9e16040200010200000004000000000000000000000000ff"
    try:
        with alloc_fail(hapd, 1, "mb_ies_by_info"):
            fst_setup_req(wpas, hglobal, 5180, apdev[0]['bssid'], req, stie,
                          mbie, no_wait=True)
    except HwsimSkip, e:
        # Skip exception to allow proper cleanup
        pass

    # Remove sessions to avoid causing issues to following test ases
    s = hglobal.request("FST-MANAGER LIST_SESSIONS " + group)
    if not s.startswith("FAIL"):
        for sid in s.split(' '):
            if len(sid):
                hglobal.request("FST-MANAGER SESSION_REMOVE " + sid)

def test_fst_many_setup(dev, apdev, test_params):
    """FST setup multiple times"""
    try:
        _test_fst_many_setup(dev, apdev, test_params)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()
        dev[1].flush_scan_cache()

def _test_fst_many_setup(dev, apdev, test_params):
    group = "fstg0d"
    sgroup = "fstg1d"
    hglobal, wpas, wpas2, hapd, hapd2 = fst_start_and_connect(apdev, group, sgroup)

    sid = wpas.global_request("FST-MANAGER SESSION_ADD " + sgroup).strip()
    if "FAIL" in sid:
        raise Exception("FST-MANAGER SESSION_ADD (STA) failed")

    fst_session_set(wpas, sid, "old_ifname", wpas.ifname)
    fst_session_set(wpas, sid, "old_peer_addr", apdev[0]['bssid'])
    fst_session_set(wpas, sid, "new_ifname", wpas2.ifname)
    fst_session_set(wpas, sid, "new_peer_addr", apdev[1]['bssid'])

    for i in range(257):
        if "OK" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
            raise Exception("FST-MANAGER SESSION_INITIATE failed")

        while True:
            ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
            if ev is None:
                raise Exception("No FST-EVENT-SESSION (AP)")
            if "new_state=SETUP_COMPLETION" in ev:
                f = re.search("session_id=(\d+)", ev)
                if f is None:
                    raise Exception("No session_id in FST-EVENT-SESSION")
                sid_ap = f.group(1)
                cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
                if "OK" not in hglobal.request(cmd):
                    raise Exception("FST-MANAGER SESSION_RESPOND failed on AP")
                break

        ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (STA)")
        if "new_state=SETUP_COMPLETION" not in ev:
            raise Exception("Unexpected FST-EVENT-SESSION data: " + ev)

        ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (STA)")
        if "event_type=EVENT_FST_ESTABLISHED" not in ev:
            raise Exception("Unexpected FST-EVENT-SESSION data: " + ev)

        if "OK" not in wpas.global_request("FST-MANAGER SESSION_TEARDOWN " + sid):
            raise Exception("FST-MANAGER SESSION_INITIATE failed")

        if i == 0:
            if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_TEARDOWN " + sid):
                raise Exception("Duplicate FST-MANAGER SESSION_TEARDOWN accepted")

        ev = wpas.wait_global_event(["FST-EVENT-SESSION"], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (STA teardown -->initial)")
        if "new_state=INITIAL" not in ev:
            raise Exception("Unexpected FST-EVENT-SESSION data (STA): " + ev)

        ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (AP teardown -->initial)")
        if "new_state=INITIAL" not in ev:
            raise Exception("Unexpected FST-EVENT-SESSION data (AP): " + ev)

        if "OK" not in hglobal.request("FST-MANAGER SESSION_REMOVE " + sid_ap):
            raise Exception("FST-MANAGER SESSION_REMOVE (AP) failed")

    if "OK" not in wpas.global_request("FST-MANAGER SESSION_REMOVE " + sid):
        raise Exception("FST-MANAGER SESSION_REMOVE failed")

    wpas.request("DISCONNECT")
    wpas.wait_disconnected()
    fst_wait_event_peer_sta(wpas, "disconnected", wpas.ifname,
                            apdev[0]['bssid'])
    fst_wait_event_peer_ap(hglobal, "disconnected", apdev[0]['ifname'],
                           wpas.own_addr())

    wpas2.request("DISCONNECT")
    wpas2.wait_disconnected()
    fst_wait_event_peer_sta(wpas, "disconnected", wpas2.ifname,
                            apdev[1]['bssid'])
    fst_wait_event_peer_ap(hglobal, "disconnected", apdev[1]['ifname'],
                           wpas2.own_addr())

    fst_detach_ap(hglobal, apdev[0]['ifname'], group)
    fst_detach_ap(hglobal, apdev[1]['ifname'], group)
    hapd.disable()
    hapd2.disable()

    fst_detach_sta(wpas, wpas.ifname, sgroup)
    fst_detach_sta(wpas, wpas2.ifname, sgroup)

def test_fst_attach_wpas_error(dev, apdev, test_params):
    """FST attach errors in wpa_supplicant"""
    if "OK" not in dev[0].global_request("FST-MANAGER TEST_REQUEST IS_SUPPORTED"):
        raise HwsimSkip("No FST testing support")
    group = "fstg0"
    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
    wpas.interface_add("wlan5")
    fst_attach_sta(wpas, wpas.ifname, group)
    if "FAIL" not in wpas.global_request("FST-ATTACH %s %s" % (wpas.ifname,
                                                               group)):
        raise Exception("Duplicated FST-ATTACH accepted")
    if "FAIL" not in wpas.global_request("FST-ATTACH %s %s" % ("foofoo",
                                                               group)):
        raise Exception("FST-ATTACH for unknown interface accepted")

def test_fst_session_initiate_errors(dev, apdev, test_params):
    """FST SESSION_INITIATE error cases"""
    try:
        _test_fst_session_initiate_errors(dev, apdev, test_params)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()
        dev[1].flush_scan_cache()

def _test_fst_session_initiate_errors(dev, apdev, test_params):
    group = "fstg0"
    sgroup = "fstg1"
    hglobal, wpas, wpas2, hapd, hapd2 = fst_start_and_connect(apdev, group, sgroup)

    sid = wpas.global_request("FST-MANAGER SESSION_ADD " + sgroup).strip()
    if "FAIL" in sid:
        raise Exception("FST-MANAGER SESSION_ADD (STA) failed")

    # No old peer MAC address
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "old_peer_addr", "00:ff:ff:ff:ff:ff")
    # No new peer MAC address
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "new_peer_addr", "00:ff:ff:ff:ff:fe")
    # No old interface defined
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "old_ifname", wpas.ifname)
    # No new interface defined
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "new_ifname", wpas.ifname)
    # Same interface set as old and new
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "new_ifname", wpas2.ifname)
    # The preset old peer address is not connected
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "old_peer_addr", apdev[0]['bssid'])
    # The preset new peer address is not connected
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Invalid FST-MANAGER SESSION_INITIATE accepted")

    fst_session_set(wpas, sid, "new_peer_addr", apdev[1]['bssid'])
    # Initiate session setup
    if "OK" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("FST-MANAGER SESSION_INITIATE failed")

    # Session in progress
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("Duplicated FST-MANAGER SESSION_INITIATE accepted")

    sid2 = wpas.global_request("FST-MANAGER SESSION_ADD " + sgroup).strip()
    if "FAIL" in sid:
        raise Exception("FST-MANAGER SESSION_ADD (STA) failed")
    fst_session_set(wpas, sid2, "old_ifname", wpas.ifname)
    fst_session_set(wpas, sid2, "old_peer_addr", apdev[0]['bssid'])
    fst_session_set(wpas, sid2, "new_ifname", wpas2.ifname)
    fst_session_set(wpas, sid2, "new_peer_addr", apdev[1]['bssid'])

    # There is another session in progress (old)
    if "FAIL" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid2):
        raise Exception("Duplicated FST-MANAGER SESSION_INITIATE accepted")

    if "OK" not in wpas.global_request("FST-MANAGER SESSION_REMOVE " + sid):
        raise Exception("FST-MANAGER SESSION_REMOVE failed")

    while True:
        ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (AP)")
        if "new_state=SETUP_COMPLETION" in ev:
            f = re.search("session_id=(\d+)", ev)
            if f is None:
                raise Exception("No session_id in FST-EVENT-SESSION")
            sid_ap = f.group(1)
            break
    if "OK" not in hglobal.request("FST-MANAGER SESSION_REMOVE " + sid_ap):
        raise Exception("FST-MANAGER SESSION_REMOVE (AP) failed")

    if "OK" not in wpas.global_request("FST-MANAGER SESSION_REMOVE " + sid2):
        raise Exception("FST-MANAGER SESSION_REMOVE failed")

def test_fst_session_respond_errors(dev, apdev, test_params):
    """FST SESSION_RESPOND error cases"""
    try:
        _test_fst_session_respond_errors(dev, apdev, test_params)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])
        dev[0].flush_scan_cache()
        dev[1].flush_scan_cache()

def _test_fst_session_respond_errors(dev, apdev, test_params):
    group = "fstg0b"
    sgroup = "fstg1b"
    hglobal, wpas, wpas2, hapd, hapd2 = fst_start_and_connect(apdev, group, sgroup)

    sid = wpas.global_request("FST-MANAGER SESSION_ADD " + sgroup).strip()
    if "FAIL" in sid:
        raise Exception("FST-MANAGER SESSION_ADD (STA) failed")

    fst_session_set(wpas, sid, "old_ifname", wpas.ifname)
    fst_session_set(wpas, sid, "old_peer_addr", apdev[0]['bssid'])
    fst_session_set(wpas, sid, "new_ifname", wpas2.ifname)
    fst_session_set(wpas, sid, "new_peer_addr", apdev[1]['bssid'])

    if "OK" not in wpas.global_request("FST-MANAGER SESSION_INITIATE " + sid):
        raise Exception("FST-MANAGER SESSION_INITIATE failed")

    while True:
        ev = hglobal.wait_event(['FST-EVENT-SESSION'], timeout=5)
        if ev is None:
            raise Exception("No FST-EVENT-SESSION (AP)")
        if "new_state=SETUP_COMPLETION" in ev:
            f = re.search("session_id=(\d+)", ev)
            if f is None:
                raise Exception("No session_id in FST-EVENT-SESSION")
            sid_ap = f.group(1)
            break

    # The preset peer address is not in the peer list
    fst_session_set_ap(hglobal, sid_ap, "old_peer_addr", "00:00:00:00:00:01")
    cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
    if "FAIL" not in hglobal.request(cmd):
        raise Exception("Invalid FST-MANAGER SESSION_RESPOND accepted")

    # Same interface set as old and new
    fst_session_set_ap(hglobal, sid_ap, "old_peer_addr", wpas.own_addr())
    fst_session_set_ap(hglobal, sid_ap, "old_ifname", apdev[1]['ifname'])
    cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
    if "FAIL" not in hglobal.request(cmd):
        raise Exception("Invalid FST-MANAGER SESSION_RESPOND accepted")

    # valid command
    fst_session_set_ap(hglobal, sid_ap, "old_ifname", apdev[0]['ifname'])
    cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
    if "OK" not in hglobal.request(cmd):
        raise Exception("FST-MANAGER SESSION_RESPOND failed")

    # incorrect state
    cmd = "FST-MANAGER SESSION_RESPOND %s accept" % sid_ap
    if "FAIL" not in hglobal.request(cmd):
        raise Exception("Invalid FST-MANAGER SESSION_RESPOND accepted")

    cmd = "FST-MANAGER SESSION_REMOVE " + sid
    if "OK" not in wpas.global_request(cmd):
        raise Exception("FST-MANAGER SESSION_REMOVE (STA) failed")

    cmd = "FST-MANAGER SESSION_REMOVE %s" % sid_ap
    if "OK" not in hglobal.request(cmd):
        raise Exception("FST-MANAGER SESSION_REMOVE (AP) failed")