26 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 
   28 #include "dbus-marshal-recursive.h" 
   29 #include "dbus-marshal-basic.h" 
   30 #include "dbus-signature.h" 
   31 #include "dbus-internals.h" 
   34 #if !defined(PRIx64) && defined(DBUS_WIN) 
   39 #define RECURSIVE_MARSHAL_WRITE_TRACE 0 
   48 basic_value_equal (
int             type,
 
   56       return strcmp (lhs->
str, rhs->
str) == 0;
 
   60       return lhs->
u64 == rhs->
u64;
 
   74   if (lhs_type != rhs_type)
 
   85       basic_value_zero (&lhs_value);
 
   86       basic_value_zero (&rhs_value);
 
   91       return basic_value_equal (lhs_type, &lhs_value, &rhs_value);
 
  101       return equal_values_helper (&lhs_sub, &rhs_sub);
 
  119   return equal_values_helper (©_lhs, ©_rhs);
 
  124 #ifndef DOXYGEN_SHOULD_SKIP_THIS 
  126 #include "dbus-test.h" 
  127 #include "dbus-list.h" 
  132 #define TEST_OOM_HANDLING 0 
  136 #define MAX_INITIAL_OFFSET 9 
  142 #define MAX_ITERATIONS_FOR_EXPENSIVE_TESTS 1000 
  158 #define N_FENCE_BYTES 5 
  159 #define FENCE_BYTES_STR "abcde" 
  160 #define INITIAL_PADDING_BYTE '\0' 
  163 data_block_init (DataBlock *block,
 
  177                                   INITIAL_PADDING_BYTE) ||
 
  179                                   INITIAL_PADDING_BYTE) ||
 
  188   block->byte_order = byte_order;
 
  189   block->initial_offset = initial_offset;
 
  195 data_block_save (DataBlock      *block,
 
  196                  DataBlockState *state)
 
  198   state->saved_sig_len = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES;
 
  199   state->saved_body_len = _dbus_string_get_length (&block->body) - N_FENCE_BYTES;
 
  203 data_block_restore (DataBlock      *block,
 
  204                     DataBlockState *state)
 
  207                        state->saved_sig_len,
 
  208                        _dbus_string_get_length (&block->signature) - state->saved_sig_len - N_FENCE_BYTES);
 
  210                        state->saved_body_len,
 
  211                        _dbus_string_get_length (&block->body) - state->saved_body_len - N_FENCE_BYTES);
 
  215 data_block_verify (DataBlock *block)
 
  222       offset = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - 8;
 
  228                                      _dbus_string_get_length (&block->signature) - offset);
 
  236       offset = _dbus_string_get_length (&block->body) - N_FENCE_BYTES - 8;
 
  242                                      _dbus_string_get_length (&block->body) - offset);
 
  247                                            0, block->initial_offset));
 
  249                                            0, block->initial_offset));
 
  253 data_block_free (DataBlock *block)
 
  255   data_block_verify (block);
 
  262 data_block_reset (DataBlock *block)
 
  264   data_block_verify (block);
 
  267                        block->initial_offset,
 
  268                        _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - block->initial_offset);
 
  270                        block->initial_offset,
 
  271                        _dbus_string_get_length (&block->body) - N_FENCE_BYTES - block->initial_offset);
 
  273   data_block_verify (block);
 
  277 data_block_init_reader_writer (DataBlock      *block,
 
  285                             block->initial_offset,
 
  287                             block->initial_offset);
 
  293                             _dbus_string_get_length (&block->signature) - N_FENCE_BYTES,
 
  295                             _dbus_string_get_length (&block->body) - N_FENCE_BYTES);
 
  301                           const char     *funcname,
 
  310       _dbus_warn (
"Read type %s while expecting %s at %s line %d",
 
  319 #define check_expected_type(reader, expected) real_check_expected_type (reader, expected, _DBUS_FUNCTION_NAME, __LINE__) 
  321 #define NEXT_EXPECTING_TRUE(reader)  do { if (!_dbus_type_reader_next (reader))         \ 
  323     _dbus_warn ("_dbus_type_reader_next() should have returned TRUE at %s %d",          \ 
  324                               _DBUS_FUNCTION_NAME, __LINE__);                           \ 
  325     _dbus_assert_not_reached ("test failed");                                           \ 
  329 #define NEXT_EXPECTING_FALSE(reader) do { if (_dbus_type_reader_next (reader))          \ 
  331     _dbus_warn ("_dbus_type_reader_next() should have returned FALSE at %s %d",         \ 
  332                               _DBUS_FUNCTION_NAME, __LINE__);                           \ 
  333     _dbus_assert_not_reached ("test failed");                                           \ 
  335  check_expected_type (reader, DBUS_TYPE_INVALID);                                       \ 
  338 typedef struct TestTypeNode               TestTypeNode;
 
  339 typedef struct TestTypeNodeClass          TestTypeNodeClass;
 
  340 typedef struct TestTypeNodeContainer      TestTypeNodeContainer;
 
  341 typedef struct TestTypeNodeContainerClass TestTypeNodeContainerClass;
 
  345   const TestTypeNodeClass *klass;
 
  348 struct TestTypeNodeContainer
 
  354 struct TestTypeNodeClass
 
  363   void          (* destroy)       (TestTypeNode   *node);
 
  376   dbus_bool_t (* build_signature) (TestTypeNode   *node,
 
  389 struct TestTypeNodeContainerClass
 
  391   TestTypeNodeClass base;
 
  399 static dbus_bool_t int16_write_value       (TestTypeNode   *node,
 
  403 static dbus_bool_t int16_read_value        (TestTypeNode   *node,
 
  406 static dbus_bool_t int16_set_value         (TestTypeNode   *node,
 
  410 static dbus_bool_t int16_write_multi       (TestTypeNode   *node,
 
  415 static dbus_bool_t int16_read_multi        (TestTypeNode   *node,
 
  419 static dbus_bool_t int32_write_value       (TestTypeNode   *node,
 
  423 static dbus_bool_t int32_read_value        (TestTypeNode   *node,
 
  426 static dbus_bool_t int32_set_value         (TestTypeNode   *node,
 
  430 static dbus_bool_t int32_write_multi       (TestTypeNode   *node,
 
  435 static dbus_bool_t int32_read_multi        (TestTypeNode   *node,
 
  439 static dbus_bool_t int64_write_value       (TestTypeNode   *node,
 
  443 static dbus_bool_t int64_read_value        (TestTypeNode   *node,
 
  446 static dbus_bool_t int64_set_value         (TestTypeNode   *node,
 
  450 static dbus_bool_t string_write_value      (TestTypeNode   *node,
 
  454 static dbus_bool_t string_read_value       (TestTypeNode   *node,
 
  457 static dbus_bool_t string_set_value        (TestTypeNode   *node,
 
  461 static dbus_bool_t bool_write_value        (TestTypeNode   *node,
 
  465 static dbus_bool_t bool_read_value         (TestTypeNode   *node,
 
  468 static dbus_bool_t bool_set_value          (TestTypeNode   *node,
 
  472 static dbus_bool_t byte_write_value        (TestTypeNode   *node,
 
  476 static dbus_bool_t byte_read_value         (TestTypeNode   *node,
 
  479 static dbus_bool_t byte_set_value          (TestTypeNode   *node,
 
  483 static dbus_bool_t double_write_value      (TestTypeNode   *node,
 
  487 static dbus_bool_t double_read_value       (TestTypeNode   *node,
 
  490 static dbus_bool_t double_set_value        (TestTypeNode   *node,
 
  494 static dbus_bool_t object_path_write_value (TestTypeNode   *node,
 
  498 static dbus_bool_t object_path_read_value  (TestTypeNode   *node,
 
  501 static dbus_bool_t object_path_set_value   (TestTypeNode   *node,
 
  505 static dbus_bool_t signature_write_value   (TestTypeNode   *node,
 
  509 static dbus_bool_t signature_read_value    (TestTypeNode   *node,
 
  512 static dbus_bool_t signature_set_value     (TestTypeNode   *node,
 
  516 static dbus_bool_t struct_write_value      (TestTypeNode   *node,
 
  520 static dbus_bool_t struct_read_value       (TestTypeNode   *node,
 
  523 static dbus_bool_t struct_set_value        (TestTypeNode   *node,
 
  527 static dbus_bool_t struct_build_signature  (TestTypeNode   *node,
 
  529 static dbus_bool_t dict_write_value        (TestTypeNode   *node,
 
  533 static dbus_bool_t dict_read_value         (TestTypeNode   *node,
 
  536 static dbus_bool_t dict_set_value          (TestTypeNode   *node,
 
  540 static dbus_bool_t dict_build_signature    (TestTypeNode   *node,
 
  542 static dbus_bool_t array_write_value       (TestTypeNode   *node,
 
  546 static dbus_bool_t array_read_value        (TestTypeNode   *node,
 
  549 static dbus_bool_t array_set_value         (TestTypeNode   *node,
 
  553 static dbus_bool_t array_build_signature   (TestTypeNode   *node,
 
  555 static dbus_bool_t variant_write_value     (TestTypeNode   *node,
 
  559 static dbus_bool_t variant_read_value      (TestTypeNode   *node,
 
  562 static dbus_bool_t variant_set_value       (TestTypeNode   *node,
 
  566 static void        container_destroy       (TestTypeNode   *node);
 
  570 static const TestTypeNodeClass int16_class = {
 
  572   sizeof (TestTypeNode),
 
  584 static const TestTypeNodeClass uint16_class = {
 
  586   sizeof (TestTypeNode),
 
  598 static const TestTypeNodeClass int32_class = {
 
  600   sizeof (TestTypeNode),
 
  612 static const TestTypeNodeClass uint32_class = {
 
  614   sizeof (TestTypeNode),
 
  626 static const TestTypeNodeClass int64_class = {
 
  628   sizeof (TestTypeNode),
 
  640 static const TestTypeNodeClass uint64_class = {
 
  642   sizeof (TestTypeNode),
 
  654 static const TestTypeNodeClass string_0_class = {
 
  656   sizeof (TestTypeNode),
 
  668 static const TestTypeNodeClass string_1_class = {
 
  670   sizeof (TestTypeNode),
 
  683 static const TestTypeNodeClass string_3_class = {
 
  685   sizeof (TestTypeNode),
 
  698 static const TestTypeNodeClass string_8_class = {
 
  700   sizeof (TestTypeNode),
 
  712 static const TestTypeNodeClass bool_class = {
 
  714   sizeof (TestTypeNode),
 
  726 static const TestTypeNodeClass byte_class = {
 
  728   sizeof (TestTypeNode),
 
  740 static const TestTypeNodeClass double_class = {
 
  742   sizeof (TestTypeNode),
 
  754 static const TestTypeNodeClass object_path_class = {
 
  756   sizeof (TestTypeNode),
 
  760   object_path_write_value,
 
  761   object_path_read_value,
 
  762   object_path_set_value,
 
  768 static const TestTypeNodeClass signature_class = {
 
  770   sizeof (TestTypeNode),
 
  774   signature_write_value,
 
  775   signature_read_value,
 
  782 static const TestTypeNodeClass struct_1_class = {
 
  784   sizeof (TestTypeNodeContainer),
 
  791   struct_build_signature,
 
  796 static const TestTypeNodeClass struct_2_class = {
 
  798   sizeof (TestTypeNodeContainer),
 
  805   struct_build_signature,
 
  810 static const TestTypeNodeClass dict_1_class = {
 
  812   sizeof (TestTypeNodeContainer),
 
  819   dict_build_signature,
 
  826 static const TestTypeNodeClass array_0_class = {
 
  828   sizeof (TestTypeNodeContainer),
 
  835   array_build_signature,
 
  840 static const TestTypeNodeClass array_1_class = {
 
  842   sizeof (TestTypeNodeContainer),
 
  849   array_build_signature,
 
  854 static const TestTypeNodeClass array_2_class = {
 
  856   sizeof (TestTypeNodeContainer),
 
  863   array_build_signature,
 
  868 static const TestTypeNodeClass array_9_class = {
 
  870   sizeof (TestTypeNodeContainer),
 
  877   array_build_signature,
 
  882 static const TestTypeNodeClass variant_class = {
 
  884   sizeof (TestTypeNodeContainer),
 
  896 static const TestTypeNodeClass* 
const 
  914 #define N_BASICS (_DBUS_N_ELEMENTS (basic_nodes)) 
  916 static const TestTypeNodeClass* 
const 
  917 container_nodes[] = {
 
  929 #define N_CONTAINERS (_DBUS_N_ELEMENTS (container_nodes)) 
  932 node_new (
const TestTypeNodeClass *klass)
 
  942   if (klass->construct)
 
  944       if (!(* klass->construct) (node))
 
  955 node_destroy (TestTypeNode *node)
 
  957   if (node->klass->destroy)
 
  958     (* node->klass->destroy) (node);
 
  963 node_write_value (TestTypeNode   *node,
 
  970   retval = (* node->klass->write_value) (node, block, writer, seed);
 
  974   data_block_verify (block);
 
  981 node_read_value (TestTypeNode   *node,
 
  987   if (!(* node->klass->read_value) (node, reader, seed))
 
  998 node_set_value (TestTypeNode   *node,
 
 1003   if (!(* node->klass->set_value) (node, reader, realign_root, seed))
 
 1010 node_build_signature (TestTypeNode *node,
 
 1013   if (node->klass->build_signature)
 
 1014     return (* node->klass->build_signature) (node, str);
 
 1020 node_append_child (TestTypeNode *node,
 
 1021                    TestTypeNode *child)
 
 1023   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 1025   _dbus_assert (node->klass->instance_size >= (
int) sizeof (TestTypeNodeContainer));
 
 1034 node_write_multi (TestTypeNode   *node,
 
 1043   retval = (* node->klass->write_multi) (node, block, writer, seed, n_copies);
 
 1047   data_block_verify (block);
 
 1054 node_read_multi (TestTypeNode   *node,
 
 1061   if (!(* node->klass->read_multi) (node, reader, seed, n_copies))
 
 1067 static int n_iterations_completed_total = 0;
 
 1068 static int n_iterations_completed_this_test = 0;
 
 1069 static int n_iterations_expected_this_test = 0;
 
 1076   TestTypeNode      **nodes;
 
 1078 } NodeIterationData;
 
 1081 run_test_copy (NodeIterationData *nid)
 
 1089   _dbus_verbose (
"\n");
 
 1095   if (!data_block_init (&dest, src->byte_order, src->initial_offset))
 
 1098   data_block_init_reader_writer (src, &reader, 
NULL);
 
 1099   data_block_init_reader_writer (&dest, 
NULL, &writer);
 
 1105                                  dest.initial_offset, 
'\0'))
 
 1114       _dbus_verbose (
"SOURCE\n");
 
 1116                                      _dbus_string_get_length (&src->signature));
 
 1117       _dbus_verbose (
"DEST\n");
 
 1119                                      _dbus_string_get_length (&dest.signature));
 
 1125       _dbus_verbose (
"SOURCE\n");
 
 1127                                      _dbus_string_get_length (&src->body));
 
 1128       _dbus_verbose (
"DEST\n");
 
 1130                                      _dbus_string_get_length (&dest.body));
 
 1138   data_block_free (&dest);
 
 1144 run_test_values_only_write (NodeIterationData *nid)
 
 1152   _dbus_verbose (
"\n");
 
 1156   data_block_reset (nid->block);
 
 1158   sig_len = _dbus_string_get_length (nid->signature);
 
 1161                                       nid->block->byte_order,
 
 1164                                       _dbus_string_get_length (&nid->block->body) - N_FENCE_BYTES);
 
 1166                           nid->block->byte_order,
 
 1169                           nid->block->initial_offset);
 
 1172   while (i < nid->n_nodes)
 
 1174       if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
 
 1181   _dbus_assert (sig_len == _dbus_string_get_length (nid->signature));
 
 1185   while (i < nid->n_nodes)
 
 1187       if (!node_read_value (nid->nodes[i], &reader, i))
 
 1190       if (i + 1 == nid->n_nodes)
 
 1191         NEXT_EXPECTING_FALSE (&reader);
 
 1193         NEXT_EXPECTING_TRUE (&reader);
 
 1201   data_block_reset (nid->block);
 
 1213 run_test_set_values (NodeIterationData *nid)
 
 1220   _dbus_verbose (
"\n");
 
 1224   data_block_init_reader_writer (nid->block,
 
 1227   realign_root = reader;
 
 1230   while (i < nid->n_nodes)
 
 1232       if (!node_set_value (nid->nodes[i],
 
 1233                            &reader, &realign_root,
 
 1237       if (i + 1 == nid->n_nodes)
 
 1238         NEXT_EXPECTING_FALSE (&reader);
 
 1240         NEXT_EXPECTING_TRUE (&reader);
 
 1247   reader = realign_root;
 
 1250   while (i < nid->n_nodes)
 
 1252       if (!node_read_value (nid->nodes[i], &reader,
 
 1256       if (i + 1 == nid->n_nodes)
 
 1257         NEXT_EXPECTING_FALSE (&reader);
 
 1259         NEXT_EXPECTING_TRUE (&reader);
 
 1271 run_test_delete_values (NodeIterationData *nid)
 
 1277   _dbus_verbose (
"\n");
 
 1281   data_block_init_reader_writer (nid->block,
 
 1306           _dbus_verbose (
"recursing into deletion loop reader.value_pos = %d array.value_pos = %d array.u.start_pos = %d\n",
 
 1311               static int cycle = 0;
 
 1317               if (elem == 3 || elem >= n_elements) 
 
 1318                 elem = n_elements - 1;
 
 1320               _dbus_verbose (
"deleting array element %d of %d type %s cycle %d reader pos %d elem pos %d\n",
 
 1348   data_block_init_reader_writer (nid->block,
 
 1363 run_test_nodes_iteration (
void *data)
 
 1365   NodeIterationData *nid = data;
 
 1379   data_block_init_reader_writer (nid->block,
 
 1386                                  nid->type_offset, 
'\0'))
 
 1390   while (i < nid->n_nodes)
 
 1392       if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
 
 1399                                      &nid->block->signature, nid->type_offset))
 
 1401       _dbus_warn (
"Expected signature '%s' and got '%s' with initial offset %d",
 
 1402                   _dbus_string_get_const_data (nid->signature),
 
 1403                   _dbus_string_get_const_data_len (&nid->block->signature, nid->type_offset, 0),
 
 1409   while (i < nid->n_nodes)
 
 1411       if (!node_read_value (nid->nodes[i], &reader, i))
 
 1414       if (i + 1 == nid->n_nodes)
 
 1415         NEXT_EXPECTING_FALSE (&reader);
 
 1417         NEXT_EXPECTING_TRUE (&reader);
 
 1422   if (n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
 
 1431       if (!run_test_set_values (nid))
 
 1434       if (!run_test_delete_values (nid))
 
 1437       if (!run_test_copy (nid))
 
 1440       if (!run_test_values_only_write (nid))
 
 1452   data_block_reset (nid->block);
 
 1458 run_test_nodes_in_one_configuration (TestTypeNode    **nodes,
 
 1465   NodeIterationData nid;
 
 1467   if (!data_block_init (&block, byte_order, initial_offset))
 
 1470   nid.signature = signature;
 
 1472   nid.type_offset = initial_offset;
 
 1474   nid.n_nodes = n_nodes;
 
 1476   if (TEST_OOM_HANDLING &&
 
 1477       n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
 
 1479       _dbus_test_oom_handling (
"running test node",
 
 1480                                run_test_nodes_iteration,
 
 1485       if (!run_test_nodes_iteration (&nid))
 
 1489   data_block_free (&block);
 
 1493 run_test_nodes (TestTypeNode **nodes,
 
 1505       if (! node_build_signature (nodes[i], &signature))
 
 1511   _dbus_verbose (
">>> test nodes with signature '%s'\n",
 
 1512                  _dbus_string_get_const_data (&signature));
 
 1515   while (i <= MAX_INITIAL_OFFSET)
 
 1517       run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
 
 1519       run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
 
 1525   n_iterations_completed_this_test += 1;
 
 1526   n_iterations_completed_total += 1;
 
 1528   if (n_iterations_completed_this_test == n_iterations_expected_this_test)
 
 1530       fprintf (stderr, 
" 100%% %d this test (%d cumulative)\n",
 
 1531                n_iterations_completed_this_test,
 
 1532                n_iterations_completed_total);
 
 1535   else if ((n_iterations_completed_this_test %
 
 1536             (
int)(n_iterations_expected_this_test / 10.0)) == 1)
 
 1538       fprintf (stderr, 
" %d%% ", (
int) (n_iterations_completed_this_test / (
double) n_iterations_expected_this_test * 100));
 
 1544 #define N_VALUES (N_BASICS * N_CONTAINERS + N_BASICS) 
 1546 static TestTypeNode*
 
 1547 value_generator (
int *ip)
 
 1550   const TestTypeNodeClass *child_klass;
 
 1551   const TestTypeNodeClass *container_klass;
 
 1552   TestTypeNode *child;
 
 1561   else if (i < N_BASICS)
 
 1563       node = node_new (basic_nodes[i]);
 
 1577       container_klass = container_nodes[i / N_BASICS];
 
 1578       child_klass = basic_nodes[i % N_BASICS];
 
 1580       node = node_new (container_klass);
 
 1581       child = node_new (child_klass);
 
 1583       node_append_child (node, child);
 
 1592 build_body (TestTypeNode **nodes,
 
 1606       if (! node_build_signature (nodes[i], signature))
 
 1612   if (!data_block_init (&block, byte_order, 0))
 
 1615   data_block_init_reader_writer (&block,
 
 1628       if (!node_write_value (nodes[i], &block, &writer, i))
 
 1635                               _dbus_string_get_length (&block.body) - N_FENCE_BYTES,
 
 1639   data_block_free (&block);  
 
 1643 dbus_internal_do_not_use_generate_bodies (
int           sequence,
 
 1648   TestTypeNode *nodes[1];
 
 1652   nodes[0] = value_generator (&sequence);
 
 1654   if (nodes[0] == 
NULL)
 
 1659   build_body (nodes, n_nodes, byte_order, signature, body);
 
 1665       node_destroy (nodes[i]);
 
 1673 make_and_run_values_inside_container (
const TestTypeNodeClass *container_klass,
 
 1677   TestTypeNode *container;
 
 1678   TestTypeNode *child;
 
 1681   root = node_new (container_klass);
 
 1683   for (i = 1; i < n_nested; i++)
 
 1685       child = node_new (container_klass);
 
 1686       node_append_child (container, child);
 
 1693   while ((child = value_generator (&i)))
 
 1695       node_append_child (container, child);
 
 1697       run_test_nodes (&root, 1);
 
 1700       node_destroy (child);
 
 1703   node_destroy (root);
 
 1707 start_next_test (
const char *description,
 
 1710   n_iterations_completed_this_test = 0;
 
 1711   n_iterations_expected_this_test = expected;
 
 1713   fprintf (stderr, 
">>> >>> %s %d iterations\n",
 
 1715            n_iterations_expected_this_test);
 
 1719 make_and_run_test_nodes (
void)
 
 1755   start_next_test (
"Each value by itself", N_VALUES);
 
 1759     while ((node = value_generator (&i)))
 
 1761         run_test_nodes (&node, 1);
 
 1763         node_destroy (node);
 
 1767   start_next_test (
"Each value by itself with arrays as blocks", N_VALUES);
 
 1768   arrays_write_fixed_in_blocks = 
TRUE;
 
 1772     while ((node = value_generator (&i)))
 
 1774         run_test_nodes (&node, 1);
 
 1776         node_destroy (node);
 
 1779   arrays_write_fixed_in_blocks = 
FALSE;
 
 1781   start_next_test (
"All values in one big toplevel", 1);
 
 1783     TestTypeNode *nodes[N_VALUES];
 
 1787     while ((node = value_generator (&i)))
 
 1789         nodes[i - 1] = node;
 
 1792     run_test_nodes (nodes, N_VALUES);
 
 1794     for (i = 0; i < N_VALUES; i++)
 
 1795       node_destroy (nodes[i]);
 
 1798   start_next_test (
"Each value,value pair combination as toplevel, in both orders",
 
 1799                    N_VALUES * N_VALUES);
 
 1801     TestTypeNode *nodes[2];
 
 1804     while ((nodes[0] = value_generator (&i)))
 
 1807         while ((nodes[1] = value_generator (&j)))
 
 1809             run_test_nodes (nodes, 2);
 
 1811             node_destroy (nodes[1]);
 
 1814         node_destroy (nodes[0]);
 
 1818   start_next_test (
"Each container containing each value",
 
 1819                    N_CONTAINERS * N_VALUES);
 
 1820   for (i = 0; i < N_CONTAINERS; i++)
 
 1822       const TestTypeNodeClass *container_klass = container_nodes[i];
 
 1824       make_and_run_values_inside_container (container_klass, 1);
 
 1827   start_next_test (
"Each container containing each value with arrays as blocks",
 
 1828                    N_CONTAINERS * N_VALUES);
 
 1829   arrays_write_fixed_in_blocks = 
TRUE;
 
 1830   for (i = 0; i < N_CONTAINERS; i++)
 
 1832       const TestTypeNodeClass *container_klass = container_nodes[i];
 
 1834       make_and_run_values_inside_container (container_klass, 1);
 
 1836   arrays_write_fixed_in_blocks = 
FALSE;
 
 1838   start_next_test (
"Each container of same container of each value",
 
 1839                    N_CONTAINERS * N_VALUES);
 
 1840   for (i = 0; i < N_CONTAINERS; i++)
 
 1842       const TestTypeNodeClass *container_klass = container_nodes[i];
 
 1844       make_and_run_values_inside_container (container_klass, 2);
 
 1847   start_next_test (
"Each container of same container of same container of each value",
 
 1848                    N_CONTAINERS * N_VALUES);
 
 1849   for (i = 0; i < N_CONTAINERS; i++)
 
 1851       const TestTypeNodeClass *container_klass = container_nodes[i];
 
 1853       make_and_run_values_inside_container (container_klass, 3);
 
 1856   start_next_test (
"Each value,value pair inside a struct",
 
 1857                    N_VALUES * N_VALUES);
 
 1859     TestTypeNode *val1, *val2;
 
 1862     node = node_new (&struct_1_class);
 
 1865     while ((val1 = value_generator (&i)))
 
 1868         while ((val2 = value_generator (&j)))
 
 1870             TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 1872             node_append_child (node, val1);
 
 1873             node_append_child (node, val2);
 
 1875             run_test_nodes (&node, 1);
 
 1878             node_destroy (val2);
 
 1880         node_destroy (val1);
 
 1882     node_destroy (node);
 
 1885   start_next_test (
"All values in one big struct", 1);
 
 1888     TestTypeNode *child;
 
 1890     node = node_new (&struct_1_class);
 
 1893     while ((child = value_generator (&i)))
 
 1894       node_append_child (node, child);
 
 1896     run_test_nodes (&node, 1);
 
 1898     node_destroy (node);
 
 1901   start_next_test (
"Each value in a large array", N_VALUES);
 
 1906     node = node_new (&array_9_class);
 
 1909     while ((val = value_generator (&i)))
 
 1911         TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 1913         node_append_child (node, val);
 
 1915         run_test_nodes (&node, 1);
 
 1921     node_destroy (node);
 
 1927       fprintf (stderr, 
"skipping remaining marshal-recursive tests, " 
 1928           "run with DBUS_TEST_SLOW=1 (or more) to enable\n");
 
 1932   start_next_test (
"Each container of each container of each value",
 
 1933                    N_CONTAINERS * N_CONTAINERS * N_VALUES);
 
 1934   for (i = 0; i < N_CONTAINERS; i++)
 
 1936       const TestTypeNodeClass *outer_container_klass = container_nodes[i];
 
 1937       TestTypeNode *outer_container = node_new (outer_container_klass);
 
 1939       for (j = 0; j < N_CONTAINERS; j++)
 
 1941           TestTypeNode *child;
 
 1942           const TestTypeNodeClass *inner_container_klass = container_nodes[j];
 
 1943           TestTypeNode *inner_container = node_new (inner_container_klass);
 
 1945           node_append_child (outer_container, inner_container);
 
 1948           while ((child = value_generator (&m)))
 
 1950               node_append_child (inner_container, child);
 
 1952               run_test_nodes (&outer_container, 1);
 
 1955               node_destroy (child);
 
 1958           node_destroy (inner_container);
 
 1960       node_destroy (outer_container);
 
 1963   start_next_test (
"Each container of each container of each container of each value",
 
 1964                    N_CONTAINERS * N_CONTAINERS * N_CONTAINERS * N_VALUES);
 
 1965   for (i = 0; i < N_CONTAINERS; i++)
 
 1967       const TestTypeNodeClass *outer_container_klass = container_nodes[i];
 
 1968       TestTypeNode *outer_container = node_new (outer_container_klass);
 
 1970       for (j = 0; j < N_CONTAINERS; j++)
 
 1972           const TestTypeNodeClass *inner_container_klass = container_nodes[j];
 
 1973           TestTypeNode *inner_container = node_new (inner_container_klass);
 
 1975           node_append_child (outer_container, inner_container);
 
 1977           for (k = 0; k < N_CONTAINERS; k++)
 
 1979               TestTypeNode *child;
 
 1980               const TestTypeNodeClass *center_container_klass = container_nodes[k];
 
 1981               TestTypeNode *center_container = node_new (center_container_klass);
 
 1983               node_append_child (inner_container, center_container);
 
 1986               while ((child = value_generator (&m)))
 
 1988                   node_append_child (center_container, child);
 
 1990                   run_test_nodes (&outer_container, 1);
 
 1993                   node_destroy (child);
 
 1996               node_destroy (center_container);
 
 1999           node_destroy (inner_container);
 
 2001       node_destroy (outer_container);
 
 2008       fprintf (stderr, 
"skipping really slow marshal-recursive test, " 
 2009           "run with DBUS_TEST_SLOW=2 (or more) to enable\n");
 
 2013   start_next_test (
"Each value,value,value triplet combination as toplevel, in all orders",
 
 2014                    N_VALUES * N_VALUES * N_VALUES);
 
 2016     TestTypeNode *nodes[3];
 
 2019     while ((nodes[0] = value_generator (&i)))
 
 2022         while ((nodes[1] = value_generator (&j)))
 
 2025             while ((nodes[2] = value_generator (&k)))
 
 2027                 run_test_nodes (nodes, 3);
 
 2029                 node_destroy (nodes[2]);
 
 2031             node_destroy (nodes[1]);
 
 2033         node_destroy (nodes[0]);
 
 2038   fprintf (stderr, 
"%d total iterations of recursive marshaling tests\n",
 
 2039            n_iterations_completed_total);
 
 2040   fprintf (stderr, 
"each iteration ran at initial offsets 0 through %d in both big and little endian\n",
 
 2041            MAX_INITIAL_OFFSET);
 
 2042   fprintf (stderr, 
"out of memory handling %s tested\n",
 
 2043            TEST_OOM_HANDLING ? 
"was" : 
"was not");
 
 2047 _dbus_marshal_recursive_test (
void)
 
 2049   make_and_run_test_nodes ();
 
 2062 #define MAX_MULTI_COUNT 5 
 2064 #define SAMPLE_INT16           1234 
 2065 #define SAMPLE_INT16_ALTERNATE 6785 
 2067 int16_from_seed (
int seed)
 
 2075     SAMPLE_INT16_ALTERNATE,
 
 2090 int16_write_value (TestTypeNode   *node,
 
 2098   v = int16_from_seed (seed);
 
 2101                                         node->klass->typecode,
 
 2106 int16_read_value (TestTypeNode   *node,
 
 2113   check_expected_type (reader, node->klass->typecode);
 
 2124 int16_set_value (TestTypeNode   *node,
 
 2132   v = int16_from_seed (seed);
 
 2140 int16_write_multi (TestTypeNode   *node,
 
 2151   for (i = 0; i < count; ++i)
 
 2152     values[i] = int16_from_seed (seed + i);
 
 2155                                               node->klass->typecode,
 
 2156                                               &v_ARRAY_INT16, count);
 
 2160 int16_read_multi (TestTypeNode   *node,
 
 2170   check_expected_type (reader, node->klass->typecode);
 
 2176   if (n_elements != count)
 
 2177     _dbus_warn (
"got %d elements expected %d", n_elements, count);
 
 2180   for (i = 0; i < count; i++)
 
 2182                                                       (
const unsigned char*)values + (i * 2))) ==
 
 2183                   int16_from_seed (seed + i));
 
 2189 #define SAMPLE_INT32           12345678 
 2190 #define SAMPLE_INT32_ALTERNATE 53781429 
 2192 int32_from_seed (
int seed)
 
 2200     SAMPLE_INT32_ALTERNATE,
 
 2215 int32_write_value (TestTypeNode   *node,
 
 2223   v = int32_from_seed (seed);
 
 2226                                         node->klass->typecode,
 
 2231 int32_read_value (TestTypeNode   *node,
 
 2238   check_expected_type (reader, node->klass->typecode);
 
 2249 int32_set_value (TestTypeNode   *node,
 
 2257   v = int32_from_seed (seed);
 
 2265 int32_write_multi (TestTypeNode   *node,
 
 2276   for (i = 0; i < count; ++i)
 
 2277     values[i] = int32_from_seed (seed + i);
 
 2280                                               node->klass->typecode,
 
 2281                                               &v_ARRAY_INT32, count);
 
 2285 int32_read_multi (TestTypeNode   *node,
 
 2295   check_expected_type (reader, node->klass->typecode);
 
 2301   if (n_elements != count)
 
 2302     _dbus_warn (
"got %d elements expected %d", n_elements, count);
 
 2305   for (i = 0; i < count; i++)
 
 2307                                              (
const unsigned char*)values + (i * 4))) ==
 
 2308                   int32_from_seed (seed + i));
 
 2314 int64_from_seed (
int seed)
 
 2319   v32 = int32_from_seed (seed);
 
 2328 int64_write_value (TestTypeNode   *node,
 
 2336   v = int64_from_seed (seed);
 
 2339                                         node->klass->typecode,
 
 2344 int64_read_value (TestTypeNode   *node,
 
 2351   check_expected_type (reader, node->klass->typecode);
 
 2362 int64_set_value (TestTypeNode   *node,
 
 2370   v = int64_from_seed (seed);
 
 2377 #define MAX_SAMPLE_STRING_LEN 10 
 2379 string_from_seed (
char *buf,
 
 2406   v = (
unsigned char) (
'A' + seed);
 
 2411       if (v < 'A' || v > 
'z')
 
 2424 string_write_value (TestTypeNode   *node,
 
 2429   char buf[MAX_SAMPLE_STRING_LEN + 1]=
"";
 
 2430   const char *v_string = buf;
 
 2433   string_from_seed (buf, node->klass->subclass_detail,
 
 2437                                         node->klass->typecode,
 
 2442 string_read_value (TestTypeNode   *node,
 
 2447   char buf[MAX_SAMPLE_STRING_LEN + 1];
 
 2450   check_expected_type (reader, node->klass->typecode);
 
 2453                                 (
const char **) &v);
 
 2455   string_from_seed (buf, node->klass->subclass_detail,
 
 2458   if (strcmp (buf, v) != 0)
 
 2460       _dbus_warn (
"read string '%s' expected '%s'",
 
 2469 string_set_value (TestTypeNode   *node,
 
 2474   char buf[MAX_SAMPLE_STRING_LEN + 1];
 
 2475   const char *v_string = buf;
 
 2477   string_from_seed (buf, node->klass->subclass_detail,
 
 2480 #if RECURSIVE_MARSHAL_WRITE_TRACE 
 2484    _dbus_verbose (
"SETTING new string '%s' len %d in place of '%s' len %d\n",
 
 2485                   v_string, strlen (v_string), old, strlen (old));
 
 2494 #define BOOL_FROM_SEED(seed) ((dbus_bool_t)((seed) % 2)) 
 2497 bool_write_value (TestTypeNode   *node,
 
 2504   v = BOOL_FROM_SEED (seed);
 
 2507                                         node->klass->typecode,
 
 2512 bool_read_value (TestTypeNode   *node,
 
 2518   check_expected_type (reader, node->klass->typecode);
 
 2521                                 (
unsigned char*) &v);
 
 2529 bool_set_value (TestTypeNode   *node,
 
 2536   v = BOOL_FROM_SEED (seed);
 
 2543 #define BYTE_FROM_SEED(seed) ((unsigned char) int32_from_seed (seed)) 
 2546 byte_write_value (TestTypeNode   *node,
 
 2553   v = BYTE_FROM_SEED (seed);
 
 2556                                         node->klass->typecode,
 
 2561 byte_read_value (TestTypeNode   *node,
 
 2567   check_expected_type (reader, node->klass->typecode);
 
 2570                                 (
unsigned char*) &v);
 
 2579 byte_set_value (TestTypeNode   *node,
 
 2586   v = BYTE_FROM_SEED (seed);
 
 2594 double_from_seed (
int seed)
 
 2596   return SAMPLE_INT32 * (double) seed + 0.3;
 
 2600 double_write_value (TestTypeNode   *node,
 
 2607   v = double_from_seed (seed);
 
 2610                                         node->klass->typecode,
 
 2615 double_read_value (TestTypeNode   *node,
 
 2622   check_expected_type (reader, node->klass->typecode);
 
 2627   expected = double_from_seed (seed);
 
 2631       _dbus_warn (
"Expected double %g got %g\n bits = 0x%" PRIx64 
" vs.\n bits = 0x%" PRIx64,
 
 2642 double_set_value (TestTypeNode   *node,
 
 2649   v = double_from_seed (seed);
 
 2656 #define MAX_SAMPLE_OBJECT_PATH_LEN 10 
 2658 object_path_from_seed (
char *buf,
 
 2668   v = (
unsigned char) (
'A' + seed);
 
 2680           if (v < 'A' || v > 
'z')
 
 2696 object_path_write_value (TestTypeNode   *node,
 
 2701   char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
 
 2702   const char *v_string = buf;
 
 2704   object_path_from_seed (buf, seed);
 
 2707                                         node->klass->typecode,
 
 2712 object_path_read_value (TestTypeNode   *node,
 
 2717   char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
 
 2719   check_expected_type (reader, node->klass->typecode);
 
 2722                                 (
const char **) &v);
 
 2724   object_path_from_seed (buf, seed);
 
 2726   if (strcmp (buf, v) != 0)
 
 2728       _dbus_warn (
"read object path '%s' expected '%s'",
 
 2737 object_path_set_value (TestTypeNode   *node,
 
 2742   char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
 
 2743   const char *v_string = buf;
 
 2745   object_path_from_seed (buf, seed);
 
 2752 #define MAX_SAMPLE_SIGNATURE_LEN 10 
 2754 signature_from_seed (
char *buf,
 
 2758   const char *sample_signatures[] = {
 
 2767   strcpy (buf, sample_signatures[seed % 
_DBUS_N_ELEMENTS(sample_signatures)]);
 
 2771 signature_write_value (TestTypeNode   *node,
 
 2776   char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
 
 2777   const char *v_string = buf;
 
 2779   signature_from_seed (buf, seed);
 
 2782                                         node->klass->typecode,
 
 2787 signature_read_value (TestTypeNode   *node,
 
 2792   char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
 
 2794   check_expected_type (reader, node->klass->typecode);
 
 2797                                 (
const char **) &v);
 
 2799   signature_from_seed (buf, seed);
 
 2801   if (strcmp (buf, v) != 0)
 
 2803       _dbus_warn (
"read signature value '%s' expected '%s'",
 
 2813 signature_set_value (TestTypeNode   *node,
 
 2818   char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
 
 2819   const char *v_string = buf;
 
 2821   signature_from_seed (buf, seed);
 
 2829 struct_write_value (TestTypeNode   *node,
 
 2834   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 2835   DataBlockState saved;
 
 2840   n_copies = node->klass->subclass_detail;
 
 2844   data_block_save (block, &saved);
 
 2852   while (i < n_copies)
 
 2857       while (link != 
NULL)
 
 2859           TestTypeNode *child = link->
data;
 
 2862           if (!node_write_value (child, block, &sub, seed + i))
 
 2864               data_block_restore (block, &saved);
 
 2876       data_block_restore (block, &saved);
 
 2884 struct_read_or_set_value (TestTypeNode   *node,
 
 2889   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 2894   n_copies = node->klass->subclass_detail;
 
 2901   while (i < n_copies)
 
 2906       while (link != 
NULL)
 
 2908           TestTypeNode *child = link->
data;
 
 2911           if (realign_root == 
NULL)
 
 2913               if (!node_read_value (child, &sub, seed + i))
 
 2918               if (!node_set_value (child, &sub, realign_root, seed + i))
 
 2922           if (i == (n_copies - 1) && next == 
NULL)
 
 2923             NEXT_EXPECTING_FALSE (&sub);
 
 2925             NEXT_EXPECTING_TRUE (&sub);
 
 2937 struct_read_value (TestTypeNode   *node,
 
 2941   return struct_read_or_set_value (node, reader, 
NULL, seed);
 
 2945 struct_set_value (TestTypeNode   *node,
 
 2950   return struct_read_or_set_value (node, reader, realign_root, seed);
 
 2954 struct_build_signature (TestTypeNode   *node,
 
 2957   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 2962   n_copies = node->klass->subclass_detail;
 
 2964   orig_len = _dbus_string_get_length (str);
 
 2970   while (i < n_copies)
 
 2975       while (link != 
NULL)
 
 2977           TestTypeNode *child = link->
data;
 
 2980           if (!node_build_signature (child, str))
 
 3000 array_write_value (TestTypeNode   *node,
 
 3005   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3006   DataBlockState saved;
 
 3012   TestTypeNode *child;
 
 3014   n_copies = node->klass->subclass_detail;
 
 3018   data_block_save (block, &saved);
 
 3025   if (!node_build_signature (child,
 
 3026                              &element_signature))
 
 3032                                   &element_signature, 0,
 
 3036   if (arrays_write_fixed_in_blocks &&
 
 3038       child->klass->write_multi)
 
 3040       if (!node_write_multi (child, block, &sub, seed, n_copies))
 
 3046       while (i < n_copies)
 
 3051           while (link != 
NULL)
 
 3053               TestTypeNode *child2 = link->
data;
 
 3056               if (!node_write_value (child2, block, &sub, seed + i))
 
 3073   data_block_restore (block, &saved);
 
 3079 array_read_or_set_value (TestTypeNode   *node,
 
 3084   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3088   TestTypeNode *child;
 
 3090   n_copies = node->klass->subclass_detail;
 
 3100       if (realign_root == 
NULL && arrays_write_fixed_in_blocks &&
 
 3102           child->klass->read_multi)
 
 3104           if (!node_read_multi (child, &sub, seed, n_copies))
 
 3110           while (i < n_copies)
 
 3115               while (link != 
NULL)
 
 3117                   TestTypeNode *child2 = link->
data;
 
 3123                   if (realign_root == 
NULL)
 
 3125                       if (!node_read_value (child2, &sub, seed + i))
 
 3130                       if (!node_set_value (child2, &sub, realign_root, seed + i))
 
 3134                   if (i == (n_copies - 1) && next == 
NULL)
 
 3135                     NEXT_EXPECTING_FALSE (&sub);
 
 3137                     NEXT_EXPECTING_TRUE (&sub);
 
 3151 array_read_value (TestTypeNode   *node,
 
 3155   return array_read_or_set_value (node, reader, 
NULL, seed);
 
 3159 array_set_value (TestTypeNode   *node,
 
 3164   return array_read_or_set_value (node, reader, realign_root, seed);
 
 3168 array_build_signature (TestTypeNode   *node,
 
 3171   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3174   orig_len = _dbus_string_get_length (str);
 
 3191 #define VARIANT_SEED 10 
 3194 variant_write_value (TestTypeNode   *node,
 
 3199   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3200   DataBlockState saved;
 
 3203   TestTypeNode *child;
 
 3210   data_block_save (block, &saved);
 
 3215   if (!node_build_signature (child,
 
 3216                              &content_signature))
 
 3220                                   &content_signature, 0,
 
 3224   if (!node_write_value (child, block, &sub, seed + VARIANT_SEED))
 
 3234   data_block_restore (block, &saved);
 
 3240 variant_read_or_set_value (TestTypeNode   *node,
 
 3245   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3247   TestTypeNode *child;
 
 3258   if (realign_root == 
NULL)
 
 3260       if (!node_read_value (child, &sub, seed + VARIANT_SEED))
 
 3265       if (!node_set_value (child, &sub, realign_root, seed + VARIANT_SEED))
 
 3269   NEXT_EXPECTING_FALSE (&sub);
 
 3275 variant_read_value (TestTypeNode   *node,
 
 3279   return variant_read_or_set_value (node, reader, 
NULL, seed);
 
 3283 variant_set_value (TestTypeNode   *node,
 
 3288   return variant_read_or_set_value (node, reader, realign_root, seed);
 
 3292 dict_write_value (TestTypeNode   *node,
 
 3297   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3298   DataBlockState saved;
 
 3304   TestTypeNode *child;
 
 3306   n_entries = node->klass->subclass_detail;
 
 3310   data_block_save (block, &saved);
 
 3323   if (!node_build_signature (child,
 
 3324                              &entry_value_signature))
 
 3333                           &dict_entry_signature,
 
 3334                           _dbus_string_get_length (&dict_entry_signature)))
 
 3342                                   &dict_entry_signature, 0,
 
 3347   while (i < n_entries)
 
 3357       key = int32_from_seed (seed + i);
 
 3364       if (!node_write_value (child, block, &entry_sub, seed + i))
 
 3381   data_block_restore (block, &saved);
 
 3388 dict_read_or_set_value (TestTypeNode   *node,
 
 3393   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3397   TestTypeNode *child;
 
 3399   n_entries = node->klass->subclass_detail;
 
 3412       while (i < n_entries)
 
 3420           if (realign_root == 
NULL)
 
 3431               NEXT_EXPECTING_TRUE (&entry_sub);
 
 3433               if (!node_read_value (child, &entry_sub, seed + i))
 
 3436               NEXT_EXPECTING_FALSE (&entry_sub);
 
 3442               v = int32_from_seed (seed + i);
 
 3449               NEXT_EXPECTING_TRUE (&entry_sub);
 
 3451               if (!node_set_value (child, &entry_sub, realign_root, seed + i))
 
 3454               NEXT_EXPECTING_FALSE (&entry_sub);
 
 3457           if (i == (n_entries - 1))
 
 3458             NEXT_EXPECTING_FALSE (&sub);
 
 3460             NEXT_EXPECTING_TRUE (&sub);
 
 3470 dict_read_value (TestTypeNode   *node,
 
 3474   return dict_read_or_set_value (node, reader, 
NULL, seed);
 
 3478 dict_set_value (TestTypeNode   *node,
 
 3483   return dict_read_or_set_value (node, reader, realign_root, seed);
 
 3487 dict_build_signature (TestTypeNode   *node,
 
 3490   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3493   orig_len = _dbus_string_get_length (str);
 
 3516 container_destroy (TestTypeNode *node)
 
 3518   TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
 
 3522   while (link != 
NULL)
 
 3524       TestTypeNode *child = link->
data;
 
 3527       node_destroy (child);