1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-message-util.c Would be in dbus-message.c, but only used by bus/tests
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-internals.h"
27 #include "dbus-test.h"
28 #include "dbus-message-private.h"
29 #include "dbus-marshal-recursive.h"
30 #include "dbus-string.h"
31 #ifdef HAVE_UNIX_FD_PASSING
32 #include "dbus-sysdeps-unix.h"
36 /* Necessary for the Linux-specific fd leak checking code only */
37 #include <sys/types.h>
44 * @addtogroup DBusMessage
48 #ifdef DBUS_BUILD_TESTS
50 * Reads arguments from a message iterator given a variable argument
51 * list. Only arguments of basic type and arrays of fixed-length
52 * basic type may be read with this function. See
53 * dbus_message_get_args() for more details.
55 * @param iter the message iterator
56 * @param error error to be filled in on failure
57 * @param first_arg_type the first argument type
58 * @param ... location for first argument value, then list of type-location pairs
59 * @returns #FALSE if the error was set
62 dbus_message_iter_get_args (DBusMessageIter *iter,
70 _dbus_return_val_if_fail (iter != NULL, FALSE);
71 _dbus_return_val_if_error_is_set (error, FALSE);
73 va_start (var_args, first_arg_type);
74 retval = _dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
79 #endif /* DBUS_BUILD_TESTS */
83 #ifdef DBUS_BUILD_TESTS
84 #include "dbus-test.h"
85 #include "dbus-message-factory.h"
89 static int validities_seen[DBUS_VALIDITY_LAST + _DBUS_NEGATIVE_VALIDITY_COUNT];
92 reset_validities_seen (void)
96 while (i < _DBUS_N_ELEMENTS (validities_seen))
98 validities_seen[i] = 0;
104 record_validity_seen (DBusValidity validity)
106 validities_seen[validity + _DBUS_NEGATIVE_VALIDITY_COUNT] += 1;
110 print_validities_seen (dbus_bool_t not_seen)
114 while (i < _DBUS_N_ELEMENTS (validities_seen))
116 if ((i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_VALIDITY_UNKNOWN ||
117 (i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_INVALID_FOR_UNKNOWN_REASON)
119 else if ((not_seen && validities_seen[i] == 0) ||
120 (!not_seen && validities_seen[i] > 0))
121 printf ("validity %3d seen %d times\n",
122 i - _DBUS_NEGATIVE_VALIDITY_COUNT,
129 check_memleaks (void)
133 if (_dbus_get_malloc_blocks_outstanding () != 0)
135 _dbus_warn ("%d dbus_malloc blocks were not freed in %s\n",
136 _dbus_get_malloc_blocks_outstanding (), __FILE__);
137 _dbus_assert_not_reached ("memleaks");
142 _dbus_check_fdleaks(void)
149 /* This works on Linux only */
151 if ((d = opendir("/proc/self/fd")))
155 while ((de = readdir(d)))
161 if (de->d_name[0] == '.')
165 l = strtol(de->d_name, &e, 10);
166 _dbus_assert(errno == 0 && e && !*e);
176 _dbus_warn("file descriptor %i leaked in %s.\n", fd, __FILE__);
177 _dbus_assert_not_reached("fdleaks");
186 check_have_valid_message (DBusMessageLoader *loader)
188 DBusMessage *message;
194 if (_dbus_message_loader_get_is_corrupted (loader))
196 _dbus_warn ("loader corrupted on message that was expected to be valid; invalid reason %d\n",
197 loader->corruption_reason);
201 message = _dbus_message_loader_pop_message (loader);
204 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
208 if (_dbus_string_get_length (&loader->data) > 0)
210 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
216 /* Verify that we're able to properly deal with the message.
217 * For example, this would detect improper handling of messages
218 * in nonstandard byte order.
220 if (!check_message_handling (message))
224 record_validity_seen (DBUS_VALID);
230 dbus_message_unref (message);
236 check_invalid_message (DBusMessageLoader *loader,
237 DBusValidity expected_validity)
243 if (!_dbus_message_loader_get_is_corrupted (loader))
245 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
249 record_validity_seen (loader->corruption_reason);
251 if (expected_validity != DBUS_INVALID_FOR_UNKNOWN_REASON &&
252 loader->corruption_reason != expected_validity)
254 _dbus_warn ("expected message to be corrupted for reason %d and was corrupted for %d instead\n",
255 expected_validity, loader->corruption_reason);
266 check_incomplete_message (DBusMessageLoader *loader)
268 DBusMessage *message;
274 if (_dbus_message_loader_get_is_corrupted (loader))
276 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete), corruption reason %d\n",
277 loader->corruption_reason);
281 message = _dbus_message_loader_pop_message (loader);
284 _dbus_warn ("loaded message that was expected to be incomplete\n");
288 record_validity_seen (DBUS_VALID_BUT_INCOMPLETE);
293 dbus_message_unref (message);
298 check_loader_results (DBusMessageLoader *loader,
299 DBusValidity expected_validity)
301 if (!_dbus_message_loader_queue_messages (loader))
302 _dbus_assert_not_reached ("no memory to queue messages");
304 if (expected_validity == DBUS_VALID)
305 return check_have_valid_message (loader);
306 else if (expected_validity == DBUS_VALID_BUT_INCOMPLETE)
307 return check_incomplete_message (loader);
308 else if (expected_validity == DBUS_VALIDITY_UNKNOWN)
310 /* here we just know we didn't segfault and that was the
311 * only test. Also, we record that we got coverage
312 * for the validity reason.
314 if (_dbus_message_loader_get_is_corrupted (loader))
315 record_validity_seen (loader->corruption_reason);
320 return check_invalid_message (loader, expected_validity);
324 * Loads the message in the given message file.
326 * @param filename filename to load
327 * @param data string to load message into
328 * @returns #TRUE if the message was loaded
331 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
335 DBusError error = DBUS_ERROR_INIT;
339 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
340 if (!_dbus_file_get_contents (data, filename, &error))
342 _dbus_warn ("Could not load message file %s: %s\n",
343 _dbus_string_get_const_data (filename),
345 dbus_error_free (&error);
357 * Tries loading the message in the given message file
358 * and verifies that DBusMessageLoader can handle it.
360 * @param filename filename to load
361 * @param expected_validity what the message has to be like to return #TRUE
362 * @returns #TRUE if the message has the expected validity
365 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
366 DBusValidity expected_validity)
373 if (!_dbus_string_init (&data))
374 _dbus_assert_not_reached ("could not allocate string\n");
376 if (!dbus_internal_do_not_use_load_message_file (filename, &data))
379 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
385 if (_dbus_string_get_length (&data) > 0)
386 _dbus_verbose_bytes_of_string (&data, 0,
387 _dbus_string_get_length (&data));
389 _dbus_warn ("Failed message loader test on %s\n",
390 _dbus_string_get_const_data (filename));
393 _dbus_string_free (&data);
399 * Tries loading the given message data.
402 * @param data the message data
403 * @param expected_validity what the message has to be like to return #TRUE
404 * @returns #TRUE if the message has the expected validity
407 dbus_internal_do_not_use_try_message_data (const DBusString *data,
408 DBusValidity expected_validity)
410 DBusMessageLoader *loader;
418 /* Write the data one byte at a time */
420 loader = _dbus_message_loader_new ();
422 /* check some trivial loader functions */
423 _dbus_message_loader_ref (loader);
424 _dbus_message_loader_unref (loader);
425 _dbus_message_loader_get_max_message_size (loader);
427 len = _dbus_string_get_length (data);
428 for (i = 0; i < len; i++)
432 _dbus_message_loader_get_buffer (loader, &buffer);
433 _dbus_string_append_byte (buffer,
434 _dbus_string_get_byte (data, i));
435 _dbus_message_loader_return_buffer (loader, buffer, 1);
438 if (!check_loader_results (loader, expected_validity))
441 _dbus_message_loader_unref (loader);
444 /* Write the data all at once */
446 loader = _dbus_message_loader_new ();
451 _dbus_message_loader_get_buffer (loader, &buffer);
452 _dbus_string_copy (data, 0, buffer,
453 _dbus_string_get_length (buffer));
454 _dbus_message_loader_return_buffer (loader, buffer, 1);
457 if (!check_loader_results (loader, expected_validity))
460 _dbus_message_loader_unref (loader);
463 /* Write the data 2 bytes at a time */
465 loader = _dbus_message_loader_new ();
467 len = _dbus_string_get_length (data);
468 for (i = 0; i < len; i += 2)
472 _dbus_message_loader_get_buffer (loader, &buffer);
473 _dbus_string_append_byte (buffer,
474 _dbus_string_get_byte (data, i));
476 _dbus_string_append_byte (buffer,
477 _dbus_string_get_byte (data, i+1));
478 _dbus_message_loader_return_buffer (loader, buffer, 1);
481 if (!check_loader_results (loader, expected_validity))
484 _dbus_message_loader_unref (loader);
492 _dbus_message_loader_unref (loader);
498 process_test_subdir (const DBusString *test_base_dir,
500 DBusValidity expected_validity,
501 DBusForeachMessageFileFunc function,
504 DBusString test_directory;
508 DBusError error = DBUS_ERROR_INIT;
513 if (!_dbus_string_init (&test_directory))
514 _dbus_assert_not_reached ("didn't allocate test_directory\n");
516 _dbus_string_init_const (&filename, subdir);
518 if (!_dbus_string_copy (test_base_dir, 0,
520 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
522 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
523 _dbus_assert_not_reached ("couldn't allocate full path");
525 _dbus_string_free (&filename);
526 if (!_dbus_string_init (&filename))
527 _dbus_assert_not_reached ("didn't allocate filename string\n");
529 dir = _dbus_directory_open (&test_directory, &error);
532 _dbus_warn ("Could not open %s: %s\n",
533 _dbus_string_get_const_data (&test_directory),
535 dbus_error_free (&error);
539 printf ("Testing %s:\n", subdir);
542 while (_dbus_directory_get_next_file (dir, &filename, &error))
544 DBusString full_path;
546 if (!_dbus_string_init (&full_path))
547 _dbus_assert_not_reached ("couldn't init string");
549 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
550 _dbus_assert_not_reached ("couldn't copy dir to full_path");
552 if (!_dbus_concat_dir_and_file (&full_path, &filename))
553 _dbus_assert_not_reached ("couldn't concat file to dir");
555 if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
559 if (_dbus_string_ends_with_c_str (&filename, ".message"))
561 _dbus_warn ("Could not load %s, message builder language no longer supported\n",
562 _dbus_string_get_const_data (&filename));
565 _dbus_verbose ("Skipping non-.message file %s\n",
566 _dbus_string_get_const_data (&filename));
567 _dbus_string_free (&full_path);
572 _dbus_string_get_const_data (&filename));
574 if (! (*function) (&full_path,
575 expected_validity, user_data))
577 _dbus_string_free (&full_path);
581 _dbus_string_free (&full_path);
584 if (dbus_error_is_set (&error))
586 _dbus_warn ("Could not get next file in %s: %s\n",
587 _dbus_string_get_const_data (&test_directory),
589 dbus_error_free (&error);
598 _dbus_directory_close (dir);
599 _dbus_string_free (&test_directory);
600 _dbus_string_free (&filename);
606 * Runs the given function on every message file in the test suite.
607 * The function should return #FALSE on test failure or fatal error.
609 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
610 * @param func the function to run
611 * @param user_data data for function
612 * @returns #FALSE if there's a failure
615 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
616 DBusForeachMessageFileFunc func,
619 DBusString test_directory;
624 _dbus_string_init_const (&test_directory, test_data_dir);
626 if (!process_test_subdir (&test_directory, "valid-messages",
627 DBUS_VALID, func, user_data))
632 if (!process_test_subdir (&test_directory, "invalid-messages",
633 DBUS_INVALID_FOR_UNKNOWN_REASON, func, user_data))
638 if (!process_test_subdir (&test_directory, "incomplete-messages",
639 DBUS_VALID_BUT_INCOMPLETE, func, user_data))
648 _dbus_string_free (&test_directory);
654 #define GET_AND_CHECK(iter, typename, literal) \
656 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
657 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
658 dbus_message_iter_get_basic (&iter, &v_##typename); \
659 if (v_##typename != literal) \
660 _dbus_assert_not_reached ("got wrong value from message iter"); \
663 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
665 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
666 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
667 dbus_message_iter_get_basic (&iter, &v_##typename); \
668 if (strcmp (v_##typename, literal) != 0) \
669 _dbus_assert_not_reached ("got wrong value from message iter"); \
672 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
674 GET_AND_CHECK(iter, typename, literal); \
675 if (!dbus_message_iter_next (&iter)) \
676 _dbus_assert_not_reached ("failed to move iter to next"); \
679 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
681 GET_AND_CHECK_STRCMP(iter, typename, literal); \
682 if (!dbus_message_iter_next (&iter)) \
683 _dbus_assert_not_reached ("failed to move iter to next"); \
687 message_iter_test (DBusMessage *message)
689 DBusMessageIter iter, array, array2;
690 const char *v_STRING;
692 dbus_int16_t v_INT16;
693 dbus_uint16_t v_UINT16;
694 dbus_int32_t v_INT32;
695 dbus_uint32_t v_UINT32;
696 #ifdef DBUS_HAVE_INT64
697 dbus_int64_t v_INT64;
698 dbus_uint64_t v_UINT64;
700 unsigned char v_BYTE;
701 dbus_bool_t v_BOOLEAN;
703 const dbus_int32_t *our_int_array;
706 dbus_message_iter_init (message, &iter);
708 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
709 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
710 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
711 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
713 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
714 _dbus_assert_not_reached ("Argument type not an array");
716 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
717 _dbus_assert_not_reached ("Array type not double");
719 dbus_message_iter_recurse (&iter, &array);
721 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
722 GET_AND_CHECK (array, DOUBLE, 2.5);
724 if (dbus_message_iter_next (&array))
725 _dbus_assert_not_reached ("Didn't reach end of array");
727 if (!dbus_message_iter_next (&iter))
728 _dbus_assert_not_reached ("Reached end of arguments");
730 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
732 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
733 _dbus_assert_not_reached ("no array");
735 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
736 _dbus_assert_not_reached ("Array type not int32");
739 dbus_message_iter_recurse (&iter, &array);
741 if (dbus_message_iter_next (&array))
742 _dbus_assert_not_reached ("Didn't reach end of array");
744 if (!dbus_message_iter_next (&iter))
745 _dbus_assert_not_reached ("Reached end of arguments");
747 GET_AND_CHECK (iter, BYTE, 0xF0);
749 if (dbus_message_iter_next (&iter))
750 _dbus_assert_not_reached ("Didn't reach end of arguments");
755 verify_test_message (DBusMessage *message)
757 DBusMessageIter iter;
758 DBusError error = DBUS_ERROR_INIT;
759 dbus_int16_t our_int16;
760 dbus_uint16_t our_uint16;
761 dbus_int32_t our_int;
762 dbus_uint32_t our_uint;
766 dbus_bool_t our_bool;
767 unsigned char our_byte_1, our_byte_2;
768 const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
769 int our_uint32_array_len;
770 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
771 int our_int32_array_len;
772 #ifdef DBUS_HAVE_INT64
773 dbus_int64_t our_int64;
774 dbus_uint64_t our_uint64;
775 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
776 int our_uint64_array_len;
777 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
778 int our_int64_array_len;
780 const double *our_double_array = (void*)0xdeadbeef;
781 int our_double_array_len;
782 const unsigned char *our_byte_array = (void*)0xdeadbeef;
783 int our_byte_array_len;
784 const dbus_bool_t *our_boolean_array = (void*)0xdeadbeef;
785 int our_boolean_array_len;
786 char **our_string_array;
787 int our_string_array_len;
789 dbus_message_iter_init (message, &iter);
791 if (!dbus_message_iter_get_args (&iter, &error,
792 DBUS_TYPE_INT16, &our_int16,
793 DBUS_TYPE_UINT16, &our_uint16,
794 DBUS_TYPE_INT32, &our_int,
795 DBUS_TYPE_UINT32, &our_uint,
796 #ifdef DBUS_HAVE_INT64
797 DBUS_TYPE_INT64, &our_int64,
798 DBUS_TYPE_UINT64, &our_uint64,
800 DBUS_TYPE_STRING, &our_str,
801 DBUS_TYPE_DOUBLE, &our_double,
802 DBUS_TYPE_BOOLEAN, &our_bool,
803 DBUS_TYPE_BYTE, &our_byte_1,
804 DBUS_TYPE_BYTE, &our_byte_2,
805 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
806 &our_uint32_array, &our_uint32_array_len,
807 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
808 &our_int32_array, &our_int32_array_len,
809 #ifdef DBUS_HAVE_INT64
810 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
811 &our_uint64_array, &our_uint64_array_len,
812 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
813 &our_int64_array, &our_int64_array_len,
815 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
816 &our_double_array, &our_double_array_len,
817 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
818 &our_byte_array, &our_byte_array_len,
819 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
820 &our_boolean_array, &our_boolean_array_len,
821 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
822 &our_string_array, &our_string_array_len,
825 _dbus_warn ("error: %s - %s\n", error.name,
826 (error.message != NULL) ? error.message : "no message");
827 _dbus_assert_not_reached ("Could not get arguments");
830 if (our_int16 != -0x123)
831 _dbus_assert_not_reached ("16-bit integers differ!");
833 if (our_uint16 != 0x123)
834 _dbus_assert_not_reached ("16-bit uints differ!");
836 if (our_int != -0x12345678)
837 _dbus_assert_not_reached ("integers differ!");
839 if (our_uint != 0x12300042)
840 _dbus_assert_not_reached ("uints differ!");
842 #ifdef DBUS_HAVE_INT64
843 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
844 _dbus_assert_not_reached ("64-bit integers differ!");
845 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
846 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
850 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double, v_DOUBLE))
851 _dbus_assert_not_reached ("doubles differ!");
853 if (strcmp (our_str, "Test string") != 0)
854 _dbus_assert_not_reached ("strings differ!");
857 _dbus_assert_not_reached ("booleans differ");
859 if (our_byte_1 != 42)
860 _dbus_assert_not_reached ("bytes differ!");
862 if (our_byte_2 != 24)
863 _dbus_assert_not_reached ("bytes differ!");
865 if (our_uint32_array_len != 4 ||
866 our_uint32_array[0] != 0x12345678 ||
867 our_uint32_array[1] != 0x23456781 ||
868 our_uint32_array[2] != 0x34567812 ||
869 our_uint32_array[3] != 0x45678123)
870 _dbus_assert_not_reached ("uint array differs");
872 if (our_int32_array_len != 4 ||
873 our_int32_array[0] != 0x12345678 ||
874 our_int32_array[1] != -0x23456781 ||
875 our_int32_array[2] != 0x34567812 ||
876 our_int32_array[3] != -0x45678123)
877 _dbus_assert_not_reached ("int array differs");
879 #ifdef DBUS_HAVE_INT64
880 if (our_uint64_array_len != 4 ||
881 our_uint64_array[0] != 0x12345678 ||
882 our_uint64_array[1] != 0x23456781 ||
883 our_uint64_array[2] != 0x34567812 ||
884 our_uint64_array[3] != 0x45678123)
885 _dbus_assert_not_reached ("uint64 array differs");
887 if (our_int64_array_len != 4 ||
888 our_int64_array[0] != 0x12345678 ||
889 our_int64_array[1] != -0x23456781 ||
890 our_int64_array[2] != 0x34567812 ||
891 our_int64_array[3] != -0x45678123)
892 _dbus_assert_not_reached ("int64 array differs");
893 #endif /* DBUS_HAVE_INT64 */
895 if (our_double_array_len != 3)
896 _dbus_assert_not_reached ("double array had wrong length");
898 /* On all IEEE machines (i.e. everything sane) exact equality
899 * should be preserved over the wire
902 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[0], v_DOUBLE))
903 _dbus_assert_not_reached ("double array had wrong values");
904 v_DOUBLE = 9876.54321;
905 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[1], v_DOUBLE))
906 _dbus_assert_not_reached ("double array had wrong values");
908 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[2], v_DOUBLE))
909 _dbus_assert_not_reached ("double array had wrong values");
911 if (our_byte_array_len != 4)
912 _dbus_assert_not_reached ("byte array had wrong length");
914 if (our_byte_array[0] != 'a' ||
915 our_byte_array[1] != 'b' ||
916 our_byte_array[2] != 'c' ||
917 our_byte_array[3] != 234)
918 _dbus_assert_not_reached ("byte array had wrong values");
920 if (our_boolean_array_len != 5)
921 _dbus_assert_not_reached ("bool array had wrong length");
923 if (our_boolean_array[0] != TRUE ||
924 our_boolean_array[1] != FALSE ||
925 our_boolean_array[2] != TRUE ||
926 our_boolean_array[3] != TRUE ||
927 our_boolean_array[4] != FALSE)
928 _dbus_assert_not_reached ("bool array had wrong values");
930 if (our_string_array_len != 4)
931 _dbus_assert_not_reached ("string array was wrong length");
933 if (strcmp (our_string_array[0], "Foo") != 0 ||
934 strcmp (our_string_array[1], "bar") != 0 ||
935 strcmp (our_string_array[2], "") != 0 ||
936 strcmp (our_string_array[3], "woo woo woo woo") != 0)
937 _dbus_assert_not_reached ("string array had wrong values");
939 dbus_free_string_array (our_string_array);
941 if (dbus_message_iter_next (&iter))
942 _dbus_assert_not_reached ("Didn't reach end of arguments");
946 * @ingroup DBusMessageInternals
947 * Unit test for DBusMessage.
949 * @returns #TRUE on success.
952 _dbus_message_test (const char *test_data_dir)
954 DBusMessage *message, *message_without_unix_fds;
955 DBusMessageLoader *loader;
961 const dbus_uint32_t our_uint32_array[] =
962 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
963 const dbus_int32_t our_int32_array[] =
964 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
965 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
966 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
967 #ifdef DBUS_HAVE_INT64
968 const dbus_uint64_t our_uint64_array[] =
969 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
970 const dbus_int64_t our_int64_array[] =
971 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
972 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
973 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
975 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
976 const char **v_ARRAY_STRING = our_string_array;
977 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
978 const double *v_ARRAY_DOUBLE = our_double_array;
979 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
980 const unsigned char *v_ARRAY_BYTE = our_byte_array;
981 const dbus_bool_t our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
982 const dbus_bool_t *v_ARRAY_BOOLEAN = our_boolean_array;
985 const char *v_STRING;
987 dbus_int16_t v_INT16;
988 dbus_uint16_t v_UINT16;
989 dbus_int32_t v_INT32;
990 dbus_uint32_t v_UINT32;
991 #ifdef DBUS_HAVE_INT64
992 dbus_int64_t v_INT64;
993 dbus_uint64_t v_UINT64;
995 unsigned char v_BYTE;
996 unsigned char v2_BYTE;
997 dbus_bool_t v_BOOLEAN;
998 DBusMessageIter iter, array_iter, struct_iter;
999 #ifdef HAVE_UNIX_FD_PASSING
1004 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1005 "/org/freedesktop/TestPath",
1006 "Foo.TestInterface",
1008 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1009 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
1011 _dbus_assert (strcmp (dbus_message_get_path (message),
1012 "/org/freedesktop/TestPath") == 0);
1013 dbus_message_set_serial (message, 1234);
1015 /* string length including nul byte not a multiple of 4 */
1016 if (!dbus_message_set_sender (message, "org.foo.bar1"))
1017 _dbus_assert_not_reached ("out of memory");
1019 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
1020 dbus_message_set_reply_serial (message, 5678);
1022 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1023 _dbus_string_get_length (&message->header.data));
1024 _dbus_verbose_bytes_of_string (&message->body, 0,
1025 _dbus_string_get_length (&message->body));
1027 if (!dbus_message_set_sender (message, NULL))
1028 _dbus_assert_not_reached ("out of memory");
1031 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1032 _dbus_string_get_length (&message->header.data));
1033 _dbus_verbose_bytes_of_string (&message->body, 0,
1034 _dbus_string_get_length (&message->body));
1037 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
1038 _dbus_assert (dbus_message_get_serial (message) == 1234);
1039 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
1040 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1042 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1043 dbus_message_set_no_reply (message, TRUE);
1044 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
1045 dbus_message_set_no_reply (message, FALSE);
1046 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1048 /* Set/get some header fields */
1050 if (!dbus_message_set_path (message, "/foo"))
1051 _dbus_assert_not_reached ("out of memory");
1052 _dbus_assert (strcmp (dbus_message_get_path (message),
1055 if (!dbus_message_set_interface (message, "org.Foo"))
1056 _dbus_assert_not_reached ("out of memory");
1057 _dbus_assert (strcmp (dbus_message_get_interface (message),
1060 if (!dbus_message_set_member (message, "Bar"))
1061 _dbus_assert_not_reached ("out of memory");
1062 _dbus_assert (strcmp (dbus_message_get_member (message),
1065 /* Set/get them with longer values */
1066 if (!dbus_message_set_path (message, "/foo/bar"))
1067 _dbus_assert_not_reached ("out of memory");
1068 _dbus_assert (strcmp (dbus_message_get_path (message),
1071 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
1072 _dbus_assert_not_reached ("out of memory");
1073 _dbus_assert (strcmp (dbus_message_get_interface (message),
1074 "org.Foo.Bar") == 0);
1076 if (!dbus_message_set_member (message, "BarFoo"))
1077 _dbus_assert_not_reached ("out of memory");
1078 _dbus_assert (strcmp (dbus_message_get_member (message),
1081 /* Realloc shorter again */
1083 if (!dbus_message_set_path (message, "/foo"))
1084 _dbus_assert_not_reached ("out of memory");
1085 _dbus_assert (strcmp (dbus_message_get_path (message),
1088 if (!dbus_message_set_interface (message, "org.Foo"))
1089 _dbus_assert_not_reached ("out of memory");
1090 _dbus_assert (strcmp (dbus_message_get_interface (message),
1093 if (!dbus_message_set_member (message, "Bar"))
1094 _dbus_assert_not_reached ("out of memory");
1095 _dbus_assert (strcmp (dbus_message_get_member (message),
1098 /* Path decomposing */
1099 dbus_message_set_path (message, NULL);
1100 dbus_message_get_path_decomposed (message, &decomposed);
1101 _dbus_assert (decomposed == NULL);
1102 dbus_free_string_array (decomposed);
1104 dbus_message_set_path (message, "/");
1105 dbus_message_get_path_decomposed (message, &decomposed);
1106 _dbus_assert (decomposed != NULL);
1107 _dbus_assert (decomposed[0] == NULL);
1108 dbus_free_string_array (decomposed);
1110 dbus_message_set_path (message, "/a/b");
1111 dbus_message_get_path_decomposed (message, &decomposed);
1112 _dbus_assert (decomposed != NULL);
1113 _dbus_assert (strcmp (decomposed[0], "a") == 0);
1114 _dbus_assert (strcmp (decomposed[1], "b") == 0);
1115 _dbus_assert (decomposed[2] == NULL);
1116 dbus_free_string_array (decomposed);
1118 dbus_message_set_path (message, "/spam/eggs");
1119 dbus_message_get_path_decomposed (message, &decomposed);
1120 _dbus_assert (decomposed != NULL);
1121 _dbus_assert (strcmp (decomposed[0], "spam") == 0);
1122 _dbus_assert (strcmp (decomposed[1], "eggs") == 0);
1123 _dbus_assert (decomposed[2] == NULL);
1124 dbus_free_string_array (decomposed);
1126 dbus_message_unref (message);
1128 /* Test the vararg functions */
1129 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1130 "/org/freedesktop/TestPath",
1131 "Foo.TestInterface",
1133 dbus_message_set_serial (message, 1);
1134 dbus_message_set_reply_serial (message, 5678);
1138 v_INT32 = -0x12345678;
1139 v_UINT32 = 0x12300042;
1140 #ifdef DBUS_HAVE_INT64
1141 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
1142 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
1144 v_STRING = "Test string";
1149 #ifdef HAVE_UNIX_FD_PASSING
1153 dbus_message_append_args (message,
1154 DBUS_TYPE_INT16, &v_INT16,
1155 DBUS_TYPE_UINT16, &v_UINT16,
1156 DBUS_TYPE_INT32, &v_INT32,
1157 DBUS_TYPE_UINT32, &v_UINT32,
1158 #ifdef DBUS_HAVE_INT64
1159 DBUS_TYPE_INT64, &v_INT64,
1160 DBUS_TYPE_UINT64, &v_UINT64,
1162 DBUS_TYPE_STRING, &v_STRING,
1163 DBUS_TYPE_DOUBLE, &v_DOUBLE,
1164 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
1165 DBUS_TYPE_BYTE, &v_BYTE,
1166 DBUS_TYPE_BYTE, &v2_BYTE,
1167 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
1168 _DBUS_N_ELEMENTS (our_uint32_array),
1169 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
1170 _DBUS_N_ELEMENTS (our_int32_array),
1171 #ifdef DBUS_HAVE_INT64
1172 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
1173 _DBUS_N_ELEMENTS (our_uint64_array),
1174 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
1175 _DBUS_N_ELEMENTS (our_int64_array),
1177 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
1178 _DBUS_N_ELEMENTS (our_double_array),
1179 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
1180 _DBUS_N_ELEMENTS (our_byte_array),
1181 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
1182 _DBUS_N_ELEMENTS (our_boolean_array),
1183 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
1184 _DBUS_N_ELEMENTS (our_string_array),
1189 sig[i++] = DBUS_TYPE_INT16;
1190 sig[i++] = DBUS_TYPE_UINT16;
1191 sig[i++] = DBUS_TYPE_INT32;
1192 sig[i++] = DBUS_TYPE_UINT32;
1193 #ifdef DBUS_HAVE_INT64
1194 sig[i++] = DBUS_TYPE_INT64;
1195 sig[i++] = DBUS_TYPE_UINT64;
1197 sig[i++] = DBUS_TYPE_STRING;
1198 sig[i++] = DBUS_TYPE_DOUBLE;
1199 sig[i++] = DBUS_TYPE_BOOLEAN;
1200 sig[i++] = DBUS_TYPE_BYTE;
1201 sig[i++] = DBUS_TYPE_BYTE;
1202 sig[i++] = DBUS_TYPE_ARRAY;
1203 sig[i++] = DBUS_TYPE_UINT32;
1204 sig[i++] = DBUS_TYPE_ARRAY;
1205 sig[i++] = DBUS_TYPE_INT32;
1206 #ifdef DBUS_HAVE_INT64
1207 sig[i++] = DBUS_TYPE_ARRAY;
1208 sig[i++] = DBUS_TYPE_UINT64;
1209 sig[i++] = DBUS_TYPE_ARRAY;
1210 sig[i++] = DBUS_TYPE_INT64;
1212 sig[i++] = DBUS_TYPE_ARRAY;
1213 sig[i++] = DBUS_TYPE_DOUBLE;
1214 sig[i++] = DBUS_TYPE_ARRAY;
1215 sig[i++] = DBUS_TYPE_BYTE;
1216 sig[i++] = DBUS_TYPE_ARRAY;
1217 sig[i++] = DBUS_TYPE_BOOLEAN;
1218 sig[i++] = DBUS_TYPE_ARRAY;
1219 sig[i++] = DBUS_TYPE_STRING;
1221 message_without_unix_fds = dbus_message_copy(message);
1222 _dbus_assert(message_without_unix_fds);
1223 #ifdef HAVE_UNIX_FD_PASSING
1224 dbus_message_append_args (message,
1225 DBUS_TYPE_UNIX_FD, &v_UNIX_FD,
1227 sig[i++] = DBUS_TYPE_UNIX_FD;
1229 sig[i++] = DBUS_TYPE_INVALID;
1231 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
1233 _dbus_verbose ("HEADER\n");
1234 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1235 _dbus_string_get_length (&message->header.data));
1236 _dbus_verbose ("BODY\n");
1237 _dbus_verbose_bytes_of_string (&message->body, 0,
1238 _dbus_string_get_length (&message->body));
1240 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
1241 sig, dbus_message_get_signature (message));
1243 s = dbus_message_get_signature (message);
1245 _dbus_assert (dbus_message_has_signature (message, sig));
1246 _dbus_assert (strcmp (s, sig) == 0);
1248 verify_test_message (message);
1250 copy = dbus_message_copy (message);
1252 _dbus_assert (dbus_message_get_reply_serial (message) ==
1253 dbus_message_get_reply_serial (copy));
1254 _dbus_assert (message->header.padding == copy->header.padding);
1256 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
1257 _dbus_string_get_length (©->header.data));
1259 _dbus_assert (_dbus_string_get_length (&message->body) ==
1260 _dbus_string_get_length (©->body));
1262 verify_test_message (copy);
1264 name1 = dbus_message_get_interface (message);
1265 name2 = dbus_message_get_interface (copy);
1267 _dbus_assert (strcmp (name1, name2) == 0);
1269 name1 = dbus_message_get_member (message);
1270 name2 = dbus_message_get_member (copy);
1272 _dbus_assert (strcmp (name1, name2) == 0);
1274 dbus_message_unref (copy);
1276 /* Message loader test */
1277 dbus_message_lock (message);
1278 loader = _dbus_message_loader_new ();
1280 /* check ref/unref */
1281 _dbus_message_loader_ref (loader);
1282 _dbus_message_loader_unref (loader);
1284 /* Write the header data one byte at a time */
1285 data = _dbus_string_get_const_data (&message->header.data);
1286 for (i = 0; i < _dbus_string_get_length (&message->header.data); i++)
1290 _dbus_message_loader_get_buffer (loader, &buffer);
1291 _dbus_string_append_byte (buffer, data[i]);
1292 _dbus_message_loader_return_buffer (loader, buffer, 1);
1295 /* Write the body data one byte at a time */
1296 data = _dbus_string_get_const_data (&message->body);
1297 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
1301 _dbus_message_loader_get_buffer (loader, &buffer);
1302 _dbus_string_append_byte (buffer, data[i]);
1303 _dbus_message_loader_return_buffer (loader, buffer, 1);
1306 #ifdef HAVE_UNIX_FD_PASSING
1309 unsigned n_unix_fds;
1311 _dbus_message_loader_get_unix_fds(loader, &unix_fds, &n_unix_fds);
1312 _dbus_assert(n_unix_fds > 0);
1313 _dbus_assert(message->n_unix_fds == 1);
1314 unix_fds[0] = _dbus_dup(message->unix_fds[0], NULL);
1315 _dbus_assert(unix_fds[0] >= 0);
1316 _dbus_message_loader_return_unix_fds(loader, unix_fds, 1);
1320 dbus_message_unref (message);
1322 /* Now pop back the message */
1323 if (!_dbus_message_loader_queue_messages (loader))
1324 _dbus_assert_not_reached ("no memory to queue messages");
1326 if (_dbus_message_loader_get_is_corrupted (loader))
1327 _dbus_assert_not_reached ("message loader corrupted");
1329 message = _dbus_message_loader_pop_message (loader);
1331 _dbus_assert_not_reached ("received a NULL message");
1333 if (dbus_message_get_reply_serial (message) != 5678)
1334 _dbus_assert_not_reached ("reply serial fields differ");
1336 dbus_message_unref (message);
1338 /* ovveride the serial, since it was reset by dbus_message_copy() */
1339 dbus_message_set_serial(message_without_unix_fds, 8901);
1341 dbus_message_lock (message_without_unix_fds);
1343 verify_test_message (message_without_unix_fds);
1346 /* Marshal and demarshal the message. */
1348 DBusMessage *message2;
1349 DBusError error = DBUS_ERROR_INIT;
1350 char *marshalled = NULL;
1352 char garbage_header[DBUS_MINIMUM_HEADER_SIZE] = "xxx";
1354 if (!dbus_message_marshal (message_without_unix_fds, &marshalled, &len))
1355 _dbus_assert_not_reached ("failed to marshal message");
1357 _dbus_assert (len != 0);
1358 _dbus_assert (marshalled != NULL);
1360 _dbus_assert (dbus_message_demarshal_bytes_needed (marshalled, len) == len);
1361 message2 = dbus_message_demarshal (marshalled, len, &error);
1363 _dbus_assert (message2 != NULL);
1364 _dbus_assert (!dbus_error_is_set (&error));
1365 verify_test_message (message2);
1367 dbus_message_unref (message2);
1368 dbus_free (marshalled);
1370 /* Demarshal invalid message. */
1372 message2 = dbus_message_demarshal ("invalid", 7, &error);
1373 _dbus_assert (message2 == NULL);
1374 _dbus_assert (dbus_error_is_set (&error));
1375 dbus_error_free (&error);
1377 /* Demarshal invalid (empty) message. */
1379 message2 = dbus_message_demarshal ("", 0, &error);
1380 _dbus_assert (message2 == NULL);
1381 _dbus_assert (dbus_error_is_set (&error));
1382 dbus_error_free (&error);
1384 /* Bytes needed to demarshal empty message: 0 (more) */
1386 _dbus_assert (dbus_message_demarshal_bytes_needed ("", 0) == 0);
1388 /* Bytes needed to demarshal invalid message: -1 (error). */
1390 _dbus_assert (dbus_message_demarshal_bytes_needed (garbage_header, DBUS_MINIMUM_HEADER_SIZE) == -1);
1393 dbus_message_unref (message_without_unix_fds);
1394 _dbus_message_loader_unref (loader);
1397 _dbus_check_fdleaks();
1399 /* Check that we can abandon a container */
1400 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1401 "/org/freedesktop/TestPath",
1402 "Foo.TestInterface",
1405 dbus_message_iter_init_append (message, &iter);
1407 _dbus_assert (dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1408 (DBUS_STRUCT_BEGIN_CHAR_AS_STRING
1409 DBUS_TYPE_STRING_AS_STRING
1410 DBUS_TYPE_STRING_AS_STRING
1411 DBUS_STRUCT_END_CHAR_AS_STRING),
1413 _dbus_assert (dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT,
1414 NULL, &struct_iter));
1417 _dbus_assert (dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING,
1420 /* uh-oh, error, try and unwind */
1422 dbus_message_iter_abandon_container (&array_iter, &struct_iter);
1423 dbus_message_iter_abandon_container (&array_iter, &iter);
1425 dbus_message_unref (message);
1427 /* Load all the sample messages from the message factory */
1429 DBusMessageDataIter diter;
1430 DBusMessageData mdata;
1433 reset_validities_seen ();
1436 _dbus_message_data_iter_init (&diter);
1438 while (_dbus_message_data_iter_get_and_next (&diter,
1441 if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
1442 mdata.expected_validity))
1444 _dbus_warn ("expected validity %d and did not get it\n",
1445 mdata.expected_validity);
1446 _dbus_assert_not_reached ("message data failed");
1449 _dbus_message_data_free (&mdata);
1454 printf ("%d sample messages tested\n", count);
1456 print_validities_seen (FALSE);
1457 print_validities_seen (TRUE);
1461 _dbus_check_fdleaks();
1463 /* Now load every message in test_data_dir if we have one */
1464 if (test_data_dir == NULL)
1467 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
1468 (DBusForeachMessageFileFunc)
1469 dbus_internal_do_not_use_try_message_file,
1473 #endif /* DBUS_BUILD_TESTS */