test_dfs.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/python
  2. #
  3. # Test cases for DFS
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import os
  9. import subprocess
  10. import time
  11. import logging
  12. logger = logging.getLogger()
  13. import hwsim_utils
  14. import hostapd
  15. def wait_dfs_event(hapd, event, timeout):
  16. dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
  17. "DFS-CAC-START", "DFS-CAC-COMPLETED",
  18. "DFS-NOP-FINISHED", "AP-ENABLED" ]
  19. ev = hapd.wait_event(dfs_events, timeout=timeout)
  20. if not ev:
  21. raise Exception("DFS event timed out")
  22. if event not in ev:
  23. raise Exception("Unexpected DFS event")
  24. return ev
  25. def start_dfs_ap(ap):
  26. ifname = ap['ifname']
  27. logger.info("Reset regulatory setup")
  28. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  29. time.sleep(1)
  30. subprocess.call(['sudo', 'iw', 'reg', 'set', 'FI'])
  31. logger.info("Starting AP " + ifname + " on DFS channel")
  32. hapd_global = hostapd.HostapdGlobal()
  33. hapd_global.remove(ifname)
  34. hapd_global.add(ifname)
  35. hapd = hostapd.Hostapd(ifname)
  36. if not hapd.ping():
  37. raise Exception("Could not ping hostapd")
  38. hapd.set_defaults()
  39. hapd.set("ssid", "dfs")
  40. hapd.set("country_code", "FI")
  41. hapd.set("ieee80211d", "1")
  42. hapd.set("ieee80211h", "1")
  43. hapd.set("hw_mode", "a")
  44. hapd.set("channel", "52")
  45. hapd.enable()
  46. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  47. if "DFS-CAC-START" not in ev:
  48. raise Exception("Unexpected DFS event")
  49. state = hapd.get_status_field("state")
  50. if state != "DFS":
  51. raise Exception("Unexpected interface state")
  52. return hapd
  53. def test_dfs(dev, apdev):
  54. """DFS CAC functionality on clear channel"""
  55. if not os.path.exists("dfs"):
  56. return "skip"
  57. hapd = start_dfs_ap(apdev[0])
  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. def test_dfs_radar(dev, apdev):
  75. """DFS CAC functionality with radar detected"""
  76. if not os.path.exists("dfs"):
  77. return "skip"
  78. hapd = start_dfs_ap(apdev[0])
  79. hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
  80. ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 70)
  81. if "freq=5260" not in ev:
  82. raise Exception("Unexpected DFS radar detection freq")
  83. state = hapd.get_status_field("state")
  84. if state != "DFS":
  85. raise Exception("Unexpected interface state")
  86. ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
  87. if "freq=5260" in ev:
  88. raise Exception("Unexpected DFS new freq")
  89. ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
  90. if "DFS-CAC-START" not in ev:
  91. raise Exception("Unexpected DFS event")
  92. ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
  93. if "success=1" not in ev:
  94. raise Exception("CAC failed")
  95. if "freq=5260" in ev:
  96. raise Exception("Unexpected DFS freq result - radar channel")
  97. ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
  98. if not ev:
  99. raise Exception("AP setup timed out")
  100. state = hapd.get_status_field("state")
  101. if state != "ENABLED":
  102. raise Exception("Unexpected interface state")
  103. freq = hapd.get_status_field("freq")
  104. if freq != "5260":
  105. raise Exception("Unexpected frequency")
  106. #TODO: need to fix hwsim for DFS?!
  107. #dev[0].connect("dfs", key_mgmt="NONE")