Debian Codegraph @

tests/gtest/gupnp-simple-igd.c

raw
1 /* GUPNP Simple IGD unit tests
2 *
3 * Copyright (C) 2008 Collabora, Nokia
4 * @author: Olivier Crete <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <glib.h>
26
27 #include <string.h>
28
29 #include "libgupnp-igd/gupnp-simple-igd.h"
30 #include "libgupnp-igd/gupnp-simple-igd-thread.h"
31
32 #include <libgupnp/gupnp.h>
33
34 #define IP_ADDRESS_FIRST "127.0.0.2"
35 #define IP_ADDRESS_SECOND "127.0.0.3"
36 #define PPP_ADDRESS_FIRST "127.0.0.4"
37 #define PPP_ADDRESS_SECOND "127.0.0.5"
38
39 #define INTERNAL_PORT 6543
40
41 typedef enum {
42 CONNECTION_IP,
43 CONNECTION_PPP
44 } ConnectionType;
45
46 static GMainLoop *loop = NULL;
47
48 static GUPnPServiceInfo *ipservice = NULL;
49 static GUPnPServiceInfo *pppservice = NULL;
50
51 gboolean return_conflict = FALSE;
52 gboolean dispose_removes = FALSE;
53 gboolean local_remove = FALSE;
54 gchar *invalid_ip = NULL;
55
56 static void
57 test_gupnp_simple_igd_new (void)
58 {
59 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
60 GUPnPSimpleIgdThread *igdthread = gupnp_simple_igd_thread_new ();
61 GUPnPSimpleIgdThread *igdthread1 = gupnp_simple_igd_thread_new ();
62
63 g_object_unref (igd);
64 g_object_unref (igdthread);
65 g_object_unref (igdthread1);
66 }
67
68
69 static void
70 get_external_ip_address_cb (GUPnPService *service,
71 GUPnPServiceAction *action,
72 gpointer user_data)
73 {
74 ConnectionType ct = GPOINTER_TO_INT (user_data);
75
76 if (invalid_ip)
77 gupnp_service_action_set (action,
78 "NewExternalIPAddress", G_TYPE_STRING, invalid_ip,
79 NULL);
80 else if (ct == CONNECTION_IP)
81 gupnp_service_action_set (action,
82 "NewExternalIPAddress", G_TYPE_STRING, IP_ADDRESS_FIRST,
83 NULL);
84 else if (ct == CONNECTION_PPP)
85 gupnp_service_action_set (action,
86 "NewExternalIPAddress", G_TYPE_STRING, PPP_ADDRESS_FIRST,
87 NULL);
88 else
89 g_assert_not_reached ();
90
91 gupnp_service_action_return_success (action);
92
93 }
94
95 static void
96 add_port_mapping_cb (GUPnPService *service,
97 GUPnPServiceAction *action,
98 gpointer user_data)
99 {
100 guint requested_external_port = GPOINTER_TO_UINT (user_data);
101 gchar *remote_host = NULL;
102 guint external_port = 0;
103 gchar *proto = NULL;
104 guint internal_port = 0;
105 gchar *internal_client = NULL;
106 gboolean enabled = -1;
107 gchar *desc = NULL;
108 guint lease = 0;
109
110 gupnp_service_action_get (action,
111 "NewRemoteHost", G_TYPE_STRING, &remote_host,
112 "NewExternalPort", G_TYPE_UINT, &external_port,
113 "NewProtocol", G_TYPE_STRING, &proto,
114 "NewInternalPort", G_TYPE_UINT, &internal_port,
115 "NewInternalClient", G_TYPE_STRING, &internal_client,
116 "NewEnabled", G_TYPE_BOOLEAN, &enabled,
117 "NewPortMappingDescription", G_TYPE_STRING, &desc,
118 "NewLeaseDuration", G_TYPE_UINT, &lease,
119 NULL);
120
121 g_assert (external_port);
122 g_assert (remote_host && !strcmp (remote_host, ""));
123 g_assert (proto && (!strcmp (proto, "UDP") || !strcmp (proto, "TCP")));
124 g_assert (internal_port == INTERNAL_PORT);
125 g_assert (internal_client && !strcmp (internal_client, "192.168.4.22"));
126 g_assert (enabled == TRUE);
127 g_assert (desc != NULL);
128 g_assert (lease == 10);
129
130 g_free (remote_host);
131 g_free (proto);
132 g_free (internal_client);
133 g_free (desc);
134
135 if (requested_external_port)
136 g_assert (external_port == requested_external_port);
137
138
139 if (return_conflict && external_port == INTERNAL_PORT)
140 gupnp_service_action_return_error (action, 718, "ConflictInMappingEntry");
141 else
142 gupnp_service_action_return_success (action);
143 }
144
145 static gboolean
146 loop_quit (gpointer user_data) {
147 g_main_loop_quit (loop);
148
149 return FALSE;
150 }
151
152 static void
153 delete_port_mapping_cb (GUPnPService *service,
154 GUPnPServiceAction *action,
155 gpointer user_data)
156 {
157 guint requested_external_port = GPOINTER_TO_UINT (user_data);
158 gchar *remote_host = NULL;
159 guint external_port = 0;
160 gchar *proto = NULL;
161
162 gupnp_service_action_get (action,
163 "NewRemoteHost", G_TYPE_STRING, &remote_host,
164 "NewExternalPort", G_TYPE_UINT, &external_port,
165 "NewProtocol", G_TYPE_STRING, &proto,
166 NULL);
167
168 g_assert (remote_host != NULL);
169 if (requested_external_port || !return_conflict)
170 g_assert (external_port == INTERNAL_PORT);
171 else
172 g_assert (external_port != INTERNAL_PORT);
173 g_assert (proto && !strcmp (proto, "UDP"));
174
175 gupnp_service_action_return_success (action);
176
177 g_free (remote_host);
178 g_free (proto);
179
180 GSource* src = g_idle_source_new ();
181 g_source_set_callback (src, loop_quit, NULL, NULL);
182 g_source_attach (src, g_main_context_get_thread_default ());
183 }
184
185 typedef struct _MappedData {
186 GMainContext *context;
187 const char *ip_address;
188 guint port;
189 } MappedData;
190
191 gboolean service_notify (gpointer user_data) {
192 MappedData *d = (MappedData *) user_data;
193 gupnp_service_notify (GUPNP_SERVICE (ipservice),
194 "ExternalIPAddress", G_TYPE_STRING, d->ip_address, NULL);
195
196 return G_SOURCE_REMOVE;
197 }
198
199 static void
200 mapped_external_port_cb (GUPnPSimpleIgd *igd, gchar *proto,
201 gchar *external_ip, gchar *replaces_external_ip, guint external_port,
202 gchar *local_ip, guint local_port, gchar *description, gpointer user_data)
203 {
204
205 MappedData *d = (MappedData *) user_data;
206 guint requested_external_port = d->port;
207
208 g_assert (invalid_ip == NULL);
209
210 if (requested_external_port)
211 g_assert (external_port == requested_external_port);
212 else if (return_conflict)
213 g_assert (external_port != INTERNAL_PORT);
214 else
215 g_assert (external_port == INTERNAL_PORT);
216 g_assert (proto && !strcmp (proto, "UDP"));
217 g_assert (local_port == INTERNAL_PORT);
218 g_assert (local_ip && !strcmp (local_ip, "192.168.4.22"));
219 g_assert (description != NULL);
220 g_assert (external_ip);
221
222 if (replaces_external_ip)
223 {
224 g_assert ((!strcmp (replaces_external_ip, IP_ADDRESS_FIRST) &&
225 !strcmp (external_ip, IP_ADDRESS_SECOND)) ||
226 (!strcmp (replaces_external_ip, PPP_ADDRESS_FIRST) &&
227 !strcmp (external_ip, PPP_ADDRESS_SECOND)));
228 if (dispose_removes)
229 g_object_unref (igd);
230 else if (local_remove)
231 gupnp_simple_igd_remove_port_local (igd, proto, local_ip, local_port);
232 else
233 gupnp_simple_igd_remove_port (igd, proto, requested_external_port);
234 }
235 else
236 {
237 if (!strcmp (external_ip, IP_ADDRESS_FIRST)) {
238 d->ip_address = IP_ADDRESS_SECOND;
239 g_main_context_invoke(d->context, service_notify, d);
240 } else if (!strcmp (external_ip, PPP_ADDRESS_FIRST)) {
241 d->ip_address = PPP_ADDRESS_SECOND;
242 g_main_context_invoke(d->context, service_notify, d);
243 } else
244 g_assert_not_reached ();
245 }
246 }
247
248 static void
249 error_mapping_port_cb (GUPnPSimpleIgd *igd, GError *error, gchar *proto,
250 guint external_port, gchar *local_ip, guint local_port,
251 gchar *description, gpointer user_data)
252 {
253 g_assert (proto && !strcmp (proto, "UDP"));
254 g_assert (local_ip && !strcmp (local_ip, "192.168.4.22"));
255 g_assert (description != NULL);
256 g_assert (local_port == INTERNAL_PORT);
257
258 if (invalid_ip && error->domain != GUPNP_CONTROL_ERROR)
259 {
260 g_assert (error);
261 g_assert (error->domain == GUPNP_SIMPLE_IGD_ERROR);
262 g_assert (error->code == GUPNP_SIMPLE_IGD_ERROR_EXTERNAL_ADDRESS);
263 g_assert (error->message);
264 g_main_loop_quit (loop);
265 }
266 #if 0
267 else
268 g_assert_not_reached ();
269 #endif
270 }
271
272 static gboolean
273 ignore_non_localhost (GUPnPSimpleIgd *igd, GUPnPContext *gupnp_context,
274 gpointer user_data)
275 {
276 if (!g_strcmp0 (gssdp_client_get_interface (GSSDP_CLIENT (gupnp_context)),
277 "lo"))
278 return FALSE;
279 else
280 return TRUE;
281 }
282
283 static void
284 run_gupnp_simple_igd_test (GMainContext *mainctx, GUPnPSimpleIgd *igd,
285 guint requested_port)
286 {
287 GUPnPContext *context;
288 GUPnPRootDevice *dev;
289 GUPnPDeviceInfo *subdev1;
290 GUPnPDeviceInfo *subdev2;
291 const gchar *xml_path = ".";
292 GError *error = NULL;
293 GInetAddress *loopback = NULL;
294
295 g_signal_connect (igd, "context-available",
296 G_CALLBACK (ignore_non_localhost), NULL);
297
298 if (mainctx)
299 g_main_context_push_thread_default (mainctx);
300 loopback = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
301 context = gupnp_context_new_for_address (loopback, 0, GSSDP_UDA_VERSION_1_0, NULL);
302 g_object_unref (loopback);
303 g_assert (context);
304
305 if (g_getenv ("XML_PATH"))
306 xml_path = g_getenv ("XML_PATH");
307
308
309 dev = gupnp_root_device_new (context, "InternetGatewayDevice.xml", xml_path, &error);
310 g_assert (dev);
311 g_assert (error == NULL);
312
313 subdev1 = gupnp_device_info_get_device (GUPNP_DEVICE_INFO (dev),
314 "urn:schemas-upnp-org:device:WANDevice:1");
315 g_assert (subdev1);
316
317 subdev2 = gupnp_device_info_get_device (subdev1,
318 "urn:schemas-upnp-org:device:WANConnectionDevice:1");
319 g_assert (subdev2);
320 g_object_unref (subdev1);
321
322 ipservice = gupnp_device_info_get_service (subdev2,
323 "urn:schemas-upnp-org:service:WANIPConnection:1");
324 g_assert (ipservice);
325 pppservice = gupnp_device_info_get_service (subdev2,
326 "urn:schemas-upnp-org:service:WANPPPConnection:1");
327 g_assert (pppservice);
328 g_object_unref (subdev2);
329
330 g_signal_connect (ipservice, "action-invoked::GetExternalIPAddress",
331 G_CALLBACK (get_external_ip_address_cb), GINT_TO_POINTER (CONNECTION_IP));
332 g_signal_connect (ipservice, "action-invoked::AddPortMapping",
333 G_CALLBACK (add_port_mapping_cb), GUINT_TO_POINTER (requested_port));;
334 g_signal_connect (ipservice, "action-invoked::DeletePortMapping",
335 G_CALLBACK (delete_port_mapping_cb), GUINT_TO_POINTER (requested_port));
336
337 g_signal_connect (pppservice, "action-invoked::GetExternalIPAddress",
338 G_CALLBACK (get_external_ip_address_cb),
339 GINT_TO_POINTER (CONNECTION_PPP));
340 g_signal_connect (pppservice, "action-invoked::AddPortMapping",
341 G_CALLBACK (add_port_mapping_cb), GUINT_TO_POINTER (requested_port));
342 g_signal_connect (pppservice, "action-invoked::DeletePortMapping",
343 G_CALLBACK (delete_port_mapping_cb), GUINT_TO_POINTER (requested_port));
344
345
346 gupnp_root_device_set_available (dev, TRUE);
347
348 MappedData d;
349 d.context = mainctx;
350 d.port = requested_port;
351
352 g_signal_connect (igd, "mapped-external-port",
353 G_CALLBACK (mapped_external_port_cb), &d);
354 g_signal_connect (igd, "error-mapping-port",
355 G_CALLBACK (error_mapping_port_cb), NULL);
356
357 gupnp_simple_igd_add_port (igd, "UDP", requested_port, "192.168.4.22",
358 INTERNAL_PORT, 10, "GUPnP Simple IGD test");
359
360 loop = g_main_loop_new (mainctx, FALSE);
361 g_main_loop_run (loop);
362 g_main_loop_unref (loop);
363
364 gupnp_root_device_set_available (dev, FALSE);
365 g_object_unref (dev);
366
367 if (mainctx)
368 g_main_context_pop_thread_default (mainctx);
369 g_object_unref (context);
370
371 }
372
373 static void
374 test_gupnp_simple_igd_default_ctx (void)
375 {
376 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
377
378 run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
379 g_object_unref (igd);
380 }
381
382 static void
383 test_gupnp_simple_igd_default_ctx_local (void)
384 {
385 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
386
387 local_remove = TRUE;
388
389 run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
390 g_object_unref (igd);
391
392 local_remove = FALSE;
393 }
394
395 static void
396 test_gupnp_simple_igd_custom_ctx (void)
397 {
398 GMainContext *mainctx = g_main_context_new ();
399 GUPnPSimpleIgd *igd;
400
401 g_main_context_push_thread_default (mainctx);
402 igd = gupnp_simple_igd_new ();
403 g_main_context_pop_thread_default (mainctx);
404
405 run_gupnp_simple_igd_test (mainctx, igd, INTERNAL_PORT);
406 g_object_unref (igd);
407 g_main_context_unref (mainctx);
408 }
409
410
411 static void
412 test_gupnp_simple_igd_thread (void)
413 {
414 GUPnPSimpleIgdThread *igd = gupnp_simple_igd_thread_new ();
415 GMainContext *mainctx = g_main_context_new ();
416
417 run_gupnp_simple_igd_test (mainctx, GUPNP_SIMPLE_IGD (igd), INTERNAL_PORT);
418 g_object_unref (igd);
419 g_main_context_unref (mainctx);
420 }
421
422
423 static void
424 test_gupnp_simple_igd_random_no_conflict (void)
425 {
426 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
427
428 run_gupnp_simple_igd_test (NULL, igd, 0);
429 g_object_unref (igd);
430 }
431
432
433 static void
434 test_gupnp_simple_igd_random_conflict (void)
435 {
436 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
437
438 return_conflict = TRUE;
439 run_gupnp_simple_igd_test (NULL, igd, 0);
440 return_conflict = FALSE;
441 g_object_unref (igd);
442 }
443
444
445 static void
446 test_gupnp_simple_igd_dispose_removes (void)
447 {
448 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
449
450 dispose_removes = TRUE;
451 run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
452 dispose_removes = FALSE;
453 }
454
455
456 static void
457 test_gupnp_simple_igd_dispose_removes_thread (void)
458 {
459 GUPnPSimpleIgdThread *igd = gupnp_simple_igd_thread_new ();
460 GMainContext *mainctx = g_main_context_new ();
461
462 dispose_removes = TRUE;
463 run_gupnp_simple_igd_test (mainctx, GUPNP_SIMPLE_IGD (igd), INTERNAL_PORT);
464 dispose_removes = FALSE;
465 g_main_context_unref (mainctx);
466 }
467
468
469 static void
470 test_gupnp_simple_igd_invalid_ip(void)
471 {
472 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
473
474 invalid_ip = "asdas";
475 run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
476 invalid_ip = NULL;
477 g_object_unref (igd);
478 }
479 static void
480 test_gupnp_simple_igd_empty_ip(void)
481 {
482 GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
483
484 invalid_ip = "";
485 run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
486 invalid_ip = NULL;
487 g_object_unref (igd);
488 }
489
490
491 int main (int argc, char **argv)
492 {
493 g_test_init (&argc, &argv, NULL);
494
495 g_test_add_func("/simpleigd/new", test_gupnp_simple_igd_new);
496 g_test_add_func ("/simpleigd/default_ctx", test_gupnp_simple_igd_default_ctx);
497 g_test_add_func ("/simpleigd/default_ctx/remove_local",
498 test_gupnp_simple_igd_default_ctx_local);
499 g_test_add_func ("/simpleigd/custom_ctx", test_gupnp_simple_igd_custom_ctx);
500 g_test_add_func ("/simpleigd/thread", test_gupnp_simple_igd_thread);
501 g_test_add_func ("/simpleigd/random/no_conflict",
502 test_gupnp_simple_igd_random_no_conflict);
503 g_test_add_func ("/simpleigd/random/conflict",
504 test_gupnp_simple_igd_random_conflict);
505 g_test_add_func ("/simpleigd/dispose_removes/regular",
506 test_gupnp_simple_igd_dispose_removes);
507 g_test_add_func ("/simpleigd/dispose_removes/thread",
508 test_gupnp_simple_igd_dispose_removes_thread);
509 g_test_add_func ("/simpleigd/invalid_ip",
510 test_gupnp_simple_igd_invalid_ip);
511 g_test_add_func ("/simpleigd/empty_ip",
512 test_gupnp_simple_igd_empty_ip);
513
514 g_test_run ();
515
516 return 0;
517 }
518