]>
git.proxmox.com Git - libxdgmime-perl.git/blob - xdgmime-source/src/test-mime-data.c
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 (strcasecmp (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
);
135 bytes_read
= fread (data
, 1, max_extent
, file
);
139 printf ("Error reading file %s\n", path
);
148 mt
= xdg_mime_get_mime_type_for_data (data
, bytes_read
, &result_prio
);
153 check_mime_type (mt
, mt_expected
, xfail
, "data", filename
);
157 test_by_file (const char *dir
,
158 const char *filename
,
159 const char *mt_expected
,
165 snprintf (path
, 1024, "%s/%s", dir
, filename
);
167 mt
= xdg_mime_get_mime_type_for_file (path
, NULL
);
169 check_mime_type (mt
, mt_expected
, xfail
, "file", filename
);
173 test_single_file (const char *dir
,
174 const char *filename
,
175 const char *mimetype
,
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
);
186 read_from_file (const char *filename
)
191 char *testfile
, *mimetype
, *flags
;
192 char *rest
, *space
, end
;
194 int xfail_name
, xfail_data
, xfail_file
;
196 file
= fopen (filename
, "r");
197 tmp
= strdup (filename
);
198 dir
= strdup (dirname (tmp
));
203 printf ("Could not open %s\n", filename
);
207 while (fgets (line
, 255, file
) != NULL
)
212 while (*rest
== ' ') rest
++;
214 if (*rest
== '#' || *rest
== '\n')
217 space
= strchr (rest
, ' ');
220 printf ("Malformed line in %s:%d\n\t%s\n", filename
, lineno
, line
);
228 while (*rest
== ' ') rest
++;
230 while (*space
!= ' ' && *space
!= '\n') space
++;
243 while (*rest
== ' ') rest
++;
245 while (*space
!= ' ' && *space
!= '\n') space
++;
251 switch (strlen (flags
))
254 printf ("%s.%d: Extra flags are ignored\n", filename
, lineno
);
269 /* Should not happen */
273 test_single_file (dir
, testfile
, mimetype
,
274 xfail_name
, xfail_data
, xfail_file
);
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");
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");
300 main (int argc
, char *argv
[])
307 for (i
= 1; i
< argc
; i
++)
309 if (strcmp (argv
[i
], "-v") == 0)
312 read_from_file (argv
[i
]);
315 if (error
> 0 || failed
> 0)
317 printf ("%d errors, %d comparisons, %d failed",
318 error
, total
, failed
);
320 printf (" (%d expected)", xfailed
);
322 printf (", %d unexpected successes", xmatch
);
327 if (xfailed
== failed
)