Debian Codegraph @

src/ws/test-unicode.c

raw
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitunicode.h"
9
10 #include "testlib/cockpittest.h"
11
12 #include <string.h>
13
14 typedef struct {
15 const gchar *input;
16 const gchar *output;
17 gboolean incomplete;
18 } Fixture;
19
20 static void
21 test_incomplete_utf8 (gconstpointer data)
22 {
23 const Fixture *fixture = data;
24 GBytes *input;
25 gboolean result;
26
27 g_assert (data != NULL);
28
29 input = g_bytes_new_static (fixture->input, strlen (fixture->input));
30 result = cockpit_unicode_has_incomplete_ending (input);
31 g_assert (result == fixture->incomplete);
32
33 g_bytes_unref (input);
34 }
35
36 static void
37 test_force_utf8 (gconstpointer data)
38 {
39 const Fixture *fixture = data;
40 const gchar *expect;
41 GBytes *input;
42 GBytes *output;
43
44 g_assert (data != NULL);
45
46 input = g_bytes_new_static (fixture->input, strlen (fixture->input));
47 output = cockpit_unicode_force_utf8 (input);
48
49 expect = fixture->output ? fixture->output : fixture->input;
50 cockpit_assert_bytes_eq (output, expect, -1);
51
52 if (!fixture->output)
53 g_assert (input == output);
54 else
55 g_assert (input != output);
56
57 g_bytes_unref (input);
58 g_bytes_unref (output);
59 }
60
61 static const Fixture fixtures[] = {
62 { "this is a ascii", NULL, FALSE },
63 { "this is \303\244 utf8", NULL, FALSE },
64 { "this is \303 invalid", "this is \357\277\275 invalid", FALSE },
65 { "this is invalid \303", "this is invalid \357\277\275", TRUE },
66 { "\303 this is \303 invalid \303", "\357\277\275 this is \357\277\275 invalid \357\277\275", TRUE },
67 { "\303 this is \303 invalid \303\303", "\357\277\275 this is \357\277\275 invalid \357\277\275\357\277\275", TRUE },
68 { "\303 this is \303 invalid \303\303a", "\357\277\275 this is \357\277\275 invalid \357\277\275\357\277\275a", FALSE },
69 { "Marmalaade!""\xe2\x94\x80", NULL, FALSE },
70 };
71
72 int
73 main (int argc,
74 char *argv[])
75 {
76 gchar *escaped;
77 gchar *name;
78 gchar *name2;
79 gint i;
80
81 cockpit_test_init (&argc, &argv);
82
83 for (i = 0; i < G_N_ELEMENTS (fixtures); i++)
84 {
85 g_assert (fixtures[i].input != NULL);
86 escaped = g_strcanon (g_strdup (fixtures[i].input), COCKPIT_TEST_CHARS, '_');
87 name = g_strdup_printf ("/unicode/force-utf8/%s", escaped);
88 name2 = g_strdup_printf ("/unicode/incomplete-utf8/%s", escaped);
89 g_free (escaped);
90
91 g_test_add_data_func (name, fixtures + i, test_force_utf8);
92 g_test_add_data_func (name2, fixtures + i, test_incomplete_utf8);
93 g_free (name);
94 g_free (name2);
95 }
96
97 return g_test_run ();
98 }