Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_ssid.py
1 # -*- coding: utf-8 -*-
2 # SSID contents and encoding tests
3 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
4 #
5 # This software may be distributed under the terms of the BSD license.
6 # See README for more details.
7
8 import logging
9 logger = logging.getLogger()
10
11 import hostapd
12
13 def test_ssid_hex_encoded(dev, apdev):
14     """SSID configuration using hex encoded version"""
15     hostapd.add_ap(apdev[0]['ifname'], { "ssid2": '68656c6c6f' })
16     dev[0].connect("hello", key_mgmt="NONE", scan_freq="2412")
17     dev[1].connect(ssid2="68656c6c6f", key_mgmt="NONE", scan_freq="2412")
18
19 def test_ssid_printf_encoded(dev, apdev):
20     """SSID configuration using printf encoded version"""
21     hostapd.add_ap(apdev[0]['ifname'], { "ssid2": 'P"\\0hello\\nthere"' })
22     dev[0].connect(ssid2="0068656c6c6f0a7468657265", key_mgmt="NONE",
23                    scan_freq="2412")
24     dev[1].connect(ssid2='P"\\x00hello\\nthere"', key_mgmt="NONE",
25                    scan_freq="2412")
26     ssid = dev[0].get_status_field("ssid")
27     bss = dev[1].get_bss(apdev[0]['bssid'])
28     if ssid != bss['ssid']:
29         raise Exception("Unexpected difference in SSID")
30     dev[2].connect(ssid2='P"' + ssid + '"', key_mgmt="NONE", scan_freq="2412")
31
32 def test_ssid_1_octet(dev, apdev):
33     """SSID with one octet"""
34     hostapd.add_ap(apdev[0]['ifname'], { "ssid": '1' })
35     dev[0].connect("1", key_mgmt="NONE", scan_freq="2412")
36
37 def test_ssid_32_octets(dev, apdev):
38     """SSID with 32 octets"""
39     hostapd.add_ap(apdev[0]['ifname'],
40                    { "ssid": '1234567890abcdef1234567890ABCDEF' })
41     dev[0].connect("1234567890abcdef1234567890ABCDEF", key_mgmt="NONE",
42                    scan_freq="2412")
43
44 def test_ssid_utf8(dev, apdev):
45     """SSID with UTF8 encoding"""
46     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'testi-åäöÅÄÖ-testi',
47                                                 "utf8_ssid": "1" })
48     dev[0].connect("testi-åäöÅÄÖ-testi", key_mgmt="NONE", scan_freq="2412")
49     dev[1].connect(ssid2="74657374692dc3a5c3a4c3b6c385c384c3962d7465737469",
50                    key_mgmt="NONE", scan_freq="2412")
51     # verify ctrl_iface for coverage
52     addrs = [ dev[0].p2p_interface_addr(), dev[1].p2p_interface_addr() ]
53     sta = hapd.get_sta(None)
54     if sta['addr'] not in addrs:
55         raise Exception("Unexpected STA address")
56     sta2 = hapd.get_sta(sta['addr'], next=True)
57     if sta2['addr'] not in addrs:
58         raise Exception("Unexpected STA2 address")
59     sta3 = hapd.get_sta(sta2['addr'], next=True)
60     if len(sta3) != 0:
61         raise Exception("Unexpected STA iteration result (did not stop)")
62
63 def test_ssid_hidden(dev, apdev):
64     """Hidden SSID"""
65     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'secret',
66                                                 "ignore_broadcast_ssid": "1" })
67     dev[1].connect("secret", key_mgmt="NONE", scan_freq="2412",
68                    wait_connect=False)
69     dev[0].connect("secret", key_mgmt="NONE", scan_freq="2412", scan_ssid="1")
70     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
71     if ev is not None:
72         raise Exception("Unexpected connection")
73     dev[0].request("DISCONNECT")
74     dev[1].request("DISCONNECT")
75     # clear BSS table to avoid issues in following test cases
76     hapd.disable()
77     dev[0].request("BSS_FLUSH 0")
78     dev[0].request("SCAN freq=2412 only_new=1")
79     dev[1].request("BSS_FLUSH 0")
80     dev[1].request("SCAN freq=2412 only_new=1")
81
82 def test_ssid_hidden2(dev, apdev):
83     """Hidden SSID using zero octets as payload"""
84     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'secret2',
85                                                 "ignore_broadcast_ssid": "2" })
86     dev[1].connect("secret2", key_mgmt="NONE", scan_freq="2412",
87                    wait_connect=False)
88     dev[0].connect("secret2", key_mgmt="NONE", scan_freq="2412", scan_ssid="1")
89     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
90     if ev is not None:
91         raise Exception("Unexpected connection")
92     dev[0].request("DISCONNECT")
93     dev[1].request("DISCONNECT")
94     # clear BSS table to avoid issues in following test cases
95     hapd.disable()
96     dev[0].request("BSS_FLUSH 0")
97     dev[0].request("SCAN freq=2412 only_new=1")
98     dev[1].request("BSS_FLUSH 0")
99     dev[1].request("SCAN freq=2412 only_new=1")
100
101 def test_ssid_hidden_wpa2(dev, apdev):
102     """Hidden SSID with WPA2-PSK"""
103     params = hostapd.wpa2_params(ssid="secret", passphrase="12345678")
104     params["ignore_broadcast_ssid"] = "1"
105     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
106     dev[1].connect("secret", psk="12345678", scan_freq="2412",
107                    wait_connect=False)
108     dev[0].connect("secret", psk="12345678", scan_freq="2412", scan_ssid="1")
109     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
110     if ev is not None:
111         raise Exception("Unexpected connection")
112     dev[0].request("DISCONNECT")
113     dev[1].request("DISCONNECT")
114     # clear BSS table to avoid issues in following test cases
115     hapd.disable()
116     dev[0].request("BSS_FLUSH 0")
117     dev[0].request("SCAN freq=2412 only_new=1")
118     dev[1].request("BSS_FLUSH 0")
119     dev[1].request("SCAN freq=2412 only_new=1")