LCOV - code coverage report
Current view: top level - glib/gio/tests - network-monitor.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 245 268 91.4 %
Date: 2024-04-23 05:16:05 Functions: 13 17 76.5 %
Branches: 13 18 72.2 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  *
       3                 :            :  * Copyright 2011 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include "gio.h"
      22                 :            : 
      23                 :            : /* hack */
      24                 :            : #define GIO_COMPILATION
      25                 :            : #include "gnetworkmonitorbase.h"
      26                 :            : 
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : /* Test data; the GInetAddresses and GInetAddressMasks get filled in
      30                 :            :  * in main(). Each address in a TestAddress matches the mask in its
      31                 :            :  * corresponding TestMask, and none of them match any of the other
      32                 :            :  * masks. The addresses in unmatched don't match any of the masks.
      33                 :            :  */
      34                 :            : 
      35                 :            : typedef struct {
      36                 :            :   const char *string;
      37                 :            :   GInetAddress *address;
      38                 :            : } TestAddress;
      39                 :            : 
      40                 :            : typedef struct {
      41                 :            :   const char *mask_string;
      42                 :            :   GInetAddressMask *mask;
      43                 :            :   TestAddress *addresses;
      44                 :            : } TestMask;
      45                 :            : 
      46                 :            : TestAddress net127addrs[] = {
      47                 :            :   { "127.0.0.1", NULL },
      48                 :            :   { "127.0.0.2", NULL },
      49                 :            :   { "127.0.0.255", NULL },
      50                 :            :   { "127.0.1.0", NULL },
      51                 :            :   { "127.0.255.0", NULL },
      52                 :            :   { "127.0.255.0", NULL },
      53                 :            :   { "127.255.255.255", NULL },
      54                 :            :   { NULL, NULL }
      55                 :            : };
      56                 :            : TestMask net127 = { "127.0.0.0/8", NULL, net127addrs };
      57                 :            : 
      58                 :            : TestAddress net10addrs[] = {
      59                 :            :   { "10.0.0.1", NULL },
      60                 :            :   { "10.0.0.2", NULL },
      61                 :            :   { "10.0.0.255", NULL },
      62                 :            :   { NULL, NULL }
      63                 :            : };
      64                 :            : TestMask net10 = { "10.0.0.0/24", NULL, net10addrs };
      65                 :            : 
      66                 :            : TestAddress net192addrs[] = {
      67                 :            :   { "192.168.0.1", NULL },
      68                 :            :   { "192.168.0.2", NULL },
      69                 :            :   { "192.168.0.255", NULL },
      70                 :            :   { "192.168.1.0", NULL },
      71                 :            :   { "192.168.15.0", NULL },
      72                 :            :   { NULL, NULL }
      73                 :            : };
      74                 :            : TestMask net192 = { "192.168.0.0/20", NULL, net192addrs };
      75                 :            : 
      76                 :            : TestAddress netlocal6addrs[] = {
      77                 :            :   { "::1", NULL },
      78                 :            :   { NULL, NULL }
      79                 :            : };
      80                 :            : TestMask netlocal6 = { "::1/128", NULL, netlocal6addrs };
      81                 :            : 
      82                 :            : TestAddress netfe80addrs[] = {
      83                 :            :   { "fe80::", NULL },
      84                 :            :   { "fe80::1", NULL },
      85                 :            :   { "fe80::21b:77ff:fea2:972a", NULL },
      86                 :            :   { NULL, NULL }
      87                 :            : };
      88                 :            : TestMask netfe80 = { "fe80::/64", NULL, netfe80addrs };
      89                 :            : 
      90                 :            : TestAddress unmatched[] = {
      91                 :            :   { "10.0.1.0", NULL },
      92                 :            :   { "10.0.255.0", NULL },
      93                 :            :   { "10.255.255.255", NULL },
      94                 :            :   { "192.168.16.0", NULL },
      95                 :            :   { "192.168.255.0", NULL },
      96                 :            :   { "192.169.0.0", NULL },
      97                 :            :   { "192.255.255.255", NULL },
      98                 :            :   { "::2", NULL },
      99                 :            :   { "1::1", NULL },
     100                 :            :   { "fe80::1:0:0:0:0", NULL },
     101                 :            :   { "fe80:8000::0:0:0:0", NULL },
     102                 :            :   { NULL, NULL }
     103                 :            : };
     104                 :            : 
     105                 :            : GInetAddressMask *ip4_default, *ip6_default;
     106                 :            : 
     107                 :            : static void
     108                 :          3 : notify_handler (GObject    *object,
     109                 :            :                 GParamSpec *pspec,
     110                 :            :                 gpointer    user_data)
     111                 :            : {
     112                 :          3 :   gboolean *emitted = user_data;
     113                 :            : 
     114                 :          3 :   *emitted = TRUE;
     115                 :          3 : }
     116                 :            : 
     117                 :            : static void
     118                 :         21 : network_changed_handler (GNetworkMonitor *monitor,
     119                 :            :                          gboolean         available,
     120                 :            :                          gpointer         user_data)
     121                 :            : {
     122                 :         21 :   gboolean *emitted = user_data;
     123                 :            : 
     124                 :         21 :   *emitted = TRUE;
     125                 :         21 : }
     126                 :            : 
     127                 :            : static void
     128                 :         25 : assert_signals (GNetworkMonitor *monitor,
     129                 :            :                 gboolean         should_emit_notify,
     130                 :            :                 gboolean         should_emit_network_changed,
     131                 :            :                 gboolean         expected_network_available)
     132                 :            : {
     133                 :         25 :   gboolean emitted_notify = FALSE, emitted_network_changed = FALSE;
     134                 :            :   guint h1, h2;
     135                 :            : 
     136                 :         25 :   h1 = g_signal_connect (monitor, "notify::network-available",
     137                 :            :                          G_CALLBACK (notify_handler),
     138                 :            :                          &emitted_notify);
     139                 :         25 :   h2 = g_signal_connect (monitor, "network-changed",
     140                 :            :                          G_CALLBACK (network_changed_handler),
     141                 :            :                          &emitted_network_changed);
     142                 :            : 
     143                 :         25 :   g_main_context_iteration (NULL, FALSE);
     144                 :            : 
     145                 :         25 :   g_signal_handler_disconnect (monitor, h1);
     146                 :         25 :   g_signal_handler_disconnect (monitor, h2);
     147                 :            : 
     148                 :         25 :   g_assert (emitted_notify == should_emit_notify);
     149                 :         25 :   g_assert (emitted_network_changed == should_emit_network_changed);
     150                 :            : 
     151                 :         25 :   g_assert (g_network_monitor_get_network_available (monitor) == expected_network_available);
     152                 :         25 : }
     153                 :            : 
     154                 :            : typedef struct {
     155                 :            :   GNetworkMonitor *monitor;
     156                 :            :   GMainLoop       *loop;
     157                 :            :   GSocketAddress  *sockaddr;
     158                 :            :   gboolean         should_be_reachable;
     159                 :            : } CanReachData;
     160                 :            : 
     161                 :            : static void
     162                 :        247 : reach_cb (GObject      *source,
     163                 :            :           GAsyncResult *res,
     164                 :            :           gpointer      user_data)
     165                 :            : {
     166                 :        247 :   GError *error = NULL;
     167                 :            :   gboolean reachable;
     168                 :        247 :   CanReachData *data = user_data;
     169                 :            : 
     170                 :        247 :   reachable = g_network_monitor_can_reach_finish (data->monitor, res, &error);
     171                 :            : 
     172         [ +  + ]:        247 :   if (data->should_be_reachable)
     173                 :        133 :     g_assert_no_error (error);
     174                 :            :   else
     175                 :            :     {
     176                 :        114 :       g_assert (error != NULL);
     177                 :        114 :       g_clear_error (&error);
     178                 :            :     }
     179                 :        247 :   g_assert (reachable == data->should_be_reachable);
     180                 :            : 
     181                 :        247 :   g_main_loop_quit (data->loop);
     182                 :        247 : }
     183                 :            : 
     184                 :            : static gboolean
     185                 :        247 : test_reach_async (gpointer user_data)
     186                 :            : {
     187                 :        247 :   CanReachData *data = user_data;
     188                 :            : 
     189                 :        247 :   g_network_monitor_can_reach_async (data->monitor,
     190                 :        247 :                                      G_SOCKET_CONNECTABLE (data->sockaddr),
     191                 :            :                                      NULL,
     192                 :            :                                      reach_cb,
     193                 :            :                                      data);
     194                 :        247 :   return G_SOURCE_REMOVE;
     195                 :            : }
     196                 :            : 
     197                 :            : static void
     198                 :         78 : run_tests (GNetworkMonitor *monitor,
     199                 :            :            TestAddress     *addresses,
     200                 :            :            gboolean         should_be_reachable)
     201                 :            : {
     202                 :         78 :   GError *error = NULL;
     203                 :            :   int i;
     204                 :            :   gboolean reachable;
     205                 :            :   GSocketAddress *sockaddr;
     206                 :            :   CanReachData data;
     207                 :            : 
     208                 :         78 :   data.monitor = monitor;
     209                 :         78 :   data.loop = g_main_loop_new (NULL, FALSE);
     210                 :            : 
     211         [ +  + ]:        325 :   for (i = 0; addresses[i].address; i++)
     212                 :            :     {
     213                 :        247 :       sockaddr = g_inet_socket_address_new (addresses[i].address, 0);
     214                 :        247 :       reachable = g_network_monitor_can_reach (monitor,
     215                 :        247 :                                                G_SOCKET_CONNECTABLE (sockaddr),
     216                 :            :                                                NULL, &error);
     217                 :        247 :       data.sockaddr = sockaddr;
     218                 :        247 :       data.should_be_reachable = should_be_reachable;
     219                 :            : 
     220                 :        247 :       g_idle_add (test_reach_async, &data);
     221                 :        247 :       g_main_loop_run (data.loop);
     222                 :            : 
     223                 :        247 :       g_object_unref (sockaddr);
     224                 :        247 :       g_assert_cmpint (reachable, ==, should_be_reachable);
     225         [ +  + ]:        247 :       if (should_be_reachable)
     226                 :        133 :         g_assert_no_error (error);
     227                 :            :       else
     228                 :            :         {
     229                 :        114 :           g_assert (error != NULL);
     230                 :        114 :           g_clear_error (&error);
     231                 :            :         }
     232                 :            :     }
     233                 :            : 
     234                 :         78 :   g_main_loop_unref (data.loop);
     235                 :         78 : }
     236                 :            : 
     237                 :            : static void
     238                 :          1 : test_default (void)
     239                 :            : {
     240                 :            :   GNetworkMonitor *monitor, *m;
     241                 :          1 :   GError *error = NULL;
     242                 :            : 
     243                 :          1 :   m = g_network_monitor_get_default ();
     244                 :          1 :   g_assert (G_IS_NETWORK_MONITOR (m));
     245                 :            : 
     246                 :          1 :   monitor = g_object_new (G_TYPE_NETWORK_MONITOR_BASE, NULL);
     247                 :          1 :   g_initable_init (G_INITABLE (monitor), NULL, &error);
     248                 :          1 :   g_assert_no_error (error);
     249                 :            : 
     250                 :            :   /* In the default configuration, all addresses are reachable */
     251                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     252                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     253                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     254                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     255                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     256                 :          1 :   run_tests (monitor, unmatched, TRUE);
     257                 :            : 
     258                 :          1 :   assert_signals (monitor, FALSE, FALSE, TRUE);
     259                 :            : 
     260                 :          1 :   g_object_unref (monitor);
     261                 :          1 : }
     262                 :            : 
     263                 :            : static void
     264                 :          1 : test_remove_default (void)
     265                 :            : {
     266                 :            :   GNetworkMonitor *monitor;
     267                 :          1 :   GError *error = NULL;
     268                 :            : 
     269                 :          1 :   monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
     270                 :          1 :   g_assert_no_error (error);
     271                 :          1 :   assert_signals (monitor, FALSE, FALSE, TRUE);
     272                 :            : 
     273                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     274                 :            :                                          ip4_default);
     275                 :          1 :   assert_signals (monitor, FALSE, TRUE, TRUE);
     276                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     277                 :            :                                          ip6_default);
     278                 :          1 :   assert_signals (monitor, TRUE, TRUE, FALSE);
     279                 :            : 
     280                 :            :   /* Now nothing should be reachable */
     281                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     282                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     283                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     284                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     285                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     286                 :          1 :   run_tests (monitor, unmatched, FALSE);
     287                 :            : 
     288                 :          1 :   g_object_unref (monitor);
     289                 :          1 : }
     290                 :            : 
     291                 :            : static void
     292                 :          1 : test_add_networks (void)
     293                 :            : {
     294                 :            :   GNetworkMonitor *monitor;
     295                 :          1 :   GError *error = NULL;
     296                 :            : 
     297                 :          1 :   monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
     298                 :          1 :   g_assert_no_error (error);
     299                 :          1 :   assert_signals (monitor, FALSE, FALSE, TRUE);
     300                 :            : 
     301                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     302                 :            :                                          ip4_default);
     303                 :          1 :   assert_signals (monitor, FALSE, TRUE, TRUE);
     304                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     305                 :            :                                          ip6_default);
     306                 :          1 :   assert_signals (monitor, TRUE, TRUE, FALSE);
     307                 :            : 
     308                 :            :   /* Now add the masks one by one */
     309                 :            : 
     310                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     311                 :            :                                       net127.mask);
     312                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     313                 :            : 
     314                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     315                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     316                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     317                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     318                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     319                 :          1 :   run_tests (monitor, unmatched, FALSE);
     320                 :            : 
     321                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     322                 :            :                                       net10.mask);
     323                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     324                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     325                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     326                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     327                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     328                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     329                 :          1 :   run_tests (monitor, unmatched, FALSE);
     330                 :            : 
     331                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     332                 :            :                                       net192.mask);
     333                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     334                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     335                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     336                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     337                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     338                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     339                 :          1 :   run_tests (monitor, unmatched, FALSE);
     340                 :            : 
     341                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     342                 :            :                                       netlocal6.mask);
     343                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     344                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     345                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     346                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     347                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     348                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     349                 :          1 :   run_tests (monitor, unmatched, FALSE);
     350                 :            : 
     351                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     352                 :            :                                       netfe80.mask);
     353                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     354                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     355                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     356                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     357                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     358                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     359                 :          1 :   run_tests (monitor, unmatched, FALSE);
     360                 :            : 
     361                 :          1 :   g_object_unref (monitor);
     362                 :          1 : }
     363                 :            : 
     364                 :            : static void
     365                 :          1 : test_remove_networks (void)
     366                 :            : {
     367                 :            :   GNetworkMonitor *monitor;
     368                 :          1 :   GError *error = NULL;
     369                 :            : 
     370                 :          1 :   monitor = g_initable_new (G_TYPE_NETWORK_MONITOR_BASE, NULL, &error, NULL);
     371                 :          1 :   g_assert_no_error (error);
     372                 :          1 :   assert_signals (monitor, FALSE, FALSE, TRUE);
     373                 :            : 
     374                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     375                 :            :                                          ip4_default);
     376                 :          1 :   assert_signals (monitor, FALSE, TRUE, TRUE);
     377                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     378                 :            :                                          ip6_default);
     379                 :          1 :   assert_signals (monitor, TRUE, TRUE, FALSE);
     380                 :            : 
     381                 :            :   /* First add them */
     382                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     383                 :            :                                       net127.mask);
     384                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     385                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     386                 :            :                                       net10.mask);
     387                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     388                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     389                 :            :                                       net192.mask);
     390                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     391                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     392                 :            :                                       netlocal6.mask);
     393                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     394                 :          1 :   g_network_monitor_base_add_network (G_NETWORK_MONITOR_BASE (monitor),
     395                 :            :                                       netfe80.mask);
     396                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     397                 :            : 
     398                 :          1 :   run_tests (monitor, net127.addresses, TRUE);
     399                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     400                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     401                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     402                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     403                 :          1 :   run_tests (monitor, unmatched, FALSE);
     404                 :            : 
     405                 :            :   /* Now remove them one by one */
     406                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     407                 :            :                                          net127.mask);
     408                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     409                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     410                 :          1 :   run_tests (monitor, net10.addresses, TRUE);
     411                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     412                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     413                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     414                 :          1 :   run_tests (monitor, unmatched, FALSE);
     415                 :            : 
     416                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     417                 :            :                                          net10.mask);
     418                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     419                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     420                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     421                 :          1 :   run_tests (monitor, net192.addresses, TRUE);
     422                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     423                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     424                 :          1 :   run_tests (monitor, unmatched, FALSE);
     425                 :            : 
     426                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     427                 :            :                                          net192.mask);
     428                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     429                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     430                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     431                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     432                 :          1 :   run_tests (monitor, netlocal6.addresses, TRUE);
     433                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     434                 :          1 :   run_tests (monitor, unmatched, FALSE);
     435                 :            : 
     436                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     437                 :            :                                          netlocal6.mask);
     438                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     439                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     440                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     441                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     442                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     443                 :          1 :   run_tests (monitor, netfe80.addresses, TRUE);
     444                 :          1 :   run_tests (monitor, unmatched, FALSE);
     445                 :            : 
     446                 :          1 :   g_network_monitor_base_remove_network (G_NETWORK_MONITOR_BASE (monitor),
     447                 :            :                                          netfe80.mask);
     448                 :          1 :   assert_signals (monitor, FALSE, TRUE, FALSE);
     449                 :          1 :   run_tests (monitor, net127.addresses, FALSE);
     450                 :          1 :   run_tests (monitor, net10.addresses, FALSE);
     451                 :          1 :   run_tests (monitor, net192.addresses, FALSE);
     452                 :          1 :   run_tests (monitor, netlocal6.addresses, FALSE);
     453                 :          1 :   run_tests (monitor, netfe80.addresses, FALSE);
     454                 :          1 :   run_tests (monitor, unmatched, FALSE);
     455                 :            : 
     456                 :          1 :   g_object_unref (monitor);
     457                 :          1 : }
     458                 :            : 
     459                 :            : 
     460                 :            : static void
     461                 :          5 : init_test (TestMask *test)
     462                 :            : {
     463                 :          5 :   GError *error = NULL;
     464                 :            :   int i;
     465                 :            : 
     466                 :          5 :   test->mask = g_inet_address_mask_new_from_string (test->mask_string, &error);
     467                 :          5 :   g_assert_no_error (error);
     468                 :            : 
     469         [ +  + ]:         24 :   for (i = 0; test->addresses[i].string; i++)
     470                 :            :     {
     471                 :         19 :       test->addresses[i].address = g_inet_address_new_from_string (test->addresses[i].string);
     472         [ +  + ]:         19 :       if (strchr (test->addresses[i].string, ':'))
     473                 :          4 :         g_assert_cmpint (g_inet_address_get_family (test->addresses[i].address), ==, G_SOCKET_FAMILY_IPV6);
     474                 :            :       else
     475                 :         15 :         g_assert_cmpint (g_inet_address_get_family (test->addresses[i].address), ==, G_SOCKET_FAMILY_IPV4);
     476                 :            :     }
     477                 :          5 : }
     478                 :            : 
     479                 :            : static void
     480                 :          5 : cleanup_test (TestMask *test)
     481                 :            : {
     482                 :            :   int i;
     483                 :            : 
     484                 :          5 :   g_object_unref (test->mask);
     485         [ +  + ]:         24 :   for (i = 0; test->addresses[i].string; i++)
     486                 :         19 :     g_object_unref (test->addresses[i].address);
     487                 :          5 : }
     488                 :            : 
     489                 :            : static void
     490                 :          0 : watch_network_changed (GNetworkMonitor *monitor,
     491                 :            :                        gboolean         available,
     492                 :            :                        gpointer         user_data)
     493                 :            : {
     494         [ #  # ]:          0 :   g_print ("Network is %s\n", available ? "up" : "down");
     495                 :          0 : }
     496                 :            : 
     497                 :            : static void
     498                 :          0 : watch_connectivity_changed (GNetworkMonitor *monitor,
     499                 :            :                             GParamSpec      *pspec,
     500                 :            :                             gpointer         user_data)
     501                 :            : {
     502                 :          0 :   g_print ("Connectivity is %d\n", g_network_monitor_get_connectivity (monitor));
     503                 :          0 : }
     504                 :            : 
     505                 :            : static void
     506                 :          0 : watch_metered_changed (GNetworkMonitor *monitor,
     507                 :            :                        GParamSpec      *pspec,
     508                 :            :                        gpointer         user_data)
     509                 :            : {
     510                 :          0 :   g_print ("Metered is %d\n", g_network_monitor_get_network_metered (monitor));
     511                 :          0 : }
     512                 :            : 
     513                 :            : static void
     514                 :          0 : do_watch_network (void)
     515                 :            : {
     516                 :          0 :   GNetworkMonitor *monitor = g_network_monitor_get_default ();
     517                 :            :   GMainLoop *loop;
     518                 :            : 
     519                 :          0 :   g_print ("Monitoring via %s\n", g_type_name_from_instance ((GTypeInstance *) monitor));
     520                 :            : 
     521                 :          0 :   g_signal_connect (monitor, "network-changed",
     522                 :            :                     G_CALLBACK (watch_network_changed), NULL);
     523                 :          0 :   g_signal_connect (monitor, "notify::connectivity",
     524                 :            :                     G_CALLBACK (watch_connectivity_changed), NULL);
     525                 :          0 :   g_signal_connect (monitor, "notify::network-metered",
     526                 :            :                     G_CALLBACK (watch_metered_changed), NULL);
     527                 :          0 :   watch_network_changed (monitor, g_network_monitor_get_network_available (monitor), NULL);
     528                 :          0 :   watch_connectivity_changed (monitor, NULL, NULL);
     529                 :          0 :   watch_metered_changed (monitor, NULL, NULL);
     530                 :            : 
     531                 :          0 :   loop = g_main_loop_new (NULL, FALSE);
     532                 :          0 :   g_main_loop_run (loop);
     533                 :          0 : }
     534                 :            : 
     535                 :            : int
     536                 :          1 : main (int argc, char **argv)
     537                 :            : {
     538                 :            :   int ret;
     539                 :            : 
     540   [ -  +  -  - ]:          1 :   if (argc == 2 && !strcmp (argv[1], "--watch"))
     541                 :            :     {
     542                 :          0 :       do_watch_network ();
     543                 :          0 :       return 0;
     544                 :            :     }
     545                 :            : 
     546                 :          1 :   g_test_init (&argc, &argv, NULL);
     547                 :            : 
     548                 :            :   /* GNetworkMonitor will resolve addresses through a proxy if one is set and a
     549                 :            :    * GIO module is available to handle it. In these tests we deliberately
     550                 :            :    * change the idea of a reachable network to exclude the proxy, which will
     551                 :            :    * lead to negative results. We're not trying to test the proxy-resolving
     552                 :            :    * functionality (that would be for e.g. glib-networking's testsuite), so
     553                 :            :    * let's just use the dummy proxy resolver, which always pretends the
     554                 :            :    * passed-in URL is directly resolvable.
     555                 :            :    */
     556                 :          1 :   g_setenv ("GIO_USE_PROXY_RESOLVER", "dummy", TRUE);
     557                 :            : 
     558                 :          1 :   init_test (&net127);
     559                 :          1 :   init_test (&net10);
     560                 :          1 :   init_test (&net192);
     561                 :          1 :   init_test (&netlocal6);
     562                 :          1 :   init_test (&netfe80);
     563                 :          1 :   ip4_default = g_inet_address_mask_new_from_string ("0.0.0.0/0", NULL);
     564                 :          1 :   ip6_default = g_inet_address_mask_new_from_string ("::/0", NULL);
     565                 :            : 
     566                 :          1 :   g_test_add_func ("/network-monitor/default", test_default);
     567                 :          1 :   g_test_add_func ("/network-monitor/remove_default", test_remove_default);
     568                 :          1 :   g_test_add_func ("/network-monitor/add_networks", test_add_networks);
     569                 :          1 :   g_test_add_func ("/network-monitor/remove_networks", test_remove_networks);
     570                 :            : 
     571                 :          1 :   ret = g_test_run ();
     572                 :            : 
     573                 :          1 :   cleanup_test (&net127);
     574                 :          1 :   cleanup_test (&net10);
     575                 :          1 :   cleanup_test (&net192);
     576                 :          1 :   cleanup_test (&netlocal6);
     577                 :          1 :   cleanup_test (&netfe80);
     578                 :          1 :   g_object_unref (ip4_default);
     579                 :          1 :   g_object_unref (ip6_default);
     580                 :            : 
     581                 :          1 :   return ret;
     582                 :            : }

Generated by: LCOV version 1.14