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