From 7ed386a10b5d4948c4edddb749ee052f20edd5c8 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 9 Sep 2024 17:37:52 +0100 Subject: [PATCH] tests: Add test for decompression failure in GResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was one of the code paths not currently covered by unit tests, so let’s add a test for it. This tests what happens when a structurally valid GResource file, but with a corrupt entry for a compressed file, is loaded. Signed-off-by: Philip Withnall Helps: #3465 --- gio/tests/meson.build | 16 +++++++++++ gio/tests/resources.c | 50 +++++++++++++++++++++++++++++++++++ gio/tests/test6.gresource.xml | 7 +++++ 3 files changed, 73 insertions(+) create mode 100644 gio/tests/test6.gresource.xml diff --git a/gio/tests/meson.build b/gio/tests/meson.build index f2c11e360..dfb8de8be 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -859,6 +859,21 @@ if not meson.is_cross_build() install_tag : 'tests', install : installed_tests_enabled) + test6_gresource = custom_target('test6.gresource', + input : 'test6.gresource.xml', + depends : [gspawn_helpers], + output : 'test6.gresource', + command : [glib_compile_resources, + compiler_type, + '--target=@OUTPUT@', + '--sourcedir=' + meson.current_source_dir(), + '--sourcedir=' + meson.current_build_dir(), + '--internal', + '@INPUT@'], + install_dir : installed_tests_execdir, + install_tag : 'tests', + install : installed_tests_enabled) + test_resources2_c = custom_target('test_resources2.c', input : 'test3.gresource.xml', depends : [gspawn_helpers], @@ -930,6 +945,7 @@ if not meson.is_cross_build() resources_extra_sources = [ test_gresource, + test6_gresource, test_resources_c, test_resources2_c, test_resources2_h, diff --git a/gio/tests/resources.c b/gio/tests/resources.c index 2542db195..21d5d6a20 100644 --- a/gio/tests/resources.c +++ b/gio/tests/resources.c @@ -401,6 +401,55 @@ test_resource_data_empty (void) g_clear_error (&local_error); } +static void +test_resource_data_corrupt_compression (void) +{ + GFile *resource_file = NULL; + GBytes *resource_bytes = NULL, *corrupt_bytes = NULL, *data_bytes = NULL; + guint8 *corrupt_data = NULL; + GResource *resource = NULL; + GError *local_error = NULL; + + g_test_summary ("Test error handling for corrupt GResource files (specifically, corrupt zlib compression)."); + + resource_file = g_file_new_for_path (g_test_get_filename (G_TEST_BUILT, "test6.gresource", NULL)); + resource_bytes = g_file_load_bytes (resource_file, NULL, NULL, &local_error); + g_assert_no_error (local_error); + g_clear_object (&resource_file); + + /* Test loading the resource normally, to check it works. */ + resource = g_resource_new_from_data (resource_bytes, &local_error); + g_assert_no_error (local_error); + + data_bytes = g_resource_lookup_data (resource, "/test-corrupt-compression.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, &local_error); + g_assert_no_error (local_error); + g_assert_nonnull (data_bytes); + g_clear_pointer (&data_bytes, g_bytes_unref); + + g_clear_pointer (&resource, g_resource_unref); + + /* Modify the data to zero out bytes 0x90 to 0x100. These are comfortably + * within the compressed file data, so should break that while not breaking + * the GVDB header. */ + corrupt_data = g_memdup2 (g_bytes_get_data (resource_bytes, NULL), g_bytes_get_size (resource_bytes)); + memset (corrupt_data + 0x90, 0, 0x10); + corrupt_bytes = g_bytes_new_take (g_steal_pointer (&corrupt_data), g_bytes_get_size (resource_bytes)); + + resource = g_resource_new_from_data (corrupt_bytes, &local_error); + g_assert_no_error (local_error); + g_bytes_unref (corrupt_bytes); + + data_bytes = g_resource_lookup_data (resource, "/test-corrupt-compression.txt", + G_RESOURCE_LOOKUP_FLAGS_NONE, &local_error); + g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL); + g_assert_null (data_bytes); + g_clear_error (&local_error); + + g_clear_pointer (&resource_bytes, g_bytes_unref); + g_clear_pointer (&resource, g_resource_unref); +} + static void test_resource_registered (void) { @@ -1060,6 +1109,7 @@ main (int argc, g_test_add_func ("/resource/data", test_resource_data); g_test_add_func ("/resource/data_unaligned", test_resource_data_unaligned); g_test_add_func ("/resource/data-corrupt", test_resource_data_corrupt); + g_test_add_func ("/resource/data-corrupt-compression", test_resource_data_corrupt_compression); g_test_add_func ("/resource/data-empty", test_resource_data_empty); g_test_add_func ("/resource/registered", test_resource_registered); g_test_add_func ("/resource/manual", test_resource_manual); diff --git a/gio/tests/test6.gresource.xml b/gio/tests/test6.gresource.xml new file mode 100644 index 000000000..aa7b93986 --- /dev/null +++ b/gio/tests/test6.gresource.xml @@ -0,0 +1,7 @@ + + + + + test.gresource.xml + +