test_wpas_config.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. import logging
  7. logger = logging.getLogger()
  8. import os
  9. from wpasupplicant import WpaSupplicant
  10. import hostapd
  11. def check_config(config):
  12. with open(config, "r") as f:
  13. data = f.read()
  14. if "update_config=1\n" not in data:
  15. raise Exception("Missing update_config")
  16. if "device_name=name\n" not in data:
  17. raise Exception("Missing device_name")
  18. if "eapol_version=2\n" not in data:
  19. raise Exception("Missing eapol_version")
  20. if "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=" not in data:
  21. raise Exception("Missing ctrl_interface")
  22. if "blob-base64-foo={" not in data:
  23. raise Exception("Missing blob")
  24. if "cred={" not in data:
  25. raise Exception("Missing cred")
  26. if "network={" not in data:
  27. raise Exception("Missing network")
  28. if "wps_priority=5\n" not in data:
  29. raise Exception("Missing wps_priority")
  30. if "ip_addr_go=192.168.1.1\n" not in data:
  31. raise Exception("Missing ip_addr_go")
  32. if "ip_addr_mask=255.255.255.0\n" not in data:
  33. raise Exception("Missing ip_addr_mask")
  34. if "ip_addr_start=192.168.1.10\n" not in data:
  35. raise Exception("Missing ip_addr_start")
  36. if "ip_addr_end=192.168.1.20\n" not in data:
  37. raise Exception("Missing ip_addr_end")
  38. return data
  39. def test_wpas_config_file(dev):
  40. """wpa_supplicant config file parsing/writing"""
  41. config = "/tmp/test_wpas_config_file.conf"
  42. if os.path.exists(config):
  43. os.remove(config)
  44. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  45. try:
  46. wpas.interface_add("wlan5", config=config)
  47. initialized = True
  48. except:
  49. initialized = False
  50. if initialized:
  51. raise Exception("Missing config file did not result in an error")
  52. try:
  53. with open(config, "w") as f:
  54. f.write("update_config=1 \t\r\n")
  55. f.write("# foo\n")
  56. f.write("\n")
  57. f.write(" \t\reapol_version=2")
  58. for i in range(0, 100):
  59. f.write(" ")
  60. f.write("foo\n")
  61. f.write("device_name=name#foo\n")
  62. wpas.interface_add("wlan5", config=config)
  63. 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")
  73. id = wpas.add_cred()
  74. wpas.set_cred(id, "priority", "3")
  75. wpas.set_cred(id, "sp_priority", "6")
  76. wpas.set_cred(id, "update_identifier", "4")
  77. wpas.set_cred(id, "ocsp", "1")
  78. wpas.set_cred(id, "eap", "TTLS")
  79. wpas.set_cred(id, "req_conn_capab", "6:1234")
  80. wpas.set_cred_quoted(id, "realm", "example.com")
  81. wpas.set_cred_quoted(id, "provisioning_sp", "example.com")
  82. wpas.set_cred_quoted(id, "domain", "example.com")
  83. wpas.set_cred_quoted(id, "domain_suffix_match", "example.com")
  84. wpas.set_cred(id, "roaming_consortium", "112233")
  85. wpas.set_cred(id, "required_roaming_consortium", "112233")
  86. wpas.set_cred_quoted(id, "roaming_partner",
  87. "roaming.example.net,1,127,*")
  88. wpas.set_cred_quoted(id, "ca_cert", "/tmp/ca.pem")
  89. wpas.set_cred_quoted(id, "username", "user")
  90. wpas.set_cred_quoted(id, "password", "secret")
  91. ev = wpas.wait_event(["CRED-MODIFIED 0 password"])
  92. wpas.request("SET blob foo 12345678")
  93. wpas.request("SET ip_addr_go 192.168.1.1")
  94. wpas.request("SET ip_addr_mask 255.255.255.0")
  95. wpas.request("SET ip_addr_start 192.168.1.10")
  96. wpas.request("SET ip_addr_end 192.168.1.20")
  97. if "OK" not in wpas.request("SAVE_CONFIG"):
  98. raise Exception("Failed to save configuration file")
  99. if "OK" not in wpas.global_request("SAVE_CONFIG"):
  100. raise Exception("Failed to save configuration file")
  101. wpas.interface_remove("wlan5")
  102. data1 = check_config(config)
  103. wpas.interface_add("wlan5", config=config)
  104. if len(wpas.list_networks()) != 1:
  105. raise Exception("Unexpected number of networks")
  106. if len(wpas.request("LIST_CREDS").splitlines()) != 2:
  107. raise Exception("Unexpected number of credentials")
  108. if "OK" not in wpas.request("SAVE_CONFIG"):
  109. raise Exception("Failed to save configuration file")
  110. data2 = check_config(config)
  111. if data1 != data2:
  112. logger.debug(data1)
  113. logger.debug(data2)
  114. raise Exception("Unexpected configuration change")
  115. wpas.request("SET update_config 0")
  116. wpas.global_request("SET update_config 0")
  117. if "OK" in wpas.request("SAVE_CONFIG"):
  118. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  119. if "OK" in wpas.global_request("SAVE_CONFIG"):
  120. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  121. # replace the config file with a directory to break writing/renaming
  122. os.remove(config)
  123. os.mkdir(config)
  124. wpas.request("SET update_config 1")
  125. wpas.global_request("SET update_config 1")
  126. if "OK" in wpas.request("SAVE_CONFIG"):
  127. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  128. if "OK" in wpas.global_request("SAVE_CONFIG"):
  129. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  130. finally:
  131. try:
  132. os.remove(config)
  133. except:
  134. pass
  135. try:
  136. os.remove(config + ".tmp")
  137. except:
  138. pass
  139. try:
  140. os.rmdir(config)
  141. except:
  142. pass
  143. def test_wpas_config_file_wps(dev, apdev):
  144. """wpa_supplicant config file parsing/writing with WPS"""
  145. config = "/tmp/test_wpas_config_file.conf"
  146. if os.path.exists(config):
  147. os.remove(config)
  148. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  149. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred" }
  150. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  151. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  152. try:
  153. with open(config, "w") as f:
  154. f.write("update_config=1\n")
  155. wpas.interface_add("wlan5", config=config)
  156. hapd.request("WPS_PIN any 12345670")
  157. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  158. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  159. ev = wpas.wait_event(["WPS-FAIL"], timeout=10)
  160. if ev is None:
  161. raise Exception("WPS-FAIL event timed out")
  162. with open(config, "r") as f:
  163. data = f.read()
  164. logger.info("Configuration file contents: " + data)
  165. if "network=" in data:
  166. raise Exception("Unexpected network block in configuration data")
  167. finally:
  168. try:
  169. os.remove(config)
  170. except:
  171. pass
  172. try:
  173. os.remove(config + ".tmp")
  174. except:
  175. pass
  176. try:
  177. os.rmdir(config)
  178. except:
  179. pass
  180. def test_wpas_config_file_wps2(dev, apdev):
  181. """wpa_supplicant config file parsing/writing with WPS (2)"""
  182. config = "/tmp/test_wpas_config_file.conf"
  183. if os.path.exists(config):
  184. os.remove(config)
  185. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  186. "skip_cred_build": "1", "extra_cred": "wps-ctrl-cred2" }
  187. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  188. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  189. try:
  190. with open(config, "w") as f:
  191. f.write("update_config=1\n")
  192. wpas.interface_add("wlan5", config=config)
  193. hapd.request("WPS_PIN any 12345670")
  194. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  195. wpas.request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  196. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=10)
  197. if ev is None:
  198. raise Exception("WPS-SUCCESS event timed out")
  199. with open(config, "r") as f:
  200. data = f.read()
  201. logger.info("Configuration file contents: " + data)
  202. with open(config, "r") as f:
  203. data = f.read()
  204. if "network=" not in data:
  205. raise Exception("Missing network block in configuration data")
  206. if "ssid=410a420d430044" not in data:
  207. raise Exception("Unexpected ssid parameter value")
  208. finally:
  209. try:
  210. os.remove(config)
  211. except:
  212. pass
  213. try:
  214. os.remove(config + ".tmp")
  215. except:
  216. pass
  217. try:
  218. os.rmdir(config)
  219. except:
  220. pass