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/>.
22 #define GIO_COMPILATION
23 #include "gnetworkmonitorbase.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.
35 GInetAddress *address;
39 const char *mask_string;
40 GInetAddressMask *mask;
41 TestAddress *addresses;
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 },
54 TestMask net127 = { "127.0.0.0/8", NULL, net127addrs };
56 TestAddress net10addrs[] = {
59 { "10.0.0.255", 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 },
72 TestMask net192 = { "192.168.0.0/20", NULL, net192addrs };
74 TestAddress netlocal6addrs[] = {
78 TestMask netlocal6 = { "::1/128", NULL, netlocal6addrs };
80 TestAddress netfe80addrs[] = {
83 { "fe80::21b:77ff:fea2:972a", NULL },
86 TestMask netfe80 = { "fe80::/64", NULL, netfe80addrs };
88 TestAddress unmatched[] = {
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 },
98 { "fe80::1:0:0:0:0", NULL },
99 { "fe80:8000::0:0:0:0", NULL },
103 GInetAddressMask *ip4_default, *ip6_default;
106 notify_handler (GObject *object,
110 gboolean *emitted = user_data;
116 network_changed_handler (GNetworkMonitor *monitor,
120 gboolean *emitted = user_data;
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;
134 h1 = g_signal_connect (monitor, "notify::network-available",
135 G_CALLBACK (notify_handler),
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);
153 GNetworkMonitor *monitor;
155 GSocketAddress *sockaddr;
156 gboolean should_be_reachable;
160 reach_cb (GObject *source,
164 GError *error = NULL;
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);
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);
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),
192 return G_SOURCE_REMOVE;
196 run_tests (GNetworkMonitor *monitor,
197 TestAddress *addresses,
198 gboolean should_be_reachable)
200 GError *error = NULL;
203 GSocketAddress *sockaddr;
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),
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);
227 g_assert (error != NULL);
228 g_clear_error (&error);
232 g_main_loop_unref (data.loop);
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);
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),
272 assert_signals (monitor, FALSE, TRUE, TRUE);
273 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
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);
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),
300 assert_signals (monitor, FALSE, TRUE, TRUE);
301 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
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),
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),
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),
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),
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),
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);
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),
373 assert_signals (monitor, FALSE, TRUE, TRUE);
374 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
376 assert_signals (monitor, TRUE, TRUE, FALSE);
379 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
381 assert_signals (monitor, FALSE, TRUE, FALSE);
382 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
384 assert_signals (monitor, FALSE, TRUE, FALSE);
385 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
387 assert_signals (monitor, FALSE, TRUE, FALSE);
388 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
390 assert_signals (monitor, FALSE, TRUE, FALSE);
391 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
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),
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),
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),
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),
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),
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);
458 init_test (TestMask *test)
460 GError *error = NULL;
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);
472 g_assert_cmpint (g_inet_address_get_family (test->addresses[i].address), ==, G_SOCKET_FAMILY_IPV4);
477 cleanup_test (TestMask *test)
481 g_object_unref (test->mask);
482 for (i = 0; test->addresses[i].string; i++)
483 g_object_unref (test->addresses[i].address);
487 watch_network_changed (GNetworkMonitor *monitor,
491 g_print ("Network is %s\n", available ? "up" : "down");
495 do_watch_network (void)
497 GNetworkMonitor *monitor = g_network_monitor_get_default ();
500 g_print ("Monitoring via %s\n", g_type_name_from_instance ((GTypeInstance *) monitor));
502 g_signal_connect (monitor, "network-changed",
503 G_CALLBACK (watch_network_changed), NULL);
504 watch_network_changed (monitor, g_network_monitor_get_network_available (monitor), NULL);
506 loop = g_main_loop_new (NULL, FALSE);
507 g_main_loop_run (loop);
511 main (int argc, char **argv)
515 if (argc == 2 && !strcmp (argv[1], "--watch"))
521 g_test_init (&argc, &argv, NULL);
526 init_test (&netlocal6);
527 init_test (&netfe80);
528 ip4_default = g_inet_address_mask_new_from_string ("0.0.0.0/0", NULL);
529 ip6_default = g_inet_address_mask_new_from_string ("::/0", NULL);
531 g_test_add_func ("/network-monitor/default", test_default);
532 g_test_add_func ("/network-monitor/remove_default", test_remove_default);
533 g_test_add_func ("/network-monitor/add_networks", test_add_networks);
534 g_test_add_func ("/network-monitor/remove_networks", test_remove_networks);
538 cleanup_test (&net127);
539 cleanup_test (&net10);
540 cleanup_test (&net192);
541 cleanup_test (&netlocal6);
542 cleanup_test (&netfe80);
543 g_object_unref (ip4_default);
544 g_object_unref (ip6_default);