tests: Pass wpas/hapd instance to test_connectivity()
[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 subprocess
9 import logging
10 logger = logging.getLogger()
11 import os.path
12
13 import hwsim_utils
14 import hostapd
15
16 def check_cipher(dev, ap, cipher):
17     if cipher not in dev.get_capability("pairwise"):
18         return "skip"
19     params = { "ssid": "test-wpa2-psk",
20                "wpa_passphrase": "12345678",
21                "wpa": "2",
22                "wpa_key_mgmt": "WPA-PSK",
23                "rsn_pairwise": cipher }
24     hapd = hostapd.add_ap(ap['ifname'], params)
25     dev.connect("test-wpa2-psk", psk="12345678",
26                 pairwise=cipher, group=cipher, scan_freq="2412")
27     hwsim_utils.test_connectivity(dev, hapd)
28
29 def test_ap_cipher_tkip(dev, apdev):
30     """WPA2-PSK/TKIP connection"""
31     return check_cipher(dev[0], apdev[0], "TKIP")
32
33 def test_ap_cipher_tkip_countermeasures_ap(dev, apdev):
34     """WPA-PSK/TKIP countermeasures (detected by AP)"""
35     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (dev[0].get_driver_status_field("phyname"), dev[0].ifname)
36     if not os.path.exists(testfile):
37         return "skip"
38
39     params = { "ssid": "tkip-countermeasures",
40                "wpa_passphrase": "12345678",
41                "wpa": "1",
42                "wpa_key_mgmt": "WPA-PSK",
43                "wpa_pairwise": "TKIP" }
44     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
45
46     dev[0].connect("tkip-countermeasures", psk="12345678",
47                    pairwise="TKIP", group="TKIP", scan_freq="2412")
48
49     dev[0].dump_monitor()
50     cmd = subprocess.Popen(["sudo", "tee", testfile],
51                            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
52     cmd.stdin.write(apdev[0]['bssid'])
53     cmd.stdin.close()
54     cmd.stdout.read()
55     cmd.stdout.close()
56     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
57     if ev is not None:
58         raise Exception("Unexpected disconnection on first Michael MIC failure")
59
60     cmd = subprocess.Popen(["sudo", "tee", testfile],
61                            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
62     cmd.stdin.write("ff:ff:ff:ff:ff:ff")
63     cmd.stdin.close()
64     cmd.stdout.read()
65     cmd.stdout.close()
66     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
67     if ev is None:
68         raise Exception("No disconnection after two Michael MIC failures")
69     if "reason=14" not in ev:
70         raise Exception("Unexpected disconnection reason: " + ev)
71     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
72     if ev is not None:
73         raise Exception("Unexpected connection during TKIP countermeasures")
74
75 def test_ap_cipher_tkip_countermeasures_sta(dev, apdev):
76     """WPA-PSK/TKIP countermeasures (detected by STA)"""
77     params = { "ssid": "tkip-countermeasures",
78                "wpa_passphrase": "12345678",
79                "wpa": "1",
80                "wpa_key_mgmt": "WPA-PSK",
81                "wpa_pairwise": "TKIP" }
82     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
83
84     testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
85     if not os.path.exists(testfile):
86         return "skip"
87
88     dev[0].connect("tkip-countermeasures", psk="12345678",
89                    pairwise="TKIP", group="TKIP", scan_freq="2412")
90
91     dev[0].dump_monitor()
92     cmd = subprocess.Popen(["sudo", "tee", testfile],
93                            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
94     cmd.stdin.write(dev[0].p2p_dev_addr())
95     cmd.stdin.close()
96     cmd.stdout.read()
97     cmd.stdout.close()
98     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
99     if ev is not None:
100         raise Exception("Unexpected disconnection on first Michael MIC failure")
101
102     cmd = subprocess.Popen(["sudo", "tee", testfile],
103                            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
104     cmd.stdin.write("ff:ff:ff:ff:ff:ff")
105     cmd.stdin.close()
106     cmd.stdout.read()
107     cmd.stdout.close()
108     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
109     if ev is None:
110         raise Exception("No disconnection after two Michael MIC failures")
111     if "reason=14 locally_generated=1" not in ev:
112         raise Exception("Unexpected disconnection reason: " + ev)
113     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
114     if ev is not None:
115         raise Exception("Unexpected connection during TKIP countermeasures")
116
117 def test_ap_cipher_ccmp(dev, apdev):
118     """WPA2-PSK/CCMP connection"""
119     return check_cipher(dev[0], apdev[0], "CCMP")
120
121 def test_ap_cipher_gcmp(dev, apdev):
122     """WPA2-PSK/GCMP connection"""
123     return check_cipher(dev[0], apdev[0], "GCMP")
124
125 def test_ap_cipher_ccmp_256(dev, apdev):
126     """WPA2-PSK/CCMP-256 connection"""
127     return check_cipher(dev[0], apdev[0], "CCMP-256")
128
129 def test_ap_cipher_gcmp_256(dev, apdev):
130     """WPA2-PSK/GCMP-256 connection"""
131     return check_cipher(dev[0], apdev[0], "GCMP-256")
132
133 def test_ap_cipher_mixed_wpa_wpa2(dev, apdev):
134     """WPA2-PSK/CCMP/ and WPA-PSK/TKIP mixed configuration"""
135     ssid = "test-wpa-wpa2-psk"
136     passphrase = "12345678"
137     params = { "ssid": ssid,
138                "wpa_passphrase": passphrase,
139                "wpa": "3",
140                "wpa_key_mgmt": "WPA-PSK",
141                "rsn_pairwise": "CCMP",
142                "wpa_pairwise": "TKIP" }
143     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
144     dev[0].connect(ssid, psk=passphrase, proto="WPA2",
145                    pairwise="CCMP", group="TKIP", scan_freq="2412")
146     status = dev[0].get_status()
147     if status['key_mgmt'] != 'WPA2-PSK':
148         raise Exception("Incorrect key_mgmt reported")
149     if status['pairwise_cipher'] != 'CCMP':
150         raise Exception("Incorrect pairwise_cipher reported")
151     if status['group_cipher'] != 'TKIP':
152         raise Exception("Incorrect group_cipher reported")
153     bss = dev[0].get_bss(apdev[0]['bssid'])
154     if bss['ssid'] != ssid:
155         raise Exception("Unexpected SSID in the BSS entry")
156     if "[WPA-PSK-TKIP]" not in bss['flags']:
157         raise Exception("Missing BSS flag WPA-PSK-TKIP")
158     if "[WPA2-PSK-CCMP]" not in bss['flags']:
159         raise Exception("Missing BSS flag WPA2-PSK-CCMP")
160     hwsim_utils.test_connectivity(dev[0], hapd)
161
162     dev[1].connect(ssid, psk=passphrase, proto="WPA",
163                    pairwise="TKIP", group="TKIP", scan_freq="2412")
164     status = dev[1].get_status()
165     if status['key_mgmt'] != 'WPA-PSK':
166         raise Exception("Incorrect key_mgmt reported")
167     if status['pairwise_cipher'] != 'TKIP':
168         raise Exception("Incorrect pairwise_cipher reported")
169     if status['group_cipher'] != 'TKIP':
170         raise Exception("Incorrect group_cipher reported")
171     hwsim_utils.test_connectivity(dev[1], hapd)
172     hwsim_utils.test_connectivity(dev[0], dev[1])