wlantest.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 clear_sta_counters(self, bssid, addr):
  56. res = subprocess.check_output([self.wlantest_cli, "clear_sta_counters",
  57. bssid, addr]);
  58. if "FAIL" in res:
  59. raise Exception("wlantest_cli command failed")
  60. def tdls_clear(self, bssid, addr1, addr2):
  61. res = subprocess.check_output([self.wlantest_cli, "clear_tdls_counters",
  62. bssid, addr1, addr2]);
  63. def get_tdls_counter(self, field, bssid, addr1, addr2):
  64. res = subprocess.check_output([self.wlantest_cli, "get_tdls_counter",
  65. field, bssid, addr1, addr2]);
  66. if "FAIL" in res:
  67. raise Exception("wlantest_cli command failed")
  68. return int(res)
  69. def require_ap_pmf_mandatory(self, bssid):
  70. res = self.info_bss("rsn_capab", bssid)
  71. if "MFPR" not in res:
  72. raise Exception("AP did not require PMF")
  73. if "MFPC" not in res:
  74. raise Exception("AP did not enable PMF")
  75. res = self.info_bss("key_mgmt", bssid)
  76. if "PSK-SHA256" not in res:
  77. raise Exception("AP did not enable SHA256-based AKM for PMF")
  78. def require_ap_pmf_optional(self, bssid):
  79. res = self.info_bss("rsn_capab", bssid)
  80. if "MFPR" in res:
  81. raise Exception("AP required PMF")
  82. if "MFPC" not in res:
  83. raise Exception("AP did not enable PMF")
  84. def require_ap_no_pmf(self, bssid):
  85. res = self.info_bss("rsn_capab", bssid)
  86. if "MFPR" in res:
  87. raise Exception("AP required PMF")
  88. if "MFPC" in res:
  89. raise Exception("AP enabled PMF")
  90. def require_sta_pmf_mandatory(self, bssid, addr):
  91. res = self.info_sta("rsn_capab", bssid, addr)
  92. if "MFPR" not in res:
  93. raise Exception("STA did not require PMF")
  94. if "MFPC" not in res:
  95. raise Exception("STA did not enable PMF")
  96. def require_sta_pmf(self, bssid, addr):
  97. res = self.info_sta("rsn_capab", bssid, addr)
  98. if "MFPC" not in res:
  99. raise Exception("STA did not enable PMF")
  100. def require_sta_key_mgmt(self, bssid, addr, key_mgmt):
  101. res = self.info_sta("key_mgmt", bssid, addr)
  102. if key_mgmt not in res:
  103. raise Exception("Unexpected STA key_mgmt")
  104. def get_tx_tid(self, bssid, addr, tid):
  105. res = subprocess.check_output([self.wlantest_cli, "get_tx_tid",
  106. bssid, addr, str(tid)]);
  107. if "FAIL" in res:
  108. raise Exception("wlantest_cli command failed")
  109. return int(res)
  110. def get_rx_tid(self, bssid, addr, tid):
  111. res = subprocess.check_output([self.wlantest_cli, "get_rx_tid",
  112. bssid, addr, str(tid)]);
  113. if "FAIL" in res:
  114. raise Exception("wlantest_cli command failed")
  115. return int(res)
  116. def get_tid_counters(self, bssid, addr):
  117. tx = {}
  118. rx = {}
  119. for tid in range(0, 17):
  120. tx[tid] = self.get_tx_tid(bssid, addr, tid)
  121. rx[tid] = self.get_rx_tid(bssid, addr, tid)
  122. return [ tx, rx ]