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
|
#!/usr/bin/python3
# Test wpa_supplicant and dhclient in various modes
__author__ = 'Martin Pitt <martin.pitt@ubuntu.com>'
__copyright__ = '(C) 2013 Canonical Ltd.'
__license__ = 'GPL v2 or later'
import sys
import os
import os.path
import re
import time
import subprocess
import unittest
sys.path.append(os.path.dirname(__file__))
import network_test_base
SSID = 'fake net'
class T(network_test_base.NetworkTestBase):
@unittest.expectedFailure
def test_open_a_ip4(self):
'''Open network, 802.11a, IPv4'''
# channel 36 ought to work everywhere
self.do_test('hw_mode=a\nchannel=36\n\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
None,
['54.0'])
@unittest.expectedFailure
def test_open_a_ip6(self):
'''Open network, 802.11a, IPv6'''
self.do_test('hw_mode=a\nchannel=36\n\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
'',
['54.0'])
def test_open_b_ip4(self):
'''Open network, 802.11b, IPv4'''
self.do_test('hw_mode=b\nchannel=1\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
None,
['11.0'])
def test_open_b_ip6_dhcp(self):
'''Open network, 802.11b, IPv6 with DHCP'''
self.do_test('hw_mode=b\nchannel=1\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
'',
['11.0'])
def test_open_b_ip6_raonly(self):
'''Open network, 802.11b, IPv6 with only RA'''
self.do_test('hw_mode=b\nchannel=1\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
'ra-only',
['11.0'])
def test_open_g_ip4(self):
'''Open network, 802.11g, IPv4'''
self.do_test('hw_mode=g\nchannel=1\nssid=' + SSID,
'ssid="%s"\nkey_mgmt=NONE' % SSID,
None,
['54.0'])
def test_wpa1_ip4(self):
'''WPA1, 802.11g, IPv4'''
self.do_test('''hw_mode=g
channel=1
ssid=%s
wpa=1
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
wpa_passphrase=12345678
''' % SSID,
'''ssid="%s"
psk="12345678"
key_mgmt=WPA-PSK
proto=WPA
pairwise=TKIP
group=TKIP''' % SSID,
None,
['54.0',
'Pairwise ciphers: TKIP',
'Authentication suites: PSK'])
def test_wpa2_ip4(self):
'''WPA2, 802.11g, IPv4'''
self.do_test('''hw_mode=g
channel=1
ssid=%s
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_passphrase=12345678
''' % SSID,
'''ssid="%s"
psk="12345678"
key_mgmt=WPA-PSK
proto=WPA2
pairwise=CCMP
group=CCMP''' % SSID,
None,
['54.0',
'Pairwise ciphers: CCMP',
'Authentication suites: PSK'])
def test_wpa2_ip6(self):
'''WPA2, 802.11g, IPv6'''
self.do_test('''hw_mode=g
channel=1
ssid=%s
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_passphrase=12345678
''' % SSID,
'''ssid="%s"
psk="12345678"
key_mgmt=WPA-PSK
proto=WPA2
pairwise=CCMP
group=CCMP''' % SSID,
'',
['54.0',
'Pairwise ciphers: CCMP',
'Authentication suites: PSK'])
#
# Common for all tests
#
def do_test(self, hostapd_conf, wpa_conf, ipv6, exp_iw_scan):
self.setup_ap(hostapd_conf, ipv6)
self.check_ssid_avail(exp_iw_scan)
self.start_wpasupp(wpa_conf)
self.check_iw_link()
self.check_communication(ipv6)
self.check_address(ipv6)
def check_ssid_avail(self, expected_strings):
subprocess.check_call(['ip', 'link', 'set', self.dev_w_client, 'up'])
out = subprocess.check_output(['iw', 'dev', self.dev_w_client, 'scan'],
universal_newlines=True)
# down it again, wpa_supplicant is supposed to up it by itself
subprocess.check_call(['ip', 'link', 'set', self.dev_w_client, 'down'])
self.assertRegex(out, 'SSID: ' + SSID)
for s in expected_strings:
self.assertRegex(out, s)
def check_iw_link(self):
tries = 10
while tries > 0:
out = subprocess.check_output(['iw', 'dev', self.dev_w_client, 'link'],
universal_newlines=True)
if 'SSID' in out:
break
tries -= 1
time.sleep(1)
else:
self.fail('timed out on iwconfig showing connected status')
self.assertRegex(out, 'Connected to ' + self.mac_w_ap)
self.assertRegex(out, 'SSID: ' + SSID)
def check_communication(self, ipv6_mode):
'''Verify that communication works between AP and client
This proves that wpa_supplicant set up a working link. We use a DHCP
request for IPv4 and IPv6 in DHCP mode. For IPv6 in other (i.
e. ra-only/slaac) modes, check that dnsmasq received a router solicit
and sends a router advertisement.
'''
if ipv6_mode is not None:
self.poll_text(self.dnsmasq_log, 'RTR-SOLICIT(%s)' % self.dev_w_ap, timeout=50)
self.poll_text(self.dnsmasq_log, 'RTR-ADVERT(%s)' % self.dev_w_ap, timeout=5)
# stop here for non-DHCP modes in IPv6
if ipv6_mode not in (None, '', 'slaac'):
return
# FIXME: sometimes takes more than 5 s in IPv6 mode
if ipv6_mode is not None:
mode = '-6'
timeout = 10
else:
mode = '-4'
timeout = 5
# run DHCP client on client
out = subprocess.check_output(['dhclient', mode, '-1', '-v',
'-lf', '/dev/null', self.dev_w_client],
universal_newlines=True, timeout=timeout,
stderr=subprocess.STDOUT)
# stop DHCP client
subprocess.call(['dhclient', '-x', mode, self.dev_w_client],
stderr=subprocess.STDOUT)
if ipv6_mode is not None:
self.assertRegex(out, 'status code Success')
self.assertRegex(out, 'IAADDR 2600::')
else:
self.assertRegex(out, 'DHCPACK of')
self.assertRegex(out, 'bound to')
def check_address(self, ipv6_mode):
'''Verify that the interface got an appropriate address assigned'''
out = subprocess.check_output(['ip', 'a', 'show', 'dev', self.dev_w_client],
universal_newlines=True)
self.assertRegex(out, 'state UP')
if ipv6_mode is None:
self.assertRegex(out, 'inet 192.168.5.\d+/24')
else:
if not ipv6_mode:
# has global address from our DHCP server
self.assertRegex(out, 'inet6 2600::[0-9a-z]+/\d')
else:
# has address with our prefix and MAC
self.assertRegex(out, 'inet6 2600::ff:fe00:[0-9a-z]+/64 scope global (?:tentative )?(?:mngtmpaddr )?dynamic')
# has a link-local address
self.assertRegex(out, 'inet6 fe80::ff:fe00:[0-9a-z:]+/64 scope link')
if re.search(b's390', subprocess.run(['dpkg', '--print-architecture'], capture_output=True).stdout):
print("s390 arch has no wireless support, skipping")
sys.exit(77)
# write to stdout, not stderr
runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
unittest.main(testRunner=runner)
|