mirror of
				https://gitlab.gnome.org/GNOME/glib.git
				synced 2025-11-04 10:08:56 +01:00 
			
		
		
		
	tests: Add test for decompression failure in GResource
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 <pwithnall@gnome.org> Helps: #3465
This commit is contained in:
		@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								gio/tests/test6.gresource.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								gio/tests/test6.gresource.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<gresources>
 | 
			
		||||
  <gresource>
 | 
			
		||||
    <!-- An arbitrary text file which is large enough to have a non-trivial compressed form -->
 | 
			
		||||
    <file compressed="true" alias="test-corrupt-compression.txt">test.gresource.xml</file>
 | 
			
		||||
  </gresource>
 | 
			
		||||
</gresources>
 | 
			
		||||
		Reference in New Issue
	
	Block a user