test_p2p_service.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. addr0 = dev[0].p2p_dev_addr()
  78. for dst in [ "00:00:00:00:00:00", addr0 ]:
  79. ev = run_sd(dev, dst, "02000001")
  80. if "0b5f6166706f766572746370c00c000c01" not in ev:
  81. raise Exception("Unexpected service discovery response contents (Bonjour)")
  82. if "496e7465726e6574" not in ev:
  83. raise Exception("Unexpected service discovery response contents (UPnP)")
  84. for req in [ "foo 02000001",
  85. addr0,
  86. addr0 + " upnp qq urn:schemas-upnp-org:device:InternetGatewayDevice:1",
  87. addr0 + " upnp 10",
  88. addr0 + " 123",
  89. addr0 + " qq" ]:
  90. if "FAIL" not in dev[1].request("P2P_SERV_DISC_REQ " + req):
  91. raise Exception("Invalid P2P_SERV_DISC_REQ accepted: " + req)
  92. def test_p2p_service_discovery2(dev):
  93. """P2P service discovery 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, "02000001")
  97. if "0b5f6166706f766572746370c00c000c01" not in ev:
  98. raise Exception("Unexpected service discovery response contents (Bonjour)")
  99. if "496e7465726e6574" not in ev:
  100. raise Exception("Unexpected service discovery response contents (UPnP)")
  101. def test_p2p_service_discovery3(dev):
  102. """P2P service discovery for Bonjour with one peer having no services"""
  103. dev[2].p2p_listen()
  104. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  105. ev = run_sd(dev, dst, "02000101")
  106. if "0b5f6166706f766572746370c00c000c01" not in ev:
  107. raise Exception("Unexpected service discovery response contents (Bonjour)")
  108. def test_p2p_service_discovery4(dev):
  109. """P2P service discovery for UPnP with one peer having no services"""
  110. dev[2].p2p_listen()
  111. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  112. ev = run_sd(dev, dst, "02000201")
  113. if "496e7465726e6574" not in ev:
  114. raise Exception("Unexpected service discovery response contents (UPnP)")
  115. def test_p2p_service_discovery_multiple_queries(dev):
  116. """P2P service discovery with multiple queries"""
  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_multiple_queries2(dev):
  124. """P2P service discovery with multiple queries with one peer having no services"""
  125. dev[2].p2p_listen()
  126. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  127. ev = run_sd(dev, dst, "02000201", query2="02000101")
  128. if "0b5f6166706f766572746370c00c000c01" not in ev[0] + ev[1]:
  129. raise Exception("Unexpected service discovery response contents (Bonjour)")
  130. if "496e7465726e6574" not in ev[0] + ev[1]:
  131. raise Exception("Unexpected service discovery response contents (UPnP)")
  132. def test_p2p_service_discovery_fragmentation(dev):
  133. """P2P service discovery with fragmentation"""
  134. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  135. ev = run_sd(dev, dst, "02000001", fragment=True)
  136. if not "long response" in ev:
  137. if "0b5f6166706f766572746370c00c000c01" not in ev:
  138. raise Exception("Unexpected service discovery response contents (Bonjour)")
  139. if "496e7465726e6574" not in ev:
  140. raise Exception("Unexpected service discovery response contents (UPnP)")
  141. def test_p2p_service_discovery_bonjour(dev):
  142. """P2P service discovery (Bonjour)"""
  143. ev = run_sd(dev, "00:00:00:00:00:00", "02000101")
  144. if "0b5f6166706f766572746370c00c000c01" not in ev:
  145. raise Exception("Unexpected service discovery response contents (Bonjour)")
  146. if "045f697070c00c000c01" not in ev:
  147. raise Exception("Unexpected service discovery response contents (Bonjour)")
  148. if "496e7465726e6574" in ev:
  149. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  150. def test_p2p_service_discovery_bonjour2(dev):
  151. """P2P service discovery (Bonjour AFS)"""
  152. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c01")
  153. if "0b5f6166706f766572746370c00c000c01" not in ev:
  154. raise Exception("Unexpected service discovery response contents (Bonjour)")
  155. if "045f697070c00c000c01" in ev:
  156. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  157. if "496e7465726e6574" in ev:
  158. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  159. def test_p2p_service_discovery_bonjour3(dev):
  160. """P2P service discovery (Bonjour AFS - no match)"""
  161. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c02")
  162. if "0300010102" not in ev:
  163. raise Exception("Requested-info-not-available was not indicated")
  164. if "0b5f6166706f766572746370c00c000c01" in ev:
  165. raise Exception("Unexpected service discovery response contents (Bonjour)")
  166. if "045f697070c00c000c01" in ev:
  167. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  168. if "496e7465726e6574" in ev:
  169. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  170. def test_p2p_service_discovery_upnp(dev):
  171. """P2P service discovery (UPnP)"""
  172. ev = run_sd(dev, "00:00:00:00:00:00", "02000201")
  173. if "0b5f6166706f766572746370c00c000c01" in ev:
  174. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  175. if "496e7465726e6574" not in ev:
  176. raise Exception("Unexpected service discovery response contents (UPnP)")
  177. def test_p2p_service_discovery_upnp2(dev):
  178. """P2P service discovery (UPnP using request helper)"""
  179. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:all", "0b00020110737364703a616c6c")
  180. if "0b5f6166706f766572746370c00c000c01" in ev:
  181. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  182. if "496e7465726e6574" not in ev:
  183. raise Exception("Unexpected service discovery response contents (UPnP)")
  184. def test_p2p_service_discovery_upnp3(dev):
  185. """P2P service discovery (UPnP using request helper - no match)"""
  186. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:foo", "0b00020110737364703a666f6f")
  187. if "0300020102" not in ev:
  188. raise Exception("Requested-info-not-available was not indicated")
  189. if "0b5f6166706f766572746370c00c000c01" in ev:
  190. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  191. if "496e7465726e6574" in ev:
  192. raise Exception("Unexpected service discovery response contents (UPnP)")
  193. def test_p2p_service_discovery_ws(dev):
  194. """P2P service discovery (WS-Discovery)"""
  195. ev = run_sd(dev, "00:00:00:00:00:00", "02000301")
  196. if "0b5f6166706f766572746370c00c000c01" in ev:
  197. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  198. if "496e7465726e6574" in ev:
  199. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  200. if "0300030101" not in ev:
  201. raise Exception("Unexpected service discovery response contents (WS)")
  202. def test_p2p_service_discovery_wfd(dev):
  203. """P2P service discovery (Wi-Fi Display)"""
  204. dev[0].request("SET wifi_display 1")
  205. ev = run_sd(dev, "00:00:00:00:00:00", "02000401")
  206. if " 030004" in ev:
  207. raise Exception("Unexpected response to invalid WFD SD query")
  208. dev[0].request("SET wifi_display 0")
  209. ev = run_sd(dev, "00:00:00:00:00:00", "0300040100")
  210. if "0300040101" not in ev:
  211. raise Exception("Unexpected response to WFD SD query (protocol was disabled)")
  212. def test_p2p_service_discovery_req_cancel(dev):
  213. """Cancel a P2P service discovery request"""
  214. if "FAIL" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ ab"):
  215. raise Exception("Unexpected SD cancel success")
  216. if "FAIL" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ qq"):
  217. raise Exception("Unexpected SD cancel success")
  218. query = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  219. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query):
  220. raise Exception("Unexpected SD cancel failure")
  221. query1 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  222. query2 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000002")
  223. query3 = dev[0].request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000003")
  224. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query2):
  225. raise Exception("Unexpected SD cancel failure")
  226. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query1):
  227. raise Exception("Unexpected SD cancel failure")
  228. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query3):
  229. raise Exception("Unexpected SD cancel failure")
  230. query = dev[0].request("P2P_SERV_DISC_REQ 00:00:00:00:00:00 02000001")
  231. if "OK" not in dev[0].request("P2P_SERV_DISC_CANCEL_REQ " + query):
  232. raise Exception("Unexpected SD(broadcast) cancel failure")
  233. def test_p2p_service_discovery_go(dev):
  234. """P2P service discovery from GO"""
  235. addr0 = dev[0].p2p_dev_addr()
  236. addr1 = dev[1].p2p_dev_addr()
  237. add_bonjour_services(dev[0])
  238. add_upnp_services(dev[0])
  239. dev[0].p2p_start_go(freq=2412)
  240. dev[1].request("P2P_FLUSH")
  241. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  242. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  243. raise Exception("Peer " + addr0 + " not found")
  244. ev = dev[0].wait_event(["P2P-SERV-DISC-REQ"], timeout=10)
  245. if ev is None:
  246. raise Exception("Service discovery timed out")
  247. if addr1 not in ev:
  248. raise Exception("Unexpected service discovery request source")
  249. ev = dev[1].wait_event(["P2P-SERV-DISC-RESP"], timeout=10)
  250. if ev is None:
  251. raise Exception("Service discovery timed out")
  252. if addr0 not in ev:
  253. raise Exception("Unexpected service discovery response source")
  254. if "0b5f6166706f766572746370c00c000c01" not in ev:
  255. raise Exception("Unexpected service discovery response contents (Bonjour)")
  256. if "496e7465726e6574" not in ev:
  257. raise Exception("Unexpected service discovery response contents (UPnP)")
  258. dev[1].p2p_stop_find()
  259. dev[0].request("P2P_SERVICE_FLUSH")
  260. dev[1].request("P2P_FLUSH")
  261. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  262. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  263. raise Exception("Peer " + addr0 + " not found")
  264. ev = dev[0].wait_event(["P2P-SERV-DISC-REQ"], timeout=10)
  265. if ev is None:
  266. raise Exception("Service discovery timed out")
  267. if addr1 not in ev:
  268. raise Exception("Unexpected service discovery request source")
  269. ev = dev[1].wait_event(["P2P-SERV-DISC-RESP"], timeout=10)
  270. if ev is None:
  271. raise Exception("Service discovery timed out")
  272. if addr0 not in ev:
  273. raise Exception("Unexpected service discovery response source")
  274. if "0300000101" not in ev:
  275. raise Exception("Unexpected service discovery response contents (Bonjour)")
  276. dev[1].p2p_stop_find()
  277. def _test_p2p_service_discovery_external(dev):
  278. addr0 = dev[0].p2p_dev_addr()
  279. addr1 = dev[1].p2p_dev_addr()
  280. if "FAIL" not in dev[0].request("P2P_SERV_DISC_EXTERNAL 2"):
  281. raise Exception("Invalid P2P_SERV_DISC_EXTERNAL accepted")
  282. if "OK" not in dev[0].request("P2P_SERV_DISC_EXTERNAL 1"):
  283. raise Exception("P2P_SERV_DISC_EXTERNAL failed")
  284. dev[0].p2p_listen()
  285. dev[1].request("P2P_FLUSH")
  286. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  287. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  288. raise Exception("Peer " + addr0 + " not found")
  289. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  290. if ev is None:
  291. raise Exception("Service discovery timed out")
  292. if addr1 not in ev:
  293. raise Exception("Unexpected service discovery request source")
  294. arg = ev.split(' ')
  295. resp = "0300000101"
  296. if "OK" not in dev[0].global_request("P2P_SERV_DISC_RESP %s %s %s %s" % (arg[2], arg[3], arg[4], resp)):
  297. raise Exception("P2P_SERV_DISC_RESP failed")
  298. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=15)
  299. if ev is None:
  300. raise Exception("Service discovery timed out")
  301. if addr0 not in ev:
  302. raise Exception("Unexpected address in SD Response: " + ev)
  303. if ev.split(' ')[4] != resp:
  304. raise Exception("Unexpected response data SD Response: " + ev)
  305. ver = ev.split(' ')[3]
  306. dev[0].request("P2P_SERVICE_UPDATE")
  307. dev[1].request("P2P_FLUSH")
  308. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  309. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  310. raise Exception("Peer " + addr0 + " not found")
  311. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  312. if ev is None:
  313. raise Exception("Service discovery timed out")
  314. if addr1 not in ev:
  315. raise Exception("Unexpected service discovery request source")
  316. arg = ev.split(' ')
  317. resp = "0300000101"
  318. if "OK" not in dev[0].global_request("P2P_SERV_DISC_RESP %s %s %s %s" % (arg[2], arg[3], arg[4], resp)):
  319. raise Exception("P2P_SERV_DISC_RESP failed")
  320. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=15)
  321. if ev is None:
  322. raise Exception("Service discovery timed out")
  323. if addr0 not in ev:
  324. raise Exception("Unexpected address in SD Response: " + ev)
  325. if ev.split(' ')[4] != resp:
  326. raise Exception("Unexpected response data SD Response: " + ev)
  327. ver2 = ev.split(' ')[3]
  328. if ver == ver2:
  329. raise Exception("Service list version did not change")
  330. for cmd in [ "%s%s%s%s" % (arg[2], arg[3], arg[4], resp),
  331. "%s %s %s %s" % ("0", arg[3], arg[4], resp),
  332. "%s %s %s %s" % (arg[2], "foo", arg[4], resp),
  333. "%s %s%s%s" % (arg[2], arg[3], arg[4], resp),
  334. "%s %s %s%s" % (arg[2], arg[3], arg[4], resp),
  335. "%s %s %s %s" % (arg[2], arg[3], arg[4], "12345"),
  336. "%s %s %s %s" % (arg[2], arg[3], arg[4], "qq") ]:
  337. if "FAIL" not in dev[0].global_request("P2P_SERV_DISC_RESP " + cmd):
  338. raise Exception("Invalid P2P_SERV_DISC_RESP accepted: " + cmd)
  339. def test_p2p_service_discovery_external(dev):
  340. """P2P service discovery using external response"""
  341. try:
  342. _test_p2p_service_discovery_external(dev)
  343. finally:
  344. dev[0].request("P2P_SERV_DISC_EXTERNAL 0")
  345. def test_p2p_service_discovery_invalid_commands(dev):
  346. """P2P service discovery invalid commands"""
  347. for cmd in [ "bonjour",
  348. "bonjour 12",
  349. "bonjour 123 12",
  350. "bonjour qq 12",
  351. "bonjour 12 123",
  352. "bonjour 12 qq",
  353. "upnp 10",
  354. "upnp qq uuid:",
  355. "foo bar" ]:
  356. if "FAIL" not in dev[0].request("P2P_SERVICE_ADD " + cmd):
  357. raise Exception("Invalid P2P_SERVICE_ADD accepted: " + cmd)
  358. for cmd in [ "bonjour",
  359. "bonjour 123",
  360. "bonjour qq",
  361. "upnp 10",
  362. "upnp ",
  363. "upnp qq uuid:",
  364. "foo bar" ]:
  365. if "FAIL" not in dev[0].request("P2P_SERVICE_DEL " + cmd):
  366. raise Exception("Invalid P2P_SERVICE_DEL accepted: " + cmd)