cb2c00a486f33f6ff42588e81837e6efbed22702
[platform/upstream/glib.git] / gio / tests / resources.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2011 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <string.h>
20 #include <gio/gio.h>
21 #include "gconstructor.h"
22 #include "test_resources2.h"
23 #include "digit_test_resources.h"
24
25 static void
26 test_resource (GResource *resource)
27 {
28   GError *error = NULL;
29   gboolean found, success;
30   gsize size;
31   guint32 flags;
32   GBytes *data;
33   char **children;
34   GInputStream *in;
35   char buffer[128];
36   const gchar *not_found_paths[] =
37     {
38       "/not/there",
39       "/",
40       "",
41     };
42   gsize i;
43
44   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
45     {
46       found = g_resource_get_info (resource,
47                                    not_found_paths[i],
48                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
49                                    &size, &flags, &error);
50       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
51       g_clear_error (&error);
52       g_assert_false (found);
53     }
54
55   found = g_resource_get_info (resource,
56                                "/test1.txt",
57                                G_RESOURCE_LOOKUP_FLAGS_NONE,
58                                &size, &flags, &error);
59   g_assert (found);
60   g_assert_no_error (error);
61   g_assert_cmpint (size, ==, 6);
62   g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
63
64   found = g_resource_get_info (resource,
65                                "/a_prefix/test2.txt",
66                                G_RESOURCE_LOOKUP_FLAGS_NONE,
67                                &size, &flags, &error);
68   g_assert (found);
69   g_assert_no_error (error);
70   g_assert_cmpint (size, ==, 6);
71   g_assert_cmpuint (flags, ==, 0);
72
73   found = g_resource_get_info (resource,
74                                "/a_prefix/test2-alias.txt",
75                                G_RESOURCE_LOOKUP_FLAGS_NONE,
76                                &size, &flags, &error);
77   g_assert (found);
78   g_assert_no_error (error);
79   g_assert_cmpint (size, ==, 6);
80   g_assert_cmpuint (flags, ==, 0);
81
82   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
83     {
84       data = g_resource_lookup_data (resource,
85                                      not_found_paths[i],
86                                      G_RESOURCE_LOOKUP_FLAGS_NONE,
87                                      &error);
88       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
89       g_clear_error (&error);
90       g_assert_null (data);
91     }
92
93   data = g_resource_lookup_data (resource,
94                                  "/test1.txt",
95                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
96                                  &error);
97   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
98   g_assert_no_error (error);
99   g_bytes_unref (data);
100
101   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
102     {
103       in = g_resource_open_stream (resource,
104                                    not_found_paths[i],
105                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
106                                    &error);
107       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
108       g_clear_error (&error);
109       g_assert_null (in);
110     }
111
112   in = g_resource_open_stream (resource,
113                                "/test1.txt",
114                                G_RESOURCE_LOOKUP_FLAGS_NONE,
115                                &error);
116   g_assert (in != NULL);
117   g_assert_no_error (error);
118
119   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
120                                      &size,
121                                      NULL, &error);
122   g_assert (success);
123   g_assert_no_error (error);
124   g_assert_cmpint (size, ==, 6);
125   buffer[size] = 0;
126   g_assert_cmpstr (buffer, ==, "test1\n");
127
128   g_input_stream_close (in, NULL, &error);
129   g_assert_no_error (error);
130   g_clear_object (&in);
131
132   data = g_resource_lookup_data (resource,
133                                  "/a_prefix/test2.txt",
134                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
135                                  &error);
136   g_assert (data != NULL);
137   g_assert_no_error (error);
138   size = g_bytes_get_size (data);
139   g_assert_cmpint (size, ==, 6);
140   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
141   g_bytes_unref (data);
142
143   data = g_resource_lookup_data (resource,
144                                  "/a_prefix/test2-alias.txt",
145                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
146                                  &error);
147   g_assert (data != NULL);
148   g_assert_no_error (error);
149   size = g_bytes_get_size (data);
150   g_assert_cmpint (size, ==, 6);
151   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
152   g_bytes_unref (data);
153
154   for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
155     {
156       if (g_str_equal (not_found_paths[i], "/"))
157         continue;
158
159       children = g_resource_enumerate_children (resource,
160                                                 not_found_paths[i],
161                                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
162                                                 &error);
163       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
164       g_clear_error (&error);
165       g_assert_null (children);
166     }
167
168   children = g_resource_enumerate_children  (resource,
169                                              "/a_prefix",
170                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
171                                              &error);
172   g_assert (children != NULL);
173   g_assert_no_error (error);
174   g_assert_cmpint (g_strv_length (children), ==, 2);
175   g_strfreev (children);
176
177   /* Test the preferred lookup where we have a trailing slash. */
178   children = g_resource_enumerate_children  (resource,
179                                              "/a_prefix/",
180                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
181                                              &error);
182   g_assert (children != NULL);
183   g_assert_no_error (error);
184   g_assert_cmpint (g_strv_length (children), ==, 2);
185   g_strfreev (children);
186
187   /* test with a path > 256 and no trailing slash to test the
188    * slow path of resources where we allocate a modified path.
189    */
190   children = g_resource_enumerate_children  (resource,
191                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
192                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
193                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
194                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
195                                              "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
196                                              "/with/no/trailing/slash",
197                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
198                                              &error);
199   g_assert (children == NULL);
200   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
201   g_clear_error (&error);
202 }
203
204 static void
205 test_resource_file (void)
206 {
207   GResource *resource;
208   GError *error = NULL;
209
210   resource = g_resource_load ("not-there", &error);
211   g_assert (resource == NULL);
212   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
213   g_clear_error (&error);
214
215   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
216   g_assert (resource != NULL);
217   g_assert_no_error (error);
218
219   test_resource (resource);
220   g_resource_unref (resource);
221 }
222
223 static void
224 test_resource_file_path (void)
225 {
226   static const struct {
227     const gchar *input;
228     const gchar *expected;
229   } test_uris[] = {
230     { "resource://", "resource:///" },
231     { "resource:///", "resource:///" },
232     { "resource://////", "resource:///" },
233     { "resource:///../../../", "resource:///" },
234     { "resource:///../../..", "resource:///" },
235     { "resource://abc", "resource:///abc" },
236     { "resource:///abc/", "resource:///abc" },
237     { "resource:/a/b/../c/", "resource:///a/c" },
238     { "resource://../a/b/../c/../", "resource:///a" },
239     { "resource://a/b/cc//bb//a///", "resource:///a/b/cc/bb/a" },
240     { "resource://././././", "resource:///" },
241     { "resource://././././../", "resource:///" },
242     { "resource://a/b/c/d.png", "resource:///a/b/c/d.png" },
243     { "resource://a/b/c/..png", "resource:///a/b/c/..png" },
244     { "resource://a/b/c/./png", "resource:///a/b/c/png" },
245   };
246   guint i;
247
248   for (i = 0; i < G_N_ELEMENTS (test_uris); i++)
249     {
250       GFile *file;
251       gchar *uri;
252
253       file = g_file_new_for_uri (test_uris[i].input);
254       g_assert (file != NULL);
255
256       uri = g_file_get_uri (file);
257       g_assert (uri != NULL);
258
259       g_assert_cmpstr (uri, ==, test_uris[i].expected);
260
261       g_object_unref (file);
262       g_free (uri);
263     }
264 }
265
266 static void
267 test_resource_data (void)
268 {
269   GResource *resource;
270   GError *error = NULL;
271   gboolean loaded_file;
272   char *content;
273   gsize content_size;
274   GBytes *data;
275
276   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
277                                      &content, &content_size, NULL);
278   g_assert (loaded_file);
279
280   data = g_bytes_new_take (content, content_size);
281   resource = g_resource_new_from_data (data, &error);
282   g_bytes_unref (data);
283   g_assert (resource != NULL);
284   g_assert_no_error (error);
285
286   test_resource (resource);
287
288   g_resource_unref (resource);
289 }
290
291 static void
292 test_resource_data_unaligned (void)
293 {
294   GResource *resource;
295   GError *error = NULL;
296   gboolean loaded_file;
297   char *content, *content_copy;
298   gsize content_size;
299   GBytes *data;
300
301   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
302                                      &content, &content_size, NULL);
303   g_assert (loaded_file);
304
305   content_copy = g_new (char, content_size + 1);
306   memcpy (content_copy + 1, content, content_size);
307
308   data = g_bytes_new_with_free_func (content_copy + 1, content_size,
309                                      (GDestroyNotify) g_free, content_copy);
310   g_free (content);
311   resource = g_resource_new_from_data (data, &error);
312   g_bytes_unref (data);
313   g_assert (resource != NULL);
314   g_assert_no_error (error);
315
316   test_resource (resource);
317
318   g_resource_unref (resource);
319 }
320
321 /* Test error handling for corrupt GResource files (specifically, a corrupt
322  * GVDB header). */
323 static void
324 test_resource_data_corrupt (void)
325 {
326   /* A GVDB header is 6 guint32s, and requires a magic number in the first two
327    * guint32s. A set of zero bytes of a greater length is considered corrupt. */
328   static const guint8 data[sizeof (guint32) * 7] = { 0, };
329   GBytes *bytes = NULL;
330   GResource *resource = NULL;
331   GError *local_error = NULL;
332
333   bytes = g_bytes_new_static (data, sizeof (data));
334   resource = g_resource_new_from_data (bytes, &local_error);
335   g_bytes_unref (bytes);
336   g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
337   g_assert_null (resource);
338
339   g_clear_error (&local_error);
340 }
341
342 /* Test handling for empty GResource files. They should also be treated as
343  * corrupt. */
344 static void
345 test_resource_data_empty (void)
346 {
347   GBytes *bytes = NULL;
348   GResource *resource = NULL;
349   GError *local_error = NULL;
350
351   bytes = g_bytes_new_static (NULL, 0);
352   resource = g_resource_new_from_data (bytes, &local_error);
353   g_bytes_unref (bytes);
354   g_assert_error (local_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL);
355   g_assert_null (resource);
356
357   g_clear_error (&local_error);
358 }
359
360 static void
361 test_resource_registered (void)
362 {
363   GResource *resource;
364   GError *error = NULL;
365   gboolean found, success;
366   gsize size;
367   guint32 flags;
368   GBytes *data;
369   char **children;
370   GInputStream *in;
371   char buffer[128];
372
373   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
374   g_assert (resource != NULL);
375   g_assert_no_error (error);
376
377   found = g_resources_get_info ("/test1.txt",
378                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
379                                 &size, &flags, &error);
380   g_assert (!found);
381   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
382   g_clear_error (&error);
383
384   g_resources_register (resource);
385
386   found = g_resources_get_info ("/test1.txt",
387                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
388                                 &size, &flags, &error);
389   g_assert (found);
390   g_assert_no_error (error);
391   g_assert_cmpint (size, ==, 6);
392   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
393
394   found = g_resources_get_info ("/a_prefix/test2.txt",
395                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
396                                 &size, &flags, &error);
397   g_assert (found);
398   g_assert_no_error (error);
399   g_assert_cmpint (size, ==, 6);
400   g_assert_cmpint (flags, ==, 0);
401
402   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
403                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
404                                 &size, &flags, &error);
405   g_assert (found);
406   g_assert_no_error (error);
407   g_assert_cmpint (size, ==, 6);
408   g_assert_cmpuint (flags, ==, 0);
409
410   data = g_resources_lookup_data ("/test1.txt",
411                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
412                                   &error);
413   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
414   g_assert_no_error (error);
415   g_bytes_unref (data);
416
417   in = g_resources_open_stream ("/test1.txt",
418                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
419                                 &error);
420   g_assert (in != NULL);
421   g_assert_no_error (error);
422
423   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
424                                      &size,
425                                      NULL, &error);
426   g_assert (success);
427   g_assert_no_error (error);
428   g_assert_cmpint (size, ==, 6);
429   buffer[size] = 0;
430   g_assert_cmpstr (buffer, ==, "test1\n");
431
432   g_input_stream_close (in, NULL, &error);
433   g_assert_no_error (error);
434   g_clear_object (&in);
435
436   data = g_resources_lookup_data ("/a_prefix/test2.txt",
437                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
438                                   &error);
439   g_assert (data != NULL);
440   g_assert_no_error (error);
441   size = g_bytes_get_size (data);
442   g_assert_cmpint (size, ==, 6);
443   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
444   g_bytes_unref (data);
445
446   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
447                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
448                                   &error);
449   g_assert (data != NULL);
450   g_assert_no_error (error);
451   size = g_bytes_get_size (data);
452   g_assert_cmpint (size, ==, 6);
453   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
454   g_bytes_unref (data);
455
456   children = g_resources_enumerate_children ("/not/here",
457                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
458                                              &error);
459   g_assert (children == NULL);
460   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
461   g_clear_error (&error);
462
463   children = g_resources_enumerate_children ("/a_prefix",
464                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
465                                              &error);
466   g_assert (children != NULL);
467   g_assert_no_error (error);
468   g_assert_cmpint (g_strv_length (children), ==, 2);
469   g_strfreev (children);
470
471   g_resources_unregister (resource);
472   g_resource_unref (resource);
473
474   found = g_resources_get_info ("/test1.txt",
475                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
476                                 &size, &flags, &error);
477   g_assert (!found);
478   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
479   g_clear_error (&error);
480 }
481
482 static void
483 test_resource_automatic (void)
484 {
485   GError *error = NULL;
486   gboolean found;
487   gsize size;
488   guint32 flags;
489   GBytes *data;
490
491   found = g_resources_get_info ("/auto_loaded/test1.txt",
492                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
493                                 &size, &flags, &error);
494   g_assert (found);
495   g_assert_no_error (error);
496   g_assert_cmpint (size, ==, 6);
497   g_assert_cmpint (flags, ==, 0);
498
499   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
500                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
501                                   &error);
502   g_assert (data != NULL);
503   g_assert_no_error (error);
504   size = g_bytes_get_size (data);
505   g_assert_cmpint (size, ==, 6);
506   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
507   g_bytes_unref (data);
508 }
509
510 static void
511 test_resource_manual (void)
512 {
513   GError *error = NULL;
514   gboolean found;
515   gsize size;
516   guint32 flags;
517   GBytes *data;
518
519   found = g_resources_get_info ("/manual_loaded/test1.txt",
520                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
521                                 &size, &flags, &error);
522   g_assert (found);
523   g_assert_no_error (error);
524   g_assert_cmpint (size, ==, 6);
525   g_assert_cmpuint (flags, ==, 0);
526
527   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
528                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
529                                   &error);
530   g_assert (data != NULL);
531   g_assert_no_error (error);
532   size = g_bytes_get_size (data);
533   g_assert_cmpint (size, ==, 6);
534   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
535   g_bytes_unref (data);
536 }
537
538 static void
539 test_resource_manual2 (void)
540 {
541   GResource *resource;
542   GBytes *data;
543   gsize size;
544   GError *error = NULL;
545
546   resource = _g_test2_get_resource ();
547
548   data = g_resource_lookup_data (resource,
549                                  "/manual_loaded/test1.txt",
550                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
551                                  &error);
552   g_assert (data != NULL);
553   g_assert_no_error (error);
554   size = g_bytes_get_size (data);
555   g_assert_cmpint (size, ==, 6);
556   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
557   g_bytes_unref (data);
558
559   g_resource_unref (resource);
560 }
561
562 /* Test building resources with external data option,
563  * where data is linked in as binary instead of compiled in.
564  * Checks if resources are automatically registered and
565  * data can be found and read. */
566 static void
567 test_resource_binary_linked (void)
568 {
569   #ifndef __linux__
570   g_test_skip ("--external-data test only works on Linux");
571   return;
572   #else /* if __linux__ */
573   GError *error = NULL;
574   gboolean found;
575   gsize size;
576   guint32 flags;
577   GBytes *data;
578
579   found = g_resources_get_info ("/binary_linked/test1.txt",
580                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
581                                 &size, &flags, &error);
582   g_assert_true (found);
583   g_assert_no_error (error);
584   g_assert_cmpint (size, ==, 6);
585   g_assert_cmpuint (flags, ==, 0);
586
587   data = g_resources_lookup_data ("/binary_linked/test1.txt",
588                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
589                                   &error);
590   g_assert_nonnull (data);
591   g_assert_no_error (error);
592   size = g_bytes_get_size (data);
593   g_assert_cmpint (size, ==, 6);
594   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
595   g_bytes_unref (data);
596   #endif /* if __linux__ */
597 }
598
599 /* Test resource whose xml file starts with more than one digit
600  * and where no explicit c-name is given
601  * Checks if resources are sucessfully registered and
602  * data can be found and read. */
603 static void
604 test_resource_digits (void)
605 {
606   GError *error = NULL;
607   gboolean found;
608   gsize size;
609   guint32 flags;
610   GBytes *data;
611
612   found = g_resources_get_info ("/digit_test/test1.txt",
613                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
614                                 &size, &flags, &error);
615   g_assert_true (found);
616   g_assert_no_error (error);
617   g_assert_cmpint (size, ==, 6);
618   g_assert_cmpuint (flags, ==, 0);
619
620   data = g_resources_lookup_data ("/digit_test/test1.txt",
621                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
622                                   &error);
623   g_assert_nonnull (data);
624   g_assert_no_error (error);
625   size = g_bytes_get_size (data);
626   g_assert_cmpint (size, ==, 6);
627   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
628   g_bytes_unref (data);
629 }
630
631 static void
632 test_resource_module (void)
633 {
634   GIOModule *module;
635   gboolean found;
636   gsize size;
637   guint32 flags;
638   GBytes *data;
639   GError *error;
640
641   if (g_module_supported ())
642     {
643       /* For in-tree, this will find the .la file and use it to get to the .so in .libs/ */
644       module = g_io_module_new (g_test_get_filename (G_TEST_BUILT, "libresourceplugin",  NULL));
645
646       error = NULL;
647
648       found = g_resources_get_info ("/resourceplugin/test1.txt",
649                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
650                                     &size, &flags, &error);
651       g_assert (!found);
652       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
653       g_clear_error (&error);
654
655       g_type_module_use (G_TYPE_MODULE (module));
656
657       found = g_resources_get_info ("/resourceplugin/test1.txt",
658                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
659                                     &size, &flags, &error);
660       g_assert (found);
661       g_assert_no_error (error);
662       g_assert_cmpint (size, ==, 6);
663       g_assert_cmpuint (flags, ==, 0);
664
665       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
666                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
667                                       &error);
668       g_assert (data != NULL);
669       g_assert_no_error (error);
670       size = g_bytes_get_size (data);
671       g_assert_cmpint (size, ==, 6);
672       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
673       g_bytes_unref (data);
674
675       g_type_module_unuse (G_TYPE_MODULE (module));
676
677       found = g_resources_get_info ("/resourceplugin/test1.txt",
678                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
679                                     &size, &flags, &error);
680       g_assert (!found);
681       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
682       g_clear_error (&error);
683     }
684 }
685
686 static void
687 test_uri_query_info (void)
688 {
689   GResource *resource;
690   GError *error = NULL;
691   gboolean loaded_file;
692   char *content;
693   gsize content_size;
694   GBytes *data;
695   GFile *file;
696   GFileInfo *info;
697   const char *content_type;
698   gchar *mime_type = NULL;
699   const char *fs_type;
700   gboolean readonly;
701
702   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
703                                      &content, &content_size, NULL);
704   g_assert (loaded_file);
705
706   data = g_bytes_new_take (content, content_size);
707   resource = g_resource_new_from_data (data, &error);
708   g_bytes_unref (data);
709   g_assert (resource != NULL);
710   g_assert_no_error (error);
711
712   g_resources_register (resource);
713
714   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
715   info = g_file_query_info (file, "*", 0, NULL, &error);
716   g_assert_no_error (error);
717
718   content_type = g_file_info_get_content_type (info);
719   g_assert (content_type);
720   mime_type = g_content_type_get_mime_type (content_type);
721   g_assert (mime_type);
722   g_assert_cmpstr (mime_type, ==, "text/plain");
723   g_free (mime_type);
724
725   g_object_unref (info);
726
727   info = g_file_query_filesystem_info (file, "*", NULL, &error);
728   g_assert_no_error (error);
729
730   fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
731   g_assert_cmpstr (fs_type, ==, "resource");
732   readonly = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
733   g_assert_true (readonly);
734
735   g_object_unref (info);
736
737   g_assert_cmpuint  (g_file_hash (file), !=, 0);
738
739   g_object_unref (file);
740
741   g_resources_unregister (resource);
742   g_resource_unref (resource);
743 }
744
745 static void
746 test_uri_file (void)
747 {
748   GResource *resource;
749   GError *error = NULL;
750   gboolean loaded_file;
751   char *content;
752   gsize content_size;
753   GBytes *data;
754   GFile *file;
755   GFileInfo *info;
756   gchar *name;
757   GFile *file2, *parent;
758   GFileEnumerator *enumerator;
759   gchar *scheme;
760   GFileAttributeInfoList *attrs;
761   GInputStream *stream;
762   gchar buf[1024];
763   gboolean ret;
764   gssize skipped;
765
766   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
767                                      &content, &content_size, NULL);
768   g_assert (loaded_file);
769
770   data = g_bytes_new_take (content, content_size);
771   resource = g_resource_new_from_data (data, &error);
772   g_bytes_unref (data);
773   g_assert (resource != NULL);
774   g_assert_no_error (error);
775
776   g_resources_register (resource);
777
778   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
779
780   g_assert (g_file_get_path (file) == NULL);
781
782   name = g_file_get_parse_name (file);
783   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
784   g_free (name);
785
786   name = g_file_get_uri (file);
787   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
788   g_free (name);
789
790   g_assert (!g_file_is_native (file));
791   g_assert (!g_file_has_uri_scheme (file, "http"));
792   g_assert (g_file_has_uri_scheme (file, "resource"));
793   scheme = g_file_get_uri_scheme (file);
794   g_assert_cmpstr (scheme, ==, "resource");
795   g_free (scheme);
796
797   file2 = g_file_dup (file);
798   g_assert (g_file_equal (file, file2));
799   g_object_unref (file2);
800
801   parent = g_file_get_parent (file);
802   enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
803   g_assert_no_error (error);
804
805   file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
806   g_assert_no_error (error);
807   g_assert (g_file_equal (file, file2));
808   g_object_unref (file2);
809
810   info = g_file_enumerator_next_file (enumerator, NULL, &error);
811   g_assert_no_error (error);
812   g_assert (info != NULL);
813   g_object_unref (info);
814
815   info = g_file_enumerator_next_file (enumerator, NULL, &error);
816   g_assert_no_error (error);
817   g_assert (info != NULL);
818   g_object_unref (info);
819
820   info = g_file_enumerator_next_file (enumerator, NULL, &error);
821   g_assert_no_error (error);
822   g_assert (info == NULL);
823
824   g_file_enumerator_close (enumerator, NULL, &error);
825   g_assert_no_error (error);
826   g_object_unref (enumerator);
827
828   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
829   g_assert (g_file_equal (file, file2));
830
831   g_assert (g_file_has_prefix (file, parent));
832
833   name = g_file_get_relative_path (parent, file);
834   g_assert_cmpstr (name, ==, "test2-alias.txt");
835   g_free (name);
836
837   g_object_unref (parent);
838
839   attrs = g_file_query_settable_attributes (file, NULL, &error);
840   g_assert_no_error (error);
841   g_file_attribute_info_list_unref (attrs);
842
843   attrs = g_file_query_writable_namespaces (file, NULL, &error);
844   g_assert_no_error (error);
845   g_file_attribute_info_list_unref (attrs);
846
847   stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
848   g_assert_no_error (error);
849   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
850   g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
851   ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
852   g_assert (ret);
853   g_assert_no_error (error);
854   skipped = g_input_stream_skip (stream, 1, NULL, &error);
855   g_assert_cmpint (skipped, ==, 1);
856   g_assert_no_error (error);
857
858   memset (buf, 0, 1024);
859   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
860   g_assert (ret);
861   g_assert_no_error (error);
862   g_assert_cmpstr (buf, ==, "st2\n");
863   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
864                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
865                                          NULL,
866                                          &error);
867   g_assert_no_error (error);
868   g_assert (info != NULL);
869   g_assert_cmpint (g_file_info_get_size (info), ==, 6);
870   g_object_unref (info);
871
872   ret = g_input_stream_close (stream, NULL, &error);
873   g_assert (ret);
874   g_assert_no_error (error);
875   g_object_unref (stream);
876
877   g_object_unref (file);
878   g_object_unref (file2);
879
880   g_resources_unregister (resource);
881   g_resource_unref (resource);
882 }
883
884 static void
885 test_resource_64k (void)
886 {
887   GError *error = NULL;
888   gboolean found;
889   gsize size;
890   guint32 flags;
891   GBytes *data;
892   gchar **tokens;
893
894   found = g_resources_get_info ("/big_prefix/gresource-big-test.txt",
895                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
896                                 &size, &flags, &error);
897   g_assert_true (found);
898   g_assert_no_error (error);
899
900   /* Check size: 100 of all lower case letters + newline char +
901    *             100 all upper case letters + newline char +
902    *             100 of all numbers between 0 to 9 + newline char
903    *             (for 12 iterations)
904    */
905
906   g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
907   g_assert_cmpuint (flags, ==, 0);
908   data = g_resources_lookup_data ("/big_prefix/gresource-big-test.txt",
909                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
910                                   &error);
911   g_assert_nonnull (data);
912   g_assert_no_error (error);
913   size = g_bytes_get_size (data);
914
915   g_assert_cmpint (size, ==, (26 + 26 + 10) * (100 + 1) * 12);
916   tokens = g_strsplit ((const gchar *) g_bytes_get_data (data, NULL), "\n", -1);
917
918   /* check tokens[x] == entry at gresource-big-test.txt's line, where x = line - 1 */
919   g_assert_cmpstr (tokens[0], ==, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
920   g_assert_cmpstr (tokens[27], ==, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
921   g_assert_cmpstr (tokens[183], ==, "7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777");
922   g_assert_cmpstr (tokens[600], ==, "QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
923   g_assert_cmpstr (tokens[742], ==, "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888");
924   g_strfreev (tokens);
925   g_bytes_unref (data);
926 }
927
928 /* Check that g_resources_get_info() respects G_RESOURCE_OVERLAYS */
929 static void
930 test_overlay (void)
931 {
932   if (g_test_subprocess ())
933     {
934        GError *error = NULL;
935        gboolean res;
936        gsize size;
937        char *overlay;
938        char *path;
939
940        path = g_test_build_filename (G_TEST_DIST, "test1.overlay", NULL);
941        overlay = g_strconcat ("/auto_loaded/test1.txt=", path, NULL);
942
943        g_setenv ("G_RESOURCE_OVERLAYS", overlay, TRUE);
944        res = g_resources_get_info ("/auto_loaded/test1.txt", 0, &size, NULL, &error);
945        g_assert_true (res);
946        g_assert_no_error (error);
947        /* test1.txt is 6 bytes, test1.overlay is 23 */
948        g_assert_cmpint (size, ==, 23);
949
950        g_free (overlay);
951        g_free (path);
952
953        return;
954     }
955   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR);
956   g_test_trap_assert_passed ();
957 }
958
959 int
960 main (int   argc,
961       char *argv[])
962 {
963   g_test_init (&argc, &argv, NULL);
964
965   _g_test2_register_resource ();
966   _digit_test_register_resource ();
967
968   g_test_add_func ("/resource/file", test_resource_file);
969   g_test_add_func ("/resource/file-path", test_resource_file_path);
970   g_test_add_func ("/resource/data", test_resource_data);
971   g_test_add_func ("/resource/data_unaligned", test_resource_data_unaligned);
972   g_test_add_func ("/resource/data-corrupt", test_resource_data_corrupt);
973   g_test_add_func ("/resource/data-empty", test_resource_data_empty);
974   g_test_add_func ("/resource/registered", test_resource_registered);
975   g_test_add_func ("/resource/manual", test_resource_manual);
976   g_test_add_func ("/resource/manual2", test_resource_manual2);
977 #ifdef G_HAS_CONSTRUCTORS
978   g_test_add_func ("/resource/automatic", test_resource_automatic);
979   /* This only uses automatic resources too, so it tests the constructors and destructors */
980   g_test_add_func ("/resource/module", test_resource_module);
981   g_test_add_func ("/resource/binary-linked", test_resource_binary_linked);
982 #endif
983   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
984   g_test_add_func ("/resource/uri/file", test_uri_file);
985   g_test_add_func ("/resource/64k", test_resource_64k);
986   g_test_add_func ("/resource/overlay", test_overlay);
987   g_test_add_func ("/resource/digits", test_resource_digits);
988
989   return g_test_run();
990 }