wlantest.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. #
  3. # Python class for controlling wlantest
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import os
  9. import time
  10. import subprocess
  11. import logging
  12. import wpaspy
  13. logger = logging.getLogger(__name__)
  14. wlantest_cli = '../../wlantest/wlantest_cli'
  15. class Wlantest:
  16. def flush(self):
  17. res = subprocess.check_output([wlantest_cli, "flush"])
  18. if "FAIL" in res:
  19. raise Exception("wlantest_cli flush failed")
  20. def add_passphrase(self, passphrase):
  21. res = subprocess.check_output([wlantest_cli, "add_passphrase",
  22. passphrase])
  23. if "FAIL" in res:
  24. raise Exception("wlantest_cli add_passphrase failed")
  25. def add_wepkey(self, key):
  26. res = subprocess.check_output([wlantest_cli, "add_wepkey", key])
  27. if "FAIL" in res:
  28. raise Exception("wlantest_cli add_key failed")
  29. def info_bss(self, field, bssid):
  30. res = subprocess.check_output([wlantest_cli, "info_bss", field, bssid])
  31. if "FAIL" in res:
  32. raise Exception("Could not get BSS info from wlantest for " + bssid)
  33. return res
  34. def info_sta(self, field, bssid, addr):
  35. res = subprocess.check_output([wlantest_cli, "info_sta", field, bssid,
  36. addr])
  37. if "FAIL" in res:
  38. raise Exception("Could not get STA info from wlantest for " + addr)
  39. return res
  40. def tdls_clear(self, bssid, addr1, addr2):
  41. subprocess.call([wlantest_cli, "clear_tdls_counters", bssid, addr1,
  42. addr2]);
  43. def get_tdls_counter(self, field, bssid, addr1, addr2):
  44. res = subprocess.check_output([wlantest_cli, "get_tdls_counter", field,
  45. bssid, addr1, addr2]);
  46. if "FAIL" in res:
  47. raise Exception("wlantest_cli command failed")
  48. return int(res)
  49. def require_ap_pmf_mandatory(self, bssid):
  50. res = self.info_bss("rsn_capab", bssid)
  51. if "MFPR" not in res:
  52. raise Exception("AP did not require PMF")
  53. if "MFPC" not in res:
  54. raise Exception("AP did not enable PMF")
  55. res = self.info_bss("key_mgmt", bssid)
  56. if "PSK-SHA256" not in res:
  57. raise Exception("AP did not enable SHA256-based AKM for PMF")
  58. def require_ap_pmf_optional(self, bssid):
  59. res = self.info_bss("rsn_capab", bssid)
  60. if "MFPR" in res:
  61. raise Exception("AP required PMF")
  62. if "MFPC" not in res:
  63. raise Exception("AP did not enable PMF")
  64. def require_ap_no_pmf(self, bssid):
  65. res = self.info_bss("rsn_capab", bssid)
  66. if "MFPR" in res:
  67. raise Exception("AP required PMF")
  68. if "MFPC" in res:
  69. raise Exception("AP enabled PMF")
  70. def require_sta_pmf_mandatory(self, bssid, addr):
  71. res = self.info_sta("rsn_capab", bssid, addr)
  72. if "MFPR" not in res:
  73. raise Exception("STA did not require PMF")
  74. if "MFPC" not in res:
  75. raise Exception("STA did not enable PMF")
  76. def require_sta_pmf(self, bssid, addr):
  77. res = self.info_sta("rsn_capab", bssid, addr)
  78. if "MFPC" not in res:
  79. raise Exception("STA did not enable PMF")
  80. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  81. res = self.info_sta("key_mgmt", bssid, addr)
  82. if key_mgmt not in res:
  83. raise Exception("Unexpected STA key_mgmt")