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"
41 basic_value_equal (
int type,
49 return strcmp (lhs->
str, rhs->
str) == 0;
53 return lhs->
u64 == rhs->
u64;
67 if (lhs_type != rhs_type)
78 basic_value_zero (&lhs_value);
79 basic_value_zero (&rhs_value);
84 return basic_value_equal (lhs_type, &lhs_value, &rhs_value);
94 return equal_values_helper (&lhs_sub, &rhs_sub);
112 return equal_values_helper (©_lhs, ©_rhs);
117 #ifndef DOXYGEN_SHOULD_SKIP_THIS
119 #include "dbus-test.h"
120 #include "dbus-list.h"
125 #define TEST_OOM_HANDLING 0
129 #define MAX_INITIAL_OFFSET 9
135 #define MAX_ITERATIONS_FOR_EXPENSIVE_TESTS 1000
151 #define N_FENCE_BYTES 5
152 #define FENCE_BYTES_STR "abcde"
153 #define INITIAL_PADDING_BYTE '\0'
156 data_block_init (DataBlock *block,
170 INITIAL_PADDING_BYTE) ||
172 INITIAL_PADDING_BYTE) ||
181 block->byte_order = byte_order;
182 block->initial_offset = initial_offset;
188 data_block_save (DataBlock *block,
189 DataBlockState *state)
191 state->saved_sig_len = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES;
192 state->saved_body_len = _dbus_string_get_length (&block->body) - N_FENCE_BYTES;
196 data_block_restore (DataBlock *block,
197 DataBlockState *state)
200 state->saved_sig_len,
201 _dbus_string_get_length (&block->signature) - state->saved_sig_len - N_FENCE_BYTES);
203 state->saved_body_len,
204 _dbus_string_get_length (&block->body) - state->saved_body_len - N_FENCE_BYTES);
208 data_block_verify (DataBlock *block)
215 offset = _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - 8;
221 _dbus_string_get_length (&block->signature) - offset);
229 offset = _dbus_string_get_length (&block->body) - N_FENCE_BYTES - 8;
235 _dbus_string_get_length (&block->body) - offset);
240 0, block->initial_offset));
242 0, block->initial_offset));
246 data_block_free (DataBlock *block)
248 data_block_verify (block);
255 data_block_reset (DataBlock *block)
257 data_block_verify (block);
260 block->initial_offset,
261 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES - block->initial_offset);
263 block->initial_offset,
264 _dbus_string_get_length (&block->body) - N_FENCE_BYTES - block->initial_offset);
266 data_block_verify (block);
270 data_block_init_reader_writer (DataBlock *block,
278 block->initial_offset,
280 block->initial_offset);
286 _dbus_string_get_length (&block->signature) - N_FENCE_BYTES,
288 _dbus_string_get_length (&block->body) - N_FENCE_BYTES);
294 const char *funcname,
303 _dbus_warn (
"Read type %s while expecting %s at %s line %d\n",
312 #define check_expected_type(reader, expected) real_check_expected_type (reader, expected, _DBUS_FUNCTION_NAME, __LINE__)
314 #define NEXT_EXPECTING_TRUE(reader) do { if (!_dbus_type_reader_next (reader)) \
316 _dbus_warn ("_dbus_type_reader_next() should have returned TRUE at %s %d\n", \
317 _DBUS_FUNCTION_NAME, __LINE__); \
318 _dbus_assert_not_reached ("test failed"); \
322 #define NEXT_EXPECTING_FALSE(reader) do { if (_dbus_type_reader_next (reader)) \
324 _dbus_warn ("_dbus_type_reader_next() should have returned FALSE at %s %d\n", \
325 _DBUS_FUNCTION_NAME, __LINE__); \
326 _dbus_assert_not_reached ("test failed"); \
328 check_expected_type (reader, DBUS_TYPE_INVALID); \
331 typedef struct TestTypeNode TestTypeNode;
332 typedef struct TestTypeNodeClass TestTypeNodeClass;
333 typedef struct TestTypeNodeContainer TestTypeNodeContainer;
334 typedef struct TestTypeNodeContainerClass TestTypeNodeContainerClass;
338 const TestTypeNodeClass *klass;
341 struct TestTypeNodeContainer
347 struct TestTypeNodeClass
356 void (* destroy) (TestTypeNode *node);
369 dbus_bool_t (* build_signature) (TestTypeNode *node,
382 struct TestTypeNodeContainerClass
384 TestTypeNodeClass base;
392 static dbus_bool_t int16_write_value (TestTypeNode *node,
396 static dbus_bool_t int16_read_value (TestTypeNode *node,
399 static dbus_bool_t int16_set_value (TestTypeNode *node,
403 static dbus_bool_t int16_write_multi (TestTypeNode *node,
408 static dbus_bool_t int16_read_multi (TestTypeNode *node,
412 static dbus_bool_t int32_write_value (TestTypeNode *node,
416 static dbus_bool_t int32_read_value (TestTypeNode *node,
419 static dbus_bool_t int32_set_value (TestTypeNode *node,
423 static dbus_bool_t int32_write_multi (TestTypeNode *node,
428 static dbus_bool_t int32_read_multi (TestTypeNode *node,
432 static dbus_bool_t int64_write_value (TestTypeNode *node,
436 static dbus_bool_t int64_read_value (TestTypeNode *node,
439 static dbus_bool_t int64_set_value (TestTypeNode *node,
443 static dbus_bool_t string_write_value (TestTypeNode *node,
447 static dbus_bool_t string_read_value (TestTypeNode *node,
450 static dbus_bool_t string_set_value (TestTypeNode *node,
454 static dbus_bool_t bool_write_value (TestTypeNode *node,
458 static dbus_bool_t bool_read_value (TestTypeNode *node,
461 static dbus_bool_t bool_set_value (TestTypeNode *node,
465 static dbus_bool_t byte_write_value (TestTypeNode *node,
469 static dbus_bool_t byte_read_value (TestTypeNode *node,
472 static dbus_bool_t byte_set_value (TestTypeNode *node,
476 static dbus_bool_t double_write_value (TestTypeNode *node,
480 static dbus_bool_t double_read_value (TestTypeNode *node,
483 static dbus_bool_t double_set_value (TestTypeNode *node,
487 static dbus_bool_t object_path_write_value (TestTypeNode *node,
491 static dbus_bool_t object_path_read_value (TestTypeNode *node,
494 static dbus_bool_t object_path_set_value (TestTypeNode *node,
498 static dbus_bool_t signature_write_value (TestTypeNode *node,
502 static dbus_bool_t signature_read_value (TestTypeNode *node,
505 static dbus_bool_t signature_set_value (TestTypeNode *node,
509 static dbus_bool_t struct_write_value (TestTypeNode *node,
513 static dbus_bool_t struct_read_value (TestTypeNode *node,
516 static dbus_bool_t struct_set_value (TestTypeNode *node,
520 static dbus_bool_t struct_build_signature (TestTypeNode *node,
522 static dbus_bool_t dict_write_value (TestTypeNode *node,
526 static dbus_bool_t dict_read_value (TestTypeNode *node,
529 static dbus_bool_t dict_set_value (TestTypeNode *node,
533 static dbus_bool_t dict_build_signature (TestTypeNode *node,
535 static dbus_bool_t array_write_value (TestTypeNode *node,
539 static dbus_bool_t array_read_value (TestTypeNode *node,
542 static dbus_bool_t array_set_value (TestTypeNode *node,
546 static dbus_bool_t array_build_signature (TestTypeNode *node,
548 static dbus_bool_t variant_write_value (TestTypeNode *node,
552 static dbus_bool_t variant_read_value (TestTypeNode *node,
555 static dbus_bool_t variant_set_value (TestTypeNode *node,
559 static void container_destroy (TestTypeNode *node);
563 static const TestTypeNodeClass int16_class = {
565 sizeof (TestTypeNode),
577 static const TestTypeNodeClass uint16_class = {
579 sizeof (TestTypeNode),
591 static const TestTypeNodeClass int32_class = {
593 sizeof (TestTypeNode),
605 static const TestTypeNodeClass uint32_class = {
607 sizeof (TestTypeNode),
619 static const TestTypeNodeClass int64_class = {
621 sizeof (TestTypeNode),
633 static const TestTypeNodeClass uint64_class = {
635 sizeof (TestTypeNode),
647 static const TestTypeNodeClass string_0_class = {
649 sizeof (TestTypeNode),
661 static const TestTypeNodeClass string_1_class = {
663 sizeof (TestTypeNode),
676 static const TestTypeNodeClass string_3_class = {
678 sizeof (TestTypeNode),
691 static const TestTypeNodeClass string_8_class = {
693 sizeof (TestTypeNode),
705 static const TestTypeNodeClass bool_class = {
707 sizeof (TestTypeNode),
719 static const TestTypeNodeClass byte_class = {
721 sizeof (TestTypeNode),
733 static const TestTypeNodeClass double_class = {
735 sizeof (TestTypeNode),
747 static const TestTypeNodeClass object_path_class = {
749 sizeof (TestTypeNode),
753 object_path_write_value,
754 object_path_read_value,
755 object_path_set_value,
761 static const TestTypeNodeClass signature_class = {
763 sizeof (TestTypeNode),
767 signature_write_value,
768 signature_read_value,
775 static const TestTypeNodeClass struct_1_class = {
777 sizeof (TestTypeNodeContainer),
784 struct_build_signature,
789 static const TestTypeNodeClass struct_2_class = {
791 sizeof (TestTypeNodeContainer),
798 struct_build_signature,
803 static const TestTypeNodeClass dict_1_class = {
805 sizeof (TestTypeNodeContainer),
812 dict_build_signature,
819 static const TestTypeNodeClass array_0_class = {
821 sizeof (TestTypeNodeContainer),
828 array_build_signature,
833 static const TestTypeNodeClass array_1_class = {
835 sizeof (TestTypeNodeContainer),
842 array_build_signature,
847 static const TestTypeNodeClass array_2_class = {
849 sizeof (TestTypeNodeContainer),
856 array_build_signature,
861 static const TestTypeNodeClass array_9_class = {
863 sizeof (TestTypeNodeContainer),
870 array_build_signature,
875 static const TestTypeNodeClass variant_class = {
877 sizeof (TestTypeNodeContainer),
889 static const TestTypeNodeClass*
const
907 #define N_BASICS (_DBUS_N_ELEMENTS (basic_nodes))
909 static const TestTypeNodeClass*
const
910 container_nodes[] = {
922 #define N_CONTAINERS (_DBUS_N_ELEMENTS (container_nodes))
925 node_new (
const TestTypeNodeClass *klass)
935 if (klass->construct)
937 if (!(* klass->construct) (node))
948 node_destroy (TestTypeNode *node)
950 if (node->klass->destroy)
951 (* node->klass->destroy) (node);
956 node_write_value (TestTypeNode *node,
963 retval = (* node->klass->write_value) (node, block, writer, seed);
967 data_block_verify (block);
974 node_read_value (TestTypeNode *node,
980 if (!(* node->klass->read_value) (node, reader, seed))
991 node_set_value (TestTypeNode *node,
996 if (!(* node->klass->set_value) (node, reader, realign_root, seed))
1003 node_build_signature (TestTypeNode *node,
1006 if (node->klass->build_signature)
1007 return (* node->klass->build_signature) (node, str);
1013 node_append_child (TestTypeNode *node,
1014 TestTypeNode *child)
1016 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
1018 _dbus_assert (node->klass->instance_size >= (
int) sizeof (TestTypeNodeContainer));
1027 node_write_multi (TestTypeNode *node,
1036 retval = (* node->klass->write_multi) (node, block, writer, seed, n_copies);
1040 data_block_verify (block);
1047 node_read_multi (TestTypeNode *node,
1054 if (!(* node->klass->read_multi) (node, reader, seed, n_copies))
1060 static int n_iterations_completed_total = 0;
1061 static int n_iterations_completed_this_test = 0;
1062 static int n_iterations_expected_this_test = 0;
1069 TestTypeNode **nodes;
1071 } NodeIterationData;
1074 run_test_copy (NodeIterationData *nid)
1082 _dbus_verbose (
"\n");
1088 if (!data_block_init (&dest, src->byte_order, src->initial_offset))
1091 data_block_init_reader_writer (src, &reader,
NULL);
1092 data_block_init_reader_writer (&dest,
NULL, &writer);
1098 dest.initial_offset,
'\0'))
1107 _dbus_verbose (
"SOURCE\n");
1109 _dbus_string_get_length (&src->signature));
1110 _dbus_verbose (
"DEST\n");
1112 _dbus_string_get_length (&dest.signature));
1118 _dbus_verbose (
"SOURCE\n");
1120 _dbus_string_get_length (&src->body));
1121 _dbus_verbose (
"DEST\n");
1123 _dbus_string_get_length (&dest.body));
1131 data_block_free (&dest);
1137 run_test_values_only_write (NodeIterationData *nid)
1145 _dbus_verbose (
"\n");
1149 data_block_reset (nid->block);
1151 sig_len = _dbus_string_get_length (nid->signature);
1157 _dbus_string_get_length (&nid->block->body) - N_FENCE_BYTES);
1162 nid->block->initial_offset);
1165 while (i < nid->n_nodes)
1167 if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
1174 _dbus_assert (sig_len == _dbus_string_get_length (nid->signature));
1178 while (i < nid->n_nodes)
1180 if (!node_read_value (nid->nodes[i], &reader, i))
1183 if (i + 1 == nid->n_nodes)
1184 NEXT_EXPECTING_FALSE (&reader);
1186 NEXT_EXPECTING_TRUE (&reader);
1194 data_block_reset (nid->block);
1206 run_test_set_values (NodeIterationData *nid)
1213 _dbus_verbose (
"\n");
1217 data_block_init_reader_writer (nid->block,
1220 realign_root = reader;
1223 while (i < nid->n_nodes)
1225 if (!node_set_value (nid->nodes[i],
1226 &reader, &realign_root,
1230 if (i + 1 == nid->n_nodes)
1231 NEXT_EXPECTING_FALSE (&reader);
1233 NEXT_EXPECTING_TRUE (&reader);
1240 reader = realign_root;
1243 while (i < nid->n_nodes)
1245 if (!node_read_value (nid->nodes[i], &reader,
1249 if (i + 1 == nid->n_nodes)
1250 NEXT_EXPECTING_FALSE (&reader);
1252 NEXT_EXPECTING_TRUE (&reader);
1264 run_test_delete_values (NodeIterationData *nid)
1270 _dbus_verbose (
"\n");
1274 data_block_init_reader_writer (nid->block,
1299 _dbus_verbose (
"recursing into deletion loop reader.value_pos = %d array.value_pos = %d array.u.start_pos = %d\n",
1304 static int cycle = 0;
1310 if (elem == 3 || elem >= n_elements)
1311 elem = n_elements - 1;
1313 _dbus_verbose (
"deleting array element %d of %d type %s cycle %d reader pos %d elem pos %d\n",
1341 data_block_init_reader_writer (nid->block,
1356 run_test_nodes_iteration (
void *data)
1358 NodeIterationData *nid = data;
1372 data_block_init_reader_writer (nid->block,
1379 nid->type_offset,
'\0'))
1383 while (i < nid->n_nodes)
1385 if (!node_write_value (nid->nodes[i], nid->block, &writer, i))
1392 &nid->block->signature, nid->type_offset))
1394 _dbus_warn (
"Expected signature '%s' and got '%s' with initial offset %d\n",
1395 _dbus_string_get_const_data (nid->signature),
1396 _dbus_string_get_const_data_len (&nid->block->signature, nid->type_offset, 0),
1402 while (i < nid->n_nodes)
1404 if (!node_read_value (nid->nodes[i], &reader, i))
1407 if (i + 1 == nid->n_nodes)
1408 NEXT_EXPECTING_FALSE (&reader);
1410 NEXT_EXPECTING_TRUE (&reader);
1415 if (n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
1424 if (!run_test_set_values (nid))
1427 if (!run_test_delete_values (nid))
1430 if (!run_test_copy (nid))
1433 if (!run_test_values_only_write (nid))
1445 data_block_reset (nid->block);
1451 run_test_nodes_in_one_configuration (TestTypeNode **nodes,
1458 NodeIterationData nid;
1460 if (!data_block_init (&block, byte_order, initial_offset))
1463 nid.signature = signature;
1465 nid.type_offset = initial_offset;
1467 nid.n_nodes = n_nodes;
1469 if (TEST_OOM_HANDLING &&
1470 n_iterations_expected_this_test <= MAX_ITERATIONS_FOR_EXPENSIVE_TESTS)
1472 _dbus_test_oom_handling (
"running test node",
1473 run_test_nodes_iteration,
1478 if (!run_test_nodes_iteration (&nid))
1482 data_block_free (&block);
1486 run_test_nodes (TestTypeNode **nodes,
1498 if (! node_build_signature (nodes[i], &signature))
1504 _dbus_verbose (
">>> test nodes with signature '%s'\n",
1505 _dbus_string_get_const_data (&signature));
1508 while (i <= MAX_INITIAL_OFFSET)
1510 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
1512 run_test_nodes_in_one_configuration (nodes, n_nodes, &signature,
1518 n_iterations_completed_this_test += 1;
1519 n_iterations_completed_total += 1;
1521 if (n_iterations_completed_this_test == n_iterations_expected_this_test)
1523 fprintf (stderr,
" 100%% %d this test (%d cumulative)\n",
1524 n_iterations_completed_this_test,
1525 n_iterations_completed_total);
1528 else if ((n_iterations_completed_this_test %
1529 (
int)(n_iterations_expected_this_test / 10.0)) == 1)
1531 fprintf (stderr,
" %d%% ", (
int) (n_iterations_completed_this_test / (
double) n_iterations_expected_this_test * 100));
1537 #define N_VALUES (N_BASICS * N_CONTAINERS + N_BASICS)
1539 static TestTypeNode*
1540 value_generator (
int *ip)
1543 const TestTypeNodeClass *child_klass;
1544 const TestTypeNodeClass *container_klass;
1545 TestTypeNode *child;
1554 else if (i < N_BASICS)
1556 node = node_new (basic_nodes[i]);
1570 container_klass = container_nodes[i / N_BASICS];
1571 child_klass = basic_nodes[i % N_BASICS];
1573 node = node_new (container_klass);
1574 child = node_new (child_klass);
1576 node_append_child (node, child);
1585 build_body (TestTypeNode **nodes,
1599 if (! node_build_signature (nodes[i], signature))
1605 if (!data_block_init (&block, byte_order, 0))
1608 data_block_init_reader_writer (&block,
1621 if (!node_write_value (nodes[i], &block, &writer, i))
1628 _dbus_string_get_length (&block.body) - N_FENCE_BYTES,
1632 data_block_free (&block);
1636 dbus_internal_do_not_use_generate_bodies (
int sequence,
1641 TestTypeNode *nodes[1];
1645 nodes[0] = value_generator (&sequence);
1647 if (nodes[0] ==
NULL)
1652 build_body (nodes, n_nodes, byte_order, signature, body);
1658 node_destroy (nodes[i]);
1666 make_and_run_values_inside_container (
const TestTypeNodeClass *container_klass,
1670 TestTypeNode *container;
1671 TestTypeNode *child;
1674 root = node_new (container_klass);
1676 for (i = 1; i < n_nested; i++)
1678 child = node_new (container_klass);
1679 node_append_child (container, child);
1686 while ((child = value_generator (&i)))
1688 node_append_child (container, child);
1690 run_test_nodes (&root, 1);
1693 node_destroy (child);
1696 node_destroy (root);
1700 start_next_test (
const char *format,
1703 n_iterations_completed_this_test = 0;
1704 n_iterations_expected_this_test = expected;
1706 fprintf (stderr,
">>> >>> ");
1707 fprintf (stderr, format,
1708 n_iterations_expected_this_test);
1712 make_and_run_test_nodes (
void)
1748 start_next_test (
"Each value by itself %d iterations\n", N_VALUES);
1752 while ((node = value_generator (&i)))
1754 run_test_nodes (&node, 1);
1756 node_destroy (node);
1760 start_next_test (
"Each value by itself with arrays as blocks %d iterations\n", N_VALUES);
1761 arrays_write_fixed_in_blocks =
TRUE;
1765 while ((node = value_generator (&i)))
1767 run_test_nodes (&node, 1);
1769 node_destroy (node);
1772 arrays_write_fixed_in_blocks =
FALSE;
1774 start_next_test (
"All values in one big toplevel %d iteration\n", 1);
1776 TestTypeNode *nodes[N_VALUES];
1779 while ((nodes[i] = value_generator (&i)))
1782 run_test_nodes (nodes, N_VALUES);
1784 for (i = 0; i < N_VALUES; i++)
1785 node_destroy (nodes[i]);
1788 start_next_test (
"Each value,value pair combination as toplevel, in both orders %d iterations\n",
1789 N_VALUES * N_VALUES);
1791 TestTypeNode *nodes[2];
1794 while ((nodes[0] = value_generator (&i)))
1797 while ((nodes[1] = value_generator (&j)))
1799 run_test_nodes (nodes, 2);
1801 node_destroy (nodes[1]);
1804 node_destroy (nodes[0]);
1808 start_next_test (
"Each container containing each value %d iterations\n",
1809 N_CONTAINERS * N_VALUES);
1810 for (i = 0; i < N_CONTAINERS; i++)
1812 const TestTypeNodeClass *container_klass = container_nodes[i];
1814 make_and_run_values_inside_container (container_klass, 1);
1817 start_next_test (
"Each container containing each value with arrays as blocks %d iterations\n",
1818 N_CONTAINERS * N_VALUES);
1819 arrays_write_fixed_in_blocks =
TRUE;
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);
1826 arrays_write_fixed_in_blocks =
FALSE;
1828 start_next_test (
"Each container of same container of each value %d iterations\n",
1829 N_CONTAINERS * N_VALUES);
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, 2);
1837 start_next_test (
"Each container of same container of same container of each value %d iterations\n",
1838 N_CONTAINERS * N_VALUES);
1839 for (i = 0; i < N_CONTAINERS; i++)
1841 const TestTypeNodeClass *container_klass = container_nodes[i];
1843 make_and_run_values_inside_container (container_klass, 3);
1846 start_next_test (
"Each value,value pair inside a struct %d iterations\n",
1847 N_VALUES * N_VALUES);
1849 TestTypeNode *val1, *val2;
1852 node = node_new (&struct_1_class);
1855 while ((val1 = value_generator (&i)))
1858 while ((val2 = value_generator (&j)))
1860 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
1862 node_append_child (node, val1);
1863 node_append_child (node, val2);
1865 run_test_nodes (&node, 1);
1868 node_destroy (val2);
1870 node_destroy (val1);
1872 node_destroy (node);
1875 start_next_test (
"All values in one big struct %d iteration\n",
1879 TestTypeNode *child;
1881 node = node_new (&struct_1_class);
1884 while ((child = value_generator (&i)))
1885 node_append_child (node, child);
1887 run_test_nodes (&node, 1);
1889 node_destroy (node);
1892 start_next_test (
"Each value in a large array %d iterations\n",
1898 node = node_new (&array_9_class);
1901 while ((val = value_generator (&i)))
1903 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
1905 node_append_child (node, val);
1907 run_test_nodes (&node, 1);
1913 node_destroy (node);
1919 fprintf (stderr,
"skipping remaining marshal-recursive tests, "
1920 "run with DBUS_TEST_SLOW=1 (or more) to enable\n");
1924 start_next_test (
"Each container of each container of each value %d iterations\n",
1925 N_CONTAINERS * N_CONTAINERS * N_VALUES);
1926 for (i = 0; i < N_CONTAINERS; i++)
1928 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
1929 TestTypeNode *outer_container = node_new (outer_container_klass);
1931 for (j = 0; j < N_CONTAINERS; j++)
1933 TestTypeNode *child;
1934 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
1935 TestTypeNode *inner_container = node_new (inner_container_klass);
1937 node_append_child (outer_container, inner_container);
1940 while ((child = value_generator (&m)))
1942 node_append_child (inner_container, child);
1944 run_test_nodes (&outer_container, 1);
1947 node_destroy (child);
1950 node_destroy (inner_container);
1952 node_destroy (outer_container);
1955 start_next_test (
"Each container of each container of each container of each value %d iterations\n",
1956 N_CONTAINERS * N_CONTAINERS * N_CONTAINERS * N_VALUES);
1957 for (i = 0; i < N_CONTAINERS; i++)
1959 const TestTypeNodeClass *outer_container_klass = container_nodes[i];
1960 TestTypeNode *outer_container = node_new (outer_container_klass);
1962 for (j = 0; j < N_CONTAINERS; j++)
1964 const TestTypeNodeClass *inner_container_klass = container_nodes[j];
1965 TestTypeNode *inner_container = node_new (inner_container_klass);
1967 node_append_child (outer_container, inner_container);
1969 for (k = 0; k < N_CONTAINERS; k++)
1971 TestTypeNode *child;
1972 const TestTypeNodeClass *center_container_klass = container_nodes[k];
1973 TestTypeNode *center_container = node_new (center_container_klass);
1975 node_append_child (inner_container, center_container);
1978 while ((child = value_generator (&m)))
1980 node_append_child (center_container, child);
1982 run_test_nodes (&outer_container, 1);
1985 node_destroy (child);
1988 node_destroy (center_container);
1991 node_destroy (inner_container);
1993 node_destroy (outer_container);
2000 fprintf (stderr,
"skipping really slow marshal-recursive test, "
2001 "run with DBUS_TEST_SLOW=2 (or more) to enable\n");
2005 start_next_test (
"Each value,value,value triplet combination as toplevel, in all orders %d iterations\n",
2006 N_VALUES * N_VALUES * N_VALUES);
2008 TestTypeNode *nodes[3];
2011 while ((nodes[0] = value_generator (&i)))
2014 while ((nodes[1] = value_generator (&j)))
2017 while ((nodes[2] = value_generator (&k)))
2019 run_test_nodes (nodes, 3);
2021 node_destroy (nodes[2]);
2023 node_destroy (nodes[1]);
2025 node_destroy (nodes[0]);
2030 fprintf (stderr,
"%d total iterations of recursive marshaling tests\n",
2031 n_iterations_completed_total);
2032 fprintf (stderr,
"each iteration ran at initial offsets 0 through %d in both big and little endian\n",
2033 MAX_INITIAL_OFFSET);
2034 fprintf (stderr,
"out of memory handling %s tested\n",
2035 TEST_OOM_HANDLING ?
"was" :
"was not");
2039 _dbus_marshal_recursive_test (
void)
2041 make_and_run_test_nodes ();
2054 #define MAX_MULTI_COUNT 5
2056 #define SAMPLE_INT16 1234
2057 #define SAMPLE_INT16_ALTERNATE 6785
2059 int16_from_seed (
int seed)
2074 v = SAMPLE_INT16_ALTERNATE;
2094 int16_write_value (TestTypeNode *node,
2102 v = int16_from_seed (seed);
2105 node->klass->typecode,
2110 int16_read_value (TestTypeNode *node,
2117 check_expected_type (reader, node->klass->typecode);
2120 (dbus_int16_t*) &v);
2128 int16_set_value (TestTypeNode *node,
2136 v = int16_from_seed (seed);
2144 int16_write_multi (TestTypeNode *node,
2151 dbus_int16_t values[MAX_MULTI_COUNT];
2152 dbus_int16_t *v_ARRAY_INT16 = values;
2155 for (i = 0; i < count; ++i)
2156 values[i] = int16_from_seed (seed + i);
2159 node->klass->typecode,
2160 &v_ARRAY_INT16, count);
2164 int16_read_multi (TestTypeNode *node,
2170 dbus_int16_t *values;
2174 check_expected_type (reader, node->klass->typecode);
2180 if (n_elements != count)
2181 _dbus_warn (
"got %d elements expected %d\n", n_elements, count);
2184 for (i = 0; i < count; i++)
2186 (
const unsigned char*)values + (i * 2))) ==
2187 int16_from_seed (seed + i));
2193 #define SAMPLE_INT32 12345678
2194 #define SAMPLE_INT32_ALTERNATE 53781429
2196 int32_from_seed (
int seed)
2211 v = SAMPLE_INT32_ALTERNATE;
2231 int32_write_value (TestTypeNode *node,
2239 v = int32_from_seed (seed);
2242 node->klass->typecode,
2247 int32_read_value (TestTypeNode *node,
2254 check_expected_type (reader, node->klass->typecode);
2257 (dbus_int32_t*) &v);
2265 int32_set_value (TestTypeNode *node,
2273 v = int32_from_seed (seed);
2281 int32_write_multi (TestTypeNode *node,
2288 dbus_int32_t values[MAX_MULTI_COUNT];
2289 dbus_int32_t *v_ARRAY_INT32 = values;
2292 for (i = 0; i < count; ++i)
2293 values[i] = int32_from_seed (seed + i);
2296 node->klass->typecode,
2297 &v_ARRAY_INT32, count);
2301 int32_read_multi (TestTypeNode *node,
2307 dbus_int32_t *values;
2311 check_expected_type (reader, node->klass->typecode);
2317 if (n_elements != count)
2318 _dbus_warn (
"got %d elements expected %d\n", n_elements, count);
2321 for (i = 0; i < count; i++)
2323 (
const unsigned char*)values + (i * 4))) ==
2324 int32_from_seed (seed + i));
2330 int64_from_seed (
int seed)
2335 v32 = int32_from_seed (seed);
2337 v = - (dbus_int32_t) ~ v32;
2338 v |= (((dbus_int64_t)v32) << 32);
2344 int64_write_value (TestTypeNode *node,
2352 v = int64_from_seed (seed);
2355 node->klass->typecode,
2360 int64_read_value (TestTypeNode *node,
2367 check_expected_type (reader, node->klass->typecode);
2370 (dbus_int64_t*) &v);
2378 int64_set_value (TestTypeNode *node,
2386 v = int64_from_seed (seed);
2393 #define MAX_SAMPLE_STRING_LEN 10
2395 string_from_seed (
char *buf,
2419 v = (
unsigned char) (
'A' + seed);
2424 if (v < 'A' || v >
'z')
2437 string_write_value (TestTypeNode *node,
2442 char buf[MAX_SAMPLE_STRING_LEN + 1]=
"";
2443 const char *v_string = buf;
2446 string_from_seed (buf, node->klass->subclass_detail,
2450 node->klass->typecode,
2455 string_read_value (TestTypeNode *node,
2460 char buf[MAX_SAMPLE_STRING_LEN + 1];
2463 check_expected_type (reader, node->klass->typecode);
2466 (
const char **) &v);
2468 string_from_seed (buf, node->klass->subclass_detail,
2471 if (strcmp (buf, v) != 0)
2473 _dbus_warn (
"read string '%s' expected '%s'\n",
2482 string_set_value (TestTypeNode *node,
2487 char buf[MAX_SAMPLE_STRING_LEN + 1];
2488 const char *v_string = buf;
2490 string_from_seed (buf, node->klass->subclass_detail,
2493 #if RECURSIVE_MARSHAL_WRITE_TRACE
2497 _dbus_verbose (
"SETTING new string '%s' len %d in place of '%s' len %d\n",
2498 v_string, strlen (v_string), old, strlen (old));
2507 #define BOOL_FROM_SEED(seed) ((dbus_bool_t)((seed) % 2))
2510 bool_write_value (TestTypeNode *node,
2517 v = BOOL_FROM_SEED (seed);
2520 node->klass->typecode,
2525 bool_read_value (TestTypeNode *node,
2531 check_expected_type (reader, node->klass->typecode);
2534 (
unsigned char*) &v);
2542 bool_set_value (TestTypeNode *node,
2549 v = BOOL_FROM_SEED (seed);
2556 #define BYTE_FROM_SEED(seed) ((unsigned char) int32_from_seed (seed))
2559 byte_write_value (TestTypeNode *node,
2566 v = BYTE_FROM_SEED (seed);
2569 node->klass->typecode,
2574 byte_read_value (TestTypeNode *node,
2580 check_expected_type (reader, node->klass->typecode);
2583 (
unsigned char*) &v);
2592 byte_set_value (TestTypeNode *node,
2599 v = BYTE_FROM_SEED (seed);
2607 double_from_seed (
int seed)
2609 return SAMPLE_INT32 * (double) seed + 0.3;
2613 double_write_value (TestTypeNode *node,
2620 v = double_from_seed (seed);
2623 node->klass->typecode,
2628 double_read_value (TestTypeNode *node,
2635 check_expected_type (reader, node->klass->typecode);
2640 expected = double_from_seed (seed);
2644 #ifdef DBUS_INT64_PRINTF_MODIFIER
2645 _dbus_warn (
"Expected double %g got %g\n bits = 0x%" DBUS_INT64_PRINTF_MODIFIER
"x vs.\n bits = 0x%" DBUS_INT64_PRINTF_MODIFIER
"x)\n",
2647 *(dbus_uint64_t*)(
char*)&expected,
2648 *(dbus_uint64_t*)(
char*)&v);
2657 double_set_value (TestTypeNode *node,
2664 v = double_from_seed (seed);
2671 #define MAX_SAMPLE_OBJECT_PATH_LEN 10
2673 object_path_from_seed (
char *buf,
2683 v = (
unsigned char) (
'A' + seed);
2695 if (v < 'A' || v >
'z')
2711 object_path_write_value (TestTypeNode *node,
2716 char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
2717 const char *v_string = buf;
2719 object_path_from_seed (buf, seed);
2722 node->klass->typecode,
2727 object_path_read_value (TestTypeNode *node,
2732 char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
2734 check_expected_type (reader, node->klass->typecode);
2737 (
const char **) &v);
2739 object_path_from_seed (buf, seed);
2741 if (strcmp (buf, v) != 0)
2743 _dbus_warn (
"read object path '%s' expected '%s'\n",
2752 object_path_set_value (TestTypeNode *node,
2757 char buf[MAX_SAMPLE_OBJECT_PATH_LEN + 1];
2758 const char *v_string = buf;
2760 object_path_from_seed (buf, seed);
2767 #define MAX_SAMPLE_SIGNATURE_LEN 10
2769 signature_from_seed (
char *buf,
2773 const char *sample_signatures[] = {
2782 strcpy (buf, sample_signatures[seed %
_DBUS_N_ELEMENTS(sample_signatures)]);
2786 signature_write_value (TestTypeNode *node,
2791 char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
2792 const char *v_string = buf;
2794 signature_from_seed (buf, seed);
2797 node->klass->typecode,
2802 signature_read_value (TestTypeNode *node,
2807 char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
2809 check_expected_type (reader, node->klass->typecode);
2812 (
const char **) &v);
2814 signature_from_seed (buf, seed);
2816 if (strcmp (buf, v) != 0)
2818 _dbus_warn (
"read signature value '%s' expected '%s'\n",
2828 signature_set_value (TestTypeNode *node,
2833 char buf[MAX_SAMPLE_SIGNATURE_LEN + 1];
2834 const char *v_string = buf;
2836 signature_from_seed (buf, seed);
2844 struct_write_value (TestTypeNode *node,
2849 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2850 DataBlockState saved;
2855 n_copies = node->klass->subclass_detail;
2859 data_block_save (block, &saved);
2867 while (i < n_copies)
2872 while (link !=
NULL)
2874 TestTypeNode *child = link->
data;
2877 if (!node_write_value (child, block, &sub, seed + i))
2879 data_block_restore (block, &saved);
2891 data_block_restore (block, &saved);
2899 struct_read_or_set_value (TestTypeNode *node,
2904 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2909 n_copies = node->klass->subclass_detail;
2916 while (i < n_copies)
2921 while (link !=
NULL)
2923 TestTypeNode *child = link->
data;
2926 if (realign_root ==
NULL)
2928 if (!node_read_value (child, &sub, seed + i))
2933 if (!node_set_value (child, &sub, realign_root, seed + i))
2937 if (i == (n_copies - 1) && next ==
NULL)
2938 NEXT_EXPECTING_FALSE (&sub);
2940 NEXT_EXPECTING_TRUE (&sub);
2952 struct_read_value (TestTypeNode *node,
2956 return struct_read_or_set_value (node, reader,
NULL, seed);
2960 struct_set_value (TestTypeNode *node,
2965 return struct_read_or_set_value (node, reader, realign_root, seed);
2969 struct_build_signature (TestTypeNode *node,
2972 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
2977 n_copies = node->klass->subclass_detail;
2979 orig_len = _dbus_string_get_length (str);
2985 while (i < n_copies)
2990 while (link !=
NULL)
2992 TestTypeNode *child = link->
data;
2995 if (!node_build_signature (child, str))
3015 array_write_value (TestTypeNode *node,
3020 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3021 DataBlockState saved;
3027 TestTypeNode *child;
3029 n_copies = node->klass->subclass_detail;
3033 data_block_save (block, &saved);
3040 if (!node_build_signature (child,
3041 &element_signature))
3047 &element_signature, 0,
3051 if (arrays_write_fixed_in_blocks &&
3053 child->klass->write_multi)
3055 if (!node_write_multi (child, block, &sub, seed, n_copies))
3061 while (i < n_copies)
3066 while (link !=
NULL)
3068 TestTypeNode *child = link->
data;
3071 if (!node_write_value (child, block, &sub, seed + i))
3088 data_block_restore (block, &saved);
3094 array_read_or_set_value (TestTypeNode *node,
3099 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3103 TestTypeNode *child;
3105 n_copies = node->klass->subclass_detail;
3115 if (realign_root ==
NULL && arrays_write_fixed_in_blocks &&
3117 child->klass->read_multi)
3119 if (!node_read_multi (child, &sub, seed, n_copies))
3125 while (i < n_copies)
3130 while (link !=
NULL)
3132 TestTypeNode *child = link->
data;
3138 if (realign_root ==
NULL)
3140 if (!node_read_value (child, &sub, seed + i))
3145 if (!node_set_value (child, &sub, realign_root, seed + i))
3149 if (i == (n_copies - 1) && next ==
NULL)
3150 NEXT_EXPECTING_FALSE (&sub);
3152 NEXT_EXPECTING_TRUE (&sub);
3166 array_read_value (TestTypeNode *node,
3170 return array_read_or_set_value (node, reader,
NULL, seed);
3174 array_set_value (TestTypeNode *node,
3179 return array_read_or_set_value (node, reader, realign_root, seed);
3183 array_build_signature (TestTypeNode *node,
3186 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3189 orig_len = _dbus_string_get_length (str);
3206 #define VARIANT_SEED 10
3209 variant_write_value (TestTypeNode *node,
3214 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3215 DataBlockState saved;
3218 TestTypeNode *child;
3225 data_block_save (block, &saved);
3230 if (!node_build_signature (child,
3231 &content_signature))
3235 &content_signature, 0,
3239 if (!node_write_value (child, block, &sub, seed + VARIANT_SEED))
3249 data_block_restore (block, &saved);
3255 variant_read_or_set_value (TestTypeNode *node,
3260 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3262 TestTypeNode *child;
3273 if (realign_root ==
NULL)
3275 if (!node_read_value (child, &sub, seed + VARIANT_SEED))
3280 if (!node_set_value (child, &sub, realign_root, seed + VARIANT_SEED))
3284 NEXT_EXPECTING_FALSE (&sub);
3290 variant_read_value (TestTypeNode *node,
3294 return variant_read_or_set_value (node, reader,
NULL, seed);
3298 variant_set_value (TestTypeNode *node,
3303 return variant_read_or_set_value (node, reader, realign_root, seed);
3307 dict_write_value (TestTypeNode *node,
3312 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3313 DataBlockState saved;
3319 TestTypeNode *child;
3321 n_entries = node->klass->subclass_detail;
3325 data_block_save (block, &saved);
3338 if (!node_build_signature (child,
3339 &entry_value_signature))
3348 &dict_entry_signature,
3349 _dbus_string_get_length (&dict_entry_signature)))
3357 &dict_entry_signature, 0,
3362 while (i < n_entries)
3372 key = int32_from_seed (seed + i);
3379 if (!node_write_value (child, block, &entry_sub, seed + i))
3396 data_block_restore (block, &saved);
3403 dict_read_or_set_value (TestTypeNode *node,
3408 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3412 TestTypeNode *child;
3414 n_entries = node->klass->subclass_detail;
3427 while (i < n_entries)
3435 if (realign_root ==
NULL)
3442 (dbus_int32_t*) &v);
3446 NEXT_EXPECTING_TRUE (&entry_sub);
3448 if (!node_read_value (child, &entry_sub, seed + i))
3451 NEXT_EXPECTING_FALSE (&entry_sub);
3457 v = int32_from_seed (seed + i);
3464 NEXT_EXPECTING_TRUE (&entry_sub);
3466 if (!node_set_value (child, &entry_sub, realign_root, seed + i))
3469 NEXT_EXPECTING_FALSE (&entry_sub);
3472 if (i == (n_entries - 1))
3473 NEXT_EXPECTING_FALSE (&sub);
3475 NEXT_EXPECTING_TRUE (&sub);
3485 dict_read_value (TestTypeNode *node,
3489 return dict_read_or_set_value (node, reader,
NULL, seed);
3493 dict_set_value (TestTypeNode *node,
3498 return dict_read_or_set_value (node, reader, realign_root, seed);
3502 dict_build_signature (TestTypeNode *node,
3505 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3508 orig_len = _dbus_string_get_length (str);
3531 container_destroy (TestTypeNode *node)
3533 TestTypeNodeContainer *container = (TestTypeNodeContainer*) node;
3537 while (link !=
NULL)
3539 TestTypeNode *child = link->
data;
3542 node_destroy (child);