Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / gdbus-addresses.c
blob0ab05661a72e94e45e385b0d05ffd7fc9c00c892
1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include <gio/gio.h>
23 #ifdef G_OS_UNIX
24 #include <gio/gunixsocketaddress.h>
25 #endif
27 /* ---------------------------------------------------------------------------------------------------- */
29 static void
30 test_empty_address (void)
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);
42 static void
43 assert_is_supported_address (const gchar *address)
45 GError *error = NULL;
47 g_assert_true (g_dbus_is_supported_address (address, NULL));
48 g_assert_true (g_dbus_is_supported_address (address, &error));
49 g_assert_no_error (error);
52 static void
53 assert_not_supported_address (const gchar *address)
55 GError *error = NULL;
57 g_assert_false (g_dbus_is_supported_address (address, NULL));
58 g_assert_false (g_dbus_is_supported_address (address, &error));
59 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
60 g_clear_error (&error);
63 #ifdef G_OS_UNIX
64 static void
65 test_unix_address (void)
67 assert_not_supported_address ("some-imaginary-transport:foo=bar");
68 assert_is_supported_address ("unix:path=/tmp/dbus-test");
69 assert_is_supported_address ("unix:abstract=/tmp/dbus-another-test");
70 g_assert (g_dbus_is_address ("unix:foo=bar"));
71 assert_not_supported_address ("unix:foo=bar");
72 g_assert (!g_dbus_is_address ("unix:path=/foo;abstract=/bar"));
73 assert_not_supported_address ("unix:path=/foo;abstract=/bar");
74 assert_is_supported_address ("unix:path=/tmp/concrete;unix:abstract=/tmp/abstract");
75 g_assert (g_dbus_is_address ("some-imaginary-transport:foo=bar"));
77 g_assert (g_dbus_is_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid"));
78 assert_not_supported_address ("some-imaginary-transport:foo=bar;unix:path=/this/is/valid");
80 #endif
82 static void
83 test_nonce_tcp_address (void)
85 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar");
86 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv6");
87 assert_is_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=ipv4");
89 assert_not_supported_address ("nonce-tcp:host=localhost,port=42,noncefile=/foo/bar,family=blah");
90 assert_not_supported_address ("nonce-tcp:host=localhost,port=420000,noncefile=/foo/bar,family=ipv4");
91 assert_not_supported_address ("nonce-tcp:host=,port=x42,noncefile=/foo/bar,family=ipv4");
92 assert_not_supported_address ("nonce-tcp:host=,port=42x,noncefile=/foo/bar,family=ipv4");
93 assert_not_supported_address ("nonce-tcp:host=,port=420000,noncefile=/foo/bar,family=ipv4");
96 static void
97 test_tcp_address (void)
99 assert_is_supported_address ("tcp:host=localhost");
100 assert_not_supported_address ("tcp:host=localhost,noncefile=/tmp/foo");
101 assert_is_supported_address ("tcp:host=localhost,port=42");
102 assert_not_supported_address ("tcp:host=localhost,port=-1");
103 assert_not_supported_address ("tcp:host=localhost,port=420000");
104 assert_not_supported_address ("tcp:host=localhost,port=42x");
105 assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv4");
106 assert_is_supported_address ("tcp:host=localhost,port=42,family=ipv6");
107 assert_not_supported_address ("tcp:host=localhost,port=42,family=sopranos");
110 static void
111 test_autolaunch_address (void)
113 assert_is_supported_address ("autolaunch:");
116 static void
117 test_mixed_address (void)
119 assert_is_supported_address ("unix:path=/tmp/dbus1;unix:path=/tmp/dbus2");
120 assert_is_supported_address ("tcp:host=localhost,port=42;autolaunch:");
121 assert_not_supported_address ("tcp:host=localhost,port=42;tcp:family=bla");
124 static const struct { const char *before; const char *after; } escaping[] = {
125 { "foo", "foo" },
126 { "/.\\-_", "/.\\-_" },
127 { "<=>", "%3C%3D%3E" },
128 { "/foo~1", "/foo%7E1" },
129 { "\xe2\x98\xad\xff", "%E2%98%AD%FF" },
130 { NULL, NULL }
133 static void
134 test_escape_address (void)
136 gsize i;
138 for (i = 0; escaping[i].before != NULL; i++)
140 gchar *s = g_dbus_address_escape_value (escaping[i].before);
142 g_assert_cmpstr (s, ==, escaping[i].after);
143 g_free (s);
148 main (int argc,
149 char *argv[])
151 g_test_init (&argc, &argv, NULL);
153 g_test_add_func ("/gdbus/empty-address", test_empty_address);
154 #ifdef G_OS_UNIX
155 g_test_add_func ("/gdbus/unix-address", test_unix_address);
156 #endif
157 g_test_add_func ("/gdbus/nonce-tcp-address", test_nonce_tcp_address);
158 g_test_add_func ("/gdbus/tcp-address", test_tcp_address);
159 g_test_add_func ("/gdbus/autolaunch-address", test_autolaunch_address);
160 g_test_add_func ("/gdbus/mixed-address", test_mixed_address);
161 g_test_add_func ("/gdbus/escape-address", test_escape_address);
163 return g_test_run();