test_dfs.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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):
  24. ifname = ap['ifname']
  25. logger.info("Reset regulatory setup")
  26. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  27. time.sleep(1)
  28. subprocess.call(['sudo', 'iw', 'reg', 'set', 'FI'])
  29. logger.info("Starting AP " + ifname + " on DFS channel")
  30. hapd_global = hostapd.HostapdGlobal()
  31. hapd_global.remove(ifname)
  32. hapd_global.add(ifname)
  33. hapd = hostapd.Hostapd(ifname)
  34. if not hapd.ping():
  35. raise Exception("Could not ping hostapd")
  36. hapd.set_defaults()
  37. hapd.set("ssid", "dfs")
  38. hapd.set("country_code", "FI")
  39. hapd.set("ieee80211d", "1")
  40. hapd.set("ieee80211h", "1")
  41. hapd.set("hw_mode", "a")
  42. hapd.set("channel", "52")
  43. hapd.enable()
  44. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  45. if "DFS-CAC-START" not in ev:
  46. raise Exception("Unexpected DFS event")
  47. state = hapd.get_status_field("state")
  48. if state != "DFS":
  49. raise Exception("Unexpected interface state")
  50. return hapd
  51. def test_dfs(dev, apdev):
  52. """DFS CAC functionality on clear channel"""
  53. if not os.path.exists("dfs"):
  54. return "skip"
  55. hapd = start_dfs_ap(apdev[0])
  56. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  57. if "success=1" not in ev:
  58. raise Exception("CAC failed")
  59. if "freq=5260" not in ev:
  60. raise Exception("Unexpected DFS freq result")
  61. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  62. if not ev:
  63. raise Exception("AP setup timed out")
  64. state = hapd.get_status_field("state")
  65. if state != "ENABLED":
  66. raise Exception("Unexpected interface state")
  67. freq = hapd.get_status_field("freq")
  68. if freq != "5260":
  69. raise Exception("Unexpected frequency")
  70. #TODO: need to fix hwsim for DFS?!
  71. #dev[0].connect("dfs", key_mgmt="NONE")
  72. def test_dfs_radar(dev, apdev):
  73. """DFS CAC functionality with radar detected"""
  74. if not os.path.exists("dfs"):
  75. return "skip"
  76. hapd = start_dfs_ap(apdev[0])
  77. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  78. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 70)
  79. if "freq=5260" not in ev:
  80. raise Exception("Unexpected DFS radar detection freq")
  81. state = hapd.get_status_field("state")
  82. if state != "DFS":
  83. raise Exception("Unexpected interface state")
  84. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  85. if "freq=5260" in ev:
  86. raise Exception("Unexpected DFS new freq")
  87. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  88. if "DFS-CAC-START" not in ev:
  89. raise Exception("Unexpected DFS event")
  90. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  91. if "success=1" not in ev:
  92. raise Exception("CAC failed")
  93. if "freq=5260" in ev:
  94. raise Exception("Unexpected DFS freq result - radar channel")
  95. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  96. if not ev:
  97. raise Exception("AP setup timed out")
  98. state = hapd.get_status_field("state")
  99. if state != "ENABLED":
  100. raise Exception("Unexpected interface state")
  101. freq = hapd.get_status_field("freq")
  102. if freq != "5260":
  103. raise Exception("Unexpected frequency")
  104. #TODO: need to fix hwsim for DFS?!
  105. #dev[0].connect("dfs", key_mgmt="NONE")