1 /* test-mime-data.c: tests for the mime implementation
3 * More info can be found at http://www.freedesktop.org/standards/
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.
36 static int failed = 0;
37 static int xfailed = 0;
38 static int xmatch = 0;
40 static int verbose = 0;
43 check_mime_type (const char *mt,
44 const char *mt_expected,
51 if (strcmp (mt, mt_expected) != 0)
60 printf ("%s, '%s' test: expected %s, got %s (expected failure)\n",
61 filename, test, mt_expected, mt);
66 printf ("%s, '%s' test: expected %s, got %s\n",
67 filename, test, mt_expected, mt);
78 printf ("%s, '%s' test: got %s (unexpected match)\n",
86 test_by_name (const char *filename,
87 const char *mt_expected,
92 mt = xdg_mime_get_mime_type_from_file_name (filename);
94 check_mime_type (mt, mt_expected, xfail, "name", filename);
98 test_by_data (const char *dir,
100 const char *mt_expected,
110 snprintf (path, 1024, "%s/%s", dir, filename);
112 file = fopen (path, "r");
116 printf ("Could not open %s\n", path);
122 max_extent = xdg_mime_get_max_buffer_extents ();
123 data = malloc (max_extent);
127 printf ("Failed to allocate memory for file %s\n", filename);
133 bytes_read = fread (data, 1, max_extent, file);
137 printf ("Error reading file %s\n", path);
146 mt = xdg_mime_get_mime_type_for_data (data, bytes_read);
151 check_mime_type (mt, mt_expected, xfail, "data", filename);
155 test_by_file (const char *dir,
156 const char *filename,
157 const char *mt_expected,
163 snprintf (path, 1024, "%s/%s", dir, filename);
165 mt = xdg_mime_get_mime_type_for_file (path, NULL);
167 check_mime_type (mt, mt_expected, xfail, "file", filename);
171 test_single_file (const char *dir,
172 const char *filename,
173 const char *mimetype,
178 test_by_name (filename, mimetype, xfail_name);
179 test_by_data (dir, filename, mimetype, xfail_data);
180 test_by_file (dir, filename, mimetype, xfail_file);
184 read_from_file (const char *filename)
189 char *testfile, *mimetype, *flags;
190 char *rest, *space, end;
192 int xfail_name, xfail_data, xfail_file;
194 file = fopen (filename, "r");
195 tmp = strdup (filename);
196 dir = strdup (dirname (tmp));
201 printf ("Could not open %s\n", filename);
205 while (fgets (line, 255, file) != NULL)
210 while (*rest == ' ') rest++;
212 if (*rest == '#' || *rest == '\n')
215 space = strchr (rest, ' ');
218 printf ("Malformed line in %s:%d\n\t%s\n", filename, lineno, line);
226 while (*rest == ' ') rest++;
228 while (*space != ' ' && *space != '\n') space++;
241 while (*rest == ' ') rest++;
243 while (*space != ' ' && *space != '\n') space++;
249 switch (strlen (flags))
252 printf ("%s.%d: Extra flags are ignored\n", filename, lineno);
267 /* Should not happen */
271 test_single_file (dir, testfile, mimetype,
272 xfail_name, xfail_data, xfail_file);
283 printf ("usage: test-mime-data <FILE>\n\n");
284 printf ("Tests the mime system.\n");
285 printf ("Testcases are specified in the following format:\n\n");
286 printf ("# comment\n");
287 printf ("<filename1> <mimetype> [<flags>]\n");
288 printf ("<filename2> <mimetype> [<flags>]\n");
290 printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n");
291 printf ("indicates an expected failure when determining the\n");
292 printf ("mimetype by name, data or file.\n");
298 main (int argc, char *argv[])
305 for (i = 1; i < argc; i++)
307 if (strcmp (argv[i], "-v") == 0)
310 read_from_file (argv[i]);
313 if (error > 0 || failed > 0)
315 printf ("%d errors, %d comparisons, %d failed",
316 error, total, failed);
318 printf (" (%d expected)", xfailed);
320 printf (", %d unexpected successes", xmatch);