LCOV - code coverage report
Current view: top level - gio/tests - resources.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 100.0 % 669 669
Test Date: 2026-10-13 05:13:35 Functions: 100.0 % 22 22
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLib testing framework examples and tests
       2                 :             :  *
       3                 :             :  * Copyright (C) 2011 Red Hat, Inc.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 of the License, or (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This library is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General
      18                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include <string.h>
      22                 :             : #include <gio/gio.h>
      23                 :             : #include <glibconfig.h>
      24                 :             : #include "gconstructor.h"
      25                 :             : #include "test_resources2.h"
      26                 :             : #include "digit_test_resources.h"
      27                 :             : #include "gvdb/gvdb-builder.h"
      28                 :             : 
      29                 :             : #ifdef _MSC_VER
      30                 :             : # define MODULE_FILENAME_PREFIX ""
      31                 :             : #else
      32                 :             : # define MODULE_FILENAME_PREFIX "lib"
      33                 :             : #endif
      34                 :             : 
      35                 :             : static void
      36                 :           6 : test_resource (GResource *resource)
      37                 :             : {
      38                 :           6 :   GError *error = NULL;
      39                 :             :   gboolean found, success;
      40                 :             :   gsize size;
      41                 :             :   guint32 flags;
      42                 :             :   GBytes *data;
      43                 :             :   char **children;
      44                 :             :   GInputStream *in;
      45                 :             :   char buffer[128];
      46                 :           6 :   const gchar *not_found_paths[] =
      47                 :             :     {
      48                 :             :       "/not/there",
      49                 :             :       "/",
      50                 :             :       "",
      51                 :             :     };
      52                 :             :   gsize i;
      53                 :             : 
      54                 :          24 :   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
      55                 :             :     {
      56                 :          27 :       found = g_resource_get_info (resource,
      57                 :          18 :                                    not_found_paths[i],
      58                 :             :                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
      59                 :             :                                    &size, &flags, &error);
      60                 :          18 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
      61                 :          18 :       g_clear_error (&error);
      62                 :          18 :       g_assert_false (found);
      63                 :           9 :     }
      64                 :             : 
      65                 :           6 :   found = g_resource_get_info (resource,
      66                 :             :                                "/test1.txt",
      67                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
      68                 :             :                                &size, &flags, &error);
      69                 :           6 :   g_assert_true (found);
      70                 :           6 :   g_assert_no_error (error);
      71                 :           6 :   g_assert_cmpint (size, ==, 6);
      72                 :           6 :   g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
      73                 :             : 
      74                 :           6 :   found = g_resource_get_info (resource,
      75                 :             :                                "/empty.txt",
      76                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
      77                 :             :                                &size, &flags, &error);
      78                 :           6 :   g_assert_true (found);
      79                 :           6 :   g_assert_no_error (error);
      80                 :           6 :   g_assert_cmpint (size, ==, 0);
      81                 :           6 :   g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
      82                 :             : 
      83                 :           6 :   found = g_resource_get_info (resource,
      84                 :             :                                "/a_prefix/test2.txt",
      85                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
      86                 :             :                                &size, &flags, &error);
      87                 :           6 :   g_assert_true (found);
      88                 :           6 :   g_assert_no_error (error);
      89                 :           6 :   g_assert_cmpint (size, ==, 6);
      90                 :           6 :   g_assert_cmpuint (flags, ==, 0);
      91                 :             : 
      92                 :           6 :   found = g_resource_get_info (resource,
      93                 :             :                                "/a_prefix/test2-alias.txt",
      94                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
      95                 :             :                                &size, &flags, &error);
      96                 :           6 :   g_assert_true (found);
      97                 :           6 :   g_assert_no_error (error);
      98                 :           6 :   g_assert_cmpint (size, ==, 6);
      99                 :           6 :   g_assert_cmpuint (flags, ==, 0);
     100                 :             : 
     101                 :          24 :   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
     102                 :             :     {
     103                 :          27 :       data = g_resource_lookup_data (resource,
     104                 :          18 :                                      not_found_paths[i],
     105                 :             :                                      G_RESOURCE_LOOKUP_FLAGS_NONE,
     106                 :             :                                      &error);
     107                 :          18 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     108                 :          18 :       g_clear_error (&error);
     109                 :          18 :       g_assert_null (data);
     110                 :           9 :     }
     111                 :             : 
     112                 :           6 :   data = g_resource_lookup_data (resource,
     113                 :             :                                  "/test1.txt",
     114                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
     115                 :             :                                  &error);
     116                 :           6 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     117                 :           6 :   g_assert_no_error (error);
     118                 :           6 :   g_bytes_unref (data);
     119                 :             : 
     120                 :           6 :   data = g_resource_lookup_data (resource,
     121                 :             :                                  "/empty.txt",
     122                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
     123                 :             :                                  &error);
     124                 :           6 :   g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
     125                 :           6 :   g_assert_no_error (error);
     126                 :           6 :   g_bytes_unref (data);
     127                 :             : 
     128                 :          24 :   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
     129                 :             :     {
     130                 :          36 :       in = g_resource_open_stream (resource,
     131                 :          18 :                                    not_found_paths[i],
     132                 :             :                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
     133                 :             :                                    &error);
     134                 :          18 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     135                 :          18 :       g_clear_error (&error);
     136                 :          18 :       g_assert_null (in);
     137                 :           9 :     }
     138                 :             : 
     139                 :           6 :   in = g_resource_open_stream (resource,
     140                 :             :                                "/test1.txt",
     141                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
     142                 :             :                                &error);
     143                 :           6 :   g_assert_nonnull (in);
     144                 :           6 :   g_assert_no_error (error);
     145                 :             : 
     146                 :           6 :   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
     147                 :             :                                      &size,
     148                 :             :                                      NULL, &error);
     149                 :           6 :   g_assert_true (success);
     150                 :           6 :   g_assert_no_error (error);
     151                 :           6 :   g_assert_cmpint (size, ==, 6);
     152                 :           6 :   buffer[size] = 0;
     153                 :           6 :   g_assert_cmpstr (buffer, ==, "test1\n");
     154                 :             : 
     155                 :           6 :   g_input_stream_close (in, NULL, &error);
     156                 :           6 :   g_assert_no_error (error);
     157                 :           6 :   g_clear_object (&in);
     158                 :             : 
     159                 :           6 :   in = g_resource_open_stream (resource,
     160                 :             :                                "/empty.txt",
     161                 :             :                                G_RESOURCE_LOOKUP_FLAGS_NONE,
     162                 :             :                                &error);
     163                 :           6 :   g_assert_no_error (error);
     164                 :           6 :   g_assert_nonnull (in);
     165                 :             : 
     166                 :           6 :   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
     167                 :             :                                      &size,
     168                 :             :                                      NULL, &error);
     169                 :           6 :   g_assert_no_error (error);
     170                 :           6 :   g_assert_true (success);
     171                 :           6 :   g_assert_cmpint (size, ==, 0);
     172                 :             : 
     173                 :           6 :   g_input_stream_close (in, NULL, &error);
     174                 :           6 :   g_assert_no_error (error);
     175                 :           6 :   g_clear_object (&in);
     176                 :             : 
     177                 :           6 :   data = g_resource_lookup_data (resource,
     178                 :             :                                  "/a_prefix/test2.txt",
     179                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
     180                 :             :                                  &error);
     181                 :           6 :   g_assert_nonnull (data);
     182                 :           6 :   g_assert_no_error (error);
     183                 :           6 :   size = g_bytes_get_size (data);
     184                 :           6 :   g_assert_cmpint (size, ==, 6);
     185                 :           6 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
     186                 :           6 :   g_bytes_unref (data);
     187                 :             : 
     188                 :           6 :   data = g_resource_lookup_data (resource,
     189                 :             :                                  "/a_prefix/test2-alias.txt",
     190                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
     191                 :             :                                  &error);
     192                 :           6 :   g_assert_nonnull (data);
     193                 :           6 :   g_assert_no_error (error);
     194                 :           6 :   size = g_bytes_get_size (data);
     195                 :           6 :   g_assert_cmpint (size, ==, 6);
     196                 :           6 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
     197                 :           6 :   g_bytes_unref (data);
     198                 :             : 
     199                 :          24 :   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
     200                 :             :     {
     201                 :          18 :       if (g_str_equal (not_found_paths[i], "/"))
     202                 :           6 :         continue;
     203                 :             : 
     204                 :          18 :       children = g_resource_enumerate_children (resource,
     205                 :          12 :                                                 not_found_paths[i],
     206                 :             :                                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     207                 :             :                                                 &error);
     208                 :          12 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     209                 :          12 :       g_clear_error (&error);
     210                 :          12 :       g_assert_null (children);
     211                 :           6 :     }
     212                 :             : 
     213                 :           6 :   children = g_resource_enumerate_children  (resource,
     214                 :             :                                              "/a_prefix",
     215                 :             :                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
     216                 :             :                                              &error);
     217                 :           6 :   g_assert_nonnull (children);
     218                 :           6 :   g_assert_no_error (error);
     219                 :           6 :   g_assert_cmpint (g_strv_length (children), ==, 2);
     220                 :           6 :   g_strfreev (children);
     221                 :             : 
     222                 :             :   /* Test the preferred lookup where we have a trailing slash. */
     223                 :           6 :   children = g_resource_enumerate_children  (resource,
     224                 :             :                                              "/a_prefix/",
     225                 :             :                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
     226                 :             :                                              &error);
     227                 :           6 :   g_assert_nonnull (children);
     228                 :           6 :   g_assert_no_error (error);
     229                 :           6 :   g_assert_cmpint (g_strv_length (children), ==, 2);
     230                 :           6 :   g_strfreev (children);
     231                 :             : 
     232                 :             :   /* test with a path > 256 and no trailing slash to test the
     233                 :             :    * slow path of resources where we allocate a modified path.
     234                 :             :    */
     235                 :           6 :   children = g_resource_enumerate_children  (resource,
     236                 :             :                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
     237                 :             :                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
     238                 :             :                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
     239                 :             :                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
     240                 :             :                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
     241                 :             :                                              "/with/no/trailing/slash",
     242                 :             :                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
     243                 :             :                                              &error);
     244                 :           6 :   g_assert_null (children);
     245                 :           6 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     246                 :           6 :   g_clear_error (&error);
     247                 :           6 : }
     248                 :             : 
     249                 :             : static void
     250                 :           2 : test_resource_file (void)
     251                 :             : {
     252                 :             :   GResource *resource;
     253                 :           2 :   GError *error = NULL;
     254                 :             : 
     255                 :           2 :   resource = g_resource_load ("not-there", &error);
     256                 :           2 :   g_assert_null (resource);
     257                 :           2 :   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
     258                 :           2 :   g_clear_error (&error);
     259                 :             : 
     260                 :           2 :   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
     261                 :           2 :   g_assert_nonnull (resource);
     262                 :           2 :   g_assert_no_error (error);
     263                 :             : 
     264                 :           2 :   test_resource (resource);
     265                 :           2 :   g_resource_unref (resource);
     266                 :           2 : }
     267                 :             : 
     268                 :             : static void
     269                 :           2 : test_resource_file_path (void)
     270                 :             : {
     271                 :             :   static const struct {
     272                 :             :     const gchar *input;
     273                 :             :     const gchar *expected;
     274                 :             :   } test_uris[] = {
     275                 :             :     { "resource://", "resource:///" },
     276                 :             :     { "resource:///", "resource:///" },
     277                 :             :     { "resource://////", "resource:///" },
     278                 :             :     { "resource:///../../../", "resource:///" },
     279                 :             :     { "resource:///../../..", "resource:///" },
     280                 :             :     { "resource://abc", "resource:///abc" },
     281                 :             :     { "resource:///abc/", "resource:///abc" },
     282                 :             :     { "resource:/a/b/../c/", "resource:///a/c" },
     283                 :             :     { "resource://../a/b/../c/../", "resource:///a" },
     284                 :             :     { "resource://a/b/cc//bb//a///", "resource:///a/b/cc/bb/a" },
     285                 :             :     { "resource://././././", "resource:///" },
     286                 :             :     { "resource://././././../", "resource:///" },
     287                 :             :     { "resource://a/b/c/d.png", "resource:///a/b/c/d.png" },
     288                 :             :     { "resource://a/b/c/..png", "resource:///a/b/c/..png" },
     289                 :             :     { "resource://a/b/c/./png", "resource:///a/b/c/png" },
     290                 :             :   };
     291                 :             :   guint i;
     292                 :             : 
     293                 :          32 :   for (i = 0; i < G_N_ELEMENTS (test_uris); i++)
     294                 :             :     {
     295                 :             :       GFile *file;
     296                 :             :       gchar *uri;
     297                 :             : 
     298                 :          30 :       file = g_file_new_for_uri (test_uris[i].input);
     299                 :          30 :       g_assert_nonnull (file);
     300                 :             : 
     301                 :          30 :       uri = g_file_get_uri (file);
     302                 :          30 :       g_assert_nonnull (uri);
     303                 :             : 
     304                 :          30 :       g_assert_cmpstr (uri, ==, test_uris[i].expected);
     305                 :             : 
     306                 :          30 :       g_object_unref (file);
     307                 :          30 :       g_free (uri);
     308                 :          15 :     }
     309                 :           2 : }
     310                 :             : 
     311                 :             : static void
     312                 :           2 : test_resource_data (void)
     313                 :             : {
     314                 :             :   GResource *resource;
     315                 :           2 :   GError *error = NULL;
     316                 :             :   gboolean loaded_file;
     317                 :             :   char *content;
     318                 :             :   gsize content_size;
     319                 :             :   GBytes *data;
     320                 :             : 
     321                 :           2 :   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
     322                 :             :                                      &content, &content_size, NULL);
     323                 :           2 :   g_assert_true (loaded_file);
     324                 :             : 
     325                 :           2 :   data = g_bytes_new_take (content, content_size);
     326                 :           2 :   resource = g_resource_new_from_data (data, &error);
     327                 :           2 :   g_bytes_unref (data);
     328                 :           2 :   g_assert_nonnull (resource);
     329                 :           2 :   g_assert_no_error (error);
     330                 :             : 
     331                 :           2 :   test_resource (resource);
     332                 :             : 
     333                 :           2 :   g_resource_unref (resource);
     334                 :           2 : }
     335                 :             : 
     336                 :             : static void
     337                 :           2 : test_resource_data_unaligned (void)
     338                 :             : {
     339                 :             :   GResource *resource;
     340                 :           2 :   GError *error = NULL;
     341                 :             :   gboolean loaded_file;
     342                 :             :   char *content, *content_copy;
     343                 :             :   gsize content_size;
     344                 :             :   GBytes *data;
     345                 :             : 
     346                 :           2 :   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
     347                 :             :                                      &content, &content_size, NULL);
     348                 :           2 :   g_assert_true (loaded_file);
     349                 :             : 
     350                 :           2 :   content_copy = g_new (char, content_size + 1);
     351                 :           2 :   memcpy (content_copy + 1, content, content_size);
     352                 :             : 
     353                 :           3 :   data = g_bytes_new_with_free_func (content_copy + 1, content_size,
     354                 :           1 :                                      (GDestroyNotify) g_free, content_copy);
     355                 :           2 :   g_free (content);
     356                 :           2 :   resource = g_resource_new_from_data (data, &error);
     357                 :           2 :   g_bytes_unref (data);
     358                 :           2 :   g_assert_nonnull (resource);
     359                 :           2 :   g_assert_no_error (error);
     360                 :             : 
     361                 :           2 :   test_resource (resource);
     362                 :             : 
     363                 :           2 :   g_resource_unref (resource);
     364                 :           2 : }
     365                 :             : 
     366                 :             : /* Test error handling for corrupt GResource files (specifically, a corrupt
     367                 :             :  * GVDB header). */
     368                 :             : static void
     369                 :           2 : test_resource_data_corrupt (void)
     370                 :             : {
     371                 :             :   /* A GVDB header is 6 guint32s, and requires a magic number in the first two
     372                 :             :    * guint32s. A set of zero bytes of a greater length is considered corrupt. */
     373                 :             :   static const guint8 data[sizeof (guint32) * 7] = { 0, };
     374                 :           2 :   GBytes *bytes = NULL;
     375                 :           2 :   GResource *resource = NULL;
     376                 :           2 :   GError *local_error = NULL;
     377                 :             : 
     378                 :           2 :   bytes = g_bytes_new_static (data, sizeof (data));
     379                 :           2 :   resource = g_resource_new_from_data (bytes, &local_error);
     380                 :           2 :   g_bytes_unref (bytes);
     381                 :           2 :   g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
     382                 :           2 :   g_assert_null (resource);
     383                 :             : 
     384                 :           2 :   g_clear_error (&local_error);
     385                 :           2 : }
     386                 :             : 
     387                 :             : /* Test handling for empty GResource files. They should also be treated as
     388                 :             :  * corrupt. */
     389                 :             : static void
     390                 :           2 : test_resource_data_empty (void)
     391                 :             : {
     392                 :           2 :   GBytes *bytes = NULL;
     393                 :           2 :   GResource *resource = NULL;
     394                 :           2 :   GError *local_error = NULL;
     395                 :             : 
     396                 :           2 :   bytes = g_bytes_new_static (NULL, 0);
     397                 :           2 :   resource = g_resource_new_from_data (bytes, &local_error);
     398                 :           2 :   g_bytes_unref (bytes);
     399                 :           2 :   g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
     400                 :           2 :   g_assert_null (resource);
     401                 :             : 
     402                 :           2 :   g_clear_error (&local_error);
     403                 :           2 : }
     404                 :             : 
     405                 :             : static void
     406                 :           2 : test_resource_data_corrupt_compression (void)
     407                 :             : {
     408                 :           2 :   GFile *resource_file = NULL;
     409                 :           2 :   GBytes *resource_bytes = NULL, *corrupt_bytes = NULL, *data_bytes = NULL;
     410                 :           2 :   guint8 *corrupt_data = NULL;
     411                 :           2 :   GResource *resource = NULL;
     412                 :           2 :   GError *local_error = NULL;
     413                 :             : 
     414                 :           2 :   g_test_summary ("Test error handling for corrupt GResource files (specifically, corrupt zlib compression).");
     415                 :             : 
     416                 :           2 :   resource_file = g_file_new_for_path (g_test_get_filename (G_TEST_BUILT, "test6.gresource", NULL));
     417                 :           2 :   resource_bytes = g_file_load_bytes (resource_file, NULL, NULL, &local_error);
     418                 :           2 :   g_assert_no_error (local_error);
     419                 :           2 :   g_clear_object (&resource_file);
     420                 :             : 
     421                 :             :   /* Test loading the resource normally, to check it works. */
     422                 :           2 :   resource = g_resource_new_from_data (resource_bytes, &local_error);
     423                 :           2 :   g_assert_no_error (local_error);
     424                 :             : 
     425                 :           2 :   data_bytes = g_resource_lookup_data (resource, "/test-corrupt-compression.txt",
     426                 :             :                                        G_RESOURCE_LOOKUP_FLAGS_NONE, &local_error);
     427                 :           2 :   g_assert_no_error (local_error);
     428                 :           2 :   g_assert_nonnull (data_bytes);
     429                 :           2 :   g_clear_pointer (&data_bytes, g_bytes_unref);
     430                 :             : 
     431                 :           2 :   g_clear_pointer (&resource, g_resource_unref);
     432                 :             : 
     433                 :             :   /* Modify the data to zero out bytes 0x90 to 0x100. These are comfortably
     434                 :             :    * within the compressed file data, so should break that while not breaking
     435                 :             :    * the GVDB header. */
     436                 :           2 :   corrupt_data = g_memdup2 (g_bytes_get_data (resource_bytes, NULL), g_bytes_get_size (resource_bytes));
     437                 :           2 :   memset (corrupt_data + 0x90, 0, 0x10);
     438                 :           2 :   corrupt_bytes = g_bytes_new_take (g_steal_pointer (&corrupt_data), g_bytes_get_size (resource_bytes));
     439                 :             : 
     440                 :           2 :   resource = g_resource_new_from_data (corrupt_bytes, &local_error);
     441                 :           2 :   g_assert_no_error (local_error);
     442                 :           2 :   g_bytes_unref (corrupt_bytes);
     443                 :             : 
     444                 :           2 :   data_bytes = g_resource_lookup_data (resource, "/test-corrupt-compression.txt",
     445                 :             :                                        G_RESOURCE_LOOKUP_FLAGS_NONE, &local_error);
     446                 :           2 :   g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
     447                 :           2 :   g_assert_null (data_bytes);
     448                 :           2 :   g_clear_error (&local_error);
     449                 :             : 
     450                 :           2 :   g_clear_pointer (&resource_bytes, g_bytes_unref);
     451                 :           2 :   g_clear_pointer (&resource, g_resource_unref);
     452                 :           2 : }
     453                 :             : 
     454                 :             : static void
     455                 :           2 : test_resource_registered (void)
     456                 :             : {
     457                 :             :   GResource *resource;
     458                 :           2 :   GError *error = NULL;
     459                 :             :   gboolean found, success;
     460                 :             :   gsize size;
     461                 :             :   guint32 flags;
     462                 :             :   GBytes *data;
     463                 :             :   char **children;
     464                 :             :   GInputStream *in;
     465                 :             :   char buffer[128];
     466                 :             : 
     467                 :           2 :   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
     468                 :           2 :   g_assert_nonnull (resource);
     469                 :           2 :   g_assert_no_error (error);
     470                 :             : 
     471                 :           2 :   found = g_resources_get_info ("/test1.txt",
     472                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     473                 :             :                                 &size, &flags, &error);
     474                 :           2 :   g_assert_false (found);
     475                 :           2 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     476                 :           2 :   g_clear_error (&error);
     477                 :             : 
     478                 :           2 :   g_resources_register (resource);
     479                 :             : 
     480                 :           2 :   found = g_resources_get_info ("/test1.txt",
     481                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     482                 :             :                                 &size, &flags, &error);
     483                 :           2 :   g_assert_true (found);
     484                 :           2 :   g_assert_no_error (error);
     485                 :           2 :   g_assert_cmpint (size, ==, 6);
     486                 :           2 :   g_assert_cmpint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
     487                 :             : 
     488                 :           2 :   found = g_resources_get_info ("/empty.txt",
     489                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     490                 :             :                                 &size, &flags, &error);
     491                 :           2 :   g_assert_no_error (error);
     492                 :           2 :   g_assert_true (found);
     493                 :           2 :   g_assert_cmpint (size, ==, 0);
     494                 :           2 :   g_assert_cmpint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
     495                 :             : 
     496                 :           2 :   found = g_resources_get_info ("/a_prefix/test2.txt",
     497                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     498                 :             :                                 &size, &flags, &error);
     499                 :           2 :   g_assert_true (found);
     500                 :           2 :   g_assert_no_error (error);
     501                 :           2 :   g_assert_cmpint (size, ==, 6);
     502                 :           2 :   g_assert_cmpint (flags, ==, 0);
     503                 :             : 
     504                 :           2 :   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
     505                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     506                 :             :                                 &size, &flags, &error);
     507                 :           2 :   g_assert_true (found);
     508                 :           2 :   g_assert_no_error (error);
     509                 :           2 :   g_assert_cmpint (size, ==, 6);
     510                 :           2 :   g_assert_cmpuint (flags, ==, 0);
     511                 :             : 
     512                 :           2 :   data = g_resources_lookup_data ("/test1.txt",
     513                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     514                 :             :                                   &error);
     515                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     516                 :           2 :   g_assert_no_error (error);
     517                 :           2 :   g_bytes_unref (data);
     518                 :             : 
     519                 :           2 :   in = g_resources_open_stream ("/test1.txt",
     520                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     521                 :             :                                 &error);
     522                 :           2 :   g_assert_nonnull (in);
     523                 :           2 :   g_assert_no_error (error);
     524                 :             : 
     525                 :           2 :   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
     526                 :             :                                      &size,
     527                 :             :                                      NULL, &error);
     528                 :           2 :   g_assert_true (success);
     529                 :           2 :   g_assert_no_error (error);
     530                 :           2 :   g_assert_cmpint (size, ==, 6);
     531                 :           2 :   buffer[size] = 0;
     532                 :           2 :   g_assert_cmpstr (buffer, ==, "test1\n");
     533                 :             : 
     534                 :           2 :   g_input_stream_close (in, NULL, &error);
     535                 :           2 :   g_assert_no_error (error);
     536                 :           2 :   g_clear_object (&in);
     537                 :             : 
     538                 :           2 :   data = g_resources_lookup_data ("/empty.txt",
     539                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     540                 :             :                                   &error);
     541                 :           2 :   g_assert_no_error (error);
     542                 :           2 :   g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
     543                 :           2 :   g_bytes_unref (data);
     544                 :             : 
     545                 :           2 :   in = g_resources_open_stream ("/empty.txt",
     546                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     547                 :             :                                 &error);
     548                 :           2 :   g_assert_no_error (error);
     549                 :           2 :   g_assert_nonnull (in);
     550                 :             : 
     551                 :           2 :   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
     552                 :             :                                      &size,
     553                 :             :                                      NULL, &error);
     554                 :           2 :   g_assert_no_error (error);
     555                 :           2 :   g_assert_true (success);
     556                 :           2 :   g_assert_cmpint (size, ==, 0);
     557                 :             : 
     558                 :           2 :   g_input_stream_close (in, NULL, &error);
     559                 :           2 :   g_assert_no_error (error);
     560                 :           2 :   g_clear_object (&in);
     561                 :             : 
     562                 :           2 :   data = g_resources_lookup_data ("/a_prefix/test2.txt",
     563                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     564                 :             :                                   &error);
     565                 :           2 :   g_assert_nonnull (data);
     566                 :           2 :   g_assert_no_error (error);
     567                 :           2 :   size = g_bytes_get_size (data);
     568                 :           2 :   g_assert_cmpint (size, ==, 6);
     569                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
     570                 :           2 :   g_bytes_unref (data);
     571                 :             : 
     572                 :           2 :   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
     573                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     574                 :             :                                   &error);
     575                 :           2 :   g_assert_nonnull (data);
     576                 :           2 :   g_assert_no_error (error);
     577                 :           2 :   size = g_bytes_get_size (data);
     578                 :           2 :   g_assert_cmpint (size, ==, 6);
     579                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
     580                 :           2 :   g_bytes_unref (data);
     581                 :             : 
     582                 :           2 :   children = g_resources_enumerate_children ("/not/here",
     583                 :             :                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
     584                 :             :                                              &error);
     585                 :           2 :   g_assert_null (children);
     586                 :           2 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     587                 :           2 :   g_clear_error (&error);
     588                 :             : 
     589                 :           2 :   children = g_resources_enumerate_children ("/a_prefix",
     590                 :             :                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
     591                 :             :                                              &error);
     592                 :           2 :   g_assert_nonnull (children);
     593                 :           2 :   g_assert_no_error (error);
     594                 :           2 :   g_assert_cmpint (g_strv_length (children), ==, 2);
     595                 :           2 :   g_strfreev (children);
     596                 :             : 
     597                 :           2 :   g_resources_unregister (resource);
     598                 :           2 :   g_resource_unref (resource);
     599                 :             : 
     600                 :           2 :   found = g_resources_get_info ("/test1.txt",
     601                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     602                 :             :                                 &size, &flags, &error);
     603                 :           2 :   g_assert_false (found);
     604                 :           2 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     605                 :           2 :   g_clear_error (&error);
     606                 :             : 
     607                 :           2 :   data = g_resources_lookup_data ("/test1.txt",
     608                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     609                 :             :                                   &error);
     610                 :           2 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     611                 :           2 :   g_assert_null (data);
     612                 :           2 :   g_clear_error (&error);
     613                 :             : 
     614                 :           2 :   in = g_resources_open_stream ("/test1.txt",
     615                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     616                 :             :                                 &error);
     617                 :           2 :   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     618                 :           2 :   g_assert_null (in);
     619                 :           2 :   g_clear_error (&error);
     620                 :           2 : }
     621                 :             : 
     622                 :             : static void
     623                 :           2 : test_resource_automatic (void)
     624                 :             : {
     625                 :           2 :   GError *error = NULL;
     626                 :             :   gboolean found;
     627                 :             :   gsize size;
     628                 :             :   guint32 flags;
     629                 :             :   GBytes *data;
     630                 :             : 
     631                 :           2 :   found = g_resources_get_info ("/auto_loaded/test1.txt",
     632                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     633                 :             :                                 &size, &flags, &error);
     634                 :           2 :   g_assert_true (found);
     635                 :           2 :   g_assert_no_error (error);
     636                 :           2 :   g_assert_cmpint (size, ==, 6);
     637                 :           2 :   g_assert_cmpint (flags, ==, 0);
     638                 :             : 
     639                 :           2 :   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
     640                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     641                 :             :                                   &error);
     642                 :           2 :   g_assert_nonnull (data);
     643                 :           2 :   g_assert_no_error (error);
     644                 :           2 :   size = g_bytes_get_size (data);
     645                 :           2 :   g_assert_cmpint (size, ==, 6);
     646                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     647                 :           2 :   g_bytes_unref (data);
     648                 :           2 : }
     649                 :             : 
     650                 :             : static void
     651                 :           2 : test_resource_manual (void)
     652                 :             : {
     653                 :           2 :   GError *error = NULL;
     654                 :             :   gboolean found;
     655                 :             :   gsize size;
     656                 :             :   guint32 flags;
     657                 :             :   GBytes *data;
     658                 :             : 
     659                 :           2 :   found = g_resources_get_info ("/manual_loaded/test1.txt",
     660                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     661                 :             :                                 &size, &flags, &error);
     662                 :           2 :   g_assert_true (found);
     663                 :           2 :   g_assert_no_error (error);
     664                 :           2 :   g_assert_cmpint (size, ==, 6);
     665                 :           2 :   g_assert_cmpuint (flags, ==, 0);
     666                 :             : 
     667                 :           2 :   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
     668                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     669                 :             :                                   &error);
     670                 :           2 :   g_assert_nonnull (data);
     671                 :           2 :   g_assert_no_error (error);
     672                 :           2 :   size = g_bytes_get_size (data);
     673                 :           2 :   g_assert_cmpint (size, ==, 6);
     674                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     675                 :           2 :   g_bytes_unref (data);
     676                 :           2 : }
     677                 :             : 
     678                 :             : static void
     679                 :           2 : test_resource_manual2 (void)
     680                 :             : {
     681                 :             :   GResource *resource;
     682                 :             :   GBytes *data;
     683                 :             :   gsize size;
     684                 :           2 :   GError *error = NULL;
     685                 :             : 
     686                 :           2 :   resource = _g_test2_get_resource ();
     687                 :             : 
     688                 :           2 :   data = g_resource_lookup_data (resource,
     689                 :             :                                  "/manual_loaded/test1.txt",
     690                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
     691                 :             :                                  &error);
     692                 :           2 :   g_assert_nonnull (data);
     693                 :           2 :   g_assert_no_error (error);
     694                 :           2 :   size = g_bytes_get_size (data);
     695                 :           2 :   g_assert_cmpint (size, ==, 6);
     696                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     697                 :           2 :   g_bytes_unref (data);
     698                 :             : 
     699                 :           2 :   g_resource_unref (resource);
     700                 :           2 : }
     701                 :             : 
     702                 :             : /* Test building resources with external data option,
     703                 :             :  * where data is linked in as binary instead of compiled in.
     704                 :             :  * Checks if resources are automatically registered and
     705                 :             :  * data can be found and read. */
     706                 :             : static void
     707                 :           2 : test_resource_binary_linked (void)
     708                 :             : {
     709                 :             :   #ifdef NO_EXTERNAL_DATA
     710                 :           1 :   g_test_skip ("--external-data cannot be tested: " NO_EXTERNAL_DATA);
     711                 :           1 :   return;
     712                 :             :   #else /* !NO_EXTERNAL_DATA */
     713                 :           1 :   GError *error = NULL;
     714                 :             :   gboolean found;
     715                 :             :   gsize size;
     716                 :             :   guint32 flags;
     717                 :             :   GBytes *data;
     718                 :             : 
     719                 :           1 :   found = g_resources_get_info ("/binary_linked/test1.txt",
     720                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     721                 :             :                                 &size, &flags, &error);
     722                 :           1 :   g_assert_true (found);
     723                 :           1 :   g_assert_no_error (error);
     724                 :           1 :   g_assert_cmpint (size, ==, 6);
     725                 :           1 :   g_assert_cmpuint (flags, ==, 0);
     726                 :             : 
     727                 :           1 :   data = g_resources_lookup_data ("/binary_linked/test1.txt",
     728                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     729                 :             :                                   &error);
     730                 :           1 :   g_assert_nonnull (data);
     731                 :           1 :   g_assert_no_error (error);
     732                 :           1 :   size = g_bytes_get_size (data);
     733                 :           1 :   g_assert_cmpint (size, ==, 6);
     734                 :           1 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     735                 :           1 :   g_bytes_unref (data);
     736                 :             :   #endif /* !NO_EXTERNAL_DATA */
     737                 :           1 : }
     738                 :             : 
     739                 :             : /* Test resource whose xml file starts with more than one digit
     740                 :             :  * and where no explicit c-name is given
     741                 :             :  * Checks if resources are successfully registered and
     742                 :             :  * data can be found and read. */
     743                 :             : static void
     744                 :           2 : test_resource_digits (void)
     745                 :             : {
     746                 :           2 :   GError *error = NULL;
     747                 :             :   gboolean found;
     748                 :             :   gsize size;
     749                 :             :   guint32 flags;
     750                 :             :   GBytes *data;
     751                 :             : 
     752                 :           2 :   found = g_resources_get_info ("/digit_test/test1.txt",
     753                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
     754                 :             :                                 &size, &flags, &error);
     755                 :           2 :   g_assert_true (found);
     756                 :           2 :   g_assert_no_error (error);
     757                 :           2 :   g_assert_cmpint (size, ==, 6);
     758                 :           2 :   g_assert_cmpuint (flags, ==, 0);
     759                 :             : 
     760                 :           2 :   data = g_resources_lookup_data ("/digit_test/test1.txt",
     761                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
     762                 :             :                                   &error);
     763                 :           2 :   g_assert_nonnull (data);
     764                 :           2 :   g_assert_no_error (error);
     765                 :           2 :   size = g_bytes_get_size (data);
     766                 :           2 :   g_assert_cmpint (size, ==, 6);
     767                 :           2 :   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     768                 :           2 :   g_bytes_unref (data);
     769                 :           2 : }
     770                 :             : 
     771                 :             : static void
     772                 :           2 : test_resource_module (void)
     773                 :             : {
     774                 :             :   GIOModule *module;
     775                 :             :   gboolean found;
     776                 :             :   gsize size;
     777                 :             :   guint32 flags;
     778                 :             :   GBytes *data;
     779                 :             :   GError *error;
     780                 :             : 
     781                 :             : #ifdef GLIB_STATIC_COMPILATION
     782                 :             :   /* The resource module is statically linked with a separate copy
     783                 :             :    * of a GLib so g_static_resource_init won't work as expected. */
     784                 :             :   g_test_skip ("Resource modules aren't supported in static builds.");
     785                 :             :   return;
     786                 :             : #endif
     787                 :             : 
     788                 :           2 :   if (g_module_supported ())
     789                 :             :     {
     790                 :           2 :       module = g_io_module_new (g_test_get_filename (G_TEST_BUILT,
     791                 :             :                                                      MODULE_FILENAME_PREFIX "resourceplugin",
     792                 :             :                                                      NULL));
     793                 :             : 
     794                 :           2 :       error = NULL;
     795                 :             : 
     796                 :           2 :       found = g_resources_get_info ("/resourceplugin/test1.txt",
     797                 :             :                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
     798                 :             :                                     &size, &flags, &error);
     799                 :           2 :       g_assert_false (found);
     800                 :           2 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     801                 :           2 :       g_clear_error (&error);
     802                 :             : 
     803                 :           2 :       g_type_module_use (G_TYPE_MODULE (module));
     804                 :             : 
     805                 :           2 :       found = g_resources_get_info ("/resourceplugin/test1.txt",
     806                 :             :                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
     807                 :             :                                     &size, &flags, &error);
     808                 :           2 :       g_assert_true (found);
     809                 :           2 :       g_assert_no_error (error);
     810                 :           2 :       g_assert_cmpint (size, ==, 6);
     811                 :           2 :       g_assert_cmpuint (flags, ==, 0);
     812                 :             : 
     813                 :           2 :       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
     814                 :             :                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
     815                 :             :                                       &error);
     816                 :           2 :       g_assert_nonnull (data);
     817                 :           2 :       g_assert_no_error (error);
     818                 :           2 :       size = g_bytes_get_size (data);
     819                 :           2 :       g_assert_cmpint (size, ==, 6);
     820                 :           2 :       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
     821                 :           2 :       g_bytes_unref (data);
     822                 :             : 
     823                 :           2 :       g_type_module_unuse (G_TYPE_MODULE (module));
     824                 :             : 
     825                 :           2 :       found = g_resources_get_info ("/resourceplugin/test1.txt",
     826                 :             :                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
     827                 :             :                                     &size, &flags, &error);
     828                 :           2 :       g_assert_false (found);
     829                 :           2 :       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
     830                 :           2 :       g_clear_error (&error);
     831                 :             : 
     832                 :           2 :       g_clear_object (&module);
     833                 :           1 :     }
     834                 :           2 : }
     835                 :             : 
     836                 :             : static void
     837                 :           2 : test_uri_query_info (void)
     838                 :             : {
     839                 :             :   GResource *resource;
     840                 :           2 :   GError *error = NULL;
     841                 :             :   gboolean loaded_file;
     842                 :             :   char *content;
     843                 :             :   gsize content_size;
     844                 :             :   GBytes *data;
     845                 :             :   GFile *file;
     846                 :             :   GFileInfo *info;
     847                 :             :   const char *content_type;
     848                 :           2 :   gchar *mime_type = NULL;
     849                 :             :   const char *fs_type;
     850                 :             :   gboolean readonly;
     851                 :             : 
     852                 :           2 :   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
     853                 :             :                                      &content, &content_size, NULL);
     854                 :           2 :   g_assert_true (loaded_file);
     855                 :             : 
     856                 :           2 :   data = g_bytes_new_take (content, content_size);
     857                 :           2 :   resource = g_resource_new_from_data (data, &error);
     858                 :           2 :   g_bytes_unref (data);
     859                 :           2 :   g_assert_nonnull (resource);
     860                 :           2 :   g_assert_no_error (error);
     861                 :             : 
     862                 :           2 :   g_resources_register (resource);
     863                 :             : 
     864                 :           2 :   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
     865                 :           2 :   info = g_file_query_info (file, "*", 0, NULL, &error);
     866                 :           2 :   g_assert_no_error (error);
     867                 :             : 
     868                 :           2 :   content_type = g_file_info_get_content_type (info);
     869                 :           2 :   g_assert_nonnull (content_type);
     870                 :           2 :   mime_type = g_content_type_get_mime_type (content_type);
     871                 :           2 :   g_assert_nonnull (mime_type);
     872                 :             : #ifdef __APPLE__
     873                 :             :   g_assert_cmpstr (mime_type, ==, "text/*");
     874                 :             : #else
     875                 :           2 :   g_assert_cmpstr (mime_type, ==, "text/plain");
     876                 :             : #endif
     877                 :           2 :   g_free (mime_type);
     878                 :             : 
     879                 :           2 :   g_object_unref (info);
     880                 :             : 
     881                 :           2 :   info = g_file_query_filesystem_info (file, "*", NULL, &error);
     882                 :           2 :   g_assert_no_error (error);
     883                 :             : 
     884                 :           2 :   fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
     885                 :           2 :   g_assert_cmpstr (fs_type, ==, "resource");
     886                 :           2 :   readonly = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
     887                 :           2 :   g_assert_true (readonly);
     888                 :             : 
     889                 :           2 :   g_object_unref (info);
     890                 :             : 
     891                 :           2 :   g_assert_cmpuint  (g_file_hash (file), !=, 0);
     892                 :             : 
     893                 :           2 :   g_object_unref (file);
     894                 :             : 
     895                 :           2 :   g_resources_unregister (resource);
     896                 :           2 :   g_resource_unref (resource);
     897                 :           2 : }
     898                 :             : 
     899                 :             : static void
     900                 :           2 : test_uri_file (void)
     901                 :             : {
     902                 :             :   GResource *resource;
     903                 :           2 :   GError *error = NULL;
     904                 :             :   gboolean loaded_file;
     905                 :             :   char *content;
     906                 :             :   gsize content_size;
     907                 :             :   GBytes *data;
     908                 :             :   GFile *file;
     909                 :             :   GFileInfo *info;
     910                 :             :   gchar *name;
     911                 :             :   GFile *file2, *parent;
     912                 :             :   GFileEnumerator *enumerator;
     913                 :             :   gchar *scheme;
     914                 :             :   GFileAttributeInfoList *attrs;
     915                 :             :   GInputStream *stream;
     916                 :             :   gchar buf[1024];
     917                 :             :   gboolean ret;
     918                 :             :   gssize skipped;
     919                 :             : 
     920                 :           2 :   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
     921                 :             :                                      &content, &content_size, NULL);
     922                 :           2 :   g_assert_true (loaded_file);
     923                 :             : 
     924                 :           2 :   data = g_bytes_new_take (content, content_size);
     925                 :           2 :   resource = g_resource_new_from_data (data, &error);
     926                 :           2 :   g_bytes_unref (data);
     927                 :           2 :   g_assert_nonnull (resource);
     928                 :           2 :   g_assert_no_error (error);
     929                 :             : 
     930                 :           2 :   g_resources_register (resource);
     931                 :             : 
     932                 :           2 :   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
     933                 :             : 
     934                 :           2 :   g_assert_null (g_file_get_path (file));
     935                 :             : 
     936                 :           2 :   name = g_file_get_parse_name (file);
     937                 :           2 :   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
     938                 :           2 :   g_free (name);
     939                 :             : 
     940                 :           2 :   name = g_file_get_uri (file);
     941                 :           2 :   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
     942                 :           2 :   g_free (name);
     943                 :             : 
     944                 :           2 :   g_assert_false (g_file_is_native (file));
     945                 :           2 :   g_assert_false (g_file_has_uri_scheme (file, "http"));
     946                 :           2 :   g_assert_true (g_file_has_uri_scheme (file, "resource"));
     947                 :           2 :   scheme = g_file_get_uri_scheme (file);
     948                 :           2 :   g_assert_cmpstr (scheme, ==, "resource");
     949                 :           2 :   g_free (scheme);
     950                 :             : 
     951                 :           2 :   file2 = g_file_dup (file);
     952                 :           2 :   g_assert_true (g_file_equal (file, file2));
     953                 :           2 :   g_object_unref (file2);
     954                 :             : 
     955                 :           2 :   parent = g_file_get_parent (file);
     956                 :           2 :   enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
     957                 :           2 :   g_assert_no_error (error);
     958                 :             : 
     959                 :           2 :   file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
     960                 :           2 :   g_assert_no_error (error);
     961                 :           2 :   g_assert_true (g_file_equal (file, file2));
     962                 :           2 :   g_object_unref (file2);
     963                 :             : 
     964                 :           2 :   info = g_file_enumerator_next_file (enumerator, NULL, &error);
     965                 :           2 :   g_assert_no_error (error);
     966                 :           2 :   g_assert_nonnull (info);
     967                 :           2 :   g_object_unref (info);
     968                 :             : 
     969                 :           2 :   info = g_file_enumerator_next_file (enumerator, NULL, &error);
     970                 :           2 :   g_assert_no_error (error);
     971                 :           2 :   g_assert_nonnull (info);
     972                 :           2 :   g_object_unref (info);
     973                 :             : 
     974                 :           2 :   info = g_file_enumerator_next_file (enumerator, NULL, &error);
     975                 :           2 :   g_assert_no_error (error);
     976                 :           2 :   g_assert_null (info);
     977                 :             : 
     978                 :           2 :   g_file_enumerator_close (enumerator, NULL, &error);
     979                 :           2 :   g_assert_no_error (error);
     980                 :           2 :   g_object_unref (enumerator);
     981                 :             : 
     982                 :           2 :   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
     983                 :           2 :   g_assert_true (g_file_equal (file, file2));
     984                 :             : 
     985                 :           2 :   g_assert_true (g_file_has_prefix (file, parent));
     986                 :             : 
     987                 :           2 :   name = g_file_get_relative_path (parent, file);
     988                 :           2 :   g_assert_cmpstr (name, ==, "test2-alias.txt");
     989                 :           2 :   g_free (name);
     990                 :             : 
     991                 :           2 :   g_object_unref (parent);
     992                 :             : 
     993                 :           2 :   attrs = g_file_query_settable_attributes (file, NULL, &error);
     994                 :           2 :   g_assert_no_error (error);
     995                 :           2 :   g_file_attribute_info_list_unref (attrs);
     996                 :             : 
     997                 :           2 :   attrs = g_file_query_writable_namespaces (file, NULL, &error);
     998                 :           2 :   g_assert_no_error (error);
     999                 :           2 :   g_file_attribute_info_list_unref (attrs);
    1000                 :             : 
    1001                 :           2 :   stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
    1002                 :           2 :   g_assert_no_error (error);
    1003                 :           2 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
    1004                 :           2 :   g_assert_true (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
    1005                 :           2 :   ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
    1006                 :           2 :   g_assert_true (ret);
    1007                 :           2 :   g_assert_no_error (error);
    1008                 :           2 :   skipped = g_input_stream_skip (stream, 1, NULL, &error);
    1009                 :           2 :   g_assert_cmpint (skipped, ==, 1);
    1010                 :           2 :   g_assert_no_error (error);
    1011                 :             : 
    1012                 :           2 :   memset (buf, 0, 1024);
    1013                 :           2 :   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
    1014                 :           2 :   g_assert_true (ret);
    1015                 :           2 :   g_assert_no_error (error);
    1016                 :           2 :   g_assert_cmpstr (buf, ==, "st2\n");
    1017                 :           2 :   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
    1018                 :             :                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
    1019                 :             :                                          NULL,
    1020                 :             :                                          &error);
    1021                 :           2 :   g_assert_no_error (error);
    1022                 :           2 :   g_assert_nonnull (info);
    1023                 :           2 :   g_assert_cmpint (g_file_info_get_size (info), ==, 6);
    1024                 :           2 :   g_object_unref (info);
    1025                 :             : 
    1026                 :           2 :   ret = g_input_stream_close (stream, NULL, &error);
    1027                 :           2 :   g_assert_true (ret);
    1028                 :           2 :   g_assert_no_error (error);
    1029                 :           2 :   g_object_unref (stream);
    1030                 :             : 
    1031                 :           2 :   g_object_unref (file);
    1032                 :           2 :   g_object_unref (file2);
    1033                 :             : 
    1034                 :           2 :   g_resources_unregister (resource);
    1035                 :           2 :   g_resource_unref (resource);
    1036                 :           2 : }
    1037                 :             : 
    1038                 :             : static void
    1039                 :           2 : test_resource_64k (void)
    1040                 :             : {
    1041                 :           2 :   GError *error = NULL;
    1042                 :             :   gboolean found;
    1043                 :             :   gsize size;
    1044                 :             :   guint32 flags;
    1045                 :             :   GBytes *data;
    1046                 :             :   gchar **tokens;
    1047                 :             : 
    1048                 :           2 :   found = g_resources_get_info ("/big_prefix/gresource-big-test.txt",
    1049                 :             :                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
    1050                 :             :                                 &size, &flags, &error);
    1051                 :           2 :   g_assert_true (found);
    1052                 :           2 :   g_assert_no_error (error);
    1053                 :             : 
    1054                 :             :   /* Check size: 100 of all lower case letters + newline char +
    1055                 :             :    *             100 all upper case letters + newline char +
    1056                 :             :    *             100 of all numbers between 0 to 9 + newline char
    1057                 :             :    *             (for 12 iterations)
    1058                 :             :    */
    1059                 :             : 
    1060                 :           2 :   g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
    1061                 :           2 :   g_assert_cmpuint (flags, ==, 0);
    1062                 :           2 :   data = g_resources_lookup_data ("/big_prefix/gresource-big-test.txt",
    1063                 :             :                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
    1064                 :             :                                   &error);
    1065                 :           2 :   g_assert_nonnull (data);
    1066                 :           2 :   g_assert_no_error (error);
    1067                 :           2 :   size = g_bytes_get_size (data);
    1068                 :             : 
    1069                 :           2 :   g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
    1070                 :           2 :   tokens = g_strsplit ((const gchar *) g_bytes_get_data (data, NULL), "\n", -1);
    1071                 :             : 
    1072                 :             :   /* check tokens[x] == entry at gresource-big-test.txt's line, where x = line - 1 */
    1073                 :           2 :   g_assert_cmpstr (tokens[0], ==, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    1074                 :           2 :   g_assert_cmpstr (tokens[27], ==, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
    1075                 :           2 :   g_assert_cmpstr (tokens[183], ==, "7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
    1076                 :           2 :   g_assert_cmpstr (tokens[600], ==, "QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
    1077                 :           2 :   g_assert_cmpstr (tokens[742], ==, "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888");
    1078                 :           2 :   g_strfreev (tokens);
    1079                 :           2 :   g_bytes_unref (data);
    1080                 :           2 : }
    1081                 :             : 
    1082                 :             : /* Check that g_resources_get_info() respects G_RESOURCE_OVERLAYS */
    1083                 :             : static void
    1084                 :           4 : test_overlay (void)
    1085                 :             : {
    1086                 :           4 :   if (g_test_subprocess ())
    1087                 :             :     {
    1088                 :           2 :       GError *error = NULL;
    1089                 :             :       gboolean res;
    1090                 :             :       gsize size;
    1091                 :             :       char *overlay;
    1092                 :             :       char *path;
    1093                 :           2 :       GInputStream *in = NULL;
    1094                 :             :       char buffer[128];
    1095                 :           2 :       GBytes *data = NULL;
    1096                 :           2 :       char *expected_overlay_data = NULL;
    1097                 :           2 :       size_t expected_overlay_size = 0;
    1098                 :             : 
    1099                 :           2 :       path = g_test_build_filename (G_TEST_DIST, "test1.overlay", NULL);
    1100                 :           2 :       res = g_file_get_contents (path, &expected_overlay_data, &expected_overlay_size, NULL);
    1101                 :           2 :       g_assert (res);
    1102                 :             : 
    1103                 :           2 :       overlay = g_strconcat ("/auto_loaded/test1.txt=", path, NULL);
    1104                 :           2 :       g_setenv ("G_RESOURCE_OVERLAYS", overlay, TRUE);
    1105                 :             : 
    1106                 :             :       /* Test getting its info. */
    1107                 :           2 :       res = g_resources_get_info ("/auto_loaded/test1.txt", 0, &size, NULL, &error);
    1108                 :           2 :       g_assert_true (res);
    1109                 :           2 :       g_assert_no_error (error);
    1110                 :             :       /* test1.txt is 6 bytes, test1.overlay is 23 */
    1111                 :           2 :       g_assert_cmpuint (size, ==, expected_overlay_size);
    1112                 :             : 
    1113                 :             :       /* Test it as a stream too. */
    1114                 :           2 :       in = g_resources_open_stream ("/auto_loaded/test1.txt",
    1115                 :             :                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
    1116                 :             :                                     &error);
    1117                 :           2 :       g_assert_no_error (error);
    1118                 :           2 :       g_assert_nonnull (in);
    1119                 :             : 
    1120                 :           2 :       res = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
    1121                 :             :                                      &size,
    1122                 :             :                                      NULL, &error);
    1123                 :           2 :       g_assert_no_error (error);
    1124                 :           2 :       g_assert_true (res);
    1125                 :           2 :       g_assert_cmpuint (size, ==, expected_overlay_size);
    1126                 :             : 
    1127                 :           2 :       g_input_stream_close (in, NULL, &error);
    1128                 :           2 :       g_assert_no_error (error);
    1129                 :           2 :       g_clear_object (&in);
    1130                 :             : 
    1131                 :             :       /* Test data lookup. */
    1132                 :           2 :       data = g_resources_lookup_data ("/auto_loaded/test1.txt",
    1133                 :             :                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
    1134                 :             :                                       &error);
    1135                 :           2 :       g_assert_nonnull (data);
    1136                 :           2 :       g_assert_no_error (error);
    1137                 :           2 :       size = g_bytes_get_size (data);
    1138                 :           2 :       g_assert_cmpuint (size, ==, expected_overlay_size);
    1139                 :           2 :       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, expected_overlay_data);
    1140                 :           2 :       g_bytes_unref (data);
    1141                 :             : 
    1142                 :           2 :       g_free (overlay);
    1143                 :           2 :       g_free (path);
    1144                 :           2 :       g_free (expected_overlay_data);
    1145                 :             : 
    1146                 :           2 :       return;
    1147                 :             :     }
    1148                 :           2 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR);
    1149                 :           2 :   g_test_trap_assert_passed ();
    1150                 :           2 : }
    1151                 :             : 
    1152                 :             : static void
    1153                 :           2 : test_resource_has_children (void)
    1154                 :             : {
    1155                 :             :   GResource *resource;
    1156                 :           2 :   GError *error = NULL;
    1157                 :             : 
    1158                 :           2 :   g_assert_true (g_resources_has_children ("/auto_loaded"));
    1159                 :           2 :   g_assert_true (g_resources_has_children ("/auto_loaded/"));
    1160                 :           2 :   g_assert_false (g_resources_has_children ("/auto_loaded/test1.txt"));
    1161                 :           2 :   g_assert_false (g_resources_has_children ("/no/such/prefix"));
    1162                 :           2 :   g_assert_false (g_resources_has_children (""));
    1163                 :             : 
    1164                 :           2 :   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
    1165                 :           2 :   g_assert_nonnull (resource);
    1166                 :           2 :   g_assert_no_error (error);
    1167                 :             : 
    1168                 :           2 :   g_assert_true (g_resource_has_children (resource, "/a_prefix"));
    1169                 :           2 :   g_assert_true (g_resource_has_children (resource, "/a_prefix/"));
    1170                 :           2 :   g_assert_false (g_resource_has_children (resource, "/a_prefix/test2.txt"));
    1171                 :           2 :   g_assert_false (g_resource_has_children (resource, "/no/such/prefix"));
    1172                 :           2 :   g_assert_false (g_resource_has_children (resource, ""));
    1173                 :             : 
    1174                 :           2 :   g_resource_unref (resource);
    1175                 :           2 : }
    1176                 :             : 
    1177                 :             : static void
    1178                 :           2 : test_resource_data_empty_entry (void)
    1179                 :             : {
    1180                 :             :   GHashTable *table;
    1181                 :             :   GvdbItem *item;
    1182                 :             :   GVariantBuilder builder;
    1183                 :             :   GVariant *v_data;
    1184                 :             :   GBytes *bytes;
    1185                 :             :   GResource *resource;
    1186                 :             :   GBytes *data;
    1187                 :           2 :   GError *error = NULL;
    1188                 :             : 
    1189                 :           2 :   g_test_summary ("Test looking up a resource entry with zero-length data. "
    1190                 :             :                    "This is a regression test: the code used to subtract 1 from "
    1191                 :             :                    "the data length without checking for zero, causing underflow.");
    1192                 :             : 
    1193                 :           2 :   table = gvdb_hash_table_new (NULL, NULL);
    1194                 :             : 
    1195                 :           2 :   item = gvdb_hash_table_insert (table, "/empty/data.txt");
    1196                 :           3 :   gvdb_item_set_parent (item,
    1197                 :           1 :                         gvdb_hash_table_insert (table, "/empty/"));
    1198                 :             : 
    1199                 :           2 :   g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("(uuay)"));
    1200                 :           2 :   g_variant_builder_add (&builder, "u", GUINT32_TO_LE (5)); /* size (uncompressed) */
    1201                 :           2 :   g_variant_builder_add (&builder, "u", 0); /* flags (not compressed) */
    1202                 :           2 :   v_data = g_variant_new_from_data (G_VARIANT_TYPE ("ay"),
    1203                 :             :                                     NULL, 0, TRUE, NULL, NULL);
    1204                 :           2 :   g_variant_builder_add_value (&builder, v_data);
    1205                 :           2 :   gvdb_item_set_value (item, g_variant_builder_end (&builder));
    1206                 :             : 
    1207                 :           2 :   bytes = gvdb_table_get_contents (table, FALSE);
    1208                 :           2 :   g_hash_table_unref (table);
    1209                 :             : 
    1210                 :           2 :   resource = g_resource_new_from_data (bytes, &error);
    1211                 :           2 :   g_assert_no_error (error);
    1212                 :           2 :   g_assert_nonnull (resource);
    1213                 :             : 
    1214                 :           2 :   data = g_resource_lookup_data (resource, "/empty/data.txt",
    1215                 :             :                                  G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
    1216                 :           2 :   g_assert_no_error (error);
    1217                 :           2 :   g_assert_nonnull (data);
    1218                 :           2 :   g_assert_cmpuint (g_bytes_get_size (data), ==, 0);
    1219                 :           2 :   g_bytes_unref (data);
    1220                 :             : 
    1221                 :           2 :   g_resource_unref (resource);
    1222                 :           2 :   g_bytes_unref (bytes);
    1223                 :           2 : }
    1224                 :             : 
    1225                 :             : int
    1226                 :           4 : main (int   argc,
    1227                 :             :       char *argv[])
    1228                 :             : {
    1229                 :           4 :   g_test_init (&argc, &argv, NULL);
    1230                 :             : 
    1231                 :           4 :   _g_test2_register_resource ();
    1232                 :           4 :   _digit_test_register_resource ();
    1233                 :             : 
    1234                 :           4 :   g_test_add_func ("/resource/file", test_resource_file);
    1235                 :           4 :   g_test_add_func ("/resource/file-path", test_resource_file_path);
    1236                 :           4 :   g_test_add_func ("/resource/data", test_resource_data);
    1237                 :           4 :   g_test_add_func ("/resource/data_unaligned", test_resource_data_unaligned);
    1238                 :           4 :   g_test_add_func ("/resource/data-corrupt", test_resource_data_corrupt);
    1239                 :           4 :   g_test_add_func ("/resource/data-corrupt-compression", test_resource_data_corrupt_compression);
    1240                 :           4 :   g_test_add_func ("/resource/data-empty", test_resource_data_empty);
    1241                 :           4 :   g_test_add_func ("/resource/registered", test_resource_registered);
    1242                 :           4 :   g_test_add_func ("/resource/manual", test_resource_manual);
    1243                 :           4 :   g_test_add_func ("/resource/manual2", test_resource_manual2);
    1244                 :             : #ifdef G_HAS_CONSTRUCTORS
    1245                 :           4 :   g_test_add_func ("/resource/automatic", test_resource_automatic);
    1246                 :             :   /* This only uses automatic resources too, so it tests the constructors and destructors */
    1247                 :           4 :   g_test_add_func ("/resource/module", test_resource_module);
    1248                 :           4 :   g_test_add_func ("/resource/binary-linked", test_resource_binary_linked);
    1249                 :             : #endif
    1250                 :           4 :   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
    1251                 :           4 :   g_test_add_func ("/resource/uri/file", test_uri_file);
    1252                 :           4 :   g_test_add_func ("/resource/64k", test_resource_64k);
    1253                 :           4 :   g_test_add_func ("/resource/overlay", test_overlay);
    1254                 :           4 :   g_test_add_func ("/resource/digits", test_resource_digits);
    1255                 :           4 :   g_test_add_func ("/resource/has-children", test_resource_has_children);
    1256                 :           4 :   g_test_add_func ("/resource/data-empty-entry", test_resource_data_empty_entry);
    1257                 :             : 
    1258                 :           4 :   return g_test_run();
    1259                 :             : }
        

Generated by: LCOV version 2.0-1