test_p2p_grpform.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/usr/bin/python
  2. #
  3. # P2P group formation test cases
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import logging
  9. logger = logging.getLogger()
  10. import time
  11. import threading
  12. import Queue
  13. import hwsim_utils
  14. import utils
  15. def check_grpform_results(i_res, r_res):
  16. if i_res['result'] != 'success' or r_res['result'] != 'success':
  17. raise Exception("Failed group formation")
  18. if i_res['ssid'] != r_res['ssid']:
  19. raise Exception("SSID mismatch")
  20. if i_res['freq'] != r_res['freq']:
  21. raise Exception("freq mismatch")
  22. if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
  23. raise Exception("go_neg_freq mismatch")
  24. if i_res['freq'] != i_res['go_neg_freq']:
  25. raise Exception("freq/go_neg_freq mismatch")
  26. if i_res['role'] != i_res['go_neg_role']:
  27. raise Exception("role/go_neg_role mismatch")
  28. if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
  29. raise Exception("role/go_neg_role mismatch")
  30. if i_res['go_dev_addr'] != r_res['go_dev_addr']:
  31. raise Exception("GO Device Address mismatch")
  32. def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
  33. logger.debug("Initiate GO Negotiation from i_dev")
  34. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
  35. logger.debug("i_res: " + str(i_res))
  36. res.put(i_res)
  37. def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
  38. r_dev.p2p_listen()
  39. i_dev.p2p_listen()
  40. pin = r_dev.wps_read_pin()
  41. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  42. r_dev.dump_monitor()
  43. res = Queue.Queue()
  44. t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
  45. t.start()
  46. logger.debug("Wait for GO Negotiation Request on r_dev")
  47. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  48. if ev is None:
  49. raise Exception("GO Negotiation timed out")
  50. r_dev.dump_monitor()
  51. logger.debug("Re-initiate GO Negotiation from r_dev")
  52. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
  53. logger.debug("r_res: " + str(r_res))
  54. r_dev.dump_monitor()
  55. t.join()
  56. i_res = res.get()
  57. logger.debug("i_res: " + str(i_res))
  58. logger.info("Group formed")
  59. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  60. i_dev.dump_monitor()
  61. return [i_res, r_res]
  62. def go_neg_pin_authorized(i_dev, r_dev, i_intent=None, r_intent=None, expect_failure=False, i_go_neg_status=None, i_method='enter', r_method='display', test_data=True, i_freq=None, r_freq=None):
  63. r_dev.p2p_listen()
  64. i_dev.p2p_listen()
  65. pin = r_dev.wps_read_pin()
  66. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  67. r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, freq=r_freq)
  68. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent, expect_failure=expect_failure, freq=i_freq)
  69. r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
  70. logger.debug("i_res: " + str(i_res))
  71. logger.debug("r_res: " + str(r_res))
  72. r_dev.dump_monitor()
  73. i_dev.dump_monitor()
  74. if i_go_neg_status:
  75. if i_res['result'] != 'go-neg-failed':
  76. raise Exception("Expected GO Negotiation failure not reported")
  77. if i_res['status'] != i_go_neg_status:
  78. raise Exception("Expected GO Negotiation status not seen")
  79. if expect_failure:
  80. return
  81. logger.info("Group formed")
  82. if test_data:
  83. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  84. return [i_res, r_res]
  85. def go_neg_init_pbc(i_dev, r_dev, i_intent, res, freq):
  86. logger.debug("Initiate GO Negotiation from i_dev")
  87. i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc",
  88. timeout=20, go_intent=i_intent, freq=freq)
  89. logger.debug("i_res: " + str(i_res))
  90. res.put(i_res)
  91. def go_neg_pbc(i_dev, r_dev, i_intent=None, r_intent=None, i_freq=None, r_freq=None):
  92. r_dev.p2p_find(social=True)
  93. i_dev.p2p_find(social=True)
  94. logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
  95. r_dev.dump_monitor()
  96. res = Queue.Queue()
  97. t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res, i_freq))
  98. t.start()
  99. logger.debug("Wait for GO Negotiation Request on r_dev")
  100. ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
  101. if ev is None:
  102. raise Exception("GO Negotiation timed out")
  103. r_dev.dump_monitor()
  104. logger.debug("Re-initiate GO Negotiation from r_dev")
  105. r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc",
  106. go_intent=r_intent, timeout=20, freq=r_freq)
  107. logger.debug("r_res: " + str(r_res))
  108. r_dev.dump_monitor()
  109. t.join()
  110. i_res = res.get()
  111. logger.debug("i_res: " + str(i_res))
  112. logger.info("Group formed")
  113. hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
  114. i_dev.dump_monitor()
  115. return [i_res, r_res]
  116. def remove_group(dev1, dev2):
  117. dev1.remove_group()
  118. try:
  119. dev2.remove_group()
  120. except:
  121. pass
  122. def test_grpform(dev):
  123. """P2P group formation using PIN and authorized connection (init -> GO)"""
  124. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  125. r_dev=dev[1], r_intent=0)
  126. check_grpform_results(i_res, r_res)
  127. remove_group(dev[0], dev[1])
  128. def test_grpform_a(dev):
  129. """P2P group formation using PIN and authorized connection (init -> GO) (init: group iface)"""
  130. dev[0].request("SET p2p_no_group_iface 0")
  131. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  132. r_dev=dev[1], r_intent=0)
  133. if "p2p-wlan" not in i_res['ifname']:
  134. raise Exception("Unexpected group interface name")
  135. check_grpform_results(i_res, r_res)
  136. remove_group(dev[0], dev[1])
  137. if i_res['ifname'] in utils.get_ifnames():
  138. raise Exception("Group interface netdev was not removed")
  139. def test_grpform_b(dev):
  140. """P2P group formation using PIN and authorized connection (init -> GO) (resp: group iface)"""
  141. dev[1].request("SET p2p_no_group_iface 0")
  142. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  143. r_dev=dev[1], r_intent=0)
  144. if "p2p-wlan" not in r_res['ifname']:
  145. raise Exception("Unexpected group interface name")
  146. check_grpform_results(i_res, r_res)
  147. remove_group(dev[0], dev[1])
  148. if r_res['ifname'] in utils.get_ifnames():
  149. raise Exception("Group interface netdev was not removed")
  150. def test_grpform_c(dev):
  151. """P2P group formation using PIN and authorized connection (init -> GO) (group iface)"""
  152. dev[0].request("SET p2p_no_group_iface 0")
  153. dev[1].request("SET p2p_no_group_iface 0")
  154. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  155. r_dev=dev[1], r_intent=0)
  156. if "p2p-wlan" not in i_res['ifname']:
  157. raise Exception("Unexpected group interface name")
  158. if "p2p-wlan" not in r_res['ifname']:
  159. raise Exception("Unexpected group interface name")
  160. check_grpform_results(i_res, r_res)
  161. remove_group(dev[0], dev[1])
  162. if i_res['ifname'] in utils.get_ifnames():
  163. raise Exception("Group interface netdev was not removed")
  164. if r_res['ifname'] in utils.get_ifnames():
  165. raise Exception("Group interface netdev was not removed")
  166. def test_grpform2(dev):
  167. """P2P group formation using PIN and authorized connection (resp -> GO)"""
  168. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
  169. remove_group(dev[0], dev[1])
  170. def test_grpform2_c(dev):
  171. """P2P group formation using PIN and authorized connection (resp -> GO) (group iface)"""
  172. dev[0].request("SET p2p_no_group_iface 0")
  173. dev[1].request("SET p2p_no_group_iface 0")
  174. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
  175. remove_group(dev[0], dev[1])
  176. if i_res['ifname'] in utils.get_ifnames():
  177. raise Exception("Group interface netdev was not removed")
  178. if r_res['ifname'] in utils.get_ifnames():
  179. raise Exception("Group interface netdev was not removed")
  180. def test_grpform3(dev):
  181. """P2P group formation using PIN and re-init GO Negotiation"""
  182. go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  183. remove_group(dev[0], dev[1])
  184. def test_grpform3_c(dev):
  185. """P2P group formation using PIN and re-init GO Negotiation (group iface)"""
  186. dev[0].request("SET p2p_no_group_iface 0")
  187. dev[1].request("SET p2p_no_group_iface 0")
  188. [i_res, r_res] = go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  189. remove_group(dev[0], dev[1])
  190. if i_res['ifname'] in utils.get_ifnames():
  191. raise Exception("Group interface netdev was not removed")
  192. if r_res['ifname'] in utils.get_ifnames():
  193. raise Exception("Group interface netdev was not removed")
  194. def test_grpform_pbc(dev):
  195. """P2P group formation using PBC and re-init GO Negotiation"""
  196. dev[0].request("SET ignore_old_scan_res 1")
  197. dev[1].request("SET ignore_old_scan_res 1")
  198. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  199. check_grpform_results(i_res, r_res)
  200. if i_res['role'] != 'GO' or r_res['role'] != 'client':
  201. raise Exception("Unexpected device roles")
  202. remove_group(dev[0], dev[1])
  203. def test_both_go_intent_15(dev):
  204. """P2P GO Negotiation with both devices using GO intent 15"""
  205. go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=15, expect_failure=True, i_go_neg_status=9)
  206. def test_both_go_neg_display(dev):
  207. """P2P GO Negotiation with both devices trying to display PIN"""
  208. go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='display', r_method='display')
  209. def test_both_go_neg_enter(dev):
  210. """P2P GO Negotiation with both devices trying to enter PIN"""
  211. go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='enter', r_method='enter')
  212. def test_grpform_per_sta_psk(dev):
  213. """P2P group formation with per-STA PSKs"""
  214. dev[0].request("P2P_SET per_sta_psk 1")
  215. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  216. check_grpform_results(i_res, r_res)
  217. pin = dev[2].wps_read_pin()
  218. dev[0].p2p_go_authorize_client(pin)
  219. c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  220. check_grpform_results(i_res, c_res)
  221. if r_res['psk'] == c_res['psk']:
  222. raise Exception("Same PSK assigned for both clients")
  223. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  224. dev[0].remove_group()
  225. dev[1].wait_go_ending_session()
  226. dev[2].wait_go_ending_session()
  227. def test_grpform_per_sta_psk_wps(dev):
  228. """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
  229. dev[0].request("P2P_SET per_sta_psk 1")
  230. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  231. check_grpform_results(i_res, r_res)
  232. dev[0].p2p_go_authorize_client_pbc()
  233. dev[2].request("WPS_PBC")
  234. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  235. if ev is None:
  236. raise Exception("Association with the GO timed out")
  237. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  238. dev[0].remove_group()
  239. dev[2].request("DISCONNECT")
  240. dev[1].wait_go_ending_session()
  241. def test_grpform_force_chan_go(dev):
  242. """P2P group formation forced channel selection by GO"""
  243. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  244. i_freq=2432,
  245. r_dev=dev[1], r_intent=0,
  246. test_data=False)
  247. check_grpform_results(i_res, r_res)
  248. if i_res['freq'] != "2432":
  249. raise Exception("Unexpected channel - did not follow GO's forced channel")
  250. remove_group(dev[0], dev[1])
  251. def test_grpform_force_chan_cli(dev):
  252. """P2P group formation forced channel selection by client"""
  253. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  254. i_freq=2417,
  255. r_dev=dev[1], r_intent=15,
  256. test_data=False)
  257. check_grpform_results(i_res, r_res)
  258. if i_res['freq'] != "2417":
  259. raise Exception("Unexpected channel - did not follow GO's forced channel")
  260. remove_group(dev[0], dev[1])
  261. def test_grpform_force_chan_conflict(dev):
  262. """P2P group formation fails due to forced channel mismatch"""
  263. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  264. r_dev=dev[1], r_intent=15, r_freq=2427,
  265. expect_failure=True, i_go_neg_status=7)
  266. def test_grpform_pref_chan_go(dev):
  267. """P2P group formation preferred channel selection by GO"""
  268. dev[0].request("SET p2p_pref_chan 81:7")
  269. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  270. r_dev=dev[1], r_intent=0,
  271. test_data=False)
  272. check_grpform_results(i_res, r_res)
  273. if i_res['freq'] != "2442":
  274. raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
  275. remove_group(dev[0], dev[1])
  276. def test_grpform_pref_chan_go_overridden(dev):
  277. """P2P group formation preferred channel selection by GO overridden by client"""
  278. dev[1].request("SET p2p_pref_chan 81:7")
  279. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  280. i_freq=2422,
  281. r_dev=dev[1], r_intent=15,
  282. test_data=False)
  283. check_grpform_results(i_res, r_res)
  284. if i_res['freq'] != "2422":
  285. raise Exception("Unexpected channel - did not follow client's forced channel")
  286. remove_group(dev[0], dev[1])
  287. def test_grpform_no_go_freq_forcing_chan(dev):
  288. """P2P group formation with no-GO freq forcing channel"""
  289. dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
  290. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  291. r_dev=dev[1], r_intent=15,
  292. test_data=False)
  293. check_grpform_results(i_res, r_res)
  294. if int(i_res['freq']) > 4000:
  295. raise Exception("Unexpected channel - did not follow no-GO freq")
  296. remove_group(dev[0], dev[1])
  297. def test_grpform_no_go_freq_conflict(dev):
  298. """P2P group formation fails due to no-GO range forced by client"""
  299. dev[1].request("SET p2p_no_go_freq 2000-3000")
  300. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  301. r_dev=dev[1], r_intent=15,
  302. expect_failure=True, i_go_neg_status=7)
  303. def test_grpform_no_5ghz_world_roaming(dev):
  304. """P2P group formation with world roaming regulatory"""
  305. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  306. r_dev=dev[1], r_intent=15,
  307. test_data=False)
  308. check_grpform_results(i_res, r_res)
  309. if int(i_res['freq']) > 4000:
  310. raise Exception("Unexpected channel - did not follow world roaming rules")
  311. remove_group(dev[0], dev[1])
  312. def test_grpform_no_5ghz_add_cli(dev):
  313. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
  314. dev[0].request("SET p2p_add_cli_chan 1")
  315. dev[1].request("SET p2p_add_cli_chan 1")
  316. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  317. r_dev=dev[1], r_intent=14,
  318. test_data=False)
  319. check_grpform_results(i_res, r_res)
  320. if int(i_res['freq']) > 4000:
  321. raise Exception("Unexpected channel - did not follow world roaming rules")
  322. remove_group(dev[0], dev[1])
  323. def test_grpform_no_5ghz_add_cli2(dev):
  324. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
  325. dev[0].request("SET p2p_add_cli_chan 1")
  326. dev[1].request("SET p2p_add_cli_chan 1")
  327. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
  328. r_dev=dev[1], r_intent=0,
  329. test_data=False)
  330. check_grpform_results(i_res, r_res)
  331. if int(i_res['freq']) > 4000:
  332. raise Exception("Unexpected channel - did not follow world roaming rules")
  333. remove_group(dev[0], dev[1])
  334. def test_grpform_no_5ghz_add_cli3(dev):
  335. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
  336. dev[0].request("SET p2p_add_cli_chan 1")
  337. dev[1].request("SET p2p_add_cli_chan 1")
  338. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  339. r_dev=dev[1], r_intent=15,
  340. test_data=False)
  341. check_grpform_results(i_res, r_res)
  342. if int(i_res['freq']) > 4000:
  343. raise Exception("Unexpected channel - did not follow world roaming rules")
  344. remove_group(dev[0], dev[1])
  345. def test_grpform_no_5ghz_add_cli4(dev):
  346. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
  347. dev[0].request("SET p2p_add_cli_chan 1")
  348. dev[1].request("SET p2p_add_cli_chan 1")
  349. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  350. r_dev=dev[1], r_intent=0,
  351. test_data=False)
  352. check_grpform_results(i_res, r_res)
  353. if int(i_res['freq']) > 4000:
  354. raise Exception("Unexpected channel - did not follow world roaming rules")
  355. remove_group(dev[0], dev[1])