From d586ab4c73b7a58c38dd0fbbddc4e4321ced0186 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 19 Mar 2019 11:18:07 +0000 Subject: [PATCH 1/3] tests: Use g_assert_*() instead of g_assert() in contenttype tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g_assert_*() give more helpful error messages on failure, and aren’t compiled out by G_DISABLE_ASSERT. Signed-off-by: Philip Withnall --- gio/tests/contenttype.c | 76 ++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/gio/tests/contenttype.c b/gio/tests/contenttype.c index 2424b8e5f..b860a1dac 100644 --- a/gio/tests/contenttype.c +++ b/gio/tests/contenttype.c @@ -39,7 +39,7 @@ test_guess (void) g_free (existing_directory); expected = g_content_type_from_mime_type ("inode/directory"); g_assert_content_type_equals (expected, res); - g_assert (uncertain); + g_assert_true (uncertain); g_free (res); g_free (expected); @@ -52,7 +52,7 @@ test_guess (void) res = g_content_type_guess ("foo.txt", data, sizeof (data) - 1, &uncertain); expected = g_content_type_from_mime_type ("text/plain"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); @@ -61,21 +61,21 @@ test_guess (void) res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain); expected = g_content_type_from_mime_type ("text/plain"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); res = g_content_type_guess ("foo.desktop", data, sizeof (data) - 1, &uncertain); expected = g_content_type_from_mime_type ("application/x-desktop"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); res = g_content_type_guess (NULL, data, sizeof (data) - 1, &uncertain); expected = g_content_type_from_mime_type ("application/x-desktop"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); @@ -84,14 +84,14 @@ test_guess (void) res = g_content_type_guess ("test.pot", (guchar *)"ABC abc", 7, &uncertain); expected = g_content_type_from_mime_type ("text/x-gettext-translation-template"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); 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"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); @@ -107,7 +107,7 @@ test_guess (void) res = g_content_type_guess ("test.otf", (guchar *)"OTTO", 4, &uncertain); expected = g_content_type_from_mime_type ("application/x-font-otf"); g_assert_content_type_equals (expected, res); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); #endif @@ -115,7 +115,7 @@ test_guess (void) 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); - g_assert (!uncertain); + g_assert_false (uncertain); g_free (res); g_free (expected); } @@ -127,7 +127,7 @@ test_unknown (void) gchar *str; unknown = g_content_type_from_mime_type ("application/octet-stream"); - g_assert (g_content_type_is_unknown (unknown)); + g_assert_true (g_content_type_is_unknown (unknown)); str = g_content_type_get_mime_type (unknown); g_assert_cmpstr (str, ==, "application/octet-stream"); g_free (str); @@ -143,8 +143,8 @@ test_subtype (void) plain = g_content_type_from_mime_type ("text/plain"); xml = g_content_type_from_mime_type ("application/xml"); - g_assert (g_content_type_is_a (xml, plain)); - g_assert (g_content_type_is_mime_type (xml, "text/plain")); + g_assert_true (g_content_type_is_a (xml, plain)); + g_assert_true (g_content_type_is_mime_type (xml, "text/plain")); g_free (plain); g_free (xml); @@ -175,11 +175,11 @@ test_list (void) types = g_content_types_get_registered (); - g_assert (g_list_length (types) > 1); + g_assert_cmpuint (g_list_length (types), >, 1); /* just check that some types are in the list */ - g_assert (g_list_find_custom (types, plain, find_mime) != NULL); - g_assert (g_list_find_custom (types, xml, find_mime) != NULL); + g_assert_nonnull (g_list_find_custom (types, plain, find_mime)); + g_assert_nonnull (g_list_find_custom (types, xml, find_mime)); g_list_free_full (types, g_free); @@ -193,15 +193,15 @@ test_executable (void) gchar *type; type = g_content_type_from_mime_type ("application/x-executable"); - g_assert (g_content_type_can_be_executable (type)); + g_assert_true (g_content_type_can_be_executable (type)); g_free (type); type = g_content_type_from_mime_type ("text/plain"); - g_assert (g_content_type_can_be_executable (type)); + g_assert_true (g_content_type_can_be_executable (type)); g_free (type); type = g_content_type_from_mime_type ("image/png"); - g_assert (!g_content_type_can_be_executable (type)); + g_assert_false (g_content_type_can_be_executable (type)); g_free (type); } @@ -213,7 +213,7 @@ test_description (void) type = g_content_type_from_mime_type ("text/plain"); desc = g_content_type_get_description (type); - g_assert (desc != NULL); + g_assert_nonnull (desc); g_free (desc); g_free (type); @@ -227,17 +227,17 @@ test_icon (void) type = g_content_type_from_mime_type ("text/plain"); icon = g_content_type_get_icon (type); - g_assert (G_IS_ICON (icon)); + g_assert_true (G_IS_ICON (icon)); if (G_IS_THEMED_ICON (icon)) { const gchar *const *names; names = g_themed_icon_get_names (G_THEMED_ICON (icon)); #ifdef __APPLE__ - g_assert (g_strv_contains (names, "text-*")); + g_assert_true (g_strv_contains (names, "text-*")); #else - g_assert (g_strv_contains (names, "text-plain")); - g_assert (g_strv_contains (names, "text-x-generic")); + g_assert_true (g_strv_contains (names, "text-plain")); + g_assert_true (g_strv_contains (names, "text-x-generic")); #endif } g_object_unref (icon); @@ -245,15 +245,15 @@ test_icon (void) type = g_content_type_from_mime_type ("application/rtf"); icon = g_content_type_get_icon (type); - g_assert (G_IS_ICON (icon)); + g_assert_true (G_IS_ICON (icon)); if (G_IS_THEMED_ICON (icon)) { const gchar *const *names; names = g_themed_icon_get_names (G_THEMED_ICON (icon)); - g_assert (g_strv_contains (names, "application-rtf")); + g_assert_true (g_strv_contains (names, "application-rtf")); #ifndef __APPLE__ - g_assert (g_strv_contains (names, "x-office-document")); + g_assert_true (g_strv_contains (names, "x-office-document")); #endif } g_object_unref (icon); @@ -269,20 +269,20 @@ test_symbolic_icon (void) type = g_content_type_from_mime_type ("text/plain"); icon = g_content_type_get_symbolic_icon (type); - g_assert (G_IS_ICON (icon)); + g_assert_true (G_IS_ICON (icon)); if (G_IS_THEMED_ICON (icon)) { const gchar *const *names; names = g_themed_icon_get_names (G_THEMED_ICON (icon)); #ifdef __APPLE__ - g_assert (g_strv_contains (names, "text-*-symbolic")); - g_assert (g_strv_contains (names, "text-*")); + g_assert_true (g_strv_contains (names, "text-*-symbolic")); + g_assert_true (g_strv_contains (names, "text-*")); #else - g_assert (g_strv_contains (names, "text-plain-symbolic")); - g_assert (g_strv_contains (names, "text-x-generic-symbolic")); - g_assert (g_strv_contains (names, "text-plain")); - g_assert (g_strv_contains (names, "text-x-generic")); + 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")); #endif } g_object_unref (icon); @@ -290,17 +290,17 @@ test_symbolic_icon (void) type = g_content_type_from_mime_type ("application/rtf"); icon = g_content_type_get_symbolic_icon (type); - g_assert (G_IS_ICON (icon)); + g_assert_true (G_IS_ICON (icon)); if (G_IS_THEMED_ICON (icon)) { const gchar *const *names; names = g_themed_icon_get_names (G_THEMED_ICON (icon)); - g_assert (g_strv_contains (names, "application-rtf-symbolic")); - g_assert (g_strv_contains (names, "application-rtf")); + g_assert_true (g_strv_contains (names, "application-rtf-symbolic")); + g_assert_true (g_strv_contains (names, "application-rtf")); #ifndef __APPLE__ - g_assert (g_strv_contains (names, "x-office-document-symbolic")); - g_assert (g_strv_contains (names, "x-office-document")); + g_assert_true (g_strv_contains (names, "x-office-document-symbolic")); + g_assert_true (g_strv_contains (names, "x-office-document")); #endif } g_object_unref (icon); From 4a0cb8f3a18b566f3644819e2ff69e07db6b5705 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 19 Mar 2019 11:16:39 +0000 Subject: [PATCH 2/3] =?UTF-8?q?xdgmime:=20Don=E2=80=99t=20set=20an=20out?= =?UTF-8?q?=20argument=20if=20it=E2=80=99s=20NULL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Philip Withnall Fixes: #1729 --- gio/xdgmime/xdgmime.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gio/xdgmime/xdgmime.c b/gio/xdgmime/xdgmime.c index 2446d3813..9bb93f791 100644 --- a/gio/xdgmime/xdgmime.c +++ b/gio/xdgmime/xdgmime.c @@ -531,7 +531,8 @@ xdg_mime_get_mime_type_for_data (const void *data, if (len == 0) { - *result_prio = 100; + if (result_prio != NULL) + *result_prio = 100; return XDG_MIME_TYPE_EMPTY; } From b73713d8933b53fcfa322e94fde949c7b2c3827d Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 19 Mar 2019 11:18:48 +0000 Subject: [PATCH 3/3] tests: Add a test for g_content_type_guess() with no arguments It should produce a generic result, but not crash. It was previously crashing on macOS. Signed-off-by: Philip Withnall Fixes: #1729 --- gio/tests/contenttype.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gio/tests/contenttype.c b/gio/tests/contenttype.c index b860a1dac..3696e3662 100644 --- a/gio/tests/contenttype.c +++ b/gio/tests/contenttype.c @@ -118,6 +118,14 @@ test_guess (void) g_assert_false (uncertain); g_free (res); g_free (expected); + + /* 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); } static void