* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the licence, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* See the included COPYING file for more information.
*
g_variant_serialiser_serialise (serialised, random_instance_filler,
(gpointer *) instances, n_children);
- g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+ if (serialised.size)
+ g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+
g_assert (g_variant_serialised_n_children (serialised) == n_children);
for (i = 0; i < n_children; i++)
g_variant_serialiser_serialise (serialised, random_instance_filler,
(gpointer *) instances, n_children);
- g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+ if (serialised.size)
+ g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+
g_assert (g_variant_serialised_n_children (serialised) == n_children);
for (i = 0; i < n_children; i++)
g_variant_serialiser_serialise (serialised, random_instance_filler,
(gpointer *) &instance, 1);
- g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+ if (serialised.size)
+ g_assert (memcmp (serialised.data, data, serialised.size) == 0);
+
g_assert (g_variant_serialised_n_children (serialised) == 1);
child = g_variant_serialised_get_child (serialised, 0);
union {
guint64 integer;
gdouble floating;
- gchar string[32];
+ gchar string[200];
} data;
gsize data_size;
};
break;
case 's': case 'o': case 'g':
- instance->data_size = g_test_rand_int_range (10, 20);
+ instance->data_size = g_test_rand_int_range (10, 200);
make_random_string (instance->data.string, instance->data_size, type);
break;
}
serialise_tree (TreeInstance *tree,
GVariantSerialised *serialised)
{
- GVariantSerialised empty = { };
+ GVariantSerialised empty = {0, };
*serialised = empty;
tree_filler (serialised, tree);
g_variant_serialised_byteswap (two);
- g_assert_cmpint (one.size, ==, two.size);
- g_assert (memcmp (one.data, two.data, one.size) == 0);
+ g_assert_cmpmem (one.data, one.size, two.data, two.size);
tree_instance_free (tree);
g_free (one.data);
}
static void
+test_string (void)
+{
+ /* Test some different methods of creating strings */
+ GVariant *v;
+
+ v = g_variant_new_string ("foo");
+ g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo");
+ g_variant_unref (v);
+
+
+ v = g_variant_new_take_string (g_strdup ("foo"));
+ g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo");
+ g_variant_unref (v);
+
+ v = g_variant_new_printf ("%s %d", "foo", 123);
+ g_assert_cmpstr (g_variant_get_string (v, NULL), ==, "foo 123");
+ g_variant_unref (v);
+}
+
+static void
test_utf8 (void)
{
const gchar invalid[] = "hello\xffworld";
g_free (printed);
}
+ /* escapes */
+ {
+ const gchar orig[] = " \342\200\254 \360\220\210\240 \a \b \f \n \r \t \v ";
+ GVariant *value;
+ gchar *printed;
+
+ value = g_variant_new_string (orig);
+ printed = g_variant_print (value, FALSE);
+ g_variant_unref (value);
+
+ g_assert_cmpstr (printed, ==, "' \\u202c \\U00010220 \\a \\b \\f \\n \\r \\t \\v '");
+ value = g_variant_parse (NULL, printed, NULL, NULL, NULL);
+ g_assert_cmpstr (g_variant_get_string (value, NULL), ==, orig);
+ g_variant_unref (value);
+ g_free (printed);
+ }
+
+#ifndef _MSC_VER
+ /* inf/nan strings are C99 features which Visual C++ does not support */
/* inf/nan mini test */
{
const gchar *tests[] = { "inf", "-inf", "nan" };
GVariant *value;
gchar *printed;
+ gchar *printed_down;
gint i;
for (i = 0; i < G_N_ELEMENTS (tests); i++)
GError *error = NULL;
value = g_variant_parse (NULL, tests[i], NULL, NULL, &error);
printed = g_variant_print (value, FALSE);
- g_assert (g_str_has_prefix (printed, tests[i]));
+ /* Canonicalize to lowercase; https://bugzilla.gnome.org/show_bug.cgi?id=704585 */
+ printed_down = g_ascii_strdown (printed, -1);
+ g_assert (g_str_has_prefix (printed_down, tests[i]));
g_free (printed);
+ g_free (printed_down);
g_variant_unref (value);
}
}
+#endif
g_variant_type_info_assert_no_infos ();
}
g_variant_unref (dict);
}
+static GVariant *
+untrusted (GVariant *a)
+{
+ GVariant *b;
+ const GVariantType *type;
+ GBytes *bytes;
+
+ type = g_variant_get_type (a);
+ bytes = g_variant_get_data_as_bytes (a);
+ b = g_variant_new_from_bytes (type, bytes, FALSE);
+ g_bytes_unref (bytes);
+ g_variant_unref (a);
+
+ return b;
+}
+
static void
test_compare (void)
{
GVariant *a;
GVariant *b;
- a = g_variant_new_byte (5);
+ a = untrusted (g_variant_new_byte (5));
b = g_variant_new_byte (6);
g_assert (g_variant_compare (a, b) < 0);
g_variant_unref (a);
g_variant_unref (b);
- a = g_variant_new_string ("abc");
+ a = untrusted (g_variant_new_int16 (G_MININT16));
+ b = g_variant_new_int16 (G_MAXINT16);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint16 (0));
+ b = g_variant_new_uint16 (G_MAXUINT16);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_int32 (G_MININT32));
+ b = g_variant_new_int32 (G_MAXINT32);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint32 (0));
+ b = g_variant_new_uint32 (G_MAXUINT32);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_int64 (G_MININT64));
+ b = g_variant_new_int64 (G_MAXINT64);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint64 (0));
+ b = g_variant_new_uint64 (G_MAXUINT64);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_double (G_MINDOUBLE));
+ b = g_variant_new_double (G_MAXDOUBLE);
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_string ("abc"));
b = g_variant_new_string ("abd");
g_assert (g_variant_compare (a, b) < 0);
g_variant_unref (a);
g_variant_unref (b);
- a = g_variant_new_boolean (FALSE);
+ a = untrusted (g_variant_new_object_path ("/abc"));
+ b = g_variant_new_object_path ("/abd");
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_signature ("g"));
+ b = g_variant_new_signature ("o");
+ g_assert (g_variant_compare (a, b) < 0);
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_boolean (FALSE));
b = g_variant_new_boolean (TRUE);
g_assert (g_variant_compare (a, b) < 0);
g_variant_unref (a);
}
static void
+test_equal (void)
+{
+ GVariant *a;
+ GVariant *b;
+
+ a = untrusted (g_variant_new_byte (5));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_int16 (G_MININT16));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint16 (0));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_int32 (G_MININT32));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint32 (0));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_int64 (G_MININT64));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_uint64 (0));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_double (G_MINDOUBLE));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_string ("abc"));
+ g_assert (g_variant_equal (a, a));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_object_path ("/abc"));
+ g_assert (g_variant_equal (a, a));
+ b = g_variant_get_normal_form (a);
+ a = untrusted (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_signature ("g"));
+ g_assert (g_variant_equal (a, a));
+ b = g_variant_get_normal_form (a);
+ a = untrusted (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+ a = untrusted (g_variant_new_boolean (FALSE));
+ b = g_variant_get_normal_form (a);
+ g_assert (g_variant_equal (a, b));
+ g_variant_unref (a);
+ g_variant_unref (b);
+}
+
+static void
test_fixed_array (void)
{
GVariant *a;
g_test_assert_expected_messages ();
g_assert (g_variant_check_format_string (value, "(s^&ay)", FALSE));
+ g_assert (g_variant_check_format_string (value, "r", FALSE));
+ g_assert (g_variant_check_format_string (value, "(?a?)", FALSE));
+
g_variant_unref (value);
}
g_variant_unref (tuple);
}
+typedef struct {
+ const GVariantType *type;
+ const gchar *in;
+ const gchar *out;
+} ContextTest;
+
+static void
+test_print_context (void)
+{
+ ContextTest tests[] = {
+ { NULL, "(1, 2, 3, 'abc", " ^^^^" },
+ { NULL, "[1, 2, 3, 'str']", " ^ ^^^^^" },
+ { G_VARIANT_TYPE_UINT16, "{ 'abc':'def' }", " ^^^^^^^^^^^^^^^" },
+ { NULL, "<5", " ^" },
+ { NULL, "'ab\\ux'", " ^^^^^^^" },
+ { NULL, "'ab\\U00efx'", " ^^^^^^^^^^^" }
+ };
+ GVariant *v;
+ gchar *s;
+ gint i;
+ GError *error = NULL;
+
+ for (i = 0; i < G_N_ELEMENTS (tests); i++)
+ {
+ v = g_variant_parse (tests[i].type, tests[i].in, NULL, NULL, &error);
+ g_assert_null (v);
+ s = g_variant_parse_error_print_context (error, tests[i].in);
+ g_assert (strstr (s, tests[i].out) != NULL);
+ g_free (s);
+ g_clear_error (&error);
+ }
+}
+
+static void
+test_error_quark (void)
+{
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+ g_assert (g_variant_parser_get_error_quark () == g_variant_parse_error_quark ());
+G_GNUC_END_IGNORE_DEPRECATIONS
+}
+
+static void
+test_stack_builder_init (void)
+{
+ GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_BYTESTRING);
+ GVariant *variant;
+
+ g_variant_builder_add_value (&builder, g_variant_new_byte ('g'));
+ g_variant_builder_add_value (&builder, g_variant_new_byte ('l'));
+ g_variant_builder_add_value (&builder, g_variant_new_byte ('i'));
+ g_variant_builder_add_value (&builder, g_variant_new_byte ('b'));
+ g_variant_builder_add_value (&builder, g_variant_new_byte ('\0'));
+
+ variant = g_variant_ref_sink (g_variant_builder_end (&builder));
+ g_assert_nonnull (variant);
+ g_assert (g_variant_type_equal (g_variant_get_type (variant),
+ G_VARIANT_TYPE_BYTESTRING));
+ g_assert_cmpuint (g_variant_n_children (variant), ==, 5);
+ g_assert_cmpstr (g_variant_get_bytestring (variant), ==, "glib");
+ g_variant_unref (variant);
+}
+
+static GVariant *
+get_asv (void)
+{
+ GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
+
+ g_variant_builder_add (&builder, "{s@v}", "foo", g_variant_new_variant (g_variant_new_string ("FOO")));
+ g_variant_builder_add (&builder, "{s@v}", "bar", g_variant_new_variant (g_variant_new_string ("BAR")));
+
+ return g_variant_ref_sink (g_variant_builder_end (&builder));
+}
+
+static void
+test_stack_dict_init (void)
+{
+ GVariant *asv = get_asv ();
+ GVariantDict dict = G_VARIANT_DICT_INIT (asv);
+ GVariant *variant;
+ GVariantIter iter;
+ gchar *key;
+ GVariant *value;
+
+ g_variant_dict_insert_value (&dict, "baz", g_variant_new_string ("BAZ"));
+ g_variant_dict_insert_value (&dict, "quux", g_variant_new_string ("QUUX"));
+
+ variant = g_variant_ref_sink (g_variant_dict_end (&dict));
+ g_assert_nonnull (variant);
+ g_assert (g_variant_type_equal (g_variant_get_type (variant),
+ G_VARIANT_TYPE_VARDICT));
+ g_assert_cmpuint (g_variant_n_children (variant), ==, 4);
+
+ g_variant_iter_init (&iter, variant);
+ while (g_variant_iter_next (&iter, "{sv}", &key, &value))
+ {
+ gchar *strup = g_ascii_strup (key, -1);
+
+ g_assert_cmpstr (strup, ==, g_variant_get_string (value, NULL));
+ g_free (key);
+ g_free (strup);
+ g_variant_unref (value);
+ }
+
+ g_variant_unref (asv);
+ g_variant_unref (variant);
+}
+
int
main (int argc, char **argv)
{
g_free (testname);
}
+ g_test_add_func ("/gvariant/string", test_string);
g_test_add_func ("/gvariant/utf8", test_utf8);
g_test_add_func ("/gvariant/containers", test_containers);
g_test_add_func ("/gvariant/format-strings", test_format_strings);
g_test_add_func ("/gvariant/lookup-value", test_lookup_value);
g_test_add_func ("/gvariant/lookup", test_lookup);
g_test_add_func ("/gvariant/compare", test_compare);
+ g_test_add_func ("/gvariant/equal", test_equal);
g_test_add_func ("/gvariant/fixed-array", test_fixed_array);
g_test_add_func ("/gvariant/check-format-string", test_check_format_string);
g_test_add_func ("/gvariant/checksum-nested", test_checksum_nested);
g_test_add_func ("/gvariant/gbytes", test_gbytes);
+ g_test_add_func ("/gvariant/print-context", test_print_context);
+ g_test_add_func ("/gvariant/error-quark", test_error_quark);
+ g_test_add_func ("/gvariant/stack-builder-init", test_stack_builder_init);
+ g_test_add_func ("/gvariant/stack-dict-init", test_stack_dict_init);
return g_test_run ();
}