Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_wpas_config.py
1 # wpa_supplicant config file
2 # Copyright (c) 2014, 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 logging
8 logger = logging.getLogger()
9 import os
10
11 from wpasupplicant import WpaSupplicant
12
13 def check_config(config):
14     with open(config, "r") as f:
15         data = f.read()
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")
32     return data
33
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):
38         os.remove(config)
39
40     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
41     try:
42         wpas.interface_add("wlan5", config=config)
43         initialized = True
44     except:
45         initialized = False
46     if initialized:
47         raise Exception("Missing config file did not result in an error")
48
49     try:
50         with open(config, "w") as f:
51             f.write("update_config=1 \t\r\n")
52             f.write("# foo\n")
53             f.write("\n")
54             f.write(" \t\reapol_version=2")
55             for i in range(0, 100):
56                 f.write("                    ")
57             f.write("foo\n")
58             f.write("device_name=name#foo\n")
59
60         wpas.interface_add("wlan5", config=config)
61
62         wpas.request("SET wps_priority 5")
63
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")
73
74         id = wpas.add_cred()
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"])
93
94         wpas.request("SET blob foo 12345678")
95
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")
100
101         wpas.interface_remove("wlan5")
102         data1 = check_config(config)
103
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")
109
110         if "OK" not in wpas.request("SAVE_CONFIG"):
111             raise Exception("Failed to save configuration file")
112         data2 = check_config(config)
113
114         if data1 != data2:
115             logger.debug(data1)
116             logger.debug(data2)
117             raise Exception("Unexpected configuration change")
118
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")
124
125         # replace the config file with a directory to break writing/renaming
126         os.remove(config)
127         os.mkdir(config)
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")
133
134     finally:
135         try:
136             os.remove(config)
137         except:
138             pass
139         try:
140             os.remove(config + ".tmp")
141         except:
142             pass
143         try:
144             os.rmdir(config)
145         except:
146             pass