2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * See the included COPYING file for more information.
11 * Author: Ryan Lortie <desrt@desrt.ca>
16 #include <glib/gvariant-internal.h>
17 #include <glib/glib-private.h>
22 #define BASIC "bynqiuxthfdsog?"
23 #define N_BASIC (G_N_ELEMENTS (BASIC) - 1)
25 #define INVALIDS "cejklpwz&@^$"
26 #define N_INVALIDS (G_N_ELEMENTS (INVALIDS) - 1)
28 /* see comment in gvariant-serialiser.c about this madness.
30 * we use this to get testing of non-strictly-aligned GVariant instances
31 * on machines that can tolerate it. it is necessary to support this
32 * because some systems have malloc() that returns non-8-aligned
33 * pointers. it is necessary to have special support in the tests
34 * because on most machines malloc() is 8-aligned.
36 #define ALIGN_BITS (sizeof (struct { char a; union { \
37 guint64 x; void *y; gdouble z; } b; }) - 9)
40 randomly (gdouble prob)
42 return g_test_rand_double_range (0, 1) < prob;
47 append_tuple_type_string (GString *, GString *, gboolean, gint);
49 /* append a random GVariantType to a GString
50 * append a description of the type to another GString
51 * return what the type is
54 append_type_string (GString *string,
59 if (!depth-- || randomly (0.3))
61 gchar b = BASIC[g_test_rand_int_range (0, N_BASIC - definite)];
62 g_string_append_c (string, b);
63 g_string_append_c (description, b);
68 return g_variant_type_copy (G_VARIANT_TYPE_BOOLEAN);
70 return g_variant_type_copy (G_VARIANT_TYPE_BYTE);
72 return g_variant_type_copy (G_VARIANT_TYPE_INT16);
74 return g_variant_type_copy (G_VARIANT_TYPE_UINT16);
76 return g_variant_type_copy (G_VARIANT_TYPE_INT32);
78 return g_variant_type_copy (G_VARIANT_TYPE_UINT32);
80 return g_variant_type_copy (G_VARIANT_TYPE_INT64);
82 return g_variant_type_copy (G_VARIANT_TYPE_UINT64);
84 return g_variant_type_copy (G_VARIANT_TYPE_HANDLE);
86 return g_variant_type_copy (G_VARIANT_TYPE_FLOAT);
88 return g_variant_type_copy (G_VARIANT_TYPE_DOUBLE);
90 return g_variant_type_copy (G_VARIANT_TYPE_STRING);
92 return g_variant_type_copy (G_VARIANT_TYPE_OBJECT_PATH);
94 return g_variant_type_copy (G_VARIANT_TYPE_SIGNATURE);
96 return g_variant_type_copy (G_VARIANT_TYPE_BASIC);
98 g_assert_not_reached ();
103 GVariantType *result;
105 switch (g_test_rand_int_range (0, definite ? 5 : 7))
109 GVariantType *element;
111 g_string_append_c (string, 'a');
112 g_string_append (description, "a of ");
113 element = append_type_string (string, description,
115 result = g_variant_type_new_array (element);
116 g_variant_type_free (element);
119 g_assert (g_variant_type_is_array (result));
124 GVariantType *element;
126 g_string_append_c (string, 'm');
127 g_string_append (description, "m of ");
128 element = append_type_string (string, description,
130 result = g_variant_type_new_maybe (element);
131 g_variant_type_free (element);
134 g_assert (g_variant_type_is_maybe (result));
138 result = append_tuple_type_string (string, description,
141 g_assert (g_variant_type_is_tuple (result));
146 GVariantType *key, *value;
148 g_string_append_c (string, '{');
149 g_string_append (description, "e of [");
150 key = append_type_string (string, description, definite, 0);
151 g_string_append (description, ", ");
152 value = append_type_string (string, description, definite, depth);
153 g_string_append_c (description, ']');
154 g_string_append_c (string, '}');
155 result = g_variant_type_new_dict_entry (key, value);
156 g_variant_type_free (key);
157 g_variant_type_free (value);
160 g_assert (g_variant_type_is_dict_entry (result));
164 g_string_append_c (string, 'v');
165 g_string_append_c (description, 'V');
166 result = g_variant_type_copy (G_VARIANT_TYPE_VARIANT);
167 g_assert (g_variant_type_equal (result, G_VARIANT_TYPE_VARIANT));
171 g_string_append_c (string, '*');
172 g_string_append_c (description, 'S');
173 result = g_variant_type_copy (G_VARIANT_TYPE_ANY);
174 g_assert (g_variant_type_equal (result, G_VARIANT_TYPE_ANY));
178 g_string_append_c (string, 'r');
179 g_string_append_c (description, 'R');
180 result = g_variant_type_copy (G_VARIANT_TYPE_TUPLE);
181 g_assert (g_variant_type_is_tuple (result));
185 g_assert_not_reached ();
192 static GVariantType *
193 append_tuple_type_string (GString *string,
194 GString *description,
198 GVariantType *result, *other_result;
199 GVariantType **types;
203 g_string_append_c (string, '(');
204 g_string_append (description, "t of [");
206 size = g_test_rand_int_range (0, 20);
207 types = g_new (GVariantType *, size + 1);
209 for (i = 0; i < size; i++)
211 types[i] = append_type_string (string, description, definite, depth);
214 g_string_append (description, ", ");
219 g_string_append_c (description, ']');
220 g_string_append_c (string, ')');
222 result = g_variant_type_new_tuple ((gpointer) types, size);
223 other_result = g_variant_type_new_tuple ((gpointer) types, -1);
224 g_assert (g_variant_type_equal (result, other_result));
225 g_variant_type_free (other_result);
226 for (i = 0; i < size; i++)
227 g_variant_type_free (types[i]);
233 /* given a valid type string, make it invalid */
235 invalid_mutation (const gchar *type_string)
237 gboolean have_parens, have_braces;
239 /* it's valid, so '(' implies ')' and same for '{' and '}' */
240 have_parens = strchr (type_string, '(') != NULL;
241 have_braces = strchr (type_string, '{') != NULL;
243 if (have_parens && have_braces && randomly (0.3))
245 /* swap a paren and a brace */
251 new = g_strdup (type_string);
261 /* count number of parens/braces */
262 while ((pp = strchr (pp + 1, p))) np++;
263 while ((bp = strchr (bp + 1, b))) nb++;
265 /* randomly pick one of each */
266 np = g_test_rand_int_range (0, np) + 1;
267 nb = g_test_rand_int_range (0, nb) + 1;
271 while (np--) pp = strchr (pp + 1, p);
272 while (nb--) bp = strchr (bp + 1, b);
275 g_assert (*bp == b && *pp == p);
282 if ((have_parens || have_braces) && randomly (0.3))
284 /* drop a paren/brace */
291 if (randomly (0.5)) p = '('; else p = ')';
293 if (randomly (0.5)) p = '{'; else p = '}';
295 new = g_strdup (type_string);
299 while ((pp = strchr (pp + 1, p))) np++;
300 np = g_test_rand_int_range (0, np) + 1;
302 while (np--) pp = strchr (pp + 1, p);
314 /* else, perform a random mutation at a random point */
322 /* insert a paren/brace */
324 if (randomly (0.5)) p = '('; else p = ')';
326 if (randomly (0.5)) p = '{'; else p = '}';
328 else if (randomly (0.5))
331 p = INVALIDS[g_test_rand_int_range (0, N_INVALIDS)];
340 length = strlen (type_string);
341 new = g_malloc (length + 2);
342 n = g_test_rand_int_range (0, length);
343 memcpy (new, type_string, n);
345 memcpy (new + n + 1, type_string + n, length - n);
346 new[length + 1] = '\0';
352 /* describe a type using the same language as is generated
353 * while generating the type with append_type_string
356 describe_type (const GVariantType *type)
360 if (g_variant_type_is_container (type))
362 g_assert (!g_variant_type_is_basic (type));
364 if (g_variant_type_is_array (type))
366 gchar *subtype = describe_type (g_variant_type_element (type));
367 result = g_strdup_printf ("a of %s", subtype);
370 else if (g_variant_type_is_maybe (type))
372 gchar *subtype = describe_type (g_variant_type_element (type));
373 result = g_strdup_printf ("m of %s", subtype);
376 else if (g_variant_type_is_tuple (type))
378 if (!g_variant_type_equal (type, G_VARIANT_TYPE_TUPLE))
380 const GVariantType *sub;
385 string = g_string_new ("t of [");
387 length = g_variant_type_n_items (type);
388 sub = g_variant_type_first (type);
389 for (i = 0; i < length; i++)
391 gchar *subtype = describe_type (sub);
392 g_string_append (string, subtype);
395 if ((sub = g_variant_type_next (sub)))
396 g_string_append (string, ", ");
398 g_assert (sub == NULL);
399 g_string_append_c (string, ']');
401 result = g_string_free (string, FALSE);
404 result = g_strdup ("R");
406 else if (g_variant_type_is_dict_entry (type))
408 gchar *key, *value, *key2, *value2;
410 key = describe_type (g_variant_type_key (type));
411 value = describe_type (g_variant_type_value (type));
412 key2 = describe_type (g_variant_type_first (type));
413 value2 = describe_type (
414 g_variant_type_next (g_variant_type_first (type)));
415 g_assert (g_variant_type_next (g_variant_type_next (
416 g_variant_type_first (type))) == NULL);
417 g_assert_cmpstr (key, ==, key2);
418 g_assert_cmpstr (value, ==, value2);
419 result = g_strjoin ("", "e of [", key, ", ", value, "]", NULL);
425 else if (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT))
427 result = g_strdup ("V");
430 g_assert_not_reached ();
434 if (g_variant_type_is_definite (type))
436 g_assert (g_variant_type_is_basic (type));
438 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
439 result = g_strdup ("b");
440 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
441 result = g_strdup ("y");
442 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16))
443 result = g_strdup ("n");
444 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
445 result = g_strdup ("q");
446 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
447 result = g_strdup ("i");
448 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32))
449 result = g_strdup ("u");
450 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64))
451 result = g_strdup ("x");
452 else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64))
453 result = g_strdup ("t");
454 else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
455 result = g_strdup ("h");
456 else if (g_variant_type_equal (type, G_VARIANT_TYPE_FLOAT))
457 result = g_strdup ("f");
458 else if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
459 result = g_strdup ("d");
460 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
461 result = g_strdup ("s");
462 else if (g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH))
463 result = g_strdup ("o");
464 else if (g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
465 result = g_strdup ("g");
467 g_assert_not_reached ();
471 if (g_variant_type_equal (type, G_VARIANT_TYPE_ANY))
473 result = g_strdup ("S");
475 else if (g_variant_type_equal (type, G_VARIANT_TYPE_BASIC))
477 result = g_strdup ("?");
480 g_assert_not_reached ();
487 /* given a type string, replace one of the indefinite type characters in
488 * it with a matching type (possibly the same type).
491 generate_subtype (const gchar *type_string)
493 GVariantType *replacement;
494 GString *result, *junk;
495 gint length, n = 0, l;
497 result = g_string_new (NULL);
498 junk = g_string_new (NULL);
500 /* count the number of indefinite type characters */
501 for (length = 0; type_string[length]; length++)
502 n += type_string[length] == 'r' ||
503 type_string[length] == '?' ||
504 type_string[length] == '*';
505 /* length now is strlen (type_string) */
507 /* pick one at random to replace */
508 n = g_test_rand_int_range (0, n) + 1;
512 while (n--) l += strcspn (type_string + l + 1, "r?*") + 1;
513 g_assert (type_string[l] == 'r' ||
514 type_string[l] == '?' ||
515 type_string[l] == '*');
517 /* store up to that point in a GString */
518 g_string_append_len (result, type_string, l);
520 /* then store the replacement in the GString */
521 if (type_string[l] == 'r')
522 replacement = append_tuple_type_string (result, junk, FALSE, 3);
524 else if (type_string[l] == '?')
525 replacement = append_type_string (result, junk, FALSE, 0);
527 else if (type_string[l] == '*')
528 replacement = append_type_string (result, junk, FALSE, 3);
531 g_assert_not_reached ();
533 /* ensure the replacement has the proper type */
534 g_assert (g_variant_type_is_subtype_of (replacement,
535 (gpointer) &type_string[l]));
537 /* store the rest from the original type string */
538 g_string_append (result, type_string + l + 1);
540 g_variant_type_free (replacement);
541 g_string_free (junk, TRUE);
543 return g_string_free (result, FALSE);
548 const GVariantType *type;
549 struct typestack *parent;
552 /* given an indefinite type string, replace one of the indefinite
553 * characters in it with a matching type and ensure that the result is a
554 * subtype of the original. repeat.
557 subtype_check (const gchar *type_string,
558 struct typestack *parent_ts)
560 struct typestack ts, *node;
564 subtype = generate_subtype (type_string);
566 ts.type = G_VARIANT_TYPE (subtype);
567 ts.parent = parent_ts;
569 for (node = &ts; node; node = node->parent)
571 /* this type should be a subtype of each parent type */
572 g_assert (g_variant_type_is_subtype_of (ts.type, node->type));
574 /* it should only be a supertype when it is exactly equal */
575 g_assert (g_variant_type_is_subtype_of (node->type, ts.type) ==
576 g_variant_type_equal (ts.type, node->type));
581 if (!g_variant_type_is_definite (ts.type) && depth < 5)
583 /* the type is still indefinite and we haven't repeated too many
584 * times. go once more.
587 subtype_check (subtype, &ts);
594 test_gvarianttype (void)
598 for (i = 0; i < 2000; i++)
600 GString *type_string, *description;
601 GVariantType *type, *other_type;
602 const GVariantType *ctype;
606 type_string = g_string_new (NULL);
607 description = g_string_new (NULL);
609 /* generate a random type, its type string and a description
611 * exercises type constructor functions and g_variant_type_copy()
613 type = append_type_string (type_string, description, FALSE, 6);
615 /* convert the type string to a type and ensure that it is equal
616 * to the one produced with the type constructor routines
618 ctype = G_VARIANT_TYPE (type_string->str);
619 g_assert (g_variant_type_equal (ctype, type));
620 g_assert (g_variant_type_hash (ctype) == g_variant_type_hash (type));
621 g_assert (g_variant_type_is_subtype_of (ctype, type));
622 g_assert (g_variant_type_is_subtype_of (type, ctype));
624 /* check if the type is indefinite */
625 if (!g_variant_type_is_definite (type))
627 struct typestack ts = { type, NULL };
629 /* if it is indefinite, then replace one of the indefinite
630 * characters with a matching type and ensure that the result
631 * is a subtype of the original type. repeat.
633 subtype_check (type_string->str, &ts);
636 /* ensure that no indefinite characters appear */
637 g_assert (strcspn (type_string->str, "r?*") == type_string->len);
640 /* describe the type.
642 * exercises the type iterator interface
644 desc = describe_type (type);
646 /* make sure the description matches */
647 g_assert_cmpstr (desc, ==, description->str);
650 /* make an invalid mutation to the type and make sure the type
651 * validation routines catch it */
652 invalid = invalid_mutation (type_string->str);
653 g_assert (g_variant_type_string_is_valid (type_string->str));
654 g_assert (!g_variant_type_string_is_valid (invalid));
657 /* concatenate another type to the type string and ensure that
658 * the result is recognised as being invalid
660 other_type = append_type_string (type_string, description, FALSE, 2);
662 g_string_free (description, TRUE);
663 g_string_free (type_string, TRUE);
664 g_variant_type_free (other_type);
665 g_variant_type_free (type);
669 #define ALIGNED(x, y) (((x + (y - 1)) / y) * y)
671 /* do our own calculation of the fixed_size and alignment of a type
672 * using a simple algorithm to make sure the "fancy" one in the
673 * implementation is correct.
676 calculate_type_info (const GVariantType *type,
680 if (g_variant_type_is_array (type) ||
681 g_variant_type_is_maybe (type))
683 calculate_type_info (g_variant_type_element (type), NULL, alignment);
688 else if (g_variant_type_is_tuple (type) ||
689 g_variant_type_is_dict_entry (type))
691 if (g_variant_type_n_items (type))
693 const GVariantType *sub;
702 sub = g_variant_type_first (type);
708 calculate_type_info (sub, &this_fs, &this_al);
710 al = MAX (al, this_al);
720 size = ALIGNED (size, this_al);
724 while ((sub = g_variant_type_next (sub)));
726 size = ALIGNED (size, al);
747 if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN) ||
748 g_variant_type_equal (type, G_VARIANT_TYPE_BYTE))
753 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16) ||
754 g_variant_type_equal (type, G_VARIANT_TYPE_UINT16))
759 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32) ||
760 g_variant_type_equal (type, G_VARIANT_TYPE_UINT32) ||
761 g_variant_type_equal (type, G_VARIANT_TYPE_FLOAT) ||
762 g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE))
767 else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64) ||
768 g_variant_type_equal (type, G_VARIANT_TYPE_UINT64) ||
769 g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
773 else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING) ||
774 g_variant_type_equal (type, G_VARIANT_TYPE_OBJECT_PATH) ||
775 g_variant_type_equal (type, G_VARIANT_TYPE_SIGNATURE))
780 else if (g_variant_type_equal (type, G_VARIANT_TYPE_VARIANT))
786 g_assert_not_reached ();
796 /* same as the describe_type() function above, but iterates over
797 * typeinfo instead of types.
800 describe_info (GVariantTypeInfo *info)
804 switch (g_variant_type_info_get_type_char (info))
806 case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
810 element = describe_info (g_variant_type_info_element (info));
811 result = g_strdup_printf ("m of %s", element);
816 case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
820 element = describe_info (g_variant_type_info_element (info));
821 result = g_strdup_printf ("a of %s", element);
826 case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
828 const gchar *sep = "";
833 string = g_string_new ("t of [");
834 length = g_variant_type_info_n_members (info);
836 for (i = 0; i < length; i++)
838 const GVariantMemberInfo *minfo;
841 g_string_append (string, sep);
844 minfo = g_variant_type_info_member_info (info, i);
845 subtype = describe_info (minfo->type_info);
846 g_string_append (string, subtype);
850 g_string_append_c (string, ']');
852 result = g_string_free (string, FALSE);
856 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
858 const GVariantMemberInfo *keyinfo, *valueinfo;
861 g_assert_cmpint (g_variant_type_info_n_members (info), ==, 2);
862 keyinfo = g_variant_type_info_member_info (info, 0);
863 valueinfo = g_variant_type_info_member_info (info, 1);
864 key = describe_info (keyinfo->type_info);
865 value = describe_info (valueinfo->type_info);
866 result = g_strjoin ("", "e of [", key, ", ", value, "]", NULL);
872 case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
873 result = g_strdup ("V");
877 result = g_strdup (g_variant_type_info_get_type_string (info));
878 g_assert_cmpint (strlen (result), ==, 1);
885 /* check that the O(1) method of calculating offsets meshes with the
886 * results of simple iteration.
889 check_offsets (GVariantTypeInfo *info,
890 const GVariantType *type)
895 length = g_variant_type_info_n_members (info);
896 g_assert_cmpint (length, ==, g_variant_type_n_items (type));
898 /* the 'flavour' is the low order bits of the ending point of
899 * variable-size items in the tuple. this lets us test that the type
900 * info is correct for various starting alignments.
902 for (flavour = 0; flavour < 8; flavour++)
904 const GVariantType *subtype;
905 gsize last_offset_index;
910 subtype = g_variant_type_first (type);
911 last_offset_index = -1;
915 /* go through the tuple, keeping track of our position */
916 for (i = 0; i < length; i++)
921 calculate_type_info (subtype, &fixed_size, &alignment);
923 position = ALIGNED (position, alignment);
925 /* compare our current aligned position (ie: the start of this
926 * item) to the start offset that would be calculated if we
930 const GVariantMemberInfo *member;
933 member = g_variant_type_info_member_info (info, i);
934 g_assert_cmpint (member->i, ==, last_offset_index);
936 /* do the calculation using the typeinfo */
942 /* did we reach the same spot? */
943 g_assert_cmpint (start, ==, position);
948 /* fixed size. add that size. */
949 position += fixed_size;
953 /* variable size. do the flavouring. */
954 while ((position & 0x7) != flavour)
957 /* and store the offset, just like it would be in the
960 last_offset = position;
965 subtype = g_variant_type_next (subtype);
968 /* make sure we used up exactly all the types */
969 g_assert (subtype == NULL);
974 test_gvarianttypeinfo (void)
978 for (i = 0; i < 2000; i++)
980 GString *type_string, *description;
981 gsize fixed_size1, fixed_size2;
982 guint alignment1, alignment2;
983 GVariantTypeInfo *info;
987 type_string = g_string_new (NULL);
988 description = g_string_new (NULL);
991 type = append_type_string (type_string, description, TRUE, 6);
993 /* create a typeinfo for it */
994 info = g_variant_type_info_get (type);
996 /* make sure the typeinfo has the right type string */
997 g_assert_cmpstr (g_variant_type_info_get_type_string (info), ==,
1000 /* calculate the alignment and fixed size, compare to the
1001 * typeinfo's calculations
1003 calculate_type_info (type, &fixed_size1, &alignment1);
1004 g_variant_type_info_query (info, &alignment2, &fixed_size2);
1005 g_assert_cmpint (fixed_size1, ==, fixed_size2);
1006 g_assert_cmpint (alignment1, ==, alignment2 + 1);
1008 /* test the iteration functions over typeinfo structures by
1009 * "describing" the typeinfo and verifying equality.
1011 desc = describe_info (info);
1012 g_assert_cmpstr (desc, ==, description->str);
1014 /* do extra checks for containers */
1015 if (g_variant_type_is_array (type) ||
1016 g_variant_type_is_maybe (type))
1018 const GVariantType *element;
1022 element = g_variant_type_element (type);
1023 calculate_type_info (element, &efs1, &ea1);
1024 g_variant_type_info_query_element (info, &ea2, &efs2);
1025 g_assert_cmpint (efs1, ==, efs2);
1026 g_assert_cmpint (ea1, ==, ea2 + 1);
1028 g_assert_cmpint (ea1, ==, alignment1);
1029 g_assert_cmpint (0, ==, fixed_size1);
1031 else if (g_variant_type_is_tuple (type) ||
1032 g_variant_type_is_dict_entry (type))
1034 /* make sure the "magic constants" are working */
1035 check_offsets (info, type);
1038 g_string_free (type_string, TRUE);
1039 g_string_free (description, TRUE);
1040 g_variant_type_info_unref (info);
1041 g_variant_type_free (type);
1045 g_variant_type_info_assert_no_infos ();
1048 #define MAX_FIXED_MULTIPLIER 256
1049 #define MAX_INSTANCE_SIZE 1024
1050 #define MAX_ARRAY_CHILDREN 128
1051 #define MAX_TUPLE_CHILDREN 128
1053 /* this function generates a random type such that all characteristics
1054 * that are "interesting" to the serialiser are tested.
1056 * this basically means:
1057 * - test different alignments
1058 * - test variable sized items and fixed sized items
1059 * - test different fixed sizes
1062 random_type_string (void)
1064 const guchar base_types[] = "ynix";
1067 base_type = base_types[g_test_rand_int_range (0, 4)];
1069 if (g_test_rand_bit ())
1070 /* construct a fixed-sized type */
1072 char type_string[MAX_FIXED_MULTIPLIER];
1076 multiplier = g_test_rand_int_range (1, sizeof type_string - 1);
1078 type_string[i++] = '(';
1079 while (multiplier--)
1080 type_string[i++] = base_type;
1081 type_string[i++] = ')';
1083 return g_strndup (type_string, i);
1086 /* construct a variable-sized type */
1088 char type_string[2] = { 'a', base_type };
1090 return g_strndup (type_string, 2);
1096 GVariantTypeInfo *type_info;
1099 gboolean is_fixed_sized;
1103 #define INSTANCE_MAGIC 1287582829
1107 static RandomInstance *
1108 random_instance (GVariantTypeInfo *type_info)
1110 RandomInstance *instance;
1112 instance = g_slice_new (RandomInstance);
1114 if (type_info == NULL)
1116 gchar *str = random_type_string ();
1117 instance->type_info = g_variant_type_info_get (G_VARIANT_TYPE (str));
1121 instance->type_info = g_variant_type_info_ref (type_info);
1123 instance->seed = g_test_rand_int ();
1125 g_variant_type_info_query (instance->type_info,
1126 &instance->alignment,
1129 instance->is_fixed_sized = instance->size != 0;
1131 if (!instance->is_fixed_sized)
1132 instance->size = g_test_rand_int_range (0, MAX_INSTANCE_SIZE);
1134 instance->magic = INSTANCE_MAGIC;
1140 random_instance_free (RandomInstance *instance)
1142 g_variant_type_info_unref (instance->type_info);
1143 g_slice_free (RandomInstance, instance);
1147 append_instance_size (RandomInstance *instance,
1150 *offset += (-*offset) & instance->alignment;
1151 *offset += instance->size;
1155 random_instance_write (RandomInstance *instance,
1161 g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0);
1163 rand = g_rand_new_with_seed (instance->seed);
1164 for (i = 0; i < instance->size; i++)
1165 buffer[i] = g_rand_int (rand);
1170 append_instance_data (RandomInstance *instance,
1173 while (((gsize) *buffer) & instance->alignment)
1174 *(*buffer)++ = '\0';
1176 random_instance_write (instance, *buffer);
1177 *buffer += instance->size;
1181 random_instance_assert (RandomInstance *instance,
1188 g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0);
1189 g_assert_cmpint (size, ==, instance->size);
1191 rand = g_rand_new_with_seed (instance->seed);
1192 for (i = 0; i < instance->size; i++)
1194 guchar byte = g_rand_int (rand);
1196 g_assert (buffer[i] == byte);
1200 return i == instance->size;
1204 random_instance_check (RandomInstance *instance,
1211 g_assert_cmpint ((gsize) buffer & ALIGN_BITS & instance->alignment, ==, 0);
1213 if (size != instance->size)
1216 rand = g_rand_new_with_seed (instance->seed);
1217 for (i = 0; i < instance->size; i++)
1218 if (buffer[i] != (guchar) g_rand_int (rand))
1222 return i == instance->size;
1226 random_instance_filler (GVariantSerialised *serialised,
1229 RandomInstance *instance = data;
1231 g_assert (instance->magic == INSTANCE_MAGIC);
1233 if (serialised->type_info == NULL)
1234 serialised->type_info = instance->type_info;
1236 if (serialised->size == 0)
1237 serialised->size = instance->size;
1239 g_assert (serialised->type_info == instance->type_info);
1240 g_assert (serialised->size == instance->size);
1242 if (serialised->data)
1243 random_instance_write (instance, serialised->data);
1247 calculate_offset_size (gsize body_size,
1253 if (body_size + n_offsets <= G_MAXUINT8)
1256 if (body_size + 2 * n_offsets <= G_MAXUINT16)
1259 if (body_size + 4 * n_offsets <= G_MAXUINT32)
1262 /* the test case won't generate anything bigger */
1263 g_assert_not_reached ();
1267 flavoured_malloc (gsize size, gsize flavour)
1269 g_assert (flavour < 8);
1274 return ((gchar *) g_malloc (size + flavour)) + flavour;
1278 flavoured_free (gpointer data,
1283 g_free (((gchar *) data) - flavour);
1287 align_malloc (gsize size)
1291 #ifdef HAVE_POSIX_MEMALIGN
1292 if (posix_memalign (&mem, 8, size))
1293 g_error ("posix_memalign failed");
1295 /* NOTE: there may be platforms that lack posix_memalign() and also
1296 * have malloc() that returns non-8-aligned. if so, we need to try
1299 mem = malloc (size);
1306 align_free (gpointer mem)
1312 append_offset (guchar **offset_ptr,
1318 guchar bytes[sizeof (gsize)];
1322 tmpvalue.integer = GSIZE_TO_LE (offset);
1323 memcpy (*offset_ptr, tmpvalue.bytes, offset_size);
1324 *offset_ptr += offset_size;
1328 prepend_offset (guchar **offset_ptr,
1334 guchar bytes[sizeof (gsize)];
1338 *offset_ptr -= offset_size;
1339 tmpvalue.integer = GSIZE_TO_LE (offset);
1340 memcpy (*offset_ptr, tmpvalue.bytes, offset_size);
1346 GVariantTypeInfo *type_info;
1347 RandomInstance *instance;
1351 instance = random_instance (NULL);
1354 const gchar *element;
1357 element = g_variant_type_info_get_type_string (instance->type_info);
1358 tmp = g_strdup_printf ("m%s", element);
1359 type_info = g_variant_type_info_get (G_VARIANT_TYPE (tmp));
1363 needed_size = g_variant_serialiser_needed_size (type_info,
1364 random_instance_filler,
1366 g_assert_cmpint (needed_size, ==, 0);
1368 needed_size = g_variant_serialiser_needed_size (type_info,
1369 random_instance_filler,
1370 (gpointer *) &instance, 1);
1372 if (instance->is_fixed_sized)
1373 g_assert_cmpint (needed_size, ==, instance->size);
1375 g_assert_cmpint (needed_size, ==, instance->size + 1);
1380 ptr = data = align_malloc (needed_size);
1381 append_instance_data (instance, &ptr);
1383 if (!instance->is_fixed_sized)
1386 g_assert_cmpint (ptr - data, ==, needed_size);
1393 alignment = (instance->alignment & ALIGN_BITS) + 1;
1395 for (flavour = 0; flavour < 8; flavour += alignment)
1397 GVariantSerialised serialised;
1398 GVariantSerialised child;
1400 serialised.type_info = type_info;
1401 serialised.data = flavoured_malloc (needed_size, flavour);
1402 serialised.size = needed_size;
1404 g_variant_serialiser_serialise (serialised,
1405 random_instance_filler,
1406 (gpointer *) &instance, 1);
1407 child = g_variant_serialised_get_child (serialised, 0);
1408 g_assert (child.type_info == instance->type_info);
1409 random_instance_assert (instance, child.data, child.size);
1410 g_variant_type_info_unref (child.type_info);
1411 flavoured_free (serialised.data, flavour);
1415 g_variant_type_info_unref (type_info);
1416 random_instance_free (instance);
1425 for (i = 0; i < 1000; i++)
1428 g_variant_type_info_assert_no_infos ();
1434 GVariantTypeInfo *element_info;
1435 GVariantTypeInfo *array_info;
1436 RandomInstance **instances;
1443 gchar *element_type, *array_type;
1445 element_type = random_type_string ();
1446 array_type = g_strdup_printf ("a%s", element_type);
1448 element_info = g_variant_type_info_get (G_VARIANT_TYPE (element_type));
1449 array_info = g_variant_type_info_get (G_VARIANT_TYPE (array_type));
1450 g_assert (g_variant_type_info_element (array_info) == element_info);
1452 g_free (element_type);
1453 g_free (array_type);
1459 n_children = g_test_rand_int_range (0, MAX_ARRAY_CHILDREN);
1460 instances = g_new (RandomInstance *, n_children);
1461 for (i = 0; i < n_children; i++)
1462 instances[i] = random_instance (element_info);
1465 needed_size = g_variant_serialiser_needed_size (array_info,
1466 random_instance_filler,
1467 (gpointer *) instances,
1471 gsize element_fixed_size;
1472 gsize body_size = 0;
1475 for (i = 0; i < n_children; i++)
1476 append_instance_size (instances[i], &body_size);
1478 g_variant_type_info_query (element_info, NULL, &element_fixed_size);
1480 if (!element_fixed_size)
1482 offset_size = calculate_offset_size (body_size, n_children);
1484 if (offset_size == 0)
1490 g_assert_cmpint (needed_size, ==, body_size + n_children * offset_size);
1494 guchar *offset_ptr, *body_ptr;
1497 body_ptr = data = align_malloc (needed_size);
1498 offset_ptr = body_ptr + needed_size - offset_size * n_children;
1500 for (i = 0; i < n_children; i++)
1502 append_instance_data (instances[i], &body_ptr);
1503 append_offset (&offset_ptr, body_ptr - data, offset_size);
1506 g_assert (body_ptr == data + needed_size - offset_size * n_children);
1507 g_assert (offset_ptr == data + needed_size);
1515 g_variant_type_info_query (array_info, &alignment, NULL);
1516 alignment = (alignment & ALIGN_BITS) + 1;
1518 for (flavour = 0; flavour < 8; flavour += alignment)
1520 GVariantSerialised serialised;
1522 serialised.type_info = array_info;
1523 serialised.data = flavoured_malloc (needed_size, flavour);
1524 serialised.size = needed_size;
1526 g_variant_serialiser_serialise (serialised, random_instance_filler,
1527 (gpointer *) instances, n_children);
1529 if (serialised.size)
1530 g_assert (memcmp (serialised.data, data, serialised.size) == 0);
1532 g_assert (g_variant_serialised_n_children (serialised) == n_children);
1534 for (i = 0; i < n_children; i++)
1536 GVariantSerialised child;
1538 child = g_variant_serialised_get_child (serialised, i);
1539 g_assert (child.type_info == instances[i]->type_info);
1540 random_instance_assert (instances[i], child.data, child.size);
1541 g_variant_type_info_unref (child.type_info);
1544 flavoured_free (serialised.data, flavour);
1551 for (i = 0; i < n_children; i++)
1552 random_instance_free (instances[i]);
1556 g_variant_type_info_unref (element_info);
1557 g_variant_type_info_unref (array_info);
1566 for (i = 0; i < 100; i++)
1569 g_variant_type_info_assert_no_infos ();
1575 GVariantTypeInfo *type_info;
1576 RandomInstance **instances;
1577 gboolean fixed_size;
1584 n_children = g_test_rand_int_range (0, MAX_TUPLE_CHILDREN);
1585 instances = g_new (RandomInstance *, n_children);
1588 GString *type_string;
1594 type_string = g_string_new ("(");
1595 for (i = 0; i < n_children; i++)
1599 instances[i] = random_instance (NULL);
1601 alignment |= instances[i]->alignment;
1602 if (!instances[i]->is_fixed_sized)
1605 str = g_variant_type_info_get_type_string (instances[i]->type_info);
1606 g_string_append (type_string, str);
1608 g_string_append_c (type_string, ')');
1610 type_info = g_variant_type_info_get (G_VARIANT_TYPE (type_string->str));
1611 g_string_free (type_string, TRUE);
1614 needed_size = g_variant_serialiser_needed_size (type_info,
1615 random_instance_filler,
1616 (gpointer *) instances,
1619 gsize body_size = 0;
1623 for (i = 0; i < n_children; i++)
1625 append_instance_size (instances[i], &body_size);
1627 if (i != n_children - 1 && !instances[i]->is_fixed_sized)
1633 body_size += (-body_size) & alignment;
1635 g_assert ((body_size == 0) == (n_children == 0));
1636 if (n_children == 0)
1640 offset_size = calculate_offset_size (body_size, offsets);
1641 g_assert_cmpint (needed_size, ==, body_size + offsets * offset_size);
1649 body_ptr = data = align_malloc (needed_size);
1650 ofs_ptr = body_ptr + needed_size;
1652 for (i = 0; i < n_children; i++)
1654 append_instance_data (instances[i], &body_ptr);
1656 if (i != n_children - 1 && !instances[i]->is_fixed_sized)
1657 prepend_offset (&ofs_ptr, body_ptr - data, offset_size);
1662 while (((gsize) body_ptr) & alignment)
1665 g_assert ((body_ptr == data) == (n_children == 0));
1666 if (n_children == 0)
1672 g_assert (body_ptr == ofs_ptr);
1679 alignment = (alignment & ALIGN_BITS) + 1;
1681 for (flavour = 0; flavour < 8; flavour += alignment)
1683 GVariantSerialised serialised;
1685 serialised.type_info = type_info;
1686 serialised.data = flavoured_malloc (needed_size, flavour);
1687 serialised.size = needed_size;
1689 g_variant_serialiser_serialise (serialised, random_instance_filler,
1690 (gpointer *) instances, n_children);
1692 if (serialised.size)
1693 g_assert (memcmp (serialised.data, data, serialised.size) == 0);
1695 g_assert (g_variant_serialised_n_children (serialised) == n_children);
1697 for (i = 0; i < n_children; i++)
1699 GVariantSerialised child;
1701 child = g_variant_serialised_get_child (serialised, i);
1702 g_assert (child.type_info == instances[i]->type_info);
1703 random_instance_assert (instances[i], child.data, child.size);
1704 g_variant_type_info_unref (child.type_info);
1707 flavoured_free (serialised.data, flavour);
1714 for (i = 0; i < n_children; i++)
1715 random_instance_free (instances[i]);
1719 g_variant_type_info_unref (type_info);
1728 for (i = 0; i < 100; i++)
1731 g_variant_type_info_assert_no_infos ();
1737 GVariantTypeInfo *type_info;
1738 RandomInstance *instance;
1739 const gchar *type_string;
1744 type_info = g_variant_type_info_get (G_VARIANT_TYPE_VARIANT);
1745 instance = random_instance (NULL);
1747 type_string = g_variant_type_info_get_type_string (instance->type_info);
1748 len = strlen (type_string);
1750 needed_size = g_variant_serialiser_needed_size (type_info,
1751 random_instance_filler,
1752 (gpointer *) &instance, 1);
1754 g_assert_cmpint (needed_size, ==, instance->size + 1 + len);
1759 ptr = data = align_malloc (needed_size);
1760 append_instance_data (instance, &ptr);
1762 memcpy (ptr, type_string, len);
1765 g_assert (data + needed_size == ptr);
1772 /* variants are always 8-aligned */
1773 alignment = ALIGN_BITS + 1;
1775 for (flavour = 0; flavour < 8; flavour += alignment)
1777 GVariantSerialised serialised;
1778 GVariantSerialised child;
1780 serialised.type_info = type_info;
1781 serialised.data = flavoured_malloc (needed_size, flavour);
1782 serialised.size = needed_size;
1784 g_variant_serialiser_serialise (serialised, random_instance_filler,
1785 (gpointer *) &instance, 1);
1787 if (serialised.size)
1788 g_assert (memcmp (serialised.data, data, serialised.size) == 0);
1790 g_assert (g_variant_serialised_n_children (serialised) == 1);
1792 child = g_variant_serialised_get_child (serialised, 0);
1793 g_assert (child.type_info == instance->type_info);
1794 random_instance_check (instance, child.data, child.size);
1796 g_variant_type_info_unref (child.type_info);
1797 flavoured_free (serialised.data, flavour);
1801 g_variant_type_info_unref (type_info);
1802 random_instance_free (instance);
1807 test_variants (void)
1811 for (i = 0; i < 100; i++)
1814 g_variant_type_info_assert_no_infos ();
1827 #define is_objpath is_string | 2
1828 #define is_sig is_string | 4
1830 { is_nval, 0, NULL },
1831 { is_nval, 13, "hello\xffworld!" },
1832 { is_string, 13, "hello world!" },
1833 { is_nval, 13, "hello world\0" },
1834 { is_nval, 13, "hello\0world!" },
1835 { is_nval, 12, "hello world!" },
1836 { is_nval, 13, "hello world!\xff" },
1838 { is_objpath, 2, "/" },
1839 { is_objpath, 3, "/a" },
1840 { is_string, 3, "//" },
1841 { is_objpath, 11, "/some/path" },
1842 { is_string, 12, "/some/path/" },
1843 { is_nval, 11, "/some\0path" },
1844 { is_string, 11, "/some\\path" },
1845 { is_string, 12, "/some//path" },
1846 { is_string, 12, "/some-/path" },
1850 { is_sig, 5, "(si)" },
1851 { is_string, 4, "(si" },
1852 { is_string, 2, "*" },
1853 { is_sig, 3, "ai" },
1854 { is_string, 3, "mi" },
1855 { is_string, 2, "r" },
1856 { is_sig, 15, "(yyy{sv}ssiai)" },
1857 { is_string, 16, "(yyy{yv}ssiai))" },
1858 { is_string, 15, "(yyy{vv}ssiai)" },
1859 { is_string, 15, "(yyy{sv)ssiai}" }
1863 for (i = 0; i < G_N_ELEMENTS (test_cases); i++)
1867 flags = g_variant_serialiser_is_string (test_cases[i].data,
1871 flags |= g_variant_serialiser_is_object_path (test_cases[i].data,
1875 flags |= g_variant_serialiser_is_signature (test_cases[i].data,
1879 g_assert (flags == test_cases[i].flags);
1883 typedef struct _TreeInstance TreeInstance;
1884 struct _TreeInstance
1886 GVariantTypeInfo *info;
1888 TreeInstance **children;
1900 static GVariantType *
1901 make_random_definite_type (int depth)
1903 GString *description;
1904 GString *type_string;
1907 description = g_string_new (NULL);
1908 type_string = g_string_new (NULL);
1909 type = append_type_string (type_string, description, TRUE, depth);
1910 g_string_free (description, TRUE);
1911 g_string_free (type_string, TRUE);
1917 make_random_string (gchar *string,
1919 const GVariantType *type)
1923 /* create strings that are valid signature strings */
1924 #define good_chars "bynqiuxthdsog"
1926 for (i = 0; i < size - 1; i++)
1927 string[i] = good_chars[g_test_rand_int_range (0, strlen (good_chars))];
1930 /* in case we need an object path, prefix a '/' */
1931 if (*g_variant_type_peek_string (type) == 'o')
1937 static TreeInstance *
1938 tree_instance_new (const GVariantType *type,
1941 const GVariantType *child_type = NULL;
1942 GVariantType *mytype = NULL;
1943 TreeInstance *instance;
1944 gboolean is_tuple_type;
1947 type = mytype = make_random_definite_type (depth);
1949 instance = g_slice_new (TreeInstance);
1950 instance->info = g_variant_type_info_get (type);
1951 instance->children = NULL;
1952 instance->n_children = 0;
1953 instance->data_size = 0;
1955 is_tuple_type = FALSE;
1957 switch (*g_variant_type_peek_string (type))
1959 case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
1960 instance->n_children = g_test_rand_int_range (0, 2);
1961 child_type = g_variant_type_element (type);
1964 case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
1965 instance->n_children = g_test_rand_int_range (0, MAX_ARRAY_CHILDREN);
1966 child_type = g_variant_type_element (type);
1969 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
1970 case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
1971 instance->n_children = g_variant_type_n_items (type);
1972 child_type = g_variant_type_first (type);
1973 is_tuple_type = TRUE;
1976 case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
1977 instance->n_children = 1;
1982 instance->data.integer = g_test_rand_int_range (0, 2);
1983 instance->data_size = 1;
1987 instance->data.integer = g_test_rand_int ();
1988 instance->data_size = 1;
1992 instance->data.integer = g_test_rand_int ();
1993 instance->data_size = 2;
1996 case 'i': case 'u': case 'h':
1997 instance->data.integer = g_test_rand_int ();
1998 instance->data_size = 4;
2002 instance->data.integer = g_test_rand_int ();
2003 instance->data.integer <<= 32;
2004 instance->data.integer |= (guint32) g_test_rand_int ();
2005 instance->data_size = 8;
2009 instance->data.single = g_test_rand_double ();
2010 instance->data_size = 4;
2014 instance->data.floating = g_test_rand_double ();
2015 instance->data_size = 8;
2018 case 's': case 'o': case 'g':
2019 instance->data_size = g_test_rand_int_range (10, 200);
2020 make_random_string (instance->data.string, instance->data_size, type);
2024 if (instance->data_size == 0)
2025 /* no data -> it is a container */
2029 instance->children = g_new (TreeInstance *, instance->n_children);
2031 for (i = 0; i < instance->n_children; i++)
2033 instance->children[i] = tree_instance_new (child_type, depth - 1);
2036 child_type = g_variant_type_next (child_type);
2039 g_assert (!is_tuple_type || child_type == NULL);
2042 g_variant_type_free (mytype);
2048 tree_instance_free (TreeInstance *instance)
2052 g_variant_type_info_unref (instance->info);
2053 for (i = 0; i < instance->n_children; i++)
2054 tree_instance_free (instance->children[i]);
2055 g_free (instance->children);
2056 g_slice_free (TreeInstance, instance);
2059 static gboolean i_am_writing_byteswapped;
2062 tree_filler (GVariantSerialised *serialised,
2065 TreeInstance *instance = data;
2067 if (serialised->type_info == NULL)
2068 serialised->type_info = instance->info;
2070 if (instance->data_size == 0)
2071 /* is a container */
2073 if (serialised->size == 0)
2075 g_variant_serialiser_needed_size (instance->info, tree_filler,
2076 (gpointer *) instance->children,
2077 instance->n_children);
2079 if (serialised->data)
2080 g_variant_serialiser_serialise (*serialised, tree_filler,
2081 (gpointer *) instance->children,
2082 instance->n_children);
2087 if (serialised->size == 0)
2088 serialised->size = instance->data_size;
2090 if (serialised->data)
2092 switch (instance->data_size)
2095 *serialised->data = instance->data.integer;
2100 guint16 value = instance->data.integer;
2102 if (i_am_writing_byteswapped)
2103 value = GUINT16_SWAP_LE_BE (value);
2105 *(guint16 *) serialised->data = value;
2111 guint32 value = instance->data.integer;
2113 if (i_am_writing_byteswapped)
2114 value = GUINT32_SWAP_LE_BE (value);
2116 *(guint32 *) serialised->data = value;
2122 guint64 value = instance->data.integer;
2124 if (i_am_writing_byteswapped)
2125 value = GUINT64_SWAP_LE_BE (value);
2127 *(guint64 *) serialised->data = value;
2132 memcpy (serialised->data,
2133 instance->data.string,
2134 instance->data_size);
2142 check_tree (TreeInstance *instance,
2143 GVariantSerialised serialised)
2145 if (instance->info != serialised.type_info)
2148 if (instance->data_size == 0)
2149 /* is a container */
2153 if (g_variant_serialised_n_children (serialised) !=
2154 instance->n_children)
2157 for (i = 0; i < instance->n_children; i++)
2159 GVariantSerialised child;
2160 gpointer data = NULL;
2163 child = g_variant_serialised_get_child (serialised, i);
2164 if (child.size && child.data == NULL)
2165 child.data = data = g_malloc0 (child.size);
2166 ok = check_tree (instance->children[i], child);
2167 g_variant_type_info_unref (child.type_info);
2179 switch (instance->data_size)
2182 g_assert (serialised.size == 1);
2183 return *(guint8 *) serialised.data ==
2184 (guint8) instance->data.integer;
2187 g_assert (serialised.size == 2);
2188 return *(guint16 *) serialised.data ==
2189 (guint16) instance->data.integer;
2192 g_assert (serialised.size == 4);
2193 return *(guint32 *) serialised.data ==
2194 (guint32) instance->data.integer;
2197 g_assert (serialised.size == 8);
2198 return *(guint64 *) serialised.data ==
2199 (guint64) instance->data.integer;
2202 if (serialised.size != instance->data_size)
2205 return memcmp (serialised.data,
2206 instance->data.string,
2207 instance->data_size) == 0;
2213 serialise_tree (TreeInstance *tree,
2214 GVariantSerialised *serialised)
2216 GVariantSerialised empty = {0, };
2218 *serialised = empty;
2219 tree_filler (serialised, tree);
2220 serialised->data = g_malloc (serialised->size);
2221 tree_filler (serialised, tree);
2225 test_byteswap (void)
2227 GVariantSerialised one, two;
2230 tree = tree_instance_new (NULL, 3);
2231 serialise_tree (tree, &one);
2233 i_am_writing_byteswapped = TRUE;
2234 serialise_tree (tree, &two);
2235 i_am_writing_byteswapped = FALSE;
2237 g_variant_serialised_byteswap (two);
2239 g_assert_cmpint (one.size, ==, two.size);
2240 g_assert (memcmp (one.data, two.data, one.size) == 0);
2242 tree_instance_free (tree);
2248 test_byteswaps (void)
2252 for (i = 0; i < 200; i++)
2255 g_variant_type_info_assert_no_infos ();
2259 test_fuzz (gdouble *fuzziness)
2261 GVariantSerialised serialised;
2264 /* make an instance */
2265 tree = tree_instance_new (NULL, 3);
2268 serialise_tree (tree, &serialised);
2270 g_assert (g_variant_serialised_is_normal (serialised));
2271 g_assert (check_tree (tree, serialised));
2273 if (serialised.size)
2275 gboolean fuzzed = FALSE;
2282 for (i = 0; i < serialised.size; i++)
2283 if (randomly (*fuzziness))
2285 serialised.data[i] += g_test_rand_int_range (1, 256);
2290 /* at least one byte in the serialised data has changed.
2292 * this means that at least one of the following is true:
2294 * - the serialised data now represents a different value:
2295 * check_tree() will return FALSE
2297 * - the serialised data is in non-normal form:
2298 * g_variant_serialiser_is_normal() will return FALSE
2300 * we always do both checks to increase exposure of the serialiser
2303 a = g_variant_serialised_is_normal (serialised);
2304 b = check_tree (tree, serialised);
2306 g_assert (!a || !b);
2309 tree_instance_free (tree);
2310 g_free (serialised.data);
2315 test_fuzzes (gpointer data)
2320 fuzziness = GPOINTER_TO_INT (data) / 100.;
2322 for (i = 0; i < 200; i++)
2323 test_fuzz (&fuzziness);
2325 g_variant_type_info_assert_no_infos ();
2329 tree_instance_get_gvariant (TreeInstance *tree)
2331 const GVariantType *type;
2334 type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
2336 switch (g_variant_type_info_get_type_char (tree->info))
2338 case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
2340 const GVariantType *child_type;
2343 if (tree->n_children)
2344 child = tree_instance_get_gvariant (tree->children[0]);
2348 child_type = g_variant_type_element (type);
2350 if (child != NULL && randomly (0.5))
2353 result = g_variant_new_maybe (child_type, child);
2357 case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
2359 const GVariantType *child_type;
2360 GVariant **children;
2363 children = g_new (GVariant *, tree->n_children);
2364 for (i = 0; i < tree->n_children; i++)
2365 children[i] = tree_instance_get_gvariant (tree->children[i]);
2367 child_type = g_variant_type_element (type);
2369 if (i > 0 && randomly (0.5))
2372 result = g_variant_new_array (child_type, children, tree->n_children);
2377 case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
2379 GVariant **children;
2382 children = g_new (GVariant *, tree->n_children);
2383 for (i = 0; i < tree->n_children; i++)
2384 children[i] = tree_instance_get_gvariant (tree->children[i]);
2386 result = g_variant_new_tuple (children, tree->n_children);
2391 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
2393 GVariant *key, *val;
2395 g_assert (tree->n_children == 2);
2397 key = tree_instance_get_gvariant (tree->children[0]);
2398 val = tree_instance_get_gvariant (tree->children[1]);
2400 result = g_variant_new_dict_entry (key, val);
2404 case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
2408 g_assert (tree->n_children == 1);
2410 value = tree_instance_get_gvariant (tree->children[0]);
2411 result = g_variant_new_variant (value);
2416 result = g_variant_new_boolean (tree->data.integer > 0);
2420 result = g_variant_new_byte (tree->data.integer);
2424 result = g_variant_new_int16 (tree->data.integer);
2428 result = g_variant_new_uint16 (tree->data.integer);
2432 result = g_variant_new_int32 (tree->data.integer);
2436 result = g_variant_new_uint32 (tree->data.integer);
2440 result = g_variant_new_int64 (tree->data.integer);
2444 result = g_variant_new_uint64 (tree->data.integer);
2448 result = g_variant_new_handle (tree->data.integer);
2452 result = g_variant_new_float (tree->data.single);
2456 result = g_variant_new_double (tree->data.floating);
2460 result = g_variant_new_string (tree->data.string);
2464 result = g_variant_new_object_path (tree->data.string);
2468 result = g_variant_new_signature (tree->data.string);
2472 g_assert_not_reached ();
2479 create_random_gvariant (guint depth)
2484 tree = tree_instance_new (NULL, depth);
2485 value = g_variant_take_ref (tree_instance_get_gvariant (tree));
2486 tree_instance_free (tree);
2492 tree_instance_check_gvariant (TreeInstance *tree,
2495 const GVariantType *type;
2497 type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
2498 g_assert (g_variant_is_of_type (value, type));
2500 switch (g_variant_type_info_get_type_char (tree->info))
2502 case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
2507 child = g_variant_get_maybe (value);
2509 if (child != NULL && tree->n_children == 1)
2510 equal = tree_instance_check_gvariant (tree->children[0], child);
2511 else if (child == NULL && tree->n_children == 0)
2517 g_variant_unref (child);
2523 case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
2524 case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
2525 case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
2529 if (g_variant_n_children (value) != tree->n_children)
2532 for (i = 0; i < tree->n_children; i++)
2537 child = g_variant_get_child_value (value, i);
2538 equal = tree_instance_check_gvariant (tree->children[i], child);
2539 g_variant_unref (child);
2549 case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
2551 const gchar *str1, *str2;
2555 child = g_variant_get_variant (value);
2556 str1 = g_variant_get_type_string (child);
2557 str2 = g_variant_type_info_get_type_string (tree->children[0]->info);
2558 /* GVariant only keeps one copy of type strings around */
2559 equal = str1 == str2 &&
2560 tree_instance_check_gvariant (tree->children[0], child);
2562 g_variant_unref (child);
2569 return g_variant_get_boolean (value) == tree->data.integer;
2572 return g_variant_get_byte (value) == (guchar) tree->data.integer;
2575 return g_variant_get_int16 (value) == (gint16) tree->data.integer;
2578 return g_variant_get_uint16 (value) == (guint16) tree->data.integer;
2581 return g_variant_get_int32 (value) == (gint32) tree->data.integer;
2584 return g_variant_get_uint32 (value) == (guint32) tree->data.integer;
2587 return g_variant_get_int64 (value) == (gint64) tree->data.integer;
2590 return g_variant_get_uint64 (value) == (guint64) tree->data.integer;
2593 return g_variant_get_handle (value) == (gint32) tree->data.integer;
2597 gfloat floating = g_variant_get_float (value);
2599 return memcmp (&floating, &tree->data.single, sizeof floating) == 0;
2604 gdouble floating = g_variant_get_double (value);
2606 return memcmp (&floating, &tree->data.floating, sizeof floating) == 0;
2612 return strcmp (g_variant_get_string (value, NULL),
2613 tree->data.string) == 0;
2616 g_assert_not_reached ();
2621 tree_instance_build_gvariant (TreeInstance *tree,
2622 GVariantBuilder *builder,
2625 const GVariantType *type;
2627 type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
2629 if (g_variant_type_is_container (type))
2633 /* force GVariantBuilder to guess the type half the time */
2634 if (guess_ok && randomly (0.5))
2636 if (g_variant_type_is_array (type) && tree->n_children)
2637 type = G_VARIANT_TYPE_ARRAY;
2639 if (g_variant_type_is_maybe (type) && tree->n_children)
2640 type = G_VARIANT_TYPE_MAYBE;
2642 if (g_variant_type_is_tuple (type))
2643 type = G_VARIANT_TYPE_TUPLE;
2645 if (g_variant_type_is_dict_entry (type))
2646 type = G_VARIANT_TYPE_DICT_ENTRY;
2651 g_variant_builder_open (builder, type);
2653 for (i = 0; i < tree->n_children; i++)
2654 tree_instance_build_gvariant (tree->children[i], builder, guess_ok);
2656 g_variant_builder_close (builder);
2659 g_variant_builder_add_value (builder, tree_instance_get_gvariant (tree));
2664 tree_instance_check_iter (TreeInstance *tree,
2669 value = g_variant_iter_next_value (iter);
2671 if (g_variant_is_container (value))
2675 iter = g_variant_iter_new (value);
2676 g_variant_unref (value);
2678 if (g_variant_iter_n_children (iter) != tree->n_children)
2680 g_variant_iter_free (iter);
2684 for (i = 0; i < tree->n_children; i++)
2685 if (!tree_instance_check_iter (tree->children[i], iter))
2687 g_variant_iter_free (iter);
2691 g_assert (g_variant_iter_next_value (iter) == NULL);
2692 g_variant_iter_free (iter);
2701 equal = tree_instance_check_gvariant (tree, value);
2702 g_variant_unref (value);
2709 test_container (void)
2715 tree = tree_instance_new (NULL, 3);
2716 value = g_variant_ref_sink (tree_instance_get_gvariant (tree));
2718 s1 = g_variant_print (value, TRUE);
2719 g_assert (tree_instance_check_gvariant (tree, value));
2721 g_variant_get_data (value);
2723 s2 = g_variant_print (value, TRUE);
2724 g_assert (tree_instance_check_gvariant (tree, value));
2726 g_assert_cmpstr (s1, ==, s2);
2728 if (g_variant_is_container (value))
2730 GVariantBuilder builder;
2736 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARIANT);
2737 tree_instance_build_gvariant (tree, &builder, TRUE);
2738 built = g_variant_builder_end (&builder);
2739 g_variant_ref_sink (built);
2740 g_variant_get_data (built);
2741 val = g_variant_get_variant (built);
2743 s3 = g_variant_print (val, TRUE);
2744 g_assert_cmpstr (s1, ==, s3);
2746 g_variant_iter_init (&iter, built);
2747 g_assert (tree_instance_check_iter (tree, &iter));
2748 g_assert (g_variant_iter_next_value (&iter) == NULL);
2750 g_variant_unref (built);
2751 g_variant_unref (val);
2755 tree_instance_free (tree);
2756 g_variant_unref (value);
2764 /* Test some different methods of creating strings */
2767 v = g_variant_new_string ("foo");
2768 g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo");
2769 g_variant_unref (v);
2772 v = g_variant_new_take_string (g_strdup ("foo"));
2773 g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo");
2774 g_variant_unref (v);
2776 v = g_variant_new_printf ("%s %d", "foo", 123);
2777 g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo 123");
2778 g_variant_unref (v);
2784 const gchar invalid[] = "hello\xffworld";
2787 /* ensure that the test data is not valid utf8... */
2788 g_assert (!g_utf8_validate (invalid, -1, NULL));
2790 /* load the data untrusted */
2791 value = g_variant_new_from_data (G_VARIANT_TYPE_STRING,
2792 invalid, sizeof invalid,
2795 /* ensure that the problem is caught and we get valid UTF-8 */
2796 g_assert (g_utf8_validate (g_variant_get_string (value, NULL), -1, NULL));
2797 g_variant_unref (value);
2800 /* now load it trusted */
2801 value = g_variant_new_from_data (G_VARIANT_TYPE_STRING,
2802 invalid, sizeof invalid,
2805 /* ensure we get the invalid data (ie: make sure that time wasn't
2806 * wasted on validating data that was marked as trusted)
2808 g_assert (g_variant_get_string (value, NULL) == invalid);
2809 g_variant_unref (value);
2813 test_containers (void)
2817 for (i = 0; i < 100; i++)
2822 g_variant_type_info_assert_no_infos ();
2826 test_format_strings (void)
2831 g_assert (g_variant_format_string_scan ("i", NULL, &end) && *end == '\0');
2832 g_assert (g_variant_format_string_scan ("@i", NULL, &end) && *end == '\0');
2833 g_assert (g_variant_format_string_scan ("@ii", NULL, &end) && *end == 'i');
2834 g_assert (g_variant_format_string_scan ("^a&s", NULL, &end) && *end == '\0');
2835 g_assert (g_variant_format_string_scan ("(^as)", NULL, &end) &&
2837 g_assert (!g_variant_format_string_scan ("(^s)", NULL, &end));
2838 g_assert (!g_variant_format_string_scan ("(^a)", NULL, &end));
2839 g_assert (!g_variant_format_string_scan ("(z)", NULL, &end));
2840 g_assert (!g_variant_format_string_scan ("az", NULL, &end));
2841 g_assert (!g_variant_format_string_scan ("{**}", NULL, &end));
2842 g_assert (!g_variant_format_string_scan ("{@**}", NULL, &end));
2843 g_assert (g_variant_format_string_scan ("{@y*}", NULL, &end) &&
2845 g_assert (g_variant_format_string_scan ("{yv}", NULL, &end) &&
2847 g_assert (!g_variant_format_string_scan ("{&?v}", NULL, &end));
2848 g_assert (g_variant_format_string_scan ("{@?v}", NULL, &end) &&
2850 g_assert (!g_variant_format_string_scan ("{&@sv}", NULL, &end));
2851 g_assert (!g_variant_format_string_scan ("{@&sv}", NULL, &end));
2852 g_assert (g_variant_format_string_scan ("{&sv}", NULL, &end) &&
2854 g_assert (!g_variant_format_string_scan ("{vv}", NULL, &end));
2855 g_assert (!g_variant_format_string_scan ("{y}", NULL, &end));
2856 g_assert (!g_variant_format_string_scan ("{yyy}", NULL, &end));
2857 g_assert (!g_variant_format_string_scan ("{ya}", NULL, &end));
2858 g_assert (g_variant_format_string_scan ("&s", NULL, &end) && *end == '\0');
2859 g_assert (!g_variant_format_string_scan ("&as", NULL, &end));
2860 g_assert (!g_variant_format_string_scan ("@z", NULL, &end));
2861 g_assert (!g_variant_format_string_scan ("az", NULL, &end));
2862 g_assert (!g_variant_format_string_scan ("a&s", NULL, &end));
2864 type = g_variant_format_string_scan_type ("mm(@xy^a&s*?@?)", NULL, &end);
2865 g_assert (type && *end == '\0');
2866 g_assert (g_variant_type_equal (type, G_VARIANT_TYPE ("mm(xyas*?\?)")));
2867 g_variant_type_free (type);
2869 type = g_variant_format_string_scan_type ("mm(@xy^a&*?@?)", NULL, NULL);
2870 g_assert (type == NULL);
2874 do_failed_test (const char *test,
2875 const gchar *pattern)
2877 g_test_trap_subprocess (test, 1000000, 0);
2878 g_test_trap_assert_failed ();
2879 g_test_trap_assert_stderr (pattern);
2883 test_invalid_varargs (void)
2888 if (!g_test_undefined ())
2891 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2892 "*GVariant format string*");
2893 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2894 "*valid_format_string*");
2895 value = g_variant_new ("z");
2896 g_test_assert_expected_messages ();
2897 g_assert (value == NULL);
2899 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2900 "*valid GVariant format string as a prefix*");
2901 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2902 "*valid_format_string*");
2903 value = g_variant_new_va ("z", &end, NULL);
2904 g_test_assert_expected_messages ();
2905 g_assert (value == NULL);
2907 value = g_variant_new ("y", 'a');
2908 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2909 "*type of 'q' but * has a type of 'y'*");
2910 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
2911 "*valid_format_string*");
2912 g_variant_get (value, "q");
2913 g_test_assert_expected_messages ();
2914 g_variant_unref (value);
2918 check_and_free (GVariant *value,
2921 gchar *valstr = g_variant_print (value, FALSE);
2922 g_assert_cmpstr (str, ==, valstr);
2923 g_variant_unref (value);
2928 test_varargs_empty_array (void)
2930 g_variant_new ("(a{s*})", NULL);
2932 g_assert_not_reached ();
2939 GVariantBuilder array;
2941 g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
2942 g_variant_builder_add_parsed (&array, "{'size', <(%i, %i)> }", 800, 600);
2943 g_variant_builder_add (&array, "{sv}", "title",
2944 g_variant_new_string ("Test case"));
2945 g_variant_builder_add_value (&array,
2946 g_variant_new_dict_entry (g_variant_new_string ("temperature"),
2947 g_variant_new_variant (
2948 g_variant_new_double (37.5))));
2949 check_and_free (g_variant_new ("(ma{sv}m(a{sv})ma{sv}ii)",
2950 NULL, FALSE, NULL, &array, 7777, 8888),
2951 "(nothing, nothing, {'size': <(800, 600)>, "
2952 "'title': <'Test case'>, "
2953 "'temperature': <37.5>}, "
2956 check_and_free (g_variant_new ("(imimimmimmimmi)",
2963 "(123, nothing, 123, nothing, just nothing, 123)");
2965 check_and_free (g_variant_new ("(ybnixd)",
2966 'a', 1, 22, 33, (guint64) 44, 5.5),
2967 "(0x61, true, 22, 33, 44, 5.5)");
2969 check_and_free (g_variant_new ("(@y?*rv)",
2970 g_variant_new ("y", 'a'),
2971 g_variant_new ("y", 'b'),
2972 g_variant_new ("y", 'c'),
2973 g_variant_new ("(y)", 'd'),
2974 g_variant_new ("y", 'e')),
2975 "(0x61, 0x62, 0x63, (0x64,), <byte 0x65>)");
2979 GVariantBuilder array;
2986 g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
2987 for (i = 0; i < 100; i++)
2989 number = g_strdup_printf ("%d", i);
2990 g_variant_builder_add (&array, "s", number);
2994 value = g_variant_builder_end (&array);
2995 g_variant_iter_init (&iter, value);
2998 while (g_variant_iter_loop (&iter, "s", &number))
3000 gchar *check = g_strdup_printf ("%d", i++);
3001 g_assert_cmpstr (number, ==, check);
3004 g_assert (number == NULL);
3005 g_assert (i == 100);
3007 g_variant_unref (value);
3009 g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
3010 for (i = 0; i < 100; i++)
3011 g_variant_builder_add (&array, "mi", i % 2 == 0, i);
3012 value = g_variant_builder_end (&array);
3015 g_variant_iter_init (&iter, value);
3016 while (g_variant_iter_loop (&iter, "mi", NULL, &val))
3017 g_assert (val == i++ || val == 0);
3018 g_assert (i == 100);
3021 g_variant_iter_init (&iter, value);
3022 while (g_variant_iter_loop (&iter, "mi", &just, &val))
3029 g_assert (val == this);
3034 g_assert (val == 0);
3037 g_assert (i == 100);
3039 g_variant_unref (value);
3043 const gchar *strvector[] = {"/hello", "/world", NULL};
3044 const gchar *test_strs[] = {"/foo", "/bar", "/baz" };
3045 GVariantBuilder builder;
3046 GVariantIter *array;
3054 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
3055 g_variant_builder_add (&builder, "s", "/foo");
3056 g_variant_builder_add (&builder, "s", "/bar");
3057 g_variant_builder_add (&builder, "s", "/baz");
3058 value = g_variant_new("(as^as^a&s)", &builder, strvector, strvector);
3059 g_variant_iter_init (&tuple, value);
3060 g_variant_iter_next (&tuple, "as", &array);
3063 while (g_variant_iter_loop (array, "s", &str))
3064 g_assert_cmpstr (str, ==, test_strs[i++]);
3067 g_variant_iter_free (array);
3070 g_variant_iter_init (&tuple, value);
3071 g_variant_iter_next (&tuple, "as", &array);
3074 while (g_variant_iter_loop (array, "&s", &str))
3075 g_assert_cmpstr (str, ==, test_strs[i++]);
3078 g_variant_iter_free (array);
3080 g_variant_iter_next (&tuple, "^a&s", &strv);
3081 g_variant_iter_next (&tuple, "^as", &my_strv);
3083 g_assert_cmpstr (strv[0], ==, "/hello");
3084 g_assert_cmpstr (strv[1], ==, "/world");
3085 g_assert (strv[2] == NULL);
3086 g_assert_cmpstr (my_strv[0], ==, "/hello");
3087 g_assert_cmpstr (my_strv[1], ==, "/world");
3088 g_assert (my_strv[2] == NULL);
3090 g_variant_unref (value);
3091 g_strfreev (my_strv);
3096 const gchar *strvector[] = {"/hello", "/world", NULL};
3097 const gchar *test_strs[] = {"/foo", "/bar", "/baz" };
3098 GVariantBuilder builder;
3099 GVariantIter *array;
3107 g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
3108 g_variant_builder_add (&builder, "o", "/foo");
3109 g_variant_builder_add (&builder, "o", "/bar");
3110 g_variant_builder_add (&builder, "o", "/baz");
3111 value = g_variant_new("(ao^ao^a&o)", &builder, strvector, strvector);
3112 g_variant_iter_init (&tuple, value);
3113 g_variant_iter_next (&tuple, "ao", &array);
3116 while (g_variant_iter_loop (array, "o", &str))
3117 g_assert_cmpstr (str, ==, test_strs[i++]);
3120 g_variant_iter_free (array);
3123 g_variant_iter_init (&tuple, value);
3124 g_variant_iter_next (&tuple, "ao", &array);
3127 while (g_variant_iter_loop (array, "&o", &str))
3128 g_assert_cmpstr (str, ==, test_strs[i++]);
3131 g_variant_iter_free (array);
3133 g_variant_iter_next (&tuple, "^a&o", &strv);
3134 g_variant_iter_next (&tuple, "^ao", &my_strv);
3136 g_assert_cmpstr (strv[0], ==, "/hello");
3137 g_assert_cmpstr (strv[1], ==, "/world");
3138 g_assert (strv[2] == NULL);
3139 g_assert_cmpstr (my_strv[0], ==, "/hello");
3140 g_assert_cmpstr (my_strv[1], ==, "/world");
3141 g_assert (my_strv[2] == NULL);
3143 g_variant_unref (value);
3144 g_strfreev (my_strv);
3149 const gchar *strvector[] = { "i", "ii", "iii", "iv", "v", "vi", NULL };
3150 GVariantBuilder builder;
3159 g_variant_builder_init (&builder, G_VARIANT_TYPE ("aas"));
3160 g_variant_builder_open (&builder, G_VARIANT_TYPE ("as"));
3161 for (i = 0; i < 6; i++)
3163 g_variant_builder_add (&builder, "s", strvector[i]);
3165 g_variant_builder_add (&builder, "&s", strvector[i]);
3166 g_variant_builder_close (&builder);
3167 g_variant_builder_add (&builder, "^as", strvector);
3168 g_variant_builder_add (&builder, "^as", strvector);
3169 value = g_variant_new ("aas", &builder);
3171 g_variant_iter_init (&iter, value);
3172 while (g_variant_iter_loop (&iter, "^as", &strv))
3173 for (i = 0; i < 6; i++)
3174 g_assert_cmpstr (strv[i], ==, strvector[i]);
3176 g_variant_iter_init (&iter, value);
3177 while (g_variant_iter_loop (&iter, "^a&s", &strv))
3178 for (i = 0; i < 6; i++)
3179 g_assert_cmpstr (strv[i], ==, strvector[i]);
3181 g_variant_iter_init (&iter, value);
3182 while (g_variant_iter_loop (&iter, "as", &i2))
3187 while (g_variant_iter_loop (i2, "s", &str))
3188 g_assert_cmpstr (str, ==, strvector[i++]);
3192 g_variant_iter_init (&iter, value);
3193 i3 = g_variant_iter_copy (&iter);
3194 while (g_variant_iter_loop (&iter, "@as", &sub))
3196 gchar *str = g_variant_print (sub, TRUE);
3197 g_assert_cmpstr (str, ==,
3198 "['i', 'ii', 'iii', 'iv', 'v', 'vi']");
3202 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
3203 "*NULL has already been returned*");
3204 g_variant_iter_next_value (&iter);
3205 g_test_assert_expected_messages ();
3207 while (g_variant_iter_loop (i3, "*", &sub))
3209 gchar *str = g_variant_print (sub, TRUE);
3210 g_assert_cmpstr (str, ==,
3211 "['i', 'ii', 'iii', 'iv', 'v', 'vi']");
3215 g_variant_iter_free (i3);
3217 for (i = 0; i < g_variant_n_children (value); i++)
3221 g_variant_get_child (value, i, "*", &sub);
3223 for (j = 0; j < g_variant_n_children (sub); j++)
3225 const gchar *str = NULL;
3228 g_variant_get_child (sub, j, "&s", &str);
3229 g_assert_cmpstr (str, ==, strvector[j]);
3231 cval = g_variant_get_child_value (sub, j);
3232 g_variant_get (cval, "&s", &str);
3233 g_assert_cmpstr (str, ==, strvector[j]);
3234 g_variant_unref (cval);
3237 g_variant_unref (sub);
3240 g_variant_unref (value);
3259 /* test all 'nothing' */
3260 value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
3263 FALSE, (gint16) 123,
3264 FALSE, (guint16) 123,
3265 FALSE, (gint32) 123,
3266 FALSE, (guint32) 123,
3267 FALSE, (gint64) 123,
3268 FALSE, (guint64) 123,
3270 FALSE, (gdouble) 37.5,
3274 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3288 memset (justs, 1, sizeof justs);
3289 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3301 g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
3302 justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
3305 memset (justs, 1, sizeof justs);
3306 byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3310 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3311 &justs[0], &byteval,
3322 g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
3323 justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
3324 g_assert (byteval == '\0' && bval == FALSE);
3325 g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
3326 u32val == 0 && i64val == 0 && u64val == 0 &&
3327 hval == 0 && dval == 0.0);
3328 g_assert (vval == NULL);
3331 byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3335 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3347 g_assert (byteval == '\0' && bval == FALSE);
3348 g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
3349 u32val == 0 && i64val == 0 && u64val == 0 &&
3350 hval == 0 && dval == 0.0);
3351 g_assert (vval == NULL);
3353 g_variant_unref (value);
3356 /* test all 'just' */
3357 value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
3361 TRUE, (guint16) 123,
3363 TRUE, (guint32) 123,
3365 TRUE, (guint64) 123,
3367 TRUE, (gdouble) 37.5,
3368 g_variant_new ("()"));
3371 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3385 memset (justs, 0, sizeof justs);
3386 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3398 g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
3399 justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
3402 memset (justs, 0, sizeof justs);
3403 byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3407 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3408 &justs[0], &byteval,
3419 g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
3420 justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
3421 g_assert (byteval == 'a' && bval == TRUE);
3422 g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
3423 u32val == 123 && i64val == 123 && u64val == 123 &&
3424 hval == -1 && dval == 37.5);
3425 g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
3426 g_variant_unref (vval);
3429 byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3433 g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3445 g_assert (byteval == 'a' && bval == TRUE);
3446 g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
3447 u32val == 123 && i64val == 123 && u64val == 123 &&
3448 hval == -1 && dval == 37.5);
3449 g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
3450 g_variant_unref (vval);
3452 g_variant_unref (value);
3459 value = g_variant_new ("(masas)", NULL, NULL);
3460 g_variant_ref_sink (value);
3462 str = g_variant_print (value, TRUE);
3463 g_assert_cmpstr (str, ==, "(@mas nothing, @as [])");
3464 g_variant_unref (value);
3467 do_failed_test ("/gvariant/varargs/subprocess/empty-array",
3468 "*which type of empty array*");
3471 g_variant_type_info_assert_no_infos ();
3475 hash_get (GVariant *value,
3476 const gchar *format,
3479 const gchar *endptr = NULL;
3483 hash = g_str_has_suffix (format, "#");
3485 va_start (ap, format);
3486 g_variant_get_va (value, format, hash ? &endptr : NULL, &ap);
3490 g_assert (*endptr == '#');
3494 hash_new (const gchar *format,
3497 const gchar *endptr = NULL;
3502 hash = g_str_has_suffix (format, "#");
3504 va_start (ap, format);
3505 value = g_variant_new_va (format, hash ? &endptr : NULL, &ap);
3509 g_assert (*endptr == '#');
3521 value = hash_new ("i", 234);
3522 hash_get (value, "i", &x);
3523 g_assert (x == 234);
3524 g_variant_unref (value);
3527 value = hash_new ("i#", 234);
3528 hash_get (value, "i#", &x);
3529 g_assert (x == 234);
3530 g_variant_unref (value);
3532 g_variant_type_info_assert_no_infos ();
3536 test_builder_memory (void)
3538 GVariantBuilder *hb;
3541 hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3542 g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3543 g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3544 g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3545 g_variant_builder_add (hb, "s", "some value");
3546 g_variant_builder_ref (hb);
3547 g_variant_builder_unref (hb);
3548 g_variant_builder_unref (hb);
3550 hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3551 g_variant_builder_unref (hb);
3553 hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3554 g_variant_builder_clear (hb);
3555 g_variant_builder_unref (hb);
3557 g_variant_builder_init (&sb, G_VARIANT_TYPE_ARRAY);
3558 g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
3559 g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
3560 g_variant_builder_add (&sb, "s", "some value");
3561 g_variant_builder_clear (&sb);
3563 g_variant_type_info_assert_no_infos ();
3569 GVariant *items[4096];
3573 table = g_hash_table_new_full (g_variant_hash, g_variant_equal,
3574 (GDestroyNotify ) g_variant_unref,
3577 for (i = 0; i < G_N_ELEMENTS (items); i++)
3583 tree = tree_instance_new (NULL, 0);
3584 items[i] = tree_instance_get_gvariant (tree);
3585 tree_instance_free (tree);
3587 for (j = 0; j < i; j++)
3588 if (g_variant_equal (items[i], items[j]))
3590 g_variant_unref (items[i]);
3594 g_hash_table_insert (table,
3595 g_variant_ref_sink (items[i]),
3596 GINT_TO_POINTER (i));
3599 for (i = 0; i < G_N_ELEMENTS (items); i++)
3603 result = g_hash_table_lookup (table, items[i]);
3604 g_assert_cmpint (GPOINTER_TO_INT (result), ==, i);
3607 g_hash_table_unref (table);
3609 g_variant_type_info_assert_no_infos ();
3613 test_gv_byteswap (void)
3615 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
3616 # define native16(x) x, 0
3617 # define swapped16(x) 0, x
3619 # define native16(x) 0, x
3620 # define swapped16(x) x, 0
3622 /* all kinds of of crazy randomised testing already performed on the
3623 * byteswapper in the /gvariant/serialiser/byteswap test and all kinds
3624 * of crazy randomised testing performed against the serialiser
3625 * normalisation functions in the /gvariant/serialiser/fuzz/ tests.
3627 * just test a few simple cases here to make sure they each work
3629 guchar validbytes[] = { 'a', '\0', swapped16(66), 2,
3631 'b', '\0', swapped16(77), 2,
3633 guchar corruptbytes[] = { 'a', '\0', swapped16(66), 2,
3635 'b', '\0', swapped16(77), 2,
3637 guint valid_data[4], corrupt_data[4];
3638 GVariant *value, *swapped;
3639 gchar *string, *string2;
3641 memcpy (valid_data, validbytes, sizeof validbytes);
3642 memcpy (corrupt_data, corruptbytes, sizeof corruptbytes);
3645 value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"),
3646 valid_data, sizeof validbytes, TRUE,
3648 swapped = g_variant_byteswap (value);
3649 g_variant_unref (value);
3650 g_assert (g_variant_get_size (swapped) == 13);
3651 string = g_variant_print (swapped, FALSE);
3652 g_variant_unref (swapped);
3653 g_assert_cmpstr (string, ==, "[('a', 66), ('b', 77)]");
3656 /* untrusted but valid */
3657 value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"),
3658 valid_data, sizeof validbytes, FALSE,
3660 swapped = g_variant_byteswap (value);
3661 g_variant_unref (value);
3662 g_assert (g_variant_get_size (swapped) == 13);
3663 string = g_variant_print (swapped, FALSE);
3664 g_variant_unref (swapped);
3665 g_assert_cmpstr (string, ==, "[('a', 66), ('b', 77)]");
3668 /* untrusted, invalid */
3669 value = g_variant_new_from_data (G_VARIANT_TYPE ("a(sn)"),
3670 corrupt_data, sizeof corruptbytes, FALSE,
3672 string = g_variant_print (value, FALSE);
3673 swapped = g_variant_byteswap (value);
3674 g_variant_unref (value);
3675 g_assert (g_variant_get_size (swapped) == 13);
3676 value = g_variant_byteswap (swapped);
3677 g_variant_unref (swapped);
3678 string2 = g_variant_print (value, FALSE);
3679 g_assert (g_variant_get_size (value) == 13);
3680 g_variant_unref (value);
3681 g_assert_cmpstr (string, ==, string2);
3689 GError *error = NULL;
3696 tree = tree_instance_new (NULL, 3);
3697 value = tree_instance_get_gvariant (tree);
3698 tree_instance_free (tree);
3700 pt = g_variant_print (value, TRUE);
3701 p = g_variant_print (value, FALSE);
3703 parsed = g_variant_parse (NULL, pt, NULL, NULL, &error);
3704 g_assert_no_error (error);
3705 res = g_variant_print (parsed, FALSE);
3706 if (!strstr (pt, "float")) /* FIXME: need reliable round-trip for floats */
3707 g_assert_cmpstr (p, ==, res);
3708 g_variant_unref (parsed);
3711 parsed = g_variant_parse (g_variant_get_type (value), p, NULL, NULL, &error);
3712 g_assert_no_error (error);
3713 res = g_variant_print (parsed, TRUE);
3714 if (!strstr (pt, "float")) /* FIXME: need reliable round-trip for floats */
3715 g_assert_cmpstr (pt, ==, res);
3716 g_variant_unref (parsed);
3719 g_variant_unref (value);
3729 for (i = 0; i < 100; i++)
3736 GError *error = NULL;
3741 for (i = 0; i < 127; i++)
3745 val = g_variant_new_string (str);
3746 p = g_variant_print (val, FALSE);
3747 g_variant_unref (val);
3749 val = g_variant_parse (NULL, p, NULL, NULL, &error);
3750 p2 = g_variant_print (val, FALSE);
3752 g_assert_cmpstr (str, ==, g_variant_get_string (val, NULL));
3753 g_assert_cmpstr (p, ==, p2);
3755 g_variant_unref (val);
3760 /* another mini test */
3765 value = g_variant_parse (G_VARIANT_TYPE_INT32, "1 2 3", NULL, &end, NULL);
3766 g_assert_cmpint (g_variant_get_int32 (value), ==, 1);
3767 /* make sure endptr returning works */
3768 g_assert_cmpstr (end, ==, " 2 3");
3769 g_variant_unref (value);
3772 /* unicode mini test */
3775 const gchar orig[] = "a\xc5\x82\xf0\x9d\x84\x9e \t\n";
3779 value = g_variant_new_string (orig);
3780 printed = g_variant_print (value, FALSE);
3781 g_variant_unref (value);
3783 g_assert_cmpstr (printed, ==, "'a\xc5\x82\xf0\x9d\x84\x9e \\t\\n'");
3784 value = g_variant_parse (NULL, printed, NULL, NULL, NULL);
3785 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, orig);
3786 g_variant_unref (value);
3792 const gchar orig[] = " \342\200\254 \360\220\210\240 \a \b \f \n \r \t \v ";
3796 value = g_variant_new_string (orig);
3797 printed = g_variant_print (value, FALSE);
3798 g_variant_unref (value);
3800 g_assert_cmpstr (printed, ==, "' \\u202c \\U00010220 \\a \\b \\f \\n \\r \\t \\v '");
3801 value = g_variant_parse (NULL, printed, NULL, NULL, NULL);
3802 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, orig);
3803 g_variant_unref (value);
3808 /* inf/nan strings are C99 features which Visual C++ does not support */
3809 /* inf/nan mini test */
3811 const gchar *tests[] = { "inf", "-inf", "nan" };
3814 gchar *printed_down;
3817 for (i = 0; i < G_N_ELEMENTS (tests); i++)
3819 GError *error = NULL;
3820 value = g_variant_parse (NULL, tests[i], NULL, NULL, &error);
3821 printed = g_variant_print (value, FALSE);
3822 /* Canonicalize to lowercase; https://bugzilla.gnome.org/show_bug.cgi?id=704585 */
3823 printed_down = g_ascii_strdown (printed, -1);
3824 g_assert (g_str_has_prefix (printed_down, tests[i]));
3826 g_free (printed_down);
3827 g_variant_unref (value);
3832 g_variant_type_info_assert_no_infos ();
3836 test_parse_failures (void)
3838 const gchar *test[] = {
3839 "[1, 2,", "6:", "expected value",
3840 "", "0:", "expected value",
3841 "(1, 2,", "6:", "expected value",
3842 "<1", "2:", "expected '>'",
3843 "[]", "0-2:", "unable to infer",
3844 "(,", "1:", "expected value",
3845 "[4,'']", "1-2,3-5:", "common type",
3846 "[4, '', 5]", "1-2,4-6:", "common type",
3847 "['', 4, 5]", "1-3,5-6:", "common type",
3848 "[4, 5, '']", "1-2,7-9:", "common type",
3849 "[[4], [], ['']]", "1-4,10-14:", "common type",
3850 "[[], [4], ['']]", "5-8,10-14:", "common type",
3851 "just", "4:", "expected value",
3852 "nothing", "0-7:", "unable to infer",
3853 "just [4, '']", "6-7,9-11:", "common type",
3854 "[[4,'']]", "2-3,4-6:", "common type",
3855 "([4,''],)", "2-3,4-6:", "common type",
3857 "{}", "0-2:", "unable to infer",
3858 "{[1,2],[3,4]}", "0-13:", "basic types",
3859 "{[1,2]:[3,4]}", "0-13:", "basic types",
3860 "justt", "0-5:", "unknown keyword",
3861 "nothng", "0-6:", "unknown keyword",
3862 "uint33", "0-6:", "unknown keyword",
3863 "@mi just ''", "9-11:", "can not parse as",
3864 "@ai ['']", "5-7:", "can not parse as",
3865 "@(i) ('',)", "6-8:", "can not parse as",
3866 "[[], 5]", "1-3,5-6:", "common type",
3867 "[[5], 5]", "1-4,6-7:", "common type",
3868 "5 5", "2:", "expected end of input",
3869 "[5, [5, '']]", "5-6,8-10:", "common type",
3870 "@i just 5", "3-9:", "can not parse as",
3871 "@i nothing", "3-10:", "can not parse as",
3872 "@i []", "3-5:", "can not parse as",
3873 "@i ()", "3-5:", "can not parse as",
3874 "@ai (4,)", "4-8:", "can not parse as",
3875 "@(i) []", "5-7:", "can not parse as",
3876 "(5 5)", "3:", "expected ','",
3877 "[5 5]", "3:", "expected ',' or ']'",
3878 "(5, 5 5)", "6:", "expected ',' or ')'",
3879 "[5, 5 5]", "6:", "expected ',' or ']'",
3880 "<@i []>", "4-6:", "can not parse as",
3881 "<[5 5]>", "4:", "expected ',' or ']'",
3882 "{[4,''],5}", "2-3,4-6:", "common type",
3883 "{5,[4,'']}", "4-5,6-8:", "common type",
3884 "@i {1,2}", "3-8:", "can not parse as",
3885 "{@i '', 5}", "4-6:", "can not parse as",
3886 "{5, @i ''}", "7-9:", "can not parse as",
3887 "@ai {}", "4-6:", "can not parse as",
3888 "{@i '': 5}", "4-6:", "can not parse as",
3889 "{5: @i ''}", "7-9:", "can not parse as",
3890 "{<4,5}", "3:", "expected '>'",
3891 "{4,<5}", "5:", "expected '>'",
3892 "{4,5,6}", "4:", "expected '}'",
3893 "{5 5}", "3:", "expected ':' or ','",
3894 "{4: 5: 6}", "5:", "expected ',' or '}'",
3895 "{4:5,<6:7}", "7:", "expected '>'",
3896 "{4:5,6:<7}", "9:", "expected '>'",
3897 "{4:5,6 7}", "7:", "expected ':'",
3898 "@o 'foo'", "3-8:", "object path",
3899 "@g 'zzz'", "3-8:", "signature",
3900 "@i true", "3-7:", "can not parse as",
3901 "@z 4", "0-2:", "invalid type",
3902 "@a* []", "0-3:", "definite",
3903 "@ai [3 3]", "7:", "expected ',' or ']'",
3904 "18446744073709551616", "0-20:", "too big for any type",
3905 "-18446744073709551616", "0-21:", "too big for any type",
3906 "byte 256", "5-8:", "out of range for type",
3907 "byte -1", "5-7:", "out of range for type",
3908 "int16 32768", "6-11:", "out of range for type",
3909 "int16 -32769", "6-12:", "out of range for type",
3910 "uint16 -1", "7-9:", "out of range for type",
3911 "uint16 65536", "7-12:", "out of range for type",
3912 "2147483648", "0-10:", "out of range for type",
3913 "-2147483649", "0-11:", "out of range for type",
3914 "uint32 -1", "7-9:", "out of range for type",
3915 "uint32 4294967296", "7-17:", "out of range for type",
3916 "@x 9223372036854775808", "3-22:", "out of range for type",
3917 "@x -9223372036854775809", "3-23:", "out of range for type",
3918 "@t -1", "3-5:", "out of range for type",
3919 "@t 18446744073709551616", "3-23:", "too big for any type",
3920 "handle 2147483648", "7-17:", "out of range for type",
3921 "handle -2147483649", "7-18:", "out of range for type",
3922 "1.798e308", "0-9:", "too big for any type",
3923 "37.5a488", "4-5:", "invalid character",
3924 "0x7ffgf", "5-6:", "invalid character",
3925 "07758", "4-5:", "invalid character",
3926 "123a5", "3-4:", "invalid character",
3927 "@ai 123", "4-7:", "can not parse as",
3928 "'\"\\'", "0-4:", "unterminated string",
3929 "'\"\\'\\", "0-5:", "unterminated string",
3930 "boolean 4", "8-9:", "can not parse as",
3931 "int32 true", "6-10:", "can not parse as",
3932 "[double 5, int32 5]", "1-9,11-18:", "common type",
3933 "string 4", "7-8:", "can not parse as"
3937 for (i = 0; i < G_N_ELEMENTS (test); i += 3)
3939 GError *error = NULL;
3942 value = g_variant_parse (NULL, test[i], NULL, NULL, &error);
3943 g_assert (value == NULL);
3945 if (!strstr (error->message, test[i+2]))
3946 g_error ("test %d: Can't find '%s' in '%s'", i / 3,
3947 test[i+2], error->message);
3949 if (!g_str_has_prefix (error->message, test[i+1]))
3950 g_error ("test %d: Expected location '%s' in '%s'", i / 3,
3951 test[i+1], error->message);
3953 g_error_free (error);
3958 test_parse_bad_format_char (void)
3960 g_variant_new_parsed ("%z");
3962 g_assert_not_reached ();
3966 test_parse_bad_format_string (void)
3968 g_variant_new_parsed ("uint32 %i", 2);
3970 g_assert_not_reached ();
3974 test_parse_bad_args (void)
3976 g_variant_new_parsed ("%@i", g_variant_new_uint32 (2));
3978 g_assert_not_reached ();
3982 test_parse_positional (void)
3985 check_and_free (g_variant_new_parsed ("[('one', 1), (%s, 2),"
3986 " ('three', %i)]", "two", 3),
3987 "[('one', 1), ('two', 2), ('three', 3)]");
3988 value = g_variant_new_parsed ("[('one', 1), (%s, 2),"
3989 " ('three', %u)]", "two", 3);
3990 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(su)")));
3991 check_and_free (value, "[('one', 1), ('two', 2), ('three', 3)]");
3992 check_and_free (g_variant_new_parsed ("{%s:%i}", "one", 1), "{'one': 1}");
3994 if (g_test_undefined ())
3996 do_failed_test ("/gvariant/parse/subprocess/bad-format-char",
3997 "*GVariant format string*");
3999 do_failed_test ("/gvariant/parse/subprocess/bad-format-string",
4000 "*can not parse as*");
4002 do_failed_test ("/gvariant/parse/subprocess/bad-args",
4003 "*expected GVariant of type 'i'*");
4008 test_floating (void)
4012 value = g_variant_new_int32 (42);
4013 g_assert (g_variant_is_floating (value));
4014 g_variant_ref_sink (value);
4015 g_assert (!g_variant_is_floating (value));
4016 g_variant_unref (value);
4020 test_bytestring (void)
4022 const gchar *test_string = "foo,bar,baz,quux,\xffoooo";
4026 const gchar *const_str;
4027 GVariant *untrusted_empty;
4029 strv = g_strsplit (test_string, ",", 0);
4031 value = g_variant_new_bytestring_array ((const gchar **) strv, -1);
4032 g_assert (g_variant_is_floating (value));
4035 str = g_variant_print (value, FALSE);
4036 g_variant_unref (value);
4038 value = g_variant_parse (NULL, str, NULL, NULL, NULL);
4041 strv = g_variant_dup_bytestring_array (value, NULL);
4042 g_variant_unref (value);
4044 str = g_strjoinv (",", strv);
4047 g_assert_cmpstr (str, ==, test_string);
4050 strv = g_strsplit (test_string, ",", 0);
4051 value = g_variant_new ("(^aay^a&ay^ay^&ay)",
4052 strv, strv, strv[0], strv[0]);
4055 g_variant_get_child (value, 0, "^a&ay", &strv);
4056 str = g_strjoinv (",", strv);
4058 g_assert_cmpstr (str, ==, test_string);
4061 g_variant_get_child (value, 0, "^aay", &strv);
4062 str = g_strjoinv (",", strv);
4064 g_assert_cmpstr (str, ==, test_string);
4067 g_variant_get_child (value, 1, "^a&ay", &strv);
4068 str = g_strjoinv (",", strv);
4070 g_assert_cmpstr (str, ==, test_string);
4073 g_variant_get_child (value, 1, "^aay", &strv);
4074 str = g_strjoinv (",", strv);
4076 g_assert_cmpstr (str, ==, test_string);
4079 g_variant_get_child (value, 2, "^ay", &str);
4080 g_assert_cmpstr (str, ==, "foo");
4083 g_variant_get_child (value, 2, "^&ay", &str);
4084 g_assert_cmpstr (str, ==, "foo");
4086 g_variant_get_child (value, 3, "^ay", &str);
4087 g_assert_cmpstr (str, ==, "foo");
4090 g_variant_get_child (value, 3, "^&ay", &str);
4091 g_assert_cmpstr (str, ==, "foo");
4092 g_variant_unref (value);
4094 untrusted_empty = g_variant_new_from_data (G_VARIANT_TYPE ("ay"), NULL, 0, FALSE, NULL, NULL);
4095 value = g_variant_get_normal_form (untrusted_empty);
4096 const_str = g_variant_get_bytestring (value);
4098 g_variant_unref (value);
4099 g_variant_unref (untrusted_empty);
4103 test_lookup_value (void)
4106 const gchar *dict, *key, *value;
4108 { "@a{ss} {'x': 'y'}", "x", "'y'" },
4109 { "@a{ss} {'x': 'y'}", "y" },
4110 { "@a{os} {'/x': 'y'}", "/x", "'y'" },
4111 { "@a{os} {'/x': 'y'}", "/y" },
4112 { "@a{sv} {'x': <'y'>}", "x", "'y'" },
4113 { "@a{sv} {'x': <5>}", "x", "5" },
4114 { "@a{sv} {'x': <'y'>}", "y" }
4118 for (i = 0; i < G_N_ELEMENTS (cases); i++)
4120 GVariant *dictionary;
4124 dictionary = g_variant_parse (NULL, cases[i].dict, NULL, NULL, NULL);
4125 value = g_variant_lookup_value (dictionary, cases[i].key, NULL);
4126 g_variant_unref (dictionary);
4128 if (value == NULL && cases[i].value == NULL)
4131 g_assert (value && cases[i].value);
4132 p = g_variant_print (value, FALSE);
4133 g_assert_cmpstr (cases[i].value, ==, p);
4134 g_variant_unref (value);
4147 dict = g_variant_parse (NULL,
4148 "{'a': <5>, 'b': <'c'>}",
4151 ok = g_variant_lookup (dict, "a", "i", &num);
4153 g_assert_cmpint (num, ==, 5);
4155 ok = g_variant_lookup (dict, "a", "&s", &str);
4158 ok = g_variant_lookup (dict, "q", "&s", &str);
4161 ok = g_variant_lookup (dict, "b", "i", &num);
4164 ok = g_variant_lookup (dict, "b", "&s", &str);
4166 g_assert_cmpstr (str, ==, "c");
4168 ok = g_variant_lookup (dict, "q", "&s", &str);
4171 g_variant_unref (dict);
4175 untrusted (GVariant *a)
4178 const GVariantType *type;
4181 type = g_variant_get_type (a);
4182 bytes = g_variant_get_data_as_bytes (a);
4183 b = g_variant_new_from_bytes (type, bytes, FALSE);
4184 g_bytes_unref (bytes);
4185 g_variant_unref (a);
4196 a = untrusted (g_variant_new_byte (5));
4197 b = g_variant_new_byte (6);
4198 g_assert (g_variant_compare (a, b) < 0);
4199 g_variant_unref (a);
4200 g_variant_unref (b);
4201 a = untrusted (g_variant_new_int16 (G_MININT16));
4202 b = g_variant_new_int16 (G_MAXINT16);
4203 g_assert (g_variant_compare (a, b) < 0);
4204 g_variant_unref (a);
4205 g_variant_unref (b);
4206 a = untrusted (g_variant_new_uint16 (0));
4207 b = g_variant_new_uint16 (G_MAXUINT16);
4208 g_assert (g_variant_compare (a, b) < 0);
4209 g_variant_unref (a);
4210 g_variant_unref (b);
4211 a = untrusted (g_variant_new_int32 (G_MININT32));
4212 b = g_variant_new_int32 (G_MAXINT32);
4213 g_assert (g_variant_compare (a, b) < 0);
4214 g_variant_unref (a);
4215 g_variant_unref (b);
4216 a = untrusted (g_variant_new_uint32 (0));
4217 b = g_variant_new_uint32 (G_MAXUINT32);
4218 g_assert (g_variant_compare (a, b) < 0);
4219 g_variant_unref (a);
4220 g_variant_unref (b);
4221 a = untrusted (g_variant_new_int64 (G_MININT64));
4222 b = g_variant_new_int64 (G_MAXINT64);
4223 g_assert (g_variant_compare (a, b) < 0);
4224 g_variant_unref (a);
4225 g_variant_unref (b);
4226 a = untrusted (g_variant_new_uint64 (0));
4227 b = g_variant_new_uint64 (G_MAXUINT64);
4228 g_assert (g_variant_compare (a, b) < 0);
4229 g_variant_unref (a);
4230 g_variant_unref (b);
4231 a = untrusted (g_variant_new_double (G_MINDOUBLE));
4232 b = g_variant_new_double (G_MAXDOUBLE);
4233 g_assert (g_variant_compare (a, b) < 0);
4234 g_variant_unref (a);
4235 g_variant_unref (b);
4236 a = untrusted (g_variant_new_string ("abc"));
4237 b = g_variant_new_string ("abd");
4238 g_assert (g_variant_compare (a, b) < 0);
4239 g_variant_unref (a);
4240 g_variant_unref (b);
4241 a = untrusted (g_variant_new_object_path ("/abc"));
4242 b = g_variant_new_object_path ("/abd");
4243 g_assert (g_variant_compare (a, b) < 0);
4244 g_variant_unref (a);
4245 g_variant_unref (b);
4246 a = untrusted (g_variant_new_signature ("g"));
4247 b = g_variant_new_signature ("o");
4248 g_assert (g_variant_compare (a, b) < 0);
4249 g_variant_unref (a);
4250 g_variant_unref (b);
4251 a = untrusted (g_variant_new_boolean (FALSE));
4252 b = g_variant_new_boolean (TRUE);
4253 g_assert (g_variant_compare (a, b) < 0);
4254 g_variant_unref (a);
4255 g_variant_unref (b);
4264 a = untrusted (g_variant_new_byte (5));
4265 b = g_variant_get_normal_form (a);
4266 g_assert (g_variant_equal (a, b));
4267 g_variant_unref (a);
4268 g_variant_unref (b);
4269 a = untrusted (g_variant_new_int16 (G_MININT16));
4270 b = g_variant_get_normal_form (a);
4271 g_assert (g_variant_equal (a, b));
4272 g_variant_unref (a);
4273 g_variant_unref (b);
4274 a = untrusted (g_variant_new_uint16 (0));
4275 b = g_variant_get_normal_form (a);
4276 g_assert (g_variant_equal (a, b));
4277 g_variant_unref (a);
4278 g_variant_unref (b);
4279 a = untrusted (g_variant_new_int32 (G_MININT32));
4280 b = g_variant_get_normal_form (a);
4281 g_assert (g_variant_equal (a, b));
4282 g_variant_unref (a);
4283 g_variant_unref (b);
4284 a = untrusted (g_variant_new_uint32 (0));
4285 b = g_variant_get_normal_form (a);
4286 g_assert (g_variant_equal (a, b));
4287 g_variant_unref (a);
4288 g_variant_unref (b);
4289 a = untrusted (g_variant_new_int64 (G_MININT64));
4290 b = g_variant_get_normal_form (a);
4291 g_assert (g_variant_equal (a, b));
4292 g_variant_unref (a);
4293 g_variant_unref (b);
4294 a = untrusted (g_variant_new_uint64 (0));
4295 b = g_variant_get_normal_form (a);
4296 g_assert (g_variant_equal (a, b));
4297 g_variant_unref (a);
4298 g_variant_unref (b);
4299 a = untrusted (g_variant_new_double (G_MINDOUBLE));
4300 b = g_variant_get_normal_form (a);
4301 g_assert (g_variant_equal (a, b));
4302 g_variant_unref (a);
4303 g_variant_unref (b);
4304 a = untrusted (g_variant_new_string ("abc"));
4305 g_assert (g_variant_equal (a, a));
4306 b = g_variant_get_normal_form (a);
4307 g_assert (g_variant_equal (a, b));
4308 g_variant_unref (a);
4309 g_variant_unref (b);
4310 a = untrusted (g_variant_new_object_path ("/abc"));
4311 g_assert (g_variant_equal (a, a));
4312 b = g_variant_get_normal_form (a);
4314 g_assert (g_variant_equal (a, b));
4315 g_variant_unref (a);
4316 g_variant_unref (b);
4317 a = untrusted (g_variant_new_signature ("g"));
4318 g_assert (g_variant_equal (a, a));
4319 b = g_variant_get_normal_form (a);
4321 g_assert (g_variant_equal (a, b));
4322 g_variant_unref (a);
4323 g_variant_unref (b);
4324 a = untrusted (g_variant_new_boolean (FALSE));
4325 b = g_variant_get_normal_form (a);
4326 g_assert (g_variant_equal (a, b));
4327 g_variant_unref (a);
4328 g_variant_unref (b);
4332 test_fixed_array (void)
4341 a = g_variant_new_parsed ("[1,2,3,4,5]");
4342 elts = g_variant_get_fixed_array (a, &n_elts, sizeof (gint32));
4343 g_assert (n_elts == 5);
4344 for (i = 0; i < 5; i++)
4345 g_assert_cmpint (elts[i], ==, i + 1);
4346 g_variant_unref (a);
4349 for (i = 0; i < 5; i++)
4351 a = g_variant_new_fixed_array (G_VARIANT_TYPE_INT32, values,
4352 G_N_ELEMENTS (values), sizeof (values[0]));
4353 g_assert_cmpstr (g_variant_get_type_string (a), ==, "ai");
4354 elts = g_variant_get_fixed_array (a, &n_elts, sizeof (gint32));
4355 g_assert (n_elts == 5);
4356 for (i = 0; i < 5; i++)
4357 g_assert_cmpint (elts[i], ==, i + 1);
4358 g_variant_unref (a);
4362 test_check_format_string (void)
4366 value = g_variant_new ("(sas)", "foo", NULL);
4367 g_variant_ref_sink (value);
4369 g_assert (g_variant_check_format_string (value, "(s*)", TRUE));
4370 g_assert (g_variant_check_format_string (value, "(s*)", FALSE));
4371 g_assert (!g_variant_check_format_string (value, "(u*)", TRUE));
4372 g_assert (!g_variant_check_format_string (value, "(u*)", FALSE));
4374 g_assert (g_variant_check_format_string (value, "(&s*)", FALSE));
4375 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*contains a '&' character*");
4376 g_assert (!g_variant_check_format_string (value, "(&s*)", TRUE));
4377 g_test_assert_expected_messages ();
4379 g_assert (g_variant_check_format_string (value, "(s^as)", TRUE));
4380 g_assert (g_variant_check_format_string (value, "(s^as)", FALSE));
4382 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*contains a '&' character*");
4383 g_assert (!g_variant_check_format_string (value, "(s^a&s)", TRUE));
4384 g_test_assert_expected_messages ();
4385 g_assert (g_variant_check_format_string (value, "(s^a&s)", FALSE));
4387 g_variant_unref (value);
4389 /* Do it again with a type that will let us put a '&' after a '^' */
4390 value = g_variant_new ("(say)", "foo", NULL);
4391 g_variant_ref_sink (value);
4393 g_assert (g_variant_check_format_string (value, "(s*)", TRUE));
4394 g_assert (g_variant_check_format_string (value, "(s*)", FALSE));
4395 g_assert (!g_variant_check_format_string (value, "(u*)", TRUE));
4396 g_assert (!g_variant_check_format_string (value, "(u*)", FALSE));
4398 g_assert (g_variant_check_format_string (value, "(&s*)", FALSE));
4399 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*contains a '&' character*");
4400 g_assert (!g_variant_check_format_string (value, "(&s*)", TRUE));
4401 g_test_assert_expected_messages ();
4403 g_assert (g_variant_check_format_string (value, "(s^ay)", TRUE));
4404 g_assert (g_variant_check_format_string (value, "(s^ay)", FALSE));
4406 g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL, "*contains a '&' character*");
4407 g_assert (!g_variant_check_format_string (value, "(s^&ay)", TRUE));
4408 g_test_assert_expected_messages ();
4409 g_assert (g_variant_check_format_string (value, "(s^&ay)", FALSE));
4411 g_assert (g_variant_check_format_string (value, "r", FALSE));
4412 g_assert (g_variant_check_format_string (value, "(?a?)", FALSE));
4414 g_variant_unref (value);
4418 verify_gvariant_checksum (const gchar *sha256,
4423 checksum = g_compute_checksum_for_data (G_CHECKSUM_SHA256,
4424 g_variant_get_data (v),
4425 g_variant_get_size (v));
4426 g_assert_cmpstr (sha256, ==, checksum);
4431 verify_gvariant_checksum_va (const gchar *sha256,
4438 va_start (args, fmt);
4440 v = g_variant_new_va (fmt, NULL, &args);
4441 g_variant_ref_sink (v);
4442 #if G_BYTE_ORDER == G_BIG_ENDIAN
4444 GVariant *byteswapped = g_variant_byteswap (v);
4445 g_variant_unref (v);
4452 verify_gvariant_checksum (sha256, v);
4454 g_variant_unref (v);
4458 test_checksum_basic (void)
4460 verify_gvariant_checksum_va ("e8a4b2ee7ede79a3afb332b5b6cc3d952a65fd8cffb897f5d18016577c33d7cc",
4462 verify_gvariant_checksum_va ("c53e363c33b00cfce298229ee83856b8a98c2e6126cab13f65899f62473b0df5",
4464 verify_gvariant_checksum_va ("2b4c342f5433ebe591a1da77e013d1b72475562d48578dca8b84bac6651c3cb9",
4466 verify_gvariant_checksum_va ("12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca",
4468 verify_gvariant_checksum_va ("e25a59b24440eb6c833aa79c93b9840e6eab6966add0dacf31df7e9e7000f5b3",
4470 verify_gvariant_checksum_va ("4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a",
4472 verify_gvariant_checksum_va ("ca2fd00fa001190744c15c317643ab092e7048ce086a243e2be9437c898de1bb",
4477 test_checksum_nested (void)
4479 static const char* const strv[] = {"foo", "bar", "baz", NULL};
4481 verify_gvariant_checksum_va ("31fbc92f08fddaca716188fe4b5d44ae122fc6306fd3c6925af53cfa47ea596d",
4483 verify_gvariant_checksum_va ("01759d683cead856d1d386d59af0578841698a424a265345ad5413122f220de8",
4484 "(su)", "moocow", 79);
4485 verify_gvariant_checksum_va ("52b3ae95f19b3e642ea1d01185aea14a09004c1d1712672644427403a8a0afe6",
4486 "(qyst)", G_MAXUINT16, 9, "moocow", G_MAXUINT64);
4487 verify_gvariant_checksum_va ("6fc6f4524161c3ae0d316812d7088e3fcd372023edaea2d7821093be40ae1060",
4488 "(@ay)", g_variant_new_bytestring ("\xFF\xFF\xFF"));
4489 verify_gvariant_checksum_va ("572aca386e1a983dd23bb6eb6e3dfa72eef9ca7c7744581aa800e18d7d9d0b0b",
4491 verify_gvariant_checksum_va ("4bddf6174c791bb44fc6a4106573031690064df34b741033a0122ed8dc05bcf3",
4492 "(yvu)", 254, g_variant_new ("(^as)", strv), 42);
4504 const guint8 values[5] = { 1, 2, 3, 4, 5 };
4510 bytes = g_bytes_new (&values, 5);
4511 a = g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes, TRUE);
4512 g_bytes_unref (bytes);
4514 elts = g_variant_get_fixed_array (a, &n_elts, sizeof (guint8));
4515 g_assert (n_elts == 5);
4516 for (i = 0; i < 5; i++)
4517 g_assert_cmpint (elts[i], ==, i + 1);
4519 bytes2 = g_variant_get_data_as_bytes (a);
4520 g_variant_unref (a);
4522 bytes = g_bytes_new (&values, 5);
4523 g_assert (g_bytes_equal (bytes, bytes2));
4524 g_bytes_unref (bytes);
4525 g_bytes_unref (bytes2);
4527 tuple = g_variant_new_parsed ("['foo', 'bar']");
4528 bytes = g_variant_get_data_as_bytes (tuple); /* force serialisation */
4529 a = g_variant_get_child_value (tuple, 1);
4530 bytes2 = g_variant_get_data_as_bytes (a);
4531 g_assert (!g_bytes_equal (bytes, bytes2));
4533 g_bytes_unref (bytes);
4534 g_bytes_unref (bytes2);
4535 g_variant_unref (a);
4536 g_variant_unref (tuple);
4538 /* Feed in some non-normal data... make sure it's aligned.
4540 * Here we have an array of three elements. The first and last are
4541 * normal ints ('iiii') and array-of-bytes data ('ayay'). The middle
4542 * element is zero-bytes wide, which will present a problem when
4543 * fetching the fixed-size integer out of it.
4545 tmp = g_strdup ("iiiiayayiiiisayay\x08\x08\x10");
4546 bytes = g_bytes_new_take (tmp, strlen (tmp));
4547 a = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(iay)"), bytes, FALSE);
4548 g_bytes_unref (bytes);
4550 /* The middle tuple is zero bytes */
4551 b = g_variant_get_child_value (a, 1);
4552 g_assert_cmpint (g_variant_get_size (b), ==, 0);
4554 /* But we're going to pull a 4-byte child out of it... */
4555 c = g_variant_get_child_value (b, 0);
4556 g_assert_cmpint (g_variant_get_size (c), ==, 4);
4558 /* g_variant_get_data() is allowed to fail in this case.
4559 * NB: if someone finds a way to avoid this then that's fine too...
4561 g_assert (g_variant_get_data (c) == NULL);
4563 /* but since it's four bytes, it ought to have data... */
4564 bytes = g_variant_get_data_as_bytes (c);
4565 g_assert_cmpint (g_bytes_get_size (bytes), ==, 4);
4566 g_assert (memcmp (g_bytes_get_data (bytes, NULL), "\0\0\0\0", 4) == 0);
4567 g_bytes_unref (bytes);
4569 g_variant_unref (c);
4570 g_variant_unref (b);
4571 g_variant_unref (a);
4575 const GVariantType *type;
4581 test_print_context (void)
4583 ContextTest tests[] = {
4584 { NULL, "(1, 2, 3, 'abc", " ^^^^" },
4585 { NULL, "[1, 2, 3, 'str']", " ^ ^^^^^" },
4586 { G_VARIANT_TYPE_UINT16, "{ 'abc':'def' }", " ^^^^^^^^^^^^^^^" },
4587 { NULL, "<5", " ^" },
4588 { NULL, "'ab\\ux'", " ^^^^^^^" },
4589 { NULL, "'ab\\U00efx'", " ^^^^^^^^^^^" }
4594 GError *error = NULL;
4596 for (i = 0; i < G_N_ELEMENTS (tests); i++)
4598 v = g_variant_parse (tests[i].type, tests[i].in, NULL, NULL, &error);
4600 s = g_variant_parse_error_print_context (error, tests[i].in);
4601 g_assert (strstr (s, tests[i].out) != NULL);
4603 g_clear_error (&error);
4608 test_error_quark (void)
4610 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
4611 g_assert (g_variant_parser_get_error_quark () == g_variant_parse_error_quark ());
4612 G_GNUC_END_IGNORE_DEPRECATIONS
4616 flatten_vectors (GVariantVectors *v)
4621 result = g_byte_array_new ();
4623 for (i = 0; i < v->vectors->len; i++)
4625 GVariantVector vec = g_array_index (v->vectors, GVariantVector, i);
4628 g_byte_array_append (result, vec.data.pointer, vec.size);
4630 g_byte_array_append (result, v->extra_bytes->data + vec.data.offset, vec.size);
4637 test_vector_serialiser (void)
4639 GVariantVectors vectors;
4640 GByteArray *flattened;
4644 for (i = 0; i < 100; i++)
4646 value = create_random_gvariant (4);
4648 GLIB_PRIVATE_CALL(g_variant_to_vectors) (value, &vectors);
4649 flattened = flatten_vectors (&vectors);
4650 g_byte_array_free (vectors.extra_bytes, TRUE);
4651 g_byte_array_free (vectors.offsets, TRUE);
4652 g_array_free (vectors.vectors, TRUE);
4655 if (flattened->len != g_variant_get_size (value) ||
4656 memcmp (flattened->data, g_variant_get_data (value), flattened->len) != 0)
4658 g_file_set_contents ("flattened", flattened->data, flattened->len, NULL);
4659 g_file_set_contents ("serialised", g_variant_get_data (value), g_variant_get_size (value), NULL);
4660 g_print ("type is %s\n", g_variant_get_type_string (value));
4661 g_assert_not_reached ();
4665 g_assert_cmpint (flattened->len, ==, g_variant_get_size (value));
4666 g_assert (memcmp (flattened->data, g_variant_get_data (value), flattened->len) == 0);
4668 g_byte_array_free (flattened, TRUE);
4669 g_variant_unref (value);
4674 main (int argc, char **argv)
4678 g_test_init (&argc, &argv, NULL);
4680 g_test_add_func ("/gvariant/type", test_gvarianttype);
4681 g_test_add_func ("/gvariant/typeinfo", test_gvarianttypeinfo);
4682 g_test_add_func ("/gvariant/serialiser/maybe", test_maybes);
4683 g_test_add_func ("/gvariant/serialiser/array", test_arrays);
4684 g_test_add_func ("/gvariant/serialiser/tuple", test_tuples);
4685 g_test_add_func ("/gvariant/serialiser/variant", test_variants);
4686 g_test_add_func ("/gvariant/serialiser/strings", test_strings);
4687 g_test_add_func ("/gvariant/serialiser/byteswap", test_byteswaps);
4689 for (i = 1; i <= 20; i += 4)
4693 testname = g_strdup_printf ("/gvariant/serialiser/fuzz/%d%%", i);
4694 g_test_add_data_func (testname, GINT_TO_POINTER (i),
4695 (gpointer) test_fuzzes);
4699 g_test_add_func ("/gvariant/string", test_string);
4700 g_test_add_func ("/gvariant/utf8", test_utf8);
4701 g_test_add_func ("/gvariant/containers", test_containers);
4702 g_test_add_func ("/gvariant/format-strings", test_format_strings);
4703 g_test_add_func ("/gvariant/invalid-varargs", test_invalid_varargs);
4704 g_test_add_func ("/gvariant/varargs", test_varargs);
4705 g_test_add_func ("/gvariant/varargs/subprocess/empty-array", test_varargs_empty_array);
4706 g_test_add_func ("/gvariant/valist", test_valist);
4707 g_test_add_func ("/gvariant/builder-memory", test_builder_memory);
4708 g_test_add_func ("/gvariant/hashing", test_hashing);
4709 g_test_add_func ("/gvariant/byteswap", test_gv_byteswap);
4710 g_test_add_func ("/gvariant/parser", test_parses);
4711 g_test_add_func ("/gvariant/parse-failures", test_parse_failures);
4712 g_test_add_func ("/gvariant/parse-positional", test_parse_positional);
4713 g_test_add_func ("/gvariant/parse/subprocess/bad-format-char", test_parse_bad_format_char);
4714 g_test_add_func ("/gvariant/parse/subprocess/bad-format-string", test_parse_bad_format_string);
4715 g_test_add_func ("/gvariant/parse/subprocess/bad-args", test_parse_bad_args);
4716 g_test_add_func ("/gvariant/floating", test_floating);
4717 g_test_add_func ("/gvariant/bytestring", test_bytestring);
4718 g_test_add_func ("/gvariant/lookup-value", test_lookup_value);
4719 g_test_add_func ("/gvariant/lookup", test_lookup);
4720 g_test_add_func ("/gvariant/compare", test_compare);
4721 g_test_add_func ("/gvariant/equal", test_equal);
4722 g_test_add_func ("/gvariant/fixed-array", test_fixed_array);
4723 g_test_add_func ("/gvariant/check-format-string", test_check_format_string);
4725 g_test_add_func ("/gvariant/checksum-basic", test_checksum_basic);
4726 g_test_add_func ("/gvariant/checksum-nested", test_checksum_nested);
4728 g_test_add_func ("/gvariant/gbytes", test_gbytes);
4729 g_test_add_func ("/gvariant/print-context", test_print_context);
4730 g_test_add_func ("/gvariant/error-quark", test_error_quark);
4731 g_test_add_func ("/gvariant/vector-serialiser", test_vector_serialiser);
4733 return g_test_run ();