1 | /* |
2 | * Copyright © 2019 Benjamin Otte |
3 | * |
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.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | * |
17 | * Authors: Benjamin Otte <otte@gnome.org> |
18 | */ |
19 | |
20 | #include "../../gtk/css/gtkcssdataurlprivate.h" |
21 | |
22 | #include <locale.h> |
23 | |
24 | typedef struct _Test Test; |
25 | |
26 | struct _Test |
27 | { |
28 | const char *name; |
29 | const char *url; |
30 | const char *mimetype; |
31 | const char *contents; |
32 | gsize contents_len; |
33 | }; |
34 | |
35 | #define CONTENTS(data) (data), sizeof(data) - 1 |
36 | Test tests[] = { |
37 | { "simple" , |
38 | "data:,HelloWorld" , |
39 | NULL, CONTENTS("HelloWorld" ) }, |
40 | { "nodata" , |
41 | "data:," , |
42 | NULL, CONTENTS("" ) }, |
43 | { "case_sensitive" , |
44 | "dATa:,HelloWorld" , |
45 | NULL, CONTENTS("HelloWorld" ) }, |
46 | { "semicolon_after_comma" , |
47 | "data:,;base64" , |
48 | NULL, CONTENTS(";base64" ) }, |
49 | { "mimetype" , |
50 | "data:image/png,nopng" , |
51 | "image/png" , CONTENTS("nopng" ) }, |
52 | { "charset" , |
53 | "data:text/plain;charset=ISO-8859-1,Timm B\344der" , |
54 | "text/plain" , CONTENTS("Timm Bäder" ) }, |
55 | { "charset_escaped" , |
56 | "data:text/plain;charset=ISO-8859-1,Timm%20B%E4der" , |
57 | "text/plain" , CONTENTS("Timm Bäder" ) }, |
58 | { "charset_base64" , |
59 | "data:text/plain;charset=ISO-8859-5;base64,wOPh29DdILjW0ePb0OLe0g==" , |
60 | "text/plain" , CONTENTS("Руслан Ижбулатов" ) }, |
61 | { "wrong_scheme" , |
62 | "duda:,Hello" , |
63 | NULL, NULL, 0 }, |
64 | { "missing_comma" , |
65 | "data:text/plain;charset=ISO-8859-1:bla" , |
66 | NULL, NULL, 0 }, |
67 | { "bad_escape" , |
68 | "data:,abc%00" , |
69 | NULL, NULL, -1 }, |
70 | }; |
71 | |
72 | static void |
73 | test_parse (gconstpointer data) |
74 | { |
75 | const Test *test = data; |
76 | GError *error = NULL; |
77 | char *mimetype = NULL; |
78 | GBytes *bytes; |
79 | |
80 | bytes = gtk_css_data_url_parse (url: test->url, out_mimetype: &mimetype, error: &error); |
81 | |
82 | if (test->contents) |
83 | { |
84 | g_assert_no_error (error); |
85 | g_assert_nonnull (bytes); |
86 | if (test->mimetype == NULL) |
87 | g_assert_null (mimetype); |
88 | else |
89 | g_assert_cmpstr (mimetype, ==, test->mimetype); |
90 | |
91 | g_assert_cmpmem (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), |
92 | test->contents, test->contents_len); |
93 | g_bytes_unref (bytes); |
94 | } |
95 | else |
96 | { |
97 | g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME); |
98 | g_assert_null (bytes); |
99 | g_error_free (error); |
100 | } |
101 | } |
102 | |
103 | int |
104 | main (int argc, char *argv[]) |
105 | { |
106 | guint i; |
107 | |
108 | (g_test_init) (argc: &argc, argv: &argv, NULL); |
109 | setlocale (LC_ALL, locale: "C" ); |
110 | |
111 | for (i = 0; i < G_N_ELEMENTS (tests); i++) |
112 | { |
113 | char *name = g_strdup_printf (format: "/css/data/load/%s" , tests[i].name); |
114 | g_test_add_data_func (testpath: name, test_data: &tests[i], test_func: test_parse); |
115 | g_free (mem: name); |
116 | } |
117 | |
118 | return g_test_run (); |
119 | } |
120 | |
121 | |