test_ap_open.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Open mode AP tests
  2. # Copyright (c) 2014, Qualcomm Atheros, Inc.
  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 struct
  9. import subprocess
  10. import time
  11. import hostapd
  12. import hwsim_utils
  13. from utils import alloc_fail
  14. def test_ap_open(dev, apdev):
  15. """AP with open mode (no security) configuration"""
  16. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  17. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  18. bg_scan_period="0")
  19. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  20. if ev is None:
  21. raise Exception("No connection event received from hostapd")
  22. hwsim_utils.test_connectivity(dev[0], hapd)
  23. dev[0].request("DISCONNECT")
  24. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  25. if ev is None:
  26. raise Exception("No disconnection event received from hostapd")
  27. def test_ap_open_packet_loss(dev, apdev):
  28. """AP with open mode configuration and large packet loss"""
  29. params = { "ssid": "open",
  30. "ignore_probe_probability": "0.5",
  31. "ignore_auth_probability": "0.5",
  32. "ignore_assoc_probability": "0.5",
  33. "ignore_reassoc_probability": "0.5" }
  34. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  35. for i in range(0, 3):
  36. dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
  37. wait_connect=False)
  38. for i in range(0, 3):
  39. dev[i].wait_connected(timeout=20)
  40. def test_ap_open_unknown_action(dev, apdev):
  41. """AP with open mode configuration and unknown Action frame"""
  42. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  43. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  44. bssid = apdev[0]['bssid']
  45. cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
  46. if "FAIL" in dev[0].request(cmd):
  47. raise Exception("Could not send test Action frame")
  48. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  49. if ev is None:
  50. raise Exception("Timeout on MGMT-TX-STATUS")
  51. if "result=SUCCESS" not in ev:
  52. raise Exception("AP did not ack Action frame")
  53. def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
  54. """Reconnect to open mode AP after inactivity related disconnection"""
  55. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  56. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  57. hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
  58. dev[0].wait_disconnected(timeout=5)
  59. dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
  60. def test_ap_open_assoc_timeout(dev, apdev):
  61. """AP timing out association"""
  62. ssid = "test"
  63. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  64. dev[0].scan(freq="2412")
  65. hapd.set("ext_mgmt_frame_handling", "1")
  66. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  67. wait_connect=False)
  68. for i in range(0, 10):
  69. req = hapd.mgmt_rx()
  70. if req is None:
  71. raise Exception("MGMT RX wait timed out")
  72. if req['subtype'] == 11:
  73. break
  74. req = None
  75. if not req:
  76. raise Exception("Authentication frame not received")
  77. resp = {}
  78. resp['fc'] = req['fc']
  79. resp['da'] = req['sa']
  80. resp['sa'] = req['da']
  81. resp['bssid'] = req['bssid']
  82. resp['payload'] = struct.pack('<HHH', 0, 2, 0)
  83. hapd.mgmt_tx(resp)
  84. assoc = 0
  85. for i in range(0, 10):
  86. req = hapd.mgmt_rx()
  87. if req is None:
  88. raise Exception("MGMT RX wait timed out")
  89. if req['subtype'] == 0:
  90. assoc += 1
  91. if assoc == 3:
  92. break
  93. if assoc != 3:
  94. raise Exception("Association Request frames not received: assoc=%d" % assoc)
  95. hapd.set("ext_mgmt_frame_handling", "0")
  96. dev[0].wait_connected(timeout=15)
  97. def test_ap_open_id_str(dev, apdev):
  98. """AP with open mode and id_str"""
  99. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  100. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
  101. wait_connect=False)
  102. ev = dev[0].wait_connected(timeout=10)
  103. if "id_str=foo" not in ev:
  104. raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
  105. if dev[0].get_status_field("id_str") != "foo":
  106. raise Exception("id_str mismatch")
  107. def test_ap_open_select_any(dev, apdev):
  108. """AP with open mode and select any network"""
  109. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  110. id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
  111. only_add_network=True)
  112. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  113. only_add_network=True)
  114. dev[0].select_network(id)
  115. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  116. if ev is not None:
  117. raise Exception("Unexpected connection")
  118. dev[0].select_network("any")
  119. dev[0].wait_connected(timeout=10)
  120. def test_ap_open_unexpected_assoc_event(dev, apdev):
  121. """AP with open mode and unexpected association event"""
  122. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  123. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  124. dev[0].request("DISCONNECT")
  125. dev[0].wait_disconnected(timeout=15)
  126. dev[0].dump_monitor()
  127. # This will be accepted due to matching network
  128. subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  129. apdev[0]['bssid']])
  130. dev[0].wait_connected(timeout=15)
  131. dev[0].dump_monitor()
  132. dev[0].request("REMOVE_NETWORK all")
  133. dev[0].wait_disconnected(timeout=5)
  134. dev[0].dump_monitor()
  135. # This will result in disconnection due to no matching network
  136. subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  137. apdev[0]['bssid']])
  138. dev[0].wait_disconnected(timeout=15)
  139. def test_ap_bss_load(dev, apdev):
  140. """AP with open mode (no security) configuration"""
  141. hapd = hostapd.add_ap(apdev[0]['ifname'],
  142. { "ssid": "open",
  143. "bss_load_update_period": "10" })
  144. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  145. # this does not really get much useful output with mac80211_hwsim currently,
  146. # but run through the channel survey update couple of times
  147. for i in range(0, 10):
  148. hwsim_utils.test_connectivity(dev[0], hapd)
  149. hwsim_utils.test_connectivity(dev[0], hapd)
  150. hwsim_utils.test_connectivity(dev[0], hapd)
  151. time.sleep(0.15)
  152. def hapd_out_of_mem(hapd, apdev, count, func):
  153. with alloc_fail(hapd, count, func):
  154. started = False
  155. try:
  156. hostapd.add_ap(apdev['ifname'], { "ssid": "open" })
  157. started = True
  158. except:
  159. pass
  160. if started:
  161. raise Exception("hostapd interface started even with memory allocation failure: " + arg)
  162. def test_ap_open_out_of_memory(dev, apdev):
  163. """hostapd failing to setup interface due to allocation failure"""
  164. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  165. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
  166. for i in range(1, 3):
  167. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
  168. for i in range(1, 5):
  169. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
  170. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
  171. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
  172. for i in range(1, 4):
  173. hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
  174. # eloop_register_read_sock() call from i802_init()
  175. hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;eloop_register_sock;?eloop_register_read_sock;=i802_init")
  176. # verify that a new interface can still be added when memory allocation does
  177. # not fail
  178. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })