]>
git.proxmox.com Git - libxdgmime-perl.git/blob - 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 * SPDX-License-Identifier: LGPL-2.1-or-later or AFL-2.0
21 static int failed
= 0;
22 static int xfailed
= 0;
23 static int xmatch
= 0;
25 static int verbose
= 0;
28 check_mime_type (const char *mt
,
29 const char *mt_expected
,
36 if (strcasecmp (mt
, mt_expected
) != 0)
45 printf ("%s, '%s' test: expected %s, got %s (expected failure)\n",
46 filename
, test
, mt_expected
, mt
);
51 fprintf (stderr
, "%s, '%s' test: expected %s, got %s\n",
52 filename
, test
, mt_expected
, mt
);
63 printf ("%s, '%s' test: got %s (unexpected match)\n",
71 test_by_name (const char *filename
,
72 const char *mt_expected
,
77 mt
= xdg_mime_get_mime_type_from_file_name (filename
);
79 check_mime_type (mt
, mt_expected
, xfail
, "name", filename
);
83 test_by_data (const char *dir
,
85 const char *mt_expected
,
96 snprintf (path
, 1024, "%s/%s", dir
, filename
);
98 file
= fopen (path
, "r");
102 fprintf (stderr
, "Could not open %s\n", path
);
108 max_extent
= xdg_mime_get_max_buffer_extents ();
109 data
= malloc (max_extent
);
113 fprintf (stderr
, "Failed to allocate memory for file %s\n", filename
);
120 bytes_read
= fread (data
, 1, max_extent
, file
);
124 fprintf (stderr
, "Error reading file %s\n", path
);
133 mt
= xdg_mime_get_mime_type_for_data (data
, bytes_read
, &result_prio
);
138 check_mime_type (mt
, mt_expected
, xfail
, "data", filename
);
142 test_by_file (const char *dir
,
143 const char *filename
,
144 const char *mt_expected
,
150 snprintf (path
, 1024, "%s/%s", dir
, filename
);
152 mt
= xdg_mime_get_mime_type_for_file (path
, NULL
);
154 check_mime_type (mt
, mt_expected
, xfail
, "file", filename
);
158 test_single_file (const char *dir
,
159 const char *filename
,
160 const char *mimetype
,
165 test_by_name (filename
, mimetype
, xfail_name
);
166 test_by_data (dir
, filename
, mimetype
, xfail_data
);
167 test_by_file (dir
, filename
, mimetype
, xfail_file
);
171 read_from_file (const char *filename
)
176 char *testfile
, *mimetype
, *flags
;
177 char *rest
, *space
, end
;
179 int xfail_name
, xfail_data
, xfail_file
;
181 file
= fopen (filename
, "r");
182 tmp
= strdup (filename
);
183 dir
= strdup (dirname (tmp
));
188 fprintf (stderr
, "Could not open %s\n", filename
);
192 while (fgets (line
, 255, file
) != NULL
)
197 while (*rest
== ' ') rest
++;
199 if (*rest
== '#' || *rest
== '\n')
202 space
= strchr (rest
, ' ');
205 fprintf (stderr
, "Malformed line in %s:%d\n\t%s\n", filename
, lineno
, line
);
213 while (*rest
== ' ') rest
++;
215 while (*space
!= ' ' && *space
!= '\n') space
++;
228 while (*rest
== ' ') rest
++;
230 while (*space
!= ' ' && *space
!= '\n') space
++;
236 switch (strlen (flags
))
239 printf ("%s.%d: Extra flags are ignored\n", filename
, lineno
);
254 /* Should not happen */
258 test_single_file (dir
, testfile
, mimetype
,
259 xfail_name
, xfail_data
, xfail_file
);
270 printf ("usage: test-mime-data <FILE>\n\n");
271 printf ("Tests the mime system.\n");
272 printf ("Testcases are specified in the following format:\n\n");
273 printf ("# comment\n");
274 printf ("<filename1> <mimetype> [<flags>]\n");
275 printf ("<filename2> <mimetype> [<flags>]\n");
277 printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n");
278 printf ("indicates an expected failure when determining the\n");
279 printf ("mimetype by name, data or file.\n");
285 main (int argc
, char *argv
[])
292 for (i
= 1; i
< argc
; i
++)
294 if (strcmp (argv
[i
], "-v") == 0)
297 read_from_file (argv
[i
]);
300 printf ("%d errors, %d comparisons, %d failed",
301 error
, total
, failed
);
303 printf (" (%d expected)", xfailed
);
305 printf (", %d unexpected successes", xmatch
);
308 if (error
> 0 || xmatch
> 0 || failed
!= xfailed
)