test_ap_acs.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Test cases for automatic channel selection with hostapd
  2. # Copyright (c) 2013-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 logging
  7. logger = logging.getLogger()
  8. import subprocess
  9. import time
  10. import hostapd
  11. def wait_acs(hapd):
  12. ev = hapd.wait_event(["ACS-STARTED", "ACS-COMPLETED", "ACS-FAILED",
  13. "AP-ENABLED", "AP-DISABLED"], timeout=5)
  14. if not ev:
  15. raise Exception("ACS start timed out")
  16. if "ACS-STARTED" not in ev:
  17. raise Exception("Unexpected ACS event: " + ev)
  18. state = hapd.get_status_field("state")
  19. if state != "ACS":
  20. raise Exception("Unexpected interface state")
  21. ev = hapd.wait_event(["ACS-COMPLETED", "ACS-FAILED", "AP-ENABLED",
  22. "AP-DISABLED"], timeout=20)
  23. if not ev:
  24. raise Exception("ACS timed out")
  25. if "ACS-COMPLETED" not in ev:
  26. raise Exception("Unexpected ACS event: " + ev)
  27. ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=5)
  28. if not ev:
  29. raise Exception("AP setup timed out")
  30. if "AP-ENABLED" not in ev:
  31. raise Exception("Unexpected ACS event: " + ev)
  32. state = hapd.get_status_field("state")
  33. if state != "ENABLED":
  34. raise Exception("Unexpected interface state")
  35. def test_ap_acs(dev, apdev):
  36. """Automatic channel selection"""
  37. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  38. params['channel'] = '0'
  39. hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
  40. wait_acs(hapd)
  41. freq = hapd.get_status_field("freq")
  42. if int(freq) < 2400:
  43. raise Exception("Unexpected frequency")
  44. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  45. def test_ap_multi_bss_acs(dev, apdev):
  46. """hostapd start with a multi-BSS configuration file using ACS"""
  47. ifname = apdev[0]['ifname']
  48. # make sure the current channel is on 2.4 GHz band as a workaround for the
  49. # limited survey functionality in mac80211_hwsim
  50. hostapd.add_ap(ifname, { "ssid": "test" })
  51. time.sleep(0.1)
  52. hapd_global = hostapd.HostapdGlobal()
  53. hapd_global.remove(ifname)
  54. # start the actual test
  55. hostapd.add_iface(ifname, 'multi-bss-acs.conf')
  56. hapd = hostapd.Hostapd(ifname)
  57. hapd.enable()
  58. wait_acs(hapd)
  59. freq = hapd.get_status_field("freq")
  60. if int(freq) < 2400:
  61. raise Exception("Unexpected frequency")
  62. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq=freq)
  63. dev[1].connect("bss-2", psk="12345678", scan_freq=freq)
  64. dev[2].connect("bss-3", psk="qwertyuiop", scan_freq=freq)
  65. def test_ap_acs_40mhz(dev, apdev):
  66. """Automatic channel selection for 40 MHz channel"""
  67. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  68. params['channel'] = '0'
  69. params['ht_capab'] = '[HT40+]'
  70. hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
  71. wait_acs(hapd)
  72. freq = hapd.get_status_field("freq")
  73. if int(freq) < 2400:
  74. raise Exception("Unexpected frequency")
  75. sec = hapd.get_status_field("secondary_channel")
  76. if int(sec) == 0:
  77. raise Exception("Secondary channel not set")
  78. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  79. def test_ap_acs_5ghz(dev, apdev):
  80. """Automatic channel selection on 5 GHz"""
  81. try:
  82. hapd = None
  83. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  84. params['hw_mode'] = 'a'
  85. params['channel'] = '0'
  86. params['country_code'] = 'US'
  87. hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
  88. # TODO: Remove exception acceptance once mac80211_hwsim supports ACS on
  89. # 5 GHz
  90. run = False
  91. try:
  92. wait_acs(hapd)
  93. run = True
  94. except Exception, e:
  95. logger.info("Ignore exception due to missing hwsim support: " + str(e))
  96. if run:
  97. freq = hapd.get_status_field("freq")
  98. if int(freq) < 5000:
  99. raise Exception("Unexpected frequency")
  100. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  101. finally:
  102. dev[0].request("DISCONNECT")
  103. if hapd:
  104. hapd.request("DISABLE")
  105. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  106. dev[0].flush_scan_cache()
  107. def test_ap_acs_5ghz_40mhz(dev, apdev):
  108. """Automatic channel selection on 5 GHz for 40 MHz channel"""
  109. try:
  110. hapd = None
  111. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  112. params['hw_mode'] = 'a'
  113. params['channel'] = '0'
  114. params['ht_capab'] = '[HT40+]'
  115. params['country_code'] = 'US'
  116. hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
  117. # TODO: Remove exception acceptance once mac80211_hwsim supports ACS on
  118. # 5 GHz
  119. run = False
  120. try:
  121. wait_acs(hapd)
  122. run = True
  123. except Exception, e:
  124. logger.info("Ignore exception due to missing hwsim support: " + str(e))
  125. if run:
  126. freq = hapd.get_status_field("freq")
  127. if int(freq) < 5000:
  128. raise Exception("Unexpected frequency")
  129. sec = hapd.get_status_field("secondary_channel")
  130. if int(sec) == 0:
  131. raise Exception("Secondary channel not set")
  132. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  133. finally:
  134. dev[0].request("DISCONNECT")
  135. if hapd:
  136. hapd.request("DISABLE")
  137. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  138. dev[0].flush_scan_cache()
  139. def test_ap_acs_vht(dev, apdev):
  140. """Automatic channel selection for VHT"""
  141. try:
  142. hapd = None
  143. params = hostapd.wpa2_params(ssid="test-acs", passphrase="12345678")
  144. params['hw_mode'] = 'a'
  145. params['channel'] = '0'
  146. params['ht_capab'] = '[HT40+]'
  147. params['country_code'] = 'US'
  148. params['ieee80211ac'] = '1'
  149. params['vht_oper_chwidth'] = '1'
  150. hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
  151. # TODO: Remove exception acceptance once mac80211_hwsim supports ACS on
  152. # 5 GHz
  153. run = False
  154. try:
  155. wait_acs(hapd)
  156. run = True
  157. except Exception, e:
  158. logger.info("Ignore exception due to missing hwsim support: " + str(e))
  159. if run:
  160. freq = hapd.get_status_field("freq")
  161. if int(freq) < 5000:
  162. raise Exception("Unexpected frequency")
  163. sec = hapd.get_status_field("secondary_channel")
  164. if int(sec) == 0:
  165. raise Exception("Secondary channel not set")
  166. dev[0].connect("test-acs", psk="12345678", scan_freq=freq)
  167. finally:
  168. dev[0].request("DISCONNECT")
  169. if hapd:
  170. hapd.request("DISABLE")
  171. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  172. dev[0].flush_scan_cache()