test_p2p_grpform.py 14 KB

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