wlantest.py 4.2 KB

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