test_ap_dynamic.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/usr/bin/python
  2. #
  3. # Test cases for dynamic BSS changes with hostapd
  4. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger()
  12. import hwsim_utils
  13. import hostapd
  14. def test_ap_change_ssid(dev, apdev):
  15. """Dynamic SSID change with hostapd and WPA2-PSK"""
  16. params = hostapd.wpa2_params(ssid="test-wpa2-psk-start",
  17. passphrase="12345678")
  18. hostapd.add_ap(apdev[0]['ifname'], params)
  19. id = dev[0].connect("test-wpa2-psk-start", psk="12345678",
  20. scan_freq="2412")
  21. dev[0].request("DISCONNECT")
  22. logger.info("Change SSID dynamically")
  23. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  24. res = hapd.request("SET ssid test-wpa2-psk-new")
  25. if "OK" not in res:
  26. raise Exception("SET command failed")
  27. res = hapd.request("RELOAD")
  28. if "OK" not in res:
  29. raise Exception("RELOAD command failed")
  30. dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
  31. dev[0].connect_network(id)
  32. def multi_check(dev, check):
  33. id = []
  34. num_bss = len(check)
  35. for i in range(0, num_bss):
  36. dev[i].request("BSS_FLUSH 0")
  37. dev[i].dump_monitor()
  38. id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
  39. scan_freq="2412", wait_connect=check[i]))
  40. for i in range(0, num_bss):
  41. if not check[i]:
  42. ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.2)
  43. if ev:
  44. raise Exception("Unexpected connection")
  45. for i in range(0, num_bss):
  46. dev[i].remove_network(id[i])
  47. time.sleep(0.3)
  48. res = ''
  49. for i in range(0, num_bss):
  50. res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
  51. for i in range(0, num_bss):
  52. if not check[i]:
  53. bssid = '02:00:00:00:03:0' + str(i)
  54. if bssid in res:
  55. raise Exception("Unexpected BSS" + str(i) + " in scan results")
  56. def test_ap_bss_add_remove(dev, apdev):
  57. """Dynamic BSS add/remove operations with hostapd"""
  58. for d in dev:
  59. d.request("SET ignore_old_scan_res 1")
  60. ifname1 = apdev[0]['ifname']
  61. ifname2 = apdev[0]['ifname'] + '-2'
  62. ifname3 = apdev[0]['ifname'] + '-3'
  63. logger.info("Set up three BSSes one by one")
  64. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  65. multi_check(dev, [ True, False, False ])
  66. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  67. multi_check(dev, [ True, True, False ])
  68. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  69. multi_check(dev, [ True, True, True ])
  70. logger.info("Remove the last BSS and re-add it")
  71. hostapd.remove_bss(ifname3)
  72. multi_check(dev, [ True, True, False ])
  73. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  74. multi_check(dev, [ True, True, True ])
  75. logger.info("Remove the middle BSS and re-add it")
  76. hostapd.remove_bss(ifname2)
  77. multi_check(dev, [ True, False, True ])
  78. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  79. multi_check(dev, [ True, True, True ])
  80. logger.info("Remove the first BSS and re-add it and other BSSs")
  81. hostapd.remove_bss(ifname1)
  82. multi_check(dev, [ False, False, False ])
  83. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  84. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  85. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  86. multi_check(dev, [ True, True, True ])
  87. logger.info("Remove two BSSes and re-add them")
  88. hostapd.remove_bss(ifname2)
  89. multi_check(dev, [ True, False, True ])
  90. hostapd.remove_bss(ifname3)
  91. multi_check(dev, [ True, False, False ])
  92. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  93. multi_check(dev, [ True, True, False ])
  94. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  95. multi_check(dev, [ True, True, True ])
  96. logger.info("Remove three BSSes in and re-add them")
  97. hostapd.remove_bss(ifname3)
  98. multi_check(dev, [ True, True, False ])
  99. hostapd.remove_bss(ifname2)
  100. multi_check(dev, [ True, False, False ])
  101. hostapd.remove_bss(ifname1)
  102. multi_check(dev, [ False, False, False ])
  103. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  104. multi_check(dev, [ True, False, False ])
  105. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  106. multi_check(dev, [ True, True, False ])
  107. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  108. multi_check(dev, [ True, True, True ])
  109. logger.info("Test error handling if a duplicate ifname is tried")
  110. hostapd.add_bss('phy3', ifname3, 'bss-3.conf', ignore_error=True)
  111. multi_check(dev, [ True, True, True ])
  112. def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
  113. """Dynamic BSS add during HT40 co-ex scan"""
  114. for d in dev:
  115. d.request("SET ignore_old_scan_res 1")
  116. ifname1 = apdev[0]['ifname']
  117. ifname2 = apdev[0]['ifname'] + '-2'
  118. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  119. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  120. multi_check(dev, [ True, True ])
  121. hostapd.remove_bss(ifname2)
  122. hostapd.remove_bss(ifname1)
  123. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  124. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  125. hostapd.remove_bss(ifname2)
  126. multi_check(dev, [ True, False ])
  127. hostapd.remove_bss(ifname1)
  128. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  129. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  130. hostapd.remove_bss(ifname1)
  131. multi_check(dev, [ False, False ])
  132. def test_ap_multi_bss_config(dev, apdev):
  133. """hostapd start with a multi-BSS configuration file"""
  134. for d in dev:
  135. d.request("SET ignore_old_scan_res 1")
  136. ifname1 = apdev[0]['ifname']
  137. ifname2 = apdev[0]['ifname'] + '-2'
  138. ifname3 = apdev[0]['ifname'] + '-3'
  139. logger.info("Set up three BSSes with one configuration file")
  140. hostapd.add_iface(ifname1, 'multi-bss.conf')
  141. hapd = hostapd.Hostapd(ifname1)
  142. hapd.enable()
  143. multi_check(dev, [ True, True, True ])
  144. hostapd.remove_bss(ifname2)
  145. multi_check(dev, [ True, False, True ])
  146. hostapd.remove_bss(ifname3)
  147. multi_check(dev, [ True, False, False ])
  148. hostapd.remove_bss(ifname1)
  149. multi_check(dev, [ False, False, False ])
  150. hostapd.add_iface(ifname1, 'multi-bss.conf')
  151. hapd = hostapd.Hostapd(ifname1)
  152. hapd.enable()
  153. hostapd.remove_bss(ifname1)
  154. multi_check(dev, [ False, False, False ])
  155. def invalid_ap(hapd_global, ifname):
  156. logger.info("Trying to start AP " + ifname + " with invalid configuration")
  157. hapd_global.remove(ifname)
  158. hapd_global.add(ifname)
  159. hapd = hostapd.Hostapd(ifname)
  160. if not hapd.ping():
  161. raise Exception("Could not ping hostapd")
  162. hapd.set_defaults()
  163. hapd.set("ssid", "invalid-config")
  164. hapd.set("channel", "12345")
  165. try:
  166. hapd.enable()
  167. started = True
  168. except Exception, e:
  169. started = False
  170. if started:
  171. raise Exception("ENABLE command succeeded unexpectedly")
  172. return hapd
  173. def test_ap_invalid_config(dev, apdev):
  174. """Try to start AP with invalid configuration and fix configuration"""
  175. hapd_global = hostapd.HostapdGlobal()
  176. ifname = apdev[0]['ifname']
  177. hapd = invalid_ap(hapd_global, ifname)
  178. logger.info("Fix configuration and start AP again")
  179. hapd.set("channel", "1")
  180. hapd.enable()
  181. dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
  182. def test_ap_invalid_config2(dev, apdev):
  183. """Try to start AP with invalid configuration and remove interface"""
  184. hapd_global = hostapd.HostapdGlobal()
  185. ifname = apdev[0]['ifname']
  186. hapd = invalid_ap(hapd_global, ifname)
  187. logger.info("Remove interface with failed configuration")
  188. hapd_global.remove(ifname)
  189. def test_ap_remove_during_acs(dev, apdev):
  190. """Remove interface during ACS"""
  191. params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
  192. params['channel'] = '0'
  193. ifname = apdev[0]['ifname']
  194. hapd = hostapd.HostapdGlobal()
  195. hostapd.add_ap(ifname, params)
  196. hapd.remove(ifname)
  197. def test_ap_remove_during_acs2(dev, apdev):
  198. """Remove BSS during ACS in multi-BSS configuration"""
  199. ifname = apdev[0]['ifname']
  200. ifname2 = ifname + "-2"
  201. hapd_global = hostapd.HostapdGlobal()
  202. hapd_global.add(ifname)
  203. hapd = hostapd.Hostapd(ifname)
  204. hapd.set_defaults()
  205. hapd.set("ssid", "test-acs-remove")
  206. hapd.set("channel", "0")
  207. hapd.set("bss", ifname2)
  208. hapd.set("ssid", "test-acs-remove2")
  209. hapd.enable()
  210. hapd_global.remove(ifname)
  211. def test_ap_remove_during_acs3(dev, apdev):
  212. """Remove second BSS during ACS in multi-BSS configuration"""
  213. ifname = apdev[0]['ifname']
  214. ifname2 = ifname + "-2"
  215. hapd_global = hostapd.HostapdGlobal()
  216. hapd_global.add(ifname)
  217. hapd = hostapd.Hostapd(ifname)
  218. hapd.set_defaults()
  219. hapd.set("ssid", "test-acs-remove")
  220. hapd.set("channel", "0")
  221. hapd.set("bss", ifname2)
  222. hapd.set("ssid", "test-acs-remove2")
  223. hapd.enable()
  224. hapd_global.remove(ifname2)
  225. def test_ap_remove_during_ht_coex_scan(dev, apdev):
  226. """Remove interface during HT co-ex scan"""
  227. params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
  228. params['channel'] = '1'
  229. params['ht_capab'] = "[HT40+]"
  230. ifname = apdev[0]['ifname']
  231. hapd = hostapd.HostapdGlobal()
  232. hostapd.add_ap(ifname, params)
  233. hapd.remove(ifname)
  234. def test_ap_remove_during_ht_coex_scan2(dev, apdev):
  235. """Remove BSS during HT co-ex scan in multi-BSS configuration"""
  236. ifname = apdev[0]['ifname']
  237. ifname2 = ifname + "-2"
  238. hapd_global = hostapd.HostapdGlobal()
  239. hapd_global.add(ifname)
  240. hapd = hostapd.Hostapd(ifname)
  241. hapd.set_defaults()
  242. hapd.set("ssid", "test-ht-remove")
  243. hapd.set("channel", "1")
  244. hapd.set("ht_capab", "[HT40+]")
  245. hapd.set("bss", ifname2)
  246. hapd.set("ssid", "test-ht-remove2")
  247. hapd.enable()
  248. hapd_global.remove(ifname)
  249. def test_ap_remove_during_ht_coex_scan3(dev, apdev):
  250. """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
  251. ifname = apdev[0]['ifname']
  252. ifname2 = ifname + "-2"
  253. hapd_global = hostapd.HostapdGlobal()
  254. hapd_global.add(ifname)
  255. hapd = hostapd.Hostapd(ifname)
  256. hapd.set_defaults()
  257. hapd.set("ssid", "test-ht-remove")
  258. hapd.set("channel", "1")
  259. hapd.set("ht_capab", "[HT40+]")
  260. hapd.set("bss", ifname2)
  261. hapd.set("ssid", "test-ht-remove2")
  262. hapd.enable()
  263. hapd_global.remove(ifname2)