Fixed use-after-free in xdgmime
[rox-filer.git] / src / test-mime-data.c
blob577f773096ef2141dac551b0e37765f0e3fc3cab
1 /* test-mime-data.c: tests for the mime implementation
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>
8 * Licensed under the Academic Free License version 2.0
9 * Or under the following terms:
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.
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.
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.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <libgen.h>
32 #include "xdgmime.h"
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;
40 static int verbose = 0;
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)
49 total++;
51 if (strcmp (mt, mt_expected) != 0)
53 failed++;
55 if (xfail)
57 xfailed++;
59 if (verbose > 1)
60 printf ("%s, '%s' test: expected %s, got %s (expected failure)\n",
61 filename, test, mt_expected, mt);
63 else
65 if (verbose > 0)
66 printf ("%s, '%s' test: expected %s, got %s\n",
67 filename, test, mt_expected, mt);
71 else
73 if (xfail)
75 xmatch++;
77 if (verbose > 1)
78 printf ("%s, '%s' test: got %s (unexpected match)\n",
79 filename, test, mt);
85 static void
86 test_by_name (const char *filename,
87 const char *mt_expected,
88 int xfail)
90 const char *mt;
92 mt = xdg_mime_get_mime_type_from_file_name (filename);
94 check_mime_type (mt, mt_expected, xfail, "name", filename);
97 static void
98 test_by_data (const char *dir,
99 const char *filename,
100 const char *mt_expected,
101 int xfail)
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];
111 snprintf (path, 1024, "%s/%s", dir, filename);
113 file = fopen (path, "r");
115 if (file == NULL)
117 printf ("Could not open %s\n", path);
118 error++;
120 return;
123 max_extent = xdg_mime_get_max_buffer_extents ();
124 data = malloc (max_extent);
126 if (data == NULL)
128 printf ("Failed to allocate memory for file %s\n", filename);
129 error++;
131 return;
134 bytes_read = fread (data, 1, max_extent, file);
136 if (ferror (file))
138 printf ("Error reading file %s\n", path);
139 error++;
141 free (data);
142 fclose (file);
144 return;
147 mt = xdg_mime_get_mime_type_for_data (data, bytes_read, &result_prio);
149 free (data);
150 fclose (file);
152 check_mime_type (mt, mt_expected, xfail, "data", filename);
155 static void
156 test_by_file (const char *dir,
157 const char *filename,
158 const char *mt_expected,
159 int xfail)
161 const char *mt;
162 char path[1024];
164 snprintf (path, 1024, "%s/%s", dir, filename);
166 mt = xdg_mime_get_mime_type_for_file (path, NULL);
168 check_mime_type (mt, mt_expected, xfail, "file", filename);
171 static void
172 test_single_file (const char *dir,
173 const char *filename,
174 const char *mimetype,
175 int xfail_name,
176 int xfail_data,
177 int xfail_file)
179 test_by_name (filename, mimetype, xfail_name);
180 test_by_data (dir, filename, mimetype, xfail_data);
181 test_by_file (dir, filename, mimetype, xfail_file);
184 static void
185 read_from_file (const char *filename)
187 FILE *file;
188 char line[255];
189 int lineno = 0;
190 char *testfile, *mimetype, *flags;
191 char *rest, *space, end;
192 char *dir, *tmp;
193 int xfail_name, xfail_data, xfail_file;
195 file = fopen (filename, "r");
196 tmp = strdup (filename);
197 dir = strdup (dirname (tmp));
198 free (tmp);
200 if (file == NULL)
202 printf ("Could not open %s\n", filename);
203 exit (1);
206 while (fgets (line, 255, file) != NULL)
208 lineno++;
210 rest = line;
211 while (*rest == ' ') rest++;
213 if (*rest == '#' || *rest == '\n')
214 continue;
216 space = strchr (rest, ' ');
217 if (!space)
219 printf ("Malformed line in %s:%d\n\t%s\n", filename, lineno, line);
220 continue;
223 *space = '\0';
224 testfile = rest;
226 rest = space + 1;
227 while (*rest == ' ') rest++;
228 space = rest;
229 while (*space != ' ' && *space != '\n') space++;
230 end = *space;
232 *space = '\0';
233 mimetype = rest;
235 xfail_name = 0;
236 xfail_data = 0;
237 xfail_file = 0;
239 if (end != '\n')
241 rest = space + 1;
242 while (*rest == ' ') rest++;
243 space = rest;
244 while (*space != ' ' && *space != '\n') space++;
245 end = *space;
247 *space = '\0';
248 flags = rest;
250 switch (strlen (flags))
252 default:
253 printf ("%s.%d: Extra flags are ignored\n", filename, lineno);
254 /* Fall thu */
255 case 3:
256 if (flags[2] == 'x')
257 xfail_file = 1;
258 /* Fall thu */
259 case 2:
260 if (flags[1] == 'x')
261 xfail_data = 1;
262 /* Fall thu */
263 case 1:
264 if (flags[0] == 'x')
265 xfail_name = 1;
266 break;
267 case 0: ;
268 /* Should not happen */
272 test_single_file (dir, testfile, mimetype,
273 xfail_name, xfail_data, xfail_file);
276 fclose (file);
278 free (dir);
281 static void
282 usage (void)
284 printf ("usage: test-mime-data <FILE>\n\n");
285 printf ("Tests the mime system.\n");
286 printf ("Testcases are specified in the following format:\n\n");
287 printf ("# comment\n");
288 printf ("<filename1> <mimetype> [<flags>]\n");
289 printf ("<filename2> <mimetype> [<flags>]\n");
290 printf ("...\n\n");
291 printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n");
292 printf ("indicates an expected failure when determining the\n");
293 printf ("mimetype by name, data or file.\n");
295 exit (1);
298 int
299 main (int argc, char *argv[])
301 int i;
303 if (argc < 2)
304 usage ();
306 for (i = 1; i < argc; i++)
308 if (strcmp (argv[i], "-v") == 0)
309 verbose++;
310 else
311 read_from_file (argv[i]);
314 if (error > 0 || failed > 0)
316 printf ("%d errors, %d comparisons, %d failed",
317 error, total, failed);
318 if (xfailed > 0)
319 printf (" (%d expected)", xfailed);
320 if (xmatch > 0)
321 printf (", %d unexpected successes", xmatch);
322 printf ("\n");
324 if (xmatch > 0)
325 return 1;
326 if (xfailed == failed)
327 return 0;
328 return 1;
331 return 0;