• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gio/gio.h>
2 #include <string.h>
3 
4 #define g_assert_content_type_equals(s1, s2) 			\
5   do { 								\
6     const char *__s1 = (s1), *__s2 = (s2); 			\
7     if (g_content_type_equals (__s1, __s2)) ;		 	\
8     else 							\
9       g_assertion_message_cmpstr (G_LOG_DOMAIN, 		\
10                                   __FILE__, __LINE__, 		\
11                                   G_STRFUNC, 			\
12                                   #s1 " == " #s2, 		\
13                                   __s1, " == ", __s2); 		\
14   } while (0)
15 
16 static void
test_guess(void)17 test_guess (void)
18 {
19   gchar *res;
20   gchar *expected;
21   gchar *existing_directory;
22   gboolean uncertain;
23   guchar data[] =
24     "[Desktop Entry]\n"
25     "Type=Application\n"
26     "Name=appinfo-test\n"
27     "Exec=./appinfo-test --option\n";
28 
29 #ifdef G_OS_WIN32
30   existing_directory = (gchar *) g_getenv ("SYSTEMROOT");
31 
32   if (existing_directory)
33     existing_directory = g_strdup_printf ("%s/", existing_directory);
34 #else
35   existing_directory = g_strdup ("/etc/");
36 #endif
37 
38   res = g_content_type_guess (existing_directory, NULL, 0, &uncertain);
39   g_free (existing_directory);
40   expected = g_content_type_from_mime_type ("inode/directory");
41   g_assert_content_type_equals (expected, res);
42   g_assert_true (uncertain);
43   g_free (res);
44   g_free (expected);
45 
46   res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
47   expected = g_content_type_from_mime_type ("text/plain");
48   g_assert_content_type_equals (expected, res);
49   g_free (res);
50   g_free (expected);
51 
52   res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain);
53   expected = g_content_type_from_mime_type ("text/plain");
54   g_assert_content_type_equals (expected, res);
55   g_assert_false (uncertain);
56   g_free (res);
57   g_free (expected);
58 
59   /* Sadly OSX just doesn't have as large and robust of a mime type database as Linux */
60 #ifndef __APPLE__
61   res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
62   expected = g_content_type_from_mime_type ("text/plain");
63   g_assert_content_type_equals (expected, res);
64   g_assert_false (uncertain);
65   g_free (res);
66   g_free (expected);
67 
68   res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
69   expected = g_content_type_from_mime_type ("application/x-desktop");
70   g_assert_content_type_equals (expected, res);
71   g_assert_false (uncertain);
72   g_free (res);
73   g_free (expected);
74 
75   res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
76   expected = g_content_type_from_mime_type ("application/x-desktop");
77   g_assert_content_type_equals (expected, res);
78   g_assert_false (uncertain);
79   g_free (res);
80   g_free (expected);
81 
82   /* this is potentially ambiguous: it does not match the PO template format,
83    * but looks like text so it can't be Powerpoint */
84   res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
85   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
86   g_assert_content_type_equals (expected, res);
87   g_assert_false (uncertain);
88   g_free (res);
89   g_free (expected);
90 
91   res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
92   expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
93   g_assert_content_type_equals (expected, res);
94   g_assert_false (uncertain);
95   g_free (res);
96   g_free (expected);
97 
98   res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
99   expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
100   g_assert_content_type_equals (expected, res);
101   /* we cannot reliably detect binary powerpoint files as long as there is no
102    * defined MIME magic, so do not check uncertain here
103    */
104   g_free (res);
105   g_free (expected);
106 
107   res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
108   expected = g_content_type_from_mime_type ("application/x-font-otf");
109   g_assert_content_type_equals (expected, res);
110   g_assert_false (uncertain);
111   g_free (res);
112   g_free (expected);
113 #endif
114 
115   res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
116   expected = g_content_type_from_mime_type ("image/x-eps");
117   g_assert_content_type_equals (expected, res);
118   g_assert_false (uncertain);
119   g_free (res);
120   g_free (expected);
121 
122   /* The data below would be detected as a valid content type, but shouldn’t be read at all. */
123   res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 0, &uncertain);
124   expected = g_content_type_from_mime_type ("application/x-zerosize");
125   g_assert_content_type_equals (expected, res);
126   g_assert_false (uncertain);
127   g_free (res);
128   g_free (expected);
129 }
130 
131 static void
test_unknown(void)132 test_unknown (void)
133 {
134   gchar *unknown;
135   gchar *str;
136 
137   unknown = g_content_type_from_mime_type ("application/octet-stream");
138   g_assert_true (g_content_type_is_unknown (unknown));
139   str = g_content_type_get_mime_type (unknown);
140   g_assert_cmpstr (str, ==, "application/octet-stream");
141   g_free (str);
142   g_free (unknown);
143 }
144 
145 static void
test_subtype(void)146 test_subtype (void)
147 {
148   gchar *plain;
149   gchar *xml;
150 
151   plain = g_content_type_from_mime_type ("text/plain");
152   xml = g_content_type_from_mime_type ("application/xml");
153 
154   g_assert_true (g_content_type_is_a (xml, plain));
155   g_assert_true (g_content_type_is_mime_type (xml, "text/plain"));
156 
157   g_free (plain);
158   g_free (xml);
159 }
160 
161 static gint
find_mime(gconstpointer a,gconstpointer b)162 find_mime (gconstpointer a, gconstpointer b)
163 {
164   if (g_content_type_equals (a, b))
165     return 0;
166   return 1;
167 }
168 
169 static void
test_list(void)170 test_list (void)
171 {
172   GList *types;
173   gchar *plain;
174   gchar *xml;
175 
176 #ifdef __APPLE__
177   g_test_skip ("The OSX backend does not implement g_content_types_get_registered()");
178   return;
179 #endif
180 
181   plain = g_content_type_from_mime_type ("text/plain");
182   xml = g_content_type_from_mime_type ("application/xml");
183 
184   types = g_content_types_get_registered ();
185 
186   g_assert_cmpuint (g_list_length (types), >, 1);
187 
188   /* just check that some types are in the list */
189   g_assert_nonnull (g_list_find_custom (types, plain, find_mime));
190   g_assert_nonnull (g_list_find_custom (types, xml, find_mime));
191 
192   g_list_free_full (types, g_free);
193 
194   g_free (plain);
195   g_free (xml);
196 }
197 
198 static void
test_executable(void)199 test_executable (void)
200 {
201   gchar *type;
202 
203   type = g_content_type_from_mime_type ("application/x-executable");
204   g_assert_true (g_content_type_can_be_executable (type));
205   g_free (type);
206 
207   type = g_content_type_from_mime_type ("text/plain");
208   g_assert_true (g_content_type_can_be_executable (type));
209   g_free (type);
210 
211   type = g_content_type_from_mime_type ("image/png");
212   g_assert_false (g_content_type_can_be_executable (type));
213   g_free (type);
214 }
215 
216 static void
test_description(void)217 test_description (void)
218 {
219   gchar *type;
220   gchar *desc;
221 
222   type = g_content_type_from_mime_type ("text/plain");
223   desc = g_content_type_get_description (type);
224   g_assert_nonnull (desc);
225 
226   g_free (desc);
227   g_free (type);
228 }
229 
230 static void
test_icon(void)231 test_icon (void)
232 {
233   gchar *type;
234   GIcon *icon;
235 
236   type = g_content_type_from_mime_type ("text/plain");
237   icon = g_content_type_get_icon (type);
238   g_assert_true (G_IS_ICON (icon));
239   if (G_IS_THEMED_ICON (icon))
240     {
241       const gchar *const *names;
242 
243       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
244 #ifdef __APPLE__
245       g_assert_true (g_strv_contains (names, "text-*"));
246 #else
247       g_assert_true (g_strv_contains (names, "text-plain"));
248       g_assert_true (g_strv_contains (names, "text-x-generic"));
249 #endif
250     }
251   g_object_unref (icon);
252   g_free (type);
253 
254   type = g_content_type_from_mime_type ("application/rtf");
255   icon = g_content_type_get_icon (type);
256   g_assert_true (G_IS_ICON (icon));
257   if (G_IS_THEMED_ICON (icon))
258     {
259       const gchar *const *names;
260 
261       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
262       g_assert_true (g_strv_contains (names, "application-rtf"));
263 #ifndef __APPLE__
264       g_assert_true (g_strv_contains (names, "x-office-document"));
265 #endif
266     }
267   g_object_unref (icon);
268   g_free (type);
269 }
270 
271 static void
test_symbolic_icon(void)272 test_symbolic_icon (void)
273 {
274 #ifndef G_OS_WIN32
275   gchar *type;
276   GIcon *icon;
277 
278   type = g_content_type_from_mime_type ("text/plain");
279   icon = g_content_type_get_symbolic_icon (type);
280   g_assert_true (G_IS_ICON (icon));
281   if (G_IS_THEMED_ICON (icon))
282     {
283       const gchar *const *names;
284 
285       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
286 #ifdef __APPLE__
287       g_assert_true (g_strv_contains (names, "text-*-symbolic"));
288       g_assert_true (g_strv_contains (names, "text-*"));
289 #else
290       g_assert_true (g_strv_contains (names, "text-plain-symbolic"));
291       g_assert_true (g_strv_contains (names, "text-x-generic-symbolic"));
292       g_assert_true (g_strv_contains (names, "text-plain"));
293       g_assert_true (g_strv_contains (names, "text-x-generic"));
294 #endif
295     }
296   g_object_unref (icon);
297   g_free (type);
298 
299   type = g_content_type_from_mime_type ("application/rtf");
300   icon = g_content_type_get_symbolic_icon (type);
301   g_assert_true (G_IS_ICON (icon));
302   if (G_IS_THEMED_ICON (icon))
303     {
304       const gchar *const *names;
305 
306       names = g_themed_icon_get_names (G_THEMED_ICON (icon));
307       g_assert_true (g_strv_contains (names, "application-rtf-symbolic"));
308       g_assert_true (g_strv_contains (names, "application-rtf"));
309 #ifndef __APPLE__
310       g_assert_true (g_strv_contains (names, "x-office-document-symbolic"));
311       g_assert_true (g_strv_contains (names, "x-office-document"));
312 #endif
313     }
314   g_object_unref (icon);
315   g_free (type);
316 #endif
317 }
318 
319 static void
test_tree(void)320 test_tree (void)
321 {
322   const gchar *tests[] = {
323     "x-content/image-dcf",
324     "x-content/unix-software",
325     "x-content/win32-software"
326   };
327   const gchar *path;
328   GFile *file;
329   gchar **types;
330   gint i;
331 
332 #ifdef __APPLE__
333   g_test_skip ("The OSX backend does not implement g_content_type_guess_for_tree()");
334   return;
335 #endif
336 
337   for (i = 0; i < G_N_ELEMENTS (tests); i++)
338     {
339       path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
340       file = g_file_new_for_path (path);
341       types = g_content_type_guess_for_tree (file);
342       g_assert_content_type_equals (types[0], tests[i]);
343       g_strfreev (types);
344       g_object_unref (file);
345    }
346 }
347 
348 static void
test_type_is_a_special_case(void)349 test_type_is_a_special_case (void)
350 {
351   gboolean res;
352 
353   g_test_bug ("782311");
354 
355   /* Everything but the inode type is application/octet-stream */
356   res = g_content_type_is_a ("inode/directory", "application/octet-stream");
357   g_assert_false (res);
358 #ifndef __APPLE__
359   res = g_content_type_is_a ("anything", "application/octet-stream");
360   g_assert_true (res);
361 #endif
362 }
363 
364 static void
test_guess_svg_from_data(void)365 test_guess_svg_from_data (void)
366 {
367   const gchar svgfilecontent[] = "<svg  xmlns=\"http://www.w3.org/2000/svg\"\
368       xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\
369     <rect x=\"10\" y=\"10\" height=\"100\" width=\"100\"\n\
370           style=\"stroke:#ff0000; fill: #0000ff\"/>\n\
371 </svg>\n";
372 
373   gboolean uncertain = TRUE;
374   gchar *res = g_content_type_guess (NULL, (guchar *)svgfilecontent,
375                                      sizeof (svgfilecontent) - 1, &uncertain);
376 #ifdef __APPLE__
377   g_assert_cmpstr (res, ==, "public.svg-image");
378 #elif defined(G_OS_WIN32)
379   g_test_skip ("svg type detection from content is not implemented on WIN32");
380 #else
381   g_assert_cmpstr (res, ==, "image/svg+xml");
382 #endif
383   g_assert_false (uncertain);
384   g_free (res);
385 }
386 
387 static void
test_mime_from_content(void)388 test_mime_from_content (void)
389 {
390 #ifdef __APPLE__
391   gchar *mime_type;
392   mime_type = g_content_type_get_mime_type ("com.microsoft.bmp");
393   g_assert_cmpstr (mime_type, ==, "image/bmp");
394   g_free (mime_type);
395   mime_type = g_content_type_get_mime_type ("com.compuserve.gif");
396   g_assert_cmpstr (mime_type, ==, "image/gif");
397   g_free (mime_type);
398   mime_type = g_content_type_get_mime_type ("public.png");
399   g_assert_cmpstr (mime_type, ==, "image/png");
400   g_free (mime_type);
401   mime_type = g_content_type_get_mime_type ("public.text");
402   g_assert_cmpstr (mime_type, ==, "text/*");
403   g_free (mime_type);
404   mime_type = g_content_type_get_mime_type ("public.svg-image");
405   g_assert_cmpstr (mime_type, ==, "image/svg+xml");
406   g_free (mime_type);
407 #elif defined(G_OS_WIN32)
408   g_test_skip ("mime from content type test not implemented on WIN32");
409 #else
410   g_test_skip ("mime from content type test not implemented on UNIX");
411 #endif
412 }
413 
414 int
main(int argc,char * argv[])415 main (int argc, char *argv[])
416 {
417   g_test_init (&argc, &argv, NULL);
418 
419   g_test_bug_base ("http://bugzilla.gnome.org/");
420 
421   g_test_add_func ("/contenttype/guess", test_guess);
422   g_test_add_func ("/contenttype/guess_svg_from_data", test_guess_svg_from_data);
423   g_test_add_func ("/contenttype/mime_from_content", test_mime_from_content);
424   g_test_add_func ("/contenttype/unknown", test_unknown);
425   g_test_add_func ("/contenttype/subtype", test_subtype);
426   g_test_add_func ("/contenttype/list", test_list);
427   g_test_add_func ("/contenttype/executable", test_executable);
428   g_test_add_func ("/contenttype/description", test_description);
429   g_test_add_func ("/contenttype/icon", test_icon);
430   g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
431   g_test_add_func ("/contenttype/tree", test_tree);
432   g_test_add_func ("/contenttype/test_type_is_a_special_case",
433                    test_type_is_a_special_case);
434 
435   return g_test_run ();
436 }
437