1 /* GLib testing framework examples and tests
3 * Copyright (C) 2011 Red Hat, Inc.
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.
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.
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/>.
21 #include "gconstructor.h"
22 #include "test_resources2.h"
23 #include "digit_test_resources.h"
26 test_resource (GResource *resource)
29 gboolean found, success;
36 const gchar *not_found_paths[] =
44 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
46 found = g_resource_get_info (resource,
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);
55 found = g_resource_get_info (resource,
57 G_RESOURCE_LOOKUP_FLAGS_NONE,
58 &size, &flags, &error);
60 g_assert_no_error (error);
61 g_assert_cmpint (size, ==, 6);
62 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
64 found = g_resource_get_info (resource,
65 "/a_prefix/test2.txt",
66 G_RESOURCE_LOOKUP_FLAGS_NONE,
67 &size, &flags, &error);
69 g_assert_no_error (error);
70 g_assert_cmpint (size, ==, 6);
71 g_assert_cmpuint (flags, ==, 0);
73 found = g_resource_get_info (resource,
74 "/a_prefix/test2-alias.txt",
75 G_RESOURCE_LOOKUP_FLAGS_NONE,
76 &size, &flags, &error);
78 g_assert_no_error (error);
79 g_assert_cmpint (size, ==, 6);
80 g_assert_cmpuint (flags, ==, 0);
82 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
84 data = g_resource_lookup_data (resource,
86 G_RESOURCE_LOOKUP_FLAGS_NONE,
88 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
89 g_clear_error (&error);
93 data = g_resource_lookup_data (resource,
95 G_RESOURCE_LOOKUP_FLAGS_NONE,
97 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
98 g_assert_no_error (error);
101 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
103 in = g_resource_open_stream (resource,
105 G_RESOURCE_LOOKUP_FLAGS_NONE,
107 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
108 g_clear_error (&error);
112 in = g_resource_open_stream (resource,
114 G_RESOURCE_LOOKUP_FLAGS_NONE,
116 g_assert (in != NULL);
117 g_assert_no_error (error);
119 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
123 g_assert_no_error (error);
124 g_assert_cmpint (size, ==, 6);
126 g_assert_cmpstr (buffer, ==, "test1\n");
128 g_input_stream_close (in, NULL, &error);
129 g_assert_no_error (error);
130 g_clear_object (&in);
132 data = g_resource_lookup_data (resource,
133 "/a_prefix/test2.txt",
134 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
143 data = g_resource_lookup_data (resource,
144 "/a_prefix/test2-alias.txt",
145 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
154 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
156 if (g_str_equal (not_found_paths[i], "/"))
159 children = g_resource_enumerate_children (resource,
161 G_RESOURCE_LOOKUP_FLAGS_NONE,
163 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
164 g_clear_error (&error);
165 g_assert_null (children);
168 children = g_resource_enumerate_children (resource,
170 G_RESOURCE_LOOKUP_FLAGS_NONE,
172 g_assert (children != NULL);
173 g_assert_no_error (error);
174 g_assert_cmpint (g_strv_length (children), ==, 2);
175 g_strfreev (children);
177 /* Test the preferred lookup where we have a trailing slash. */
178 children = g_resource_enumerate_children (resource,
180 G_RESOURCE_LOOKUP_FLAGS_NONE,
182 g_assert (children != NULL);
183 g_assert_no_error (error);
184 g_assert_cmpint (g_strv_length (children), ==, 2);
185 g_strfreev (children);
187 /* test with a path > 256 and no trailing slash to test the
188 * slow path of resources where we allocate a modified path.
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,
199 g_assert (children == NULL);
200 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
201 g_clear_error (&error);
205 test_resource_file (void)
208 GError *error = NULL;
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);
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);
219 test_resource (resource);
220 g_resource_unref (resource);
224 test_resource_file_path (void)
226 static const struct {
228 const gchar *expected;
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" },
248 for (i = 0; i < G_N_ELEMENTS (test_uris); i++)
253 file = g_file_new_for_uri (test_uris[i].input);
254 g_assert (file != NULL);
256 uri = g_file_get_uri (file);
257 g_assert (uri != NULL);
259 g_assert_cmpstr (uri, ==, test_uris[i].expected);
261 g_object_unref (file);
267 test_resource_data (void)
270 GError *error = NULL;
271 gboolean loaded_file;
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);
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);
286 test_resource (resource);
288 g_resource_unref (resource);
292 test_resource_data_unaligned (void)
295 GError *error = NULL;
296 gboolean loaded_file;
297 char *content, *content_copy;
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);
305 content_copy = g_new (char, content_size + 1);
306 memcpy (content_copy + 1, content, content_size);
308 data = g_bytes_new_with_free_func (content_copy + 1, content_size,
309 (GDestroyNotify) g_free, content_copy);
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);
316 test_resource (resource);
318 g_resource_unref (resource);
321 /* Test error handling for corrupt GResource files (specifically, a corrupt
324 test_resource_data_corrupt (void)
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;
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);
339 g_clear_error (&local_error);
342 /* Test handling for empty GResource files. They should also be treated as
345 test_resource_data_empty (void)
347 GBytes *bytes = NULL;
348 GResource *resource = NULL;
349 GError *local_error = NULL;
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);
357 g_clear_error (&local_error);
361 test_resource_registered (void)
364 GError *error = NULL;
365 gboolean found, success;
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);
377 found = g_resources_get_info ("/test1.txt",
378 G_RESOURCE_LOOKUP_FLAGS_NONE,
379 &size, &flags, &error);
381 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
382 g_clear_error (&error);
384 g_resources_register (resource);
386 found = g_resources_get_info ("/test1.txt",
387 G_RESOURCE_LOOKUP_FLAGS_NONE,
388 &size, &flags, &error);
390 g_assert_no_error (error);
391 g_assert_cmpint (size, ==, 6);
392 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
394 found = g_resources_get_info ("/a_prefix/test2.txt",
395 G_RESOURCE_LOOKUP_FLAGS_NONE,
396 &size, &flags, &error);
398 g_assert_no_error (error);
399 g_assert_cmpint (size, ==, 6);
400 g_assert_cmpint (flags, ==, 0);
402 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
403 G_RESOURCE_LOOKUP_FLAGS_NONE,
404 &size, &flags, &error);
406 g_assert_no_error (error);
407 g_assert_cmpint (size, ==, 6);
408 g_assert_cmpuint (flags, ==, 0);
410 data = g_resources_lookup_data ("/test1.txt",
411 G_RESOURCE_LOOKUP_FLAGS_NONE,
413 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
414 g_assert_no_error (error);
415 g_bytes_unref (data);
417 in = g_resources_open_stream ("/test1.txt",
418 G_RESOURCE_LOOKUP_FLAGS_NONE,
420 g_assert (in != NULL);
421 g_assert_no_error (error);
423 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
427 g_assert_no_error (error);
428 g_assert_cmpint (size, ==, 6);
430 g_assert_cmpstr (buffer, ==, "test1\n");
432 g_input_stream_close (in, NULL, &error);
433 g_assert_no_error (error);
434 g_clear_object (&in);
436 data = g_resources_lookup_data ("/a_prefix/test2.txt",
437 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
446 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
447 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
456 children = g_resources_enumerate_children ("/not/here",
457 G_RESOURCE_LOOKUP_FLAGS_NONE,
459 g_assert (children == NULL);
460 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
461 g_clear_error (&error);
463 children = g_resources_enumerate_children ("/a_prefix",
464 G_RESOURCE_LOOKUP_FLAGS_NONE,
466 g_assert (children != NULL);
467 g_assert_no_error (error);
468 g_assert_cmpint (g_strv_length (children), ==, 2);
469 g_strfreev (children);
471 g_resources_unregister (resource);
472 g_resource_unref (resource);
474 found = g_resources_get_info ("/test1.txt",
475 G_RESOURCE_LOOKUP_FLAGS_NONE,
476 &size, &flags, &error);
478 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
479 g_clear_error (&error);
483 test_resource_automatic (void)
485 GError *error = NULL;
491 found = g_resources_get_info ("/auto_loaded/test1.txt",
492 G_RESOURCE_LOOKUP_FLAGS_NONE,
493 &size, &flags, &error);
495 g_assert_no_error (error);
496 g_assert_cmpint (size, ==, 6);
497 g_assert_cmpint (flags, ==, 0);
499 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
500 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
511 test_resource_manual (void)
513 GError *error = NULL;
519 found = g_resources_get_info ("/manual_loaded/test1.txt",
520 G_RESOURCE_LOOKUP_FLAGS_NONE,
521 &size, &flags, &error);
523 g_assert_no_error (error);
524 g_assert_cmpint (size, ==, 6);
525 g_assert_cmpuint (flags, ==, 0);
527 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
528 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
539 test_resource_manual2 (void)
544 GError *error = NULL;
546 resource = _g_test2_get_resource ();
548 data = g_resource_lookup_data (resource,
549 "/manual_loaded/test1.txt",
550 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
559 g_resource_unref (resource);
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. */
567 test_resource_binary_linked (void)
570 g_test_skip ("--external-data test only works on Linux");
572 #else /* if __linux__ */
573 GError *error = NULL;
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);
587 data = g_resources_lookup_data ("/binary_linked/test1.txt",
588 G_RESOURCE_LOOKUP_FLAGS_NONE,
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__ */
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. */
604 test_resource_digits (void)
606 GError *error = NULL;
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);
620 data = g_resources_lookup_data ("/digit_test/test1.txt",
621 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
632 test_resource_module (void)
641 if (g_module_supported ())
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));
648 found = g_resources_get_info ("/resourceplugin/test1.txt",
649 G_RESOURCE_LOOKUP_FLAGS_NONE,
650 &size, &flags, &error);
652 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
653 g_clear_error (&error);
655 g_type_module_use (G_TYPE_MODULE (module));
657 found = g_resources_get_info ("/resourceplugin/test1.txt",
658 G_RESOURCE_LOOKUP_FLAGS_NONE,
659 &size, &flags, &error);
661 g_assert_no_error (error);
662 g_assert_cmpint (size, ==, 6);
663 g_assert_cmpuint (flags, ==, 0);
665 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
666 G_RESOURCE_LOOKUP_FLAGS_NONE,
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);
675 g_type_module_unuse (G_TYPE_MODULE (module));
677 found = g_resources_get_info ("/resourceplugin/test1.txt",
678 G_RESOURCE_LOOKUP_FLAGS_NONE,
679 &size, &flags, &error);
681 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
682 g_clear_error (&error);
687 test_uri_query_info (void)
690 GError *error = NULL;
691 gboolean loaded_file;
697 const char *content_type;
698 gchar *mime_type = NULL;
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);
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);
712 g_resources_register (resource);
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);
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");
725 g_object_unref (info);
727 info = g_file_query_filesystem_info (file, "*", NULL, &error);
728 g_assert_no_error (error);
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);
735 g_object_unref (info);
737 g_assert_cmpuint (g_file_hash (file), !=, 0);
739 g_object_unref (file);
741 g_resources_unregister (resource);
742 g_resource_unref (resource);
749 GError *error = NULL;
750 gboolean loaded_file;
757 GFile *file2, *parent;
758 GFileEnumerator *enumerator;
760 GFileAttributeInfoList *attrs;
761 GInputStream *stream;
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);
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);
776 g_resources_register (resource);
778 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
780 g_assert (g_file_get_path (file) == NULL);
782 name = g_file_get_parse_name (file);
783 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
786 name = g_file_get_uri (file);
787 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
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");
797 file2 = g_file_dup (file);
798 g_assert (g_file_equal (file, file2));
799 g_object_unref (file2);
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);
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);
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);
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);
820 info = g_file_enumerator_next_file (enumerator, NULL, &error);
821 g_assert_no_error (error);
822 g_assert (info == NULL);
824 g_file_enumerator_close (enumerator, NULL, &error);
825 g_assert_no_error (error);
826 g_object_unref (enumerator);
828 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
829 g_assert (g_file_equal (file, file2));
831 g_assert (g_file_has_prefix (file, parent));
833 name = g_file_get_relative_path (parent, file);
834 g_assert_cmpstr (name, ==, "test2-alias.txt");
837 g_object_unref (parent);
839 attrs = g_file_query_settable_attributes (file, NULL, &error);
840 g_assert_no_error (error);
841 g_file_attribute_info_list_unref (attrs);
843 attrs = g_file_query_writable_namespaces (file, NULL, &error);
844 g_assert_no_error (error);
845 g_file_attribute_info_list_unref (attrs);
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);
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);
858 memset (buf, 0, 1024);
859 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
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,
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);
872 ret = g_input_stream_close (stream, NULL, &error);
874 g_assert_no_error (error);
875 g_object_unref (stream);
877 g_object_unref (file);
878 g_object_unref (file2);
880 g_resources_unregister (resource);
881 g_resource_unref (resource);
885 test_resource_64k (void)
887 GError *error = NULL;
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);
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)
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,
911 g_assert_nonnull (data);
912 g_assert_no_error (error);
913 size = g_bytes_get_size (data);
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);
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");
925 g_bytes_unref (data);
928 /* Check that g_resources_get_info() respects G_RESOURCE_OVERLAYS */
932 if (g_test_subprocess ())
934 GError *error = NULL;
940 path = g_test_build_filename (G_TEST_DIST, "test1.overlay", NULL);
941 overlay = g_strconcat ("/auto_loaded/test1.txt=", path, NULL);
943 g_setenv ("G_RESOURCE_OVERLAYS", overlay, TRUE);
944 res = g_resources_get_info ("/auto_loaded/test1.txt", 0, &size, NULL, &error);
946 g_assert_no_error (error);
947 /* test1.txt is 6 bytes, test1.overlay is 23 */
948 g_assert_cmpint (size, ==, 23);
955 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR);
956 g_test_trap_assert_passed ();
963 g_test_init (&argc, &argv, NULL);
965 _g_test2_register_resource ();
966 _digit_test_register_resource ();
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);
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);