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
8from remotehost import remote_compatible
9import logging
10logger = logging.getLogger()
11
12import hostapd
13
14@remote_compatible
15def test_ssid_hex_encoded(dev, apdev):
16    """SSID configuration using hex encoded version"""
17    hostapd.add_ap(apdev[0], {"ssid2": '68656c6c6f'})
18    dev[0].connect("hello", key_mgmt="NONE", scan_freq="2412")
19    dev[1].connect(ssid2="68656c6c6f", key_mgmt="NONE", scan_freq="2412")
20
21def test_ssid_printf_encoded(dev, apdev):
22    """SSID configuration using printf encoded version"""
23    hostapd.add_ap(apdev[0], {"ssid2": 'P"\\0hello\\nthere"'})
24    dev[0].connect(ssid2="0068656c6c6f0a7468657265", key_mgmt="NONE",
25                   scan_freq="2412")
26    dev[1].connect(ssid2='P"\\x00hello\\nthere"', key_mgmt="NONE",
27                   scan_freq="2412")
28    ssid = dev[0].get_status_field("ssid")
29    bss = dev[1].get_bss(apdev[0]['bssid'])
30    if ssid != bss['ssid']:
31        raise Exception("Unexpected difference in SSID")
32    dev[2].connect(ssid2='P"' + ssid + '"', key_mgmt="NONE", scan_freq="2412")
33
34@remote_compatible
35def test_ssid_1_octet(dev, apdev):
36    """SSID with one octet"""
37    hostapd.add_ap(apdev[0], {"ssid": '1'})
38    dev[0].connect("1", key_mgmt="NONE", scan_freq="2412")
39
40@remote_compatible
41def test_ssid_32_octets(dev, apdev):
42    """SSID with 32 octets"""
43    hostapd.add_ap(apdev[0],
44                   {"ssid": '1234567890abcdef1234567890ABCDEF'})
45    dev[0].connect("1234567890abcdef1234567890ABCDEF", key_mgmt="NONE",
46                   scan_freq="2412")
47
48def test_ssid_32_octets_nul_term(dev, apdev):
49    """SSID with 32 octets with nul at the end"""
50    ssid = 'P"1234567890abcdef1234567890ABCDE\\x00"'
51    hostapd.add_ap(apdev[0],
52                   {"ssid2": ssid})
53    dev[0].connect(ssid2=ssid, key_mgmt="NONE", scan_freq="2412")
54
55@remote_compatible
56def test_ssid_utf8(dev, apdev):
57    """SSID with UTF8 encoding"""
58    hapd = hostapd.add_ap(apdev[0], {"ssid": 'testi-åäöÅÄÖ-testi',
59                                     "utf8_ssid": "1"})
60    dev[0].connect("testi-åäöÅÄÖ-testi", key_mgmt="NONE", scan_freq="2412")
61    dev[1].connect(ssid2="74657374692dc3a5c3a4c3b6c385c384c3962d7465737469",
62                   key_mgmt="NONE", scan_freq="2412")
63    # verify ctrl_iface for coverage
64    addrs = [dev[0].p2p_interface_addr(), dev[1].p2p_interface_addr()]
65    sta = hapd.get_sta(None)
66    if sta['addr'] not in addrs:
67        raise Exception("Unexpected STA address")
68    sta2 = hapd.get_sta(sta['addr'], next=True)
69    if sta2['addr'] not in addrs:
70        raise Exception("Unexpected STA2 address")
71    sta3 = hapd.get_sta(sta2['addr'], next=True)
72    if len(sta3) != 0:
73        raise Exception("Unexpected STA iteration result (did not stop)")
74
75    if "[UTF-8]" not in dev[0].get_bss(hapd.own_addr())['flags']:
76        raise Exception("[UTF-8] flag not included in BSS")
77    if "[UTF-8]" not in dev[0].request("SCAN_RESULTS"):
78        raise Exception("[UTF-8] flag not included in SCAN_RESULTS")
79
80def clear_scan_cache2(hapd, dev):
81    # clear BSS table to avoid issues in following test cases
82    dev[0].request("REMOVE_NETWORK all")
83    dev[1].request("REMOVE_NETWORK all")
84    dev[0].wait_disconnected()
85    hapd.disable()
86    dev[0].flush_scan_cache()
87    dev[1].flush_scan_cache()
88
89@remote_compatible
90def test_ssid_hidden(dev, apdev):
91    """Hidden SSID"""
92    hapd = hostapd.add_ap(apdev[0], {"ssid": 'secret',
93                                     "ignore_broadcast_ssid": "1"})
94    dev[1].connect("secret", key_mgmt="NONE", scan_freq="2412",
95                   wait_connect=False)
96    dev[0].connect("secret", key_mgmt="NONE", scan_freq="2412", scan_ssid="1")
97    ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
98    if ev is not None:
99        raise Exception("Unexpected connection")
100    clear_scan_cache2(hapd, dev)
101
102@remote_compatible
103def test_ssid_hidden2(dev, apdev):
104    """Hidden SSID using zero octets as payload"""
105    hapd = hostapd.add_ap(apdev[0], {"ssid": 'secret2',
106                                     "ignore_broadcast_ssid": "2"})
107    dev[1].connect("secret2", key_mgmt="NONE", scan_freq="2412",
108                   wait_connect=False)
109    dev[0].connect("secret2", key_mgmt="NONE", scan_freq="2412", scan_ssid="1")
110    ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
111    if ev is not None:
112        raise Exception("Unexpected connection")
113    clear_scan_cache2(hapd, dev)
114
115@remote_compatible
116def test_ssid_hidden_wpa2(dev, apdev):
117    """Hidden SSID with WPA2-PSK"""
118    params = hostapd.wpa2_params(ssid="secret", passphrase="12345678")
119    params["ignore_broadcast_ssid"] = "1"
120    hapd = hostapd.add_ap(apdev[0], params)
121    dev[1].connect("secret", psk="12345678", scan_freq="2412",
122                   wait_connect=False)
123    dev[0].connect("secret", psk="12345678", scan_freq="2412", scan_ssid="1")
124    ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
125    if ev is not None:
126        raise Exception("Unexpected connection")
127    clear_scan_cache2(hapd, dev)
128