1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-marshal-validate-util.c Would be in dbus-marshal-validate.c, but only used by tests/bus
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #ifdef DBUS_BUILD_TESTS
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 #include "dbus-internals.h"
30 #include "dbus-marshal-validate.h"
31 #include "dbus-marshal-recursive.h"
33 #include "dbus-test.h"
39 DBusValidity expected;
43 run_validity_tests (const ValidityTest *tests,
45 DBusValidity (* func) (const DBusString*,int,int))
49 for (i = 0; i < n_tests; i++)
54 _dbus_string_init_const (&str, tests[i].data);
56 v = (*func) (&str, 0, _dbus_string_get_length (&str));
58 if (v != tests[i].expected)
60 _dbus_warn ("Improper validation result %d for '%s'\n",
62 _dbus_assert_not_reached ("test failed");
69 static const ValidityTest signature_tests[] = {
73 { "(i)", DBUS_VALID },
74 { "w", DBUS_INVALID_UNKNOWN_TYPECODE },
75 { "a", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
76 { "aaaaaa", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
77 { "ii(ii)a", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
78 { "ia", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
79 /* DBUS_INVALID_SIGNATURE_TOO_LONG, */ /* too hard to test this way */
80 { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
81 DBUS_INVALID_EXCEEDED_MAXIMUM_ARRAY_RECURSION },
82 { "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((ii))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
83 DBUS_INVALID_EXCEEDED_MAXIMUM_STRUCT_RECURSION },
84 { ")", DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED },
85 { "i)", DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED },
86 { "a)", DBUS_INVALID_STRUCT_ENDED_BUT_NOT_STARTED },
87 { "(", DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED },
88 { "(i", DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED },
89 { "(iiiii", DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED },
90 { "(ai", DBUS_INVALID_STRUCT_STARTED_BUT_NOT_ENDED },
91 { "()", DBUS_INVALID_STRUCT_HAS_NO_FIELDS },
92 { "(())", DBUS_INVALID_STRUCT_HAS_NO_FIELDS },
93 { "a()", DBUS_INVALID_STRUCT_HAS_NO_FIELDS },
94 { "i()", DBUS_INVALID_STRUCT_HAS_NO_FIELDS },
95 { "()i", DBUS_INVALID_STRUCT_HAS_NO_FIELDS },
96 { "(a)", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
97 { "a{ia}", DBUS_INVALID_MISSING_ARRAY_ELEMENT_TYPE },
98 { "a{}", DBUS_INVALID_DICT_ENTRY_HAS_NO_FIELDS },
99 { "a{aii}", DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE },
100 /* { "a{i}", DBUS_INVALID_DICT_ENTRY_HAS_ONLY_ONE_FIELD }, */
101 /* { "{is}", DBUS_INVALID_DICT_ENTRY_NOT_INSIDE_ARRAY }, */
102 /* { "a{isi}", DBUS_INVALID_DICT_ENTRY_HAS_TOO_MANY_FIELDS }, */
106 _dbus_marshal_validate_test (void)
111 const char *valid_paths[] = {
117 const char *invalid_paths[] = {
133 const char *valid_interfaces[] = {
134 "org.freedesktop.Foo",
136 "Blah.Blah.Blah.Blah.Blah",
139 "a0.b1.c2.d3.e4.f5.g6",
142 const char *invalid_interfaces[] = {
170 const char *valid_unique_names[] = {
179 ":abce.freedesktop.blah"
181 const char *invalid_unique_names[] = {
195 const char *valid_members[] = {
203 const char *invalid_members[] = {
218 const char *valid_signatures[] = {
225 const char *invalid_signatures[] = {
227 "not a valid signature",
231 "a{(ii)i}" /* https://bugs.freedesktop.org/show_bug.cgi?id=17803 */
234 /* Signature with reason */
236 run_validity_tests (signature_tests, _DBUS_N_ELEMENTS (signature_tests),
237 _dbus_validate_signature_with_reason);
239 /* Path validation */
241 while (i < (int) _DBUS_N_ELEMENTS (valid_paths))
243 _dbus_string_init_const (&str, valid_paths[i]);
245 if (!_dbus_validate_path (&str, 0,
246 _dbus_string_get_length (&str)))
248 _dbus_warn ("Path \"%s\" should have been valid\n", valid_paths[i]);
249 _dbus_assert_not_reached ("invalid path");
256 while (i < (int) _DBUS_N_ELEMENTS (invalid_paths))
258 _dbus_string_init_const (&str, invalid_paths[i]);
260 if (_dbus_validate_path (&str, 0,
261 _dbus_string_get_length (&str)))
263 _dbus_warn ("Path \"%s\" should have been invalid\n", invalid_paths[i]);
264 _dbus_assert_not_reached ("valid path");
270 /* Interface validation */
272 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
274 _dbus_string_init_const (&str, valid_interfaces[i]);
276 if (!_dbus_validate_interface (&str, 0,
277 _dbus_string_get_length (&str)))
279 _dbus_warn ("Interface \"%s\" should have been valid\n", valid_interfaces[i]);
280 _dbus_assert_not_reached ("invalid interface");
287 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
289 _dbus_string_init_const (&str, invalid_interfaces[i]);
291 if (_dbus_validate_interface (&str, 0,
292 _dbus_string_get_length (&str)))
294 _dbus_warn ("Interface \"%s\" should have been invalid\n", invalid_interfaces[i]);
295 _dbus_assert_not_reached ("valid interface");
301 /* Bus name validation (check that valid interfaces are valid bus names,
302 * and invalid interfaces are invalid services except if they start with ':')
305 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
307 _dbus_string_init_const (&str, valid_interfaces[i]);
309 if (!_dbus_validate_bus_name (&str, 0,
310 _dbus_string_get_length (&str)))
312 _dbus_warn ("Bus name \"%s\" should have been valid\n", valid_interfaces[i]);
313 _dbus_assert_not_reached ("invalid bus name");
320 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
322 if (invalid_interfaces[i][0] != ':')
324 _dbus_string_init_const (&str, invalid_interfaces[i]);
326 if (_dbus_validate_bus_name (&str, 0,
327 _dbus_string_get_length (&str)))
329 _dbus_warn ("Bus name \"%s\" should have been invalid\n", invalid_interfaces[i]);
330 _dbus_assert_not_reached ("valid bus name");
337 /* unique name validation */
339 while (i < (int) _DBUS_N_ELEMENTS (valid_unique_names))
341 _dbus_string_init_const (&str, valid_unique_names[i]);
343 if (!_dbus_validate_bus_name (&str, 0,
344 _dbus_string_get_length (&str)))
346 _dbus_warn ("Bus name \"%s\" should have been valid\n", valid_unique_names[i]);
347 _dbus_assert_not_reached ("invalid unique name");
354 while (i < (int) _DBUS_N_ELEMENTS (invalid_unique_names))
356 _dbus_string_init_const (&str, invalid_unique_names[i]);
358 if (_dbus_validate_bus_name (&str, 0,
359 _dbus_string_get_length (&str)))
361 _dbus_warn ("Bus name \"%s\" should have been invalid\n", invalid_unique_names[i]);
362 _dbus_assert_not_reached ("valid unique name");
369 /* Error name validation (currently identical to interfaces)
372 while (i < (int) _DBUS_N_ELEMENTS (valid_interfaces))
374 _dbus_string_init_const (&str, valid_interfaces[i]);
376 if (!_dbus_validate_error_name (&str, 0,
377 _dbus_string_get_length (&str)))
379 _dbus_warn ("Error name \"%s\" should have been valid\n", valid_interfaces[i]);
380 _dbus_assert_not_reached ("invalid error name");
387 while (i < (int) _DBUS_N_ELEMENTS (invalid_interfaces))
389 if (invalid_interfaces[i][0] != ':')
391 _dbus_string_init_const (&str, invalid_interfaces[i]);
393 if (_dbus_validate_error_name (&str, 0,
394 _dbus_string_get_length (&str)))
396 _dbus_warn ("Error name \"%s\" should have been invalid\n", invalid_interfaces[i]);
397 _dbus_assert_not_reached ("valid error name");
404 /* Member validation */
406 while (i < (int) _DBUS_N_ELEMENTS (valid_members))
408 _dbus_string_init_const (&str, valid_members[i]);
410 if (!_dbus_validate_member (&str, 0,
411 _dbus_string_get_length (&str)))
413 _dbus_warn ("Member \"%s\" should have been valid\n", valid_members[i]);
414 _dbus_assert_not_reached ("invalid member");
421 while (i < (int) _DBUS_N_ELEMENTS (invalid_members))
423 _dbus_string_init_const (&str, invalid_members[i]);
425 if (_dbus_validate_member (&str, 0,
426 _dbus_string_get_length (&str)))
428 _dbus_warn ("Member \"%s\" should have been invalid\n", invalid_members[i]);
429 _dbus_assert_not_reached ("valid member");
435 /* Signature validation */
437 while (i < (int) _DBUS_N_ELEMENTS (valid_signatures))
439 _dbus_string_init_const (&str, valid_signatures[i]);
441 if (!_dbus_validate_signature (&str, 0,
442 _dbus_string_get_length (&str)))
444 _dbus_warn ("Signature \"%s\" should have been valid\n", valid_signatures[i]);
445 _dbus_assert_not_reached ("invalid signature");
452 while (i < (int) _DBUS_N_ELEMENTS (invalid_signatures))
454 _dbus_string_init_const (&str, invalid_signatures[i]);
456 if (_dbus_validate_signature (&str, 0,
457 _dbus_string_get_length (&str)))
459 _dbus_warn ("Signature \"%s\" should have been invalid\n", invalid_signatures[i]);
460 _dbus_assert_not_reached ("valid signature");
466 /* Validate claimed length longer than real length */
467 _dbus_string_init_const (&str, "abc.efg");
468 if (_dbus_validate_bus_name (&str, 0, 8))
469 _dbus_assert_not_reached ("validated too-long string");
470 if (_dbus_validate_interface (&str, 0, 8))
471 _dbus_assert_not_reached ("validated too-long string");
472 if (_dbus_validate_error_name (&str, 0, 8))
473 _dbus_assert_not_reached ("validated too-long string");
475 _dbus_string_init_const (&str, "abc");
476 if (_dbus_validate_member (&str, 0, 4))
477 _dbus_assert_not_reached ("validated too-long string");
479 _dbus_string_init_const (&str, "sss");
480 if (_dbus_validate_signature (&str, 0, 4))
481 _dbus_assert_not_reached ("validated too-long signature");
483 /* Validate string exceeding max name length */
484 if (!_dbus_string_init (&str))
485 _dbus_assert_not_reached ("no memory");
487 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
488 if (!_dbus_string_append (&str, "abc.def"))
489 _dbus_assert_not_reached ("no memory");
491 if (_dbus_validate_bus_name (&str, 0, _dbus_string_get_length (&str)))
492 _dbus_assert_not_reached ("validated overmax string");
493 if (_dbus_validate_interface (&str, 0, _dbus_string_get_length (&str)))
494 _dbus_assert_not_reached ("validated overmax string");
495 if (_dbus_validate_error_name (&str, 0, _dbus_string_get_length (&str)))
496 _dbus_assert_not_reached ("validated overmax string");
498 /* overlong member */
499 _dbus_string_set_length (&str, 0);
500 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
501 if (!_dbus_string_append (&str, "abc"))
502 _dbus_assert_not_reached ("no memory");
504 if (_dbus_validate_member (&str, 0, _dbus_string_get_length (&str)))
505 _dbus_assert_not_reached ("validated overmax string");
507 /* overlong unique name */
508 _dbus_string_set_length (&str, 0);
509 _dbus_string_append (&str, ":");
510 while (_dbus_string_get_length (&str) <= DBUS_MAXIMUM_NAME_LENGTH)
511 if (!_dbus_string_append (&str, "abc"))
512 _dbus_assert_not_reached ("no memory");
514 if (_dbus_validate_bus_name (&str, 0, _dbus_string_get_length (&str)))
515 _dbus_assert_not_reached ("validated overmax string");
517 _dbus_string_free (&str);
519 /* Body validation; test basic validation of valid bodies for both endian */
523 DBusString signature;
526 if (!_dbus_string_init (&signature) || !_dbus_string_init (&body))
527 _dbus_assert_not_reached ("oom");
530 while (dbus_internal_do_not_use_generate_bodies (sequence,
534 DBusValidity validity;
536 validity = _dbus_validate_body_with_reason (&signature, 0,
539 _dbus_string_get_length (&body));
540 if (validity != DBUS_VALID)
542 _dbus_warn ("invalid code %d expected valid on sequence %d little endian\n",
544 _dbus_verbose_bytes_of_string (&signature, 0, _dbus_string_get_length (&signature));
545 _dbus_verbose_bytes_of_string (&body, 0, _dbus_string_get_length (&body));
546 _dbus_assert_not_reached ("test failed");
549 _dbus_string_set_length (&signature, 0);
550 _dbus_string_set_length (&body, 0);
555 while (dbus_internal_do_not_use_generate_bodies (sequence,
559 DBusValidity validity;
561 validity = _dbus_validate_body_with_reason (&signature, 0,
564 _dbus_string_get_length (&body));
565 if (validity != DBUS_VALID)
567 _dbus_warn ("invalid code %d expected valid on sequence %d big endian\n",
569 _dbus_verbose_bytes_of_string (&signature, 0, _dbus_string_get_length (&signature));
570 _dbus_verbose_bytes_of_string (&body, 0, _dbus_string_get_length (&body));
571 _dbus_assert_not_reached ("test failed");
574 _dbus_string_set_length (&signature, 0);
575 _dbus_string_set_length (&body, 0);
579 _dbus_string_free (&signature);
580 _dbus_string_free (&body);
586 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
588 #endif /* DBUS_BUILD_TESTS */