00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "dbus-internals.h"
00026 #include "dbus-test.h"
00027 #include "dbus-message-private.h"
00028 #include "dbus-marshal-recursive.h"
00029 #include "dbus-string.h"
00030
00036 #ifdef DBUS_BUILD_TESTS
00037
00049 static dbus_bool_t
00050 dbus_message_iter_get_args (DBusMessageIter *iter,
00051 DBusError *error,
00052 int first_arg_type,
00053 ...)
00054 {
00055 dbus_bool_t retval;
00056 va_list var_args;
00057
00058 _dbus_return_val_if_fail (iter != NULL, FALSE);
00059 _dbus_return_val_if_error_is_set (error, FALSE);
00060
00061 va_start (var_args, first_arg_type);
00062 retval = _dbus_message_iter_get_args_valist (iter, error, first_arg_type, var_args);
00063 va_end (var_args);
00064
00065 return retval;
00066 }
00067 #endif
00068
00071 #ifdef DBUS_BUILD_TESTS
00072 #include "dbus-test.h"
00073 #include "dbus-message-factory.h"
00074 #include <stdio.h>
00075 #include <stdlib.h>
00076
00077 static int validities_seen[DBUS_VALIDITY_LAST + _DBUS_NEGATIVE_VALIDITY_COUNT];
00078
00079 static void
00080 reset_validities_seen (void)
00081 {
00082 int i;
00083 i = 0;
00084 while (i < _DBUS_N_ELEMENTS (validities_seen))
00085 {
00086 validities_seen[i] = 0;
00087 ++i;
00088 }
00089 }
00090
00091 static void
00092 record_validity_seen (DBusValidity validity)
00093 {
00094 validities_seen[validity + _DBUS_NEGATIVE_VALIDITY_COUNT] += 1;
00095 }
00096
00097 static void
00098 print_validities_seen (dbus_bool_t not_seen)
00099 {
00100 int i;
00101 i = 0;
00102 while (i < _DBUS_N_ELEMENTS (validities_seen))
00103 {
00104 if ((i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_VALIDITY_UNKNOWN ||
00105 (i - _DBUS_NEGATIVE_VALIDITY_COUNT) == DBUS_INVALID_FOR_UNKNOWN_REASON)
00106 ;
00107 else if ((not_seen && validities_seen[i] == 0) ||
00108 (!not_seen && validities_seen[i] > 0))
00109 printf ("validity %3d seen %d times\n",
00110 i - _DBUS_NEGATIVE_VALIDITY_COUNT,
00111 validities_seen[i]);
00112 ++i;
00113 }
00114 }
00115
00116 static void
00117 check_memleaks (void)
00118 {
00119 dbus_shutdown ();
00120
00121 if (_dbus_get_malloc_blocks_outstanding () != 0)
00122 {
00123 _dbus_warn ("%d dbus_malloc blocks were not freed in %s\n",
00124 _dbus_get_malloc_blocks_outstanding (), __FILE__);
00125 _dbus_assert_not_reached ("memleaks");
00126 }
00127 }
00128
00129 static dbus_bool_t
00130 check_have_valid_message (DBusMessageLoader *loader)
00131 {
00132 DBusMessage *message;
00133 dbus_bool_t retval;
00134
00135 message = NULL;
00136 retval = FALSE;
00137
00138 if (_dbus_message_loader_get_is_corrupted (loader))
00139 {
00140 _dbus_warn ("loader corrupted on message that was expected to be valid; invalid reason %d\n",
00141 loader->corruption_reason);
00142 goto failed;
00143 }
00144
00145 message = _dbus_message_loader_pop_message (loader);
00146 if (message == NULL)
00147 {
00148 _dbus_warn ("didn't load message that was expected to be valid (message not popped)\n");
00149 goto failed;
00150 }
00151
00152 if (_dbus_string_get_length (&loader->data) > 0)
00153 {
00154 _dbus_warn ("had leftover bytes from expected-to-be-valid single message\n");
00155 goto failed;
00156 }
00157
00158 #if 0
00159
00160
00161
00162
00163
00164 if (!check_message_handling (message))
00165 goto failed;
00166 #endif
00167
00168 record_validity_seen (DBUS_VALID);
00169
00170 retval = TRUE;
00171
00172 failed:
00173 if (message)
00174 dbus_message_unref (message);
00175
00176 return retval;
00177 }
00178
00179 static dbus_bool_t
00180 check_invalid_message (DBusMessageLoader *loader,
00181 DBusValidity expected_validity)
00182 {
00183 dbus_bool_t retval;
00184
00185 retval = FALSE;
00186
00187 if (!_dbus_message_loader_get_is_corrupted (loader))
00188 {
00189 _dbus_warn ("loader not corrupted on message that was expected to be invalid\n");
00190 goto failed;
00191 }
00192
00193 record_validity_seen (loader->corruption_reason);
00194
00195 if (expected_validity != DBUS_INVALID_FOR_UNKNOWN_REASON &&
00196 loader->corruption_reason != expected_validity)
00197 {
00198 _dbus_warn ("expected message to be corrupted for reason %d and was corrupted for %d instead\n",
00199 expected_validity, loader->corruption_reason);
00200 goto failed;
00201 }
00202
00203 retval = TRUE;
00204
00205 failed:
00206 return retval;
00207 }
00208
00209 static dbus_bool_t
00210 check_incomplete_message (DBusMessageLoader *loader)
00211 {
00212 DBusMessage *message;
00213 dbus_bool_t retval;
00214
00215 message = NULL;
00216 retval = FALSE;
00217
00218 if (_dbus_message_loader_get_is_corrupted (loader))
00219 {
00220 _dbus_warn ("loader corrupted on message that was expected to be valid (but incomplete), corruption reason %d\n",
00221 loader->corruption_reason);
00222 goto failed;
00223 }
00224
00225 message = _dbus_message_loader_pop_message (loader);
00226 if (message != NULL)
00227 {
00228 _dbus_warn ("loaded message that was expected to be incomplete\n");
00229 goto failed;
00230 }
00231
00232 record_validity_seen (DBUS_VALID_BUT_INCOMPLETE);
00233 retval = TRUE;
00234
00235 failed:
00236 if (message)
00237 dbus_message_unref (message);
00238 return retval;
00239 }
00240
00241 static dbus_bool_t
00242 check_loader_results (DBusMessageLoader *loader,
00243 DBusValidity expected_validity)
00244 {
00245 if (!_dbus_message_loader_queue_messages (loader))
00246 _dbus_assert_not_reached ("no memory to queue messages");
00247
00248 if (expected_validity == DBUS_VALID)
00249 return check_have_valid_message (loader);
00250 else if (expected_validity == DBUS_VALID_BUT_INCOMPLETE)
00251 return check_incomplete_message (loader);
00252 else if (expected_validity == DBUS_VALIDITY_UNKNOWN)
00253 {
00254
00255
00256
00257
00258 if (_dbus_message_loader_get_is_corrupted (loader))
00259 record_validity_seen (loader->corruption_reason);
00260
00261 return TRUE;
00262 }
00263 else
00264 return check_invalid_message (loader, expected_validity);
00265 }
00266
00274 dbus_bool_t
00275 dbus_internal_do_not_use_load_message_file (const DBusString *filename,
00276 DBusString *data)
00277 {
00278 dbus_bool_t retval;
00279 DBusError error = DBUS_ERROR_INIT;
00280
00281 retval = FALSE;
00282
00283 _dbus_verbose ("Loading raw %s\n", _dbus_string_get_const_data (filename));
00284 if (!_dbus_file_get_contents (data, filename, &error))
00285 {
00286 _dbus_warn ("Could not load message file %s: %s\n",
00287 _dbus_string_get_const_data (filename),
00288 error.message);
00289 dbus_error_free (&error);
00290 goto failed;
00291 }
00292
00293 retval = TRUE;
00294
00295 failed:
00296
00297 return retval;
00298 }
00299
00308 dbus_bool_t
00309 dbus_internal_do_not_use_try_message_file (const DBusString *filename,
00310 DBusValidity expected_validity)
00311 {
00312 DBusString data;
00313 dbus_bool_t retval;
00314
00315 retval = FALSE;
00316
00317 if (!_dbus_string_init (&data))
00318 _dbus_assert_not_reached ("could not allocate string\n");
00319
00320 if (!dbus_internal_do_not_use_load_message_file (filename, &data))
00321 goto failed;
00322
00323 retval = dbus_internal_do_not_use_try_message_data (&data, expected_validity);
00324
00325 failed:
00326
00327 if (!retval)
00328 {
00329 if (_dbus_string_get_length (&data) > 0)
00330 _dbus_verbose_bytes_of_string (&data, 0,
00331 _dbus_string_get_length (&data));
00332
00333 _dbus_warn ("Failed message loader test on %s\n",
00334 _dbus_string_get_const_data (filename));
00335 }
00336
00337 _dbus_string_free (&data);
00338
00339 return retval;
00340 }
00341
00350 dbus_bool_t
00351 dbus_internal_do_not_use_try_message_data (const DBusString *data,
00352 DBusValidity expected_validity)
00353 {
00354 DBusMessageLoader *loader;
00355 dbus_bool_t retval;
00356 int len;
00357 int i;
00358
00359 loader = NULL;
00360 retval = FALSE;
00361
00362
00363
00364 loader = _dbus_message_loader_new ();
00365
00366
00367 _dbus_message_loader_ref (loader);
00368 _dbus_message_loader_unref (loader);
00369 _dbus_message_loader_get_max_message_size (loader);
00370
00371 len = _dbus_string_get_length (data);
00372 for (i = 0; i < len; i++)
00373 {
00374 DBusString *buffer;
00375
00376 _dbus_message_loader_get_buffer (loader, &buffer);
00377 _dbus_string_append_byte (buffer,
00378 _dbus_string_get_byte (data, i));
00379 _dbus_message_loader_return_buffer (loader, buffer, 1);
00380 }
00381
00382 if (!check_loader_results (loader, expected_validity))
00383 goto failed;
00384
00385 _dbus_message_loader_unref (loader);
00386 loader = NULL;
00387
00388
00389
00390 loader = _dbus_message_loader_new ();
00391
00392 {
00393 DBusString *buffer;
00394
00395 _dbus_message_loader_get_buffer (loader, &buffer);
00396 _dbus_string_copy (data, 0, buffer,
00397 _dbus_string_get_length (buffer));
00398 _dbus_message_loader_return_buffer (loader, buffer, 1);
00399 }
00400
00401 if (!check_loader_results (loader, expected_validity))
00402 goto failed;
00403
00404 _dbus_message_loader_unref (loader);
00405 loader = NULL;
00406
00407
00408
00409 loader = _dbus_message_loader_new ();
00410
00411 len = _dbus_string_get_length (data);
00412 for (i = 0; i < len; i += 2)
00413 {
00414 DBusString *buffer;
00415
00416 _dbus_message_loader_get_buffer (loader, &buffer);
00417 _dbus_string_append_byte (buffer,
00418 _dbus_string_get_byte (data, i));
00419 if ((i+1) < len)
00420 _dbus_string_append_byte (buffer,
00421 _dbus_string_get_byte (data, i+1));
00422 _dbus_message_loader_return_buffer (loader, buffer, 1);
00423 }
00424
00425 if (!check_loader_results (loader, expected_validity))
00426 goto failed;
00427
00428 _dbus_message_loader_unref (loader);
00429 loader = NULL;
00430
00431 retval = TRUE;
00432
00433 failed:
00434
00435 if (loader)
00436 _dbus_message_loader_unref (loader);
00437
00438 return retval;
00439 }
00440
00441 static dbus_bool_t
00442 process_test_subdir (const DBusString *test_base_dir,
00443 const char *subdir,
00444 DBusValidity expected_validity,
00445 DBusForeachMessageFileFunc function,
00446 void *user_data)
00447 {
00448 DBusString test_directory;
00449 DBusString filename;
00450 DBusDirIter *dir;
00451 dbus_bool_t retval;
00452 DBusError error = DBUS_ERROR_INIT;
00453
00454 retval = FALSE;
00455 dir = NULL;
00456
00457 if (!_dbus_string_init (&test_directory))
00458 _dbus_assert_not_reached ("didn't allocate test_directory\n");
00459
00460 _dbus_string_init_const (&filename, subdir);
00461
00462 if (!_dbus_string_copy (test_base_dir, 0,
00463 &test_directory, 0))
00464 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
00465
00466 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
00467 _dbus_assert_not_reached ("couldn't allocate full path");
00468
00469 _dbus_string_free (&filename);
00470 if (!_dbus_string_init (&filename))
00471 _dbus_assert_not_reached ("didn't allocate filename string\n");
00472
00473 dir = _dbus_directory_open (&test_directory, &error);
00474 if (dir == NULL)
00475 {
00476 _dbus_warn ("Could not open %s: %s\n",
00477 _dbus_string_get_const_data (&test_directory),
00478 error.message);
00479 dbus_error_free (&error);
00480 goto failed;
00481 }
00482
00483 printf ("Testing %s:\n", subdir);
00484
00485 next:
00486 while (_dbus_directory_get_next_file (dir, &filename, &error))
00487 {
00488 DBusString full_path;
00489
00490 if (!_dbus_string_init (&full_path))
00491 _dbus_assert_not_reached ("couldn't init string");
00492
00493 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
00494 _dbus_assert_not_reached ("couldn't copy dir to full_path");
00495
00496 if (!_dbus_concat_dir_and_file (&full_path, &filename))
00497 _dbus_assert_not_reached ("couldn't concat file to dir");
00498
00499 if (_dbus_string_ends_with_c_str (&filename, ".message-raw"))
00500 ;
00501 else
00502 {
00503 if (_dbus_string_ends_with_c_str (&filename, ".message"))
00504 {
00505 _dbus_warn ("Could not load %s, message builder language no longer supported\n",
00506 _dbus_string_get_const_data (&filename));
00507 }
00508
00509 _dbus_verbose ("Skipping non-.message file %s\n",
00510 _dbus_string_get_const_data (&filename));
00511 _dbus_string_free (&full_path);
00512 goto next;
00513 }
00514
00515 printf (" %s\n",
00516 _dbus_string_get_const_data (&filename));
00517
00518 if (! (*function) (&full_path,
00519 expected_validity, user_data))
00520 {
00521 _dbus_string_free (&full_path);
00522 goto failed;
00523 }
00524 else
00525 _dbus_string_free (&full_path);
00526 }
00527
00528 if (dbus_error_is_set (&error))
00529 {
00530 _dbus_warn ("Could not get next file in %s: %s\n",
00531 _dbus_string_get_const_data (&test_directory),
00532 error.message);
00533 dbus_error_free (&error);
00534 goto failed;
00535 }
00536
00537 retval = TRUE;
00538
00539 failed:
00540
00541 if (dir)
00542 _dbus_directory_close (dir);
00543 _dbus_string_free (&test_directory);
00544 _dbus_string_free (&filename);
00545
00546 return retval;
00547 }
00548
00558 dbus_bool_t
00559 dbus_internal_do_not_use_foreach_message_file (const char *test_data_dir,
00560 DBusForeachMessageFileFunc func,
00561 void *user_data)
00562 {
00563 DBusString test_directory;
00564 dbus_bool_t retval;
00565
00566 retval = FALSE;
00567
00568 _dbus_string_init_const (&test_directory, test_data_dir);
00569
00570 if (!process_test_subdir (&test_directory, "valid-messages",
00571 DBUS_VALID, func, user_data))
00572 goto failed;
00573
00574 check_memleaks ();
00575
00576 if (!process_test_subdir (&test_directory, "invalid-messages",
00577 DBUS_INVALID_FOR_UNKNOWN_REASON, func, user_data))
00578 goto failed;
00579
00580 check_memleaks ();
00581
00582 if (!process_test_subdir (&test_directory, "incomplete-messages",
00583 DBUS_VALID_BUT_INCOMPLETE, func, user_data))
00584 goto failed;
00585
00586 check_memleaks ();
00587
00588 retval = TRUE;
00589
00590 failed:
00591
00592 _dbus_string_free (&test_directory);
00593
00594 return retval;
00595 }
00596
00597 #if 0
00598 #define GET_AND_CHECK(iter, typename, literal) \
00599 do { \
00600 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
00601 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
00602 dbus_message_iter_get_basic (&iter, &v_##typename); \
00603 if (v_##typename != literal) \
00604 _dbus_assert_not_reached ("got wrong value from message iter"); \
00605 } while (0)
00606
00607 #define GET_AND_CHECK_STRCMP(iter, typename, literal) \
00608 do { \
00609 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_##typename) \
00610 _dbus_assert_not_reached ("got wrong argument type from message iter"); \
00611 dbus_message_iter_get_basic (&iter, &v_##typename); \
00612 if (strcmp (v_##typename, literal) != 0) \
00613 _dbus_assert_not_reached ("got wrong value from message iter"); \
00614 } while (0)
00615
00616 #define GET_AND_CHECK_AND_NEXT(iter, typename, literal) \
00617 do { \
00618 GET_AND_CHECK(iter, typename, literal); \
00619 if (!dbus_message_iter_next (&iter)) \
00620 _dbus_assert_not_reached ("failed to move iter to next"); \
00621 } while (0)
00622
00623 #define GET_AND_CHECK_STRCMP_AND_NEXT(iter, typename, literal) \
00624 do { \
00625 GET_AND_CHECK_STRCMP(iter, typename, literal); \
00626 if (!dbus_message_iter_next (&iter)) \
00627 _dbus_assert_not_reached ("failed to move iter to next"); \
00628 } while (0)
00629
00630 static void
00631 message_iter_test (DBusMessage *message)
00632 {
00633 DBusMessageIter iter, array, array2;
00634 const char *v_STRING;
00635 double v_DOUBLE;
00636 dbus_int16_t v_INT16;
00637 dbus_uint16_t v_UINT16;
00638 dbus_int32_t v_INT32;
00639 dbus_uint32_t v_UINT32;
00640 #ifdef DBUS_HAVE_INT64
00641 dbus_int64_t v_INT64;
00642 dbus_uint64_t v_UINT64;
00643 #endif
00644 unsigned char v_BYTE;
00645 dbus_bool_t v_BOOLEAN;
00646
00647 const dbus_int32_t *our_int_array;
00648 int len;
00649
00650 dbus_message_iter_init (message, &iter);
00651
00652 GET_AND_CHECK_STRCMP_AND_NEXT (iter, STRING, "Test string");
00653 GET_AND_CHECK_AND_NEXT (iter, INT32, -0x12345678);
00654 GET_AND_CHECK_AND_NEXT (iter, UINT32, 0xedd1e);
00655 GET_AND_CHECK_AND_NEXT (iter, DOUBLE, 3.14159);
00656
00657 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
00658 _dbus_assert_not_reached ("Argument type not an array");
00659
00660 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DOUBLE)
00661 _dbus_assert_not_reached ("Array type not double");
00662
00663 dbus_message_iter_recurse (&iter, &array);
00664
00665 GET_AND_CHECK_AND_NEXT (array, DOUBLE, 1.5);
00666 GET_AND_CHECK (array, DOUBLE, 2.5);
00667
00668 if (dbus_message_iter_next (&array))
00669 _dbus_assert_not_reached ("Didn't reach end of array");
00670
00671 if (!dbus_message_iter_next (&iter))
00672 _dbus_assert_not_reached ("Reached end of arguments");
00673
00674 GET_AND_CHECK_AND_NEXT (iter, BYTE, 0xF0);
00675
00676 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY)
00677 _dbus_assert_not_reached ("no array");
00678
00679 if (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_INT32)
00680 _dbus_assert_not_reached ("Array type not int32");
00681
00682
00683 dbus_message_iter_recurse (&iter, &array);
00684
00685 if (dbus_message_iter_next (&array))
00686 _dbus_assert_not_reached ("Didn't reach end of array");
00687
00688 if (!dbus_message_iter_next (&iter))
00689 _dbus_assert_not_reached ("Reached end of arguments");
00690
00691 GET_AND_CHECK (iter, BYTE, 0xF0);
00692
00693 if (dbus_message_iter_next (&iter))
00694 _dbus_assert_not_reached ("Didn't reach end of arguments");
00695 }
00696 #endif
00697
00698 static void
00699 verify_test_message (DBusMessage *message)
00700 {
00701 DBusMessageIter iter;
00702 DBusError error = DBUS_ERROR_INIT;
00703 dbus_int16_t our_int16;
00704 dbus_uint16_t our_uint16;
00705 dbus_int32_t our_int;
00706 dbus_uint32_t our_uint;
00707 const char *our_str;
00708 double our_double;
00709 double v_DOUBLE;
00710 dbus_bool_t our_bool;
00711 unsigned char our_byte_1, our_byte_2;
00712 const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
00713 int our_uint32_array_len;
00714 dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
00715 int our_int32_array_len;
00716 #ifdef DBUS_HAVE_INT64
00717 dbus_int64_t our_int64;
00718 dbus_uint64_t our_uint64;
00719 dbus_int64_t *our_uint64_array = (void*)0xdeadbeef;
00720 int our_uint64_array_len;
00721 const dbus_int64_t *our_int64_array = (void*)0xdeadbeef;
00722 int our_int64_array_len;
00723 #endif
00724 const double *our_double_array = (void*)0xdeadbeef;
00725 int our_double_array_len;
00726 const unsigned char *our_byte_array = (void*)0xdeadbeef;
00727 int our_byte_array_len;
00728 const dbus_bool_t *our_boolean_array = (void*)0xdeadbeef;
00729 int our_boolean_array_len;
00730 char **our_string_array;
00731 int our_string_array_len;
00732
00733 dbus_message_iter_init (message, &iter);
00734
00735 if (!dbus_message_iter_get_args (&iter, &error,
00736 DBUS_TYPE_INT16, &our_int16,
00737 DBUS_TYPE_UINT16, &our_uint16,
00738 DBUS_TYPE_INT32, &our_int,
00739 DBUS_TYPE_UINT32, &our_uint,
00740 #ifdef DBUS_HAVE_INT64
00741 DBUS_TYPE_INT64, &our_int64,
00742 DBUS_TYPE_UINT64, &our_uint64,
00743 #endif
00744 DBUS_TYPE_STRING, &our_str,
00745 DBUS_TYPE_DOUBLE, &our_double,
00746 DBUS_TYPE_BOOLEAN, &our_bool,
00747 DBUS_TYPE_BYTE, &our_byte_1,
00748 DBUS_TYPE_BYTE, &our_byte_2,
00749 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32,
00750 &our_uint32_array, &our_uint32_array_len,
00751 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32,
00752 &our_int32_array, &our_int32_array_len,
00753 #ifdef DBUS_HAVE_INT64
00754 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64,
00755 &our_uint64_array, &our_uint64_array_len,
00756 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64,
00757 &our_int64_array, &our_int64_array_len,
00758 #endif
00759 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE,
00760 &our_double_array, &our_double_array_len,
00761 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
00762 &our_byte_array, &our_byte_array_len,
00763 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN,
00764 &our_boolean_array, &our_boolean_array_len,
00765 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
00766 &our_string_array, &our_string_array_len,
00767 0))
00768 {
00769 _dbus_warn ("error: %s - %s\n", error.name,
00770 (error.message != NULL) ? error.message : "no message");
00771 _dbus_assert_not_reached ("Could not get arguments");
00772 }
00773
00774 if (our_int16 != -0x123)
00775 _dbus_assert_not_reached ("16-bit integers differ!");
00776
00777 if (our_uint16 != 0x123)
00778 _dbus_assert_not_reached ("16-bit uints differ!");
00779
00780 if (our_int != -0x12345678)
00781 _dbus_assert_not_reached ("integers differ!");
00782
00783 if (our_uint != 0x12300042)
00784 _dbus_assert_not_reached ("uints differ!");
00785
00786 #ifdef DBUS_HAVE_INT64
00787 if (our_int64 != DBUS_INT64_CONSTANT (-0x123456789abcd))
00788 _dbus_assert_not_reached ("64-bit integers differ!");
00789 if (our_uint64 != DBUS_UINT64_CONSTANT (0x123456789abcd))
00790 _dbus_assert_not_reached ("64-bit unsigned integers differ!");
00791 #endif
00792
00793 v_DOUBLE = 3.14159;
00794 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double, v_DOUBLE))
00795 _dbus_assert_not_reached ("doubles differ!");
00796
00797 if (strcmp (our_str, "Test string") != 0)
00798 _dbus_assert_not_reached ("strings differ!");
00799
00800 if (!our_bool)
00801 _dbus_assert_not_reached ("booleans differ");
00802
00803 if (our_byte_1 != 42)
00804 _dbus_assert_not_reached ("bytes differ!");
00805
00806 if (our_byte_2 != 24)
00807 _dbus_assert_not_reached ("bytes differ!");
00808
00809 if (our_uint32_array_len != 4 ||
00810 our_uint32_array[0] != 0x12345678 ||
00811 our_uint32_array[1] != 0x23456781 ||
00812 our_uint32_array[2] != 0x34567812 ||
00813 our_uint32_array[3] != 0x45678123)
00814 _dbus_assert_not_reached ("uint array differs");
00815
00816 if (our_int32_array_len != 4 ||
00817 our_int32_array[0] != 0x12345678 ||
00818 our_int32_array[1] != -0x23456781 ||
00819 our_int32_array[2] != 0x34567812 ||
00820 our_int32_array[3] != -0x45678123)
00821 _dbus_assert_not_reached ("int array differs");
00822
00823 #ifdef DBUS_HAVE_INT64
00824 if (our_uint64_array_len != 4 ||
00825 our_uint64_array[0] != 0x12345678 ||
00826 our_uint64_array[1] != 0x23456781 ||
00827 our_uint64_array[2] != 0x34567812 ||
00828 our_uint64_array[3] != 0x45678123)
00829 _dbus_assert_not_reached ("uint64 array differs");
00830
00831 if (our_int64_array_len != 4 ||
00832 our_int64_array[0] != 0x12345678 ||
00833 our_int64_array[1] != -0x23456781 ||
00834 our_int64_array[2] != 0x34567812 ||
00835 our_int64_array[3] != -0x45678123)
00836 _dbus_assert_not_reached ("int64 array differs");
00837 #endif
00838
00839 if (our_double_array_len != 3)
00840 _dbus_assert_not_reached ("double array had wrong length");
00841
00842
00843
00844
00845 v_DOUBLE = 0.1234;
00846 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[0], v_DOUBLE))
00847 _dbus_assert_not_reached ("double array had wrong values");
00848 v_DOUBLE = 9876.54321;
00849 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[1], v_DOUBLE))
00850 _dbus_assert_not_reached ("double array had wrong values");
00851 v_DOUBLE = -300.0;
00852 if (! _DBUS_DOUBLES_BITWISE_EQUAL (our_double_array[2], v_DOUBLE))
00853 _dbus_assert_not_reached ("double array had wrong values");
00854
00855 if (our_byte_array_len != 4)
00856 _dbus_assert_not_reached ("byte array had wrong length");
00857
00858 if (our_byte_array[0] != 'a' ||
00859 our_byte_array[1] != 'b' ||
00860 our_byte_array[2] != 'c' ||
00861 our_byte_array[3] != 234)
00862 _dbus_assert_not_reached ("byte array had wrong values");
00863
00864 if (our_boolean_array_len != 5)
00865 _dbus_assert_not_reached ("bool array had wrong length");
00866
00867 if (our_boolean_array[0] != TRUE ||
00868 our_boolean_array[1] != FALSE ||
00869 our_boolean_array[2] != TRUE ||
00870 our_boolean_array[3] != TRUE ||
00871 our_boolean_array[4] != FALSE)
00872 _dbus_assert_not_reached ("bool array had wrong values");
00873
00874 if (our_string_array_len != 4)
00875 _dbus_assert_not_reached ("string array was wrong length");
00876
00877 if (strcmp (our_string_array[0], "Foo") != 0 ||
00878 strcmp (our_string_array[1], "bar") != 0 ||
00879 strcmp (our_string_array[2], "") != 0 ||
00880 strcmp (our_string_array[3], "woo woo woo woo") != 0)
00881 _dbus_assert_not_reached ("string array had wrong values");
00882
00883 dbus_free_string_array (our_string_array);
00884
00885 if (dbus_message_iter_next (&iter))
00886 _dbus_assert_not_reached ("Didn't reach end of arguments");
00887 }
00888
00895 dbus_bool_t
00896 _dbus_message_test (const char *test_data_dir)
00897 {
00898 DBusMessage *message;
00899 DBusMessageLoader *loader;
00900 int i;
00901 const char *data;
00902 DBusMessage *copy;
00903 const char *name1;
00904 const char *name2;
00905 const dbus_uint32_t our_uint32_array[] =
00906 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
00907 const dbus_int32_t our_int32_array[] =
00908 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
00909 const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
00910 const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
00911 #ifdef DBUS_HAVE_INT64
00912 const dbus_uint64_t our_uint64_array[] =
00913 { 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
00914 const dbus_int64_t our_int64_array[] =
00915 { 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
00916 const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
00917 const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
00918 #endif
00919 const char *our_string_array[] = { "Foo", "bar", "", "woo woo woo woo" };
00920 const char **v_ARRAY_STRING = our_string_array;
00921 const double our_double_array[] = { 0.1234, 9876.54321, -300.0 };
00922 const double *v_ARRAY_DOUBLE = our_double_array;
00923 const unsigned char our_byte_array[] = { 'a', 'b', 'c', 234 };
00924 const unsigned char *v_ARRAY_BYTE = our_byte_array;
00925 const dbus_bool_t our_boolean_array[] = { TRUE, FALSE, TRUE, TRUE, FALSE };
00926 const dbus_bool_t *v_ARRAY_BOOLEAN = our_boolean_array;
00927 char sig[64];
00928 const char *s;
00929 const char *v_STRING;
00930 double v_DOUBLE;
00931 dbus_int16_t v_INT16;
00932 dbus_uint16_t v_UINT16;
00933 dbus_int32_t v_INT32;
00934 dbus_uint32_t v_UINT32;
00935 #ifdef DBUS_HAVE_INT64
00936 dbus_int64_t v_INT64;
00937 dbus_uint64_t v_UINT64;
00938 #endif
00939 unsigned char v_BYTE;
00940 unsigned char v2_BYTE;
00941 dbus_bool_t v_BOOLEAN;
00942
00943 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
00944 "/org/freedesktop/TestPath",
00945 "Foo.TestInterface",
00946 "TestMethod");
00947 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
00948 _dbus_assert (dbus_message_is_method_call (message, "Foo.TestInterface",
00949 "TestMethod"));
00950 _dbus_assert (strcmp (dbus_message_get_path (message),
00951 "/org/freedesktop/TestPath") == 0);
00952 _dbus_message_set_serial (message, 1234);
00953
00954
00955 if (!dbus_message_set_sender (message, "org.foo.bar1"))
00956 _dbus_assert_not_reached ("out of memory");
00957
00958 _dbus_assert (dbus_message_has_sender (message, "org.foo.bar1"));
00959 dbus_message_set_reply_serial (message, 5678);
00960
00961 _dbus_verbose_bytes_of_string (&message->header.data, 0,
00962 _dbus_string_get_length (&message->header.data));
00963 _dbus_verbose_bytes_of_string (&message->body, 0,
00964 _dbus_string_get_length (&message->body));
00965
00966 if (!dbus_message_set_sender (message, NULL))
00967 _dbus_assert_not_reached ("out of memory");
00968
00969
00970 _dbus_verbose_bytes_of_string (&message->header.data, 0,
00971 _dbus_string_get_length (&message->header.data));
00972 _dbus_verbose_bytes_of_string (&message->body, 0,
00973 _dbus_string_get_length (&message->body));
00974
00975
00976 _dbus_assert (!dbus_message_has_sender (message, "org.foo.bar1"));
00977 _dbus_assert (dbus_message_get_serial (message) == 1234);
00978 _dbus_assert (dbus_message_get_reply_serial (message) == 5678);
00979 _dbus_assert (dbus_message_has_destination (message, "org.freedesktop.DBus.TestService"));
00980
00981 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
00982 dbus_message_set_no_reply (message, TRUE);
00983 _dbus_assert (dbus_message_get_no_reply (message) == TRUE);
00984 dbus_message_set_no_reply (message, FALSE);
00985 _dbus_assert (dbus_message_get_no_reply (message) == FALSE);
00986
00987
00988
00989 if (!dbus_message_set_path (message, "/foo"))
00990 _dbus_assert_not_reached ("out of memory");
00991 _dbus_assert (strcmp (dbus_message_get_path (message),
00992 "/foo") == 0);
00993
00994 if (!dbus_message_set_interface (message, "org.Foo"))
00995 _dbus_assert_not_reached ("out of memory");
00996 _dbus_assert (strcmp (dbus_message_get_interface (message),
00997 "org.Foo") == 0);
00998
00999 if (!dbus_message_set_member (message, "Bar"))
01000 _dbus_assert_not_reached ("out of memory");
01001 _dbus_assert (strcmp (dbus_message_get_member (message),
01002 "Bar") == 0);
01003
01004
01005 if (!dbus_message_set_path (message, "/foo/bar"))
01006 _dbus_assert_not_reached ("out of memory");
01007 _dbus_assert (strcmp (dbus_message_get_path (message),
01008 "/foo/bar") == 0);
01009
01010 if (!dbus_message_set_interface (message, "org.Foo.Bar"))
01011 _dbus_assert_not_reached ("out of memory");
01012 _dbus_assert (strcmp (dbus_message_get_interface (message),
01013 "org.Foo.Bar") == 0);
01014
01015 if (!dbus_message_set_member (message, "BarFoo"))
01016 _dbus_assert_not_reached ("out of memory");
01017 _dbus_assert (strcmp (dbus_message_get_member (message),
01018 "BarFoo") == 0);
01019
01020
01021
01022 if (!dbus_message_set_path (message, "/foo"))
01023 _dbus_assert_not_reached ("out of memory");
01024 _dbus_assert (strcmp (dbus_message_get_path (message),
01025 "/foo") == 0);
01026
01027 if (!dbus_message_set_interface (message, "org.Foo"))
01028 _dbus_assert_not_reached ("out of memory");
01029 _dbus_assert (strcmp (dbus_message_get_interface (message),
01030 "org.Foo") == 0);
01031
01032 if (!dbus_message_set_member (message, "Bar"))
01033 _dbus_assert_not_reached ("out of memory");
01034 _dbus_assert (strcmp (dbus_message_get_member (message),
01035 "Bar") == 0);
01036
01037 dbus_message_unref (message);
01038
01039
01040 message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
01041 "/org/freedesktop/TestPath",
01042 "Foo.TestInterface",
01043 "TestMethod");
01044 _dbus_message_set_serial (message, 1);
01045 dbus_message_set_reply_serial (message, 5678);
01046
01047 v_INT16 = -0x123;
01048 v_UINT16 = 0x123;
01049 v_INT32 = -0x12345678;
01050 v_UINT32 = 0x12300042;
01051 #ifdef DBUS_HAVE_INT64
01052 v_INT64 = DBUS_INT64_CONSTANT (-0x123456789abcd);
01053 v_UINT64 = DBUS_UINT64_CONSTANT (0x123456789abcd);
01054 #endif
01055 v_STRING = "Test string";
01056 v_DOUBLE = 3.14159;
01057 v_BOOLEAN = TRUE;
01058 v_BYTE = 42;
01059 v2_BYTE = 24;
01060
01061 dbus_message_append_args (message,
01062 DBUS_TYPE_INT16, &v_INT16,
01063 DBUS_TYPE_UINT16, &v_UINT16,
01064 DBUS_TYPE_INT32, &v_INT32,
01065 DBUS_TYPE_UINT32, &v_UINT32,
01066 #ifdef DBUS_HAVE_INT64
01067 DBUS_TYPE_INT64, &v_INT64,
01068 DBUS_TYPE_UINT64, &v_UINT64,
01069 #endif
01070 DBUS_TYPE_STRING, &v_STRING,
01071 DBUS_TYPE_DOUBLE, &v_DOUBLE,
01072 DBUS_TYPE_BOOLEAN, &v_BOOLEAN,
01073 DBUS_TYPE_BYTE, &v_BYTE,
01074 DBUS_TYPE_BYTE, &v2_BYTE,
01075 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &v_ARRAY_UINT32,
01076 _DBUS_N_ELEMENTS (our_uint32_array),
01077 DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY_INT32,
01078 _DBUS_N_ELEMENTS (our_int32_array),
01079 #ifdef DBUS_HAVE_INT64
01080 DBUS_TYPE_ARRAY, DBUS_TYPE_UINT64, &v_ARRAY_UINT64,
01081 _DBUS_N_ELEMENTS (our_uint64_array),
01082 DBUS_TYPE_ARRAY, DBUS_TYPE_INT64, &v_ARRAY_INT64,
01083 _DBUS_N_ELEMENTS (our_int64_array),
01084 #endif
01085 DBUS_TYPE_ARRAY, DBUS_TYPE_DOUBLE, &v_ARRAY_DOUBLE,
01086 _DBUS_N_ELEMENTS (our_double_array),
01087 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &v_ARRAY_BYTE,
01088 _DBUS_N_ELEMENTS (our_byte_array),
01089 DBUS_TYPE_ARRAY, DBUS_TYPE_BOOLEAN, &v_ARRAY_BOOLEAN,
01090 _DBUS_N_ELEMENTS (our_boolean_array),
01091 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
01092 _DBUS_N_ELEMENTS (our_string_array),
01093 DBUS_TYPE_INVALID);
01094
01095 i = 0;
01096 sig[i++] = DBUS_TYPE_INT16;
01097 sig[i++] = DBUS_TYPE_UINT16;
01098 sig[i++] = DBUS_TYPE_INT32;
01099 sig[i++] = DBUS_TYPE_UINT32;
01100 #ifdef DBUS_HAVE_INT64
01101 sig[i++] = DBUS_TYPE_INT64;
01102 sig[i++] = DBUS_TYPE_UINT64;
01103 #endif
01104 sig[i++] = DBUS_TYPE_STRING;
01105 sig[i++] = DBUS_TYPE_DOUBLE;
01106 sig[i++] = DBUS_TYPE_BOOLEAN;
01107 sig[i++] = DBUS_TYPE_BYTE;
01108 sig[i++] = DBUS_TYPE_BYTE;
01109 sig[i++] = DBUS_TYPE_ARRAY;
01110 sig[i++] = DBUS_TYPE_UINT32;
01111 sig[i++] = DBUS_TYPE_ARRAY;
01112 sig[i++] = DBUS_TYPE_INT32;
01113 #ifdef DBUS_HAVE_INT64
01114 sig[i++] = DBUS_TYPE_ARRAY;
01115 sig[i++] = DBUS_TYPE_UINT64;
01116 sig[i++] = DBUS_TYPE_ARRAY;
01117 sig[i++] = DBUS_TYPE_INT64;
01118 #endif
01119 sig[i++] = DBUS_TYPE_ARRAY;
01120 sig[i++] = DBUS_TYPE_DOUBLE;
01121 sig[i++] = DBUS_TYPE_ARRAY;
01122 sig[i++] = DBUS_TYPE_BYTE;
01123 sig[i++] = DBUS_TYPE_ARRAY;
01124 sig[i++] = DBUS_TYPE_BOOLEAN;
01125 sig[i++] = DBUS_TYPE_ARRAY;
01126 sig[i++] = DBUS_TYPE_STRING;
01127 sig[i++] = DBUS_TYPE_INVALID;
01128
01129 _dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
01130
01131 _dbus_verbose ("HEADER\n");
01132 _dbus_verbose_bytes_of_string (&message->header.data, 0,
01133 _dbus_string_get_length (&message->header.data));
01134 _dbus_verbose ("BODY\n");
01135 _dbus_verbose_bytes_of_string (&message->body, 0,
01136 _dbus_string_get_length (&message->body));
01137
01138 _dbus_verbose ("Signature expected \"%s\" actual \"%s\"\n",
01139 sig, dbus_message_get_signature (message));
01140
01141 s = dbus_message_get_signature (message);
01142
01143 _dbus_assert (dbus_message_has_signature (message, sig));
01144 _dbus_assert (strcmp (s, sig) == 0);
01145
01146 verify_test_message (message);
01147
01148 copy = dbus_message_copy (message);
01149
01150 _dbus_assert (dbus_message_get_reply_serial (message) ==
01151 dbus_message_get_reply_serial (copy));
01152 _dbus_assert (message->header.padding == copy->header.padding);
01153
01154 _dbus_assert (_dbus_string_get_length (&message->header.data) ==
01155 _dbus_string_get_length (©->header.data));
01156
01157 _dbus_assert (_dbus_string_get_length (&message->body) ==
01158 _dbus_string_get_length (©->body));
01159
01160 verify_test_message (copy);
01161
01162 name1 = dbus_message_get_interface (message);
01163 name2 = dbus_message_get_interface (copy);
01164
01165 _dbus_assert (strcmp (name1, name2) == 0);
01166
01167 name1 = dbus_message_get_member (message);
01168 name2 = dbus_message_get_member (copy);
01169
01170 _dbus_assert (strcmp (name1, name2) == 0);
01171
01172 dbus_message_unref (copy);
01173
01174
01175 _dbus_message_lock (message);
01176 loader = _dbus_message_loader_new ();
01177
01178
01179 _dbus_message_loader_ref (loader);
01180 _dbus_message_loader_unref (loader);
01181
01182
01183 data = _dbus_string_get_const_data (&message->header.data);
01184 for (i = 0; i < _dbus_string_get_length (&message->header.data); i++)
01185 {
01186 DBusString *buffer;
01187
01188 _dbus_message_loader_get_buffer (loader, &buffer);
01189 _dbus_string_append_byte (buffer, data[i]);
01190 _dbus_message_loader_return_buffer (loader, buffer, 1);
01191 }
01192
01193
01194 data = _dbus_string_get_const_data (&message->body);
01195 for (i = 0; i < _dbus_string_get_length (&message->body); i++)
01196 {
01197 DBusString *buffer;
01198
01199 _dbus_message_loader_get_buffer (loader, &buffer);
01200 _dbus_string_append_byte (buffer, data[i]);
01201 _dbus_message_loader_return_buffer (loader, buffer, 1);
01202 }
01203
01204 dbus_message_unref (message);
01205
01206
01207 if (!_dbus_message_loader_queue_messages (loader))
01208 _dbus_assert_not_reached ("no memory to queue messages");
01209
01210 if (_dbus_message_loader_get_is_corrupted (loader))
01211 _dbus_assert_not_reached ("message loader corrupted");
01212
01213 message = _dbus_message_loader_pop_message (loader);
01214 if (!message)
01215 _dbus_assert_not_reached ("received a NULL message");
01216
01217 if (dbus_message_get_reply_serial (message) != 5678)
01218 _dbus_assert_not_reached ("reply serial fields differ");
01219
01220 verify_test_message (message);
01221
01222 {
01223
01224
01225 DBusMessage *message2;
01226 DBusError error = DBUS_ERROR_INIT;
01227 char *marshalled = NULL;
01228 int len = 0;
01229
01230 if (!dbus_message_marshal (message, &marshalled, &len))
01231 _dbus_assert_not_reached ("failed to marshal message");
01232
01233 _dbus_assert (len != 0);
01234 _dbus_assert (marshalled != NULL);
01235
01236 message2 = dbus_message_demarshal (marshalled, len, &error);
01237
01238 _dbus_assert (message2 != NULL);
01239 _dbus_assert (!dbus_error_is_set (&error));
01240 verify_test_message (message2);
01241
01242 dbus_message_unref (message2);
01243 dbus_free (marshalled);
01244
01245
01246
01247 message2 = dbus_message_demarshal ("invalid", 7, &error);
01248 _dbus_assert (message2 == NULL);
01249 _dbus_assert (dbus_error_is_set (&error));
01250 dbus_error_free (&error);
01251
01252
01253
01254 message2 = dbus_message_demarshal ("", 0, &error);
01255 _dbus_assert (message2 == NULL);
01256 _dbus_assert (dbus_error_is_set (&error));
01257 dbus_error_free (&error);
01258 }
01259
01260 dbus_message_unref (message);
01261 _dbus_message_loader_unref (loader);
01262
01263 check_memleaks ();
01264
01265
01266 {
01267 DBusMessageDataIter diter;
01268 DBusMessageData mdata;
01269 int count;
01270
01271 reset_validities_seen ();
01272
01273 count = 0;
01274 _dbus_message_data_iter_init (&diter);
01275
01276 while (_dbus_message_data_iter_get_and_next (&diter,
01277 &mdata))
01278 {
01279 if (!dbus_internal_do_not_use_try_message_data (&mdata.data,
01280 mdata.expected_validity))
01281 {
01282 _dbus_warn ("expected validity %d and did not get it\n",
01283 mdata.expected_validity);
01284 _dbus_assert_not_reached ("message data failed");
01285 }
01286
01287 _dbus_message_data_free (&mdata);
01288
01289 count += 1;
01290 }
01291
01292 printf ("%d sample messages tested\n", count);
01293
01294 print_validities_seen (FALSE);
01295 print_validities_seen (TRUE);
01296 }
01297
01298 check_memleaks ();
01299
01300
01301 if (test_data_dir == NULL)
01302 return TRUE;
01303
01304 return dbus_internal_do_not_use_foreach_message_file (test_data_dir,
01305 (DBusForeachMessageFileFunc)
01306 dbus_internal_do_not_use_try_message_file,
01307 NULL);
01308 }
01309
01310 #endif