LCOV - code coverage report
Current view: top level - glib/gio/tests - inet-address.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 243 243 100.0 %
Date: 2024-04-23 05:16:05 Functions: 12 12 100.0 %
Branches: 2 2 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* Unit tests for GInetAddress
       2                 :            :  * Copyright (C) 2012 Red Hat, Inc
       3                 :            :  * Author: Matthias Clasen
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       7                 :            :  * This work is provided "as is"; redistribution and modification
       8                 :            :  * in whole or in part, in any medium, physical or electronic is
       9                 :            :  * permitted without restriction.
      10                 :            :  *
      11                 :            :  * This work 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.
      14                 :            :  *
      15                 :            :  * In no event shall the authors or contributors be liable for any
      16                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      17                 :            :  * damages (including, but not limited to, procurement of substitute
      18                 :            :  * goods or services; loss of use, data, or profits; or business
      19                 :            :  * interruption) however caused and on any theory of liability, whether
      20                 :            :  * in contract, strict liability, or tort (including negligence or
      21                 :            :  * otherwise) arising in any way out of the use of this software, even
      22                 :            :  * if advised of the possibility of such damage.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include "config.h"
      26                 :            : 
      27                 :            : #include <gio/gio.h>
      28                 :            : #include <gio/gnetworking.h>
      29                 :            : 
      30                 :            : static void
      31                 :          1 : test_parse (void)
      32                 :            : {
      33                 :            :   GInetAddress *addr;
      34                 :            : 
      35                 :          1 :   addr = g_inet_address_new_from_string ("0:0:0:0:0:0:0:0");
      36                 :          1 :   g_assert (addr != NULL);
      37                 :          1 :   g_object_unref (addr);
      38                 :          1 :   addr = g_inet_address_new_from_string ("1:0:0:0:0:0:0:8");
      39                 :          1 :   g_assert (addr != NULL);
      40                 :          1 :   g_object_unref (addr);
      41                 :          1 :   addr = g_inet_address_new_from_string ("0:0:0:0:0:FFFF:204.152.189.116");
      42                 :          1 :   g_assert (addr != NULL);
      43                 :          1 :   g_object_unref (addr);
      44                 :          1 :   addr = g_inet_address_new_from_string ("::1");
      45                 :          1 :   g_assert (addr != NULL);
      46                 :          1 :   g_object_unref (addr);
      47                 :          1 :   addr = g_inet_address_new_from_string ("::");
      48                 :          1 :   g_assert (addr != NULL);
      49                 :          1 :   g_object_unref (addr);
      50                 :          1 :   addr = g_inet_address_new_from_string ("::FFFF:204.152.189.116");
      51                 :          1 :   g_assert (addr != NULL);
      52                 :          1 :   g_object_unref (addr);
      53                 :          1 :   addr = g_inet_address_new_from_string ("204.152.189.116");
      54                 :          1 :   g_assert (addr != NULL);
      55                 :          1 :   g_object_unref (addr);
      56                 :            : 
      57                 :          1 :   addr = g_inet_address_new_from_string ("::1::2");
      58                 :          1 :   g_assert (addr == NULL);
      59                 :          1 :   addr = g_inet_address_new_from_string ("2001:1:2:3:4:5:6:7]");
      60                 :          1 :   g_assert (addr == NULL);
      61                 :          1 :   addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7");
      62                 :          1 :   g_assert (addr == NULL);
      63                 :          1 :   addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]");
      64                 :          1 :   g_assert (addr == NULL);
      65                 :          1 :   addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]:80");
      66                 :          1 :   g_assert (addr == NULL);
      67                 :          1 :   addr = g_inet_address_new_from_string ("0:1:2:3:4:5:6:7:8:9");
      68                 :          1 :   g_assert (addr == NULL);
      69                 :          1 :   addr = g_inet_address_new_from_string ("::FFFFFFF");
      70                 :          1 :   g_assert (addr == NULL);
      71                 :          1 :   addr = g_inet_address_new_from_string ("204.152.189.116:80");
      72                 :          1 :   g_assert (addr == NULL);
      73                 :          1 : }
      74                 :            : 
      75                 :            : static void
      76                 :          1 : test_any (void)
      77                 :            : {
      78                 :            :   GInetAddress *addr;
      79                 :          1 :   GSocketFamily family[2] = { G_SOCKET_FAMILY_IPV4, G_SOCKET_FAMILY_IPV6 };
      80                 :          1 :   gsize size[2] = { 4, 16 };
      81                 :            :   gint i;
      82                 :            : 
      83         [ +  + ]:          3 :   for (i = 0; i < 2; i++)
      84                 :            :     {
      85                 :          2 :       addr = g_inet_address_new_any (family[i]);
      86                 :            : 
      87                 :          2 :       g_assert (g_inet_address_get_is_any (addr));
      88                 :          2 :       g_assert (g_inet_address_get_family (addr) == family[i]);
      89                 :          2 :       g_assert (g_inet_address_get_native_size (addr) == size[i]);
      90                 :          2 :       g_assert (!g_inet_address_get_is_loopback (addr));
      91                 :          2 :       g_assert (!g_inet_address_get_is_link_local (addr));
      92                 :          2 :       g_assert (!g_inet_address_get_is_site_local (addr));
      93                 :          2 :       g_assert (!g_inet_address_get_is_multicast (addr));
      94                 :          2 :       g_assert (!g_inet_address_get_is_mc_global (addr));
      95                 :          2 :       g_assert (!g_inet_address_get_is_mc_link_local (addr));
      96                 :          2 :       g_assert (!g_inet_address_get_is_mc_node_local (addr));
      97                 :          2 :       g_assert (!g_inet_address_get_is_mc_org_local (addr));
      98                 :          2 :       g_assert (!g_inet_address_get_is_mc_site_local (addr));
      99                 :            : 
     100                 :          2 :       g_object_unref (addr);
     101                 :            :    }
     102                 :          1 : }
     103                 :            : 
     104                 :            : static void
     105                 :          1 : test_loopback (void)
     106                 :            : {
     107                 :            :   GInetAddress *addr;
     108                 :            : 
     109                 :          1 :   addr = g_inet_address_new_from_string ("::1");
     110                 :          1 :   g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV6);
     111                 :          1 :   g_assert (g_inet_address_get_is_loopback (addr));
     112                 :          1 :   g_object_unref (addr);
     113                 :            : 
     114                 :          1 :   addr = g_inet_address_new_from_string ("127.0.0.0");
     115                 :          1 :   g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV4);
     116                 :          1 :   g_assert (g_inet_address_get_is_loopback (addr));
     117                 :          1 :   g_object_unref (addr);
     118                 :          1 : }
     119                 :            : 
     120                 :            : static void
     121                 :          1 : test_bytes (void)
     122                 :            : {
     123                 :            :   GInetAddress *addr1, *addr2, *addr3;
     124                 :            :   const guint8 *bytes;
     125                 :            : 
     126                 :          1 :   addr1 = g_inet_address_new_from_string ("192.168.0.100");
     127                 :          1 :   addr2 = g_inet_address_new_from_string ("192.168.0.101");
     128                 :          1 :   bytes = g_inet_address_to_bytes (addr1);
     129                 :          1 :   addr3 = g_inet_address_new_from_bytes (bytes, G_SOCKET_FAMILY_IPV4);
     130                 :            : 
     131                 :          1 :   g_assert (!g_inet_address_equal (addr1, addr2));
     132                 :          1 :   g_assert (g_inet_address_equal (addr1, addr3));
     133                 :            : 
     134                 :          1 :   g_object_unref (addr1);
     135                 :          1 :   g_object_unref (addr2);
     136                 :          1 :   g_object_unref (addr3);
     137                 :          1 : }
     138                 :            : 
     139                 :            : static void
     140                 :          1 : test_property (void)
     141                 :            : {
     142                 :            :   GInetAddress *addr;
     143                 :            :   GSocketFamily family;
     144                 :            :   const guint8 *bytes;
     145                 :            :   gboolean any;
     146                 :            :   gboolean loopback;
     147                 :            :   gboolean link_local;
     148                 :            :   gboolean site_local;
     149                 :            :   gboolean multicast;
     150                 :            :   gboolean mc_global;
     151                 :            :   gboolean mc_link_local;
     152                 :            :   gboolean mc_node_local;
     153                 :            :   gboolean mc_org_local;
     154                 :            :   gboolean mc_site_local;
     155                 :            : 
     156                 :          1 :   addr = g_inet_address_new_from_string ("ff85::");
     157                 :          1 :   g_object_get (addr,
     158                 :            :                 "family", &family,
     159                 :            :                 "bytes", &bytes,
     160                 :            :                 "is-any", &any,
     161                 :            :                 "is-loopback", &loopback,
     162                 :            :                 "is-link-local", &link_local,
     163                 :            :                 "is-site-local", &site_local,
     164                 :            :                 "is-multicast", &multicast,
     165                 :            :                 "is-mc-global", &mc_global,
     166                 :            :                 "is-mc-link-local", &mc_link_local,
     167                 :            :                 "is-mc-node-local", &mc_node_local,
     168                 :            :                 "is-mc-org-local", &mc_org_local,
     169                 :            :                 "is-mc-site-local", &mc_site_local,
     170                 :            :                 NULL);
     171                 :            : 
     172                 :          1 :   g_assert (family == G_SOCKET_FAMILY_IPV6);
     173                 :          1 :   g_assert (!any);
     174                 :          1 :   g_assert (!loopback);
     175                 :          1 :   g_assert (!link_local);
     176                 :          1 :   g_assert (!site_local);
     177                 :          1 :   g_assert (multicast);
     178                 :          1 :   g_assert (!mc_global);
     179                 :          1 :   g_assert (!mc_link_local);
     180                 :          1 :   g_assert (!mc_node_local);
     181                 :          1 :   g_assert (!mc_org_local);
     182                 :          1 :   g_assert (mc_site_local);
     183                 :            : 
     184                 :          1 :   g_object_unref (addr);
     185                 :          1 : }
     186                 :            : 
     187                 :            : static void
     188                 :          1 : test_socket_address (void)
     189                 :            : {
     190                 :            :   GInetAddress *addr;
     191                 :            :   GInetSocketAddress *saddr;
     192                 :            :   guint port;
     193                 :            :   guint32 flowinfo;
     194                 :            :   guint32 scope_id;
     195                 :            :   GSocketFamily family;
     196                 :            : 
     197                 :          1 :   addr = g_inet_address_new_from_string ("::ffff:125.1.15.5");
     198                 :          1 :   saddr = G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, 308));
     199                 :            : 
     200                 :          1 :   g_assert (g_inet_address_equal (addr, g_inet_socket_address_get_address (saddr)));
     201                 :          1 :   g_object_unref (addr);
     202                 :            : 
     203                 :          1 :   g_assert (g_inet_socket_address_get_port (saddr) == 308);
     204                 :          1 :   g_assert (g_inet_socket_address_get_flowinfo (saddr) == 0);
     205                 :          1 :   g_assert (g_inet_socket_address_get_scope_id (saddr) == 0);
     206                 :            : 
     207                 :          1 :   g_object_unref (saddr);
     208                 :            : 
     209                 :          1 :   addr = g_inet_address_new_from_string ("::1");
     210                 :          1 :   saddr = G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
     211                 :            :                                                "address", addr,
     212                 :            :                                                "port", 308,
     213                 :            :                                                "flowinfo", 10,
     214                 :            :                                                "scope-id", 25,
     215                 :            :                                                NULL));
     216                 :          1 :   g_object_unref (addr);
     217                 :            : 
     218                 :          1 :   g_assert (g_inet_socket_address_get_port (saddr) == 308);
     219                 :          1 :   g_assert (g_inet_socket_address_get_flowinfo (saddr) == 10);
     220                 :          1 :   g_assert (g_inet_socket_address_get_scope_id (saddr) == 25);
     221                 :            : 
     222                 :          1 :   g_object_get (saddr,
     223                 :            :                 "family", &family,
     224                 :            :                 "address", &addr,
     225                 :            :                 "port", &port,
     226                 :            :                 "flowinfo", &flowinfo,
     227                 :            :                 "scope-id", &scope_id,
     228                 :            :                 NULL);
     229                 :            : 
     230                 :          1 :   g_assert (family == G_SOCKET_FAMILY_IPV6);
     231                 :          1 :   g_assert (addr != NULL);
     232                 :          1 :   g_assert (port == 308);
     233                 :          1 :   g_assert (flowinfo == 10);
     234                 :          1 :   g_assert (scope_id == 25);
     235                 :            : 
     236                 :          1 :   g_object_unref (addr);
     237                 :          1 :   g_object_unref (saddr);
     238                 :          1 : }
     239                 :            : 
     240                 :            : static void
     241                 :          1 : test_socket_address_to_string (void)
     242                 :            : {
     243                 :          1 :   GSocketAddress *sa = NULL;
     244                 :          1 :   GInetAddress *ia = NULL;
     245                 :          1 :   gchar *str = NULL;
     246                 :            : 
     247                 :            :   /* IPv4. */
     248                 :          1 :   ia = g_inet_address_new_from_string ("123.1.123.1");
     249                 :          1 :   sa = g_inet_socket_address_new (ia, 80);
     250                 :          1 :   str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     251                 :          1 :   g_assert_cmpstr (str, ==, "123.1.123.1:80");
     252                 :          1 :   g_free (str);
     253                 :          1 :   g_object_unref (sa);
     254                 :          1 :   g_object_unref (ia);
     255                 :            : 
     256                 :            :   /* IPv6. */
     257                 :          1 :   ia = g_inet_address_new_from_string ("fe80::80");
     258                 :          1 :   sa = g_inet_socket_address_new (ia, 80);
     259                 :          1 :   str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     260                 :          1 :   g_assert_cmpstr (str, ==, "[fe80::80]:80");
     261                 :          1 :   g_free (str);
     262                 :          1 :   g_object_unref (sa);
     263                 :          1 :   g_object_unref (ia);
     264                 :            : 
     265                 :            :   /* IPv6 without port. */
     266                 :          1 :   ia = g_inet_address_new_from_string ("fe80::80");
     267                 :          1 :   sa = g_inet_socket_address_new (ia, 0);
     268                 :          1 :   str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     269                 :          1 :   g_assert_cmpstr (str, ==, "fe80::80");
     270                 :          1 :   g_free (str);
     271                 :          1 :   g_object_unref (sa);
     272                 :          1 :   g_object_unref (ia);
     273                 :            : 
     274                 :            :   /* IPv6 with scope. */
     275                 :          1 :   ia = g_inet_address_new_from_string ("::1");
     276                 :          1 :   sa = G_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
     277                 :            :                                        "address", ia,
     278                 :            :                                        "port", 123,
     279                 :            :                                        "flowinfo", 10,
     280                 :            :                                        "scope-id", 25,
     281                 :            :                                        NULL));
     282                 :          1 :   str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     283                 :          1 :   g_assert_cmpstr (str, ==, "[::1%25]:123");
     284                 :          1 :   g_free (str);
     285                 :          1 :   g_object_unref (sa);
     286                 :          1 :   g_object_unref (ia);
     287                 :          1 : }
     288                 :            : 
     289                 :            : static void
     290                 :          1 : test_mask_parse (void)
     291                 :            : {
     292                 :            :   GInetAddressMask *mask;
     293                 :          1 :   GError *error = NULL;
     294                 :            : 
     295                 :          1 :   mask = g_inet_address_mask_new_from_string ("10.0.0.0/8", &error);
     296                 :          1 :   g_assert_no_error (error);
     297                 :          1 :   g_assert (mask != NULL);
     298                 :          1 :   g_object_unref (mask);
     299                 :            : 
     300                 :          1 :   mask = g_inet_address_mask_new_from_string ("fe80::/10", &error);
     301                 :          1 :   g_assert_no_error (error);
     302                 :          1 :   g_assert (mask != NULL);
     303                 :          1 :   g_object_unref (mask);
     304                 :            : 
     305                 :          1 :   mask = g_inet_address_mask_new_from_string ("::", &error);
     306                 :          1 :   g_assert_no_error (error);
     307                 :          1 :   g_assert (mask != NULL);
     308                 :          1 :   g_object_unref (mask);
     309                 :            : 
     310                 :          1 :   mask = g_inet_address_mask_new_from_string ("::/abc", &error);
     311                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     312                 :          1 :   g_assert (mask == NULL);
     313                 :          1 :   g_clear_error (&error);
     314                 :            : 
     315                 :          1 :   mask = g_inet_address_mask_new_from_string ("127.0.0.1/128", &error);
     316                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     317                 :          1 :   g_assert (mask == NULL);
     318                 :          1 :   g_clear_error (&error);
     319                 :          1 : }
     320                 :            : 
     321                 :            : static void
     322                 :          1 : test_mask_property (void)
     323                 :            : {
     324                 :            :   GInetAddressMask *mask;
     325                 :            :   GInetAddress *addr;
     326                 :            :   GSocketFamily family;
     327                 :            :   guint len;
     328                 :            : 
     329                 :          1 :   addr = g_inet_address_new_from_string ("fe80::");
     330                 :          1 :   mask = g_inet_address_mask_new_from_string ("fe80::/10", NULL);
     331                 :          1 :   g_assert (g_inet_address_mask_get_family (mask) == G_SOCKET_FAMILY_IPV6);
     332                 :          1 :   g_assert (g_inet_address_equal (addr, g_inet_address_mask_get_address (mask)));
     333                 :          1 :   g_assert (g_inet_address_mask_get_length (mask) == 10);
     334                 :          1 :   g_object_unref (addr);
     335                 :            : 
     336                 :          1 :   g_object_get (mask,
     337                 :            :                 "family", &family,
     338                 :            :                 "address", &addr,
     339                 :            :                 "length", &len,
     340                 :            :                 NULL);
     341                 :          1 :   g_assert (family == G_SOCKET_FAMILY_IPV6);
     342                 :          1 :   g_assert (addr != NULL);
     343                 :          1 :   g_assert (len == 10);
     344                 :          1 :   g_object_unref (addr);
     345                 :            : 
     346                 :          1 :   g_object_unref (mask);
     347                 :          1 : }
     348                 :            : 
     349                 :            : static void
     350                 :          1 : test_mask_equal (void)
     351                 :            : {
     352                 :            :   GInetAddressMask *mask;
     353                 :            :   GInetAddressMask *mask2;
     354                 :            :   gchar *str;
     355                 :            : 
     356                 :          1 :   mask = g_inet_address_mask_new_from_string ("fe80:0:0::/10", NULL);
     357                 :          1 :   str = g_inet_address_mask_to_string (mask);
     358                 :          1 :   g_assert_cmpstr (str, ==, "fe80::/10");
     359                 :          1 :   mask2 = g_inet_address_mask_new_from_string (str, NULL);
     360                 :          1 :   g_assert (g_inet_address_mask_equal (mask, mask2));
     361                 :          1 :   g_object_unref (mask2);
     362                 :          1 :   g_free (str);
     363                 :            : 
     364                 :          1 :   mask2 = g_inet_address_mask_new_from_string ("fe80::/12", NULL);
     365                 :          1 :   g_assert (!g_inet_address_mask_equal (mask, mask2));
     366                 :          1 :   g_object_unref (mask2);
     367                 :            : 
     368                 :          1 :   mask2 = g_inet_address_mask_new_from_string ("ff80::/10", NULL);
     369                 :          1 :   g_assert (!g_inet_address_mask_equal (mask, mask2));
     370                 :          1 :   g_object_unref (mask2);
     371                 :            : 
     372                 :          1 :   g_object_unref (mask);
     373                 :          1 : }
     374                 :            : 
     375                 :            : static void
     376                 :          1 : test_mask_match (void)
     377                 :            : {
     378                 :            :   GInetAddressMask *mask;
     379                 :            :   GInetAddress *addr;
     380                 :            : 
     381                 :          1 :   mask = g_inet_address_mask_new_from_string ("1.2.0.0/16", NULL);
     382                 :            : 
     383                 :          1 :   addr = g_inet_address_new_from_string ("1.2.0.0");
     384                 :          1 :   g_assert (g_inet_address_mask_matches (mask, addr));
     385                 :          1 :   g_object_unref (addr);
     386                 :          1 :   addr = g_inet_address_new_from_string ("1.2.3.4");
     387                 :          1 :   g_assert (g_inet_address_mask_matches (mask, addr));
     388                 :          1 :   g_object_unref (addr);
     389                 :          1 :   addr = g_inet_address_new_from_string ("1.3.1.1");
     390                 :          1 :   g_assert (!g_inet_address_mask_matches (mask, addr));
     391                 :          1 :   g_object_unref (addr);
     392                 :            : 
     393                 :          1 :   g_object_unref (mask);
     394                 :            : 
     395                 :          1 :   mask = g_inet_address_mask_new_from_string ("1.2.0.0/24", NULL);
     396                 :            : 
     397                 :          1 :   addr = g_inet_address_new_from_string ("1.2.0.0");
     398                 :          1 :   g_assert (g_inet_address_mask_matches (mask, addr));
     399                 :          1 :   g_object_unref (addr);
     400                 :          1 :   addr = g_inet_address_new_from_string ("1.2.3.4");
     401                 :          1 :   g_assert (!g_inet_address_mask_matches (mask, addr));
     402                 :          1 :   g_object_unref (addr);
     403                 :          1 :   addr = g_inet_address_new_from_string ("1.2.0.24");
     404                 :          1 :   g_assert (g_inet_address_mask_matches (mask, addr));
     405                 :          1 :   g_object_unref (addr);
     406                 :            : 
     407                 :          1 :   g_object_unref (mask);
     408                 :            : 
     409                 :          1 : }
     410                 :            : 
     411                 :            : int
     412                 :          1 : main (int argc, char *argv[])
     413                 :            : {
     414                 :          1 :   g_test_init (&argc, &argv, NULL);
     415                 :            : 
     416                 :          1 :   g_test_add_func ("/inet-address/parse", test_parse);
     417                 :          1 :   g_test_add_func ("/inet-address/any", test_any);
     418                 :          1 :   g_test_add_func ("/inet-address/loopback", test_loopback);
     419                 :          1 :   g_test_add_func ("/inet-address/bytes", test_bytes);
     420                 :          1 :   g_test_add_func ("/inet-address/property", test_property);
     421                 :          1 :   g_test_add_func ("/socket-address/basic", test_socket_address);
     422                 :          1 :   g_test_add_func ("/socket-address/to-string", test_socket_address_to_string);
     423                 :          1 :   g_test_add_func ("/address-mask/parse", test_mask_parse);
     424                 :          1 :   g_test_add_func ("/address-mask/property", test_mask_property);
     425                 :          1 :   g_test_add_func ("/address-mask/equal", test_mask_equal);
     426                 :          1 :   g_test_add_func ("/address-mask/match", test_mask_match);
     427                 :            : 
     428                 :          1 :   return g_test_run ();
     429                 :            : }

Generated by: LCOV version 1.14