Add some more cases to the app-id unit tests
[glib.git] / gio / tests / network-monitor.c
blob2e38e39f6533776bca2bdab304610ab37782f356
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "gio.h"
21 /* hack */
22 #define GIO_COMPILATION
23 #include "gnetworkmonitorbase.h"
25 #include <string.h>
27 /* Test data; the GInetAddresses and GInetAddressMasks get filled in
28 * in main(). Each address in a TestAddress matches the mask in its
29 * corresponding TestMask, and none of them match any of the other
30 * masks. The addresses in unmatched don't match any of the masks.
33 typedef struct {
34 const char *string;
35 GInetAddress *address;
36 } TestAddress;
38 typedef struct {
39 const char *mask_string;
40 GInetAddressMask *mask;
41 TestAddress *addresses;
42 } TestMask;
44 TestAddress net127addrs[] = {
45 { "127.0.0.1", NULL },
46 { "127.0.0.2", NULL },
47 { "127.0.0.255", NULL },
48 { "127.0.1.0", NULL },
49 { "127.0.255.0", NULL },
50 { "127.0.255.0", NULL },
51 { "127.255.255.255", NULL },
52 { NULL, NULL }
54 TestMask net127 = { "127.0.0.0/8", NULL, net127addrs };
56 TestAddress net10addrs[] = {
57 { "10.0.0.1", NULL },
58 { "10.0.0.2", NULL },
59 { "10.0.0.255", NULL },
60 { NULL, NULL }
62 TestMask net10 = { "10.0.0.0/24", NULL, net10addrs };
64 TestAddress net192addrs[] = {
65 { "192.168.0.1", NULL },
66 { "192.168.0.2", NULL },
67 { "192.168.0.255", NULL },
68 { "192.168.1.0", NULL },
69 { "192.168.15.0", NULL },
70 { NULL, NULL }
72 TestMask net192 = { "192.168.0.0/20", NULL, net192addrs };
74 TestAddress netlocal6addrs[] = {
75 { "::1", NULL },
76 { NULL, NULL }
78 TestMask netlocal6 = { "::1/128", NULL, netlocal6addrs };
80 TestAddress netfe80addrs[] = {
81 { "fe80::", NULL },
82 { "fe80::1", NULL },
83 { "fe80::21b:77ff:fea2:972a", NULL },
84 { NULL, NULL }
86 TestMask netfe80 = { "fe80::/64", NULL, netfe80addrs };
88 TestAddress unmatched[] = {
89 { "10.0.1.0", NULL },
90 { "10.0.255.0", NULL },
91 { "10.255.255.255", NULL },
92 { "192.168.16.0", NULL },
93 { "192.168.255.0", NULL },
94 { "192.169.0.0", NULL },
95 { "192.255.255.255", NULL },
96 { "::2", NULL },
97 { "1::1", NULL },
98 { "fe80::1:0:0:0:0", NULL },
99 { "fe80:8000::0:0:0:0", NULL },
100 { NULL, NULL }
103 GInetAddressMask *ip4_default, *ip6_default;
105 static void
106 notify_handler (GObject *object,
107 GParamSpec *pspec,
108 gpointer user_data)
110 gboolean *emitted = user_data;
112 *emitted = TRUE;
115 static void
116 network_changed_handler (GNetworkMonitor *monitor,
117 gboolean available,
118 gpointer user_data)
120 gboolean *emitted = user_data;
122 *emitted = TRUE;
125 static void
126 assert_signals (GNetworkMonitor *monitor,
127 gboolean should_emit_notify,
128 gboolean should_emit_network_changed,
129 gboolean expected_network_available)
131 gboolean emitted_notify = FALSE, emitted_network_changed = FALSE;
132 guint h1, h2;
134 h1 = g_signal_connect (monitor, "notify::network-available",
135 G_CALLBACK (notify_handler),
136 &emitted_notify);
137 h2 = g_signal_connect (monitor, "network-changed",
138 G_CALLBACK (network_changed_handler),
139 &emitted_network_changed);
141 g_main_context_iteration (NULL, FALSE);
143 g_signal_handler_disconnect (monitor, h1);
144 g_signal_handler_disconnect (monitor, h2);
146 g_assert (emitted_notify == should_emit_notify);
147 g_assert (emitted_network_changed == should_emit_network_changed);
149 g_assert (g_network_monitor_get_network_available (monitor) == expected_network_available);
152 typedef struct {
153 GNetworkMonitor *monitor;
154 GMainLoop *loop;
155 GSocketAddress *sockaddr;
156 gboolean should_be_reachable;
157 } CanReachData;
159 static void
160 reach_cb (GObject *source,
161 GAsyncResult *res,
162 gpointer user_data)
164 GError *error = NULL;
165 gboolean reachable;
166 CanReachData *data = user_data;
168 reachable = g_network_monitor_can_reach_finish (data->monitor, res, &error);
170 if (data->should_be_reachable)
171 g_assert_no_error (error);
172 else
174 g_assert (error != NULL);
175 g_clear_error (&error);
177 g_assert (reachable == data->should_be_reachable);
179 g_main_loop_quit (data->loop);
182 static gboolean
183 test_reach_async (gpointer user_data)
185 CanReachData *data = user_data;
187 g_network_monitor_can_reach_async (data->monitor,
188 G_SOCKET_CONNECTABLE (data->sockaddr),
189 NULL,
190 reach_cb,
191 data);
192 return G_SOURCE_REMOVE;
195 static void
196 run_tests (GNetworkMonitor *monitor,
197 TestAddress *addresses,
198 gboolean should_be_reachable)
200 GError *error = NULL;
201 int i;
202 gboolean reachable;
203 GSocketAddress *sockaddr;
204 CanReachData data;
206 data.monitor = monitor;
207 data.loop = g_main_loop_new (NULL, FALSE);
209 for (i = 0; addresses[i].address; i++)
211 sockaddr = g_inet_socket_address_new (addresses[i].address, 0);
212 reachable = g_network_monitor_can_reach (monitor,
213 G_SOCKET_CONNECTABLE (sockaddr),
214 NULL, &error);
215 data.sockaddr = sockaddr;
216 data.should_be_reachable = should_be_reachable;
218 g_idle_add (test_reach_async, &data);
219 g_main_loop_run (data.loop);
221 g_object_unref (sockaddr);
222 g_assert_cmpint (reachable, ==, should_be_reachable);
223 if (should_be_reachable)
224 g_assert_no_error (error);
225 else
227 g_assert (error != NULL);
228 g_clear_error (&error);
232 g_main_loop_unref (data.loop);
235 static void
236 test_default (void)
238 GNetworkMonitor *monitor, *m;
239 GError *error = NULL;
241 m = g_network_monitor_get_default ();
242 g_assert (G_IS_NETWORK_MONITOR (m));
244 monitor = g_initable_newv (G_TYPE_NETWORK_MONITOR_BASE, 0, NULL, NULL, &error);
245 g_assert_no_error (error);
247 /* In the default configuration, all addresses are reachable */
248 run_tests (monitor, net127.addresses, TRUE);
249 run_tests (monitor, net10.addresses, TRUE);
250 run_tests (monitor, net192.addresses, TRUE);
251 run_tests (monitor, netlocal6.addresses, TRUE);
252 run_tests (monitor, netfe80.addresses, TRUE);
253 run_tests (monitor, unmatched, TRUE);
255 assert_signals (monitor, FALSE, FALSE, TRUE);
257 g_object_unref (monitor);
260 static void
261 test_remove_default (void)
263 GNetworkMonitor *monitor;
264 GError *error = NULL;
266 monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
267 g_assert_no_error (error);
268 assert_signals (monitor, FALSE, FALSE, TRUE);
270 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
271 ip4_default);
272 assert_signals (monitor, FALSE, TRUE, TRUE);
273 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
274 ip6_default);
275 assert_signals (monitor, TRUE, TRUE, FALSE);
277 /* Now nothing should be reachable */
278 run_tests (monitor, net127.addresses, FALSE);
279 run_tests (monitor, net10.addresses, FALSE);
280 run_tests (monitor, net192.addresses, FALSE);
281 run_tests (monitor, netlocal6.addresses, FALSE);
282 run_tests (monitor, netfe80.addresses, FALSE);
283 run_tests (monitor, unmatched, FALSE);
285 g_object_unref (monitor);
288 static void
289 test_add_networks (void)
291 GNetworkMonitor *monitor;
292 GError *error = NULL;
294 monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
295 g_assert_no_error (error);
296 assert_signals (monitor, FALSE, FALSE, TRUE);
298 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
299 ip4_default);
300 assert_signals (monitor, FALSE, TRUE, TRUE);
301 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
302 ip6_default);
303 assert_signals (monitor, TRUE, TRUE, FALSE);
305 /* Now add the masks one by one */
307 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
308 net127.mask);
309 assert_signals (monitor, FALSE, TRUE, FALSE);
311 run_tests (monitor, net127.addresses, TRUE);
312 run_tests (monitor, net10.addresses, FALSE);
313 run_tests (monitor, net192.addresses, FALSE);
314 run_tests (monitor, netlocal6.addresses, FALSE);
315 run_tests (monitor, netfe80.addresses, FALSE);
316 run_tests (monitor, unmatched, FALSE);
318 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
319 net10.mask);
320 assert_signals (monitor, FALSE, TRUE, FALSE);
321 run_tests (monitor, net127.addresses, TRUE);
322 run_tests (monitor, net10.addresses, TRUE);
323 run_tests (monitor, net192.addresses, FALSE);
324 run_tests (monitor, netlocal6.addresses, FALSE);
325 run_tests (monitor, netfe80.addresses, FALSE);
326 run_tests (monitor, unmatched, FALSE);
328 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
329 net192.mask);
330 assert_signals (monitor, FALSE, TRUE, FALSE);
331 run_tests (monitor, net127.addresses, TRUE);
332 run_tests (monitor, net10.addresses, TRUE);
333 run_tests (monitor, net192.addresses, TRUE);
334 run_tests (monitor, netlocal6.addresses, FALSE);
335 run_tests (monitor, netfe80.addresses, FALSE);
336 run_tests (monitor, unmatched, FALSE);
338 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
339 netlocal6.mask);
340 assert_signals (monitor, FALSE, TRUE, FALSE);
341 run_tests (monitor, net127.addresses, TRUE);
342 run_tests (monitor, net10.addresses, TRUE);
343 run_tests (monitor, net192.addresses, TRUE);
344 run_tests (monitor, netlocal6.addresses, TRUE);
345 run_tests (monitor, netfe80.addresses, FALSE);
346 run_tests (monitor, unmatched, FALSE);
348 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
349 netfe80.mask);
350 assert_signals (monitor, FALSE, TRUE, FALSE);
351 run_tests (monitor, net127.addresses, TRUE);
352 run_tests (monitor, net10.addresses, TRUE);
353 run_tests (monitor, net192.addresses, TRUE);
354 run_tests (monitor, netlocal6.addresses, TRUE);
355 run_tests (monitor, netfe80.addresses, TRUE);
356 run_tests (monitor, unmatched, FALSE);
358 g_object_unref (monitor);
361 static void
362 test_remove_networks (void)
364 GNetworkMonitor *monitor;
365 GError *error = NULL;
367 monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
368 g_assert_no_error (error);
369 assert_signals (monitor, FALSE, FALSE, TRUE);
371 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
372 ip4_default);
373 assert_signals (monitor, FALSE, TRUE, TRUE);
374 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
375 ip6_default);
376 assert_signals (monitor, TRUE, TRUE, FALSE);
378 /* First add them */
379 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
380 net127.mask);
381 assert_signals (monitor, FALSE, TRUE, FALSE);
382 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
383 net10.mask);
384 assert_signals (monitor, FALSE, TRUE, FALSE);
385 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
386 net192.mask);
387 assert_signals (monitor, FALSE, TRUE, FALSE);
388 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
389 netlocal6.mask);
390 assert_signals (monitor, FALSE, TRUE, FALSE);
391 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
392 netfe80.mask);
393 assert_signals (monitor, FALSE, TRUE, FALSE);
395 run_tests (monitor, net127.addresses, TRUE);
396 run_tests (monitor, net10.addresses, TRUE);
397 run_tests (monitor, net192.addresses, TRUE);
398 run_tests (monitor, netlocal6.addresses, TRUE);
399 run_tests (monitor, netfe80.addresses, TRUE);
400 run_tests (monitor, unmatched, FALSE);
402 /* Now remove them one by one */
403 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
404 net127.mask);
405 assert_signals (monitor, FALSE, TRUE, FALSE);
406 run_tests (monitor, net127.addresses, FALSE);
407 run_tests (monitor, net10.addresses, TRUE);
408 run_tests (monitor, net192.addresses, TRUE);
409 run_tests (monitor, netlocal6.addresses, TRUE);
410 run_tests (monitor, netfe80.addresses, TRUE);
411 run_tests (monitor, unmatched, FALSE);
413 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
414 net10.mask);
415 assert_signals (monitor, FALSE, TRUE, FALSE);
416 run_tests (monitor, net127.addresses, FALSE);
417 run_tests (monitor, net10.addresses, FALSE);
418 run_tests (monitor, net192.addresses, TRUE);
419 run_tests (monitor, netlocal6.addresses, TRUE);
420 run_tests (monitor, netfe80.addresses, TRUE);
421 run_tests (monitor, unmatched, FALSE);
423 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
424 net192.mask);
425 assert_signals (monitor, FALSE, TRUE, FALSE);
426 run_tests (monitor, net127.addresses, FALSE);
427 run_tests (monitor, net10.addresses, FALSE);
428 run_tests (monitor, net192.addresses, FALSE);
429 run_tests (monitor, netlocal6.addresses, TRUE);
430 run_tests (monitor, netfe80.addresses, TRUE);
431 run_tests (monitor, unmatched, FALSE);
433 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
434 netlocal6.mask);
435 assert_signals (monitor, FALSE, TRUE, FALSE);
436 run_tests (monitor, net127.addresses, FALSE);
437 run_tests (monitor, net10.addresses, FALSE);
438 run_tests (monitor, net192.addresses, FALSE);
439 run_tests (monitor, netlocal6.addresses, FALSE);
440 run_tests (monitor, netfe80.addresses, TRUE);
441 run_tests (monitor, unmatched, FALSE);
443 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
444 netfe80.mask);
445 assert_signals (monitor, FALSE, TRUE, FALSE);
446 run_tests (monitor, net127.addresses, FALSE);
447 run_tests (monitor, net10.addresses, FALSE);
448 run_tests (monitor, net192.addresses, FALSE);
449 run_tests (monitor, netlocal6.addresses, FALSE);
450 run_tests (monitor, netfe80.addresses, FALSE);
451 run_tests (monitor, unmatched, FALSE);
453 g_object_unref (monitor);
457 static void
458 init_test (TestMask *test)
460 GError *error = NULL;
461 int i;
463 test->mask = g_inet_address_mask_new_from_string (test->mask_string, &error);
464 g_assert_no_error (error);
466 for (i = 0; test->addresses[i].string; i++)
468 test->addresses[i].address = g_inet_address_new_from_string (test->addresses[i].string);
469 if (strchr (test->addresses[i].string, ':'))
470 g_assert_cmpint (g_inet_address_get_family (test->addresses[i].address), ==, G_SOCKET_FAMILY_IPV6);
471 else
472 g_assert_cmpint (g_inet_address_get_family (test->addresses[i].address), ==, G_SOCKET_FAMILY_IPV4);
476 static void
477 cleanup_test (TestMask *test)
479 int i;
481 g_object_unref (test->mask);
482 for (i = 0; test->addresses[i].string; i++)
483 g_object_unref (test->addresses[i].address);
486 static void
487 watch_network_changed (GNetworkMonitor *monitor,
488 gboolean available,
489 gpointer user_data)
491 g_print ("Network is %s\n", available ? "up" : "down");
494 static void
495 watch_connectivity_changed (GNetworkMonitor *monitor,
496 GParamSpec *pspec,
497 gpointer user_data)
499 g_print ("Connectivity is %d\n", g_network_monitor_get_connectivity (monitor));
502 static void
503 watch_metered_changed (GNetworkMonitor *monitor,
504 GParamSpec *pspec,
505 gpointer user_data)
507 g_print ("Metered is %d\n", g_network_monitor_get_network_metered (monitor));
510 static void
511 do_watch_network (void)
513 GNetworkMonitor *monitor = g_network_monitor_get_default ();
514 GMainLoop *loop;
516 g_print ("Monitoring via %s\n", g_type_name_from_instance ((GTypeInstance *) monitor));
518 g_signal_connect (monitor, "network-changed",
519 G_CALLBACK (watch_network_changed), NULL);
520 g_signal_connect (monitor, "notify::connectivity",
521 G_CALLBACK (watch_connectivity_changed), NULL);
522 g_signal_connect (monitor, "notify::network-metered",
523 G_CALLBACK (watch_metered_changed), NULL);
524 watch_network_changed (monitor, g_network_monitor_get_network_available (monitor), NULL);
525 watch_connectivity_changed (monitor, NULL, NULL);
526 watch_metered_changed (monitor, NULL, NULL);
528 loop = g_main_loop_new (NULL, FALSE);
529 g_main_loop_run (loop);
533 main (int argc, char **argv)
535 int ret;
537 if (argc == 2 && !strcmp (argv[1], "--watch"))
539 do_watch_network ();
540 return 0;
543 g_test_init (&argc, &argv, NULL);
545 init_test (&net127);
546 init_test (&net10);
547 init_test (&net192);
548 init_test (&netlocal6);
549 init_test (&netfe80);
550 ip4_default = g_inet_address_mask_new_from_string ("0.0.0.0/0", NULL);
551 ip6_default = g_inet_address_mask_new_from_string ("::/0", NULL);
553 g_test_add_func ("/network-monitor/default", test_default);
554 g_test_add_func ("/network-monitor/remove_default", test_remove_default);
555 g_test_add_func ("/network-monitor/add_networks", test_add_networks);
556 g_test_add_func ("/network-monitor/remove_networks", test_remove_networks);
558 ret = g_test_run ();
560 cleanup_test (&net127);
561 cleanup_test (&net10);
562 cleanup_test (&net192);
563 cleanup_test (&netlocal6);
564 cleanup_test (&netfe80);
565 g_object_unref (ip4_default);
566 g_object_unref (ip6_default);
568 return ret;