test_wpas_config.py 5.3 KB

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