test_p2p_grpform.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  135. check_grpform_results(i_res, r_res)
  136. if i_res['role'] != 'GO' or r_res['role'] != 'client':
  137. raise Exception("Unexpected device roles")
  138. remove_group(dev[0], dev[1])
  139. def test_both_go_intent_15(dev):
  140. """P2P GO Negotiation with both devices using GO intent 15"""
  141. 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)
  142. def test_both_go_neg_display(dev):
  143. """P2P GO Negotiation with both devices trying to display PIN"""
  144. 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')
  145. def test_both_go_neg_enter(dev):
  146. """P2P GO Negotiation with both devices trying to enter PIN"""
  147. 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')
  148. def test_grpform_per_sta_psk(dev):
  149. """P2P group formation with per-STA PSKs"""
  150. dev[0].request("P2P_SET per_sta_psk 1")
  151. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  152. check_grpform_results(i_res, r_res)
  153. pin = dev[2].wps_read_pin()
  154. dev[0].p2p_go_authorize_client(pin)
  155. c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  156. check_grpform_results(i_res, c_res)
  157. if r_res['psk'] == c_res['psk']:
  158. raise Exception("Same PSK assigned for both clients")
  159. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  160. dev[0].remove_group()
  161. dev[1].wait_go_ending_session()
  162. dev[2].wait_go_ending_session()
  163. def test_grpform_per_sta_psk_wps(dev):
  164. """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
  165. dev[0].request("P2P_SET per_sta_psk 1")
  166. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
  167. check_grpform_results(i_res, r_res)
  168. dev[0].p2p_go_authorize_client_pbc()
  169. dev[2].request("WPS_PBC")
  170. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  171. if ev is None:
  172. raise Exception("Association with the GO timed out")
  173. hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
  174. dev[0].remove_group()
  175. dev[2].request("DISCONNECT")
  176. dev[1].wait_go_ending_session()
  177. def test_grpform_force_chan_go(dev):
  178. """P2P group formation forced channel selection by GO"""
  179. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  180. i_freq=2432,
  181. r_dev=dev[1], r_intent=0,
  182. test_data=False)
  183. check_grpform_results(i_res, r_res)
  184. if i_res['freq'] != "2432":
  185. raise Exception("Unexpected channel - did not follow GO's forced channel")
  186. remove_group(dev[0], dev[1])
  187. def test_grpform_force_chan_cli(dev):
  188. """P2P group formation forced channel selection by client"""
  189. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  190. i_freq=2417,
  191. r_dev=dev[1], r_intent=15,
  192. test_data=False)
  193. check_grpform_results(i_res, r_res)
  194. if i_res['freq'] != "2417":
  195. raise Exception("Unexpected channel - did not follow GO's forced channel")
  196. remove_group(dev[0], dev[1])
  197. def test_grpform_force_chan_conflict(dev):
  198. """P2P group formation fails due to forced channel mismatch"""
  199. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  200. r_dev=dev[1], r_intent=15, r_freq=2427,
  201. expect_failure=True, i_go_neg_status=7)
  202. def test_grpform_pref_chan_go(dev):
  203. """P2P group formation preferred channel selection by GO"""
  204. dev[0].request("SET p2p_pref_chan 81:7")
  205. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  206. r_dev=dev[1], r_intent=0,
  207. test_data=False)
  208. check_grpform_results(i_res, r_res)
  209. if i_res['freq'] != "2442":
  210. raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
  211. remove_group(dev[0], dev[1])
  212. def test_grpform_pref_chan_go_overridden(dev):
  213. """P2P group formation preferred channel selection by GO overridden by client"""
  214. dev[1].request("SET p2p_pref_chan 81:7")
  215. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  216. i_freq=2422,
  217. r_dev=dev[1], r_intent=15,
  218. test_data=False)
  219. check_grpform_results(i_res, r_res)
  220. if i_res['freq'] != "2422":
  221. raise Exception("Unexpected channel - did not follow client's forced channel")
  222. remove_group(dev[0], dev[1])
  223. def test_grpform_no_go_freq_forcing_chan(dev):
  224. """P2P group formation with no-GO freq forcing channel"""
  225. dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
  226. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  227. r_dev=dev[1], r_intent=15,
  228. test_data=False)
  229. check_grpform_results(i_res, r_res)
  230. if int(i_res['freq']) > 4000:
  231. raise Exception("Unexpected channel - did not follow no-GO freq")
  232. remove_group(dev[0], dev[1])
  233. def test_grpform_no_go_freq_conflict(dev):
  234. """P2P group formation fails due to no-GO range forced by client"""
  235. dev[1].request("SET p2p_no_go_freq 2000-3000")
  236. go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
  237. r_dev=dev[1], r_intent=15,
  238. expect_failure=True, i_go_neg_status=7)
  239. def test_grpform_no_5ghz_world_roaming(dev):
  240. """P2P group formation with world roaming regulatory"""
  241. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  242. r_dev=dev[1], r_intent=15,
  243. test_data=False)
  244. check_grpform_results(i_res, r_res)
  245. if int(i_res['freq']) > 4000:
  246. raise Exception("Unexpected channel - did not follow world roaming rules")
  247. remove_group(dev[0], dev[1])
  248. def test_grpform_no_5ghz_add_cli(dev):
  249. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
  250. dev[0].request("SET p2p_add_cli_chan 1")
  251. dev[1].request("SET p2p_add_cli_chan 1")
  252. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  253. r_dev=dev[1], r_intent=14,
  254. test_data=False)
  255. check_grpform_results(i_res, r_res)
  256. if int(i_res['freq']) > 4000:
  257. raise Exception("Unexpected channel - did not follow world roaming rules")
  258. remove_group(dev[0], dev[1])
  259. def test_grpform_no_5ghz_add_cli2(dev):
  260. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
  261. dev[0].request("SET p2p_add_cli_chan 1")
  262. dev[1].request("SET p2p_add_cli_chan 1")
  263. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
  264. r_dev=dev[1], r_intent=0,
  265. test_data=False)
  266. check_grpform_results(i_res, r_res)
  267. if int(i_res['freq']) > 4000:
  268. raise Exception("Unexpected channel - did not follow world roaming rules")
  269. remove_group(dev[0], dev[1])
  270. def test_grpform_no_5ghz_add_cli3(dev):
  271. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
  272. dev[0].request("SET p2p_add_cli_chan 1")
  273. dev[1].request("SET p2p_add_cli_chan 1")
  274. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
  275. r_dev=dev[1], r_intent=15,
  276. test_data=False)
  277. check_grpform_results(i_res, r_res)
  278. if int(i_res['freq']) > 4000:
  279. raise Exception("Unexpected channel - did not follow world roaming rules")
  280. remove_group(dev[0], dev[1])
  281. def test_grpform_no_5ghz_add_cli4(dev):
  282. """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
  283. dev[0].request("SET p2p_add_cli_chan 1")
  284. dev[1].request("SET p2p_add_cli_chan 1")
  285. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  286. r_dev=dev[1], r_intent=0,
  287. test_data=False)
  288. check_grpform_results(i_res, r_res)
  289. if int(i_res['freq']) > 4000:
  290. raise Exception("Unexpected channel - did not follow world roaming rules")
  291. remove_group(dev[0], dev[1])