test_wpas_config.py 5.0 KB

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