wlantest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 get_sta_counter(self, field, bssid, addr):
  41. res = subprocess.check_output([wlantest_cli, "get_sta_counter", field,
  42. bssid, addr]);
  43. if "FAIL" in res:
  44. raise Exception("wlantest_cli command failed")
  45. return int(res)
  46. def tdls_clear(self, bssid, addr1, addr2):
  47. subprocess.call([wlantest_cli, "clear_tdls_counters", bssid, addr1,
  48. addr2]);
  49. def get_tdls_counter(self, field, bssid, addr1, addr2):
  50. res = subprocess.check_output([wlantest_cli, "get_tdls_counter", field,
  51. bssid, addr1, addr2]);
  52. if "FAIL" in res:
  53. raise Exception("wlantest_cli command failed")
  54. return int(res)
  55. def require_ap_pmf_mandatory(self, bssid):
  56. res = self.info_bss("rsn_capab", bssid)
  57. if "MFPR" not in res:
  58. raise Exception("AP did not require PMF")
  59. if "MFPC" not in res:
  60. raise Exception("AP did not enable PMF")
  61. res = self.info_bss("key_mgmt", bssid)
  62. if "PSK-SHA256" not in res:
  63. raise Exception("AP did not enable SHA256-based AKM for PMF")
  64. def require_ap_pmf_optional(self, bssid):
  65. res = self.info_bss("rsn_capab", bssid)
  66. if "MFPR" in res:
  67. raise Exception("AP required PMF")
  68. if "MFPC" not in res:
  69. raise Exception("AP did not enable PMF")
  70. def require_ap_no_pmf(self, bssid):
  71. res = self.info_bss("rsn_capab", bssid)
  72. if "MFPR" in res:
  73. raise Exception("AP required PMF")
  74. if "MFPC" in res:
  75. raise Exception("AP enabled PMF")
  76. def require_sta_pmf_mandatory(self, bssid, addr):
  77. res = self.info_sta("rsn_capab", bssid, addr)
  78. if "MFPR" not in res:
  79. raise Exception("STA did not require PMF")
  80. if "MFPC" not in res:
  81. raise Exception("STA did not enable PMF")
  82. def require_sta_pmf(self, bssid, addr):
  83. res = self.info_sta("rsn_capab", bssid, addr)
  84. if "MFPC" not in res:
  85. raise Exception("STA did not enable PMF")
  86. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  87. res = self.info_sta("key_mgmt", bssid, addr)
  88. if key_mgmt not in res:
  89. raise Exception("Unexpected STA key_mgmt")