test_wpas_config.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. def check_config(config):
  11. with open(config, "r") as f:
  12. data = f.read()
  13. if "update_config=1\n" not in data:
  14. raise Exception("Missing update_config")
  15. if "device_name=name\n" not in data:
  16. raise Exception("Missing device_name")
  17. if "eapol_version=2\n" not in data:
  18. raise Exception("Missing eapol_version")
  19. if "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=" not in data:
  20. raise Exception("Missing ctrl_interface")
  21. if "blob-base64-foo={" not in data:
  22. raise Exception("Missing blob")
  23. if "cred={" not in data:
  24. raise Exception("Missing cred")
  25. if "network={" not in data:
  26. raise Exception("Missing network")
  27. if "wps_priority=5\n" not in data:
  28. raise Exception("Missing wps_priority")
  29. if "ip_addr_go=192.168.1.1\n" not in data:
  30. raise Exception("Missing ip_addr_go")
  31. if "ip_addr_mask=255.255.255.0\n" not in data:
  32. raise Exception("Missing ip_addr_mask")
  33. if "ip_addr_start=192.168.1.10\n" not in data:
  34. raise Exception("Missing ip_addr_start")
  35. if "ip_addr_end=192.168.1.20\n" not in data:
  36. raise Exception("Missing ip_addr_end")
  37. return data
  38. def test_wpas_config_file(dev):
  39. """wpa_supplicant config file parsing/writing"""
  40. config = "/tmp/test_wpas_config_file.conf"
  41. if os.path.exists(config):
  42. os.remove(config)
  43. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  44. try:
  45. wpas.interface_add("wlan5", config=config)
  46. initialized = True
  47. except:
  48. initialized = False
  49. if initialized:
  50. raise Exception("Missing config file did not result in an error")
  51. try:
  52. with open(config, "w") as f:
  53. f.write("update_config=1 \t\r\n")
  54. f.write("# foo\n")
  55. f.write("\n")
  56. f.write(" \t\reapol_version=2")
  57. for i in range(0, 100):
  58. f.write(" ")
  59. f.write("foo\n")
  60. f.write("device_name=name#foo\n")
  61. wpas.interface_add("wlan5", config=config)
  62. wpas.request("SET wps_priority 5")
  63. id = wpas.add_network()
  64. wpas.set_network_quoted(id, "ssid", "foo")
  65. wpas.set_network_quoted(id, "psk", "12345678")
  66. wpas.set_network(id, "bssid", "00:11:22:33:44:55")
  67. wpas.set_network(id, "proto", "RSN")
  68. wpas.set_network(id, "key_mgmt", "WPA-PSK-SHA256")
  69. wpas.set_network(id, "pairwise", "CCMP")
  70. wpas.set_network(id, "group", "CCMP")
  71. wpas.set_network(id, "auth_alg", "OPEN")
  72. id = wpas.add_cred()
  73. wpas.set_cred(id, "priority", "3")
  74. wpas.set_cred(id, "sp_priority", "6")
  75. wpas.set_cred(id, "update_identifier", "4")
  76. wpas.set_cred(id, "ocsp", "1")
  77. wpas.set_cred(id, "eap", "TTLS")
  78. wpas.set_cred(id, "req_conn_capab", "6:1234")
  79. wpas.set_cred_quoted(id, "realm", "example.com")
  80. wpas.set_cred_quoted(id, "provisioning_sp", "example.com")
  81. wpas.set_cred_quoted(id, "domain", "example.com")
  82. wpas.set_cred_quoted(id, "domain_suffix_match", "example.com")
  83. wpas.set_cred(id, "roaming_consortium", "112233")
  84. wpas.set_cred(id, "required_roaming_consortium", "112233")
  85. wpas.set_cred_quoted(id, "roaming_partner",
  86. "roaming.example.net,1,127,*")
  87. wpas.set_cred_quoted(id, "ca_cert", "/tmp/ca.pem")
  88. wpas.set_cred_quoted(id, "username", "user")
  89. wpas.set_cred_quoted(id, "password", "secret")
  90. ev = wpas.wait_event(["CRED-MODIFIED 0 password"])
  91. wpas.request("SET blob foo 12345678")
  92. wpas.request("SET ip_addr_go 192.168.1.1")
  93. wpas.request("SET ip_addr_mask 255.255.255.0")
  94. wpas.request("SET ip_addr_start 192.168.1.10")
  95. wpas.request("SET ip_addr_end 192.168.1.20")
  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. wpas.interface_remove("wlan5")
  101. data1 = check_config(config)
  102. wpas.interface_add("wlan5", config=config)
  103. if len(wpas.list_networks()) != 1:
  104. raise Exception("Unexpected number of networks")
  105. if len(wpas.request("LIST_CREDS").splitlines()) != 2:
  106. raise Exception("Unexpected number of credentials")
  107. if "OK" not in wpas.request("SAVE_CONFIG"):
  108. raise Exception("Failed to save configuration file")
  109. data2 = check_config(config)
  110. if data1 != data2:
  111. logger.debug(data1)
  112. logger.debug(data2)
  113. raise Exception("Unexpected configuration change")
  114. wpas.request("SET update_config 0")
  115. wpas.global_request("SET update_config 0")
  116. if "OK" in wpas.request("SAVE_CONFIG"):
  117. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  118. if "OK" in wpas.global_request("SAVE_CONFIG"):
  119. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  120. # replace the config file with a directory to break writing/renaming
  121. os.remove(config)
  122. os.mkdir(config)
  123. wpas.request("SET update_config 1")
  124. wpas.global_request("SET update_config 1")
  125. if "OK" in wpas.request("SAVE_CONFIG"):
  126. raise Exception("SAVE_CONFIG succeeded unexpectedly")
  127. if "OK" in wpas.global_request("SAVE_CONFIG"):
  128. raise Exception("SAVE_CONFIG (global) succeeded unexpectedly")
  129. finally:
  130. try:
  131. os.remove(config)
  132. except:
  133. pass
  134. try:
  135. os.remove(config + ".tmp")
  136. except:
  137. pass
  138. try:
  139. os.rmdir(config)
  140. except:
  141. pass