tests: Add wait_connected() and wait_disconnected() helpers
[mech_eap.git] / tests / hwsim / test_ap_ciphers.py
1 # Cipher suite tests
2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import time
8 import logging
9 logger = logging.getLogger()
10 import os.path
11
12 import hwsim_utils
13 import hostapd
14
15 def check_cipher(dev, ap, cipher):
16     if cipher not in dev.get_capability("pairwise"):
17         return "skip"
18     params = { "ssid": "test-wpa2-psk",
19                "wpa_passphrase": "12345678",
20                "wpa": "2",
21                "wpa_key_mgmt": "WPA-PSK",
22                "rsn_pairwise": cipher }
23     hapd = hostapd.add_ap(ap['ifname'], params)
24     dev.connect("test-wpa2-psk", psk="12345678",
25                 pairwise=cipher, group=cipher, scan_freq="2412")
26     hwsim_utils.test_connectivity(dev, hapd)
27
28 def test_ap_cipher_tkip(dev, apdev):
29     """WPA2-PSK/TKIP connection"""
30     return check_cipher(dev[0], apdev[0], "TKIP")
31
32 def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
33     """WPA-PSK/TKIP countermeasures (detected by AP)"""
34     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
35     if not os.path.exists(testfile):
36         return "skip"
37
38     params = { "ssid": "tkip-countermeasures",
39                "wpa_passphrase": "12345678",
40                "wpa": "1",
41                "wpa_key_mgmt": "WPA-PSK",
42                "wpa_pairwise": "TKIP" }
43     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
44
45     dev[0].connect("tkip-countermeasures", psk="12345678",
46                    pairwise="TKIP", group="TKIP", scan_freq="2412")
47
48     dev[0].dump_monitor()
49     with open(testfile, "w") as f:
50         f.write(apdev[0]['bssid'])
51     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
52     if ev is not None:
53         raise Exception("Unexpected disconnection on first Michael MIC failure")
54
55     with open(testfile, "w") as f:
56         f.write("ff:ff:ff:ff:ff:ff")
57     ev = dev[0].wait_disconnected(timeout=10,
58                                   error="No disconnection after two Michael MIC failures")
59     if "reason=14" not in ev:
60         raise Exception("Unexpected disconnection reason: " + ev)
61     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
62     if ev is not None:
63         raise Exception("Unexpected connection during TKIP countermeasures")
64
65 def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
66     """WPA-PSK/TKIP countermeasures (detected by STA)"""
67     params = { "ssid": "tkip-countermeasures",
68                "wpa_passphrase": "12345678",
69                "wpa": "1",
70                "wpa_key_mgmt": "WPA-PSK",
71                "wpa_pairwise": "TKIP" }
72     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
73
74     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
75     if not os.path.exists(testfile):
76         return "skip"
77
78     dev[0].connect("tkip-countermeasures", psk="12345678",
79                    pairwise="TKIP", group="TKIP", scan_freq="2412")
80
81     dev[0].dump_monitor()
82     with open(testfile, "w") as f:
83         f.write(dev[0].p2p_dev_addr())
84     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
85     if ev is not None:
86         raise Exception("Unexpected disconnection on first Michael MIC failure")
87
88     with open(testfile, "w") as f:
89         f.write("ff:ff:ff:ff:ff:ff")
90     ev = dev[0].wait_disconnected(timeout=10,
91                                   error="No disconnection after two Michael MIC failures")
92     if "reason=14 locally_generated=1" not in ev:
93         raise Exception("Unexpected disconnection reason: " + ev)
94     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
95     if ev is not None:
96         raise Exception("Unexpected connection during TKIP countermeasures")
97
98 def test_ap_cipher_ccmp(dev, apdev):
99     """WPA2-PSK/CCMP connection"""
100     return check_cipher(dev[0], apdev[0], "CCMP")
101
102 def test_ap_cipher_gcmp(dev, apdev):
103     """WPA2-PSK/GCMP connection"""
104     return check_cipher(dev[0], apdev[0], "GCMP")
105
106 def test_ap_cipher_ccmp_256(dev, apdev):
107     """WPA2-PSK/CCMP-256 connection"""
108     return check_cipher(dev[0], apdev[0], "CCMP-256")
109
110 def test_ap_cipher_gcmp_256(dev, apdev):
111     """WPA2-PSK/GCMP-256 connection"""
112     return check_cipher(dev[0], apdev[0], "GCMP-256")
113
114 def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
115     """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
116     ssid = "test-wpa-wpa2-psk"
117     passphrase = "12345678"
118     params = { "ssid": ssid,
119                "wpa_passphrase": passphrase,
120                "wpa": "3",
121                "wpa_key_mgmt": "WPA-PSK",
122                "rsn_pairwise": "CCMP",
123                "wpa_pairwise": "TKIP" }
124     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
125     dev[0].connect(ssid, psk=passphrase, proto="WPA2",
126                    pairwise="CCMP", group="TKIP", scan_freq="2412")
127     status = dev[0].get_status()
128     if status['key_mgmt'] != 'WPA2-PSK':
129         raise Exception("Incorrect key_mgmt reported")
130     if status['pairwise_cipher'] != 'CCMP':
131         raise Exception("Incorrect pairwise_cipher reported")
132     if status['group_cipher'] != 'TKIP':
133         raise Exception("Incorrect group_cipher reported")
134     bss = dev[0].get_bss(apdev[0]['bssid'])
135     if bss['ssid'] != ssid:
136         raise Exception("Unexpected SSID in the BSS entry")
137     if "[WPA-PSK-TKIP]" not in bss['flags']:
138         raise Exception("Missing BSS flag WPA-PSK-TKIP")
139     if "[WPA2-PSK-CCMP]" not in bss['flags']:
140         raise Exception("Missing BSS flag WPA2-PSK-CCMP")
141     hwsim_utils.test_connectivity(dev[0], hapd)
142
143     dev[1].connect(ssid, psk=passphrase, proto="WPA",
144                    pairwise="TKIP", group="TKIP", scan_freq="2412")
145     status = dev[1].get_status()
146     if status['key_mgmt'] != 'WPA-PSK':
147         raise Exception("Incorrect key_mgmt reported")
148     if status['pairwise_cipher'] != 'TKIP':
149         raise Exception("Incorrect pairwise_cipher reported")
150     if status['group_cipher'] != 'TKIP':
151         raise Exception("Incorrect group_cipher reported")
152     hwsim_utils.test_connectivity(dev[1], hapd)
153     hwsim_utils.test_connectivity(dev[0], dev[1])