test_p2p_service.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # P2P service discovery 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 uuid
  9. import hwsim_utils
  10. def add_bonjour_services(dev):
  11. dev.request("P2P_SERVICE_ADD bonjour 0b5f6166706f766572746370c00c000c01 074578616d706c65c027")
  12. dev.request("P2P_SERVICE_ADD bonjour 076578616d706c650b5f6166706f766572746370c00c001001 00")
  13. dev.request("P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027")
  14. dev.request("P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001 09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074")
  15. def add_upnp_services(dev):
  16. dev.request("P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice")
  17. dev.request("P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::upnp:rootdevice")
  18. dev.request("P2P_SERVICE_ADD upnp 10 uuid:1122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2")
  19. dev.request("P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2")
  20. dev.request("P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1")
  21. def add_extra_services(dev):
  22. for i in range(0, 100):
  23. dev.request("P2P_SERVICE_ADD upnp 10 uuid:" + str(uuid.uuid4()) + "::upnp:rootdevice")
  24. def run_sd(dev, dst, query, exp_query=None, fragment=False, query2=None):
  25. addr0 = dev[0].p2p_dev_addr()
  26. addr1 = dev[1].p2p_dev_addr()
  27. add_bonjour_services(dev[0])
  28. add_upnp_services(dev[0])
  29. if fragment:
  30. add_extra_services(dev[0])
  31. dev[0].p2p_listen()
  32. dev[1].request("P2P_FLUSH")
  33. dev[1].request("P2P_SERV_DISC_REQ " + dst + " " + query)
  34. if query2:
  35. dev[1].request("P2P_SERV_DISC_REQ " + dst + " " + query2)
  36. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  37. raise Exception("Peer " + addr0 + " not found")
  38. ev = dev[0].wait_event(["P2P-SERV-DISC-REQ"], timeout=10)
  39. if ev is None:
  40. raise Exception("Service discovery timed out")
  41. if addr1 not in ev:
  42. raise Exception("Unexpected service discovery request source")
  43. if exp_query is None:
  44. exp_query = query
  45. if exp_query not in ev and (query2 is None or query2 not in ev):
  46. raise Exception("Unexpected service discovery request contents")
  47. if query2:
  48. ev_list = []
  49. for i in range(0, 4):
  50. ev = dev[1].wait_event(["P2P-SERV-DISC-RESP"], timeout=10)
  51. if ev is None:
  52. raise Exception("Service discovery timed out")
  53. if addr0 in ev:
  54. ev_list.append(ev)
  55. if len(ev_list) == 2:
  56. break
  57. return ev_list
  58. for i in range(0, 2):
  59. ev = dev[1].wait_event(["P2P-SERV-DISC-RESP"], timeout=10)
  60. if ev is None:
  61. raise Exception("Service discovery timed out")
  62. if addr0 in ev:
  63. break
  64. dev[0].p2p_stop_find()
  65. dev[1].p2p_stop_find()
  66. if "OK" not in dev[0].request("P2P_SERVICE_DEL upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"):
  67. raise Exception("Failed to delete a UPnP service")
  68. if "FAIL" not in dev[0].request("P2P_SERVICE_DEL upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"):
  69. raise Exception("Unexpected deletion success for UPnP service")
  70. if "OK" not in dev[0].request("P2P_SERVICE_DEL bonjour 0b5f6166706f766572746370c00c000c01"):
  71. raise Exception("Failed to delete a Bonjour service")
  72. if "FAIL" not in dev[0].request("P2P_SERVICE_DEL bonjour 0b5f6166706f766572746370c00c000c01"):
  73. raise Exception("Unexpected deletion success for Bonjour service")
  74. return ev
  75. def test_p2p_service_discovery(dev):
  76. """P2P service discovery"""
  77. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  78. ev = run_sd(dev, dst, "02000001")
  79. if "0b5f6166706f766572746370c00c000c01" not in ev:
  80. raise Exception("Unexpected service discovery response contents (Bonjour)")
  81. if "496e7465726e6574" not in ev:
  82. raise Exception("Unexpected service discovery response contents (UPnP)")
  83. def test_p2p_service_discovery2(dev):
  84. """P2P service discovery with one peer having no services"""
  85. dev[2].p2p_listen()
  86. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  87. ev = run_sd(dev, dst, "02000001")
  88. if "0b5f6166706f766572746370c00c000c01" not in ev:
  89. raise Exception("Unexpected service discovery response contents (Bonjour)")
  90. if "496e7465726e6574" not in ev:
  91. raise Exception("Unexpected service discovery response contents (UPnP)")
  92. def test_p2p_service_discovery3(dev):
  93. """P2P service discovery for Bonjour with one peer having no services"""
  94. dev[2].p2p_listen()
  95. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  96. ev = run_sd(dev, dst, "02000101")
  97. if "0b5f6166706f766572746370c00c000c01" not in ev:
  98. raise Exception("Unexpected service discovery response contents (Bonjour)")
  99. def test_p2p_service_discovery4(dev):
  100. """P2P service discovery for UPnP with one peer having no services"""
  101. dev[2].p2p_listen()
  102. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  103. ev = run_sd(dev, dst, "02000201")
  104. if "496e7465726e6574" not in ev:
  105. raise Exception("Unexpected service discovery response contents (UPnP)")
  106. def test_p2p_service_discovery_multiple_queries(dev):
  107. """P2P service discovery with multiple queries"""
  108. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  109. ev = run_sd(dev, dst, "02000201", query2="02000101")
  110. if "0b5f6166706f766572746370c00c000c01" not in ev[0] + ev[1]:
  111. raise Exception("Unexpected service discovery response contents (Bonjour)")
  112. if "496e7465726e6574" not in ev[0] + ev[1]:
  113. raise Exception("Unexpected service discovery response contents (UPnP)")
  114. def test_p2p_service_discovery_multiple_queries2(dev):
  115. """P2P service discovery with multiple queries with one peer having no services"""
  116. dev[2].p2p_listen()
  117. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  118. ev = run_sd(dev, dst, "02000201", query2="02000101")
  119. if "0b5f6166706f766572746370c00c000c01" not in ev[0] + ev[1]:
  120. raise Exception("Unexpected service discovery response contents (Bonjour)")
  121. if "496e7465726e6574" not in ev[0] + ev[1]:
  122. raise Exception("Unexpected service discovery response contents (UPnP)")
  123. def test_p2p_service_discovery_fragmentation(dev):
  124. """P2P service discovery with fragmentation"""
  125. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  126. ev = run_sd(dev, dst, "02000001", fragment=True)
  127. if not "long response" in ev:
  128. if "0b5f6166706f766572746370c00c000c01" not in ev:
  129. raise Exception("Unexpected service discovery response contents (Bonjour)")
  130. if "496e7465726e6574" not in ev:
  131. raise Exception("Unexpected service discovery response contents (UPnP)")
  132. def test_p2p_service_discovery_bonjour(dev):
  133. """P2P service discovery (Bonjour)"""
  134. ev = run_sd(dev, "00:00:00:00:00:00", "02000101")
  135. if "0b5f6166706f766572746370c00c000c01" not in ev:
  136. raise Exception("Unexpected service discovery response contents (Bonjour)")
  137. if "045f697070c00c000c01" not in ev:
  138. raise Exception("Unexpected service discovery response contents (Bonjour)")
  139. if "496e7465726e6574" in ev:
  140. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  141. def test_p2p_service_discovery_bonjour2(dev):
  142. """P2P service discovery (Bonjour AFS)"""
  143. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c01")
  144. if "0b5f6166706f766572746370c00c000c01" not in ev:
  145. raise Exception("Unexpected service discovery response contents (Bonjour)")
  146. if "045f697070c00c000c01" in ev:
  147. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  148. if "496e7465726e6574" in ev:
  149. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  150. def test_p2p_service_discovery_bonjour3(dev):
  151. """P2P service discovery (Bonjour AFS - no match)"""
  152. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c02")
  153. if "0300010102" not in ev:
  154. raise Exception("Requested-info-not-available was not indicated")
  155. if "0b5f6166706f766572746370c00c000c01" in ev:
  156. raise Exception("Unexpected service discovery response contents (Bonjour)")
  157. if "045f697070c00c000c01" in ev:
  158. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  159. if "496e7465726e6574" in ev:
  160. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  161. def test_p2p_service_discovery_upnp(dev):
  162. """P2P service discovery (UPnP)"""
  163. ev = run_sd(dev, "00:00:00:00:00:00", "02000201")
  164. if "0b5f6166706f766572746370c00c000c01" in ev:
  165. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  166. if "496e7465726e6574" not in ev:
  167. raise Exception("Unexpected service discovery response contents (UPnP)")
  168. def test_p2p_service_discovery_upnp2(dev):
  169. """P2P service discovery (UPnP using request helper)"""
  170. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:all", "0b00020110737364703a616c6c")
  171. if "0b5f6166706f766572746370c00c000c01" in ev:
  172. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  173. if "496e7465726e6574" not in ev:
  174. raise Exception("Unexpected service discovery response contents (UPnP)")
  175. def test_p2p_service_discovery_upnp3(dev):
  176. """P2P service discovery (UPnP using request helper - no match)"""
  177. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:foo", "0b00020110737364703a666f6f")
  178. if "0300020102" not in ev:
  179. raise Exception("Requested-info-not-available was not indicated")
  180. if "0b5f6166706f766572746370c00c000c01" in ev:
  181. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  182. if "496e7465726e6574" in ev:
  183. raise Exception("Unexpected service discovery response contents (UPnP)")
  184. def test_p2p_service_discovery_ws(dev):
  185. """P2P service discovery (WS-Discovery)"""
  186. ev = run_sd(dev, "00:00:00:00:00:00", "02000301")
  187. if "0b5f6166706f766572746370c00c000c01" in ev:
  188. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  189. if "496e7465726e6574" in ev:
  190. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  191. if "0300030101" not in ev:
  192. raise Exception("Unexpected service discovery response contents (WS)")
  193. def test_p2p_service_discovery_wfd(dev):
  194. """P2P service discovery (Wi-Fi Display)"""
  195. dev[0].request("SET wifi_display 1")
  196. ev = run_sd(dev, "00:00:00:00:00:00", "02000401")
  197. if " 030004" in ev:
  198. raise Exception("Unexpected response to invalid WFD SD query")
  199. dev[0].request("SET wifi_display 0")
  200. ev = run_sd(dev, "00:00:00:00:00:00", "0300040100")
  201. if "0300040101" not in ev:
  202. raise Exception("Unexpected response to WFD SD query (protocol was disabled)")
  203. def test_p2p_service_discovery_req_cancel(dev):
  204. """Cancel a P2P service discovery request"""
  205. if "FAIL" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ ab"):
  206. raise Exception("Unexpected SD cancel success")
  207. query = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  208. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query):
  209. raise Exception("Unexpected SD cancel failure")
  210. query1 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  211. query2 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000002")
  212. query3 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000003")
  213. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query2):
  214. raise Exception("Unexpected SD cancel failure")
  215. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query1):
  216. raise Exception("Unexpected SD cancel failure")
  217. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query3):
  218. raise Exception("Unexpected SD cancel failure")
  219. query = dev[0].request("P2P_SERV_DISC_REQ 00:00:00:00:00:00 02000001")
  220. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query):
  221. raise Exception("Unexpected SD(broadcast) cancel failure")
  222. def test_p2p_service_discovery_go(dev):
  223. """P2P service discovery from GO"""
  224. addr0 = dev[0].p2p_dev_addr()
  225. addr1 = dev[1].p2p_dev_addr()
  226. add_bonjour_services(dev[0])
  227. add_upnp_services(dev[0])
  228. dev[0].p2p_start_go(freq=2412)
  229. dev[1].request("P2P_FLUSH")
  230. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  231. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  232. raise Exception("Peer " + addr0 + " not found")
  233. ev = dev[0].wait_event(["P2P-SERV-DISC-REQ"], timeout=10)
  234. if ev is None:
  235. raise Exception("Service discovery timed out")
  236. if addr1 not in ev:
  237. raise Exception("Unexpected service discovery request source")
  238. ev = dev[1].wait_event(["P2P-SERV-DISC-RESP"], timeout=10)
  239. if ev is None:
  240. raise Exception("Service discovery timed out")
  241. if addr0 not in ev:
  242. raise Exception("Unexpected service discovery response source")
  243. if "0b5f6166706f766572746370c00c000c01" not in ev:
  244. raise Exception("Unexpected service discovery response contents (Bonjour)")
  245. if "496e7465726e6574" not in ev:
  246. raise Exception("Unexpected service discovery response contents (UPnP)")