2010-07-05 04:27:01 +02:00
|
|
|
|
#include <gio/gio.h>
|
2012-05-01 21:40:15 +02:00
|
|
|
|
#include <string.h>
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
2013-12-21 17:56:24 +01:00
|
|
|
|
#define g_assert_content_type_equals(s1, s2) \
|
|
|
|
|
do { \
|
|
|
|
|
const char *__s1 = (s1), *__s2 = (s2); \
|
|
|
|
|
if (g_content_type_equals (__s1, __s2)) ; \
|
|
|
|
|
else \
|
|
|
|
|
g_assertion_message_cmpstr (G_LOG_DOMAIN, \
|
|
|
|
|
__FILE__, __LINE__, \
|
|
|
|
|
G_STRFUNC, \
|
|
|
|
|
#s1 " == " #s2, \
|
|
|
|
|
__s1, " == ", __s2); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2010-07-05 04:27:01 +02:00
|
|
|
|
static void
|
|
|
|
|
test_guess (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *res;
|
|
|
|
|
gchar *expected;
|
2015-05-03 01:46:06 +02:00
|
|
|
|
gchar *existing_directory;
|
2010-07-05 04:27:01 +02:00
|
|
|
|
gboolean uncertain;
|
2012-05-01 21:40:15 +02:00
|
|
|
|
guchar data[] =
|
2012-04-08 16:17:32 +02:00
|
|
|
|
"[Desktop Entry]\n"
|
|
|
|
|
"Type=Application\n"
|
|
|
|
|
"Name=appinfo-test\n"
|
|
|
|
|
"Exec=./appinfo-test --option\n";
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
2015-05-03 01:46:06 +02:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
existing_directory = (gchar *) g_getenv ("SYSTEMROOT");
|
|
|
|
|
|
|
|
|
|
if (existing_directory)
|
2022-02-16 12:25:09 +01:00
|
|
|
|
existing_directory = g_strdup_printf ("%s" G_DIR_SEPARATOR_S, existing_directory);
|
2015-05-03 01:46:06 +02:00
|
|
|
|
#else
|
|
|
|
|
existing_directory = g_strdup ("/etc/");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
res = g_content_type_guess (existing_directory, NULL, 0, &uncertain);
|
|
|
|
|
g_free (existing_directory);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("inode/directory");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (uncertain);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
|
|
|
|
res = g_content_type_guess ("foo.txt", NULL, 0, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("text/plain");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2017-03-22 06:37:43 +01:00
|
|
|
|
res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("text/plain");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2022-02-16 12:27:12 +01:00
|
|
|
|
/* Sadly win32 & OSX just don't have as large and robust of a mime type database as Linux */
|
|
|
|
|
#ifndef G_OS_WIN32
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifndef __APPLE__
|
2017-03-22 06:37:43 +01:00
|
|
|
|
res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("text/plain");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2017-03-22 06:37:43 +01:00
|
|
|
|
res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("application/x-desktop");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2012-05-01 21:40:15 +02:00
|
|
|
|
res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("application/x-desktop");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2012-06-27 16:30:38 +02:00
|
|
|
|
/* this is potentially ambiguous: it does not match the PO template format,
|
|
|
|
|
* but looks like text so it can't be Powerpoint */
|
2012-06-27 09:25:37 +02:00
|
|
|
|
res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain);
|
2012-06-27 16:30:38 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-06-27 16:30:38 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
|
|
|
|
res = g_content_type_guess ("test.pot", (guchar *)"msgid \"", 7, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("text/x-gettext-translation-template");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2012-06-27 16:30:38 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
|
|
|
|
res = g_content_type_guess ("test.pot", (guchar *)"\xCF\xD0\xE0\x11", 4, &uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("application/vnd.ms-powerpoint");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2012-06-27 16:30:38 +02:00
|
|
|
|
/* we cannot reliably detect binary powerpoint files as long as there is no
|
2013-12-21 17:56:24 +01:00
|
|
|
|
* defined MIME magic, so do not check uncertain here
|
|
|
|
|
*/
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
|
|
|
|
|
2012-06-27 09:25:37 +02:00
|
|
|
|
res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
expected = g_content_type_from_mime_type ("application/x-font-otf");
|
2013-12-21 17:56:24 +01:00
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#endif /* __APPLE__ */
|
2013-12-23 18:11:03 +01:00
|
|
|
|
|
|
|
|
|
res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("image/x-eps");
|
|
|
|
|
g_assert_content_type_equals (expected, res);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (uncertain);
|
2013-12-23 18:11:03 +01:00
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
2019-03-19 12:18:48 +01:00
|
|
|
|
|
|
|
|
|
/* The data below would be detected as a valid content type, but shouldn’t be read at all. */
|
|
|
|
|
res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 0, &uncertain);
|
|
|
|
|
expected = g_content_type_from_mime_type ("application/x-zerosize");
|
|
|
|
|
g_assert_content_type_equals (expected, res);
|
|
|
|
|
g_assert_false (uncertain);
|
|
|
|
|
g_free (res);
|
|
|
|
|
g_free (expected);
|
2022-02-16 12:27:12 +01:00
|
|
|
|
#endif /* G_OS_WIN32 */
|
2010-07-05 04:27:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_unknown (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *unknown;
|
|
|
|
|
gchar *str;
|
|
|
|
|
|
|
|
|
|
unknown = g_content_type_from_mime_type ("application/octet-stream");
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_content_type_is_unknown (unknown));
|
2010-07-05 04:27:01 +02:00
|
|
|
|
str = g_content_type_get_mime_type (unknown);
|
|
|
|
|
g_assert_cmpstr (str, ==, "application/octet-stream");
|
|
|
|
|
g_free (str);
|
|
|
|
|
g_free (unknown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_subtype (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *plain;
|
|
|
|
|
gchar *xml;
|
|
|
|
|
|
|
|
|
|
plain = g_content_type_from_mime_type ("text/plain");
|
|
|
|
|
xml = g_content_type_from_mime_type ("application/xml");
|
|
|
|
|
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_content_type_is_a (xml, plain));
|
|
|
|
|
g_assert_true (g_content_type_is_mime_type (xml, "text/plain"));
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
|
|
|
|
g_free (plain);
|
|
|
|
|
g_free (xml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
find_mime (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
if (g_content_type_equals (a, b))
|
|
|
|
|
return 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_list (void)
|
|
|
|
|
{
|
|
|
|
|
GList *types;
|
|
|
|
|
gchar *plain;
|
|
|
|
|
gchar *xml;
|
|
|
|
|
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifdef __APPLE__
|
2017-03-22 06:37:43 +01:00
|
|
|
|
g_test_skip ("The OSX backend does not implement g_content_types_get_registered()");
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-07-05 04:27:01 +02:00
|
|
|
|
plain = g_content_type_from_mime_type ("text/plain");
|
|
|
|
|
xml = g_content_type_from_mime_type ("application/xml");
|
|
|
|
|
|
|
|
|
|
types = g_content_types_get_registered ();
|
|
|
|
|
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_cmpuint (g_list_length (types), >, 1);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
|
|
|
|
/* just check that some types are in the list */
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_nonnull (g_list_find_custom (types, plain, find_mime));
|
|
|
|
|
g_assert_nonnull (g_list_find_custom (types, xml, find_mime));
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
2012-01-02 15:20:42 +01:00
|
|
|
|
g_list_free_full (types, g_free);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
|
|
|
|
g_free (plain);
|
|
|
|
|
g_free (xml);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_executable (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *type;
|
|
|
|
|
|
2022-01-21 22:32:52 +01:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
type = g_content_type_from_mime_type ("application/vnd.microsoft.portable-executable");
|
|
|
|
|
/* FIXME: the MIME is not in the default `MIME\Database\Content Type` registry.
|
|
|
|
|
* g_assert_true (g_content_type_can_be_executable (type));
|
|
|
|
|
*/
|
|
|
|
|
g_free (type);
|
|
|
|
|
#else
|
2010-07-05 04:27:01 +02:00
|
|
|
|
type = g_content_type_from_mime_type ("application/x-executable");
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_content_type_can_be_executable (type));
|
2010-07-05 04:27:01 +02:00
|
|
|
|
g_free (type);
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("text/plain");
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_content_type_can_be_executable (type));
|
2010-07-05 04:27:01 +02:00
|
|
|
|
g_free (type);
|
2022-01-21 22:32:52 +01:00
|
|
|
|
#endif
|
2010-07-05 09:09:36 +02:00
|
|
|
|
type = g_content_type_from_mime_type ("image/png");
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_false (g_content_type_can_be_executable (type));
|
2010-07-05 09:09:36 +02:00
|
|
|
|
g_free (type);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 05:42:07 +02:00
|
|
|
|
static void
|
|
|
|
|
test_description (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *type;
|
|
|
|
|
gchar *desc;
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("text/plain");
|
|
|
|
|
desc = g_content_type_get_description (type);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_nonnull (desc);
|
2010-07-05 05:42:07 +02:00
|
|
|
|
|
|
|
|
|
g_free (desc);
|
|
|
|
|
g_free (type);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-08 16:17:32 +02:00
|
|
|
|
static void
|
|
|
|
|
test_icon (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *type;
|
|
|
|
|
GIcon *icon;
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("text/plain");
|
|
|
|
|
icon = g_content_type_get_icon (type);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (G_IS_ICON (icon));
|
2014-02-23 18:03:13 +01:00
|
|
|
|
if (G_IS_THEMED_ICON (icon))
|
|
|
|
|
{
|
|
|
|
|
const gchar *const *names;
|
|
|
|
|
|
|
|
|
|
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifdef __APPLE__
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "text-*"));
|
2022-10-14 15:09:55 +02:00
|
|
|
|
#elif defined(G_OS_WIN32)
|
|
|
|
|
g_assert_cmpuint (g_strv_length ((GStrv) names), >, 0);
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#else
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "text-plain"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-x-generic"));
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#endif
|
2014-02-23 18:03:13 +01:00
|
|
|
|
}
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_object_unref (icon);
|
|
|
|
|
g_free (type);
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("application/rtf");
|
|
|
|
|
icon = g_content_type_get_icon (type);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (G_IS_ICON (icon));
|
2014-02-23 18:03:13 +01:00
|
|
|
|
if (G_IS_THEMED_ICON (icon))
|
|
|
|
|
{
|
|
|
|
|
const gchar *const *names;
|
|
|
|
|
|
|
|
|
|
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
|
2022-02-16 12:39:39 +01:00
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-x-generic"));
|
|
|
|
|
#else
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "application-rtf"));
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifndef __APPLE__
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "x-office-document"));
|
2022-02-16 12:39:39 +01:00
|
|
|
|
#endif
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#endif
|
2014-02-23 18:03:13 +01:00
|
|
|
|
}
|
|
|
|
|
g_object_unref (icon);
|
|
|
|
|
g_free (type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_symbolic_icon (void)
|
|
|
|
|
{
|
2021-09-27 14:11:21 +02:00
|
|
|
|
#ifndef G_OS_WIN32
|
2014-02-23 18:03:13 +01:00
|
|
|
|
gchar *type;
|
|
|
|
|
GIcon *icon;
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("text/plain");
|
|
|
|
|
icon = g_content_type_get_symbolic_icon (type);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (G_IS_ICON (icon));
|
2014-02-23 18:03:13 +01:00
|
|
|
|
if (G_IS_THEMED_ICON (icon))
|
|
|
|
|
{
|
|
|
|
|
const gchar *const *names;
|
|
|
|
|
|
|
|
|
|
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifdef __APPLE__
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "text-*-symbolic"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-*"));
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#else
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "text-plain-symbolic"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-x-generic-symbolic"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-plain"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "text-x-generic"));
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#endif
|
2014-02-23 18:03:13 +01:00
|
|
|
|
}
|
|
|
|
|
g_object_unref (icon);
|
|
|
|
|
g_free (type);
|
|
|
|
|
|
|
|
|
|
type = g_content_type_from_mime_type ("application/rtf");
|
|
|
|
|
icon = g_content_type_get_symbolic_icon (type);
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (G_IS_ICON (icon));
|
2014-02-23 18:03:13 +01:00
|
|
|
|
if (G_IS_THEMED_ICON (icon))
|
|
|
|
|
{
|
|
|
|
|
const gchar *const *names;
|
|
|
|
|
|
|
|
|
|
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "application-rtf-symbolic"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "application-rtf"));
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifndef __APPLE__
|
2019-03-19 12:18:07 +01:00
|
|
|
|
g_assert_true (g_strv_contains (names, "x-office-document-symbolic"));
|
|
|
|
|
g_assert_true (g_strv_contains (names, "x-office-document"));
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#endif
|
2014-02-23 18:03:13 +01:00
|
|
|
|
}
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_object_unref (icon);
|
|
|
|
|
g_free (type);
|
2015-05-03 01:46:06 +02:00
|
|
|
|
#endif
|
2012-04-08 16:17:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 18:11:03 +01:00
|
|
|
|
static void
|
|
|
|
|
test_tree (void)
|
|
|
|
|
{
|
|
|
|
|
const gchar *tests[] = {
|
|
|
|
|
"x-content/image-dcf",
|
|
|
|
|
"x-content/unix-software",
|
|
|
|
|
"x-content/win32-software"
|
|
|
|
|
};
|
|
|
|
|
const gchar *path;
|
|
|
|
|
GFile *file;
|
|
|
|
|
gchar **types;
|
2020-11-17 23:43:09 +01:00
|
|
|
|
gsize i;
|
2013-12-23 18:11:03 +01:00
|
|
|
|
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#if defined(__APPLE__) || defined(G_OS_WIN32)
|
2022-01-21 22:48:13 +01:00
|
|
|
|
g_test_skip ("The OSX & Windows backends do not implement g_content_type_guess_for_tree()");
|
2017-03-22 06:37:43 +01:00
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-12-23 18:11:03 +01:00
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (tests); i++)
|
|
|
|
|
{
|
|
|
|
|
path = g_test_get_filename (G_TEST_DIST, tests[i], NULL);
|
|
|
|
|
file = g_file_new_for_path (path);
|
|
|
|
|
types = g_content_type_guess_for_tree (file);
|
|
|
|
|
g_assert_content_type_equals (types[0], tests[i]);
|
|
|
|
|
g_strfreev (types);
|
|
|
|
|
g_object_unref (file);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-08 16:17:32 +02:00
|
|
|
|
|
2023-11-13 13:14:01 +01:00
|
|
|
|
static void
|
|
|
|
|
test_tree_invalid_encoding (void)
|
|
|
|
|
{
|
|
|
|
|
gchar *path;
|
|
|
|
|
gchar *name;
|
|
|
|
|
GFile *tmpdir;
|
|
|
|
|
GFile *file;
|
|
|
|
|
gchar **types;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3168");
|
|
|
|
|
|
|
|
|
|
#if defined(__APPLE__) || defined(G_OS_WIN32)
|
|
|
|
|
g_test_skip ("The OSX & Windows backends do not implement g_content_type_guess_for_tree()");
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
path = g_dir_make_tmp ("gio-test-tree-invalid-encoding-XXXXXX", &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
tmpdir = g_file_new_for_path (path);
|
|
|
|
|
g_free (path);
|
|
|
|
|
|
|
|
|
|
name = g_strdup_printf ("\260");
|
|
|
|
|
file = g_file_get_child (tmpdir, name);
|
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
|
|
g_file_replace_contents (file, "", 0, NULL, FALSE, 0, NULL, NULL, &error);
|
|
|
|
|
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT))
|
|
|
|
|
{
|
|
|
|
|
g_test_skip ("Unable to create testing file with non-ASCII characters.");
|
|
|
|
|
|
|
|
|
|
g_object_unref (tmpdir);
|
|
|
|
|
g_object_unref (file);
|
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
|
|
|
|
types = g_content_type_guess_for_tree (tmpdir);
|
|
|
|
|
g_strfreev (types);
|
|
|
|
|
|
|
|
|
|
g_file_delete (file, NULL, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
g_object_unref (file);
|
|
|
|
|
|
|
|
|
|
g_file_delete (tmpdir, NULL, &error);
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
g_object_unref (tmpdir);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 18:09:35 +02:00
|
|
|
|
static void
|
|
|
|
|
test_type_is_a_special_case (void)
|
|
|
|
|
{
|
|
|
|
|
gboolean res;
|
|
|
|
|
|
2021-05-13 23:12:29 +02:00
|
|
|
|
g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=782311");
|
2017-05-10 18:09:35 +02:00
|
|
|
|
|
|
|
|
|
/* Everything but the inode type is application/octet-stream */
|
|
|
|
|
res = g_content_type_is_a ("inode/directory", "application/octet-stream");
|
|
|
|
|
g_assert_false (res);
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#if !defined(__APPLE__) && !defined(G_OS_WIN32)
|
2017-05-10 18:09:35 +02:00
|
|
|
|
res = g_content_type_is_a ("anything", "application/octet-stream");
|
|
|
|
|
g_assert_true (res);
|
2017-10-27 23:45:14 +02:00
|
|
|
|
#endif
|
2017-05-10 18:09:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-11 15:59:16 +02:00
|
|
|
|
static void
|
|
|
|
|
test_guess_svg_from_data (void)
|
|
|
|
|
{
|
|
|
|
|
const gchar svgfilecontent[] = "<svg xmlns=\"http://www.w3.org/2000/svg\"\
|
|
|
|
|
xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n\
|
|
|
|
|
<rect x=\"10\" y=\"10\" height=\"100\" width=\"100\"\n\
|
|
|
|
|
style=\"stroke:#ff0000; fill: #0000ff\"/>\n\
|
|
|
|
|
</svg>\n";
|
|
|
|
|
|
|
|
|
|
gboolean uncertain = TRUE;
|
|
|
|
|
gchar *res = g_content_type_guess (NULL, (guchar *)svgfilecontent,
|
|
|
|
|
sizeof (svgfilecontent) - 1, &uncertain);
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifdef __APPLE__
|
2017-10-11 15:59:16 +02:00
|
|
|
|
g_assert_cmpstr (res, ==, "public.svg-image");
|
|
|
|
|
#elif defined(G_OS_WIN32)
|
|
|
|
|
g_test_skip ("svg type detection from content is not implemented on WIN32");
|
|
|
|
|
#else
|
|
|
|
|
g_assert_cmpstr (res, ==, "image/svg+xml");
|
|
|
|
|
#endif
|
|
|
|
|
g_assert_false (uncertain);
|
|
|
|
|
g_free (res);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-15 13:16:44 +02:00
|
|
|
|
static void
|
|
|
|
|
test_mime_from_content (void)
|
|
|
|
|
{
|
2022-11-02 11:01:09 +01:00
|
|
|
|
#ifdef __APPLE__
|
2017-10-15 13:16:44 +02:00
|
|
|
|
gchar *mime_type;
|
|
|
|
|
mime_type = g_content_type_get_mime_type ("com.microsoft.bmp");
|
|
|
|
|
g_assert_cmpstr (mime_type, ==, "image/bmp");
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
mime_type = g_content_type_get_mime_type ("com.compuserve.gif");
|
|
|
|
|
g_assert_cmpstr (mime_type, ==, "image/gif");
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
mime_type = g_content_type_get_mime_type ("public.png");
|
|
|
|
|
g_assert_cmpstr (mime_type, ==, "image/png");
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
mime_type = g_content_type_get_mime_type ("public.text");
|
|
|
|
|
g_assert_cmpstr (mime_type, ==, "text/*");
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
mime_type = g_content_type_get_mime_type ("public.svg-image");
|
|
|
|
|
g_assert_cmpstr (mime_type, ==, "image/svg+xml");
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
#elif defined(G_OS_WIN32)
|
|
|
|
|
g_test_skip ("mime from content type test not implemented on WIN32");
|
|
|
|
|
#else
|
|
|
|
|
g_test_skip ("mime from content type test not implemented on UNIX");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 04:27:01 +02:00
|
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
|
|
g_test_add_func ("/contenttype/guess", test_guess);
|
2017-10-11 15:59:16 +02:00
|
|
|
|
g_test_add_func ("/contenttype/guess_svg_from_data", test_guess_svg_from_data);
|
2017-10-15 13:16:44 +02:00
|
|
|
|
g_test_add_func ("/contenttype/mime_from_content", test_mime_from_content);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
g_test_add_func ("/contenttype/unknown", test_unknown);
|
|
|
|
|
g_test_add_func ("/contenttype/subtype", test_subtype);
|
|
|
|
|
g_test_add_func ("/contenttype/list", test_list);
|
|
|
|
|
g_test_add_func ("/contenttype/executable", test_executable);
|
2010-07-05 05:42:07 +02:00
|
|
|
|
g_test_add_func ("/contenttype/description", test_description);
|
2012-04-08 16:17:32 +02:00
|
|
|
|
g_test_add_func ("/contenttype/icon", test_icon);
|
2014-02-23 18:03:13 +01:00
|
|
|
|
g_test_add_func ("/contenttype/symbolic-icon", test_symbolic_icon);
|
2013-12-23 18:11:03 +01:00
|
|
|
|
g_test_add_func ("/contenttype/tree", test_tree);
|
2023-11-13 13:14:01 +01:00
|
|
|
|
g_test_add_func ("/contenttype/tree_invalid_encoding",
|
|
|
|
|
test_tree_invalid_encoding);
|
2017-05-10 18:09:35 +02:00
|
|
|
|
g_test_add_func ("/contenttype/test_type_is_a_special_case",
|
|
|
|
|
test_type_is_a_special_case);
|
2010-07-05 04:27:01 +02:00
|
|
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
|
}
|