1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message-builder.c Build messages from text files for testing (internal to D-BUS implementation)
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-message-builder.h"
28 #include "dbus-hash.h"
29 #include "dbus-internals.h"
30 #include "dbus-marshal.h"
33 * @defgroup DBusMessageBuilder code for loading test message data
34 * @ingroup DBusInternals
35 * @brief code for loading up test data for unit tests
37 * The code in here is used for unit testing, it loads
38 * up message data from a description in a file.
46 int start; /**< Calculate length since here */
47 int length; /**< length to write */
48 int offset; /**< where to write it into the data */
49 int endian; /**< endianness to write with */
53 free_saved_length (void *data)
55 SavedLength *sl = data;
58 return; /* all hash free functions have to accept NULL */
60 _dbus_string_free (&sl->name);
65 ensure_saved_length (DBusHashTable *hash,
66 const DBusString *name)
71 _dbus_string_get_const_data (name, &s);
73 sl = _dbus_hash_table_lookup_string (hash, s);
77 sl = dbus_new0 (SavedLength, 1);
79 if (!_dbus_string_init (&sl->name, _DBUS_INT_MAX))
85 if (!_dbus_string_copy (name, 0, &sl->name, 0))
88 _dbus_string_get_const_data (&sl->name, &s);
90 if (!_dbus_hash_table_insert_string (hash, (char*)s, sl))
101 free_saved_length (sl);
106 save_start (DBusHashTable *hash,
107 const DBusString *name,
112 sl = ensure_saved_length (hash, name);
116 else if (sl->start >= 0)
118 _dbus_warn ("Same START_LENGTH given twice\n");
128 save_length (DBusHashTable *hash,
129 const DBusString *name,
134 sl = ensure_saved_length (hash, name);
138 else if (sl->length >= 0)
140 _dbus_warn ("Same END_LENGTH given twice\n");
150 save_offset (DBusHashTable *hash,
151 const DBusString *name,
157 sl = ensure_saved_length (hash, name);
161 else if (sl->offset >= 0)
163 _dbus_warn ("Same LENGTH given twice\n");
175 /** Saves the segment to delete in order to unalign the next item */
176 #define SAVE_FOR_UNALIGN(str, boundary) \
177 int align_pad_start = _dbus_string_get_length (str); \
178 int align_pad_end = _DBUS_ALIGN_VALUE (align_pad_start, (boundary))
180 /** Deletes the alignment padding */
181 #define PERFORM_UNALIGN(str) \
184 _dbus_string_delete ((str), align_pad_start, \
185 align_pad_end - align_pad_start); \
191 append_quoted_string (DBusString *dest,
192 const DBusString *quoted,
196 dbus_bool_t in_quotes = FALSE;
199 /* FIXME: We might want to add escaping in case we want to put '
200 * characters in our strings.
204 while (i < _dbus_string_get_length (quoted))
208 b = _dbus_string_get_byte (quoted, i);
216 if (!_dbus_string_append_byte (dest, b))
224 else if (b == ' ' || b == '\n' || b == '\t')
225 break; /* end on whitespace if not quoted */
228 if (!_dbus_string_append_byte (dest, b))
239 if (!_dbus_string_append_byte (dest, '\0'))
245 append_saved_length (DBusString *dest,
246 DBusHashTable *length_hash,
247 const DBusString *name,
251 if (!save_offset (length_hash, name,
254 _dbus_warn ("failed to save offset to LENGTH\n");
258 if (!_dbus_marshal_uint32 (dest, endian,
261 _dbus_warn ("failed to append a length\n");
269 * Reads the given filename, which should be in "message description
270 * language" (look at some examples), and builds up the message data
271 * from it. The message data may be invalid, or valid.
273 * The parser isn't very strict, it's just a hack for test programs.
275 * The file format is:
277 * VALID_HEADER normal header; byte order, padding, header len, body len, serial
278 * BIG_ENDIAN switch to big endian
279 * LITTLE_ENDIAN switch to little endian
280 * OPPOSITE_ENDIAN switch to opposite endian
281 * ALIGN <N> aligns to the given value
282 * UNALIGN skips alignment for the next marshal
283 * BYTE <N> inserts the given integer in [0,255] or char in 'a' format
284 * START_LENGTH <name> marks the start of a length to measure
285 * END_LENGTH <name> records the length since START_LENGTH under the given name
286 * (or if no START_LENGTH, absolute length)
287 * LENGTH <name> inserts the saved length of the same name
288 * CHOP <N> chops last N bytes off the data
289 * FIELD_NAME <abcd> inserts 4-byte field name
290 * TYPE <typename> inserts a typecode byte
293 * Following commands insert aligned data unless
294 * preceded by "UNALIGN":
296 * INT32 <N> marshals an INT32
297 * UINT32 <N> marshals a UINT32
298 * DOUBLE <N> marshals a double
299 * STRING 'Foo' marshals a string
300 * INT32_ARRAY { 3, 4, 5, 6} marshals an INT32 array
301 * UINT32_ARRAY { 3, 4, 5, 6} marshals an UINT32 array
302 * DOUBLE_ARRAY { 1.0, 2.0, 3.0, 4.0} marshals a DOUBLE array
305 * @todo add support for array types INT32_ARRAY { 3, 4, 5, 6 }
308 * @param dest the string to append the message data to
309 * @param filename the filename to load
310 * @returns #TRUE on success
313 _dbus_message_data_load (DBusString *dest,
314 const DBusString *filename)
317 DBusResultCode result;
322 DBusHashTable *length_hash;
329 if (!_dbus_string_init (&file, _DBUS_INT_MAX))
332 if (!_dbus_string_init (&line, _DBUS_INT_MAX))
334 _dbus_string_free (&file);
338 if ((result = _dbus_file_get_contents (&file, filename)) != DBUS_RESULT_SUCCESS)
341 _dbus_string_get_const_data (filename, &s);
342 _dbus_warn ("Getting contents of %s failed: %s\n",
343 s, dbus_result_to_string (result));
348 length_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
351 if (length_hash == NULL)
354 endian = DBUS_COMPILER_BYTE_ORDER;
358 while (_dbus_string_pop_line (&file, &line))
360 dbus_bool_t just_set_unalign;
362 just_set_unalign = FALSE;
365 _dbus_string_delete_leading_blanks (&line);
367 if (_dbus_string_get_length (&line) == 0)
372 else if (_dbus_string_starts_with_c_str (&line,
375 /* Ignore this comment */
378 else if (_dbus_string_starts_with_c_str (&line,
384 if (!_dbus_string_append_byte (dest, endian))
386 _dbus_warn ("could not append endianness\n");
393 if (!_dbus_string_append_byte (dest, '\0'))
395 _dbus_warn ("could not append nul pad\n");
401 _dbus_string_init_const (&name, "Header");
402 if (!append_saved_length (dest, length_hash,
403 &name, _dbus_string_get_length (dest),
407 _dbus_string_init_const (&name, "Body");
408 if (!append_saved_length (dest, length_hash,
409 &name, _dbus_string_get_length (dest),
414 if (!_dbus_marshal_int32 (dest, endian, 1))
416 _dbus_warn ("couldn't append client serial\n");
420 else if (_dbus_string_starts_with_c_str (&line,
423 endian = DBUS_BIG_ENDIAN;
425 else if (_dbus_string_starts_with_c_str (&line,
428 endian = DBUS_LITTLE_ENDIAN;
430 else if (_dbus_string_starts_with_c_str (&line,
433 if (endian == DBUS_BIG_ENDIAN)
434 endian = DBUS_LITTLE_ENDIAN;
436 endian = DBUS_BIG_ENDIAN;
438 else if (_dbus_string_starts_with_c_str (&line,
443 _dbus_string_delete_first_word (&line);
445 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
447 _dbus_warn ("Failed to parse integer\n");
453 _dbus_warn ("Aligning to %ld boundary is crack\n",
458 if (!_dbus_string_align_length (dest, val))
461 else if (_dbus_string_starts_with_c_str (&line, "UNALIGN"))
464 just_set_unalign = TRUE;
466 else if (_dbus_string_starts_with_c_str (&line, "CHOP"))
470 /* FIXME if you CHOP the offset for a LENGTH
471 * command, we segfault.
474 _dbus_string_delete_first_word (&line);
476 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
478 _dbus_warn ("Failed to parse integer to chop\n");
482 if (val > _dbus_string_get_length (dest))
484 _dbus_warn ("Trying to chop %ld bytes but we only have %d\n",
486 _dbus_string_get_length (dest));
490 _dbus_string_shorten (dest, val);
492 else if (_dbus_string_starts_with_c_str (&line, "BYTE"))
494 unsigned char the_byte;
496 _dbus_string_delete_first_word (&line);
498 if (_dbus_string_equal_c_str (&line, "'\\''"))
500 else if (_dbus_string_get_byte (&line, 0) == '\'' &&
501 _dbus_string_get_length (&line) >= 3 &&
502 _dbus_string_get_byte (&line, 2) == '\'')
503 the_byte = _dbus_string_get_byte (&line, 1);
507 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
509 _dbus_warn ("Failed to parse integer for BYTE\n");
515 _dbus_warn ("A byte must be in range 0-255 not %ld\n",
519 the_byte = (unsigned char) val;
522 _dbus_string_append_byte (dest, the_byte);
524 else if (_dbus_string_starts_with_c_str (&line,
527 _dbus_string_delete_first_word (&line);
529 if (!save_start (length_hash, &line,
530 _dbus_string_get_length (dest)))
532 _dbus_warn ("failed to save length start\n");
536 else if (_dbus_string_starts_with_c_str (&line,
539 _dbus_string_delete_first_word (&line);
541 if (!save_length (length_hash, &line,
542 _dbus_string_get_length (dest)))
544 _dbus_warn ("failed to save length end\n");
548 else if (_dbus_string_starts_with_c_str (&line,
551 SAVE_FOR_UNALIGN (dest, 4);
553 _dbus_string_delete_first_word (&line);
555 if (!append_saved_length (dest, length_hash,
557 unalign ? align_pad_start : align_pad_end,
560 _dbus_warn ("failed to add LENGTH\n");
564 PERFORM_UNALIGN (dest);
566 else if (_dbus_string_starts_with_c_str (&line,
569 _dbus_string_delete_first_word (&line);
571 if (_dbus_string_get_length (&line) != 4)
574 _dbus_string_get_const_data (&line, &s);
575 _dbus_warn ("Field name must be four characters not \"%s\"\n",
583 _dbus_string_align_length (dest, 4);
585 if (!_dbus_string_copy (&line, 0, dest,
586 _dbus_string_get_length (dest)))
589 else if (_dbus_string_starts_with_c_str (&line,
594 _dbus_string_delete_first_word (&line);
596 if (_dbus_string_starts_with_c_str (&line, "INVALID"))
597 code = DBUS_TYPE_INVALID;
598 else if (_dbus_string_starts_with_c_str (&line, "NIL"))
599 code = DBUS_TYPE_NIL;
600 else if (_dbus_string_starts_with_c_str (&line, "BOOLEAN_ARRAY"))
601 code = DBUS_TYPE_BOOLEAN_ARRAY;
602 else if (_dbus_string_starts_with_c_str (&line, "INT32_ARRAY"))
603 code = DBUS_TYPE_INT32_ARRAY;
604 else if (_dbus_string_starts_with_c_str (&line, "UINT32_ARRAY"))
605 code = DBUS_TYPE_UINT32_ARRAY;
606 else if (_dbus_string_starts_with_c_str (&line, "DOUBLE_ARRAY"))
607 code = DBUS_TYPE_DOUBLE_ARRAY;
608 else if (_dbus_string_starts_with_c_str (&line, "BYTE_ARRAY"))
609 code = DBUS_TYPE_BYTE_ARRAY;
610 else if (_dbus_string_starts_with_c_str (&line, "STRING_ARRAY"))
611 code = DBUS_TYPE_STRING_ARRAY;
612 else if (_dbus_string_starts_with_c_str (&line, "BOOLEAN"))
613 code = DBUS_TYPE_BOOLEAN;
614 else if (_dbus_string_starts_with_c_str (&line, "INT32"))
615 code = DBUS_TYPE_INT32;
616 else if (_dbus_string_starts_with_c_str (&line, "UINT32"))
617 code = DBUS_TYPE_UINT32;
618 else if (_dbus_string_starts_with_c_str (&line, "DOUBLE"))
619 code = DBUS_TYPE_DOUBLE;
620 else if (_dbus_string_starts_with_c_str (&line, "STRING"))
621 code = DBUS_TYPE_STRING;
625 _dbus_string_get_const_data (&line, &s);
626 _dbus_warn ("%s is not a valid type name\n", s);
630 if (!_dbus_string_append_byte (dest, code))
632 _dbus_warn ("could not append typecode byte\n");
636 else if (_dbus_string_starts_with_c_str (&line,
639 SAVE_FOR_UNALIGN (dest, 4);
640 int i, len, allocated;
641 unsigned char *values;
642 unsigned char b, val;
645 values = dbus_new (unsigned char, allocated);
648 _dbus_warn ("could not allocate memory for BOOLEAN_ARRAY\n");
654 _dbus_string_delete_first_word (&line);
655 _dbus_string_skip_blank (&line, 0, &i);
656 b = _dbus_string_get_byte (&line, i++);
661 while (i < _dbus_string_get_length (&line))
663 _dbus_string_skip_blank (&line, i, &i);
664 printf ("i is: %d\n", i);
666 if (_dbus_string_find_to (&line, i, i + 5,
672 else if (_dbus_string_find_to (&line, i, i + 4,
680 _dbus_warn ("could not parse BOOLEAN_ARRAY\n");
685 if (len == allocated)
688 values = dbus_realloc (values, allocated * sizeof (unsigned char));
691 _dbus_warn ("could not allocate memory for BOOLEAN_ARRAY\n");
696 _dbus_string_skip_blank (&line, i, &i);
698 b = _dbus_string_get_byte (&line, i++);
706 if (!_dbus_marshal_int32 (dest, endian, len) ||
707 !_dbus_string_append_len (dest, values, len))
709 _dbus_warn ("failed to append BOOLEAN_ARRAY\n");
714 PERFORM_UNALIGN (dest);
716 else if (_dbus_string_starts_with_c_str (&line,
719 SAVE_FOR_UNALIGN (dest, 4);
720 int i, len, allocated;
721 dbus_int32_t *values;
726 values = dbus_new (dbus_int32_t, allocated);
729 _dbus_warn ("could not allocate memory for INT32_ARRAY\n");
735 _dbus_string_delete_first_word (&line);
736 _dbus_string_skip_blank (&line, 0, &i);
737 b = _dbus_string_get_byte (&line, i++);
742 while (i < _dbus_string_get_length (&line))
744 _dbus_string_skip_blank (&line, i, &i);
746 if (!_dbus_string_parse_int (&line, i, &val, &i))
748 _dbus_warn ("could not parse integer for INT32_ARRAY\n");
753 if (len == allocated)
756 values = dbus_realloc (values, allocated * sizeof (dbus_int32_t));
759 _dbus_warn ("could not allocate memory for INT32_ARRAY\n");
764 _dbus_string_skip_blank (&line, i, &i);
766 b = _dbus_string_get_byte (&line, i++);
774 if (!_dbus_marshal_int32_array (dest, endian, values, len))
776 _dbus_warn ("failed to append INT32_ARRAY\n");
781 PERFORM_UNALIGN (dest);
783 else if (_dbus_string_starts_with_c_str (&line,
786 SAVE_FOR_UNALIGN (dest, 4);
787 int i, len, allocated;
788 dbus_uint32_t *values;
793 values = dbus_new (dbus_uint32_t, allocated);
796 _dbus_warn ("could not allocate memory for UINT32_ARRAY\n");
802 _dbus_string_delete_first_word (&line);
803 _dbus_string_skip_blank (&line, 0, &i);
804 b = _dbus_string_get_byte (&line, i++);
809 while (i < _dbus_string_get_length (&line))
811 _dbus_string_skip_blank (&line, i, &i);
813 if (!_dbus_string_parse_int (&line, i, &val, &i))
815 _dbus_warn ("could not parse integer for UINT32_ARRAY\n");
820 if (len == allocated)
823 values = dbus_realloc (values, allocated * sizeof (dbus_uint32_t));
826 _dbus_warn ("could not allocate memory for UINT32_ARRAY\n");
831 _dbus_string_skip_blank (&line, i, &i);
833 b = _dbus_string_get_byte (&line, i++);
841 if (!_dbus_marshal_uint32_array (dest, endian, values, len))
843 _dbus_warn ("failed to append UINT32_ARRAY\n");
848 PERFORM_UNALIGN (dest);
850 else if (_dbus_string_starts_with_c_str (&line,
853 SAVE_FOR_UNALIGN (dest, 8);
854 int i, len, allocated;
860 values = dbus_new (double, allocated);
863 _dbus_warn ("could not allocate memory for DOUBLE_ARRAY\n");
869 _dbus_string_delete_first_word (&line);
870 _dbus_string_skip_blank (&line, 0, &i);
871 b = _dbus_string_get_byte (&line, i++);
876 while (i < _dbus_string_get_length (&line))
878 _dbus_string_skip_blank (&line, i, &i);
880 if (!_dbus_string_parse_double (&line, i, &val, &i))
882 _dbus_warn ("could not parse double for DOUBLE_ARRAY\n");
887 if (len == allocated)
890 values = dbus_realloc (values, allocated * sizeof (double));
893 _dbus_warn ("could not allocate memory for DOUBLE_ARRAY\n");
898 _dbus_string_skip_blank (&line, i, &i);
900 b = _dbus_string_get_byte (&line, i++);
908 if (!_dbus_marshal_double_array (dest, endian, values, len))
910 _dbus_warn ("failed to append DOUBLE_ARRAY\n");
915 PERFORM_UNALIGN (dest);
917 else if (_dbus_string_starts_with_c_str (&line,
920 SAVE_FOR_UNALIGN (dest, 4);
921 int i, len, allocated;
928 values = dbus_new (char *, allocated);
931 _dbus_warn ("could not allocate memory for DOUBLE_ARRAY\n");
937 _dbus_string_delete_first_word (&line);
938 _dbus_string_skip_blank (&line, 0, &i);
939 b = _dbus_string_get_byte (&line, i++);
944 _dbus_string_init (&val_str, _DBUS_INT_MAX);
945 while (i < _dbus_string_get_length (&line))
947 _dbus_string_skip_blank (&line, i, &i);
949 if (!append_quoted_string (&val_str, &line, i, &i))
951 _dbus_warn ("could not parse quoted string for STRING_ARRAY\n");
956 if (!_dbus_string_steal_data (&val_str, &val))
958 _dbus_warn ("could not allocate memory for STRING_ARRAY string\n");
963 if (len == allocated)
966 values = dbus_realloc (values, allocated * sizeof (char *));
969 _dbus_warn ("could not allocate memory for STRING_ARRAY\n");
974 _dbus_string_skip_blank (&line, i, &i);
976 b = _dbus_string_get_byte (&line, i++);
982 _dbus_warn ("missing comma when parsing STRING_ARRAY\n");
986 _dbus_string_free (&val_str);
988 if (!_dbus_marshal_string_array (dest, endian, (const char **)values, len))
990 _dbus_warn ("failed to append STRING_ARRAY\n");
995 dbus_free_string_array (values);
997 PERFORM_UNALIGN (dest);
999 else if (_dbus_string_starts_with_c_str (&line,
1004 _dbus_string_delete_first_word (&line);
1006 if (_dbus_string_starts_with_c_str (&line, "true"))
1008 else if (_dbus_string_starts_with_c_str (&line, "false"))
1012 _dbus_warn ("could not parse BOOLEAN\n");
1015 if (!_dbus_string_append_byte (dest, val))
1017 _dbus_warn ("failed to append BOOLEAN\n");
1022 else if (_dbus_string_starts_with_c_str (&line,
1025 SAVE_FOR_UNALIGN (dest, 4);
1028 _dbus_string_delete_first_word (&line);
1030 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
1032 _dbus_warn ("could not parse integer for INT32\n");
1036 if (!_dbus_marshal_int32 (dest, endian,
1039 _dbus_warn ("failed to append INT32\n");
1043 PERFORM_UNALIGN (dest);
1045 else if (_dbus_string_starts_with_c_str (&line,
1048 SAVE_FOR_UNALIGN (dest, 4);
1051 _dbus_string_delete_first_word (&line);
1053 /* FIXME should have _dbus_string_parse_uint32 */
1054 if (!_dbus_string_parse_int (&line, 0, &val, NULL))
1057 if (!_dbus_marshal_uint32 (dest, endian,
1060 _dbus_warn ("failed to append UINT32\n");
1064 PERFORM_UNALIGN (dest);
1066 else if (_dbus_string_starts_with_c_str (&line,
1069 SAVE_FOR_UNALIGN (dest, 8);
1072 _dbus_string_delete_first_word (&line);
1074 if (!_dbus_string_parse_double (&line, 0, &val, NULL))
1077 if (!_dbus_marshal_double (dest, endian,
1080 _dbus_warn ("failed to append DOUBLE\n");
1084 PERFORM_UNALIGN (dest);
1086 else if (_dbus_string_starts_with_c_str (&line,
1089 SAVE_FOR_UNALIGN (dest, 4);
1093 _dbus_string_delete_first_word (&line);
1095 size_offset = _dbus_string_get_length (dest);
1096 size_offset = _DBUS_ALIGN_VALUE (size_offset, 4);
1097 if (!_dbus_marshal_uint32 (dest, endian, 0))
1099 _dbus_warn ("Failed to append string size\n");
1103 old_len = _dbus_string_get_length (dest);
1104 if (!append_quoted_string (dest, &line, 0, NULL))
1106 _dbus_warn ("Failed to append quoted string\n");
1110 _dbus_marshal_set_uint32 (dest, endian, size_offset,
1111 /* subtract 1 for nul */
1112 _dbus_string_get_length (dest) - old_len - 1);
1114 PERFORM_UNALIGN (dest);
1119 if (!just_set_unalign && unalign)
1121 _dbus_warn ("UNALIGN prior to something that isn't aligned\n");
1125 goto next_iteration; /* skip parse_failed */
1130 _dbus_string_get_const_data (&line, &s);
1131 _dbus_warn ("couldn't process line %d \"%s\"\n",
1137 _dbus_hash_iter_init (length_hash, &iter);
1138 while (_dbus_hash_iter_next (&iter))
1140 SavedLength *sl = _dbus_hash_iter_get_value (&iter);
1143 _dbus_string_get_const_data (&sl->name, &s);
1147 _dbus_warn ("Used LENGTH %s but never did END_LENGTH\n",
1151 else if (sl->offset < 0)
1153 _dbus_warn ("Did END_LENGTH %s but never used LENGTH\n",
1162 _dbus_verbose ("Filling in length %s endian = %d offset = %d start = %d length = %d\n",
1163 s, sl->endian, sl->offset, sl->start, sl->length);
1164 _dbus_marshal_set_int32 (dest,
1167 sl->length - sl->start);
1170 _dbus_hash_iter_remove_entry (&iter);
1176 if (length_hash != NULL)
1177 _dbus_hash_table_unref (length_hash);
1179 _dbus_string_free (&file);
1180 _dbus_string_free (&line);
1185 #endif /* DBUS_BUILD_TESTS */