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