wlantest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Python class for controlling wlantest
  2. # Copyright (c) 2013-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 os
  7. import time
  8. import subprocess
  9. import logging
  10. import wpaspy
  11. logger = logging.getLogger()
  12. class Wlantest:
  13. def __init__(self):
  14. if os.path.isfile('../../wlantest/wlantest_cli'):
  15. self.wlantest_cli = '../../wlantest/wlantest_cli'
  16. else:
  17. self.wlantest_cli = 'wlantest_cli'
  18. def flush(self):
  19. res = subprocess.check_output([self.wlantest_cli, "flush"])
  20. if "FAIL" in res:
  21. raise Exception("wlantest_cli flush failed")
  22. def relog(self):
  23. res = subprocess.check_output([self.wlantest_cli, "relog"])
  24. if "FAIL" in res:
  25. raise Exception("wlantest_cli relog failed")
  26. def add_passphrase(self, passphrase):
  27. res = subprocess.check_output([self.wlantest_cli, "add_passphrase",
  28. passphrase])
  29. if "FAIL" in res:
  30. raise Exception("wlantest_cli add_passphrase failed")
  31. def add_wepkey(self, key):
  32. res = subprocess.check_output([self.wlantest_cli, "add_wepkey", key])
  33. if "FAIL" in res:
  34. raise Exception("wlantest_cli add_key failed")
  35. def info_bss(self, field, bssid):
  36. res = subprocess.check_output([self.wlantest_cli, "info_bss",
  37. field, bssid])
  38. if "FAIL" in res:
  39. raise Exception("Could not get BSS info from wlantest for " + bssid)
  40. return res
  41. def get_bss_counter(self, field, bssid):
  42. try:
  43. res = subprocess.check_output([self.wlantest_cli, "get_bss_counter",
  44. field, bssid]);
  45. except Exception, e:
  46. return 0
  47. if "FAIL" in res:
  48. return 0
  49. return int(res)
  50. def clear_bss_counters(self, bssid):
  51. subprocess.call([self.wlantest_cli, "clear_bss_counters", bssid]);
  52. def info_sta(self, field, bssid, addr):
  53. res = subprocess.check_output([self.wlantest_cli, "info_sta",
  54. field, bssid, addr])
  55. if "FAIL" in res:
  56. raise Exception("Could not get STA info from wlantest for " + addr)
  57. return res
  58. def get_sta_counter(self, field, bssid, addr):
  59. res = subprocess.check_output([self.wlantest_cli, "get_sta_counter",
  60. field, bssid, addr]);
  61. if "FAIL" in res:
  62. raise Exception("wlantest_cli command failed")
  63. return int(res)
  64. def clear_sta_counters(self, bssid, addr):
  65. res = subprocess.check_output([self.wlantest_cli, "clear_sta_counters",
  66. bssid, addr]);
  67. if "FAIL" in res:
  68. raise Exception("wlantest_cli command failed")
  69. def tdls_clear(self, bssid, addr1, addr2):
  70. res = subprocess.check_output([self.wlantest_cli, "clear_tdls_counters",
  71. bssid, addr1, addr2]);
  72. def get_tdls_counter(self, field, bssid, addr1, addr2):
  73. res = subprocess.check_output([self.wlantest_cli, "get_tdls_counter",
  74. field, bssid, addr1, addr2]);
  75. if "FAIL" in res:
  76. raise Exception("wlantest_cli command failed")
  77. return int(res)
  78. def require_ap_pmf_mandatory(self, bssid):
  79. res = self.info_bss("rsn_capab", bssid)
  80. if "MFPR" not in res:
  81. raise Exception("AP did not require PMF")
  82. if "MFPC" not in res:
  83. raise Exception("AP did not enable PMF")
  84. res = self.info_bss("key_mgmt", bssid)
  85. if "PSK-SHA256" not in res:
  86. raise Exception("AP did not enable SHA256-based AKM for PMF")
  87. def require_ap_pmf_optional(self, bssid):
  88. res = self.info_bss("rsn_capab", bssid)
  89. if "MFPR" in res:
  90. raise Exception("AP required PMF")
  91. if "MFPC" not in res:
  92. raise Exception("AP did not enable PMF")
  93. def require_ap_no_pmf(self, bssid):
  94. res = self.info_bss("rsn_capab", bssid)
  95. if "MFPR" in res:
  96. raise Exception("AP required PMF")
  97. if "MFPC" in res:
  98. raise Exception("AP enabled PMF")
  99. def require_sta_pmf_mandatory(self, bssid, addr):
  100. res = self.info_sta("rsn_capab", bssid, addr)
  101. if "MFPR" not in res:
  102. raise Exception("STA did not require PMF")
  103. if "MFPC" not in res:
  104. raise Exception("STA did not enable PMF")
  105. def require_sta_pmf(self, bssid, addr):
  106. res = self.info_sta("rsn_capab", bssid, addr)
  107. if "MFPC" not in res:
  108. raise Exception("STA did not enable PMF")
  109. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  110. res = self.info_sta("key_mgmt", bssid, addr)
  111. if key_mgmt not in res:
  112. raise Exception("Unexpected STA key_mgmt")
  113. def get_tx_tid(self, bssid, addr, tid):
  114. res = subprocess.check_output([self.wlantest_cli, "get_tx_tid",
  115. bssid, addr, str(tid)]);
  116. if "FAIL" in res:
  117. raise Exception("wlantest_cli command failed")
  118. return int(res)
  119. def get_rx_tid(self, bssid, addr, tid):
  120. res = subprocess.check_output([self.wlantest_cli, "get_rx_tid",
  121. bssid, addr, str(tid)]);
  122. if "FAIL" in res:
  123. raise Exception("wlantest_cli command failed")
  124. return int(res)
  125. def get_tid_counters(self, bssid, addr):
  126. tx = {}
  127. rx = {}
  128. for tid in range(0, 17):
  129. tx[tid] = self.get_tx_tid(bssid, addr, tid)
  130. rx[tid] = self.get_rx_tid(bssid, addr, tid)
  131. return [ tx, rx ]