1 /* Simple sanity-check for D-Bus syntax validation.
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2010-2011 Nokia Corporation
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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
31 #include <dbus/dbus.h>
38 dbus_bool_t (*function) (const char *, DBusError *);
39 const char * const * valid;
40 const char * const * invalid;
43 Test paths, interfaces, members, errors, bus_names, signatures,
44 single_signatures, strings;
46 const char * const valid_paths[] = {
52 "/org/freedesktop/DBus",
53 "/org/freedesktop/Telepathy/AccountManager",
57 const char * const invalid_paths[] = {
63 "/com//example/MyApp",
65 "/\xa9", /* © in latin-1 */
66 "/\xc2\xa9", /* © in UTF-8 */
70 const char * const valid_interfaces[] = {
73 "org.freedesktop.DBus",
77 const char * const invalid_interfaces[] = {
85 "com.example.a\xc2\xa9",
89 const char * const valid_members[] = {
94 "BadgerMushroomSnake",
98 const char * const invalid_members[] = {
111 const char * const valid_errors[] = {
114 "org.freedesktop.DBus.NameHasNoOwner",
118 const char * const invalid_errors[] = {
126 "com.example.a\xc2\xa9",
130 const char * const valid_bus_names[] = {
137 "org.freedesktop.DBus",
141 const char * const invalid_bus_names[] = {
149 "com.example.a\xc2\xa9",
153 const char * const valid_signatures[] = {
159 const char * const invalid_signatures[] = {
165 const char * const valid_single_signatures[] = {
171 const char * const invalid_single_signatures[] = {
179 const char * const valid_strings[] = {
185 const char * const invalid_strings[] = {
192 gconstpointer arg G_GNUC_UNUSED)
194 dbus_error_init (&f->e);
196 #define FILL_TEST(name, func) \
198 (name).function = (func); \
199 (name).valid = valid_ ## name; \
200 (name).invalid = invalid_ ## name; \
203 FILL_TEST (paths, dbus_validate_path);
204 FILL_TEST (interfaces, dbus_validate_interface);
205 FILL_TEST (members, dbus_validate_member);
206 FILL_TEST (errors, dbus_validate_error_name);
207 FILL_TEST (bus_names, dbus_validate_bus_name);
208 FILL_TEST (signatures, dbus_signature_validate);
209 FILL_TEST (single_signatures, dbus_signature_validate_single);
210 FILL_TEST (strings, dbus_validate_utf8);
214 test_syntax (Fixture *f,
217 const Test *test = arg;
220 g_assert (test != NULL);
221 g_assert (test->function != NULL);
222 g_assert (test->valid != NULL);
223 g_assert (test->invalid != NULL);
225 for (i = 0; test->valid[i] != NULL; i++)
227 dbus_bool_t ok = test->function (test->valid[i], &f->e);
229 if (dbus_error_is_set (&f->e))
230 g_error ("%s was considered invalid: %s: %s", test->valid[i],
231 f->e.name, f->e.message);
234 g_error ("%s was considered invalid without an error", test->valid[i]);
237 for (i = 0; test->invalid[i] != NULL; i++)
239 dbus_bool_t ok = test->function (test->invalid[i], &f->e);
242 g_error ("%s should have been considered invalid", test->invalid[i]);
244 if (!dbus_error_is_set (&f->e))
245 g_error ("%s should have an error set", test->invalid[i]);
247 if (!dbus_validate_error_name (f->e.name, NULL))
248 g_error ("%s produced an invalid error name: %s",
249 test->invalid[i], f->e.name);
251 if (!dbus_validate_utf8 (f->e.message, NULL))
252 g_error ("%s produced an invalid error message: %s",
253 test->invalid[i], f->e.message);
255 dbus_error_free (&f->e);
260 teardown (Fixture *f,
261 gconstpointer arg G_GNUC_UNUSED)
263 dbus_error_free (&f->e);
270 g_test_init (&argc, &argv, NULL);
272 g_test_add ("/syntax/path", Fixture, &paths, setup, test_syntax, teardown);
273 g_test_add ("/syntax/interface", Fixture, &interfaces,
274 setup, test_syntax, teardown);
275 g_test_add ("/syntax/error", Fixture, &errors,
276 setup, test_syntax, teardown);
277 g_test_add ("/syntax/member", Fixture, &members,
278 setup, test_syntax, teardown);
279 g_test_add ("/syntax/bus-name", Fixture, &bus_names,
280 setup, test_syntax, teardown);
281 g_test_add ("/syntax/signature", Fixture, &signatures,
282 setup, test_syntax, teardown);
283 g_test_add ("/syntax/single-signature", Fixture, &single_signatures,
284 setup, test_syntax, teardown);
285 g_test_add ("/syntax/utf8", Fixture, &strings,
286 setup, test_syntax, teardown);
288 return g_test_run ();