LCOV - code coverage report
Current view: top level - gio/tests - gdbus-addresses.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 120 120
Test Date: 2024-11-26 05:23:01 Functions: 100.0 % 12 12
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLib testing framework examples and tests
       2                 :             :  *
       3                 :             :  * Copyright (C) 2008-2010 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                 :             :  * Author: David Zeuthen <davidz@redhat.com>
      21                 :             :  */
      22                 :             : 
      23                 :             : #include <gio/gio.h>
      24                 :             : 
      25                 :             : #ifdef G_OS_UNIX
      26                 :             : #include <gio/gunixsocketaddress.h>
      27                 :             : #endif
      28                 :             : 
      29                 :             : /* ---------------------------------------------------------------------------------------------------- */
      30                 :             : 
      31                 :             : static void
      32                 :           1 : test_empty_address (void)
      33                 :             : {
      34                 :             :   GError *error;
      35                 :           1 :   error = NULL;
      36                 :           1 :   g_dbus_address_get_stream_sync ("",
      37                 :             :                                   NULL,
      38                 :             :                                   NULL,
      39                 :             :                                   &error);
      40                 :           1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      41                 :           1 :   g_error_free (error);
      42                 :           1 : }
      43                 :             : 
      44                 :             : /* Test that g_dbus_is_supported_address() returns FALSE for an unparsable
      45                 :             :  * address. */
      46                 :             : static void
      47                 :           1 : test_unsupported_address (void)
      48                 :             : {
      49                 :           1 :   GError *error = NULL;
      50                 :             : 
      51                 :           1 :   g_assert_false (g_dbus_is_supported_address (";", &error));
      52                 :           1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      53                 :           1 :   g_clear_error (&error);
      54                 :           1 : }
      55                 :             : 
      56                 :             : static void
      57                 :          22 : assert_is_supported_address (const gchar *address)
      58                 :             : {
      59                 :          22 :   GError *error = NULL;
      60                 :             : 
      61                 :          22 :   g_assert_true (g_dbus_is_address (address));
      62                 :             : 
      63                 :          22 :   g_assert_true (g_dbus_is_supported_address (address, NULL));
      64                 :          22 :   g_assert_true (g_dbus_is_supported_address (address, &error));
      65                 :          22 :   g_assert_no_error (error);
      66                 :          22 : }
      67                 :             : 
      68                 :             : static void
      69                 :          29 : assert_not_supported_address (const gchar *address)
      70                 :             : {
      71                 :          29 :   GError *error = NULL;
      72                 :             : 
      73                 :          29 :   g_assert_true (g_dbus_is_address (address));
      74                 :             : 
      75                 :          29 :   g_assert_false (g_dbus_is_supported_address (address, NULL));
      76                 :          29 :   g_assert_false (g_dbus_is_supported_address (address, &error));
      77                 :          29 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      78                 :          29 :   g_clear_error (&error);
      79                 :          29 : }
      80                 :             : 
      81                 :             : /* Test that g_dbus_is_address() returns FALSE for various differently invalid
      82                 :             :  * input strings. */
      83                 :             : static void
      84                 :           1 : test_address_parsing (void)
      85                 :             : {
      86                 :           1 :   assert_not_supported_address ("some-imaginary-transport:foo=bar");
      87                 :           1 :   g_assert_true (g_dbus_is_address ("some-imaginary-transport:foo=bar"));
      88                 :             : 
      89                 :           1 :   assert_not_supported_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid");
      90                 :             : 
      91                 :           1 :   g_assert_false (g_dbus_is_address (""));
      92                 :           1 :   g_assert_false (g_dbus_is_address (";"));
      93                 :           1 :   g_assert_false (g_dbus_is_address (":"));
      94                 :           1 :   g_assert_false (g_dbus_is_address ("=:;"));
      95                 :           1 :   g_assert_false (g_dbus_is_address (":=;:="));
      96                 :           1 :   g_assert_false (g_dbus_is_address ("transport-name:="));
      97                 :           1 :   g_assert_false (g_dbus_is_address ("transport-name:=bar"));
      98                 :             : 
      99                 :           1 :   g_assert_false (g_dbus_is_address ("transport-name:foo"));
     100                 :           1 :   g_assert_false (g_dbus_is_address ("transport-name:foo=%00"));
     101                 :           1 :   g_assert_false (g_dbus_is_address ("transport-name:%00=bar"));
     102                 :             : 
     103                 :           1 :   assert_not_supported_address ("magic-tractor:");
     104                 :           1 : }
     105                 :             : 
     106                 :             : static void
     107                 :           1 : test_unix_address (void)
     108                 :             : {
     109                 :             : #ifndef G_OS_UNIX
     110                 :             :   g_test_skip ("unix transport is not supported on non-Unix platforms");
     111                 :             : #else
     112                 :           1 :   assert_is_supported_address ("unix:path=/tmp/dbus-test");
     113                 :           1 :   assert_is_supported_address ("unix:path=/tmp/dbus-test,guid=0");
     114                 :           1 :   assert_is_supported_address ("unix:abstract=/tmp/dbus-another-test");
     115                 :           1 :   assert_is_supported_address ("unix:abstract=/tmp/dbus-another-test,guid=1000");
     116                 :           1 :   assert_not_supported_address ("unix:foo=bar");
     117                 :           1 :   g_assert_false (g_dbus_is_address ("unix:path=/foo;abstract=/bar"));
     118                 :           1 :   assert_is_supported_address ("unix:path=/tmp/concrete;unix:abstract=/tmp/abstract");
     119                 :           1 :   assert_is_supported_address ("unix:tmpdir=/tmp");
     120                 :           1 :   assert_is_supported_address ("unix:dir=/tmp");
     121                 :           1 :   assert_not_supported_address ("unix:tmpdir=/tmp,path=/tmp");
     122                 :           1 :   assert_not_supported_address ("unix:tmpdir=/tmp,abstract=/tmp/foo");
     123                 :           1 :   assert_not_supported_address ("unix:tmpdir=/tmp,dir=/tmp");
     124                 :           1 :   assert_not_supported_address ("unix:path=/tmp,abstract=/tmp/foo");
     125                 :           1 :   assert_not_supported_address ("unix:path=/tmp,dir=/tmp");
     126                 :           1 :   assert_not_supported_address ("unix:abstract=/tmp/foo,dir=/tmp");
     127                 :           1 :   assert_not_supported_address ("unix:");
     128                 :             : #endif
     129                 :           1 : }
     130                 :             : 
     131                 :             : static void
     132                 :           1 : test_nonce_tcp_address (void)
     133                 :             : {
     134                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar");
     135                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,guid=0");
     136                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv6");
     137                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv4");
     138                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost");
     139                 :           1 :   assert_is_supported_address ("nonce-tcp:host=localhost,guid=1000");
     140                 :             : 
     141                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=blah");
     142                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=420000,noncefile=/foo/bar,family=ipv4");
     143                 :           1 :   assert_not_supported_address ("nonce-tcp:host=,port=x42,noncefile=/foo/bar,family=ipv4");
     144                 :           1 :   assert_not_supported_address ("nonce-tcp:host=,port=42x,noncefile=/foo/bar,family=ipv4");
     145                 :           1 :   assert_not_supported_address ("nonce-tcp:host=,port=420000,noncefile=/foo/bar,family=ipv4");
     146                 :           1 :   assert_not_supported_address ("nonce-tcp:meaningless-key=blah");
     147                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=-1");
     148                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=420000");
     149                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=42x");
     150                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=");
     151                 :           1 :   assert_not_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=");
     152                 :           1 : }
     153                 :             : 
     154                 :             : static void
     155                 :           1 : test_tcp_address (void)
     156                 :             : {
     157                 :           1 :   assert_is_supported_address ("tcp:host=localhost");
     158                 :           1 :   assert_is_supported_address ("tcp:host=localhost,guid=1000");
     159                 :           1 :   assert_not_supported_address ("tcp:host=localhost,noncefile=/tmp/foo");
     160                 :           1 :   assert_is_supported_address ("tcp:host=localhost,port=42");
     161                 :           1 :   assert_is_supported_address ("tcp:host=localhost,port=42,guid=1000");
     162                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=-1");
     163                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=420000");
     164                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=42x");
     165                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=");
     166                 :           1 :   assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv4");
     167                 :           1 :   assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv6");
     168                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=42,family=sopranos");
     169                 :           1 : }
     170                 :             : 
     171                 :             : static void
     172                 :           1 : test_autolaunch_address (void)
     173                 :             : {
     174                 :           1 :   assert_is_supported_address ("autolaunch:");
     175                 :           1 : }
     176                 :             : 
     177                 :             : static void
     178                 :           1 : test_mixed_address (void)
     179                 :             : {
     180                 :           1 :   assert_is_supported_address ("unix:path=/tmp/dbus1;unix:path=/tmp/dbus2");
     181                 :           1 :   assert_is_supported_address ("tcp:host=localhost,port=42;autolaunch:");
     182                 :           1 :   assert_not_supported_address ("tcp:host=localhost,port=42;tcp:family=bla");
     183                 :           1 : }
     184                 :             : 
     185                 :             : static const struct { const char *before; const char *after; } escaping[] = {
     186                 :             :       { "foo", "foo" },
     187                 :             :       { "/.\\-_", "/.\\-_" },
     188                 :             :       { "<=>", "%3C%3D%3E" },
     189                 :             :       { "/foo~1", "/foo%7E1" },
     190                 :             :       { "\xe2\x98\xad\xff", "%E2%98%AD%FF" },
     191                 :             :       { NULL, NULL }
     192                 :             : };
     193                 :             : 
     194                 :             : static void
     195                 :           1 : test_escape_address (void)
     196                 :             : {
     197                 :             :   gsize i;
     198                 :             : 
     199                 :           6 :   for (i = 0; escaping[i].before != NULL; i++)
     200                 :             :     {
     201                 :           5 :       gchar *s = g_dbus_address_escape_value (escaping[i].before);
     202                 :             : 
     203                 :           5 :       g_assert_cmpstr (s, ==, escaping[i].after);
     204                 :           5 :       g_free (s);
     205                 :             :     }
     206                 :           1 : }
     207                 :             : 
     208                 :             : int
     209                 :           1 : main (int   argc,
     210                 :             :       char *argv[])
     211                 :             : {
     212                 :           1 :   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     213                 :             : 
     214                 :           1 :   g_test_add_func ("/gdbus/empty-address", test_empty_address);
     215                 :           1 :   g_test_add_func ("/gdbus/unsupported-address", test_unsupported_address);
     216                 :           1 :   g_test_add_func ("/gdbus/address-parsing", test_address_parsing);
     217                 :           1 :   g_test_add_func ("/gdbus/unix-address", test_unix_address);
     218                 :           1 :   g_test_add_func ("/gdbus/nonce-tcp-address", test_nonce_tcp_address);
     219                 :           1 :   g_test_add_func ("/gdbus/tcp-address", test_tcp_address);
     220                 :           1 :   g_test_add_func ("/gdbus/autolaunch-address", test_autolaunch_address);
     221                 :           1 :   g_test_add_func ("/gdbus/mixed-address", test_mixed_address);
     222                 :           1 :   g_test_add_func ("/gdbus/escape-address", test_escape_address);
     223                 :             : 
     224                 :           1 :   return g_test_run();
     225                 :             : }
        

Generated by: LCOV version 2.0-1