test_bgscan.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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], { "ssid": "bgscan" })
  14. hostapd.add_ap(apdev[1], { "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. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  20. bgscan="simple:1:-45")
  21. dev[2].request("REMOVE_NETWORK all")
  22. dev[2].wait_disconnected()
  23. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  24. bgscan="simple:0:0")
  25. dev[2].request("REMOVE_NETWORK all")
  26. dev[2].wait_disconnected()
  27. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  28. bgscan="simple")
  29. dev[2].request("REMOVE_NETWORK all")
  30. dev[2].wait_disconnected()
  31. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  32. bgscan="simple:1")
  33. dev[2].request("REMOVE_NETWORK all")
  34. dev[2].wait_disconnected()
  35. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  36. if ev is None:
  37. raise Exception("dev0 did not indicate signal change event")
  38. if "above=0" not in ev:
  39. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  40. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  41. if ev is None:
  42. raise Exception("dev1 did not indicate signal change event")
  43. if "above=1" not in ev:
  44. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  45. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  46. if ev is None:
  47. raise Exception("dev0 did not start a scan")
  48. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  49. if ev is None:
  50. raise Exception("dev1 did not start a scan")
  51. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  52. if ev is None:
  53. raise Exception("dev0 did not complete a scan")
  54. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  55. if ev is None:
  56. raise Exception("dev1 did not complete a scan")
  57. def test_bgscan_learn(dev, apdev):
  58. """bgscan_learn"""
  59. hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  60. hostapd.add_ap(apdev[1], { "ssid": "bgscan" })
  61. try:
  62. os.remove("/tmp/test_bgscan_learn.bgscan")
  63. except:
  64. pass
  65. try:
  66. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  67. bgscan="learn:1:-20:2")
  68. id = dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  69. bgscan="learn:1:-45:2:/tmp/test_bgscan_learn.bgscan")
  70. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  71. bgscan="learn:1:-45")
  72. dev[2].request("REMOVE_NETWORK all")
  73. dev[2].wait_disconnected()
  74. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  75. bgscan="learn:0:0")
  76. dev[2].request("REMOVE_NETWORK all")
  77. dev[2].wait_disconnected()
  78. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  79. bgscan="learn")
  80. dev[2].request("REMOVE_NETWORK all")
  81. dev[2].wait_disconnected()
  82. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  83. bgscan="learn:1")
  84. dev[2].request("REMOVE_NETWORK all")
  85. dev[2].wait_disconnected()
  86. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  87. if ev is None:
  88. raise Exception("dev0 did not indicate signal change event")
  89. if "above=0" not in ev:
  90. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  91. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  92. if ev is None:
  93. raise Exception("dev1 did not indicate signal change event")
  94. if "above=1" not in ev:
  95. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  96. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  97. if ev is None:
  98. raise Exception("dev0 did not start a scan")
  99. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  100. if ev is None:
  101. raise Exception("dev1 did not start a scan")
  102. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  103. if ev is None:
  104. raise Exception("dev0 did not complete a scan")
  105. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  106. if ev is None:
  107. raise Exception("dev1 did not complete a scan")
  108. dev[0].request("DISCONNECT")
  109. dev[1].request("DISCONNECT")
  110. dev[0].request("REMOVE_NETWORK all")
  111. with open("/tmp/test_bgscan_learn.bgscan", "r") as f:
  112. lines = f.read().splitlines()
  113. if lines[0] != "wpa_supplicant-bgscan-learn":
  114. raise Exception("Unexpected bgscan header line")
  115. if 'BSS 02:00:00:00:03:00 2412' not in lines:
  116. raise Exception("Missing BSS1")
  117. if 'BSS 02:00:00:00:04:00 2412' not in lines:
  118. raise Exception("Missing BSS2")
  119. if 'NEIGHBOR 02:00:00:00:03:00 02:00:00:00:04:00' not in lines:
  120. raise Exception("Missing BSS1->BSS2 neighbor entry")
  121. if 'NEIGHBOR 02:00:00:00:04:00 02:00:00:00:03:00' not in lines:
  122. raise Exception("Missing BSS2->BSS1 neighbor entry")
  123. dev[1].set_network(id, "scan_freq", "")
  124. dev[1].connect_network(id)
  125. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=10)
  126. if ev is None:
  127. raise Exception("dev1 did not start a scan")
  128. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  129. if ev is None:
  130. raise Exception("dev1 did not complete a scan")
  131. dev[1].request("REMOVE_NETWORK all")
  132. finally:
  133. try:
  134. os.remove("/tmp/test_bgscan_learn.bgscan")
  135. except:
  136. pass