1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: David Zeuthen <davidz@redhat.com>
26 #include <gio/gunixsocketaddress.h>
29 /* ---------------------------------------------------------------------------------------------------- */
32 test_empty_address (void)
36 g_dbus_address_get_stream_sync ("",
40 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
44 /* Test that g_dbus_is_supported_address() returns FALSE for an unparsable
47 test_unsupported_address (void)
51 g_assert_false (g_dbus_is_supported_address (";", &error));
52 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
53 g_clear_error (&error);
57 assert_is_supported_address (const gchar *address)
61 g_assert_true (g_dbus_is_address (address));
63 g_assert_true (g_dbus_is_supported_address (address, NULL));
64 g_assert_true (g_dbus_is_supported_address (address, &error));
65 g_assert_no_error (error);
69 assert_not_supported_address (const gchar *address)
73 g_assert_true (g_dbus_is_address (address));
75 g_assert_false (g_dbus_is_supported_address (address, NULL));
76 g_assert_false (g_dbus_is_supported_address (address, &error));
77 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
78 g_clear_error (&error);
81 /* Test that g_dbus_is_address() returns FALSE for various differently invalid
84 test_address_parsing (void)
86 assert_not_supported_address ("some-imaginary-transport:foo=bar");
87 g_assert_true (g_dbus_is_address ("some-imaginary-transport:foo=bar"));
89 assert_not_supported_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid");
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 ("=:;"));
95 g_assert_false (g_dbus_is_address (":=;:="));
96 g_assert_false (g_dbus_is_address ("transport-name:="));
97 g_assert_false (g_dbus_is_address ("transport-name:=bar"));
99 g_assert_false (g_dbus_is_address ("transport-name:foo"));
100 g_assert_false (g_dbus_is_address ("transport-name:foo=%00"));
101 g_assert_false (g_dbus_is_address ("transport-name:%00=bar"));
103 assert_not_supported_address ("magic-tractor:");
107 test_unix_address (void)
110 g_test_skip ("unix transport is not supported on non-Unix platforms");
112 assert_is_supported_address ("unix:path=/tmp/dbus-test");
113 assert_is_supported_address ("unix:path=/tmp/dbus-test,guid=0");
114 assert_is_supported_address ("unix:abstract=/tmp/dbus-another-test");
115 assert_is_supported_address ("unix:abstract=/tmp/dbus-another-test,guid=1000");
116 assert_not_supported_address ("unix:foo=bar");
117 g_assert_false (g_dbus_is_address ("unix:path=/foo;abstract=/bar"));
118 assert_is_supported_address ("unix:path=/tmp/concrete;unix:abstract=/tmp/abstract");
119 assert_is_supported_address ("unix:tmpdir=/tmp");
120 assert_is_supported_address ("unix:dir=/tmp");
121 assert_not_supported_address ("unix:tmpdir=/tmp,path=/tmp");
122 assert_not_supported_address ("unix:tmpdir=/tmp,abstract=/tmp/foo");
123 assert_not_supported_address ("unix:tmpdir=/tmp,dir=/tmp");
124 assert_not_supported_address ("unix:path=/tmp,abstract=/tmp/foo");
125 assert_not_supported_address ("unix:path=/tmp,dir=/tmp");
126 assert_not_supported_address ("unix:abstract=/tmp/foo,dir=/tmp");
127 assert_not_supported_address ("unix:");
132 test_nonce_tcp_address (void)
134 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar");
135 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,guid=0");
136 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv6");
137 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv4");
138 assert_is_supported_address ("nonce-tcp:host=localhost");
139 assert_is_supported_address ("nonce-tcp:host=localhost,guid=1000");
141 assert_not_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=blah");
142 assert_not_supported_address ("nonce-tcp:host=localhost,port=420000,noncefile=/foo/bar,family=ipv4");
143 assert_not_supported_address ("nonce-tcp:host=,port=x42,noncefile=/foo/bar,family=ipv4");
144 assert_not_supported_address ("nonce-tcp:host=,port=42x,noncefile=/foo/bar,family=ipv4");
145 assert_not_supported_address ("nonce-tcp:host=,port=420000,noncefile=/foo/bar,family=ipv4");
146 assert_not_supported_address ("nonce-tcp:meaningless-key=blah");
147 assert_not_supported_address ("nonce-tcp:host=localhost,port=-1");
148 assert_not_supported_address ("nonce-tcp:host=localhost,port=420000");
149 assert_not_supported_address ("nonce-tcp:host=localhost,port=42x");
150 assert_not_supported_address ("nonce-tcp:host=localhost,port=");
151 assert_not_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=");
155 test_tcp_address (void)
157 assert_is_supported_address ("tcp:host=localhost");
158 assert_is_supported_address ("tcp:host=localhost,guid=1000");
159 assert_not_supported_address ("tcp:host=localhost,noncefile=/tmp/foo");
160 assert_is_supported_address ("tcp:host=localhost,port=42");
161 assert_is_supported_address ("tcp:host=localhost,port=42,guid=1000");
162 assert_not_supported_address ("tcp:host=localhost,port=-1");
163 assert_not_supported_address ("tcp:host=localhost,port=420000");
164 assert_not_supported_address ("tcp:host=localhost,port=42x");
165 assert_not_supported_address ("tcp:host=localhost,port=");
166 assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv4");
167 assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv6");
168 assert_not_supported_address ("tcp:host=localhost,port=42,family=sopranos");
172 test_autolaunch_address (void)
174 assert_is_supported_address ("autolaunch:");
178 test_mixed_address (void)
180 assert_is_supported_address ("unix:path=/tmp/dbus1;unix:path=/tmp/dbus2");
181 assert_is_supported_address ("tcp:host=localhost,port=42;autolaunch:");
182 assert_not_supported_address ("tcp:host=localhost,port=42;tcp:family=bla");
185 static const struct { const char *before; const char *after; } escaping[] = {
187 { "/.\\-_", "/.\\-_" },
188 { "<=>", "%3C%3D%3E" },
189 { "/foo~1", "/foo%7E1" },
190 { "\xe2\x98\xad\xff", "%E2%98%AD%FF" },
195 test_escape_address (void)
199 for (i = 0; escaping[i].before != NULL; i++)
201 gchar *s = g_dbus_address_escape_value (escaping[i].before);
203 g_assert_cmpstr (s, ==, escaping[i].after);
212 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
214 g_test_add_func ("/gdbus/empty-address", test_empty_address);
215 g_test_add_func ("/gdbus/unsupported-address", test_unsupported_address);
216 g_test_add_func ("/gdbus/address-parsing", test_address_parsing);
217 g_test_add_func ("/gdbus/unix-address", test_unix_address);
218 g_test_add_func ("/gdbus/nonce-tcp-address", test_nonce_tcp_address);
219 g_test_add_func ("/gdbus/tcp-address", test_tcp_address);
220 g_test_add_func ("/gdbus/autolaunch-address", test_autolaunch_address);
221 g_test_add_func ("/gdbus/mixed-address", test_mixed_address);
222 g_test_add_func ("/gdbus/escape-address", test_escape_address);