]>
Commit | Line | Data |
---|---|---|
cf31d981 SI |
1 | /* test-mime-data.c: tests for the mime implementation |
2 | * | |
3 | * More info can be found at http://www.freedesktop.org/standards/ | |
4 | * | |
5 | * Copyright (C) 2005 Red Hat, Inc. | |
6 | * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com> | |
7 | * | |
73d5a30a | 8 | * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0 |
cf31d981 SI |
9 | */ |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
73d5a30a | 14 | #include <strings.h> |
cf31d981 SI |
15 | #include <libgen.h> |
16 | ||
17 | #include "xdgmime.h" | |
18 | ||
19 | static int error = 0; | |
20 | static int total = 0; | |
21 | static int failed = 0; | |
22 | static int xfailed = 0; | |
23 | static int xmatch = 0; | |
24 | ||
25 | static int verbose = 0; | |
26 | ||
27 | static void | |
28 | check_mime_type (const char *mt, | |
29 | const char *mt_expected, | |
30 | int xfail, | |
31 | const char *test, | |
32 | const char *filename) | |
33 | { | |
34 | total++; | |
35 | ||
36 | if (strcasecmp (mt, mt_expected) != 0) | |
37 | { | |
38 | failed++; | |
39 | ||
40 | if (xfail) | |
41 | { | |
42 | xfailed++; | |
43 | ||
44 | if (verbose > 1) | |
45 | printf ("%s, '%s' test: expected %s, got %s (expected failure)\n", | |
46 | filename, test, mt_expected, mt); | |
47 | } | |
48 | else | |
49 | { | |
50 | if (verbose > 0) | |
73d5a30a SI |
51 | fprintf (stderr, "%s, '%s' test: expected %s, got %s\n", |
52 | filename, test, mt_expected, mt); | |
cf31d981 SI |
53 | } |
54 | ||
55 | } | |
56 | else | |
57 | { | |
58 | if (xfail) | |
59 | { | |
60 | xmatch++; | |
61 | ||
62 | if (verbose > 0) | |
63 | printf ("%s, '%s' test: got %s (unexpected match)\n", | |
64 | filename, test, mt); | |
65 | } | |
66 | } | |
67 | ||
68 | } | |
69 | ||
70 | static void | |
71 | test_by_name (const char *filename, | |
72 | const char *mt_expected, | |
73 | int xfail) | |
74 | { | |
75 | const char *mt; | |
76 | ||
77 | mt = xdg_mime_get_mime_type_from_file_name (filename); | |
78 | ||
79 | check_mime_type (mt, mt_expected, xfail, "name", filename); | |
80 | } | |
81 | ||
82 | static void | |
83 | test_by_data (const char *dir, | |
84 | const char *filename, | |
85 | const char *mt_expected, | |
86 | int xfail) | |
87 | { | |
88 | FILE *file; | |
89 | const char *mt; | |
90 | int max_extent; | |
91 | char *data; | |
92 | int bytes_read; | |
93 | int result_prio; | |
94 | char path[1024]; | |
95 | ||
96 | snprintf (path, 1024, "%s/%s", dir, filename); | |
97 | ||
98 | file = fopen (path, "r"); | |
99 | ||
100 | if (file == NULL) | |
101 | { | |
73d5a30a | 102 | fprintf (stderr, "Could not open %s\n", path); |
cf31d981 SI |
103 | error++; |
104 | ||
105 | return; | |
106 | } | |
107 | ||
108 | max_extent = xdg_mime_get_max_buffer_extents (); | |
109 | data = malloc (max_extent); | |
110 | ||
111 | if (data == NULL) | |
112 | { | |
73d5a30a | 113 | fprintf (stderr, "Failed to allocate memory for file %s\n", filename); |
cf31d981 SI |
114 | error++; |
115 | ||
116 | fclose (file); | |
117 | return; | |
118 | } | |
119 | ||
120 | bytes_read = fread (data, 1, max_extent, file); | |
121 | ||
122 | if (ferror (file)) | |
123 | { | |
73d5a30a | 124 | fprintf (stderr, "Error reading file %s\n", path); |
cf31d981 SI |
125 | error++; |
126 | ||
127 | free (data); | |
128 | fclose (file); | |
129 | ||
130 | return; | |
131 | } | |
132 | ||
133 | mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio); | |
134 | ||
135 | free (data); | |
136 | fclose (file); | |
137 | ||
138 | check_mime_type (mt, mt_expected, xfail, "data", filename); | |
139 | } | |
140 | ||
141 | static void | |
142 | test_by_file (const char *dir, | |
143 | const char *filename, | |
144 | const char *mt_expected, | |
145 | int xfail) | |
146 | { | |
147 | const char *mt; | |
148 | char path[1024]; | |
149 | ||
150 | snprintf (path, 1024, "%s/%s", dir, filename); | |
151 | ||
152 | mt = xdg_mime_get_mime_type_for_file (path, NULL); | |
153 | ||
154 | check_mime_type (mt, mt_expected, xfail, "file", filename); | |
155 | } | |
156 | ||
157 | static void | |
158 | test_single_file (const char *dir, | |
159 | const char *filename, | |
160 | const char *mimetype, | |
161 | int xfail_name, | |
162 | int xfail_data, | |
163 | int xfail_file) | |
164 | { | |
165 | test_by_name (filename, mimetype, xfail_name); | |
166 | test_by_data (dir, filename, mimetype, xfail_data); | |
167 | test_by_file (dir, filename, mimetype, xfail_file); | |
168 | } | |
169 | ||
170 | static void | |
171 | read_from_file (const char *filename) | |
172 | { | |
173 | FILE *file; | |
174 | char line[255]; | |
175 | int lineno = 0; | |
176 | char *testfile, *mimetype, *flags; | |
177 | char *rest, *space, end; | |
178 | char *dir, *tmp; | |
179 | int xfail_name, xfail_data, xfail_file; | |
180 | ||
181 | file = fopen (filename, "r"); | |
182 | tmp = strdup (filename); | |
183 | dir = strdup (dirname (tmp)); | |
184 | free (tmp); | |
185 | ||
186 | if (file == NULL) | |
187 | { | |
73d5a30a | 188 | fprintf (stderr, "Could not open %s\n", filename); |
cf31d981 SI |
189 | exit (1); |
190 | } | |
191 | ||
192 | while (fgets (line, 255, file) != NULL) | |
193 | { | |
194 | lineno++; | |
195 | ||
196 | rest = line; | |
197 | while (*rest == ' ') rest++; | |
198 | ||
199 | if (*rest == '#' || *rest == '\n') | |
200 | continue; | |
201 | ||
202 | space = strchr (rest, ' '); | |
203 | if (!space) | |
204 | { | |
73d5a30a | 205 | fprintf (stderr, "Malformed line in %s:%d\n\t%s\n", filename, lineno, line); |
cf31d981 SI |
206 | continue; |
207 | } | |
208 | ||
209 | *space = '\0'; | |
210 | testfile = rest; | |
211 | ||
212 | rest = space + 1; | |
213 | while (*rest == ' ') rest++; | |
214 | space = rest; | |
215 | while (*space != ' ' && *space != '\n') space++; | |
216 | end = *space; | |
217 | ||
218 | *space = '\0'; | |
219 | mimetype = rest; | |
220 | ||
221 | xfail_name = 0; | |
222 | xfail_data = 0; | |
223 | xfail_file = 0; | |
224 | ||
225 | if (end != '\n') | |
226 | { | |
227 | rest = space + 1; | |
228 | while (*rest == ' ') rest++; | |
229 | space = rest; | |
230 | while (*space != ' ' && *space != '\n') space++; | |
231 | end = *space; | |
232 | ||
233 | *space = '\0'; | |
234 | flags = rest; | |
235 | ||
236 | switch (strlen (flags)) | |
237 | { | |
238 | default: | |
239 | printf ("%s.%d: Extra flags are ignored\n", filename, lineno); | |
240 | /* Fall thu */ | |
241 | case 3: | |
242 | if (flags[2] == 'x') | |
243 | xfail_file = 1; | |
244 | /* Fall thu */ | |
245 | case 2: | |
246 | if (flags[1] == 'x') | |
247 | xfail_data = 1; | |
248 | /* Fall thu */ | |
249 | case 1: | |
250 | if (flags[0] == 'x') | |
251 | xfail_name = 1; | |
252 | break; | |
253 | case 0: ; | |
254 | /* Should not happen */ | |
255 | } | |
256 | } | |
257 | ||
258 | test_single_file (dir, testfile, mimetype, | |
259 | xfail_name, xfail_data, xfail_file); | |
260 | } | |
261 | ||
262 | fclose (file); | |
263 | ||
264 | free (dir); | |
265 | } | |
266 | ||
267 | static void | |
268 | usage (void) | |
269 | { | |
270 | printf ("usage: test-mime-data <FILE>\n\n"); | |
271 | printf ("Tests the mime system.\n"); | |
272 | printf ("Testcases are specified in the following format:\n\n"); | |
273 | printf ("# comment\n"); | |
274 | printf ("<filename1> <mimetype> [<flags>]\n"); | |
275 | printf ("<filename2> <mimetype> [<flags>]\n"); | |
276 | printf ("...\n\n"); | |
277 | printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n"); | |
278 | printf ("indicates an expected failure when determining the\n"); | |
279 | printf ("mimetype by name, data or file.\n"); | |
280 | ||
281 | exit (1); | |
282 | } | |
283 | ||
284 | int | |
285 | main (int argc, char *argv[]) | |
286 | { | |
287 | int i; | |
288 | ||
289 | if (argc < 2) | |
290 | usage (); | |
291 | ||
292 | for (i = 1; i < argc; i++) | |
293 | { | |
294 | if (strcmp (argv[i], "-v") == 0) | |
295 | verbose++; | |
296 | else | |
297 | read_from_file (argv[i]); | |
298 | } | |
299 | ||
73d5a30a SI |
300 | printf ("%d errors, %d comparisons, %d failed", |
301 | error, total, failed); | |
302 | if (xfailed > 0) | |
303 | printf (" (%d expected)", xfailed); | |
304 | if (xmatch > 0) | |
305 | printf (", %d unexpected successes", xmatch); | |
306 | printf ("\n"); | |
307 | ||
308 | if (error > 0 || xmatch > 0 || failed != xfailed) | |
309 | return 1; | |
cf31d981 SI |
310 | return 0; |
311 | } |