test_p2p_autogo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. if dev[0].request("P2P_GET_PASSPHRASE") != res['passphrase']:
  242. raise Exception("passphrase mismatch(2)")
  243. logger.info("Connect P2P client")
  244. connect_cli(dev[0], dev[1], social=True, freq=2462)
  245. logger.info("Connect legacy WPS client")
  246. pin = dev[2].wps_read_pin()
  247. dev[0].p2p_go_authorize_client(pin)
  248. dev[2].request("P2P_SET disabled 1")
  249. dev[2].dump_monitor()
  250. dev[2].request("WPS_PIN any " + pin)
  251. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  252. if ev is None:
  253. raise Exception("Association with the GO timed out")
  254. status = dev[2].get_status()
  255. if status['wpa_state'] != 'COMPLETED':
  256. raise Exception("Not fully connected")
  257. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  258. dev[2].request("DISCONNECT")
  259. logger.info("Connect legacy non-WPS client")
  260. dev[2].request("FLUSH")
  261. dev[2].request("P2P_SET disabled 1")
  262. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  263. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  264. scan_freq=res['freq'])
  265. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  266. dev[2].request("DISCONNECT")
  267. dev[0].remove_group()
  268. dev[1].wait_go_ending_session()
  269. def test_autogo_chan_switch(dev):
  270. """P2P autonomous GO switching channels"""
  271. autogo(dev[0], freq=2417)
  272. connect_cli(dev[0], dev[1])
  273. res = dev[0].request("CHAN_SWITCH 5 2422")
  274. if "FAIL" in res:
  275. # for now, skip test since mac80211_hwsim support is not yet widely
  276. # deployed
  277. return 'skip'
  278. ev = dev[0].wait_event(["AP-CSA-FINISHED"], timeout=10)
  279. if ev is None:
  280. raise Exception("CSA finished event timed out")
  281. if "freq=2422" not in ev:
  282. raise Exception("Unexpected cahnnel in CSA finished event")
  283. dev[0].dump_monitor()
  284. dev[1].dump_monitor()
  285. time.sleep(0.1)
  286. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  287. def test_autogo_extra_cred(dev):
  288. """P2P autonomous GO sending two WPS credentials"""
  289. if "FAIL" in dev[0].request("SET wps_testing_dummy_cred 1"):
  290. raise Exception("Failed to enable test mode")
  291. autogo(dev[0], freq=2412)
  292. connect_cli(dev[0], dev[1], social=True, freq=2412)
  293. dev[0].remove_group()
  294. dev[1].wait_go_ending_session()
  295. def test_autogo_ifdown(dev):
  296. """P2P autonomous GO and external ifdown"""
  297. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  298. wpas.interface_add("wlan5")
  299. res = autogo(wpas)
  300. wpas.dump_monitor()
  301. wpas.interface_remove("wlan5")
  302. wpas.interface_add("wlan5")
  303. res = autogo(wpas)
  304. wpas.dump_monitor()
  305. subprocess.call(['sudo', 'ifconfig', res['ifname'], 'down'])
  306. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  307. if ev is None:
  308. raise Exception("Group removal not reported")
  309. if res['ifname'] not in ev:
  310. raise Exception("Unexpected group removal event: " + ev)
  311. def test_autogo_start_during_scan(dev):
  312. """P2P autonomous GO started during ongoing manual scan"""
  313. try:
  314. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  315. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  316. raise Exception("Failed to set autoscan")
  317. autogo(dev[0], freq=2462)
  318. connect_cli(dev[0], dev[1], social=True, freq=2462)
  319. dev[0].remove_group()
  320. dev[1].wait_go_ending_session()
  321. finally:
  322. dev[0].request("AUTOSCAN ")
  323. def test_autogo_passphrase_len(dev):
  324. """P2P autonomous GO and longer passphrase"""
  325. try:
  326. if "OK" not in dev[0].request("SET p2p_passphrase_len 13"):
  327. raise Exception("Failed to set passphrase length")
  328. res = autogo(dev[0], freq=2412)
  329. if len(res['passphrase']) != 13:
  330. raise Exception("Unexpected passphrase length")
  331. if dev[0].get_group_status_field("passphrase", extra="WPS") != res['passphrase']:
  332. raise Exception("passphrase mismatch")
  333. logger.info("Connect P2P client")
  334. connect_cli(dev[0], dev[1], social=True, freq=2412)
  335. logger.info("Connect legacy WPS client")
  336. pin = dev[2].wps_read_pin()
  337. dev[0].p2p_go_authorize_client(pin)
  338. dev[2].request("P2P_SET disabled 1")
  339. dev[2].dump_monitor()
  340. dev[2].request("WPS_PIN any " + pin)
  341. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  342. if ev is None:
  343. raise Exception("Association with the GO timed out")
  344. status = dev[2].get_status()
  345. if status['wpa_state'] != 'COMPLETED':
  346. raise Exception("Not fully connected")
  347. dev[2].request("DISCONNECT")
  348. logger.info("Connect legacy non-WPS client")
  349. dev[2].request("FLUSH")
  350. dev[2].request("P2P_SET disabled 1")
  351. dev[2].connect(ssid=res['ssid'], psk=res['passphrase'], proto='RSN',
  352. key_mgmt='WPA-PSK', pairwise='CCMP', group='CCMP',
  353. scan_freq=res['freq'])
  354. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  355. dev[2].request("DISCONNECT")
  356. dev[0].remove_group()
  357. dev[1].wait_go_ending_session()
  358. finally:
  359. dev[0].request("SET p2p_passphrase_len 8")
  360. def test_autogo_bridge(dev):
  361. """P2P autonomous GO in a bridge"""
  362. try:
  363. # use autoscan to set scan_req = MANUAL_SCAN_REQ
  364. if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
  365. raise Exception("Failed to set autoscan")
  366. autogo(dev[0])
  367. subprocess.call(['sudo', 'brctl', 'addbr', 'p2p-br0'])
  368. subprocess.call(['sudo', 'brctl', 'setfd', 'p2p-br0', '0'])
  369. subprocess.call(['sudo', 'brctl', 'addif', 'p2p-br0', dev[0].ifname])
  370. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'up'])
  371. time.sleep(0.1)
  372. subprocess.call(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname])
  373. time.sleep(0.1)
  374. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'])
  375. time.sleep(0.1)
  376. subprocess.call(['sudo', 'brctl', 'delbr', 'p2p-br0'])
  377. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=1)
  378. if ev is not None:
  379. raise Exception("P2P group removed unexpectedly")
  380. if dev[0].get_status_field('wpa_state') != "COMPLETED":
  381. raise Exception("Unexpected wpa_state")
  382. dev[0].remove_group()
  383. finally:
  384. dev[0].request("AUTOSCAN ")
  385. subprocess.Popen(['sudo', 'brctl', 'delif', 'p2p-br0', dev[0].ifname],
  386. stderr=open('/dev/null', 'w'))
  387. subprocess.Popen(['sudo', 'ip', 'link', 'set', 'dev', 'p2p-br0', 'down'],
  388. stderr=open('/dev/null', 'w'))
  389. subprocess.Popen(['sudo', 'brctl', 'delbr', 'p2p-br0'],
  390. stderr=open('/dev/null', 'w'))