(root)/
glib-2.79.0/
gio/
tests/
inet-address.c
       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  test_parse (void)
      32  {
      33    GInetAddress *addr;
      34  
      35    addr = g_inet_address_new_from_string ("0:0:0:0:0:0:0:0");
      36    g_assert (addr != NULL);
      37    g_object_unref (addr);
      38    addr = g_inet_address_new_from_string ("1:0:0:0:0:0:0:8");
      39    g_assert (addr != NULL);
      40    g_object_unref (addr);
      41    addr = g_inet_address_new_from_string ("0:0:0:0:0:FFFF:204.152.189.116");
      42    g_assert (addr != NULL);
      43    g_object_unref (addr);
      44    addr = g_inet_address_new_from_string ("::1");
      45    g_assert (addr != NULL);
      46    g_object_unref (addr);
      47    addr = g_inet_address_new_from_string ("::");
      48    g_assert (addr != NULL);
      49    g_object_unref (addr);
      50    addr = g_inet_address_new_from_string ("::FFFF:204.152.189.116");
      51    g_assert (addr != NULL);
      52    g_object_unref (addr);
      53    addr = g_inet_address_new_from_string ("204.152.189.116");
      54    g_assert (addr != NULL);
      55    g_object_unref (addr);
      56  
      57    addr = g_inet_address_new_from_string ("::1::2");
      58    g_assert (addr == NULL);
      59    addr = g_inet_address_new_from_string ("2001:1:2:3:4:5:6:7]");
      60    g_assert (addr == NULL);
      61    addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7");
      62    g_assert (addr == NULL);
      63    addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]");
      64    g_assert (addr == NULL);
      65    addr = g_inet_address_new_from_string ("[2001:1:2:3:4:5:6:7]:80");
      66    g_assert (addr == NULL);
      67    addr = g_inet_address_new_from_string ("0:1:2:3:4:5:6:7:8:9");
      68    g_assert (addr == NULL);
      69    addr = g_inet_address_new_from_string ("::FFFFFFF");
      70    g_assert (addr == NULL);
      71    addr = g_inet_address_new_from_string ("204.152.189.116:80");
      72    g_assert (addr == NULL);
      73  }
      74  
      75  static void
      76  test_any (void)
      77  {
      78    GInetAddress *addr;
      79    GSocketFamily family[2] = { G_SOCKET_FAMILY_IPV4, G_SOCKET_FAMILY_IPV6 };
      80    gsize size[2] = { 4, 16 };
      81    gint i;
      82  
      83    for (i = 0; i < 2; i++)
      84      {
      85        addr = g_inet_address_new_any (family[i]);
      86  
      87        g_assert (g_inet_address_get_is_any (addr));
      88        g_assert (g_inet_address_get_family (addr) == family[i]);
      89        g_assert (g_inet_address_get_native_size (addr) == size[i]);
      90        g_assert (!g_inet_address_get_is_loopback (addr));
      91        g_assert (!g_inet_address_get_is_link_local (addr));
      92        g_assert (!g_inet_address_get_is_site_local (addr));
      93        g_assert (!g_inet_address_get_is_multicast (addr));
      94        g_assert (!g_inet_address_get_is_mc_global (addr));
      95        g_assert (!g_inet_address_get_is_mc_link_local (addr));
      96        g_assert (!g_inet_address_get_is_mc_node_local (addr));
      97        g_assert (!g_inet_address_get_is_mc_org_local (addr));
      98        g_assert (!g_inet_address_get_is_mc_site_local (addr));
      99  
     100        g_object_unref (addr);
     101     }
     102  }
     103  
     104  static void
     105  test_loopback (void)
     106  {
     107    GInetAddress *addr;
     108  
     109    addr = g_inet_address_new_from_string ("::1");
     110    g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV6);
     111    g_assert (g_inet_address_get_is_loopback (addr));
     112    g_object_unref (addr);
     113  
     114    addr = g_inet_address_new_from_string ("127.0.0.0");
     115    g_assert (g_inet_address_get_family (addr) == G_SOCKET_FAMILY_IPV4);
     116    g_assert (g_inet_address_get_is_loopback (addr));
     117    g_object_unref (addr);
     118  }
     119  
     120  static void
     121  test_bytes (void)
     122  {
     123    GInetAddress *addr1, *addr2, *addr3;
     124    const guint8 *bytes;
     125  
     126    addr1 = g_inet_address_new_from_string ("192.168.0.100");
     127    addr2 = g_inet_address_new_from_string ("192.168.0.101");
     128    bytes = g_inet_address_to_bytes (addr1);
     129    addr3 = g_inet_address_new_from_bytes (bytes, G_SOCKET_FAMILY_IPV4);
     130  
     131    g_assert (!g_inet_address_equal (addr1, addr2));
     132    g_assert (g_inet_address_equal (addr1, addr3));
     133  
     134    g_object_unref (addr1);
     135    g_object_unref (addr2);
     136    g_object_unref (addr3);
     137  }
     138  
     139  static void
     140  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    addr = g_inet_address_new_from_string ("ff85::");
     157    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    g_assert (family == G_SOCKET_FAMILY_IPV6);
     173    g_assert (!any);
     174    g_assert (!loopback);
     175    g_assert (!link_local);
     176    g_assert (!site_local);
     177    g_assert (multicast);
     178    g_assert (!mc_global);
     179    g_assert (!mc_link_local);
     180    g_assert (!mc_node_local);
     181    g_assert (!mc_org_local);
     182    g_assert (mc_site_local);
     183  
     184    g_object_unref (addr);
     185  }
     186  
     187  static void
     188  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    addr = g_inet_address_new_from_string ("::ffff:125.1.15.5");
     198    saddr = G_INET_SOCKET_ADDRESS (g_inet_socket_address_new (addr, 308));
     199  
     200    g_assert (g_inet_address_equal (addr, g_inet_socket_address_get_address (saddr)));
     201    g_object_unref (addr);
     202  
     203    g_assert (g_inet_socket_address_get_port (saddr) == 308);
     204    g_assert (g_inet_socket_address_get_flowinfo (saddr) == 0);
     205    g_assert (g_inet_socket_address_get_scope_id (saddr) == 0);
     206  
     207    g_object_unref (saddr);
     208  
     209    addr = g_inet_address_new_from_string ("::1");
     210    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    g_object_unref (addr);
     217  
     218    g_assert (g_inet_socket_address_get_port (saddr) == 308);
     219    g_assert (g_inet_socket_address_get_flowinfo (saddr) == 10);
     220    g_assert (g_inet_socket_address_get_scope_id (saddr) == 25);
     221  
     222    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    g_assert (family == G_SOCKET_FAMILY_IPV6);
     231    g_assert (addr != NULL);
     232    g_assert (port == 308);
     233    g_assert (flowinfo == 10);
     234    g_assert (scope_id == 25);
     235  
     236    g_object_unref (addr);
     237    g_object_unref (saddr);
     238  }
     239  
     240  static void
     241  test_socket_address_to_string (void)
     242  {
     243    GSocketAddress *sa = NULL;
     244    GInetAddress *ia = NULL;
     245    gchar *str = NULL;
     246  
     247    /* IPv4. */
     248    ia = g_inet_address_new_from_string ("123.1.123.1");
     249    sa = g_inet_socket_address_new (ia, 80);
     250    str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     251    g_assert_cmpstr (str, ==, "123.1.123.1:80");
     252    g_free (str);
     253    g_object_unref (sa);
     254    g_object_unref (ia);
     255  
     256    /* IPv6. */
     257    ia = g_inet_address_new_from_string ("fe80::80");
     258    sa = g_inet_socket_address_new (ia, 80);
     259    str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     260    g_assert_cmpstr (str, ==, "[fe80::80]:80");
     261    g_free (str);
     262    g_object_unref (sa);
     263    g_object_unref (ia);
     264  
     265    /* IPv6 without port. */
     266    ia = g_inet_address_new_from_string ("fe80::80");
     267    sa = g_inet_socket_address_new (ia, 0);
     268    str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     269    g_assert_cmpstr (str, ==, "fe80::80");
     270    g_free (str);
     271    g_object_unref (sa);
     272    g_object_unref (ia);
     273  
     274    /* IPv6 with scope. */
     275    ia = g_inet_address_new_from_string ("::1");
     276    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    str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
     283    g_assert_cmpstr (str, ==, "[::1%25]:123");
     284    g_free (str);
     285    g_object_unref (sa);
     286    g_object_unref (ia);
     287  }
     288  
     289  static void
     290  test_mask_parse (void)
     291  {
     292    GInetAddressMask *mask;
     293    GError *error = NULL;
     294  
     295    mask = g_inet_address_mask_new_from_string ("10.0.0.0/8", &error);
     296    g_assert_no_error (error);
     297    g_assert (mask != NULL);
     298    g_object_unref (mask);
     299  
     300    mask = g_inet_address_mask_new_from_string ("fe80::/10", &error);
     301    g_assert_no_error (error);
     302    g_assert (mask != NULL);
     303    g_object_unref (mask);
     304  
     305    mask = g_inet_address_mask_new_from_string ("::", &error);
     306    g_assert_no_error (error);
     307    g_assert (mask != NULL);
     308    g_object_unref (mask);
     309  
     310    mask = g_inet_address_mask_new_from_string ("::/abc", &error);
     311    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     312    g_assert (mask == NULL);
     313    g_clear_error (&error);
     314  
     315    mask = g_inet_address_mask_new_from_string ("127.0.0.1/128", &error);
     316    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     317    g_assert (mask == NULL);
     318    g_clear_error (&error);
     319  }
     320  
     321  static void
     322  test_mask_property (void)
     323  {
     324    GInetAddressMask *mask;
     325    GInetAddress *addr;
     326    GSocketFamily family;
     327    guint len;
     328  
     329    addr = g_inet_address_new_from_string ("fe80::");
     330    mask = g_inet_address_mask_new_from_string ("fe80::/10", NULL);
     331    g_assert (g_inet_address_mask_get_family (mask) == G_SOCKET_FAMILY_IPV6);
     332    g_assert (g_inet_address_equal (addr, g_inet_address_mask_get_address (mask)));
     333    g_assert (g_inet_address_mask_get_length (mask) == 10);
     334    g_object_unref (addr);
     335  
     336    g_object_get (mask,
     337                  "family", &family,
     338                  "address", &addr,
     339                  "length", &len,
     340                  NULL);
     341    g_assert (family == G_SOCKET_FAMILY_IPV6);
     342    g_assert (addr != NULL);
     343    g_assert (len == 10);
     344    g_object_unref (addr);
     345  
     346    g_object_unref (mask);
     347  }
     348  
     349  static void
     350  test_mask_equal (void)
     351  {
     352    GInetAddressMask *mask;
     353    GInetAddressMask *mask2;
     354    gchar *str;
     355  
     356    mask = g_inet_address_mask_new_from_string ("fe80:0:0::/10", NULL);
     357    str = g_inet_address_mask_to_string (mask);
     358    g_assert_cmpstr (str, ==, "fe80::/10");
     359    mask2 = g_inet_address_mask_new_from_string (str, NULL);
     360    g_assert (g_inet_address_mask_equal (mask, mask2));
     361    g_object_unref (mask2);
     362    g_free (str);
     363  
     364    mask2 = g_inet_address_mask_new_from_string ("fe80::/12", NULL);
     365    g_assert (!g_inet_address_mask_equal (mask, mask2));
     366    g_object_unref (mask2);
     367  
     368    mask2 = g_inet_address_mask_new_from_string ("ff80::/10", NULL);
     369    g_assert (!g_inet_address_mask_equal (mask, mask2));
     370    g_object_unref (mask2);
     371  
     372    g_object_unref (mask);
     373  }
     374  
     375  static void
     376  test_mask_match (void)
     377  {
     378    GInetAddressMask *mask;
     379    GInetAddress *addr;
     380  
     381    mask = g_inet_address_mask_new_from_string ("1.2.0.0/16", NULL);
     382  
     383    addr = g_inet_address_new_from_string ("1.2.0.0");
     384    g_assert (g_inet_address_mask_matches (mask, addr));
     385    g_object_unref (addr);
     386    addr = g_inet_address_new_from_string ("1.2.3.4");
     387    g_assert (g_inet_address_mask_matches (mask, addr));
     388    g_object_unref (addr);
     389    addr = g_inet_address_new_from_string ("1.3.1.1");
     390    g_assert (!g_inet_address_mask_matches (mask, addr));
     391    g_object_unref (addr);
     392  
     393    g_object_unref (mask);
     394  
     395    mask = g_inet_address_mask_new_from_string ("1.2.0.0/24", NULL);
     396  
     397    addr = g_inet_address_new_from_string ("1.2.0.0");
     398    g_assert (g_inet_address_mask_matches (mask, addr));
     399    g_object_unref (addr);
     400    addr = g_inet_address_new_from_string ("1.2.3.4");
     401    g_assert (!g_inet_address_mask_matches (mask, addr));
     402    g_object_unref (addr);
     403    addr = g_inet_address_new_from_string ("1.2.0.24");
     404    g_assert (g_inet_address_mask_matches (mask, addr));
     405    g_object_unref (addr);
     406  
     407    g_object_unref (mask);
     408  
     409  }
     410  
     411  int
     412  main (int argc, char *argv[])
     413  {
     414    g_test_init (&argc, &argv, NULL);
     415  
     416    g_test_add_func ("/inet-address/parse", test_parse);
     417    g_test_add_func ("/inet-address/any", test_any);
     418    g_test_add_func ("/inet-address/loopback", test_loopback);
     419    g_test_add_func ("/inet-address/bytes", test_bytes);
     420    g_test_add_func ("/inet-address/property", test_property);
     421    g_test_add_func ("/socket-address/basic", test_socket_address);
     422    g_test_add_func ("/socket-address/to-string", test_socket_address_to_string);
     423    g_test_add_func ("/address-mask/parse", test_mask_parse);
     424    g_test_add_func ("/address-mask/property", test_mask_property);
     425    g_test_add_func ("/address-mask/equal", test_mask_equal);
     426    g_test_add_func ("/address-mask/match", test_mask_match);
     427  
     428    return g_test_run ();
     429  }