test_bgscan.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # bgscan tests
  2. # Copyright (c) 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 time
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import hostapd
  11. def test_bgscan_simple(dev, apdev):
  12. """bgscan_simple"""
  13. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "bgscan" })
  14. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "bgscan" })
  15. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  16. bgscan="simple:1:-20:2")
  17. dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  18. bgscan="simple:1:-45:2")
  19. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  20. if ev is None:
  21. raise Exception("dev0 did not indicate signal change event")
  22. if "above=0" not in ev:
  23. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  24. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  25. if ev is None:
  26. raise Exception("dev1 did not indicate signal change event")
  27. if "above=1" not in ev:
  28. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  29. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  30. if ev is None:
  31. raise Exception("dev0 did not start a scan")
  32. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  33. if ev is None:
  34. raise Exception("dev1 did not start a scan")
  35. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  36. if ev is None:
  37. raise Exception("dev0 did not complete a scan")
  38. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  39. if ev is None:
  40. raise Exception("dev1 did not complete a scan")
  41. def test_bgscan_learn(dev, apdev):
  42. """bgscan_learn"""
  43. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "bgscan" })
  44. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "bgscan" })
  45. try:
  46. os.remove("/tmp/test_bgscan_learn.bgscan")
  47. except:
  48. pass
  49. try:
  50. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  51. bgscan="learn:1:-20:2")
  52. dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  53. bgscan="learn:1:-45:2:/tmp/test_bgscan_learn.bgscan")
  54. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  55. if ev is None:
  56. raise Exception("dev0 did not indicate signal change event")
  57. if "above=0" not in ev:
  58. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  59. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  60. if ev is None:
  61. raise Exception("dev1 did not indicate signal change event")
  62. if "above=1" not in ev:
  63. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  64. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  65. if ev is None:
  66. raise Exception("dev0 did not start a scan")
  67. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  68. if ev is None:
  69. raise Exception("dev1 did not start a scan")
  70. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  71. if ev is None:
  72. raise Exception("dev0 did not complete a scan")
  73. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  74. if ev is None:
  75. raise Exception("dev1 did not complete a scan")
  76. dev[0].request("DISCONNECT")
  77. dev[1].request("DISCONNECT")
  78. dev[0].request("REMOVE_NETWORK all")
  79. dev[1].request("REMOVE_NETWORK all")
  80. with open("/tmp/test_bgscan_learn.bgscan", "r") as f:
  81. lines = f.read().splitlines()
  82. if lines[0] != "wpa_supplicant-bgscan-learn":
  83. raise Exception("Unexpected bgscan header line")
  84. if 'BSS 02:00:00:00:03:00 2412' not in lines:
  85. raise Exception("Missing BSS1")
  86. if 'BSS 02:00:00:00:04:00 2412' not in lines:
  87. raise Exception("Missing BSS2")
  88. if 'NEIGHBOR 02:00:00:00:03:00 02:00:00:00:04:00' not in lines:
  89. raise Exception("Missing BSS1->BSS2 neighbor entry")
  90. if 'NEIGHBOR 02:00:00:00:04:00 02:00:00:00:03:00' not in lines:
  91. raise Exception("Missing BSS2->BSS1 neighbor entry")
  92. finally:
  93. try:
  94. os.remove("/tmp/test_bgscan_learn.bgscan")
  95. except:
  96. pass