1 # wpa_supplicant config file
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
11 from wpasupplicant import WpaSupplicant
13 def check_config(config):
14 with open(config, "r") as f:
16 if "update_config=1\n" not in data:
17 raise Exception("Missing update_config")
18 if "device_name=name\n" not in data:
19 raise Exception("Missing device_name")
20 if "eapol_version=2\n" not in data:
21 raise Exception("Missing eapol_version")
22 if "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=" not in data:
23 raise Exception("Missing ctrl_interface")
24 if "blob-base64-foo={" not in data:
25 raise Exception("Missing blob")
26 if "cred={" not in data:
27 raise Exception("Missing cred")
28 if "network={" not in data:
29 raise Exception("Missing network")
30 if "wps_priority=5\n" not in data:
31 raise Exception("Missing wps_priority")
34 def test_wpas_config_file(dev):
35 """wpa_supplicant config file parsing/writing"""
36 config = "/tmp/test_wpas_config_file.conf"
37 if os.path.exists(config):
40 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
42 wpas.interface_add("wlan5", config=config)
47 raise Exception("Missing config file did not result in an error")
50 with open(config, "w") as f:
51 f.write("update_config=1 \t\r\n")
54 f.write(" \t\reapol_version=2")
55 for i in range(0, 100):
58 f.write("device_name=name#foo\n")
60 wpas.interface_add("wlan5", config=config)
62 wpas.request("SET wps_priority 5")
64 id = wpas.add_network()
65 wpas.set_network_quoted(id, "ssid", "foo")
66 wpas.set_network_quoted(id, "psk", "12345678")
67 wpas.set_network(id, "bssid", "00:11:22:33:44:55")
68 wpas.set_network(id, "proto", "RSN")
69 wpas.set_network(id, "key_mgmt", "WPA-PSK-SHA256")
70 wpas.set_network(id, "pairwise", "CCMP")
71 wpas.set_network(id, "group", "CCMP")
72 wpas.set_network(id, "auth_alg", "OPEN")
75 wpas.set_cred(id, "priority", "3")
76 wpas.set_cred(id, "sp_priority", "6")
77 wpas.set_cred(id, "update_identifier", "4")
78 wpas.set_cred(id, "ocsp", "1")
79 wpas.set_cred(id, "eap", "TTLS")
80 wpas.set_cred(id, "req_conn_capab", "6:1234")
81 wpas.set_cred_quoted(id, "realm", "example.com")
82 wpas.set_cred_quoted(id, "provisioning_sp", "example.com")
83 wpas.set_cred_quoted(id, "domain", "example.com")
84 wpas.set_cred_quoted(id, "domain_suffix_match", "example.com")
85 wpas.set_cred(id, "roaming_consortium", "112233")
86 wpas.set_cred(id, "required_roaming_consortium", "112233")
87 wpas.set_cred_quoted(id, "roaming_partner",
88 "roaming.example.net,1,127,*")
89 wpas.set_cred_quoted(id, "ca_cert", "/tmp/ca.pem")
90 wpas.set_cred_quoted(id, "username", "user")
91 wpas.set_cred_quoted(id, "password", "secret")
92 ev = wpas.wait_event(["CRED-MODIFIED 0 password"])
94 wpas.request("SET blob foo 12345678")
96 if "OK" not in wpas.request("SAVE_CONFIG"):
97 raise Exception("Failed to save configuration file")
98 if "OK" not in wpas.global_request("SAVE_CONFIG"):
99 raise Exception("Failed to save configuration file")
101 wpas.interface_remove("wlan5")
102 data1 = check_config(config)
104 wpas.interface_add("wlan5", config=config)
105 if len(wpas.list_networks()) != 1:
106 raise Exception("Unexpected number of networks")
107 if len(wpas.request("LIST_CREDS").splitlines()) != 2:
108 raise Exception("Unexpected number of credentials")
110 if "OK" not in wpas.request("SAVE_CONFIG"):
111 raise Exception("Failed to save configuration file")
112 data2 = check_config(config)
117 raise Exception("Unexpected configuration change")
119 wpas.request("SET update_config 0")
120 if "OK" in wpas.request("SAVE_CONFIG"):
121 raise Exception("SAVE_CONFIG succeeded unexpectedly")
122 if "OK" in wpas.global_request("SAVE_CONFIG"):
123 raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
125 # replace the config file with a directory to break writing/renaming
128 wpas.request("SET update_config 1")
129 if "OK" in wpas.request("SAVE_CONFIG"):
130 raise Exception("SAVE_CONFIG succeeded unexpectedly")
131 if "OK" in wpas.global_request("SAVE_CONFIG"):
132 raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
140 os.remove(config + ".tmp")