[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / gdbus-addresses.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include <gio/gio.h>
22
23 #ifdef G_OS_UNIX
24 #include <gio/gunixsocketaddress.h>
25 #endif
26
27 /* ---------------------------------------------------------------------------------------------------- */
28
29 static void
30 test_empty_address (void)
31 {
32   GError *error;
33   error = NULL;
34   g_dbus_address_get_stream_sync ("",
35                                   NULL,
36                                   NULL,
37                                   &error);
38   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
39   g_error_free (error);
40 }
41
42 #ifdef G_OS_UNIX
43 static void
44 test_unix_address (void)
45 {
46   g_assert (!g_dbus_is_supported_address ("some-imaginary-transport:foo=bar", NULL));
47   g_assert (g_dbus_is_supported_address ("unix:path=/tmp/dbus-test", NULL));
48   g_assert (g_dbus_is_supported_address ("unix:abstract=/tmp/dbus-another-test", NULL));
49   g_assert (g_dbus_is_address ("unix:foo=bar"));
50   g_assert (!g_dbus_is_supported_address ("unix:foo=bar", NULL));
51   g_assert (!g_dbus_is_address ("unix:path=/foo;abstract=/bar"));
52   g_assert (!g_dbus_is_supported_address ("unix:path=/foo;abstract=/bar", NULL));
53   g_assert (g_dbus_is_supported_address ("unix:path=/tmp/concrete;unix:abstract=/tmp/abstract", NULL));
54   g_assert (g_dbus_is_address ("some-imaginary-transport:foo=bar"));
55
56   g_assert (g_dbus_is_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid"));
57   g_assert (!g_dbus_is_supported_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid", NULL));
58 }
59 #endif
60
61 static void
62 test_nonce_tcp_address (void)
63 {
64   g_assert (g_dbus_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar", NULL));
65   g_assert (g_dbus_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv6", NULL));
66   g_assert (g_dbus_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv4", NULL));
67
68   g_assert (!g_dbus_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=blah", NULL));
69   g_assert (!g_dbus_is_supported_address ("nonce-tcp:host=localhost,port=420000,noncefile=/foo/bar,family=ipv4", NULL));
70   g_assert (!g_dbus_is_supported_address ("nonce-tcp:host=,port=x42,noncefile=/foo/bar,family=ipv4", NULL));
71   g_assert (!g_dbus_is_supported_address ("nonce-tcp:host=,port=42x,noncefile=/foo/bar,family=ipv4", NULL));
72   g_assert (!g_dbus_is_supported_address ("nonce-tcp:host=,port=420000,noncefile=/foo/bar,family=ipv4", NULL));
73 }
74
75 static void
76 test_tcp_address (void)
77 {
78   g_assert (g_dbus_is_supported_address ("tcp:host=localhost", NULL));
79   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,noncefile=/tmp/foo", NULL));
80   g_assert (g_dbus_is_supported_address ("tcp:host=localhost,port=42", NULL));
81   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,port=-1", NULL));
82   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,port=420000", NULL));
83   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,port=42x", NULL));
84   g_assert (g_dbus_is_supported_address ("tcp:host=localhost,port=42,family=ipv4", NULL));
85   g_assert (g_dbus_is_supported_address ("tcp:host=localhost,port=42,family=ipv6", NULL));
86   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,port=42,family=sopranos", NULL));
87 }
88
89 static void
90 test_autolaunch_address (void)
91 {
92   g_assert (g_dbus_is_supported_address ("autolaunch:", NULL));
93 }
94
95 static void
96 test_mixed_address (void)
97 {
98   g_assert (g_dbus_is_supported_address ("unix:path=/tmp/dbus1;unix:path=/tmp/dbus2", NULL));
99   g_assert (g_dbus_is_supported_address ("tcp:host=localhost,port=42;autolaunch:", NULL));
100   g_assert (!g_dbus_is_supported_address ("tcp:host=localhost,port=42;tcp:family=bla", NULL));
101 }
102
103 static const struct { const char *before; const char *after; } escaping[] = {
104       { "foo", "foo" },
105       { "/.\\-_", "/.\\-_" },
106       { "<=>", "%3C%3D%3E" },
107       { "/foo~1", "/foo%7E1" },
108       { "\xe2\x98\xad\xff", "%E2%98%AD%FF" },
109       { NULL, NULL }
110 };
111
112 static void
113 test_escape_address (void)
114 {
115   gsize i;
116
117   for (i = 0; escaping[i].before != NULL; i++)
118     {
119       gchar *s = g_dbus_address_escape_value (escaping[i].before);
120
121       g_assert_cmpstr (s, ==, escaping[i].after);
122       g_free (s);
123     }
124 }
125
126 int
127 main (int   argc,
128       char *argv[])
129 {
130   g_test_init (&argc, &argv, NULL);
131
132   g_test_add_func ("/gdbus/empty-address", test_empty_address);
133 #ifdef G_OS_UNIX
134   g_test_add_func ("/gdbus/unix-address", test_unix_address);
135 #endif
136   g_test_add_func ("/gdbus/nonce-tcp-address", test_nonce_tcp_address);
137   g_test_add_func ("/gdbus/tcp-address", test_tcp_address);
138   g_test_add_func ("/gdbus/autolaunch-address", test_autolaunch_address);
139   g_test_add_func ("/gdbus/mixed-address", test_mixed_address);
140   g_test_add_func ("/gdbus/escape-address", test_escape_address);
141
142   return g_test_run();
143 }
144