test_p2p_invitation.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # P2P invitation test cases
  2. # Copyright (c) 2013, 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 logging
  7. logger = logging.getLogger()
  8. import hwsim_utils
  9. def test_p2p_go_invite(dev):
  10. """P2P GO inviting a client to join"""
  11. addr0 = dev[0].p2p_dev_addr()
  12. addr1 = dev[1].p2p_dev_addr()
  13. logger.info("Generate BSS table entry for old group")
  14. # this adds more coverage to testing by forcing the GO to be found with an
  15. # older entry in the BSS table and with that entry having a different
  16. # operating channel.
  17. dev[0].p2p_start_go(freq=2422)
  18. dev[1].scan()
  19. dev[0].remove_group()
  20. logger.info("Discover peer")
  21. dev[1].p2p_listen()
  22. if not dev[0].discover_peer(addr1, social=True):
  23. raise Exception("Peer " + addr1 + " not found")
  24. logger.info("Start GO on non-social channel")
  25. res = dev[0].p2p_start_go(freq=2417)
  26. logger.debug("res: " + str(res))
  27. logger.info("Invite peer to join the group")
  28. dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
  29. ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=10)
  30. if ev is None:
  31. raise Exception("Timeout on invitation on peer")
  32. ev = dev[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout=10)
  33. if ev is None:
  34. raise Exception("Timeout on invitation on GO")
  35. if "status=1" not in ev:
  36. raise Exception("Unexpected invitation result")
  37. logger.info("Join the group")
  38. pin = dev[1].wps_read_pin()
  39. dev[0].p2p_go_authorize_client(pin)
  40. dev[1].p2p_connect_group(addr0, pin, timeout=60)
  41. logger.info("Client connected")
  42. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  43. logger.info("Terminate group")
  44. dev[0].remove_group()
  45. dev[1].wait_go_ending_session()
  46. def test_p2p_go_invite_auth(dev):
  47. """P2P GO inviting a client to join (authorized invitation)"""
  48. addr0 = dev[0].p2p_dev_addr()
  49. addr1 = dev[1].p2p_dev_addr()
  50. logger.info("Generate BSS table entry for old group")
  51. # this adds more coverage to testing by forcing the GO to be found with an
  52. # older entry in the BSS table and with that entry having a different
  53. # operating channel.
  54. dev[0].p2p_start_go(freq=2432)
  55. dev[1].scan()
  56. dev[0].remove_group()
  57. dev[0].dump_monitor()
  58. dev[1].dump_monitor()
  59. logger.info("Discover peer")
  60. dev[1].p2p_listen()
  61. if not dev[0].discover_peer(addr1, social=True):
  62. raise Exception("Peer " + addr1 + " not found")
  63. dev[0].p2p_listen()
  64. if not dev[1].discover_peer(addr0, social=True):
  65. raise Exception("Peer " + addr0 + " not found")
  66. dev[1].p2p_listen()
  67. logger.info("Authorize invitation")
  68. pin = dev[1].wps_read_pin()
  69. dev[1].global_request("P2P_CONNECT " + addr0 + " " + pin + " join auth")
  70. logger.info("Start GO on non-social channel")
  71. res = dev[0].p2p_start_go(freq=2427)
  72. logger.debug("res: " + str(res))
  73. logger.info("Invite peer to join the group")
  74. dev[0].p2p_go_authorize_client(pin)
  75. dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
  76. ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED",
  77. "P2P-GROUP-STARTED"], timeout=20)
  78. if ev is None:
  79. raise Exception("Timeout on invitation on peer")
  80. if "P2P-INVITATION-RECEIVED" in ev:
  81. raise Exception("Unexpected request to accept pre-authorized invitaton")
  82. dev[1].group_form_result(ev)
  83. dev[0].dump_monitor()
  84. logger.info("Client connected")
  85. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  86. logger.info("Terminate group")
  87. dev[0].remove_group()
  88. dev[1].wait_go_ending_session()
  89. def test_p2p_go_invite_unknown(dev):
  90. """P2P GO inviting a client that has not discovered the GO"""
  91. try:
  92. addr0 = dev[0].p2p_dev_addr()
  93. addr1 = dev[1].p2p_dev_addr()
  94. dev[1].p2p_listen()
  95. if not dev[0].discover_peer(addr1, social=True):
  96. raise Exception("Peer " + addr1 + " not found")
  97. dev[1].global_request("P2P_FLUSH")
  98. dev[1].p2p_listen()
  99. dev[0].p2p_start_go(freq=2412)
  100. logger.info("Invite peer to join the group")
  101. # Prevent peer entry from being added for testing coverage
  102. if "OK" not in dev[1].global_request("P2P_SET peer_filter 00:11:22:33:44:55"):
  103. raise Exception("Failed to set peer_filter")
  104. dev[0].p2p_go_authorize_client("12345670")
  105. dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
  106. ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=15)
  107. if ev is None:
  108. raise Exception("Invitation Request not received")
  109. ev = dev[0].wait_global_event(["P2P-INVITATION-RESULT"], timeout=15)
  110. if ev is None:
  111. raise Exception("Invitation Response not received")
  112. if "status=1" not in ev:
  113. raise Exception("Unexpected invitation result: " + ev)
  114. finally:
  115. dev[1].global_request("P2P_SET peer_filter 00:00:00:00:00:00")
  116. def test_p2p_cli_invite(dev):
  117. """P2P Client inviting a device to join"""
  118. addr0 = dev[0].p2p_dev_addr()
  119. addr1 = dev[1].p2p_dev_addr()
  120. addr2 = dev[2].p2p_dev_addr()
  121. dev[0].p2p_start_go(freq=2412)
  122. pin = dev[1].wps_read_pin()
  123. dev[0].p2p_go_authorize_client(pin)
  124. dev[1].p2p_connect_group(addr0, pin, timeout=60)
  125. dev[2].p2p_listen()
  126. if not dev[1].discover_peer(addr2, social=True):
  127. raise Exception("Peer " + addr2 + " not found")
  128. if "OK" not in dev[1].global_request("P2P_INVITE group=" + dev[1].group_ifname + " peer=" + addr2):
  129. raise Exception("Unexpected failure of P2P_INVITE to known peer")
  130. ev = dev[2].wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=10)
  131. if ev is None:
  132. raise Exception("Timeout on invitation invited peer")
  133. if "sa=" + addr1 not in ev:
  134. raise Exception("Incorrect source address")
  135. if "go_dev_addr=" + addr0 not in ev:
  136. raise Exception("Incorrect GO address")
  137. ev = dev[1].wait_global_event(["P2P-INVITATION-RESULT"], timeout=10)
  138. if ev is None:
  139. raise Exception("Timeout on invitation on inviting client")
  140. if "status=1" not in ev:
  141. raise Exception("Unexpected invitation result")
  142. pin = dev[2].wps_read_pin()
  143. dev[0].p2p_go_authorize_client(pin)
  144. dev[2].p2p_connect_group(addr0, pin, timeout=60)
  145. if "FAIL" not in dev[1].global_request("P2P_INVITE group=" + dev[1].group_ifname + " peer=00:11:22:33:44:55"):
  146. raise Exception("Unexpected success of P2P_INVITE to unknown peer")
  147. dev[0].remove_group()
  148. dev[1].wait_go_ending_session()
  149. dev[2].wait_go_ending_session()
  150. def test_p2p_invite_invalid(dev):
  151. """Invalid parameters to P2P_INVITE"""
  152. id = dev[0].add_network()
  153. for cmd in [ "foo=bar",
  154. "persistent=123 peer=foo",
  155. "persistent=123",
  156. "persistent=%d" % id,
  157. "group=foo",
  158. "group=foo peer=foo",
  159. "group=foo peer=00:11:22:33:44:55 go_dev_addr=foo" ]:
  160. if "FAIL" not in dev[0].request("P2P_INVITE " + cmd):
  161. raise Exception("Invalid P2P_INVITE accepted: " + cmd)