test_dfs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #TODO: need to fix hwsim for DFS?!
  73. #dev[0].connect("dfs", key_mgmt="NONE")
  74. finally:
  75. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  76. def test_dfs_radar(dev, apdev):
  77. """DFS CAC functionality with radar detected"""
  78. if not os.path.exists("dfs"):
  79. return "skip"
  80. try:
  81. hapd = start_dfs_ap(apdev[0])
  82. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  83. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 70)
  84. if "freq=5260" not in ev:
  85. raise Exception("Unexpected DFS radar detection freq")
  86. state = hapd.get_status_field("state")
  87. if state != "DFS":
  88. raise Exception("Unexpected interface state")
  89. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  90. if "freq=5260" in ev:
  91. raise Exception("Unexpected DFS new freq")
  92. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  93. if "DFS-CAC-START" not in ev:
  94. raise Exception("Unexpected DFS event")
  95. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  96. if "success=1" not in ev:
  97. raise Exception("CAC failed")
  98. if "freq=5260" in ev:
  99. raise Exception("Unexpected DFS freq result - radar channel")
  100. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  101. if not ev:
  102. raise Exception("AP setup timed out")
  103. state = hapd.get_status_field("state")
  104. if state != "ENABLED":
  105. raise Exception("Unexpected interface state")
  106. freq = hapd.get_status_field("freq")
  107. if freq != "5260":
  108. raise Exception("Unexpected frequency")
  109. #TODO: need to fix hwsim for DFS?!
  110. #dev[0].connect("dfs", key_mgmt="NONE")
  111. finally:
  112. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  113. def test_dfs_radar_on_non_dfs_channel(dev, apdev):
  114. """DFS radar detection test code on non-DFS channel"""
  115. params = { "ssid": "radar" }
  116. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  117. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  118. hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")