Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / test / syntax.c
1 /* Simple sanity-check for D-Bus syntax validation.
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2010-2011 Nokia Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <glib.h>
30
31 #include <dbus/dbus.h>
32
33 typedef struct {
34     DBusError e;
35 } Fixture;
36
37 typedef struct {
38     dbus_bool_t (*function) (const char *, DBusError *);
39     const char * const * valid;
40     const char * const * invalid;
41 } Test;
42
43 Test paths, interfaces, members, errors, bus_names, signatures,
44      single_signatures, strings;
45
46 const char * const valid_paths[] = {
47     "/",
48     "/a",
49     "/_",
50     "/a/b/c",
51     "/com/example/123",
52     "/org/freedesktop/DBus",
53     "/org/freedesktop/Telepathy/AccountManager",
54     NULL
55 };
56
57 const char * const invalid_paths[] = {
58     "",
59     ".",
60     "//",
61     "/a/",
62     "/-",
63     "/com//example/MyApp",
64     "/$",
65     "/\xa9",      /* © in latin-1 */
66     "/\xc2\xa9",  /* © in UTF-8 */
67     NULL
68 };
69
70 const char * const valid_interfaces[] = {
71     "com.example",
72     "com.example.a0",
73     "org.freedesktop.DBus",
74     NULL
75 };
76
77 const char * const invalid_interfaces[] = {
78    "",
79     "com",
80     "com.example.",
81     "com.example..a0",
82     "com.example.0a",
83     "com.example.a$",
84     "com.example.a\xa9",
85     "com.example.a\xc2\xa9",
86     NULL
87 };
88
89 const char * const valid_members[] = {
90     "_",
91     "a",
92     "a0",
93     "GetAll",
94     "BadgerMushroomSnake",
95     NULL
96 };
97
98 const char * const invalid_members[] = {
99     "",
100     "-",
101     "a-",
102     "0",
103     "0_",
104     "Badger.Mushroom",
105     "a$",
106     "a\xa9",
107     "a\xc2\xa9",
108     NULL
109 };
110
111 const char * const valid_errors[] = {
112     "com.example",
113     "com.example.a0",
114     "org.freedesktop.DBus.NameHasNoOwner",
115     NULL
116 };
117
118 const char * const invalid_errors[] = {
119    "",
120     "com",
121     "com.example.",
122     "com.example..a0",
123     "com.example.0a",
124     "com.example.a$",
125     "com.example.a\xa9",
126     "com.example.a\xc2\xa9",
127     NULL
128 };
129
130 const char * const valid_bus_names[] = {
131     "com.example",
132     "com.example.a0",
133     "com.example._",
134     ":1.42",
135     ":1.2.3.4.5",
136     ":com.example",
137     "org.freedesktop.DBus",
138     NULL
139 };
140
141 const char * const invalid_bus_names[] = {
142    "",
143     "com",
144     "com.example.",
145     "com.example..a0",
146     "com.example.0a",
147     "com.example.a:b",
148     "com.example.a\xa9",
149     "com.example.a\xc2\xa9",
150     NULL
151 };
152
153 const char * const valid_signatures[] = {
154     "",
155     "a{sv}",
156     NULL
157 };
158
159 const char * const invalid_signatures[] = {
160     "a",
161     "a{s_}",
162     NULL
163 };
164
165 const char * const valid_single_signatures[] = {
166     "s",
167     "a{sv}",
168     NULL
169 };
170
171 const char * const invalid_single_signatures[] = {
172     "",
173     "a",
174     "sv",
175     "a{sv}as",
176     NULL
177 };
178
179 const char * const valid_strings[] = {
180     "",
181     "\xc2\xa9",       /* UTF-8 (c) symbol */
182     "\xef\xbf\xbe",   /* U+FFFE is reserved but Corrigendum 9 says it's OK */
183     NULL
184 };
185
186 const char * const invalid_strings[] = {
187     "\xa9",           /* Latin-1 (c) symbol */
188     "\xed\xa0\x80",   /* UTF-16 surrogates are not valid in UTF-8 */
189     NULL
190 };
191
192 static void
193 setup (Fixture *f,
194     gconstpointer arg G_GNUC_UNUSED)
195 {
196   dbus_error_init (&f->e);
197
198 #define FILL_TEST(name, func) \
199   do { \
200     (name).function = (func); \
201     (name).valid = valid_ ## name; \
202     (name).invalid = invalid_ ## name; \
203   } while (0)
204
205   FILL_TEST (paths, dbus_validate_path);
206   FILL_TEST (interfaces, dbus_validate_interface);
207   FILL_TEST (members, dbus_validate_member);
208   FILL_TEST (errors, dbus_validate_error_name);
209   FILL_TEST (bus_names, dbus_validate_bus_name);
210   FILL_TEST (signatures, dbus_signature_validate);
211   FILL_TEST (single_signatures, dbus_signature_validate_single);
212   FILL_TEST (strings, dbus_validate_utf8);
213 }
214
215 static void
216 test_syntax (Fixture *f,
217     gconstpointer arg)
218 {
219   const Test *test = arg;
220   int i;
221
222   g_assert (test != NULL);
223   g_assert (test->function != NULL);
224   g_assert (test->valid != NULL);
225   g_assert (test->invalid != NULL);
226
227   for (i = 0; test->valid[i] != NULL; i++)
228     {
229       dbus_bool_t ok = test->function (test->valid[i], &f->e);
230
231       if (dbus_error_is_set (&f->e))
232         g_error ("%s was considered invalid: %s: %s", test->valid[i],
233             f->e.name, f->e.message);
234
235       if (!ok)
236         g_error ("%s was considered invalid without an error", test->valid[i]);
237     }
238
239   for (i = 0; test->invalid[i] != NULL; i++)
240     {
241       dbus_bool_t ok = test->function (test->invalid[i], &f->e);
242
243       if (ok)
244         g_error ("%s should have been considered invalid", test->invalid[i]);
245
246       if (!dbus_error_is_set (&f->e))
247         g_error ("%s should have an error set", test->invalid[i]);
248
249       if (!dbus_validate_error_name (f->e.name, NULL))
250         g_error ("%s produced an invalid error name: %s",
251             test->invalid[i], f->e.name);
252
253       if (!dbus_validate_utf8 (f->e.message, NULL))
254         g_error ("%s produced an invalid error message: %s",
255             test->invalid[i], f->e.message);
256
257       dbus_error_free (&f->e);
258     }
259 }
260
261 static void
262 teardown (Fixture *f,
263     gconstpointer arg G_GNUC_UNUSED)
264 {
265   dbus_error_free (&f->e);
266 }
267
268 int
269 main (int argc,
270     char **argv)
271 {
272   g_test_init (&argc, &argv, NULL);
273
274   g_test_add ("/syntax/path", Fixture, &paths, setup, test_syntax, teardown);
275   g_test_add ("/syntax/interface", Fixture, &interfaces,
276       setup, test_syntax, teardown);
277   g_test_add ("/syntax/error", Fixture, &errors,
278       setup, test_syntax, teardown);
279   g_test_add ("/syntax/member", Fixture, &members,
280       setup, test_syntax, teardown);
281   g_test_add ("/syntax/bus-name", Fixture, &bus_names,
282       setup, test_syntax, teardown);
283   g_test_add ("/syntax/signature", Fixture, &signatures,
284       setup, test_syntax, teardown);
285   g_test_add ("/syntax/single-signature", Fixture, &single_signatures,
286       setup, test_syntax, teardown);
287   g_test_add ("/syntax/utf8", Fixture, &strings,
288       setup, test_syntax, teardown);
289
290   return g_test_run ();
291 }