1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-internals.h"
26 #include "dbus-test.h"
27 #include "dbus-message-private.h"
28 #include "dbus-marshal-recursive.h"
29 #include "dbus-string.h"
32 * @addtogroup DBusMessage
36 #ifdef DBUS_BUILD_TESTS
38 * Reads arguments from a message iterator given a variable argument
39 * list. Only arguments of basic type and arrays of fixed-length
40 * basic type may be read with this function. See
41 * dbus_message_get_args() for more details.
43 * @param iter the message iterator
44 * @param error error to be filled in on failure
45 * @param first_arg_type the first argument type
46 * @param ... location for first argument value, then list of type-location pairs
47 * @returns #FALSE if the error was set
50 dbus_message_iter_get_args (DBusMessageIter *iter,
58 _dbus_return_val_if_fail (iter != NULL, FALSE);
59 _dbus_return_val_if_error_is_set (error, FALSE);
61 va_start (var_args, first_arg_type);
62 retval = _dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
67 #endif /* DBUS_BUILD_TESTS */
71 #ifdef DBUS_BUILD_TESTS
72 #include "dbus-test.h"
73 #include "dbus-message-factory.h"
77 static int validities_seen[DBUS_VALIDITY_LAST + _DBUS_NEGATIVE_VALIDITY_COUNT];
80 reset_validities_seen (void)
84 while (i < _DBUS_N_ELEMENTS (validities_seen))
86 validities_seen[i] = 0;
92 record_validity_seen (DBusValidity validity)
94 validities_seen[validity + _DBUS_NEGATIVE_VALIDITY_COUNT] += 1;
98 print_validities_seen (dbus_bool_t not_seen)
102 while (i < _DBUS_N_ELEMENTS (validities_seen))
104 if ((i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_VALIDITY_UNKNOWN ||
105 (i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_INVALID_FOR_UNKNOWN_REASON)
107 else if ((not_seen && validities_seen[i] == 0) ||
108 (!not_seen && validities_seen[i] > 0))
109 printf ("validity %3d seen %d times\n",
110 i - _DBUS_NEGATIVE_VALIDITY_COUNT,
117 check_memleaks (void)
121 if (_dbus_get_malloc_blocks_outstanding () != 0)
123 _dbus_warn ("%d dbus_malloc blocks were not freed in %s\n",
124 _dbus_get_malloc_blocks_outstanding (), __FILE__);
125 _dbus_assert_not_reached ("memleaks");
130 check_have_valid_message (DBusMessageLoader *loader)
132 DBusMessage *message;
138 if (_dbus_message_loader_get_is_corrupted (loader))
140 _dbus_warn ("loader corrupted on message that was expected to be valid; invalid reason %d\n",
141 loader->corruption_reason);
145 message = _dbus_message_loader_pop_message (loader);
148 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
152 if (_dbus_string_get_length (&loader->data) > 0)
154 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
160 /* Verify that we're able to properly deal with the message.
161 * For example, this would detect improper handling of messages
162 * in nonstandard byte order.
164 if (!check_message_handling (message))
168 record_validity_seen (DBUS_VALID);
174 dbus_message_unref (message);
180 check_invalid_message (DBusMessageLoader *loader,
181 DBusValidity expected_validity)
187 if (!_dbus_message_loader_get_is_corrupted (loader))
189 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
193 record_validity_seen (loader->corruption_reason);
195 if (expected_validity != DBUS_INVALID_FOR_UNKNOWN_REASON &&
196 loader->corruption_reason != expected_validity)
198 _dbus_warn ("expected message to be corrupted for reason %d and was corrupted for %d instead\n",
199 expected_validity, loader->corruption_reason);
210 check_incomplete_message (DBusMessageLoader *loader)
212 DBusMessage *message;
218 if (_dbus_message_loader_get_is_corrupted (loader))
220 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete), corruption reason %d\n",
221 loader->corruption_reason);
225 message = _dbus_message_loader_pop_message (loader);
228 _dbus_warn ("loaded message that was expected to be incomplete\n");
232 record_validity_seen (DBUS_VALID_BUT_INCOMPLETE);
237 dbus_message_unref (message);
242 check_loader_results (DBusMessageLoader *loader,
243 DBusValidity expected_validity)
245 if (!_dbus_message_loader_queue_messages (loader))
246 _dbus_assert_not_reached ("no memory to queue messages");
248 if (expected_validity == DBUS_VALID)
249 return check_have_valid_message (loader);
250 else if (expected_validity == DBUS_VALID_BUT_INCOMPLETE)
251 return check_incomplete_message (loader);
252 else if (expected_validity == DBUS_VALIDITY_UNKNOWN)
254 /* here we just know we didn't segfault and that was the
255 * only test. Also, we record that we got coverage
256 * for the validity reason.
258 if (_dbus_message_loader_get_is_corrupted (loader))
259 record_validity_seen (loader->corruption_reason);
264 return check_invalid_message (loader, expected_validity);
268 * Loads the message in the given message file.
270 * @param filename filename to load
271 * @param data string to load message into
272 * @returns #TRUE if the message was loaded
275 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
283 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
284 dbus_error_init (&error);
285 if (!_dbus_file_get_contents (data, filename, &error))
287 _dbus_warn ("Could not load message file %s: %s\n",
288 _dbus_string_get_const_data (filename),
290 dbus_error_free (&error);
302 * Tries loading the message in the given message file
303 * and verifies that DBusMessageLoader can handle it.
305 * @param filename filename to load
306 * @param expected_validity what the message has to be like to return #TRUE
307 * @returns #TRUE if the message has the expected validity
310 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
311 DBusValidity expected_validity)
318 if (!_dbus_string_init (&data))
319 _dbus_assert_not_reached ("could not allocate string\n");
321 if (!dbus_internal_do_not_use_load_message_file (filename, &data))
324 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
330 if (_dbus_string_get_length (&data) > 0)
331 _dbus_verbose_bytes_of_string (&data, 0,
332 _dbus_string_get_length (&data));
334 _dbus_warn ("Failed message loader test on %s\n",
335 _dbus_string_get_const_data (filename));
338 _dbus_string_free (&data);
344 * Tries loading the given message data.
347 * @param data the message data
348 * @param expected_validity what the message has to be like to return #TRUE
349 * @returns #TRUE if the message has the expected validity
352 dbus_internal_do_not_use_try_message_data (const DBusString *data,
353 DBusValidity expected_validity)
355 DBusMessageLoader *loader;
363 /* Write the data one byte at a time */
365 loader = _dbus_message_loader_new ();
367 /* check some trivial loader functions */
368 _dbus_message_loader_ref (loader);
369 _dbus_message_loader_unref (loader);
370 _dbus_message_loader_get_max_message_size (loader);
372 len = _dbus_string_get_length (data);
373 for (i = 0; i < len; i++)
377 _dbus_message_loader_get_buffer (loader, &buffer);
378 _dbus_string_append_byte (buffer,
379 _dbus_string_get_byte (data, i));
380 _dbus_message_loader_return_buffer (loader, buffer, 1);
383 if (!check_loader_results (loader, expected_validity))
386 _dbus_message_loader_unref (loader);
389 /* Write the data all at once */
391 loader = _dbus_message_loader_new ();
396 _dbus_message_loader_get_buffer (loader, &buffer);
397 _dbus_string_copy (data, 0, buffer,
398 _dbus_string_get_length (buffer));
399 _dbus_message_loader_return_buffer (loader, buffer, 1);
402 if (!check_loader_results (loader, expected_validity))
405 _dbus_message_loader_unref (loader);
408 /* Write the data 2 bytes at a time */
410 loader = _dbus_message_loader_new ();
412 len = _dbus_string_get_length (data);
413 for (i = 0; i < len; i += 2)
417 _dbus_message_loader_get_buffer (loader, &buffer);
418 _dbus_string_append_byte (buffer,
419 _dbus_string_get_byte (data, i));
421 _dbus_string_append_byte (buffer,
422 _dbus_string_get_byte (data, i+1));
423 _dbus_message_loader_return_buffer (loader, buffer, 1);
426 if (!check_loader_results (loader, expected_validity))
429 _dbus_message_loader_unref (loader);
437 _dbus_message_loader_unref (loader);
443 process_test_subdir (const DBusString *test_base_dir,
445 DBusValidity expected_validity,
446 DBusForeachMessageFileFunc function,
449 DBusString test_directory;
458 if (!_dbus_string_init (&test_directory))
459 _dbus_assert_not_reached ("didn't allocate test_directory\n");
461 _dbus_string_init_const (&filename, subdir);
463 if (!_dbus_string_copy (test_base_dir, 0,
465 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
467 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
468 _dbus_assert_not_reached ("couldn't allocate full path");
470 _dbus_string_free (&filename);
471 if (!_dbus_string_init (&filename))
472 _dbus_assert_not_reached ("didn't allocate filename string\n");
474 dbus_error_init (&error);
475 dir = _dbus_directory_open (&test_directory, &error);
478 _dbus_warn ("Could not open %s: %s\n",
479 _dbus_string_get_const_data (&test_directory),
481 dbus_error_free (&error);
485 printf ("Testing %s:\n", subdir);
488 while (_dbus_directory_get_next_file (dir, &filename, &error))
490 DBusString full_path;
492 if (!_dbus_string_init (&full_path))
493 _dbus_assert_not_reached ("couldn't init string");
495 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
496 _dbus_assert_not_reached ("couldn't copy dir to full_path");
498 if (!_dbus_concat_dir_and_file (&full_path, &filename))
499 _dbus_assert_not_reached ("couldn't concat file to dir");
501 if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
505 if (_dbus_string_ends_with_c_str (&filename, ".message"))
507 _dbus_warn ("Could not load %s, message builder language no longer supported\n",
508 _dbus_string_get_const_data (&filename));
511 _dbus_verbose ("Skipping non-.message file %s\n",
512 _dbus_string_get_const_data (&filename));
513 _dbus_string_free (&full_path);
518 _dbus_string_get_const_data (&filename));
520 if (! (*function) (&full_path,
521 expected_validity, user_data))
523 _dbus_string_free (&full_path);
527 _dbus_string_free (&full_path);
530 if (dbus_error_is_set (&error))
532 _dbus_warn ("Could not get next file in %s: %s\n",
533 _dbus_string_get_const_data (&test_directory),
535 dbus_error_free (&error);
544 _dbus_directory_close (dir);
545 _dbus_string_free (&test_directory);
546 _dbus_string_free (&filename);
552 * Runs the given function on every message file in the test suite.
553 * The function should return #FALSE on test failure or fatal error.
555 * @param test_data_dir root dir of the test suite data files (top_srcdir/test/data)
556 * @param func the function to run
557 * @param user_data data for function
558 * @returns #FALSE if there's a failure
561 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
562 DBusForeachMessageFileFunc func,
565 DBusString test_directory;
570 _dbus_string_init_const (&test_directory, test_data_dir);
572 if (!process_test_subdir (&test_directory, "valid-messages",
573 DBUS_VALID, func, user_data))
578 if (!process_test_subdir (&test_directory, "invalid-messages",
579 DBUS_INVALID_FOR_UNKNOWN_REASON, func, user_data))
584 if (!process_test_subdir (&test_directory, "incomplete-messages",
585 DBUS_VALID_BUT_INCOMPLETE, func, user_data))
594 _dbus_string_free (&test_directory);
600 #define GET_AND_CHECK(iter, typename, literal) \
602 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
603 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
604 dbus_message_iter_get_basic (&iter, &v_##typename); \
605 if (v_##typename != literal) \
606 _dbus_assert_not_reached ("got wrong value from message iter"); \
609 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
611 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
612 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
613 dbus_message_iter_get_basic (&iter, &v_##typename); \
614 if (strcmp (v_##typename, literal) != 0) \
615 _dbus_assert_not_reached ("got wrong value from message iter"); \
618 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
620 GET_AND_CHECK(iter, typename, literal); \
621 if (!dbus_message_iter_next (&iter)) \
622 _dbus_assert_not_reached ("failed to move iter to next"); \
625 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
627 GET_AND_CHECK_STRCMP(iter, typename, literal); \
628 if (!dbus_message_iter_next (&iter)) \
629 _dbus_assert_not_reached ("failed to move iter to next"); \
633 message_iter_test (DBusMessage *message)
635 DBusMessageIter iter, array, array2;
636 const char *v_STRING;
638 dbus_int16_t v_INT16;
639 dbus_uint16_t v_UINT16;
640 dbus_int32_t v_INT32;
641 dbus_uint32_t v_UINT32;
642 #ifdef DBUS_HAVE_INT64
643 dbus_int64_t v_INT64;
644 dbus_uint64_t v_UINT64;
646 unsigned char v_BYTE;
647 dbus_bool_t v_BOOLEAN;
649 const dbus_int32_t *our_int_array;
652 dbus_message_iter_init (message, &iter);
654 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
655 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
656 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
657 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
659 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
660 _dbus_assert_not_reached ("Argument type not an array");
662 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
663 _dbus_assert_not_reached ("Array type not double");
665 dbus_message_iter_recurse (&iter, &array);
667 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
668 GET_AND_CHECK (array, DOUBLE, 2.5);
670 if (dbus_message_iter_next (&array))
671 _dbus_assert_not_reached ("Didn't reach end of array");
673 if (!dbus_message_iter_next (&iter))
674 _dbus_assert_not_reached ("Reached end of arguments");
676 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
678 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
679 _dbus_assert_not_reached ("no array");
681 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
682 _dbus_assert_not_reached ("Array type not int32");
685 dbus_message_iter_recurse (&iter, &array);
687 if (dbus_message_iter_next (&array))
688 _dbus_assert_not_reached ("Didn't reach end of array");
690 if (!dbus_message_iter_next (&iter))
691 _dbus_assert_not_reached ("Reached end of arguments");
693 GET_AND_CHECK (iter, BYTE, 0xF0);
695 if (dbus_message_iter_next (&iter))
696 _dbus_assert_not_reached ("Didn't reach end of arguments");
701 verify_test_message (DBusMessage *message)
703 DBusMessageIter iter;
705 dbus_int16_t our_int16;
706 dbus_uint16_t our_uint16;
707 dbus_int32_t our_int;
708 dbus_uint32_t our_uint;
712 dbus_bool_t our_bool;
713 unsigned char our_byte_1, our_byte_2;
714 const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
715 int our_uint32_array_len;
716 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
717 int our_int32_array_len;
718 #ifdef DBUS_HAVE_INT64
719 dbus_int64_t our_int64;
720 dbus_uint64_t our_uint64;
721 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
722 int our_uint64_array_len;
723 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
724 int our_int64_array_len;
726 const double *our_double_array = (void*)0xdeadbeef;
727 int our_double_array_len;
728 const unsigned char *our_byte_array = (void*)0xdeadbeef;
729 int our_byte_array_len;
730 const dbus_bool_t *our_boolean_array = (void*)0xdeadbeef;
731 int our_boolean_array_len;
732 char **our_string_array;
733 int our_string_array_len;
735 dbus_message_iter_init (message, &iter);
737 dbus_error_init (&error);
738 if (!dbus_message_iter_get_args (&iter, &error,
739 DBUS_TYPE_INT16, &our_int16,
740 DBUS_TYPE_UINT16, &our_uint16,
741 DBUS_TYPE_INT32, &our_int,
742 DBUS_TYPE_UINT32, &our_uint,
743 #ifdef DBUS_HAVE_INT64
744 DBUS_TYPE_INT64, &our_int64,
745 DBUS_TYPE_UINT64, &our_uint64,
747 DBUS_TYPE_STRING, &our_str,
748 DBUS_TYPE_DOUBLE, &our_double,
749 DBUS_TYPE_BOOLEAN, &our_bool,
750 DBUS_TYPE_BYTE, &our_byte_1,
751 DBUS_TYPE_BYTE, &our_byte_2,
752 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
753 &our_uint32_array, &our_uint32_array_len,
754 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
755 &our_int32_array, &our_int32_array_len,
756 #ifdef DBUS_HAVE_INT64
757 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
758 &our_uint64_array, &our_uint64_array_len,
759 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
760 &our_int64_array, &our_int64_array_len,
762 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
763 &our_double_array, &our_double_array_len,
764 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
765 &our_byte_array, &our_byte_array_len,
766 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
767 &our_boolean_array, &our_boolean_array_len,
768 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
769 &our_string_array, &our_string_array_len,
772 _dbus_warn ("error: %s - %s\n", error.name,
773 (error.message != NULL) ? error.message : "no message");
774 _dbus_assert_not_reached ("Could not get arguments");
777 if (our_int16 != -0x123)
778 _dbus_assert_not_reached ("16-bit integers differ!");
780 if (our_uint16 != 0x123)
781 _dbus_assert_not_reached ("16-bit uints differ!");
783 if (our_int != -0x12345678)
784 _dbus_assert_not_reached ("integers differ!");
786 if (our_uint != 0x12300042)
787 _dbus_assert_not_reached ("uints differ!");
789 #ifdef DBUS_HAVE_INT64
790 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
791 _dbus_assert_not_reached ("64-bit integers differ!");
792 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
793 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
797 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double, v_DOUBLE))
798 _dbus_assert_not_reached ("doubles differ!");
800 if (strcmp (our_str, "Test string") != 0)
801 _dbus_assert_not_reached ("strings differ!");
804 _dbus_assert_not_reached ("booleans differ");
806 if (our_byte_1 != 42)
807 _dbus_assert_not_reached ("bytes differ!");
809 if (our_byte_2 != 24)
810 _dbus_assert_not_reached ("bytes differ!");
812 if (our_uint32_array_len != 4 ||
813 our_uint32_array[0] != 0x12345678 ||
814 our_uint32_array[1] != 0x23456781 ||
815 our_uint32_array[2] != 0x34567812 ||
816 our_uint32_array[3] != 0x45678123)
817 _dbus_assert_not_reached ("uint array differs");
819 if (our_int32_array_len != 4 ||
820 our_int32_array[0] != 0x12345678 ||
821 our_int32_array[1] != -0x23456781 ||
822 our_int32_array[2] != 0x34567812 ||
823 our_int32_array[3] != -0x45678123)
824 _dbus_assert_not_reached ("int array differs");
826 #ifdef DBUS_HAVE_INT64
827 if (our_uint64_array_len != 4 ||
828 our_uint64_array[0] != 0x12345678 ||
829 our_uint64_array[1] != 0x23456781 ||
830 our_uint64_array[2] != 0x34567812 ||
831 our_uint64_array[3] != 0x45678123)
832 _dbus_assert_not_reached ("uint64 array differs");
834 if (our_int64_array_len != 4 ||
835 our_int64_array[0] != 0x12345678 ||
836 our_int64_array[1] != -0x23456781 ||
837 our_int64_array[2] != 0x34567812 ||
838 our_int64_array[3] != -0x45678123)
839 _dbus_assert_not_reached ("int64 array differs");
840 #endif /* DBUS_HAVE_INT64 */
842 if (our_double_array_len != 3)
843 _dbus_assert_not_reached ("double array had wrong length");
845 /* On all IEEE machines (i.e. everything sane) exact equality
846 * should be preserved over the wire
849 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[0], v_DOUBLE))
850 _dbus_assert_not_reached ("double array had wrong values");
851 v_DOUBLE = 9876.54321;
852 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[1], v_DOUBLE))
853 _dbus_assert_not_reached ("double array had wrong values");
855 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[2], v_DOUBLE))
856 _dbus_assert_not_reached ("double array had wrong values");
858 if (our_byte_array_len != 4)
859 _dbus_assert_not_reached ("byte array had wrong length");
861 if (our_byte_array[0] != 'a' ||
862 our_byte_array[1] != 'b' ||
863 our_byte_array[2] != 'c' ||
864 our_byte_array[3] != 234)
865 _dbus_assert_not_reached ("byte array had wrong values");
867 if (our_boolean_array_len != 5)
868 _dbus_assert_not_reached ("bool array had wrong length");
870 if (our_boolean_array[0] != TRUE ||
871 our_boolean_array[1] != FALSE ||
872 our_boolean_array[2] != TRUE ||
873 our_boolean_array[3] != TRUE ||
874 our_boolean_array[4] != FALSE)
875 _dbus_assert_not_reached ("bool array had wrong values");
877 if (our_string_array_len != 4)
878 _dbus_assert_not_reached ("string array was wrong length");
880 if (strcmp (our_string_array[0], "Foo") != 0 ||
881 strcmp (our_string_array[1], "bar") != 0 ||
882 strcmp (our_string_array[2], "") != 0 ||
883 strcmp (our_string_array[3], "woo woo woo woo") != 0)
884 _dbus_assert_not_reached ("string array had wrong values");
886 dbus_free_string_array (our_string_array);
888 if (dbus_message_iter_next (&iter))
889 _dbus_assert_not_reached ("Didn't reach end of arguments");
893 * @ingroup DBusMessageInternals
894 * Unit test for DBusMessage.
896 * @returns #TRUE on success.
899 _dbus_message_test (const char *test_data_dir)
901 DBusMessage *message;
902 DBusMessageLoader *loader;
908 const dbus_uint32_t our_uint32_array[] =
909 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
910 const dbus_int32_t our_int32_array[] =
911 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
912 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
913 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
914 #ifdef DBUS_HAVE_INT64
915 const dbus_uint64_t our_uint64_array[] =
916 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
917 const dbus_int64_t our_int64_array[] =
918 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
919 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
920 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
922 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
923 const char **v_ARRAY_STRING = our_string_array;
924 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
925 const double *v_ARRAY_DOUBLE = our_double_array;
926 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
927 const unsigned char *v_ARRAY_BYTE = our_byte_array;
928 const dbus_bool_t our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
929 const dbus_bool_t *v_ARRAY_BOOLEAN = our_boolean_array;
932 const char *v_STRING;
934 dbus_int16_t v_INT16;
935 dbus_uint16_t v_UINT16;
936 dbus_int32_t v_INT32;
937 dbus_uint32_t v_UINT32;
938 #ifdef DBUS_HAVE_INT64
939 dbus_int64_t v_INT64;
940 dbus_uint64_t v_UINT64;
942 unsigned char v_BYTE;
943 unsigned char v2_BYTE;
944 dbus_bool_t v_BOOLEAN;
946 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
947 "/org/freedesktop/TestPath",
950 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
951 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
953 _dbus_assert (strcmp (dbus_message_get_path (message),
954 "/org/freedesktop/TestPath") == 0);
955 _dbus_message_set_serial (message, 1234);
957 /* string length including nul byte not a multiple of 4 */
958 if (!dbus_message_set_sender (message, "org.foo.bar1"))
959 _dbus_assert_not_reached ("out of memory");
961 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
962 dbus_message_set_reply_serial (message, 5678);
964 _dbus_verbose_bytes_of_string (&message->header.data, 0,
965 _dbus_string_get_length (&message->header.data));
966 _dbus_verbose_bytes_of_string (&message->body, 0,
967 _dbus_string_get_length (&message->body));
969 if (!dbus_message_set_sender (message, NULL))
970 _dbus_assert_not_reached ("out of memory");
973 _dbus_verbose_bytes_of_string (&message->header.data, 0,
974 _dbus_string_get_length (&message->header.data));
975 _dbus_verbose_bytes_of_string (&message->body, 0,
976 _dbus_string_get_length (&message->body));
979 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
980 _dbus_assert (dbus_message_get_serial (message) == 1234);
981 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
982 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
984 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
985 dbus_message_set_no_reply (message, TRUE);
986 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
987 dbus_message_set_no_reply (message, FALSE);
988 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
990 /* Set/get some header fields */
992 if (!dbus_message_set_path (message, "/foo"))
993 _dbus_assert_not_reached ("out of memory");
994 _dbus_assert (strcmp (dbus_message_get_path (message),
997 if (!dbus_message_set_interface (message, "org.Foo"))
998 _dbus_assert_not_reached ("out of memory");
999 _dbus_assert (strcmp (dbus_message_get_interface (message),
1002 if (!dbus_message_set_member (message, "Bar"))
1003 _dbus_assert_not_reached ("out of memory");
1004 _dbus_assert (strcmp (dbus_message_get_member (message),
1007 /* Set/get them with longer values */
1008 if (!dbus_message_set_path (message, "/foo/bar"))
1009 _dbus_assert_not_reached ("out of memory");
1010 _dbus_assert (strcmp (dbus_message_get_path (message),
1013 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
1014 _dbus_assert_not_reached ("out of memory");
1015 _dbus_assert (strcmp (dbus_message_get_interface (message),
1016 "org.Foo.Bar") == 0);
1018 if (!dbus_message_set_member (message, "BarFoo"))
1019 _dbus_assert_not_reached ("out of memory");
1020 _dbus_assert (strcmp (dbus_message_get_member (message),
1023 /* Realloc shorter again */
1025 if (!dbus_message_set_path (message, "/foo"))
1026 _dbus_assert_not_reached ("out of memory");
1027 _dbus_assert (strcmp (dbus_message_get_path (message),
1030 if (!dbus_message_set_interface (message, "org.Foo"))
1031 _dbus_assert_not_reached ("out of memory");
1032 _dbus_assert (strcmp (dbus_message_get_interface (message),
1035 if (!dbus_message_set_member (message, "Bar"))
1036 _dbus_assert_not_reached ("out of memory");
1037 _dbus_assert (strcmp (dbus_message_get_member (message),
1040 dbus_message_unref (message);
1042 /* Test the vararg functions */
1043 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
1044 "/org/freedesktop/TestPath",
1045 "Foo.TestInterface",
1047 _dbus_message_set_serial (message, 1);
1048 dbus_message_set_reply_serial (message, 5678);
1052 v_INT32 = -0x12345678;
1053 v_UINT32 = 0x12300042;
1054 #ifdef DBUS_HAVE_INT64
1055 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
1056 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
1058 v_STRING = "Test string";
1064 dbus_message_append_args (message,
1065 DBUS_TYPE_INT16, &v_INT16,
1066 DBUS_TYPE_UINT16, &v_UINT16,
1067 DBUS_TYPE_INT32, &v_INT32,
1068 DBUS_TYPE_UINT32, &v_UINT32,
1069 #ifdef DBUS_HAVE_INT64
1070 DBUS_TYPE_INT64, &v_INT64,
1071 DBUS_TYPE_UINT64, &v_UINT64,
1073 DBUS_TYPE_STRING, &v_STRING,
1074 DBUS_TYPE_DOUBLE, &v_DOUBLE,
1075 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
1076 DBUS_TYPE_BYTE, &v_BYTE,
1077 DBUS_TYPE_BYTE, &v2_BYTE,
1078 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
1079 _DBUS_N_ELEMENTS (our_uint32_array),
1080 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
1081 _DBUS_N_ELEMENTS (our_int32_array),
1082 #ifdef DBUS_HAVE_INT64
1083 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
1084 _DBUS_N_ELEMENTS (our_uint64_array),
1085 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
1086 _DBUS_N_ELEMENTS (our_int64_array),
1088 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
1089 _DBUS_N_ELEMENTS (our_double_array),
1090 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
1091 _DBUS_N_ELEMENTS (our_byte_array),
1092 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
1093 _DBUS_N_ELEMENTS (our_boolean_array),
1094 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
1095 _DBUS_N_ELEMENTS (our_string_array),
1099 sig[i++] = DBUS_TYPE_INT16;
1100 sig[i++] = DBUS_TYPE_UINT16;
1101 sig[i++] = DBUS_TYPE_INT32;
1102 sig[i++] = DBUS_TYPE_UINT32;
1103 #ifdef DBUS_HAVE_INT64
1104 sig[i++] = DBUS_TYPE_INT64;
1105 sig[i++] = DBUS_TYPE_UINT64;
1107 sig[i++] = DBUS_TYPE_STRING;
1108 sig[i++] = DBUS_TYPE_DOUBLE;
1109 sig[i++] = DBUS_TYPE_BOOLEAN;
1110 sig[i++] = DBUS_TYPE_BYTE;
1111 sig[i++] = DBUS_TYPE_BYTE;
1112 sig[i++] = DBUS_TYPE_ARRAY;
1113 sig[i++] = DBUS_TYPE_UINT32;
1114 sig[i++] = DBUS_TYPE_ARRAY;
1115 sig[i++] = DBUS_TYPE_INT32;
1116 #ifdef DBUS_HAVE_INT64
1117 sig[i++] = DBUS_TYPE_ARRAY;
1118 sig[i++] = DBUS_TYPE_UINT64;
1119 sig[i++] = DBUS_TYPE_ARRAY;
1120 sig[i++] = DBUS_TYPE_INT64;
1122 sig[i++] = DBUS_TYPE_ARRAY;
1123 sig[i++] = DBUS_TYPE_DOUBLE;
1124 sig[i++] = DBUS_TYPE_ARRAY;
1125 sig[i++] = DBUS_TYPE_BYTE;
1126 sig[i++] = DBUS_TYPE_ARRAY;
1127 sig[i++] = DBUS_TYPE_BOOLEAN;
1128 sig[i++] = DBUS_TYPE_ARRAY;
1129 sig[i++] = DBUS_TYPE_STRING;
1130 sig[i++] = DBUS_TYPE_INVALID;
1132 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
1134 _dbus_verbose ("HEADER\n");
1135 _dbus_verbose_bytes_of_string (&message->header.data, 0,
1136 _dbus_string_get_length (&message->header.data));
1137 _dbus_verbose ("BODY\n");
1138 _dbus_verbose_bytes_of_string (&message->body, 0,
1139 _dbus_string_get_length (&message->body));
1141 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
1142 sig, dbus_message_get_signature (message));
1144 s = dbus_message_get_signature (message);
1146 _dbus_assert (dbus_message_has_signature (message, sig));
1147 _dbus_assert (strcmp (s, sig) == 0);
1149 verify_test_message (message);
1151 copy = dbus_message_copy (message);
1153 _dbus_assert (dbus_message_get_reply_serial (message) ==
1154 dbus_message_get_reply_serial (copy));
1155 _dbus_assert (message->header.padding == copy->header.padding);
1157 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
1158 _dbus_string_get_length (©->header.data));
1160 _dbus_assert (_dbus_string_get_length (&message->body) ==
1161 _dbus_string_get_length (©->body));
1163 verify_test_message (copy);
1165 name1 = dbus_message_get_interface (message);
1166 name2 = dbus_message_get_interface (copy);
1168 _dbus_assert (strcmp (name1, name2) == 0);
1170 name1 = dbus_message_get_member (message);
1171 name2 = dbus_message_get_member (copy);
1173 _dbus_assert (strcmp (name1, name2) == 0);
1175 dbus_message_unref (copy);
1177 /* Message loader test */
1178 _dbus_message_lock (message);
1179 loader = _dbus_message_loader_new ();
1181 /* check ref/unref */
1182 _dbus_message_loader_ref (loader);
1183 _dbus_message_loader_unref (loader);
1185 /* Write the header data one byte at a time */
1186 data = _dbus_string_get_const_data (&message->header.data);
1187 for (i = 0; i < _dbus_string_get_length (&message->header.data); i++)
1191 _dbus_message_loader_get_buffer (loader, &buffer);
1192 _dbus_string_append_byte (buffer, data[i]);
1193 _dbus_message_loader_return_buffer (loader, buffer, 1);
1196 /* Write the body data one byte at a time */
1197 data = _dbus_string_get_const_data (&message->body);
1198 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
1202 _dbus_message_loader_get_buffer (loader, &buffer);
1203 _dbus_string_append_byte (buffer, data[i]);
1204 _dbus_message_loader_return_buffer (loader, buffer, 1);
1207 dbus_message_unref (message);
1209 /* Now pop back the message */
1210 if (!_dbus_message_loader_queue_messages (loader))
1211 _dbus_assert_not_reached ("no memory to queue messages");
1213 if (_dbus_message_loader_get_is_corrupted (loader))
1214 _dbus_assert_not_reached ("message loader corrupted");
1216 message = _dbus_message_loader_pop_message (loader);
1218 _dbus_assert_not_reached ("received a NULL message");
1220 if (dbus_message_get_reply_serial (message) != 5678)
1221 _dbus_assert_not_reached ("reply serial fields differ");
1223 verify_test_message (message);
1225 dbus_message_unref (message);
1226 _dbus_message_loader_unref (loader);
1230 /* Load all the sample messages from the message factory */
1232 DBusMessageDataIter diter;
1233 DBusMessageData mdata;
1236 reset_validities_seen ();
1239 _dbus_message_data_iter_init (&diter);
1241 while (_dbus_message_data_iter_get_and_next (&diter,
1244 if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
1245 mdata.expected_validity))
1247 _dbus_warn ("expected validity %d and did not get it\n",
1248 mdata.expected_validity);
1249 _dbus_assert_not_reached ("message data failed");
1252 _dbus_message_data_free (&mdata);
1257 printf ("%d sample messages tested\n", count);
1259 print_validities_seen (FALSE);
1260 print_validities_seen (TRUE);
1265 /* Now load every message in test_data_dir if we have one */
1266 if (test_data_dir == NULL)
1269 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
1270 (DBusForeachMessageFileFunc)
1271 dbus_internal_do_not_use_try_message_file,
1275 #endif /* DBUS_BUILD_TESTS */