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_ENABLE_EMBEDDED_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_ENABLE_EMBEDDED_TESTS */
83 #ifdef DBUS_ENABLE_EMBEDDED_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 struct DBusInitialFDs {
148 _dbus_check_fdleaks_enter (void)
154 /* this is plain malloc so it won't interfere with leak checking */
155 fds = malloc (sizeof (DBusInitialFDs));
156 _dbus_assert (fds != NULL);
158 /* This works on Linux only */
160 if ((d = opendir ("/proc/self/fd")))
164 while ((de = readdir(d)))
170 if (de->d_name[0] == '.')
174 l = strtol (de->d_name, &e, 10);
175 _dbus_assert (errno == 0 && e && !*e);
185 FD_SET (fd, &fds->set);
198 _dbus_check_fdleaks_leave (DBusInitialFDs *fds)
203 /* This works on Linux only */
205 if ((d = opendir ("/proc/self/fd")))
209 while ((de = readdir(d)))
215 if (de->d_name[0] == '.')
219 l = strtol (de->d_name, &e, 10);
220 _dbus_assert (errno == 0 && e && !*e);
230 if (FD_ISSET (fd, &fds->set))
233 _dbus_warn ("file descriptor %i leaked in %s.\n", fd, __FILE__);
234 _dbus_assert_not_reached ("fdleaks");
242 _dbus_assert (fds == NULL);
247 check_have_valid_message (DBusMessageLoader *loader)
249 DBusMessage *message;
255 if (_dbus_message_loader_get_is_corrupted (loader))
257 _dbus_warn ("loader corrupted on message that was expected to be valid; invalid reason %d\n",
258 loader->corruption_reason);
262 message = _dbus_message_loader_pop_message (loader);
265 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
269 if (_dbus_string_get_length (&loader->data) > 0)
271 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
277 /* Verify that we're able to properly deal with the message.
278 * For example, this would detect improper handling of messages
279 * in nonstandard byte order.
281 if (!check_message_handling (message))
285 record_validity_seen (DBUS_VALID);
291 dbus_message_unref (message);
297 check_invalid_message (DBusMessageLoader *loader,
298 DBusValidity expected_validity)
304 if (!_dbus_message_loader_get_is_corrupted (loader))
306 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
310 record_validity_seen (loader->corruption_reason);
312 if (expected_validity != DBUS_INVALID_FOR_UNKNOWN_REASON &&
313 loader->corruption_reason != expected_validity)
315 _dbus_warn ("expected message to be corrupted for reason %d and was corrupted for %d instead\n",
316 expected_validity, loader->corruption_reason);
327 check_incomplete_message (DBusMessageLoader *loader)
329 DBusMessage *message;
335 if (_dbus_message_loader_get_is_corrupted (loader))
337 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete), corruption reason %d\n",
338 loader->corruption_reason);
342 message = _dbus_message_loader_pop_message (loader);
345 _dbus_warn ("loaded message that was expected to be incomplete\n");
349 record_validity_seen (DBUS_VALID_BUT_INCOMPLETE);
354 dbus_message_unref (message);
359 check_loader_results (DBusMessageLoader *loader,
360 DBusValidity expected_validity)
362 if (!_dbus_message_loader_queue_messages (loader))
363 _dbus_assert_not_reached ("no memory to queue messages");
365 if (expected_validity == DBUS_VALID)
366 return check_have_valid_message (loader);
367 else if (expected_validity == DBUS_VALID_BUT_INCOMPLETE)
368 return check_incomplete_message (loader);
369 else if (expected_validity == DBUS_VALIDITY_UNKNOWN)
371 /* here we just know we didn't segfault and that was the
372 * only test. Also, we record that we got coverage
373 * for the validity reason.
375 if (_dbus_message_loader_get_is_corrupted (loader))
376 record_validity_seen (loader->corruption_reason);
381 return check_invalid_message (loader, expected_validity);
385 * Loads the message in the given message file.
387 * @param filename filename to load
388 * @param data string to load message into
389 * @returns #TRUE if the message was loaded
392 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
396 DBusError error = DBUS_ERROR_INIT;
400 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
401 if (!_dbus_file_get_contents (data, filename, &error))
403 _dbus_warn ("Could not load message file %s: %s\n",
404 _dbus_string_get_const_data (filename),
406 dbus_error_free (&error);
418 * Tries loading the message in the given message file
419 * and verifies that DBusMessageLoader can handle it.
421 * @param filename filename to load
422 * @param expected_validity what the message has to be like to return #TRUE
423 * @returns #TRUE if the message has the expected validity
426 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
427 DBusValidity expected_validity)
434 if (!_dbus_string_init (&data))
435 _dbus_assert_not_reached ("could not allocate string\n");
437 if (!dbus_internal_do_not_use_load_message_file (filename, &data))
440 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
446 if (_dbus_string_get_length (&data) > 0)
447 _dbus_verbose_bytes_of_string (&data, 0,
448 _dbus_string_get_length (&data));
450 _dbus_warn ("Failed message loader test on %s\n",
451 _dbus_string_get_const_data (filename));
454 _dbus_string_free (&data);
460 * Tries loading the given message data.
463 * @param data the message data
464 * @param expected_validity what the message has to be like to return #TRUE
465 * @returns #TRUE if the message has the expected validity
468 dbus_internal_do_not_use_try_message_data (const DBusString *data,
469 DBusValidity expected_validity)
471 DBusMessageLoader *loader;
479 /* Write the data one byte at a time */
481 loader = _dbus_message_loader_new ();
483 /* check some trivial loader functions */
484 _dbus_message_loader_ref (loader);
485 _dbus_message_loader_unref (loader);
486 _dbus_message_loader_get_max_message_size (loader);
488 len = _dbus_string_get_length (data);
489 for (i = 0; i < len; i++)
493 _dbus_message_loader_get_buffer (loader, &buffer);
494 _dbus_string_append_byte (buffer,
495 _dbus_string_get_byte (data, i));
496 _dbus_message_loader_return_buffer (loader, buffer, 1);
499 if (!check_loader_results (loader, expected_validity))
502 _dbus_message_loader_unref (loader);
505 /* Write the data all at once */
507 loader = _dbus_message_loader_new ();
512 _dbus_message_loader_get_buffer (loader, &buffer);
513 _dbus_string_copy (data, 0, buffer,
514 _dbus_string_get_length (buffer));
515 _dbus_message_loader_return_buffer (loader, buffer, 1);
518 if (!check_loader_results (loader, expected_validity))
521 _dbus_message_loader_unref (loader);
524 /* Write the data 2 bytes at a time */
526 loader = _dbus_message_loader_new ();
528 len = _dbus_string_get_length (data);
529 for (i = 0; i < len; i += 2)
533 _dbus_message_loader_get_buffer (loader, &buffer);
534 _dbus_string_append_byte (buffer,
535 _dbus_string_get_byte (data, i));
537 _dbus_string_append_byte (buffer,
538 _dbus_string_get_byte (data, i+1));
539 _dbus_message_loader_return_buffer (loader, buffer, 1);
542 if (!check_loader_results (loader, expected_validity))
545 _dbus_message_loader_unref (loader);
553 _dbus_message_loader_unref (loader);
559 process_test_subdir (const DBusString *test_base_dir,
561 DBusValidity expected_validity,
562 DBusForeachMessageFileFunc function,
565 DBusString test_directory;
569 DBusError error = DBUS_ERROR_INIT;
574 if (!_dbus_string_init (&test_directory))
575 _dbus_assert_not_reached ("didn't allocate test_directory\n");
577 _dbus_string_init_const (&filename, subdir);
579 if (!_dbus_string_copy (test_base_dir, 0,
581 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
583 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
584 _dbus_assert_not_reached ("couldn't allocate full path");
586 _dbus_string_free (&filename);
587 if (!_dbus_string_init (&filename))
588 _dbus_assert_not_reached ("didn't allocate filename string\n");
590 dir = _dbus_directory_open (&test_directory, &error);
593 _dbus_warn ("Could not open %s: %s\n",
594 _dbus_string_get_const_data (&test_directory),
596 dbus_error_free (&error);
600 printf ("Testing %s:\n", subdir);
603 while (_dbus_directory_get_next_file (dir, &filename, &error))
605 DBusString full_path;
607 if (!_dbus_string_init (&full_path))
608 _dbus_assert_not_reached ("couldn't init string");
610 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
611 _dbus_assert_not_reached ("couldn't copy dir to full_path");
613 if (!_dbus_concat_dir_and_file (&full_path, &filename))
614 _dbus_assert_not_reached ("couldn't concat file to dir");
616 if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
620 if (_dbus_string_ends_with_c_str (&filename, ".message"))
622 printf ("SKIP: Could not load %s, message builder language no longer supported\n",
623 _dbus_string_get_const_data (&filename));
626 _dbus_verbose ("Skipping non-.message file %s\n",
627 _dbus_string_get_const_data (&filename));
628 _dbus_string_free (&full_path);
633 _dbus_string_get_const_data (&filename));
635 if (! (*function) (&full_path,
636 expected_validity, user_data))
638 _dbus_string_free (&full_path);
642 _dbus_string_free (&full_path);
645 if (dbus_error_is_set (&error))
647 _dbus_warn ("Could not get next file in %s: %s\n",
648 _dbus_string_get_const_data (&test_directory),
650 dbus_error_free (&error);
659 _dbus_directory_close (dir);
660 _dbus_string_free (&test_directory);
661 _dbus_string_free (&filename);
667 * Runs the given function on every message file in the test suite.
668 * The function should return #FALSE on test failure or fatal error.
670 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
671 * @param func the function to run
672 * @param user_data data for function
673 * @returns #FALSE if there's a failure
676 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
677 DBusForeachMessageFileFunc func,
680 DBusString test_directory;
685 _dbus_string_init_const (&test_directory, test_data_dir);
687 if (!process_test_subdir (&test_directory, "valid-messages",
688 DBUS_VALID, func, user_data))
693 if (!process_test_subdir (&test_directory, "invalid-messages",
694 DBUS_INVALID_FOR_UNKNOWN_REASON, func, user_data))
699 if (!process_test_subdir (&test_directory, "incomplete-messages",
700 DBUS_VALID_BUT_INCOMPLETE, func, user_data))
709 _dbus_string_free (&test_directory);
715 #define GET_AND_CHECK(iter, typename, literal) \
717 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
718 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
719 dbus_message_iter_get_basic (&iter, &v_##typename); \
720 if (v_##typename != literal) \
721 _dbus_assert_not_reached ("got wrong value from message iter"); \
724 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
726 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
727 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
728 dbus_message_iter_get_basic (&iter, &v_##typename); \
729 if (strcmp (v_##typename, literal) != 0) \
730 _dbus_assert_not_reached ("got wrong value from message iter"); \
733 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
735 GET_AND_CHECK(iter, typename, literal); \
736 if (!dbus_message_iter_next (&iter)) \
737 _dbus_assert_not_reached ("failed to move iter to next"); \
740 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
742 GET_AND_CHECK_STRCMP(iter, typename, literal); \
743 if (!dbus_message_iter_next (&iter)) \
744 _dbus_assert_not_reached ("failed to move iter to next"); \
748 message_iter_test (DBusMessage *message)
750 DBusMessageIter iter, array, array2;
751 const char *v_STRING;
753 dbus_int16_t v_INT16;
754 dbus_uint16_t v_UINT16;
755 dbus_int32_t v_INT32;
756 dbus_uint32_t v_UINT32;
757 dbus_int64_t v_INT64;
758 dbus_uint64_t v_UINT64;
759 unsigned char v_BYTE;
760 dbus_bool_t v_BOOLEAN;
762 const dbus_int32_t *our_int_array;
765 dbus_message_iter_init (message, &iter);
767 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
768 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
769 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
770 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
772 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
773 _dbus_assert_not_reached ("Argument type not an array");
775 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
776 _dbus_assert_not_reached ("Array type not double");
778 dbus_message_iter_recurse (&iter, &array);
780 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
781 GET_AND_CHECK (array, DOUBLE, 2.5);
783 if (dbus_message_iter_next (&array))
784 _dbus_assert_not_reached ("Didn't reach end of array");
786 if (!dbus_message_iter_next (&iter))
787 _dbus_assert_not_reached ("Reached end of arguments");
789 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
791 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
792 _dbus_assert_not_reached ("no array");
794 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
795 _dbus_assert_not_reached ("Array type not int32");
798 dbus_message_iter_recurse (&iter, &array);
800 if (dbus_message_iter_next (&array))
801 _dbus_assert_not_reached ("Didn't reach end of array");
803 if (!dbus_message_iter_next (&iter))
804 _dbus_assert_not_reached ("Reached end of arguments");
806 GET_AND_CHECK (iter, BYTE, 0xF0);
808 if (dbus_message_iter_next (&iter))
809 _dbus_assert_not_reached ("Didn't reach end of arguments");
814 verify_test_message (DBusMessage *message)
816 DBusMessageIter iter;
817 DBusError error = DBUS_ERROR_INIT;
818 dbus_int16_t our_int16;
819 dbus_uint16_t our_uint16;
820 dbus_int32_t our_int;
821 dbus_uint32_t our_uint;
825 dbus_bool_t our_bool;
826 unsigned char our_byte_1, our_byte_2;
827 const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
828 int our_uint32_array_len;
829 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
830 int our_int32_array_len;
831 dbus_int64_t our_int64;
832 dbus_uint64_t our_uint64;
833 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
834 int our_uint64_array_len;
835 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
836 int our_int64_array_len;
837 const double *our_double_array = (void*)0xdeadbeef;
838 int our_double_array_len;
839 const unsigned char *our_byte_array = (void*)0xdeadbeef;
840 int our_byte_array_len;
841 const dbus_bool_t *our_boolean_array = (void*)0xdeadbeef;
842 int our_boolean_array_len;
843 char **our_string_array;
844 int our_string_array_len;
846 dbus_message_iter_init (message, &iter);
848 if (!dbus_message_iter_get_args (&iter, &error,
849 DBUS_TYPE_INT16, &our_int16,
850 DBUS_TYPE_UINT16, &our_uint16,
851 DBUS_TYPE_INT32, &our_int,
852 DBUS_TYPE_UINT32, &our_uint,
853 DBUS_TYPE_INT64, &our_int64,
854 DBUS_TYPE_UINT64, &our_uint64,
855 DBUS_TYPE_STRING, &our_str,
856 DBUS_TYPE_DOUBLE, &our_double,
857 DBUS_TYPE_BOOLEAN, &our_bool,
858 DBUS_TYPE_BYTE, &our_byte_1,
859 DBUS_TYPE_BYTE, &our_byte_2,
860 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
861 &our_uint32_array, &our_uint32_array_len,
862 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
863 &our_int32_array, &our_int32_array_len,
864 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
865 &our_uint64_array, &our_uint64_array_len,
866 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
867 &our_int64_array, &our_int64_array_len,
868 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
869 &our_double_array, &our_double_array_len,
870 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
871 &our_byte_array, &our_byte_array_len,
872 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
873 &our_boolean_array, &our_boolean_array_len,
874 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
875 &our_string_array, &our_string_array_len,
878 _dbus_warn ("error: %s - %s\n", error.name,
879 (error.message != NULL) ? error.message : "no message");
880 _dbus_assert_not_reached ("Could not get arguments");
883 if (our_int16 != -0x123)
884 _dbus_assert_not_reached ("16-bit integers differ!");
886 if (our_uint16 != 0x123)
887 _dbus_assert_not_reached ("16-bit uints differ!");
889 if (our_int != -0x12345678)
890 _dbus_assert_not_reached ("integers differ!");
892 if (our_uint != 0x12300042)
893 _dbus_assert_not_reached ("uints differ!");
895 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
896 _dbus_assert_not_reached ("64-bit integers differ!");
897 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
898 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
901 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double, v_DOUBLE))
902 _dbus_assert_not_reached ("doubles differ!");
904 if (strcmp (our_str, "Test string") != 0)
905 _dbus_assert_not_reached ("strings differ!");
908 _dbus_assert_not_reached ("booleans differ");
910 if (our_byte_1 != 42)
911 _dbus_assert_not_reached ("bytes differ!");
913 if (our_byte_2 != 24)
914 _dbus_assert_not_reached ("bytes differ!");
916 if (our_uint32_array_len != 4 ||
917 our_uint32_array[0] != 0x12345678 ||
918 our_uint32_array[1] != 0x23456781 ||
919 our_uint32_array[2] != 0x34567812 ||
920 our_uint32_array[3] != 0x45678123)
921 _dbus_assert_not_reached ("uint array differs");
923 if (our_int32_array_len != 4 ||
924 our_int32_array[0] != 0x12345678 ||
925 our_int32_array[1] != -0x23456781 ||
926 our_int32_array[2] != 0x34567812 ||
927 our_int32_array[3] != -0x45678123)
928 _dbus_assert_not_reached ("int array differs");
930 if (our_uint64_array_len != 4 ||
931 our_uint64_array[0] != 0x12345678 ||
932 our_uint64_array[1] != 0x23456781 ||
933 our_uint64_array[2] != 0x34567812 ||
934 our_uint64_array[3] != 0x45678123)
935 _dbus_assert_not_reached ("uint64 array differs");
937 if (our_int64_array_len != 4 ||
938 our_int64_array[0] != 0x12345678 ||
939 our_int64_array[1] != -0x23456781 ||
940 our_int64_array[2] != 0x34567812 ||
941 our_int64_array[3] != -0x45678123)
942 _dbus_assert_not_reached ("int64 array differs");
944 if (our_double_array_len != 3)
945 _dbus_assert_not_reached ("double array had wrong length");
947 /* On all IEEE machines (i.e. everything sane) exact equality
948 * should be preserved over the wire
951 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[0], v_DOUBLE))
952 _dbus_assert_not_reached ("double array had wrong values");
953 v_DOUBLE = 9876.54321;
954 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[1], v_DOUBLE))
955 _dbus_assert_not_reached ("double array had wrong values");
957 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[2], v_DOUBLE))
958 _dbus_assert_not_reached ("double array had wrong values");
960 if (our_byte_array_len != 4)
961 _dbus_assert_not_reached ("byte array had wrong length");
963 if (our_byte_array[0] != 'a' ||
964 our_byte_array[1] != 'b' ||
965 our_byte_array[2] != 'c' ||
966 our_byte_array[3] != 234)
967 _dbus_assert_not_reached ("byte array had wrong values");
969 if (our_boolean_array_len != 5)
970 _dbus_assert_not_reached ("bool array had wrong length");
972 if (our_boolean_array[0] != TRUE ||
973 our_boolean_array[1] != FALSE ||
974 our_boolean_array[2] != TRUE ||
975 our_boolean_array[3] != TRUE ||
976 our_boolean_array[4] != FALSE)
977 _dbus_assert_not_reached ("bool array had wrong values");
979 if (our_string_array_len != 4)
980 _dbus_assert_not_reached ("string array was wrong length");
982 if (strcmp (our_string_array[0], "Foo") != 0 ||
983 strcmp (our_string_array[1], "bar") != 0 ||
984 strcmp (our_string_array[2], "") != 0 ||
985 strcmp (our_string_array[3], "woo woo woo woo") != 0)
986 _dbus_assert_not_reached ("string array had wrong values");
988 dbus_free_string_array (our_string_array);
990 if (dbus_message_iter_next (&iter))
991 _dbus_assert_not_reached ("Didn't reach end of arguments");
995 * @ingroup DBusMessageInternals
996 * Unit test for DBusMessage.
998 * @returns #TRUE on success.
1001 _dbus_message_test (const char *test_data_dir)
1003 DBusMessage *message, *message_without_unix_fds;
1004 DBusMessageLoader *loader;
1010 const dbus_uint32_t our_uint32_array[] =
1011 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
1012 const dbus_int32_t our_int32_array[] =
1013 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
1014 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
1015 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
1016 const dbus_uint64_t our_uint64_array[] =
1017 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
1018 const dbus_int64_t our_int64_array[] =
1019 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
1020 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
1021 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
1022 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
1023 const char **v_ARRAY_STRING = our_string_array;
1024 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
1025 const double *v_ARRAY_DOUBLE = our_double_array;
1026 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
1027 const unsigned char *v_ARRAY_BYTE = our_byte_array;
1028 const dbus_bool_t our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
1029 const dbus_bool_t *v_ARRAY_BOOLEAN = our_boolean_array;
1032 const char *v_STRING;
1034 dbus_int16_t v_INT16;
1035 dbus_uint16_t v_UINT16;
1036 dbus_int32_t v_INT32;
1037 dbus_uint32_t v_UINT32;
1038 dbus_int64_t v_INT64;
1039 dbus_uint64_t v_UINT64;
1040 unsigned char v_BYTE;
1041 unsigned char v2_BYTE;
1042 dbus_bool_t v_BOOLEAN;
1043 DBusMessageIter iter, array_iter, struct_iter;
1044 #ifdef HAVE_UNIX_FD_PASSING
1048 DBusInitialFDs *initial_fds;
1050 initial_fds = _dbus_check_fdleaks_enter ();
1052 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1053 "/org/freedesktop/TestPath",
1054 "Foo.TestInterface",
1056 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1057 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
1059 _dbus_assert (strcmp (dbus_message_get_path (message),
1060 "/org/freedesktop/TestPath") == 0);
1061 dbus_message_set_serial (message, 1234);
1063 /* string length including nul byte not a multiple of 4 */
1064 if (!dbus_message_set_sender (message, "org.foo.bar1"))
1065 _dbus_assert_not_reached ("out of memory");
1067 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
1068 dbus_message_set_reply_serial (message, 5678);
1070 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1071 _dbus_string_get_length (&message->header.data));
1072 _dbus_verbose_bytes_of_string (&message->body, 0,
1073 _dbus_string_get_length (&message->body));
1075 if (!dbus_message_set_sender (message, NULL))
1076 _dbus_assert_not_reached ("out of memory");
1079 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1080 _dbus_string_get_length (&message->header.data));
1081 _dbus_verbose_bytes_of_string (&message->body, 0,
1082 _dbus_string_get_length (&message->body));
1085 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
1086 _dbus_assert (dbus_message_get_serial (message) == 1234);
1087 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
1088 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1090 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1091 dbus_message_set_no_reply (message, TRUE);
1092 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
1093 dbus_message_set_no_reply (message, FALSE);
1094 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1096 /* Set/get some header fields */
1098 if (!dbus_message_set_path (message, "/foo"))
1099 _dbus_assert_not_reached ("out of memory");
1100 _dbus_assert (strcmp (dbus_message_get_path (message),
1103 if (!dbus_message_set_interface (message, "org.Foo"))
1104 _dbus_assert_not_reached ("out of memory");
1105 _dbus_assert (strcmp (dbus_message_get_interface (message),
1108 if (!dbus_message_set_member (message, "Bar"))
1109 _dbus_assert_not_reached ("out of memory");
1110 _dbus_assert (strcmp (dbus_message_get_member (message),
1113 /* Set/get them with longer values */
1114 if (!dbus_message_set_path (message, "/foo/bar"))
1115 _dbus_assert_not_reached ("out of memory");
1116 _dbus_assert (strcmp (dbus_message_get_path (message),
1119 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
1120 _dbus_assert_not_reached ("out of memory");
1121 _dbus_assert (strcmp (dbus_message_get_interface (message),
1122 "org.Foo.Bar") == 0);
1124 if (!dbus_message_set_member (message, "BarFoo"))
1125 _dbus_assert_not_reached ("out of memory");
1126 _dbus_assert (strcmp (dbus_message_get_member (message),
1129 /* Realloc shorter again */
1131 if (!dbus_message_set_path (message, "/foo"))
1132 _dbus_assert_not_reached ("out of memory");
1133 _dbus_assert (strcmp (dbus_message_get_path (message),
1136 if (!dbus_message_set_interface (message, "org.Foo"))
1137 _dbus_assert_not_reached ("out of memory");
1138 _dbus_assert (strcmp (dbus_message_get_interface (message),
1141 if (!dbus_message_set_member (message, "Bar"))
1142 _dbus_assert_not_reached ("out of memory");
1143 _dbus_assert (strcmp (dbus_message_get_member (message),
1146 /* Path decomposing */
1147 dbus_message_set_path (message, NULL);
1148 dbus_message_get_path_decomposed (message, &decomposed);
1149 _dbus_assert (decomposed == NULL);
1150 dbus_free_string_array (decomposed);
1152 dbus_message_set_path (message, "/");
1153 dbus_message_get_path_decomposed (message, &decomposed);
1154 _dbus_assert (decomposed != NULL);
1155 _dbus_assert (decomposed[0] == NULL);
1156 dbus_free_string_array (decomposed);
1158 dbus_message_set_path (message, "/a/b");
1159 dbus_message_get_path_decomposed (message, &decomposed);
1160 _dbus_assert (decomposed != NULL);
1161 _dbus_assert (strcmp (decomposed[0], "a") == 0);
1162 _dbus_assert (strcmp (decomposed[1], "b") == 0);
1163 _dbus_assert (decomposed[2] == NULL);
1164 dbus_free_string_array (decomposed);
1166 dbus_message_set_path (message, "/spam/eggs");
1167 dbus_message_get_path_decomposed (message, &decomposed);
1168 _dbus_assert (decomposed != NULL);
1169 _dbus_assert (strcmp (decomposed[0], "spam") == 0);
1170 _dbus_assert (strcmp (decomposed[1], "eggs") == 0);
1171 _dbus_assert (decomposed[2] == NULL);
1172 dbus_free_string_array (decomposed);
1174 dbus_message_unref (message);
1176 /* Test the vararg functions */
1177 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1178 "/org/freedesktop/TestPath",
1179 "Foo.TestInterface",
1181 dbus_message_set_serial (message, 1);
1182 dbus_message_set_reply_serial (message, 5678);
1186 v_INT32 = -0x12345678;
1187 v_UINT32 = 0x12300042;
1188 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
1189 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
1190 v_STRING = "Test string";
1195 #ifdef HAVE_UNIX_FD_PASSING
1199 dbus_message_append_args (message,
1200 DBUS_TYPE_INT16, &v_INT16,
1201 DBUS_TYPE_UINT16, &v_UINT16,
1202 DBUS_TYPE_INT32, &v_INT32,
1203 DBUS_TYPE_UINT32, &v_UINT32,
1204 DBUS_TYPE_INT64, &v_INT64,
1205 DBUS_TYPE_UINT64, &v_UINT64,
1206 DBUS_TYPE_STRING, &v_STRING,
1207 DBUS_TYPE_DOUBLE, &v_DOUBLE,
1208 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
1209 DBUS_TYPE_BYTE, &v_BYTE,
1210 DBUS_TYPE_BYTE, &v2_BYTE,
1211 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
1212 _DBUS_N_ELEMENTS (our_uint32_array),
1213 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
1214 _DBUS_N_ELEMENTS (our_int32_array),
1215 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
1216 _DBUS_N_ELEMENTS (our_uint64_array),
1217 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
1218 _DBUS_N_ELEMENTS (our_int64_array),
1219 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
1220 _DBUS_N_ELEMENTS (our_double_array),
1221 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
1222 _DBUS_N_ELEMENTS (our_byte_array),
1223 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
1224 _DBUS_N_ELEMENTS (our_boolean_array),
1225 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
1226 _DBUS_N_ELEMENTS (our_string_array),
1231 sig[i++] = DBUS_TYPE_INT16;
1232 sig[i++] = DBUS_TYPE_UINT16;
1233 sig[i++] = DBUS_TYPE_INT32;
1234 sig[i++] = DBUS_TYPE_UINT32;
1235 sig[i++] = DBUS_TYPE_INT64;
1236 sig[i++] = DBUS_TYPE_UINT64;
1237 sig[i++] = DBUS_TYPE_STRING;
1238 sig[i++] = DBUS_TYPE_DOUBLE;
1239 sig[i++] = DBUS_TYPE_BOOLEAN;
1240 sig[i++] = DBUS_TYPE_BYTE;
1241 sig[i++] = DBUS_TYPE_BYTE;
1242 sig[i++] = DBUS_TYPE_ARRAY;
1243 sig[i++] = DBUS_TYPE_UINT32;
1244 sig[i++] = DBUS_TYPE_ARRAY;
1245 sig[i++] = DBUS_TYPE_INT32;
1246 sig[i++] = DBUS_TYPE_ARRAY;
1247 sig[i++] = DBUS_TYPE_UINT64;
1248 sig[i++] = DBUS_TYPE_ARRAY;
1249 sig[i++] = DBUS_TYPE_INT64;
1250 sig[i++] = DBUS_TYPE_ARRAY;
1251 sig[i++] = DBUS_TYPE_DOUBLE;
1252 sig[i++] = DBUS_TYPE_ARRAY;
1253 sig[i++] = DBUS_TYPE_BYTE;
1254 sig[i++] = DBUS_TYPE_ARRAY;
1255 sig[i++] = DBUS_TYPE_BOOLEAN;
1256 sig[i++] = DBUS_TYPE_ARRAY;
1257 sig[i++] = DBUS_TYPE_STRING;
1259 message_without_unix_fds = dbus_message_copy(message);
1260 _dbus_assert(message_without_unix_fds);
1261 #ifdef HAVE_UNIX_FD_PASSING
1262 dbus_message_append_args (message,
1263 DBUS_TYPE_UNIX_FD, &v_UNIX_FD,
1265 sig[i++] = DBUS_TYPE_UNIX_FD;
1267 sig[i++] = DBUS_TYPE_INVALID;
1269 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
1271 _dbus_verbose ("HEADER\n");
1272 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1273 _dbus_string_get_length (&message->header.data));
1274 _dbus_verbose ("BODY\n");
1275 _dbus_verbose_bytes_of_string (&message->body, 0,
1276 _dbus_string_get_length (&message->body));
1278 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
1279 sig, dbus_message_get_signature (message));
1281 s = dbus_message_get_signature (message);
1283 _dbus_assert (dbus_message_has_signature (message, sig));
1284 _dbus_assert (strcmp (s, sig) == 0);
1286 verify_test_message (message);
1288 copy = dbus_message_copy (message);
1290 _dbus_assert (dbus_message_get_reply_serial (message) ==
1291 dbus_message_get_reply_serial (copy));
1292 _dbus_assert (message->header.padding == copy->header.padding);
1294 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
1295 _dbus_string_get_length (©->header.data));
1297 _dbus_assert (_dbus_string_get_length (&message->body) ==
1298 _dbus_string_get_length (©->body));
1300 verify_test_message (copy);
1302 name1 = dbus_message_get_interface (message);
1303 name2 = dbus_message_get_interface (copy);
1305 _dbus_assert (strcmp (name1, name2) == 0);
1307 name1 = dbus_message_get_member (message);
1308 name2 = dbus_message_get_member (copy);
1310 _dbus_assert (strcmp (name1, name2) == 0);
1312 dbus_message_unref (copy);
1314 /* Message loader test */
1315 dbus_message_lock (message);
1316 loader = _dbus_message_loader_new ();
1318 /* check ref/unref */
1319 _dbus_message_loader_ref (loader);
1320 _dbus_message_loader_unref (loader);
1322 /* Write the header data one byte at a time */
1323 data = _dbus_string_get_const_data (&message->header.data);
1324 for (i = 0; i < _dbus_string_get_length (&message->header.data); i++)
1328 _dbus_message_loader_get_buffer (loader, &buffer);
1329 _dbus_string_append_byte (buffer, data[i]);
1330 _dbus_message_loader_return_buffer (loader, buffer, 1);
1333 /* Write the body data one byte at a time */
1334 data = _dbus_string_get_const_data (&message->body);
1335 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
1339 _dbus_message_loader_get_buffer (loader, &buffer);
1340 _dbus_string_append_byte (buffer, data[i]);
1341 _dbus_message_loader_return_buffer (loader, buffer, 1);
1344 #ifdef HAVE_UNIX_FD_PASSING
1347 unsigned n_unix_fds;
1349 _dbus_message_loader_get_unix_fds(loader, &unix_fds, &n_unix_fds);
1350 _dbus_assert(n_unix_fds > 0);
1351 _dbus_assert(message->n_unix_fds == 1);
1352 unix_fds[0] = _dbus_dup(message->unix_fds[0], NULL);
1353 _dbus_assert(unix_fds[0] >= 0);
1354 _dbus_message_loader_return_unix_fds(loader, unix_fds, 1);
1358 dbus_message_unref (message);
1360 /* Now pop back the message */
1361 if (!_dbus_message_loader_queue_messages (loader))
1362 _dbus_assert_not_reached ("no memory to queue messages");
1364 if (_dbus_message_loader_get_is_corrupted (loader))
1365 _dbus_assert_not_reached ("message loader corrupted");
1367 message = _dbus_message_loader_pop_message (loader);
1369 _dbus_assert_not_reached ("received a NULL message");
1371 if (dbus_message_get_reply_serial (message) != 5678)
1372 _dbus_assert_not_reached ("reply serial fields differ");
1374 dbus_message_unref (message);
1376 /* ovveride the serial, since it was reset by dbus_message_copy() */
1377 dbus_message_set_serial(message_without_unix_fds, 8901);
1379 dbus_message_lock (message_without_unix_fds);
1381 verify_test_message (message_without_unix_fds);
1384 /* Marshal and demarshal the message. */
1386 DBusMessage *message2;
1387 DBusError error = DBUS_ERROR_INIT;
1388 char *marshalled = NULL;
1390 char garbage_header[DBUS_MINIMUM_HEADER_SIZE] = "xxx";
1392 if (!dbus_message_marshal (message_without_unix_fds, &marshalled, &len))
1393 _dbus_assert_not_reached ("failed to marshal message");
1395 _dbus_assert (len != 0);
1396 _dbus_assert (marshalled != NULL);
1398 _dbus_assert (dbus_message_demarshal_bytes_needed (marshalled, len) == len);
1399 message2 = dbus_message_demarshal (marshalled, len, &error);
1401 _dbus_assert (message2 != NULL);
1402 _dbus_assert (!dbus_error_is_set (&error));
1403 verify_test_message (message2);
1405 dbus_message_unref (message2);
1406 dbus_free (marshalled);
1408 /* Demarshal invalid message. */
1410 message2 = dbus_message_demarshal ("invalid", 7, &error);
1411 _dbus_assert (message2 == NULL);
1412 _dbus_assert (dbus_error_is_set (&error));
1413 dbus_error_free (&error);
1415 /* Demarshal invalid (empty) message. */
1417 message2 = dbus_message_demarshal ("", 0, &error);
1418 _dbus_assert (message2 == NULL);
1419 _dbus_assert (dbus_error_is_set (&error));
1420 dbus_error_free (&error);
1422 /* Bytes needed to demarshal empty message: 0 (more) */
1424 _dbus_assert (dbus_message_demarshal_bytes_needed ("", 0) == 0);
1426 /* Bytes needed to demarshal invalid message: -1 (error). */
1428 _dbus_assert (dbus_message_demarshal_bytes_needed (garbage_header, DBUS_MINIMUM_HEADER_SIZE) == -1);
1431 dbus_message_unref (message_without_unix_fds);
1432 _dbus_message_loader_unref (loader);
1435 _dbus_check_fdleaks_leave (initial_fds);
1436 initial_fds = _dbus_check_fdleaks_enter ();
1438 /* Check that we can abandon a container */
1439 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1440 "/org/freedesktop/TestPath",
1441 "Foo.TestInterface",
1444 dbus_message_iter_init_append (message, &iter);
1446 _dbus_assert (dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1447 (DBUS_STRUCT_BEGIN_CHAR_AS_STRING
1448 DBUS_TYPE_STRING_AS_STRING
1449 DBUS_TYPE_STRING_AS_STRING
1450 DBUS_STRUCT_END_CHAR_AS_STRING),
1452 _dbus_assert (dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT,
1453 NULL, &struct_iter));
1456 _dbus_assert (dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING,
1459 /* uh-oh, error, try and unwind */
1461 dbus_message_iter_abandon_container (&array_iter, &struct_iter);
1462 dbus_message_iter_abandon_container (&array_iter, &iter);
1464 dbus_message_unref (message);
1466 /* Load all the sample messages from the message factory */
1468 DBusMessageDataIter diter;
1469 DBusMessageData mdata;
1472 reset_validities_seen ();
1475 _dbus_message_data_iter_init (&diter);
1477 while (_dbus_message_data_iter_get_and_next (&diter,
1480 if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
1481 mdata.expected_validity))
1483 _dbus_warn ("expected validity %d and did not get it\n",
1484 mdata.expected_validity);
1485 _dbus_assert_not_reached ("message data failed");
1488 _dbus_message_data_free (&mdata);
1493 printf ("%d sample messages tested\n", count);
1495 print_validities_seen (FALSE);
1496 print_validities_seen (TRUE);
1500 _dbus_check_fdleaks_leave (initial_fds);
1502 /* Now load every message in test_data_dir if we have one */
1503 if (test_data_dir == NULL)
1506 initial_fds = _dbus_check_fdleaks_enter ();
1508 if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
1509 (DBusForeachMessageFileFunc)
1510 dbus_internal_do_not_use_try_message_file,
1512 _dbus_assert_not_reached ("foreach_message_file test failed");
1514 _dbus_check_fdleaks_leave (initial_fds);
1519 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */