test_p2p_autogo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # P2P autonomous GO test cases
  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 time
  7. import subprocess
  8. import logging
  9. logger = logging.getLogger()
  10. import hwsim_utils
  11. import utils
  12. from wlantest import Wlantest
  13. from wpasupplicant import WpaSupplicant
  14. def autogo(go, freq=None, persistent=None):
  15. logger.info("Start autonomous GO " + go.ifname)
  16. res = go.p2p_start_go(freq=freq, persistent=persistent)
  17. logger.debug("res: " + str(res))
  18. return res
  19. def connect_cli(go, client, social=False, freq=None):
  20. logger.info("Try to connect the client to the GO")
  21. pin = client.wps_read_pin()
  22. go.p2p_go_authorize_client(pin)
  23. res = client.p2p_connect_group(go.p2p_dev_addr(), pin, timeout=60,
  24. social=social, freq=freq)
  25. logger.info("Client connected")
  26. hwsim_utils.test_connectivity_p2p(go, client)
  27. return res
  28. def test_autogo(dev):
  29. """P2P autonomous GO and client joining group"""
  30. res = autogo(dev[0])
  31. if "p2p-wlan" in res['ifname']:
  32. raise Exception("Unexpected group interface name on GO")
  33. res = connect_cli(dev[0], dev[1])
  34. if "p2p-wlan" in res['ifname']:
  35. raise Exception("Unexpected group interface name on client")
  36. bss = dev[1].get_bss("p2p_dev_addr=" + dev[0].p2p_dev_addr())
  37. if bss['bssid'] != dev[0].p2p_interface_addr():
  38. raise Exception("Unexpected BSSID in the BSS entry for the GO")
  39. id = bss['id']
  40. bss = dev[1].get_bss("ID-" + id)
  41. if bss['id'] != id:
  42. raise Exception("Could not find BSS entry based on id")
  43. res = dev[1].request("BSS RANGE=" + id + "- MASK=0x1")
  44. if "id=" + id not in res:
  45. raise Exception("Could not find BSS entry based on id range")
  46. # Presence request to increase testing coverage
  47. if "FAIL" in dev[1].group_request("P2P_PRESENCE_REQ 30000 102400"):
  48. raise Exception("Could not send presence request")
  49. ev = dev[1].wait_event(["P2P-PRESENCE-RESPONSE"])
  50. if ev is None:
  51. raise Exception("Timeout while waiting for Presence Response")
  52. dev[0].remove_group()
  53. dev[1].wait_go_ending_session()
  54. def test_autogo2(dev):
  55. """P2P autonomous GO with a separate group interface and client joining group"""
  56. dev[0].request("SET p2p_no_group_iface 0")
  57. res = autogo(dev[0], freq=2437)
  58. if "p2p-wlan" not in res['ifname']:
  59. raise Exception("Unexpected group interface name on GO")
  60. if res['ifname'] not in utils.get_ifnames():
  61. raise Exception("Could not find group interface netdev")
  62. connect_cli(dev[0], dev[1], social=True, freq=2437)
  63. dev[0].remove_group()
  64. dev[1].wait_go_ending_session()
  65. if res['ifname'] in utils.get_ifnames():
  66. raise Exception("Group interface netdev was not removed")
  67. def test_autogo3(dev):
  68. """P2P autonomous GO and client with a separate group interface joining group"""
  69. dev[1].request("SET p2p_no_group_iface 0")
  70. autogo(dev[0], freq=2462)
  71. res = connect_cli(dev[0], dev[1], social=True, freq=2462)
  72. if "p2p-wlan" not in res['ifname']:
  73. raise Exception("Unexpected group interface name on client")
  74. if res['ifname'] not in utils.get_ifnames():
  75. raise Exception("Could not find group interface netdev")
  76. dev[0].remove_group()
  77. dev[1].wait_go_ending_session()
  78. dev[1].ping()
  79. if res['ifname'] in utils.get_ifnames():
  80. raise Exception("Group interface netdev was not removed")
  81. def test_autogo4(dev):
  82. """P2P autonomous GO and client joining group (both with a separate group interface)"""
  83. dev[0].request("SET p2p_no_group_iface 0")
  84. dev[1].request("SET p2p_no_group_iface 0")
  85. res1 = autogo(dev[0], freq=2412)
  86. res2 = connect_cli(dev[0], dev[1], social=True, freq=2412)
  87. if "p2p-wlan" not in res1['ifname']:
  88. raise Exception("Unexpected group interface name on GO")
  89. if "p2p-wlan" not in res2['ifname']:
  90. raise Exception("Unexpected group interface name on client")
  91. ifnames = utils.get_ifnames()
  92. if res1['ifname'] not in ifnames:
  93. raise Exception("Could not find GO group interface netdev")
  94. if res2['ifname'] not in ifnames:
  95. raise Exception("Could not find client group interface netdev")
  96. dev[0].remove_group()
  97. dev[1].wait_go_ending_session()
  98. dev[1].ping()
  99. ifnames = utils.get_ifnames()
  100. if res1['ifname'] in ifnames:
  101. raise Exception("GO group interface netdev was not removed")
  102. if res2['ifname'] in ifnames:
  103. raise Exception("Client group interface netdev was not removed")
  104. def test_autogo_m2d(dev):
  105. """P2P autonomous GO and clients not authorized"""
  106. autogo(dev[0], freq=2412)
  107. go_addr = dev[0].p2p_dev_addr()
  108. dev[1].request("SET p2p_no_group_iface 0")
  109. if not dev[1].discover_peer(go_addr, social=True):
  110. raise Exception("GO " + go_addr + " not found")
  111. dev[1].dump_monitor()
  112. if not dev[2].discover_peer(go_addr, social=True):
  113. raise Exception("GO " + go_addr + " not found")
  114. dev[2].dump_monitor()
  115. logger.info("Trying to join the group when GO has not authorized the client")
  116. pin = dev[1].wps_read_pin()
  117. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  118. if "OK" not in dev[1].global_request(cmd):
  119. raise Exception("P2P_CONNECT join failed")
  120. pin = dev[2].wps_read_pin()
  121. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  122. if "OK" not in dev[2].global_request(cmd):
  123. raise Exception("P2P_CONNECT join failed")
  124. ev = dev[1].wait_global_event(["WPS-M2D"], timeout=10)
  125. if ev is None:
  126. raise Exception("No global M2D event")
  127. ifaces = dev[1].request("INTERFACES").splitlines()
  128. iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
  129. wpas = WpaSupplicant(ifname=iface)
  130. ev = wpas.wait_event(["WPS-M2D"], timeout=10)
  131. if ev is None:
  132. raise Exception("No M2D event on group interface")
  133. ev = dev[2].wait_global_event(["WPS-M2D"], timeout=10)
  134. if ev is None:
  135. raise Exception("No global M2D event (2)")
  136. ev = dev[2].wait_event(["WPS-M2D"], timeout=10)
  137. if ev is None:
  138. raise Exception("No M2D event on group interface (2)")
  139. def test_autogo_fail(dev):
  140. """P2P autonomous GO and incorrect PIN"""
  141. autogo(dev[0], freq=2412)
  142. go_addr = dev[0].p2p_dev_addr()
  143. dev[0].p2p_go_authorize_client("00000000")
  144. dev[1].request("SET p2p_no_group_iface 0")
  145. if not dev[1].discover_peer(go_addr, social=True):
  146. raise Exception("GO " + go_addr + " not found")
  147. dev[1].dump_monitor()
  148. logger.info("Trying to join the group when GO has not authorized the client")
  149. pin = dev[1].wps_read_pin()
  150. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  151. if "OK" not in dev[1].global_request(cmd):
  152. raise Exception("P2P_CONNECT join failed")
  153. ev = dev[1].wait_global_event(["WPS-FAIL"], timeout=10)
  154. if ev is None:
  155. raise Exception("No global WPS-FAIL event")
  156. def test_autogo_2cli(dev):
  157. """P2P autonomous GO and two clients joining group"""
  158. autogo(dev[0], freq=2412)
  159. connect_cli(dev[0], dev[1], social=True, freq=2412)
  160. connect_cli(dev[0], dev[2], social=True, freq=2412)
  161. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  162. dev[0].global_request("P2P_REMOVE_CLIENT " + dev[1].p2p_dev_addr())
  163. dev[1].wait_go_ending_session()
  164. dev[0].remove_group()
  165. dev[2].wait_go_ending_session()
  166. def test_autogo_pbc(dev):
  167. """P2P autonomous GO and PBC"""
  168. dev[1].request("SET p2p_no_group_iface 0")
  169. autogo(dev[0], freq=2412)
  170. if "FAIL" not in dev[0].group_request("WPS_PBC p2p_dev_addr=00:11:22:33:44"):
  171. raise Exception("Invalid WPS_PBC succeeded")
  172. if "OK" not in dev[0].group_request("WPS_PBC p2p_dev_addr=" + dev[1].p2p_dev_addr()):
  173. raise Exception("WPS_PBC failed")
  174. dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=0,
  175. social=True)
  176. ev = dev[2].wait_event(["WPS-M2D"], timeout=15)
  177. if ev is None:
  178. raise Exception("WPS-M2D not reported")
  179. if "config_error=12" not in ev:
  180. raise Exception("Unexpected config_error: " + ev)
  181. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), "pbc", timeout=15,
  182. social=True)
  183. def test_autogo_tdls(dev):
  184. """P2P autonomous GO and two clients using TDLS"""
  185. wt = Wlantest()
  186. go = dev[0]
  187. logger.info("Start autonomous GO with fixed parameters " + go.ifname)
  188. id = go.add_network()
  189. go.set_network_quoted(id, "ssid", "DIRECT-tdls")
  190. go.set_network_quoted(id, "psk", "12345678")
  191. go.set_network(id, "mode", "3")
  192. go.set_network(id, "disabled", "2")
  193. res = go.p2p_start_go(persistent=id, freq="2462")
  194. logger.debug("res: " + str(res))
  195. wt.flush()
  196. wt.add_passphrase("12345678")
  197. connect_cli(go, dev[1], social=True, freq=2462)
  198. connect_cli(go, dev[2], social=True, freq=2462)
  199. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  200. bssid = dev[0].p2p_interface_addr()
  201. addr1 = dev[1].p2p_interface_addr()
  202. addr2 = dev[2].p2p_interface_addr()
  203. dev[1].tdls_setup(addr2)
  204. time.sleep(1)
  205. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  206. conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr1, addr2);
  207. if conf == 0:
  208. raise Exception("No TDLS Setup Confirm (success) seen")
  209. dl = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  210. if dl == 0:
  211. raise Exception("No valid frames through direct link")
  212. wt.tdls_clear(bssid, addr1, addr2);
  213. dev[1].tdls_teardown(addr2)
  214. time.sleep(1)
  215. teardown = wt.get_tdls_counter("teardown", bssid, addr1, addr2);
  216. if teardown == 0:
  217. raise Exception("No TDLS Setup Teardown seen")
  218. wt.tdls_clear(bssid, addr1, addr2);
  219. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  220. ap_path = wt.get_tdls_counter("valid_ap_path", bssid, addr1, addr2);
  221. if ap_path == 0:
  222. raise Exception("No valid frames via AP path")
  223. direct_link = wt.get_tdls_counter("valid_direct_link", bssid, addr1, addr2);
  224. if direct_link > 0:
  225. raise Exception("Unexpected frames through direct link")
  226. idirect_link = wt.get_tdls_counter("invalid_direct_link", bssid, addr1,
  227. addr2);
  228. if idirect_link > 0:
  229. raise Exception("Unexpected frames through direct link (invalid)")
  230. dev[2].remove_group()
  231. dev[1].remove_group()
  232. dev[0].remove_group()
  233. def test_autogo_legacy(dev):
  234. """P2P autonomous GO and legacy clients"""
  235. res = autogo(dev[0], freq=2462)
  236. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  237. raise Exception("passphrase mismatch")
  238. logger.info("Connect P2P client")
  239. connect_cli(dev[0], dev[1], social=True, freq=2462)
  240. logger.info("Connect legacy WPS client")
  241. pin = dev[2].wps_read_pin()
  242. dev[0].p2p_go_authorize_client(pin)
  243. dev[2].request("P2P_SET disabled 1")
  244. dev[2].dump_monitor()
  245. dev[2].request("WPS_PIN any " + pin)
  246. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  247. if ev is None:
  248. raise Exception("Association with the GO timed out")
  249. status = dev[2].get_status()
  250. if status['wpa_state'] != 'COMPLETED':
  251. raise Exception("Not fully connected")
  252. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  253. dev[2].request("DISCONNECT")
  254. logger.info("Connect legacy non-WPS client")
  255. dev[2].request("FLUSH")
  256. dev[2].request("P2P_SET disabled 1")
  257. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  258. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  259. scan_freq=res['freq'])
  260. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  261. dev[2].request("DISCONNECT")
  262. dev[0].remove_group()
  263. dev[1].wait_go_ending_session()
  264. def test_autogo_chan_switch(dev):
  265. """P2P autonomous GO switching channels"""
  266. autogo(dev[0], freq=2417)
  267. connect_cli(dev[0], dev[1])
  268. res = dev[0].request("CHAN_SWITCH 5 2422")
  269. if "FAIL" in res:
  270. # for now, skip test since mac80211_hwsim support is not yet widely
  271. # deployed
  272. return 'skip'
  273. ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
  274. if ev is None:
  275. raise Exception("CSA finished event timed out")
  276. if "freq=2422" not in ev:
  277. raise Exception("Unexpected cahnnel in CSA finished event")
  278. dev[0].dump_monitor()
  279. dev[1].dump_monitor()
  280. time.sleep(0.1)
  281. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  282. def test_autogo_extra_cred(dev):
  283. """P2P autonomous GO sending two WPS credentials"""
  284. if "FAIL" in dev[0].request("SET wps_testing_dummy_cred 1"):
  285. raise Exception("Failed to enable test mode")
  286. autogo(dev[0], freq=2412)
  287. connect_cli(dev[0], dev[1], social=True, freq=2412)
  288. dev[0].remove_group()
  289. dev[1].wait_go_ending_session()
  290. def test_autogo_ifdown(dev):
  291. """P2P autonomous GO and external ifdown"""
  292. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  293. wpas.interface_add("wlan5")
  294. res = autogo(wpas)
  295. wpas.dump_monitor()
  296. wpas.interface_remove("wlan5")
  297. wpas.interface_add("wlan5")
  298. res = autogo(wpas)
  299. wpas.dump_monitor()
  300. subprocess.call(['sudo', 'ifconfig', res['ifname'], 'down'])
  301. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  302. if ev is None:
  303. raise Exception("Group removal not reported")
  304. if res['ifname'] not in ev:
  305. raise Exception("Unexpected group removal event: " + ev)
  306. def test_autogo_start_during_scan(dev):
  307. """P2P autonomous GO started during ongoing manual scan"""
  308. try:
  309. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  310. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  311. raise Exception("Failed to set autoscan")
  312. autogo(dev[0], freq=2462)
  313. connect_cli(dev[0], dev[1], social=True, freq=2462)
  314. dev[0].remove_group()
  315. dev[1].wait_go_ending_session()
  316. finally:
  317. dev[0].request("AUTOSCAN ")
  318. def test_autogo_passphrase_len(dev):
  319. """P2P autonomous GO and longer passphrase"""
  320. try:
  321. if "OK" not in dev[0].request("SET p2p_passphrase_len 13"):
  322. raise Exception("Failed to set passphrase length")
  323. res = autogo(dev[0], freq=2412)
  324. if len(res['passphrase']) != 13:
  325. raise Exception("Unexpected passphrase length")
  326. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  327. raise Exception("passphrase mismatch")
  328. logger.info("Connect P2P client")
  329. connect_cli(dev[0], dev[1], social=True, freq=2412)
  330. logger.info("Connect legacy WPS client")
  331. pin = dev[2].wps_read_pin()
  332. dev[0].p2p_go_authorize_client(pin)
  333. dev[2].request("P2P_SET disabled 1")
  334. dev[2].dump_monitor()
  335. dev[2].request("WPS_PIN any " + pin)
  336. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  337. if ev is None:
  338. raise Exception("Association with the GO timed out")
  339. status = dev[2].get_status()
  340. if status['wpa_state'] != 'COMPLETED':
  341. raise Exception("Not fully connected")
  342. dev[2].request("DISCONNECT")
  343. logger.info("Connect legacy non-WPS client")
  344. dev[2].request("FLUSH")
  345. dev[2].request("P2P_SET disabled 1")
  346. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  347. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  348. scan_freq=res['freq'])
  349. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  350. dev[2].request("DISCONNECT")
  351. dev[0].remove_group()
  352. dev[1].wait_go_ending_session()
  353. finally:
  354. dev[0].request("SET p2p_passphrase_len 8")
  355. def test_autogo_bridge(dev):
  356. """P2P autonomous GO in a bridge"""
  357. try:
  358. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  359. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  360. raise Exception("Failed to set autoscan")
  361. autogo(dev[0])
  362. subprocess.call(['sudo', 'brctl', 'addbr', 'p2p-br0'])
  363. subprocess.call(['sudo', 'brctl', 'setfd', 'p2p-br0', '0'])
  364. subprocess.call(['sudo', 'brctl', 'addif', 'p2p-br0', dev[0].ifname])
  365. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'up'])
  366. time.sleep(0.1)
  367. subprocess.call(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname])
  368. time.sleep(0.1)
  369. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'])
  370. time.sleep(0.1)
  371. subprocess.call(['sudo', 'brctl', 'delbr', 'p2p-br0'])
  372. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=1)
  373. if ev is not None:
  374. raise Exception("P2P group removed unexpectedly")
  375. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  376. raise Exception("Unexpected wpa_state")
  377. dev[0].remove_group()
  378. finally:
  379. dev[0].request("AUTOSCAN ")
  380. subprocess.Popen(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname],
  381. stderr=open('/dev/null', 'w'))
  382. subprocess.Popen(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'],
  383. stderr=open('/dev/null', 'w'))
  384. subprocess.Popen(['sudo', 'brctl', 'delbr', 'p2p-br0'],
  385. stderr=open('/dev/null', 'w'))