wlantest.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class Wlantest:
  15. def __init__(self):
  16. if os.path.isfile('../../wlantest/wlantest_cli'):
  17. self.wlantest_cli = '../../wlantest/wlantest_cli'
  18. else:
  19. self.wlantest_cli = 'wlantest_cli'
  20. def flush(self):
  21. res = subprocess.check_output([self.wlantest_cli, "flush"])
  22. if "FAIL" in res:
  23. raise Exception("wlantest_cli flush failed")
  24. def add_passphrase(self, passphrase):
  25. res = subprocess.check_output([self.wlantest_cli, "add_passphrase",
  26. passphrase])
  27. if "FAIL" in res:
  28. raise Exception("wlantest_cli add_passphrase failed")
  29. def add_wepkey(self, key):
  30. res = subprocess.check_output([self.wlantest_cli, "add_wepkey", key])
  31. if "FAIL" in res:
  32. raise Exception("wlantest_cli add_key failed")
  33. def info_bss(self, field, bssid):
  34. res = subprocess.check_output([self.wlantest_cli, "info_bss",
  35. field, bssid])
  36. if "FAIL" in res:
  37. raise Exception("Could not get BSS info from wlantest for " + bssid)
  38. return res
  39. def info_sta(self, field, bssid, addr):
  40. res = subprocess.check_output([self.wlantest_cli, "info_sta",
  41. field, bssid, addr])
  42. if "FAIL" in res:
  43. raise Exception("Could not get STA info from wlantest for " + addr)
  44. return res
  45. def get_sta_counter(self, field, bssid, addr):
  46. res = subprocess.check_output([self.wlantest_cli, "get_sta_counter",
  47. field, bssid, addr]);
  48. if "FAIL" in res:
  49. raise Exception("wlantest_cli command failed")
  50. return int(res)
  51. def tdls_clear(self, bssid, addr1, addr2):
  52. res = subprocess.check_output([self.wlantest_cli, "clear_tdls_counters",
  53. bssid, addr1, addr2]);
  54. def get_tdls_counter(self, field, bssid, addr1, addr2):
  55. res = subprocess.check_output([self.wlantest_cli, "get_tdls_counter",
  56. field, bssid, addr1, addr2]);
  57. if "FAIL" in res:
  58. raise Exception("wlantest_cli command failed")
  59. return int(res)
  60. def require_ap_pmf_mandatory(self, bssid):
  61. res = self.info_bss("rsn_capab", bssid)
  62. if "MFPR" not in res:
  63. raise Exception("AP did not require PMF")
  64. if "MFPC" not in res:
  65. raise Exception("AP did not enable PMF")
  66. res = self.info_bss("key_mgmt", bssid)
  67. if "PSK-SHA256" not in res:
  68. raise Exception("AP did not enable SHA256-based AKM for PMF")
  69. def require_ap_pmf_optional(self, bssid):
  70. res = self.info_bss("rsn_capab", bssid)
  71. if "MFPR" in res:
  72. raise Exception("AP required PMF")
  73. if "MFPC" not in res:
  74. raise Exception("AP did not enable PMF")
  75. def require_ap_no_pmf(self, bssid):
  76. res = self.info_bss("rsn_capab", bssid)
  77. if "MFPR" in res:
  78. raise Exception("AP required PMF")
  79. if "MFPC" in res:
  80. raise Exception("AP enabled PMF")
  81. def require_sta_pmf_mandatory(self, bssid, addr):
  82. res = self.info_sta("rsn_capab", bssid, addr)
  83. if "MFPR" not in res:
  84. raise Exception("STA did not require PMF")
  85. if "MFPC" not in res:
  86. raise Exception("STA did not enable PMF")
  87. def require_sta_pmf(self, bssid, addr):
  88. res = self.info_sta("rsn_capab", bssid, addr)
  89. if "MFPC" not in res:
  90. raise Exception("STA did not enable PMF")
  91. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  92. res = self.info_sta("key_mgmt", bssid, addr)
  93. if key_mgmt not in res:
  94. raise Exception("Unexpected STA key_mgmt")