]> git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/test-mime-data.c
Merge commit 'cf31d981a600e46f3d3344be8f33f0813a909bb1' as 'xdgmime-source'
[libxdgmime-perl.git] / xdgmime-source / src / test-mime-data.c
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 *
8 * Licensed under the Academic Free License version 2.0
9 * Or under the following terms:
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libgen.h>
31
32 #include "xdgmime.h"
33
34 static int error = 0;
35 static int total = 0;
36 static int failed = 0;
37 static int xfailed = 0;
38 static int xmatch = 0;
39
40 static int verbose = 0;
41
42 static void
43 check_mime_type (const char *mt,
44 const char *mt_expected,
45 int xfail,
46 const char *test,
47 const char *filename)
48 {
49 total++;
50
51 if (strcasecmp (mt, mt_expected) != 0)
52 {
53 failed++;
54
55 if (xfail)
56 {
57 xfailed++;
58
59 if (verbose > 1)
60 printf ("%s, '%s' test: expected %s, got %s (expected failure)\n",
61 filename, test, mt_expected, mt);
62 }
63 else
64 {
65 if (verbose > 0)
66 printf ("%s, '%s' test: expected %s, got %s\n",
67 filename, test, mt_expected, mt);
68 }
69
70 }
71 else
72 {
73 if (xfail)
74 {
75 xmatch++;
76
77 if (verbose > 0)
78 printf ("%s, '%s' test: got %s (unexpected match)\n",
79 filename, test, mt);
80 }
81 }
82
83 }
84
85 static void
86 test_by_name (const char *filename,
87 const char *mt_expected,
88 int xfail)
89 {
90 const char *mt;
91
92 mt = xdg_mime_get_mime_type_from_file_name (filename);
93
94 check_mime_type (mt, mt_expected, xfail, "name", filename);
95 }
96
97 static void
98 test_by_data (const char *dir,
99 const char *filename,
100 const char *mt_expected,
101 int xfail)
102 {
103 FILE *file;
104 const char *mt;
105 int max_extent;
106 char *data;
107 int bytes_read;
108 int result_prio;
109 char path[1024];
110
111 snprintf (path, 1024, "%s/%s", dir, filename);
112
113 file = fopen (path, "r");
114
115 if (file == NULL)
116 {
117 printf ("Could not open %s\n", path);
118 error++;
119
120 return;
121 }
122
123 max_extent = xdg_mime_get_max_buffer_extents ();
124 data = malloc (max_extent);
125
126 if (data == NULL)
127 {
128 printf ("Failed to allocate memory for file %s\n", filename);
129 error++;
130
131 fclose (file);
132 return;
133 }
134
135 bytes_read = fread (data, 1, max_extent, file);
136
137 if (ferror (file))
138 {
139 printf ("Error reading file %s\n", path);
140 error++;
141
142 free (data);
143 fclose (file);
144
145 return;
146 }
147
148 mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio);
149
150 free (data);
151 fclose (file);
152
153 check_mime_type (mt, mt_expected, xfail, "data", filename);
154 }
155
156 static void
157 test_by_file (const char *dir,
158 const char *filename,
159 const char *mt_expected,
160 int xfail)
161 {
162 const char *mt;
163 char path[1024];
164
165 snprintf (path, 1024, "%s/%s", dir, filename);
166
167 mt = xdg_mime_get_mime_type_for_file (path, NULL);
168
169 check_mime_type (mt, mt_expected, xfail, "file", filename);
170 }
171
172 static void
173 test_single_file (const char *dir,
174 const char *filename,
175 const char *mimetype,
176 int xfail_name,
177 int xfail_data,
178 int xfail_file)
179 {
180 test_by_name (filename, mimetype, xfail_name);
181 test_by_data (dir, filename, mimetype, xfail_data);
182 test_by_file (dir, filename, mimetype, xfail_file);
183 }
184
185 static void
186 read_from_file (const char *filename)
187 {
188 FILE *file;
189 char line[255];
190 int lineno = 0;
191 char *testfile, *mimetype, *flags;
192 char *rest, *space, end;
193 char *dir, *tmp;
194 int xfail_name, xfail_data, xfail_file;
195
196 file = fopen (filename, "r");
197 tmp = strdup (filename);
198 dir = strdup (dirname (tmp));
199 free (tmp);
200
201 if (file == NULL)
202 {
203 printf ("Could not open %s\n", filename);
204 exit (1);
205 }
206
207 while (fgets (line, 255, file) != NULL)
208 {
209 lineno++;
210
211 rest = line;
212 while (*rest == ' ') rest++;
213
214 if (*rest == '#' || *rest == '\n')
215 continue;
216
217 space = strchr (rest, ' ');
218 if (!space)
219 {
220 printf ("Malformed line in %s:%d\n\t%s\n", filename, lineno, line);
221 continue;
222 }
223
224 *space = '\0';
225 testfile = rest;
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 mimetype = rest;
235
236 xfail_name = 0;
237 xfail_data = 0;
238 xfail_file = 0;
239
240 if (end != '\n')
241 {
242 rest = space + 1;
243 while (*rest == ' ') rest++;
244 space = rest;
245 while (*space != ' ' && *space != '\n') space++;
246 end = *space;
247
248 *space = '\0';
249 flags = rest;
250
251 switch (strlen (flags))
252 {
253 default:
254 printf ("%s.%d: Extra flags are ignored\n", filename, lineno);
255 /* Fall thu */
256 case 3:
257 if (flags[2] == 'x')
258 xfail_file = 1;
259 /* Fall thu */
260 case 2:
261 if (flags[1] == 'x')
262 xfail_data = 1;
263 /* Fall thu */
264 case 1:
265 if (flags[0] == 'x')
266 xfail_name = 1;
267 break;
268 case 0: ;
269 /* Should not happen */
270 }
271 }
272
273 test_single_file (dir, testfile, mimetype,
274 xfail_name, xfail_data, xfail_file);
275 }
276
277 fclose (file);
278
279 free (dir);
280 }
281
282 static void
283 usage (void)
284 {
285 printf ("usage: test-mime-data <FILE>\n\n");
286 printf ("Tests the mime system.\n");
287 printf ("Testcases are specified in the following format:\n\n");
288 printf ("# comment\n");
289 printf ("<filename1> <mimetype> [<flags>]\n");
290 printf ("<filename2> <mimetype> [<flags>]\n");
291 printf ("...\n\n");
292 printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n");
293 printf ("indicates an expected failure when determining the\n");
294 printf ("mimetype by name, data or file.\n");
295
296 exit (1);
297 }
298
299 int
300 main (int argc, char *argv[])
301 {
302 int i;
303
304 if (argc < 2)
305 usage ();
306
307 for (i = 1; i < argc; i++)
308 {
309 if (strcmp (argv[i], "-v") == 0)
310 verbose++;
311 else
312 read_from_file (argv[i]);
313 }
314
315 if (error > 0 || failed > 0)
316 {
317 printf ("%d errors, %d comparisons, %d failed",
318 error, total, failed);
319 if (xfailed > 0)
320 printf (" (%d expected)", xfailed);
321 if (xmatch > 0)
322 printf (", %d unexpected successes", xmatch);
323 printf ("\n");
324
325 if (xmatch > 0)
326 return 1;
327 if (xfailed == failed)
328 return 0;
329 return 1;
330 }
331
332 return 0;
333 }