test_p2p_autogo.py 17 KB

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