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);
487 len = _dbus_string_get_length (data);
488 for (i = 0; i < len; i++)
492 _dbus_message_loader_get_buffer (loader, &buffer);
493 _dbus_string_append_byte (buffer,
494 _dbus_string_get_byte (data, i));
495 _dbus_message_loader_return_buffer (loader, buffer);
498 if (!check_loader_results (loader, expected_validity))
501 _dbus_message_loader_unref (loader);
504 /* Write the data all at once */
506 loader = _dbus_message_loader_new ();
511 _dbus_message_loader_get_buffer (loader, &buffer);
512 _dbus_string_copy (data, 0, buffer,
513 _dbus_string_get_length (buffer));
514 _dbus_message_loader_return_buffer (loader, buffer);
517 if (!check_loader_results (loader, expected_validity))
520 _dbus_message_loader_unref (loader);
523 /* Write the data 2 bytes at a time */
525 loader = _dbus_message_loader_new ();
527 len = _dbus_string_get_length (data);
528 for (i = 0; i < len; i += 2)
532 _dbus_message_loader_get_buffer (loader, &buffer);
533 _dbus_string_append_byte (buffer,
534 _dbus_string_get_byte (data, i));
536 _dbus_string_append_byte (buffer,
537 _dbus_string_get_byte (data, i+1));
538 _dbus_message_loader_return_buffer (loader, buffer);
541 if (!check_loader_results (loader, expected_validity))
544 _dbus_message_loader_unref (loader);
552 _dbus_message_loader_unref (loader);
558 process_test_subdir (const DBusString *test_base_dir,
560 DBusValidity expected_validity,
561 DBusForeachMessageFileFunc function,
564 DBusString test_directory;
568 DBusError error = DBUS_ERROR_INIT;
573 if (!_dbus_string_init (&test_directory))
574 _dbus_assert_not_reached ("didn't allocate test_directory");
576 _dbus_string_init_const (&filename, subdir);
578 if (!_dbus_string_copy (test_base_dir, 0,
580 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
582 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
583 _dbus_assert_not_reached ("couldn't allocate full path");
585 _dbus_string_free (&filename);
586 if (!_dbus_string_init (&filename))
587 _dbus_assert_not_reached ("didn't allocate filename string");
589 dir = _dbus_directory_open (&test_directory, &error);
592 _dbus_warn ("Could not open %s: %s\n",
593 _dbus_string_get_const_data (&test_directory),
595 dbus_error_free (&error);
599 printf ("Testing %s:\n", subdir);
602 while (_dbus_directory_get_next_file (dir, &filename, &error))
604 DBusString full_path;
606 if (!_dbus_string_init (&full_path))
607 _dbus_assert_not_reached ("couldn't init string");
609 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
610 _dbus_assert_not_reached ("couldn't copy dir to full_path");
612 if (!_dbus_concat_dir_and_file (&full_path, &filename))
613 _dbus_assert_not_reached ("couldn't concat file to dir");
615 if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
619 if (_dbus_string_ends_with_c_str (&filename, ".message"))
621 printf ("SKIP: Could not load %s, message builder language no longer supported\n",
622 _dbus_string_get_const_data (&filename));
625 _dbus_verbose ("Skipping non-.message file %s\n",
626 _dbus_string_get_const_data (&filename));
627 _dbus_string_free (&full_path);
632 _dbus_string_get_const_data (&filename));
634 if (! (*function) (&full_path,
635 expected_validity, user_data))
637 _dbus_string_free (&full_path);
641 _dbus_string_free (&full_path);
644 if (dbus_error_is_set (&error))
646 _dbus_warn ("Could not get next file in %s: %s\n",
647 _dbus_string_get_const_data (&test_directory),
649 dbus_error_free (&error);
658 _dbus_directory_close (dir);
659 _dbus_string_free (&test_directory);
660 _dbus_string_free (&filename);
666 * Runs the given function on every message file in the test suite.
667 * The function should return #FALSE on test failure or fatal error.
669 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
670 * @param func the function to run
671 * @param user_data data for function
672 * @returns #FALSE if there's a failure
675 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
676 DBusForeachMessageFileFunc func,
679 DBusString test_directory;
684 _dbus_string_init_const (&test_directory, test_data_dir);
686 if (!process_test_subdir (&test_directory, "valid-messages",
687 DBUS_VALID, func, user_data))
692 if (!process_test_subdir (&test_directory, "invalid-messages",
693 DBUS_INVALID_FOR_UNKNOWN_REASON, func, user_data))
698 if (!process_test_subdir (&test_directory, "incomplete-messages",
699 DBUS_VALID_BUT_INCOMPLETE, func, user_data))
708 _dbus_string_free (&test_directory);
714 #define GET_AND_CHECK(iter, typename, literal) \
716 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
717 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
718 dbus_message_iter_get_basic (&iter, &v_##typename); \
719 if (v_##typename != literal) \
720 _dbus_assert_not_reached ("got wrong value from message iter"); \
723 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
725 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
726 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
727 dbus_message_iter_get_basic (&iter, &v_##typename); \
728 if (strcmp (v_##typename, literal) != 0) \
729 _dbus_assert_not_reached ("got wrong value from message iter"); \
732 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
734 GET_AND_CHECK(iter, typename, literal); \
735 if (!dbus_message_iter_next (&iter)) \
736 _dbus_assert_not_reached ("failed to move iter to next"); \
739 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
741 GET_AND_CHECK_STRCMP(iter, typename, literal); \
742 if (!dbus_message_iter_next (&iter)) \
743 _dbus_assert_not_reached ("failed to move iter to next"); \
747 message_iter_test (DBusMessage *message)
749 DBusMessageIter iter, array, array2;
750 const char *v_STRING;
752 dbus_int16_t v_INT16;
753 dbus_uint16_t v_UINT16;
754 dbus_int32_t v_INT32;
755 dbus_uint32_t v_UINT32;
756 dbus_int64_t v_INT64;
757 dbus_uint64_t v_UINT64;
758 unsigned char v_BYTE;
759 dbus_bool_t v_BOOLEAN;
761 const dbus_int32_t *our_int_array;
764 dbus_message_iter_init (message, &iter);
766 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
767 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
768 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
769 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
771 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
772 _dbus_assert_not_reached ("Argument type not an array");
774 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
775 _dbus_assert_not_reached ("Array type not double");
777 dbus_message_iter_recurse (&iter, &array);
779 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
780 GET_AND_CHECK (array, DOUBLE, 2.5);
782 if (dbus_message_iter_next (&array))
783 _dbus_assert_not_reached ("Didn't reach end of array");
785 if (!dbus_message_iter_next (&iter))
786 _dbus_assert_not_reached ("Reached end of arguments");
788 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
790 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
791 _dbus_assert_not_reached ("no array");
793 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
794 _dbus_assert_not_reached ("Array type not int32");
797 dbus_message_iter_recurse (&iter, &array);
799 if (dbus_message_iter_next (&array))
800 _dbus_assert_not_reached ("Didn't reach end of array");
802 if (!dbus_message_iter_next (&iter))
803 _dbus_assert_not_reached ("Reached end of arguments");
805 GET_AND_CHECK (iter, BYTE, 0xF0);
807 if (dbus_message_iter_next (&iter))
808 _dbus_assert_not_reached ("Didn't reach end of arguments");
813 verify_test_message (DBusMessage *message)
815 DBusMessageIter iter;
816 DBusError error = DBUS_ERROR_INIT;
817 dbus_int16_t our_int16;
818 dbus_uint16_t our_uint16;
819 dbus_int32_t our_int;
820 dbus_uint32_t our_uint;
824 dbus_bool_t our_bool;
825 unsigned char our_byte_1, our_byte_2;
826 const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
827 int our_uint32_array_len;
828 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
829 int our_int32_array_len;
830 dbus_int64_t our_int64;
831 dbus_uint64_t our_uint64;
832 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
833 int our_uint64_array_len;
834 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
835 int our_int64_array_len;
836 const double *our_double_array = (void*)0xdeadbeef;
837 int our_double_array_len;
838 const unsigned char *our_byte_array = (void*)0xdeadbeef;
839 int our_byte_array_len;
840 const dbus_bool_t *our_boolean_array = (void*)0xdeadbeef;
841 int our_boolean_array_len;
842 char **our_string_array;
843 int our_string_array_len;
845 dbus_message_iter_init (message, &iter);
847 if (!dbus_message_iter_get_args (&iter, &error,
848 DBUS_TYPE_INT16, &our_int16,
849 DBUS_TYPE_UINT16, &our_uint16,
850 DBUS_TYPE_INT32, &our_int,
851 DBUS_TYPE_UINT32, &our_uint,
852 DBUS_TYPE_INT64, &our_int64,
853 DBUS_TYPE_UINT64, &our_uint64,
854 DBUS_TYPE_STRING, &our_str,
855 DBUS_TYPE_DOUBLE, &our_double,
856 DBUS_TYPE_BOOLEAN, &our_bool,
857 DBUS_TYPE_BYTE, &our_byte_1,
858 DBUS_TYPE_BYTE, &our_byte_2,
859 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
860 &our_uint32_array, &our_uint32_array_len,
861 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
862 &our_int32_array, &our_int32_array_len,
863 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
864 &our_uint64_array, &our_uint64_array_len,
865 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
866 &our_int64_array, &our_int64_array_len,
867 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
868 &our_double_array, &our_double_array_len,
869 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
870 &our_byte_array, &our_byte_array_len,
871 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
872 &our_boolean_array, &our_boolean_array_len,
873 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
874 &our_string_array, &our_string_array_len,
877 _dbus_warn ("error: %s - %s\n", error.name,
878 (error.message != NULL) ? error.message : "no message");
879 _dbus_assert_not_reached ("Could not get arguments");
882 if (our_int16 != -0x123)
883 _dbus_assert_not_reached ("16-bit integers differ!");
885 if (our_uint16 != 0x123)
886 _dbus_assert_not_reached ("16-bit uints differ!");
888 if (our_int != -0x12345678)
889 _dbus_assert_not_reached ("integers differ!");
891 if (our_uint != 0x12300042)
892 _dbus_assert_not_reached ("uints differ!");
894 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
895 _dbus_assert_not_reached ("64-bit integers differ!");
896 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
897 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
900 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double, v_DOUBLE))
901 _dbus_assert_not_reached ("doubles differ!");
903 if (strcmp (our_str, "Test string") != 0)
904 _dbus_assert_not_reached ("strings differ!");
907 _dbus_assert_not_reached ("booleans differ");
909 if (our_byte_1 != 42)
910 _dbus_assert_not_reached ("bytes differ!");
912 if (our_byte_2 != 24)
913 _dbus_assert_not_reached ("bytes differ!");
915 if (our_uint32_array_len != 4 ||
916 our_uint32_array[0] != 0x12345678 ||
917 our_uint32_array[1] != 0x23456781 ||
918 our_uint32_array[2] != 0x34567812 ||
919 our_uint32_array[3] != 0x45678123)
920 _dbus_assert_not_reached ("uint array differs");
922 if (our_int32_array_len != 4 ||
923 our_int32_array[0] != 0x12345678 ||
924 our_int32_array[1] != -0x23456781 ||
925 our_int32_array[2] != 0x34567812 ||
926 our_int32_array[3] != -0x45678123)
927 _dbus_assert_not_reached ("int array differs");
929 if (our_uint64_array_len != 4 ||
930 our_uint64_array[0] != 0x12345678 ||
931 our_uint64_array[1] != 0x23456781 ||
932 our_uint64_array[2] != 0x34567812 ||
933 our_uint64_array[3] != 0x45678123)
934 _dbus_assert_not_reached ("uint64 array differs");
936 if (our_int64_array_len != 4 ||
937 our_int64_array[0] != 0x12345678 ||
938 our_int64_array[1] != -0x23456781 ||
939 our_int64_array[2] != 0x34567812 ||
940 our_int64_array[3] != -0x45678123)
941 _dbus_assert_not_reached ("int64 array differs");
943 if (our_double_array_len != 3)
944 _dbus_assert_not_reached ("double array had wrong length");
946 /* On all IEEE machines (i.e. everything sane) exact equality
947 * should be preserved over the wire
950 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[0], v_DOUBLE))
951 _dbus_assert_not_reached ("double array had wrong values");
952 v_DOUBLE = 9876.54321;
953 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[1], v_DOUBLE))
954 _dbus_assert_not_reached ("double array had wrong values");
956 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[2], v_DOUBLE))
957 _dbus_assert_not_reached ("double array had wrong values");
959 if (our_byte_array_len != 4)
960 _dbus_assert_not_reached ("byte array had wrong length");
962 if (our_byte_array[0] != 'a' ||
963 our_byte_array[1] != 'b' ||
964 our_byte_array[2] != 'c' ||
965 our_byte_array[3] != 234)
966 _dbus_assert_not_reached ("byte array had wrong values");
968 if (our_boolean_array_len != 5)
969 _dbus_assert_not_reached ("bool array had wrong length");
971 if (our_boolean_array[0] != TRUE ||
972 our_boolean_array[1] != FALSE ||
973 our_boolean_array[2] != TRUE ||
974 our_boolean_array[3] != TRUE ||
975 our_boolean_array[4] != FALSE)
976 _dbus_assert_not_reached ("bool array had wrong values");
978 if (our_string_array_len != 4)
979 _dbus_assert_not_reached ("string array was wrong length");
981 if (strcmp (our_string_array[0], "Foo") != 0 ||
982 strcmp (our_string_array[1], "bar") != 0 ||
983 strcmp (our_string_array[2], "") != 0 ||
984 strcmp (our_string_array[3], "woo woo woo woo") != 0)
985 _dbus_assert_not_reached ("string array had wrong values");
987 dbus_free_string_array (our_string_array);
989 if (dbus_message_iter_next (&iter))
990 _dbus_assert_not_reached ("Didn't reach end of arguments");
994 verify_test_message_args_ignored (DBusMessage *message)
996 DBusMessageIter iter;
997 DBusError error = DBUS_ERROR_INIT;
998 dbus_uint32_t our_uint;
999 DBusInitialFDs *initial_fds;
1001 initial_fds = _dbus_check_fdleaks_enter ();
1003 /* parse with empty signature: "" */
1004 dbus_message_iter_init (message, &iter);
1005 if (!dbus_message_iter_get_args (&iter, &error,
1008 _dbus_warn ("error: %s - %s\n", error.name,
1009 (error.message != NULL) ? error.message : "no message");
1013 _dbus_assert (!dbus_error_is_set (&error));
1014 _dbus_verbose ("arguments ignored.\n");
1017 /* parse with shorter signature: "u" */
1018 dbus_message_iter_init (message, &iter);
1019 if (!dbus_message_iter_get_args (&iter, &error,
1020 DBUS_TYPE_UINT32, &our_uint,
1023 _dbus_warn ("error: %s - %s\n", error.name,
1024 (error.message != NULL) ? error.message : "no message");
1028 _dbus_assert (!dbus_error_is_set (&error));
1029 _dbus_verbose ("arguments ignored.\n");
1032 _dbus_check_fdleaks_leave (initial_fds);
1036 verify_test_message_memleak (DBusMessage *message)
1038 DBusMessageIter iter;
1039 DBusError error = DBUS_ERROR_INIT;
1040 dbus_uint32_t our_uint1;
1041 dbus_uint32_t our_uint2;
1042 dbus_uint32_t our_uint3;
1043 char **our_string_array1;
1044 int our_string_array_len1;
1045 char **our_string_array2;
1046 int our_string_array_len2;
1047 #ifdef HAVE_UNIX_FD_PASSING
1051 DBusInitialFDs *initial_fds;
1053 initial_fds = _dbus_check_fdleaks_enter ();
1055 /* parse with wrong signature: "uashuu" */
1056 dbus_error_free (&error);
1057 dbus_message_iter_init (message, &iter);
1058 if (!dbus_message_iter_get_args (&iter, &error,
1059 DBUS_TYPE_UINT32, &our_uint1,
1060 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1061 &our_string_array1, &our_string_array_len1,
1062 #ifdef HAVE_UNIX_FD_PASSING
1063 DBUS_TYPE_UNIX_FD, &our_unix_fd1,
1065 DBUS_TYPE_UINT32, &our_uint2,
1066 DBUS_TYPE_UINT32, &our_uint3,
1069 _dbus_verbose ("expected error: %s - %s\n", error.name,
1070 (error.message != NULL) ? error.message : "no message");
1071 /* ensure array of string and unix fd not leaked */
1072 _dbus_assert (our_string_array1 == NULL);
1073 #ifdef HAVE_UNIX_FD_PASSING
1074 _dbus_assert (our_unix_fd1 == -1);
1079 _dbus_warn ("error: parse with wrong signature: 'uashuu'.\n");
1082 /* parse with wrong signature: "uashuashu" */
1083 dbus_message_iter_init (message, &iter);
1084 dbus_error_free (&error);
1085 if (!dbus_message_iter_get_args (&iter, &error,
1086 DBUS_TYPE_UINT32, &our_uint1,
1087 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1088 &our_string_array1, &our_string_array_len1,
1089 #ifdef HAVE_UNIX_FD_PASSING
1090 DBUS_TYPE_UNIX_FD, &our_unix_fd1,
1092 DBUS_TYPE_UINT32, &our_uint2,
1093 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1094 &our_string_array2, &our_string_array_len2,
1095 #ifdef HAVE_UNIX_FD_PASSING
1096 DBUS_TYPE_UNIX_FD, &our_unix_fd2,
1098 DBUS_TYPE_UINT32, &our_uint3,
1101 _dbus_verbose ("expected error: %s - %s\n", error.name,
1102 (error.message != NULL) ? error.message : "no message");
1103 /* ensure array of string and unix fd not leaked */
1104 _dbus_assert (our_string_array1 == NULL);
1105 _dbus_assert (our_string_array2 == NULL);
1106 #ifdef HAVE_UNIX_FD_PASSING
1107 _dbus_assert (our_unix_fd1 == -1);
1108 _dbus_assert (our_unix_fd2 == -1);
1113 _dbus_warn ("error: parse with wrong signature: 'uashuashu'.\n");
1116 /* parse with correct signature: "uashuash" */
1117 dbus_message_iter_init (message, &iter);
1118 dbus_error_free (&error);
1119 if (!dbus_message_iter_get_args (&iter, &error,
1120 DBUS_TYPE_UINT32, &our_uint1,
1121 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1122 &our_string_array1, &our_string_array_len1,
1123 #ifdef HAVE_UNIX_FD_PASSING
1124 DBUS_TYPE_UNIX_FD, &our_unix_fd1,
1126 DBUS_TYPE_UINT32, &our_uint2,
1127 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1128 &our_string_array2, &our_string_array_len2,
1129 #ifdef HAVE_UNIX_FD_PASSING
1130 DBUS_TYPE_UNIX_FD, &our_unix_fd2,
1134 _dbus_warn ("error: %s - %s\n", error.name,
1135 (error.message != NULL) ? error.message : "no message");
1136 _dbus_assert_not_reached ("Could not get arguments");
1140 dbus_free_string_array (our_string_array1);
1141 dbus_free_string_array (our_string_array2);
1142 #ifdef HAVE_UNIX_FD_PASSING
1143 _dbus_close (our_unix_fd1, &error);
1144 _dbus_close (our_unix_fd2, &error);
1147 _dbus_check_fdleaks_leave (initial_fds);
1151 * @ingroup DBusMessageInternals
1152 * Unit test for DBusMessage.
1154 * @returns #TRUE on success.
1157 _dbus_message_test (const char *test_data_dir)
1159 DBusMessage *message, *message_without_unix_fds;
1160 DBusMessageLoader *loader;
1166 const dbus_uint32_t our_uint32_array[] =
1167 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
1168 const dbus_int32_t our_int32_array[] =
1169 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
1170 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
1171 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
1172 const dbus_uint64_t our_uint64_array[] =
1173 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
1174 const dbus_int64_t our_int64_array[] =
1175 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
1176 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
1177 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
1178 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
1179 const char *our_string_array1[] = { "foo", "Bar", "", "Woo woo Woo woo" };
1180 const char **v_ARRAY_STRING = our_string_array;
1181 const char **v1_ARRAY_STRING = our_string_array1;
1182 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
1183 const double *v_ARRAY_DOUBLE = our_double_array;
1184 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
1185 const unsigned char *v_ARRAY_BYTE = our_byte_array;
1186 const dbus_bool_t our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
1187 const dbus_bool_t *v_ARRAY_BOOLEAN = our_boolean_array;
1190 const char *v_STRING;
1192 dbus_int16_t v_INT16;
1193 dbus_uint16_t v_UINT16;
1194 dbus_int32_t v_INT32;
1195 dbus_uint32_t v_UINT32;
1196 dbus_uint32_t v1_UINT32;
1197 dbus_int64_t v_INT64;
1198 dbus_uint64_t v_UINT64;
1199 unsigned char v_BYTE;
1200 unsigned char v2_BYTE;
1201 dbus_bool_t v_BOOLEAN;
1202 DBusMessageIter iter, array_iter, struct_iter;
1203 #ifdef HAVE_UNIX_FD_PASSING
1208 DBusInitialFDs *initial_fds;
1210 char basic_types[] = DBUS_TYPE_BYTE_AS_STRING \
1211 DBUS_TYPE_BOOLEAN_AS_STRING \
1212 DBUS_TYPE_INT16_AS_STRING \
1213 DBUS_TYPE_INT32_AS_STRING \
1214 DBUS_TYPE_INT64_AS_STRING \
1215 DBUS_TYPE_UINT16_AS_STRING \
1216 DBUS_TYPE_UINT32_AS_STRING \
1217 DBUS_TYPE_UINT64_AS_STRING \
1218 DBUS_TYPE_DOUBLE_AS_STRING \
1219 DBUS_TYPE_STRING_AS_STRING;
1221 initial_fds = _dbus_check_fdleaks_enter ();
1223 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1224 "/org/freedesktop/TestPath",
1225 "Foo.TestInterface",
1227 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1228 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
1230 _dbus_assert (strcmp (dbus_message_get_path (message),
1231 "/org/freedesktop/TestPath") == 0);
1232 dbus_message_set_serial (message, 1234);
1234 /* string length including nul byte not a multiple of 4 */
1235 if (!dbus_message_set_sender (message, "org.foo.bar1"))
1236 _dbus_assert_not_reached ("out of memory");
1238 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
1239 dbus_message_set_reply_serial (message, 5678);
1241 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1242 _dbus_string_get_length (&message->header.data));
1243 _dbus_verbose_bytes_of_string (&message->body, 0,
1244 _dbus_string_get_length (&message->body));
1246 if (!dbus_message_set_sender (message, NULL))
1247 _dbus_assert_not_reached ("out of memory");
1250 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1251 _dbus_string_get_length (&message->header.data));
1252 _dbus_verbose_bytes_of_string (&message->body, 0,
1253 _dbus_string_get_length (&message->body));
1256 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
1257 _dbus_assert (dbus_message_get_serial (message) == 1234);
1258 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
1259 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
1261 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1262 dbus_message_set_no_reply (message, TRUE);
1263 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
1264 dbus_message_set_no_reply (message, FALSE);
1265 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
1267 /* Set/get some header fields */
1269 if (!dbus_message_set_path (message, "/foo"))
1270 _dbus_assert_not_reached ("out of memory");
1271 _dbus_assert (strcmp (dbus_message_get_path (message),
1274 if (!dbus_message_set_interface (message, "org.Foo"))
1275 _dbus_assert_not_reached ("out of memory");
1276 _dbus_assert (strcmp (dbus_message_get_interface (message),
1279 if (!dbus_message_set_member (message, "Bar"))
1280 _dbus_assert_not_reached ("out of memory");
1281 _dbus_assert (strcmp (dbus_message_get_member (message),
1284 /* Set/get them with longer values */
1285 if (!dbus_message_set_path (message, "/foo/bar"))
1286 _dbus_assert_not_reached ("out of memory");
1287 _dbus_assert (strcmp (dbus_message_get_path (message),
1290 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
1291 _dbus_assert_not_reached ("out of memory");
1292 _dbus_assert (strcmp (dbus_message_get_interface (message),
1293 "org.Foo.Bar") == 0);
1295 if (!dbus_message_set_member (message, "BarFoo"))
1296 _dbus_assert_not_reached ("out of memory");
1297 _dbus_assert (strcmp (dbus_message_get_member (message),
1300 /* Realloc shorter again */
1302 if (!dbus_message_set_path (message, "/foo"))
1303 _dbus_assert_not_reached ("out of memory");
1304 _dbus_assert (strcmp (dbus_message_get_path (message),
1307 if (!dbus_message_set_interface (message, "org.Foo"))
1308 _dbus_assert_not_reached ("out of memory");
1309 _dbus_assert (strcmp (dbus_message_get_interface (message),
1312 if (!dbus_message_set_member (message, "Bar"))
1313 _dbus_assert_not_reached ("out of memory");
1314 _dbus_assert (strcmp (dbus_message_get_member (message),
1317 /* Path decomposing */
1318 dbus_message_set_path (message, NULL);
1319 dbus_message_get_path_decomposed (message, &decomposed);
1320 _dbus_assert (decomposed == NULL);
1321 dbus_free_string_array (decomposed);
1323 dbus_message_set_path (message, "/");
1324 dbus_message_get_path_decomposed (message, &decomposed);
1325 _dbus_assert (decomposed != NULL);
1326 _dbus_assert (decomposed[0] == NULL);
1327 dbus_free_string_array (decomposed);
1329 dbus_message_set_path (message, "/a/b");
1330 dbus_message_get_path_decomposed (message, &decomposed);
1331 _dbus_assert (decomposed != NULL);
1332 _dbus_assert (strcmp (decomposed[0], "a") == 0);
1333 _dbus_assert (strcmp (decomposed[1], "b") == 0);
1334 _dbus_assert (decomposed[2] == NULL);
1335 dbus_free_string_array (decomposed);
1337 dbus_message_set_path (message, "/spam/eggs");
1338 dbus_message_get_path_decomposed (message, &decomposed);
1339 _dbus_assert (decomposed != NULL);
1340 _dbus_assert (strcmp (decomposed[0], "spam") == 0);
1341 _dbus_assert (strcmp (decomposed[1], "eggs") == 0);
1342 _dbus_assert (decomposed[2] == NULL);
1343 dbus_free_string_array (decomposed);
1345 dbus_message_unref (message);
1347 /* Test the vararg functions */
1348 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1349 "/org/freedesktop/TestPath",
1350 "Foo.TestInterface",
1352 dbus_message_set_serial (message, 1);
1353 dbus_message_set_reply_serial (message, 5678);
1357 v_INT32 = -0x12345678;
1358 v_UINT32 = 0x12300042;
1359 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
1360 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
1361 v_STRING = "Test string";
1366 #ifdef HAVE_UNIX_FD_PASSING
1371 dbus_message_append_args (message,
1372 DBUS_TYPE_INT16, &v_INT16,
1373 DBUS_TYPE_UINT16, &v_UINT16,
1374 DBUS_TYPE_INT32, &v_INT32,
1375 DBUS_TYPE_UINT32, &v_UINT32,
1376 DBUS_TYPE_INT64, &v_INT64,
1377 DBUS_TYPE_UINT64, &v_UINT64,
1378 DBUS_TYPE_STRING, &v_STRING,
1379 DBUS_TYPE_DOUBLE, &v_DOUBLE,
1380 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
1381 DBUS_TYPE_BYTE, &v_BYTE,
1382 DBUS_TYPE_BYTE, &v2_BYTE,
1383 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
1384 _DBUS_N_ELEMENTS (our_uint32_array),
1385 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
1386 _DBUS_N_ELEMENTS (our_int32_array),
1387 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
1388 _DBUS_N_ELEMENTS (our_uint64_array),
1389 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
1390 _DBUS_N_ELEMENTS (our_int64_array),
1391 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
1392 _DBUS_N_ELEMENTS (our_double_array),
1393 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
1394 _DBUS_N_ELEMENTS (our_byte_array),
1395 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
1396 _DBUS_N_ELEMENTS (our_boolean_array),
1397 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
1398 _DBUS_N_ELEMENTS (our_string_array),
1403 sig[i++] = DBUS_TYPE_INT16;
1404 sig[i++] = DBUS_TYPE_UINT16;
1405 sig[i++] = DBUS_TYPE_INT32;
1406 sig[i++] = DBUS_TYPE_UINT32;
1407 sig[i++] = DBUS_TYPE_INT64;
1408 sig[i++] = DBUS_TYPE_UINT64;
1409 sig[i++] = DBUS_TYPE_STRING;
1410 sig[i++] = DBUS_TYPE_DOUBLE;
1411 sig[i++] = DBUS_TYPE_BOOLEAN;
1412 sig[i++] = DBUS_TYPE_BYTE;
1413 sig[i++] = DBUS_TYPE_BYTE;
1414 sig[i++] = DBUS_TYPE_ARRAY;
1415 sig[i++] = DBUS_TYPE_UINT32;
1416 sig[i++] = DBUS_TYPE_ARRAY;
1417 sig[i++] = DBUS_TYPE_INT32;
1418 sig[i++] = DBUS_TYPE_ARRAY;
1419 sig[i++] = DBUS_TYPE_UINT64;
1420 sig[i++] = DBUS_TYPE_ARRAY;
1421 sig[i++] = DBUS_TYPE_INT64;
1422 sig[i++] = DBUS_TYPE_ARRAY;
1423 sig[i++] = DBUS_TYPE_DOUBLE;
1424 sig[i++] = DBUS_TYPE_ARRAY;
1425 sig[i++] = DBUS_TYPE_BYTE;
1426 sig[i++] = DBUS_TYPE_ARRAY;
1427 sig[i++] = DBUS_TYPE_BOOLEAN;
1428 sig[i++] = DBUS_TYPE_ARRAY;
1429 sig[i++] = DBUS_TYPE_STRING;
1431 message_without_unix_fds = dbus_message_copy(message);
1432 _dbus_assert(message_without_unix_fds);
1433 #ifdef HAVE_UNIX_FD_PASSING
1434 dbus_message_append_args (message,
1435 DBUS_TYPE_UNIX_FD, &v_UNIX_FD,
1437 sig[i++] = DBUS_TYPE_UNIX_FD;
1439 sig[i++] = DBUS_TYPE_INVALID;
1441 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
1443 _dbus_verbose ("HEADER\n");
1444 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1445 _dbus_string_get_length (&message->header.data));
1446 _dbus_verbose ("BODY\n");
1447 _dbus_verbose_bytes_of_string (&message->body, 0,
1448 _dbus_string_get_length (&message->body));
1450 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
1451 sig, dbus_message_get_signature (message));
1453 s = dbus_message_get_signature (message);
1455 _dbus_assert (dbus_message_has_signature (message, sig));
1456 _dbus_assert (strcmp (s, sig) == 0);
1458 verify_test_message (message);
1460 copy = dbus_message_copy (message);
1462 _dbus_assert (dbus_message_get_reply_serial (message) ==
1463 dbus_message_get_reply_serial (copy));
1464 _dbus_assert (message->header.padding == copy->header.padding);
1466 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
1467 _dbus_string_get_length (©->header.data));
1469 _dbus_assert (_dbus_string_get_length (&message->body) ==
1470 _dbus_string_get_length (©->body));
1472 verify_test_message (copy);
1474 name1 = dbus_message_get_interface (message);
1475 name2 = dbus_message_get_interface (copy);
1477 _dbus_assert (strcmp (name1, name2) == 0);
1479 name1 = dbus_message_get_member (message);
1480 name2 = dbus_message_get_member (copy);
1482 _dbus_assert (strcmp (name1, name2) == 0);
1484 dbus_message_unref (copy);
1486 /* Message loader test */
1487 dbus_message_lock (message);
1488 loader = _dbus_message_loader_new ();
1490 /* check ref/unref */
1491 _dbus_message_loader_ref (loader);
1492 _dbus_message_loader_unref (loader);
1494 /* Write the header data one byte at a time */
1495 data = _dbus_string_get_const_data (&message->header.data);
1496 for (i = 0; i < _dbus_string_get_length (&message->header.data); i++)
1500 _dbus_message_loader_get_buffer (loader, &buffer);
1501 _dbus_string_append_byte (buffer, data[i]);
1502 _dbus_message_loader_return_buffer (loader, buffer);
1505 /* Write the body data one byte at a time */
1506 data = _dbus_string_get_const_data (&message->body);
1507 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
1511 _dbus_message_loader_get_buffer (loader, &buffer);
1512 _dbus_string_append_byte (buffer, data[i]);
1513 _dbus_message_loader_return_buffer (loader, buffer);
1516 #ifdef HAVE_UNIX_FD_PASSING
1519 unsigned n_unix_fds;
1521 _dbus_message_loader_get_unix_fds(loader, &unix_fds, &n_unix_fds);
1522 _dbus_assert(n_unix_fds > 0);
1523 _dbus_assert(message->n_unix_fds == 1);
1524 unix_fds[0] = _dbus_dup(message->unix_fds[0], NULL);
1525 _dbus_assert(unix_fds[0] >= 0);
1526 _dbus_message_loader_return_unix_fds(loader, unix_fds, 1);
1530 dbus_message_unref (message);
1532 /* Now pop back the message */
1533 if (!_dbus_message_loader_queue_messages (loader))
1534 _dbus_assert_not_reached ("no memory to queue messages");
1536 if (_dbus_message_loader_get_is_corrupted (loader))
1537 _dbus_assert_not_reached ("message loader corrupted");
1539 message = _dbus_message_loader_pop_message (loader);
1541 _dbus_assert_not_reached ("received a NULL message");
1543 if (dbus_message_get_reply_serial (message) != 5678)
1544 _dbus_assert_not_reached ("reply serial fields differ");
1546 dbus_message_unref (message);
1548 /* ovveride the serial, since it was reset by dbus_message_copy() */
1549 dbus_message_set_serial(message_without_unix_fds, 8901);
1551 dbus_message_lock (message_without_unix_fds);
1553 verify_test_message (message_without_unix_fds);
1556 /* Marshal and demarshal the message. */
1558 DBusMessage *message2;
1559 DBusError error = DBUS_ERROR_INIT;
1560 char *marshalled = NULL;
1562 char garbage_header[DBUS_MINIMUM_HEADER_SIZE] = "xxx";
1564 if (!dbus_message_marshal (message_without_unix_fds, &marshalled, &len))
1565 _dbus_assert_not_reached ("failed to marshal message");
1567 _dbus_assert (len != 0);
1568 _dbus_assert (marshalled != NULL);
1570 _dbus_assert (dbus_message_demarshal_bytes_needed (marshalled, len) == len);
1571 message2 = dbus_message_demarshal (marshalled, len, &error);
1573 _dbus_assert (message2 != NULL);
1574 _dbus_assert (!dbus_error_is_set (&error));
1575 verify_test_message (message2);
1577 dbus_message_unref (message2);
1578 dbus_free (marshalled);
1580 /* Demarshal invalid message. */
1582 message2 = dbus_message_demarshal ("invalid", 7, &error);
1583 _dbus_assert (message2 == NULL);
1584 _dbus_assert (dbus_error_is_set (&error));
1585 dbus_error_free (&error);
1587 /* Demarshal invalid (empty) message. */
1589 message2 = dbus_message_demarshal ("", 0, &error);
1590 _dbus_assert (message2 == NULL);
1591 _dbus_assert (dbus_error_is_set (&error));
1592 dbus_error_free (&error);
1594 /* Bytes needed to demarshal empty message: 0 (more) */
1596 _dbus_assert (dbus_message_demarshal_bytes_needed ("", 0) == 0);
1598 /* Bytes needed to demarshal invalid message: -1 (error). */
1600 _dbus_assert (dbus_message_demarshal_bytes_needed (garbage_header, DBUS_MINIMUM_HEADER_SIZE) == -1);
1603 dbus_message_unref (message_without_unix_fds);
1604 _dbus_message_loader_unref (loader);
1607 _dbus_check_fdleaks_leave (initial_fds);
1608 initial_fds = _dbus_check_fdleaks_enter ();
1610 /* Test enumeration of array elements */
1611 for (i = strlen (basic_types) - 1; i > 0; i--)
1615 char* signature = _dbus_strdup ("?");
1617 signature[0] = basic_types[i];
1618 s = "SomeThingToSay";
1619 memset (&val, '\0', sizeof (val));
1621 message = dbus_message_new_method_call ("de.ende.test",
1622 "/de/ende/test", "de.ende.Test", "ArtistName");
1623 _dbus_assert (message != NULL);
1624 dbus_message_iter_init_append (message, &iter);
1625 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1626 signature, &array_iter);
1627 for (some = 0; some < 3; some++)
1629 if (basic_types[i] == DBUS_TYPE_STRING)
1630 dbus_message_iter_append_basic (&array_iter, DBUS_TYPE_STRING, &s);
1632 dbus_message_iter_append_basic (&array_iter, basic_types[i], &val);
1634 dbus_message_iter_close_container (&iter, &array_iter);
1635 dbus_message_iter_init (message, &iter);
1636 _dbus_assert (dbus_message_iter_get_element_count (&iter) == some);
1637 dbus_message_unref (message);
1638 dbus_free (signature);
1640 /* Array of structs */
1641 message = dbus_message_new_method_call ("de.ende.test",
1642 "/de/ende/test", "de.ende.Test", "ArtistName");
1643 _dbus_assert (message != NULL);
1644 dbus_message_iter_init_append (message, &iter);
1645 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1646 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
1647 DBUS_TYPE_STRING_AS_STRING
1648 DBUS_STRUCT_END_CHAR_AS_STRING, &array_iter);
1649 dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT,
1650 NULL, &struct_iter);
1652 dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING, &s);
1653 dbus_message_iter_close_container (&array_iter, &struct_iter);
1654 dbus_message_iter_close_container (&iter, &array_iter);
1655 dbus_message_iter_init (message, &iter);
1656 _dbus_assert (dbus_message_iter_get_element_count (&iter) == 1);
1657 dbus_message_unref (message);
1660 /* Check that we can abandon a container */
1661 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1662 "/org/freedesktop/TestPath",
1663 "Foo.TestInterface",
1666 dbus_message_iter_init_append (message, &iter);
1668 ok = dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
1669 (DBUS_STRUCT_BEGIN_CHAR_AS_STRING
1670 DBUS_TYPE_STRING_AS_STRING
1671 DBUS_TYPE_STRING_AS_STRING
1672 DBUS_STRUCT_END_CHAR_AS_STRING),
1675 ok = dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT,
1676 NULL, &struct_iter);
1679 ok = dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING, &s);
1682 /* uh-oh, error, try and unwind */
1684 dbus_message_iter_abandon_container (&array_iter, &struct_iter);
1685 dbus_message_iter_abandon_container (&array_iter, &iter);
1687 dbus_message_unref (message);
1689 /* Check we should not leak array of string or unix fd, fd.o#21259 */
1690 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1691 "/org/freedesktop/TestPath",
1692 "Foo.TestInterface",
1695 /* signature "uashuash" */
1696 dbus_message_append_args (message,
1697 DBUS_TYPE_UINT32, &v_UINT32,
1698 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
1699 _DBUS_N_ELEMENTS (our_string_array),
1700 #ifdef HAVE_UNIX_FD_PASSING
1701 DBUS_TYPE_UNIX_FD, &v_UNIX_FD,
1703 DBUS_TYPE_UINT32, &v1_UINT32,
1704 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v1_ARRAY_STRING,
1705 _DBUS_N_ELEMENTS (our_string_array1),
1706 #ifdef HAVE_UNIX_FD_PASSING
1707 DBUS_TYPE_UNIX_FD, &v1_UNIX_FD,
1713 sig[i++] = DBUS_TYPE_UINT32;
1714 sig[i++] = DBUS_TYPE_ARRAY;
1715 sig[i++] = DBUS_TYPE_STRING;
1716 #ifdef HAVE_UNIX_FD_PASSING
1717 sig[i++] = DBUS_TYPE_UNIX_FD;
1719 sig[i++] = DBUS_TYPE_UINT32;
1720 sig[i++] = DBUS_TYPE_ARRAY;
1721 sig[i++] = DBUS_TYPE_STRING;
1722 #ifdef HAVE_UNIX_FD_PASSING
1723 sig[i++] = DBUS_TYPE_UNIX_FD;
1725 sig[i++] = DBUS_TYPE_INVALID;
1727 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
1729 verify_test_message_args_ignored (message);
1730 verify_test_message_memleak (message);
1732 dbus_message_unref (message);
1734 /* Load all the sample messages from the message factory */
1736 DBusMessageDataIter diter;
1737 DBusMessageData mdata;
1740 reset_validities_seen ();
1743 _dbus_message_data_iter_init (&diter);
1745 while (_dbus_message_data_iter_get_and_next (&diter,
1748 if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
1749 mdata.expected_validity))
1751 _dbus_warn ("expected validity %d and did not get it\n",
1752 mdata.expected_validity);
1753 _dbus_assert_not_reached ("message data failed");
1756 _dbus_message_data_free (&mdata);
1761 printf ("%d sample messages tested\n", count);
1763 print_validities_seen (FALSE);
1764 print_validities_seen (TRUE);
1768 _dbus_check_fdleaks_leave (initial_fds);
1770 /* Now load every message in test_data_dir if we have one */
1771 if (test_data_dir == NULL)
1774 initial_fds = _dbus_check_fdleaks_enter ();
1776 if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
1777 (DBusForeachMessageFileFunc)
1778 dbus_internal_do_not_use_try_message_file,
1780 _dbus_assert_not_reached ("foreach_message_file test failed");
1782 _dbus_check_fdleaks_leave (initial_fds);
1787 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */