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.1 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_object_new (G_TYPE_NETWORK_MONITOR_BASE
, NULL
);
245 g_initable_init (G_INITABLE (monitor
), NULL
, &error
);
246 g_assert_no_error (error
);
248 /* In the default configuration, all addresses are reachable */
249 run_tests (monitor
, net127
.addresses
, TRUE
);
250 run_tests (monitor
, net10
.addresses
, TRUE
);
251 run_tests (monitor
, net192
.addresses
, TRUE
);
252 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
253 run_tests (monitor
, netfe80
.addresses
, TRUE
);
254 run_tests (monitor
, unmatched
, TRUE
);
256 assert_signals (monitor
, FALSE
, FALSE
, TRUE
);
258 g_object_unref (monitor
);
262 test_remove_default (void)
264 GNetworkMonitor
*monitor
;
265 GError
*error
= NULL
;
267 monitor
= g_initable_new (G_TYPE_NETWORK_MONITOR_BASE
, NULL
, &error
, NULL
);
268 g_assert_no_error (error
);
269 assert_signals (monitor
, FALSE
, FALSE
, TRUE
);
271 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
273 assert_signals (monitor
, FALSE
, TRUE
, TRUE
);
274 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
276 assert_signals (monitor
, TRUE
, TRUE
, FALSE
);
278 /* Now nothing should be reachable */
279 run_tests (monitor
, net127
.addresses
, FALSE
);
280 run_tests (monitor
, net10
.addresses
, FALSE
);
281 run_tests (monitor
, net192
.addresses
, FALSE
);
282 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
283 run_tests (monitor
, netfe80
.addresses
, FALSE
);
284 run_tests (monitor
, unmatched
, FALSE
);
286 g_object_unref (monitor
);
290 test_add_networks (void)
292 GNetworkMonitor
*monitor
;
293 GError
*error
= NULL
;
295 monitor
= g_initable_new (G_TYPE_NETWORK_MONITOR_BASE
, NULL
, &error
, NULL
);
296 g_assert_no_error (error
);
297 assert_signals (monitor
, FALSE
, FALSE
, TRUE
);
299 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
301 assert_signals (monitor
, FALSE
, TRUE
, TRUE
);
302 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
304 assert_signals (monitor
, TRUE
, TRUE
, FALSE
);
306 /* Now add the masks one by one */
308 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
310 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
312 run_tests (monitor
, net127
.addresses
, TRUE
);
313 run_tests (monitor
, net10
.addresses
, FALSE
);
314 run_tests (monitor
, net192
.addresses
, FALSE
);
315 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
316 run_tests (monitor
, netfe80
.addresses
, FALSE
);
317 run_tests (monitor
, unmatched
, FALSE
);
319 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
321 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
322 run_tests (monitor
, net127
.addresses
, TRUE
);
323 run_tests (monitor
, net10
.addresses
, TRUE
);
324 run_tests (monitor
, net192
.addresses
, FALSE
);
325 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
326 run_tests (monitor
, netfe80
.addresses
, FALSE
);
327 run_tests (monitor
, unmatched
, FALSE
);
329 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
331 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
332 run_tests (monitor
, net127
.addresses
, TRUE
);
333 run_tests (monitor
, net10
.addresses
, TRUE
);
334 run_tests (monitor
, net192
.addresses
, TRUE
);
335 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
336 run_tests (monitor
, netfe80
.addresses
, FALSE
);
337 run_tests (monitor
, unmatched
, FALSE
);
339 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
341 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
342 run_tests (monitor
, net127
.addresses
, TRUE
);
343 run_tests (monitor
, net10
.addresses
, TRUE
);
344 run_tests (monitor
, net192
.addresses
, TRUE
);
345 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
346 run_tests (monitor
, netfe80
.addresses
, FALSE
);
347 run_tests (monitor
, unmatched
, FALSE
);
349 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
351 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
352 run_tests (monitor
, net127
.addresses
, TRUE
);
353 run_tests (monitor
, net10
.addresses
, TRUE
);
354 run_tests (monitor
, net192
.addresses
, TRUE
);
355 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
356 run_tests (monitor
, netfe80
.addresses
, TRUE
);
357 run_tests (monitor
, unmatched
, FALSE
);
359 g_object_unref (monitor
);
363 test_remove_networks (void)
365 GNetworkMonitor
*monitor
;
366 GError
*error
= NULL
;
368 monitor
= g_initable_new (G_TYPE_NETWORK_MONITOR_BASE
, NULL
, &error
, NULL
);
369 g_assert_no_error (error
);
370 assert_signals (monitor
, FALSE
, FALSE
, TRUE
);
372 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
374 assert_signals (monitor
, FALSE
, TRUE
, TRUE
);
375 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
377 assert_signals (monitor
, TRUE
, TRUE
, FALSE
);
380 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
382 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
383 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
385 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
386 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
388 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
389 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
391 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
392 g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor
),
394 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
396 run_tests (monitor
, net127
.addresses
, TRUE
);
397 run_tests (monitor
, net10
.addresses
, TRUE
);
398 run_tests (monitor
, net192
.addresses
, TRUE
);
399 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
400 run_tests (monitor
, netfe80
.addresses
, TRUE
);
401 run_tests (monitor
, unmatched
, FALSE
);
403 /* Now remove them one by one */
404 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
406 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
407 run_tests (monitor
, net127
.addresses
, FALSE
);
408 run_tests (monitor
, net10
.addresses
, TRUE
);
409 run_tests (monitor
, net192
.addresses
, TRUE
);
410 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
411 run_tests (monitor
, netfe80
.addresses
, TRUE
);
412 run_tests (monitor
, unmatched
, FALSE
);
414 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
416 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
417 run_tests (monitor
, net127
.addresses
, FALSE
);
418 run_tests (monitor
, net10
.addresses
, FALSE
);
419 run_tests (monitor
, net192
.addresses
, TRUE
);
420 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
421 run_tests (monitor
, netfe80
.addresses
, TRUE
);
422 run_tests (monitor
, unmatched
, FALSE
);
424 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
426 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
427 run_tests (monitor
, net127
.addresses
, FALSE
);
428 run_tests (monitor
, net10
.addresses
, FALSE
);
429 run_tests (monitor
, net192
.addresses
, FALSE
);
430 run_tests (monitor
, netlocal6
.addresses
, TRUE
);
431 run_tests (monitor
, netfe80
.addresses
, TRUE
);
432 run_tests (monitor
, unmatched
, FALSE
);
434 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
436 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
437 run_tests (monitor
, net127
.addresses
, FALSE
);
438 run_tests (monitor
, net10
.addresses
, FALSE
);
439 run_tests (monitor
, net192
.addresses
, FALSE
);
440 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
441 run_tests (monitor
, netfe80
.addresses
, TRUE
);
442 run_tests (monitor
, unmatched
, FALSE
);
444 g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor
),
446 assert_signals (monitor
, FALSE
, TRUE
, FALSE
);
447 run_tests (monitor
, net127
.addresses
, FALSE
);
448 run_tests (monitor
, net10
.addresses
, FALSE
);
449 run_tests (monitor
, net192
.addresses
, FALSE
);
450 run_tests (monitor
, netlocal6
.addresses
, FALSE
);
451 run_tests (monitor
, netfe80
.addresses
, FALSE
);
452 run_tests (monitor
, unmatched
, FALSE
);
454 g_object_unref (monitor
);
459 init_test (TestMask
*test
)
461 GError
*error
= NULL
;
464 test
->mask
= g_inet_address_mask_new_from_string (test
->mask_string
, &error
);
465 g_assert_no_error (error
);
467 for (i
= 0; test
->addresses
[i
].string
; i
++)
469 test
->addresses
[i
].address
= g_inet_address_new_from_string (test
->addresses
[i
].string
);
470 if (strchr (test
->addresses
[i
].string
, ':'))
471 g_assert_cmpint (g_inet_address_get_family (test
->addresses
[i
].address
), ==, G_SOCKET_FAMILY_IPV6
);
473 g_assert_cmpint (g_inet_address_get_family (test
->addresses
[i
].address
), ==, G_SOCKET_FAMILY_IPV4
);
478 cleanup_test (TestMask
*test
)
482 g_object_unref (test
->mask
);
483 for (i
= 0; test
->addresses
[i
].string
; i
++)
484 g_object_unref (test
->addresses
[i
].address
);
488 watch_network_changed (GNetworkMonitor
*monitor
,
492 g_print ("Network is %s\n", available
? "up" : "down");
496 watch_connectivity_changed (GNetworkMonitor
*monitor
,
500 g_print ("Connectivity is %d\n", g_network_monitor_get_connectivity (monitor
));
504 watch_metered_changed (GNetworkMonitor
*monitor
,
508 g_print ("Metered is %d\n", g_network_monitor_get_network_metered (monitor
));
512 do_watch_network (void)
514 GNetworkMonitor
*monitor
= g_network_monitor_get_default ();
517 g_print ("Monitoring via %s\n", g_type_name_from_instance ((GTypeInstance
*) monitor
));
519 g_signal_connect (monitor
, "network-changed",
520 G_CALLBACK (watch_network_changed
), NULL
);
521 g_signal_connect (monitor
, "notify::connectivity",
522 G_CALLBACK (watch_connectivity_changed
), NULL
);
523 g_signal_connect (monitor
, "notify::network-metered",
524 G_CALLBACK (watch_metered_changed
), NULL
);
525 watch_network_changed (monitor
, g_network_monitor_get_network_available (monitor
), NULL
);
526 watch_connectivity_changed (monitor
, NULL
, NULL
);
527 watch_metered_changed (monitor
, NULL
, NULL
);
529 loop
= g_main_loop_new (NULL
, FALSE
);
530 g_main_loop_run (loop
);
534 main (int argc
, char **argv
)
538 if (argc
== 2 && !strcmp (argv
[1], "--watch"))
544 g_test_init (&argc
, &argv
, NULL
);
546 /* GNetworkMonitor will resolve addresses through a proxy if one is set and a
547 * GIO module is available to handle it. In these tests we deliberately
548 * change the idea of a reachable network to exclude the proxy, which will
549 * lead to negative results. We're not trying to test the proxy-resolving
550 * functionality (that would be for e.g. glib-networking's testsuite), so
551 * let's just use the dummy proxy resolver, which always pretends the
552 * passed-in URL is directly resolvable.
554 g_setenv ("GIO_USE_PROXY_RESOLVER", "dummy", TRUE
);
559 init_test (&netlocal6
);
560 init_test (&netfe80
);
561 ip4_default
= g_inet_address_mask_new_from_string ("0.0.0.0/0", NULL
);
562 ip6_default
= g_inet_address_mask_new_from_string ("::/0", NULL
);
564 g_test_add_func ("/network-monitor/default", test_default
);
565 g_test_add_func ("/network-monitor/remove_default", test_remove_default
);
566 g_test_add_func ("/network-monitor/add_networks", test_add_networks
);
567 g_test_add_func ("/network-monitor/remove_networks", test_remove_networks
);
571 cleanup_test (&net127
);
572 cleanup_test (&net10
);
573 cleanup_test (&net192
);
574 cleanup_test (&netlocal6
);
575 cleanup_test (&netfe80
);
576 g_object_unref (ip4_default
);
577 g_object_unref (ip6_default
);