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

source code of gtk/subprojects/glib/gio/tests/gdbus-addresses.c