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
,
111 snprintf (path
, 1024, "%s/%s", dir
, filename
);
113 file
= fopen (path
, "r");
117 printf ("Could not open %s\n", path
);
123 max_extent
= xdg_mime_get_max_buffer_extents ();
124 data
= malloc (max_extent
);
128 printf ("Failed to allocate memory for file %s\n", filename
);
134 bytes_read
= fread (data
, 1, max_extent
, file
);
138 printf ("Error reading file %s\n", path
);
147 mt
= xdg_mime_get_mime_type_for_data (data
, bytes_read
, &result_prio
);
152 check_mime_type (mt
, mt_expected
, xfail
, "data", filename
);
156 test_by_file (const char *dir
,
157 const char *filename
,
158 const char *mt_expected
,
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
);
172 test_single_file (const char *dir
,
173 const char *filename
,
174 const char *mimetype
,
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
);
185 read_from_file (const char *filename
)
190 char *testfile
, *mimetype
, *flags
;
191 char *rest
, *space
, end
;
193 int xfail_name
, xfail_data
, xfail_file
;
195 file
= fopen (filename
, "r");
196 tmp
= strdup (filename
);
197 dir
= strdup (dirname (tmp
));
202 printf ("Could not open %s\n", filename
);
206 while (fgets (line
, 255, file
) != NULL
)
211 while (*rest
== ' ') rest
++;
213 if (*rest
== '#' || *rest
== '\n')
216 space
= strchr (rest
, ' ');
219 printf ("Malformed line in %s:%d\n\t%s\n", filename
, lineno
, line
);
227 while (*rest
== ' ') rest
++;
229 while (*space
!= ' ' && *space
!= '\n') space
++;
242 while (*rest
== ' ') rest
++;
244 while (*space
!= ' ' && *space
!= '\n') space
++;
250 switch (strlen (flags
))
253 printf ("%s.%d: Extra flags are ignored\n", filename
, lineno
);
268 /* Should not happen */
272 test_single_file (dir
, testfile
, mimetype
,
273 xfail_name
, xfail_data
, xfail_file
);
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");
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");
299 main (int argc
, char *argv
[])
306 for (i
= 1; i
< argc
; i
++)
308 if (strcmp (argv
[i
], "-v") == 0)
311 read_from_file (argv
[i
]);
314 if (error
> 0 || failed
> 0)
316 printf ("%d errors, %d comparisons, %d failed",
317 error
, total
, failed
);
319 printf (" (%d expected)", xfailed
);
321 printf (", %d unexpected successes", xmatch
);
326 if (xfailed
== failed
)