test_p2p_service.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. from remotehost import remote_compatible
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import time
  11. import uuid
  12. import hwsim_utils
  13. def add_bonjour_services(dev):
  14. dev.global_request("P2P_SERVICE_ADD bonjour 0b5f6166706f766572746370c00c000c01 074578616d706c65c027")
  15. dev.global_request("P2P_SERVICE_ADD bonjour 076578616d706c650b5f6166706f766572746370c00c001001 00")
  16. dev.global_request("P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027")
  17. dev.global_request("P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001 09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074")
  18. def add_upnp_services(dev):
  19. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice")
  20. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::upnp:rootdevice")
  21. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:1122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2")
  22. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2")
  23. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1")
  24. def add_extra_services(dev):
  25. for i in range(0, 100):
  26. dev.global_request("P2P_SERVICE_ADD upnp 10 uuid:" + str(uuid.uuid4()) + "::upnp:rootdevice")
  27. def run_sd(dev, dst, query, exp_query=None, fragment=False, query2=None):
  28. addr0 = dev[0].p2p_dev_addr()
  29. addr1 = dev[1].p2p_dev_addr()
  30. add_bonjour_services(dev[0])
  31. add_upnp_services(dev[0])
  32. if fragment:
  33. add_extra_services(dev[0])
  34. dev[0].p2p_listen()
  35. dev[1].global_request("P2P_FLUSH")
  36. dev[1].global_request("P2P_SERV_DISC_REQ " + dst + " " + query)
  37. if query2:
  38. dev[1].global_request("P2P_SERV_DISC_REQ " + dst + " " + query2)
  39. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  40. raise Exception("Peer " + addr0 + " not found")
  41. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  42. if ev is None:
  43. raise Exception("Service discovery timed out")
  44. if addr1 not in ev:
  45. raise Exception("Unexpected service discovery request source")
  46. if exp_query is None:
  47. exp_query = query
  48. if exp_query not in ev and (query2 is None or query2 not in ev):
  49. raise Exception("Unexpected service discovery request contents")
  50. if query2:
  51. ev_list = []
  52. for i in range(0, 4):
  53. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  54. if ev is None:
  55. raise Exception("Service discovery timed out")
  56. if addr0 in ev:
  57. ev_list.append(ev)
  58. if len(ev_list) == 2:
  59. break
  60. return ev_list
  61. for i in range(0, 2):
  62. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  63. if ev is None:
  64. raise Exception("Service discovery timed out")
  65. if addr0 in ev:
  66. break
  67. dev[0].p2p_stop_find()
  68. dev[1].p2p_stop_find()
  69. if "OK" not in dev[0].global_request("P2P_SERVICE_DEL upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"):
  70. raise Exception("Failed to delete a UPnP service")
  71. if "FAIL" not in dev[0].global_request("P2P_SERVICE_DEL upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice"):
  72. raise Exception("Unexpected deletion success for UPnP service")
  73. if "OK" not in dev[0].global_request("P2P_SERVICE_DEL bonjour 0b5f6166706f766572746370c00c000c01"):
  74. raise Exception("Failed to delete a Bonjour service")
  75. if "FAIL" not in dev[0].global_request("P2P_SERVICE_DEL bonjour 0b5f6166706f766572746370c00c000c01"):
  76. raise Exception("Unexpected deletion success for Bonjour service")
  77. return ev
  78. @remote_compatible
  79. def test_p2p_service_discovery(dev):
  80. """P2P service discovery"""
  81. addr0 = dev[0].p2p_dev_addr()
  82. for dst in [ "00:00:00:00:00:00", addr0 ]:
  83. ev = run_sd(dev, dst, "02000001")
  84. if "0b5f6166706f766572746370c00c000c01" not in ev:
  85. raise Exception("Unexpected service discovery response contents (Bonjour)")
  86. if "496e7465726e6574" not in ev:
  87. raise Exception("Unexpected service discovery response contents (UPnP)")
  88. for req in [ "foo 02000001",
  89. addr0,
  90. addr0 + " upnp qq urn:schemas-upnp-org:device:InternetGatewayDevice:1",
  91. addr0 + " upnp 10",
  92. addr0 + " 123",
  93. addr0 + " qq" ]:
  94. if "FAIL" not in dev[1].global_request("P2P_SERV_DISC_REQ " + req):
  95. raise Exception("Invalid P2P_SERV_DISC_REQ accepted: " + req)
  96. def test_p2p_service_discovery2(dev):
  97. """P2P service discovery with one peer having no services"""
  98. dev[2].p2p_listen()
  99. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  100. ev = run_sd(dev, dst, "02000001")
  101. if "0b5f6166706f766572746370c00c000c01" not in ev:
  102. raise Exception("Unexpected service discovery response contents (Bonjour)")
  103. if "496e7465726e6574" not in ev:
  104. raise Exception("Unexpected service discovery response contents (UPnP)")
  105. def test_p2p_service_discovery3(dev):
  106. """P2P service discovery for Bonjour with one peer having no services"""
  107. dev[2].p2p_listen()
  108. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  109. ev = run_sd(dev, dst, "02000101")
  110. if "0b5f6166706f766572746370c00c000c01" not in ev:
  111. raise Exception("Unexpected service discovery response contents (Bonjour)")
  112. def test_p2p_service_discovery4(dev):
  113. """P2P service discovery for UPnP with one peer having no services"""
  114. dev[2].p2p_listen()
  115. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  116. ev = run_sd(dev, dst, "02000201")
  117. if "496e7465726e6574" not in ev:
  118. raise Exception("Unexpected service discovery response contents (UPnP)")
  119. @remote_compatible
  120. def test_p2p_service_discovery_multiple_queries(dev):
  121. """P2P service discovery with multiple queries"""
  122. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  123. ev = run_sd(dev, dst, "02000201", query2="02000101")
  124. if "0b5f6166706f766572746370c00c000c01" not in ev[0] + ev[1]:
  125. raise Exception("Unexpected service discovery response contents (Bonjour)")
  126. if "496e7465726e6574" not in ev[0] + ev[1]:
  127. raise Exception("Unexpected service discovery response contents (UPnP)")
  128. def test_p2p_service_discovery_multiple_queries2(dev):
  129. """P2P service discovery with multiple queries with one peer having no services"""
  130. dev[2].p2p_listen()
  131. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  132. ev = run_sd(dev, dst, "02000201", query2="02000101")
  133. if "0b5f6166706f766572746370c00c000c01" not in ev[0] + ev[1]:
  134. raise Exception("Unexpected service discovery response contents (Bonjour)")
  135. if "496e7465726e6574" not in ev[0] + ev[1]:
  136. raise Exception("Unexpected service discovery response contents (UPnP)")
  137. def test_p2p_service_discovery_fragmentation(dev):
  138. """P2P service discovery with fragmentation"""
  139. for dst in [ "00:00:00:00:00:00", dev[0].p2p_dev_addr() ]:
  140. ev = run_sd(dev, dst, "02000001", fragment=True)
  141. if not "long response" in ev:
  142. if "0b5f6166706f766572746370c00c000c01" not in ev:
  143. raise Exception("Unexpected service discovery response contents (Bonjour)")
  144. if "496e7465726e6574" not in ev:
  145. raise Exception("Unexpected service discovery response contents (UPnP)")
  146. @remote_compatible
  147. def test_p2p_service_discovery_bonjour(dev):
  148. """P2P service discovery (Bonjour)"""
  149. ev = run_sd(dev, "00:00:00:00:00:00", "02000101")
  150. if "0b5f6166706f766572746370c00c000c01" not in ev:
  151. raise Exception("Unexpected service discovery response contents (Bonjour)")
  152. if "045f697070c00c000c01" not in ev:
  153. raise Exception("Unexpected service discovery response contents (Bonjour)")
  154. if "496e7465726e6574" in ev:
  155. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  156. @remote_compatible
  157. def test_p2p_service_discovery_bonjour2(dev):
  158. """P2P service discovery (Bonjour AFS)"""
  159. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c01")
  160. if "0b5f6166706f766572746370c00c000c01" not in ev:
  161. raise Exception("Unexpected service discovery response contents (Bonjour)")
  162. if "045f697070c00c000c01" in ev:
  163. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  164. if "496e7465726e6574" in ev:
  165. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  166. @remote_compatible
  167. def test_p2p_service_discovery_bonjour3(dev):
  168. """P2P service discovery (Bonjour AFS - no match)"""
  169. ev = run_sd(dev, "00:00:00:00:00:00", "130001010b5f6166706f766572746370c00c000c02")
  170. if "0300010102" not in ev:
  171. raise Exception("Requested-info-not-available was not indicated")
  172. if "0b5f6166706f766572746370c00c000c01" in ev:
  173. raise Exception("Unexpected service discovery response contents (Bonjour)")
  174. if "045f697070c00c000c01" in ev:
  175. raise Exception("Unexpected service discovery response contents (Bonjour mismatching)")
  176. if "496e7465726e6574" in ev:
  177. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  178. @remote_compatible
  179. def test_p2p_service_discovery_upnp(dev):
  180. """P2P service discovery (UPnP)"""
  181. ev = run_sd(dev, "00:00:00:00:00:00", "02000201")
  182. if "0b5f6166706f766572746370c00c000c01" in ev:
  183. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  184. if "496e7465726e6574" not in ev:
  185. raise Exception("Unexpected service discovery response contents (UPnP)")
  186. @remote_compatible
  187. def test_p2p_service_discovery_upnp2(dev):
  188. """P2P service discovery (UPnP using request helper)"""
  189. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:all", "0b00020110737364703a616c6c")
  190. if "0b5f6166706f766572746370c00c000c01" in ev:
  191. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  192. if "496e7465726e6574" not in ev:
  193. raise Exception("Unexpected service discovery response contents (UPnP)")
  194. @remote_compatible
  195. def test_p2p_service_discovery_upnp3(dev):
  196. """P2P service discovery (UPnP using request helper - no match)"""
  197. ev = run_sd(dev, "00:00:00:00:00:00", "upnp 10 ssdp:foo", "0b00020110737364703a666f6f")
  198. if "0300020102" not in ev:
  199. raise Exception("Requested-info-not-available was not indicated")
  200. if "0b5f6166706f766572746370c00c000c01" in ev:
  201. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  202. if "496e7465726e6574" in ev:
  203. raise Exception("Unexpected service discovery response contents (UPnP)")
  204. @remote_compatible
  205. def test_p2p_service_discovery_ws(dev):
  206. """P2P service discovery (WS-Discovery)"""
  207. ev = run_sd(dev, "00:00:00:00:00:00", "02000301")
  208. if "0b5f6166706f766572746370c00c000c01" in ev:
  209. raise Exception("Unexpected service discovery response contents (Bonjour not expected)")
  210. if "496e7465726e6574" in ev:
  211. raise Exception("Unexpected service discovery response contents (UPnP not expected)")
  212. if "0300030101" not in ev:
  213. raise Exception("Unexpected service discovery response contents (WS)")
  214. @remote_compatible
  215. def test_p2p_service_discovery_wfd(dev):
  216. """P2P service discovery (Wi-Fi Display)"""
  217. dev[0].global_request("SET wifi_display 1")
  218. ev = run_sd(dev, "00:00:00:00:00:00", "02000401")
  219. if " 030004" in ev:
  220. raise Exception("Unexpected response to invalid WFD SD query")
  221. dev[0].global_request("SET wifi_display 0")
  222. ev = run_sd(dev, "00:00:00:00:00:00", "0300040100")
  223. if "0300040101" not in ev:
  224. raise Exception("Unexpected response to WFD SD query (protocol was disabled)")
  225. @remote_compatible
  226. def test_p2p_service_discovery_req_cancel(dev):
  227. """Cancel a P2P service discovery request"""
  228. if "FAIL" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ ab"):
  229. raise Exception("Unexpected SD cancel success")
  230. if "FAIL" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ qq"):
  231. raise Exception("Unexpected SD cancel success")
  232. query = dev[0].global_request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  233. if "OK" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ " + query):
  234. raise Exception("Unexpected SD cancel failure")
  235. query1 = dev[0].global_request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000001")
  236. query2 = dev[0].global_request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000002")
  237. query3 = dev[0].global_request("P2P_SERV_DISC_REQ " + dev[1].p2p_dev_addr() + " 02000003")
  238. if "OK" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ " + query2):
  239. raise Exception("Unexpected SD cancel failure")
  240. if "OK" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ " + query1):
  241. raise Exception("Unexpected SD cancel failure")
  242. if "OK" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ " + query3):
  243. raise Exception("Unexpected SD cancel failure")
  244. query = dev[0].global_request("P2P_SERV_DISC_REQ 00:00:00:00:00:00 02000001")
  245. if "OK" not in dev[0].global_request("P2P_SERV_DISC_CANCEL_REQ " + query):
  246. raise Exception("Unexpected SD(broadcast) cancel failure")
  247. @remote_compatible
  248. def test_p2p_service_discovery_go(dev):
  249. """P2P service discovery from GO"""
  250. addr0 = dev[0].p2p_dev_addr()
  251. addr1 = dev[1].p2p_dev_addr()
  252. add_bonjour_services(dev[0])
  253. add_upnp_services(dev[0])
  254. dev[0].p2p_start_go(freq=2412)
  255. dev[1].global_request("P2P_FLUSH")
  256. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  257. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  258. raise Exception("Peer " + addr0 + " not found")
  259. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  260. if ev is None:
  261. raise Exception("Service discovery timed out")
  262. if addr1 not in ev:
  263. raise Exception("Unexpected service discovery request source")
  264. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  265. if ev is None:
  266. raise Exception("Service discovery timed out")
  267. if addr0 not in ev:
  268. raise Exception("Unexpected service discovery response source")
  269. if "0b5f6166706f766572746370c00c000c01" not in ev:
  270. raise Exception("Unexpected service discovery response contents (Bonjour)")
  271. if "496e7465726e6574" not in ev:
  272. raise Exception("Unexpected service discovery response contents (UPnP)")
  273. dev[1].p2p_stop_find()
  274. dev[0].global_request("P2P_SERVICE_FLUSH")
  275. dev[1].global_request("P2P_FLUSH")
  276. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  277. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  278. raise Exception("Peer " + addr0 + " not found")
  279. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  280. if ev is None:
  281. raise Exception("Service discovery timed out")
  282. if addr1 not in ev:
  283. raise Exception("Unexpected service discovery request source")
  284. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  285. if ev is None:
  286. raise Exception("Service discovery timed out")
  287. if addr0 not in ev:
  288. raise Exception("Unexpected service discovery response source")
  289. if "0300000101" not in ev:
  290. raise Exception("Unexpected service discovery response contents (Bonjour)")
  291. dev[1].p2p_stop_find()
  292. def _test_p2p_service_discovery_external(dev):
  293. addr0 = dev[0].p2p_dev_addr()
  294. addr1 = dev[1].p2p_dev_addr()
  295. if "FAIL" not in dev[0].global_request("P2P_SERV_DISC_EXTERNAL 2"):
  296. raise Exception("Invalid P2P_SERV_DISC_EXTERNAL accepted")
  297. if "OK" not in dev[0].global_request("P2P_SERV_DISC_EXTERNAL 1"):
  298. raise Exception("P2P_SERV_DISC_EXTERNAL failed")
  299. dev[0].p2p_listen()
  300. dev[1].global_request("P2P_FLUSH")
  301. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  302. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  303. raise Exception("Peer " + addr0 + " not found")
  304. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  305. if ev is None:
  306. raise Exception("Service discovery timed out")
  307. if addr1 not in ev:
  308. raise Exception("Unexpected service discovery request source")
  309. arg = ev.split(' ')
  310. resp = "0300000101"
  311. if "OK" not in dev[0].global_request("P2P_SERV_DISC_RESP %s %s %s %s" % (arg[2], arg[3], arg[4], resp)):
  312. raise Exception("P2P_SERV_DISC_RESP failed")
  313. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=15)
  314. if ev is None:
  315. raise Exception("Service discovery timed out")
  316. if addr0 not in ev:
  317. raise Exception("Unexpected address in SD Response: " + ev)
  318. if ev.split(' ')[4] != resp:
  319. raise Exception("Unexpected response data SD Response: " + ev)
  320. ver = ev.split(' ')[3]
  321. dev[0].global_request("P2P_SERVICE_UPDATE")
  322. dev[1].global_request("P2P_FLUSH")
  323. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  324. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  325. raise Exception("Peer " + addr0 + " not found")
  326. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=10)
  327. if ev is None:
  328. raise Exception("Service discovery timed out")
  329. if addr1 not in ev:
  330. raise Exception("Unexpected service discovery request source")
  331. arg = ev.split(' ')
  332. resp = "0300000101"
  333. if "OK" not in dev[0].global_request("P2P_SERV_DISC_RESP %s %s %s %s" % (arg[2], arg[3], arg[4], resp)):
  334. raise Exception("P2P_SERV_DISC_RESP failed")
  335. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=15)
  336. if ev is None:
  337. raise Exception("Service discovery timed out")
  338. if addr0 not in ev:
  339. raise Exception("Unexpected address in SD Response: " + ev)
  340. if ev.split(' ')[4] != resp:
  341. raise Exception("Unexpected response data SD Response: " + ev)
  342. ver2 = ev.split(' ')[3]
  343. if ver == ver2:
  344. raise Exception("Service list version did not change")
  345. for cmd in [ "%s%s%s%s" % (arg[2], arg[3], arg[4], resp),
  346. "%s %s %s %s" % ("0", arg[3], arg[4], resp),
  347. "%s %s %s %s" % (arg[2], "foo", arg[4], resp),
  348. "%s %s%s%s" % (arg[2], arg[3], arg[4], resp),
  349. "%s %s %s%s" % (arg[2], arg[3], arg[4], resp),
  350. "%s %s %s %s" % (arg[2], arg[3], arg[4], "12345"),
  351. "%s %s %s %s" % (arg[2], arg[3], arg[4], "qq") ]:
  352. if "FAIL" not in dev[0].global_request("P2P_SERV_DISC_RESP " + cmd):
  353. raise Exception("Invalid P2P_SERV_DISC_RESP accepted: " + cmd)
  354. @remote_compatible
  355. def test_p2p_service_discovery_external(dev):
  356. """P2P service discovery using external response"""
  357. try:
  358. _test_p2p_service_discovery_external(dev)
  359. finally:
  360. dev[0].global_request("P2P_SERV_DISC_EXTERNAL 0")
  361. @remote_compatible
  362. def test_p2p_service_discovery_invalid_commands(dev):
  363. """P2P service discovery invalid commands"""
  364. for cmd in [ "bonjour",
  365. "bonjour 12",
  366. "bonjour 123 12",
  367. "bonjour qq 12",
  368. "bonjour 12 123",
  369. "bonjour 12 qq",
  370. "upnp 10",
  371. "upnp qq uuid:",
  372. "foo bar" ]:
  373. if "FAIL" not in dev[0].global_request("P2P_SERVICE_ADD " + cmd):
  374. raise Exception("Invalid P2P_SERVICE_ADD accepted: " + cmd)
  375. for cmd in [ "bonjour",
  376. "bonjour 123",
  377. "bonjour qq",
  378. "upnp 10",
  379. "upnp ",
  380. "upnp qq uuid:",
  381. "foo bar" ]:
  382. if "FAIL" not in dev[0].global_request("P2P_SERVICE_DEL " + cmd):
  383. raise Exception("Invalid P2P_SERVICE_DEL accepted: " + cmd)
  384. def test_p2p_service_discovery_cancel_during_query(dev):
  385. """P2P service discovery and cancel during query"""
  386. for i in range(2):
  387. add_bonjour_services(dev[i])
  388. add_upnp_services(dev[i])
  389. add_extra_services(dev[i])
  390. dev[i].p2p_listen()
  391. dev[2].request("P2P_FLUSH")
  392. id1 = dev[2].request("P2P_SERV_DISC_REQ 00:00:00:00:00:00 02000201")
  393. id2 = dev[2].request("P2P_SERV_DISC_REQ 00:00:00:00:00:00 02000101")
  394. dev[2].p2p_find(social=True)
  395. ev = dev[2].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  396. if ev is None:
  397. raise Exception("Could not discover peer")
  398. if "OK" not in dev[2].request("P2P_SERV_DISC_CANCEL_REQ " + id1):
  399. raise Exception("Failed to cancel req1")
  400. if "OK" not in dev[2].request("P2P_SERV_DISC_CANCEL_REQ " + id2):
  401. raise Exception("Failed to cancel req2")
  402. ev = dev[2].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=3)
  403. # we may or may not get a response depending on timing, so ignore the result
  404. dev[2].p2p_stop_find()
  405. dev[1].p2p_stop_find()
  406. dev[0].p2p_stop_find()
  407. def get_p2p_state(dev):
  408. res = dev.global_request("STATUS")
  409. p2p_state = None
  410. for line in res.splitlines():
  411. if line.startswith("p2p_state="):
  412. p2p_state = line.split('=')[1]
  413. break
  414. if p2p_state is None:
  415. raise Exception("Could not get p2p_state")
  416. return p2p_state
  417. @remote_compatible
  418. def test_p2p_service_discovery_peer_not_listening(dev):
  419. """P2P service discovery and peer not listening"""
  420. addr0 = dev[0].p2p_dev_addr()
  421. addr1 = dev[1].p2p_dev_addr()
  422. add_bonjour_services(dev[0])
  423. add_upnp_services(dev[0])
  424. dev[0].p2p_listen()
  425. dev[1].global_request("P2P_FIND 4 type=social")
  426. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=4)
  427. if ev is None:
  428. raise Exception("Peer not found")
  429. dev[0].p2p_stop_find()
  430. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=1)
  431. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=1)
  432. time.sleep(0.03)
  433. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  434. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=1)
  435. if ev is not None:
  436. raise Exception("Service discovery request unexpectedly received")
  437. ev = dev[1].wait_global_event(["P2P-FIND-STOPPED", "P2P-SERV-DISC-RESP"],
  438. timeout=10)
  439. if ev is None:
  440. raise Exception("P2P-FIND-STOPPED event timed out")
  441. if "P2P-SERV-DISC-RESP" in ev:
  442. raise Exception("Unexpected SD response")
  443. p2p_state = get_p2p_state(dev[1])
  444. if p2p_state != "IDLE":
  445. raise Exception("Unexpected p2p_state after P2P_FIND timeout: " + p2p_state)
  446. @remote_compatible
  447. def test_p2p_service_discovery_peer_not_listening2(dev):
  448. """P2P service discovery and peer not listening"""
  449. addr0 = dev[0].p2p_dev_addr()
  450. addr1 = dev[1].p2p_dev_addr()
  451. add_bonjour_services(dev[0])
  452. add_upnp_services(dev[0])
  453. dev[0].p2p_listen()
  454. dev[1].global_request("P2P_FIND type=social")
  455. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  456. if ev is None:
  457. raise Exception("Peer not found")
  458. dev[0].p2p_stop_find()
  459. time.sleep(0.53)
  460. dev[1].request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  461. ev = dev[0].wait_global_event(["P2P-SERV-DISC-REQ"], timeout=0.5)
  462. if ev is not None:
  463. raise Exception("Service discovery request unexpectedly received")
  464. dev[1].p2p_stop_find()
  465. ev = dev[1].wait_global_event(["P2P-FIND-STOPPED", "P2P-SERV-DISC-RESP"],
  466. timeout=10)
  467. if ev is None:
  468. raise Exception("P2P-FIND-STOPPED event timed out")
  469. if "P2P-SERV-DISC-RESP" in ev:
  470. raise Exception("Unexpected SD response")
  471. p2p_state = get_p2p_state(dev[1])
  472. if p2p_state != "IDLE":
  473. raise Exception("Unexpected p2p_state after P2P_FIND timeout: " + p2p_state)
  474. def test_p2p_service_discovery_restart(dev):
  475. """P2P service discovery restarted immediately"""
  476. try:
  477. _test_p2p_service_discovery_restart(dev)
  478. finally:
  479. dev[1].global_request("P2P_SET disc_int 1 3 -1")
  480. def _test_p2p_service_discovery_restart(dev):
  481. addr0 = dev[0].p2p_dev_addr()
  482. addr1 = dev[1].p2p_dev_addr()
  483. # Use shorter listen interval to keep P2P_FIND loop shorter.
  484. dev[1].global_request("P2P_SET disc_int 1 1 10")
  485. add_bonjour_services(dev[0])
  486. #add_upnp_services(dev[0])
  487. dev[0].p2p_listen()
  488. dev[1].global_request("P2P_FLUSH")
  489. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  490. if not dev[1].discover_peer(addr0, social=True, force_find=True):
  491. raise Exception("Peer " + addr0 + " not found")
  492. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  493. if ev is None:
  494. raise Exception("Service discovery timed out")
  495. # The following P2P_LISTEN operation used to get delayed due to the last
  496. # Action frame TX operation in SD Response using wait_time of 200 ms. It is
  497. # somewhat difficult to test for this automatically, but the debug log can
  498. # be verified to see that the remain-on-channel event for operation arrives
  499. # immediately instead of getting delayed 200 ms. We can use a maximum
  500. # acceptable time for the SD Response, but need to keep the limit somewhat
  501. # high to avoid making this fail under heavy load. Still, it is apparently
  502. # possible for this to take about the same amount of time with fixed
  503. # implementation every now and then, so run this multiple time and pass the
  504. # test if any attempt is fast enough.
  505. for i in range(10):
  506. dev[0].p2p_stop_find()
  507. time.sleep(0.01)
  508. dev[0].p2p_listen()
  509. dev[1].global_request("P2P_SERV_DISC_REQ " + addr0 + " 02000001")
  510. start = os.times()[4]
  511. ev = dev[1].wait_global_event(["P2P-SERV-DISC-RESP"], timeout=10)
  512. if ev is None:
  513. raise Exception("Service discovery timed out")
  514. end = os.times()[4]
  515. logger.info("Second SD Response in " + str(end - start) + " seconds")
  516. if end - start < 0.8:
  517. break
  518. if end - start > 0.8:
  519. raise Exception("Unexpectedly slow second SD Response: " + str(end - start) + " seconds")