test_ap_dynamic.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. ifname1 = apdev[0]['ifname']
  59. ifname2 = apdev[0]['ifname'] + '-2'
  60. ifname3 = apdev[0]['ifname'] + '-3'
  61. logger.info("Set up three BSSes one by one")
  62. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  63. multi_check(dev, [ True, False, False ])
  64. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  65. multi_check(dev, [ True, True, False ])
  66. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  67. multi_check(dev, [ True, True, True ])
  68. logger.info("Remove the last BSS and re-add it")
  69. hostapd.remove_bss(ifname3)
  70. multi_check(dev, [ True, True, False ])
  71. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  72. multi_check(dev, [ True, True, True ])
  73. logger.info("Remove the middle BSS and re-add it")
  74. hostapd.remove_bss(ifname2)
  75. multi_check(dev, [ True, False, True ])
  76. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  77. multi_check(dev, [ True, True, True ])
  78. logger.info("Remove the first BSS and re-add it and other BSSs")
  79. hostapd.remove_bss(ifname1)
  80. multi_check(dev, [ False, False, False ])
  81. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  82. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  83. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  84. multi_check(dev, [ True, True, True ])
  85. logger.info("Remove two BSSes and re-add them")
  86. hostapd.remove_bss(ifname2)
  87. multi_check(dev, [ True, False, True ])
  88. hostapd.remove_bss(ifname3)
  89. multi_check(dev, [ True, False, False ])
  90. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  91. multi_check(dev, [ True, True, False ])
  92. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  93. multi_check(dev, [ True, True, True ])
  94. logger.info("Remove three BSSes in and re-add them")
  95. hostapd.remove_bss(ifname3)
  96. multi_check(dev, [ True, True, False ])
  97. hostapd.remove_bss(ifname2)
  98. multi_check(dev, [ True, False, False ])
  99. hostapd.remove_bss(ifname1)
  100. multi_check(dev, [ False, False, False ])
  101. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  102. multi_check(dev, [ True, False, False ])
  103. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  104. multi_check(dev, [ True, True, False ])
  105. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  106. multi_check(dev, [ True, True, True ])
  107. logger.info("Test error handling if a duplicate ifname is tried")
  108. hostapd.add_bss('phy3', ifname3, 'bss-3.conf', ignore_error=True)
  109. multi_check(dev, [ True, True, True ])
  110. def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
  111. """Dynamic BSS add during HT40 co-ex scan"""
  112. ifname1 = apdev[0]['ifname']
  113. ifname2 = apdev[0]['ifname'] + '-2'
  114. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  115. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  116. multi_check(dev, [ True, True ])
  117. hostapd.remove_bss(ifname2)
  118. hostapd.remove_bss(ifname1)
  119. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  120. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  121. hostapd.remove_bss(ifname2)
  122. multi_check(dev, [ True, False ])
  123. hostapd.remove_bss(ifname1)
  124. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  125. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  126. hostapd.remove_bss(ifname1)
  127. multi_check(dev, [ False, False ])
  128. def test_ap_multi_bss_config(dev, apdev):
  129. """hostapd start with a multi-BSS configuration file"""
  130. ifname1 = apdev[0]['ifname']
  131. ifname2 = apdev[0]['ifname'] + '-2'
  132. ifname3 = apdev[0]['ifname'] + '-3'
  133. logger.info("Set up three BSSes with one configuration file")
  134. hostapd.add_iface(ifname1, 'multi-bss.conf')
  135. hapd = hostapd.Hostapd(ifname1)
  136. hapd.enable()
  137. multi_check(dev, [ True, True, True ])
  138. hostapd.remove_bss(ifname2)
  139. multi_check(dev, [ True, False, True ])
  140. hostapd.remove_bss(ifname3)
  141. multi_check(dev, [ True, False, False ])
  142. hostapd.remove_bss(ifname1)
  143. multi_check(dev, [ False, False, False ])
  144. hostapd.add_iface(ifname1, 'multi-bss.conf')
  145. hapd = hostapd.Hostapd(ifname1)
  146. hapd.enable()
  147. hostapd.remove_bss(ifname1)
  148. multi_check(dev, [ False, False, False ])
  149. def invalid_ap(hapd_global, ifname):
  150. logger.info("Trying to start AP " + ifname + " with invalid configuration")
  151. hapd_global.remove(ifname)
  152. hapd_global.add(ifname)
  153. hapd = hostapd.Hostapd(ifname)
  154. if not hapd.ping():
  155. raise Exception("Could not ping hostapd")
  156. hapd.set_defaults()
  157. hapd.set("ssid", "invalid-config")
  158. hapd.set("channel", "12345")
  159. try:
  160. hapd.enable()
  161. started = True
  162. except Exception, e:
  163. started = False
  164. if started:
  165. raise Exception("ENABLE command succeeded unexpectedly")
  166. return hapd
  167. def test_ap_invalid_config(dev, apdev):
  168. """Try to start AP with invalid configuration and fix configuration"""
  169. hapd_global = hostapd.HostapdGlobal()
  170. ifname = apdev[0]['ifname']
  171. hapd = invalid_ap(hapd_global, ifname)
  172. logger.info("Fix configuration and start AP again")
  173. hapd.set("channel", "1")
  174. hapd.enable()
  175. dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
  176. def test_ap_invalid_config2(dev, apdev):
  177. """Try to start AP with invalid configuration and remove interface"""
  178. hapd_global = hostapd.HostapdGlobal()
  179. ifname = apdev[0]['ifname']
  180. hapd = invalid_ap(hapd_global, ifname)
  181. logger.info("Remove interface with failed configuration")
  182. hapd_global.remove(ifname)
  183. def test_ap_remove_during_acs(dev, apdev):
  184. """Remove interface during ACS"""
  185. params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
  186. params['channel'] = '0'
  187. ifname = apdev[0]['ifname']
  188. hapd = hostapd.HostapdGlobal()
  189. hostapd.add_ap(ifname, params)
  190. hapd.remove(ifname)
  191. def test_ap_remove_during_acs2(dev, apdev):
  192. """Remove BSS during ACS in multi-BSS configuration"""
  193. ifname = apdev[0]['ifname']
  194. ifname2 = ifname + "-2"
  195. hapd_global = hostapd.HostapdGlobal()
  196. hapd_global.add(ifname)
  197. hapd = hostapd.Hostapd(ifname)
  198. hapd.set_defaults()
  199. hapd.set("ssid", "test-acs-remove")
  200. hapd.set("channel", "0")
  201. hapd.set("bss", ifname2)
  202. hapd.set("ssid", "test-acs-remove2")
  203. hapd.enable()
  204. hapd_global.remove(ifname)
  205. def test_ap_remove_during_acs3(dev, apdev):
  206. """Remove second BSS during ACS in multi-BSS configuration"""
  207. ifname = apdev[0]['ifname']
  208. ifname2 = ifname + "-2"
  209. hapd_global = hostapd.HostapdGlobal()
  210. hapd_global.add(ifname)
  211. hapd = hostapd.Hostapd(ifname)
  212. hapd.set_defaults()
  213. hapd.set("ssid", "test-acs-remove")
  214. hapd.set("channel", "0")
  215. hapd.set("bss", ifname2)
  216. hapd.set("ssid", "test-acs-remove2")
  217. hapd.enable()
  218. hapd_global.remove(ifname2)
  219. def test_ap_remove_during_ht_coex_scan(dev, apdev):
  220. """Remove interface during HT co-ex scan"""
  221. params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
  222. params['channel'] = '1'
  223. params['ht_capab'] = "[HT40+]"
  224. ifname = apdev[0]['ifname']
  225. hapd = hostapd.HostapdGlobal()
  226. hostapd.add_ap(ifname, params)
  227. hapd.remove(ifname)
  228. def test_ap_remove_during_ht_coex_scan2(dev, apdev):
  229. """Remove BSS during HT co-ex scan in multi-BSS configuration"""
  230. ifname = apdev[0]['ifname']
  231. ifname2 = ifname + "-2"
  232. hapd_global = hostapd.HostapdGlobal()
  233. hapd_global.add(ifname)
  234. hapd = hostapd.Hostapd(ifname)
  235. hapd.set_defaults()
  236. hapd.set("ssid", "test-ht-remove")
  237. hapd.set("channel", "1")
  238. hapd.set("ht_capab", "[HT40+]")
  239. hapd.set("bss", ifname2)
  240. hapd.set("ssid", "test-ht-remove2")
  241. hapd.enable()
  242. hapd_global.remove(ifname)
  243. def test_ap_remove_during_ht_coex_scan3(dev, apdev):
  244. """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
  245. ifname = apdev[0]['ifname']
  246. ifname2 = ifname + "-2"
  247. hapd_global = hostapd.HostapdGlobal()
  248. hapd_global.add(ifname)
  249. hapd = hostapd.Hostapd(ifname)
  250. hapd.set_defaults()
  251. hapd.set("ssid", "test-ht-remove")
  252. hapd.set("channel", "1")
  253. hapd.set("ht_capab", "[HT40+]")
  254. hapd.set("bss", ifname2)
  255. hapd.set("ssid", "test-ht-remove2")
  256. hapd.enable()
  257. hapd_global.remove(ifname2)