Fixed use-after-free in xdgmime
[rox-filer.git] / src / test-mime.c
blob716bab499dc81d7712a7f74d2904869b2804f4ed
1 /*
2 * Copyright (C) 2003,2004 Red Hat, Inc.
3 * Copyright (C) 2003,2004 Jonathan Blandford <jrb@alum.mit.edu>
5 * Licensed under the Academic Free License version 2.0
6 * Or under the following terms:
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
23 #include "xdgmime.h"
24 #include "xdgmimeglob.h"
25 #include <string.h>
26 #include <stdio.h>
29 static void
30 test_individual_glob (const char *glob,
31 XdgGlobType expected_type)
33 XdgGlobType test_type;
35 test_type = _xdg_glob_determine_type (glob);
36 if (test_type != expected_type)
38 printf ("Test Failed: %s is of type %s, but %s is expected\n",
39 glob,
40 ((test_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
41 ((test_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_FULL")),
42 ((expected_type == XDG_GLOB_LITERAL)?"XDG_GLOB_LITERAL":
43 ((expected_type == XDG_GLOB_SIMPLE)?"XDG_GLOB_SIMPLE":"XDG_GLOB_COMPLEX")));
47 static void
48 test_glob_type (void)
50 test_individual_glob ("*.gif", XDG_GLOB_SIMPLE);
51 test_individual_glob ("Foo*.gif", XDG_GLOB_FULL);
52 test_individual_glob ("*[4].gif", XDG_GLOB_FULL);
53 test_individual_glob ("Makefile", XDG_GLOB_LITERAL);
54 test_individual_glob ("sldkfjvlsdf\\\\slkdjf", XDG_GLOB_FULL);
55 test_individual_glob ("tree.[ch]", XDG_GLOB_FULL);
58 static void
59 test_alias (const char *mime_a,
60 const char *mime_b,
61 int expected)
63 int actual;
65 actual = xdg_mime_mime_type_equal (mime_a, mime_b);
67 if (actual != expected)
69 printf ("Test Failed: %s is %s to %s\n",
70 mime_a, actual ? "equal" : "not equal", mime_b);
74 static void
75 test_aliasing (void)
77 test_alias ("application/wordperfect", "application/vnd.wordperfect", 1);
78 test_alias ("application/x-gnome-app-info", "application/x-desktop", 1);
79 test_alias ("application/x-wordperfect", "application/vnd.wordperfect", 1);
80 test_alias ("application/x-wordperfect", "audio/x-midi", 0);
81 test_alias ("/", "vnd/vnd", 0);
82 test_alias ("application/octet-stream", "text/plain", 0);
83 test_alias ("text/plain", "text/*", 0);
86 static void
87 test_subclass (const char *mime_a,
88 const char *mime_b,
89 int expected)
91 int actual;
93 actual = xdg_mime_mime_type_subclass (mime_a, mime_b);
95 if (actual != expected)
97 printf ("Test Failed: %s is %s of %s\n",
98 mime_a, actual ? "subclass" : "not subclass", mime_b);
102 static void
103 test_subclassing (void)
105 test_subclass ("application/rtf", "text/plain", 1);
106 test_subclass ("message/news", "text/plain", 1);
107 test_subclass ("message/news", "message/*", 1);
108 test_subclass ("message/news", "text/*", 1);
109 test_subclass ("message/news", "application/octet-stream", 1);
110 test_subclass ("application/rtf", "application/octet-stream", 1);
111 test_subclass ("application/x-gnome-app-info", "text/plain", 1);
112 test_subclass ("image/x-djvu", "image/vnd.djvu", 1);
113 test_subclass ("image/vnd.djvu", "image/x-djvu", 1);
114 test_subclass ("image/vnd.djvu", "text/plain", 0);
115 test_subclass ("image/vnd.djvu", "text/*", 0);
116 test_subclass ("text/*", "text/plain", 1);
119 static void
120 test_one_match (const char *filename, const char *expected)
122 const char *actual;
124 actual = xdg_mime_get_mime_type_from_file_name (filename);
126 if (strcmp (actual, expected) != 0)
128 printf ("Test Failed: mime type of %s is %s, expected %s\n",
129 filename, actual, expected);
133 static void
134 test_matches (void)
136 test_one_match ("foo.bar.epub", "application/epub+zip");
137 test_one_match ("core", "application/x-core");
138 test_one_match ("README.in", "text/x-readme");
139 test_one_match ("README.gz", "application/x-gzip");
140 test_one_match ("blabla.cs", "text/x-csharp");
141 test_one_match ("blabla.f90", "text/x-fortran");
142 test_one_match ("blabla.F95", "text/x-fortran");
143 test_one_match ("tarball.tar.gz", "application/x-compressed-tar");
144 test_one_match ("file.gz", "application/x-gzip");
145 test_one_match ("file.tar.lzo", "application/x-tzo");
146 test_one_match ("file.lzo", "application/x-lzop");
149 static void
150 test_one_icon (const char *mimetype, const char *expected)
152 const char *actual;
154 actual = xdg_mime_get_icon (mimetype);
156 if (actual != expected && (actual == NULL || strcmp (actual, expected) != 0))
158 printf ("Test Failed: icon of %s is %s, expected %s\n",
159 mimetype, actual, expected);
163 static void
164 test_icons (void)
166 test_one_icon ("application/x-font-ttx", "font-x-generic");
167 test_one_icon ("application/mathematica", "x-office-document");
168 test_one_icon ("text/plain", NULL);
172 main (int argc, char *argv[])
174 const char *result;
175 const char *file_name;
176 int i;
178 test_glob_type ();
179 test_aliasing ();
180 test_subclassing ();
181 test_matches ();
182 test_icons ();
184 for (i = 1; i < argc; i++)
186 file_name = argv[i];
187 result = xdg_mime_get_mime_type_for_file (file_name, NULL);
188 printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
191 #if 0
192 xdg_mime_dump ();
193 #endif
194 return 0;