run-ap-tests.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python
  2. #
  3. # AP tests
  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 re
  10. import sys
  11. import time
  12. import logging
  13. from wpasupplicant import WpaSupplicant
  14. from hostapd import HostapdGlobal
  15. def reset_devs(dev, hapd_ifaces):
  16. for d in dev:
  17. d.reset()
  18. hapd = HostapdGlobal()
  19. for h in hapd_ifaces:
  20. hapd.remove(h)
  21. def main():
  22. idx = 1
  23. if len(sys.argv) > 1 and sys.argv[1] == '-d':
  24. logging.basicConfig(level=logging.DEBUG)
  25. idx = idx + 1
  26. elif len(sys.argv) > 1 and sys.argv[1] == '-q':
  27. logging.basicConfig(level=logging.WARNING)
  28. idx = idx + 1
  29. else:
  30. logging.basicConfig(level=logging.INFO)
  31. if len(sys.argv) > idx:
  32. test_filter = sys.argv[idx]
  33. else:
  34. test_filter = None
  35. dev0 = WpaSupplicant('wlan0')
  36. dev1 = WpaSupplicant('wlan1')
  37. dev = [ dev0, dev1 ]
  38. hapd_ifaces = [ 'wlan2', 'wlan3' ]
  39. for d in dev:
  40. if not d.ping():
  41. print d.ifname + ": No response from wpa_supplicant"
  42. return
  43. d.reset()
  44. print "DEV: " + d.ifname + ": " + d.p2p_dev_addr()
  45. tests = []
  46. for t in os.listdir("."):
  47. m = re.match(r'(test_ap_.*)\.py$', t)
  48. if m:
  49. print "Import test cases from " + t
  50. mod = __import__(m.group(1))
  51. for s in dir(mod):
  52. if s.startswith("test_"):
  53. func = mod.__dict__.get(s)
  54. tests.append(func)
  55. passed = []
  56. failed = []
  57. for t in tests:
  58. if test_filter:
  59. if test_filter != t.__name__:
  60. continue
  61. reset_devs(dev, hapd_ifaces)
  62. print "START " + t.__name__
  63. if t.__doc__:
  64. print "Test: " + t.__doc__
  65. for d in dev:
  66. d.request("NOTE TEST-START " + t.__name__)
  67. try:
  68. t(dev)
  69. passed.append(t.__name__)
  70. print "PASS " + t.__name__
  71. except Exception, e:
  72. print e
  73. failed.append(t.__name__)
  74. print "FAIL " + t.__name__
  75. for d in dev:
  76. d.request("NOTE TEST-STOP " + t.__name__)
  77. if not test_filter:
  78. reset_devs(dev, hapd_ifaces)
  79. print "passed tests: " + str(passed)
  80. print "failed tests: " + str(failed)
  81. if len(failed):
  82. sys.exit(1)
  83. if __name__ == "__main__":
  84. main()