test_dfs.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Test cases for DFS
  2. # Copyright (c) 2013, 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 subprocess
  8. import time
  9. import logging
  10. logger = logging.getLogger()
  11. import hwsim_utils
  12. import hostapd
  13. def wait_dfs_event(hapd, event, timeout):
  14. dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
  15. "DFS-CAC-START", "DFS-CAC-COMPLETED",
  16. "DFS-NOP-FINISHED", "AP-ENABLED" ]
  17. ev = hapd.wait_event(dfs_events, timeout=timeout)
  18. if not ev:
  19. raise Exception("DFS event timed out")
  20. if event not in ev:
  21. raise Exception("Unexpected DFS event")
  22. return ev
  23. def start_dfs_ap(ap, allow_failure=False):
  24. ifname = ap['ifname']
  25. logger.info("Starting AP " + ifname + " on DFS channel")
  26. hapd_global = hostapd.HostapdGlobal()
  27. hapd_global.remove(ifname)
  28. hapd_global.add(ifname)
  29. hapd = hostapd.Hostapd(ifname)
  30. if not hapd.ping():
  31. raise Exception("Could not ping hostapd")
  32. hapd.set_defaults()
  33. hapd.set("ssid", "dfs")
  34. hapd.set("country_code", "FI")
  35. hapd.set("ieee80211d", "1")
  36. hapd.set("ieee80211h", "1")
  37. hapd.set("hw_mode", "a")
  38. hapd.set("channel", "52")
  39. hapd.enable()
  40. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  41. if "DFS-CAC-START" not in ev:
  42. raise Exception("Unexpected DFS event")
  43. state = hapd.get_status_field("state")
  44. if state != "DFS":
  45. if allow_failure:
  46. logger.info("Interface state not DFS: " + state)
  47. return None
  48. raise Exception("Unexpected interface state: " + state)
  49. return hapd
  50. def test_dfs(dev, apdev):
  51. """DFS CAC functionality on clear channel"""
  52. try:
  53. hapd = start_dfs_ap(apdev[0], allow_failure=True)
  54. if hapd is None:
  55. if not os.path.exists("dfs"):
  56. return "skip"
  57. raise Exception("Failed to start DFS AP")
  58. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  59. if "success=1" not in ev:
  60. raise Exception("CAC failed")
  61. if "freq=5260" not in ev:
  62. raise Exception("Unexpected DFS freq result")
  63. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  64. if not ev:
  65. raise Exception("AP setup timed out")
  66. state = hapd.get_status_field("state")
  67. if state != "ENABLED":
  68. raise Exception("Unexpected interface state")
  69. freq = hapd.get_status_field("freq")
  70. if freq != "5260":
  71. raise Exception("Unexpected frequency")
  72. dev[0].connect("dfs", key_mgmt="NONE")
  73. hwsim_utils.test_connectivity(dev[0].ifname, apdev[0]['ifname'])
  74. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  75. ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
  76. if ev is None:
  77. raise Exception("DFS-RADAR-DETECTED event not reported")
  78. if "freq=5260" not in ev:
  79. raise Exception("Incorrect frequency in radar detected event: " + ev);
  80. ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
  81. if ev is None:
  82. raise Exception("DFS-NEW-CHANNEL event not reported")
  83. if "freq=5260" in ev:
  84. raise Exception("Channel did not change after radar was detected");
  85. ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
  86. if ev is None:
  87. raise Exception("AP-CSA-FINISHED event not reported")
  88. if "freq=5260" in ev:
  89. raise Exception("Channel did not change after radar was detected(2)");
  90. time.sleep(1)
  91. hwsim_utils.test_connectivity(dev[0].ifname, apdev[0]['ifname'])
  92. finally:
  93. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  94. def test_dfs_radar(dev, apdev):
  95. """DFS CAC functionality with radar detected"""
  96. if not os.path.exists("dfs"):
  97. return "skip"
  98. try:
  99. hapd = start_dfs_ap(apdev[0])
  100. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  101. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 70)
  102. if "freq=5260" not in ev:
  103. raise Exception("Unexpected DFS radar detection freq")
  104. state = hapd.get_status_field("state")
  105. if state != "DFS":
  106. raise Exception("Unexpected interface state")
  107. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  108. if "freq=5260" in ev:
  109. raise Exception("Unexpected DFS new freq")
  110. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  111. if "DFS-CAC-START" not in ev:
  112. raise Exception("Unexpected DFS event")
  113. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  114. if "success=1" not in ev:
  115. raise Exception("CAC failed")
  116. if "freq=5260" in ev:
  117. raise Exception("Unexpected DFS freq result - radar channel")
  118. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  119. if not ev:
  120. raise Exception("AP setup timed out")
  121. state = hapd.get_status_field("state")
  122. if state != "ENABLED":
  123. raise Exception("Unexpected interface state")
  124. freq = hapd.get_status_field("freq")
  125. if freq != "5260":
  126. raise Exception("Unexpected frequency")
  127. dev[0].connect("dfs", key_mgmt="NONE")
  128. finally:
  129. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  130. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  131. """DFS radar detection test code on non-DFS channel"""
  132. params = { "ssid": "radar" }
  133. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  134. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  135. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")