Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / resources.c
blob6ae8e7d64779b9ee90682944d72c43da0f90a854
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/>.
19 #include <string.h>
20 #include <gio/gio.h>
21 #include "gconstructor.h"
22 #include "test_resources2.h"
24 static void
25 test_resource (GResource *resource)
27 GError *error = NULL;
28 gboolean found, success;
29 gsize size;
30 guint32 flags;
31 GBytes *data;
32 char **children;
33 GInputStream *in;
34 char buffer[128];
35 const gchar *not_found_paths[] =
37 "/not/there",
38 "/",
39 "",
41 gsize i;
43 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
45 found = g_resource_get_info (resource,
46 not_found_paths[i],
47 G_RESOURCE_LOOKUP_FLAGS_NONE,
48 &size, &flags, &error);
49 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
50 g_clear_error (&error);
51 g_assert_false (found);
54 found = g_resource_get_info (resource,
55 "/test1.txt",
56 G_RESOURCE_LOOKUP_FLAGS_NONE,
57 &size, &flags, &error);
58 g_assert (found);
59 g_assert_no_error (error);
60 g_assert_cmpint (size, ==, 6);
61 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
63 found = g_resource_get_info (resource,
64 "/a_prefix/test2.txt",
65 G_RESOURCE_LOOKUP_FLAGS_NONE,
66 &size, &flags, &error);
67 g_assert (found);
68 g_assert_no_error (error);
69 g_assert_cmpint (size, ==, 6);
70 g_assert_cmpuint (flags, ==, 0);
72 found = g_resource_get_info (resource,
73 "/a_prefix/test2-alias.txt",
74 G_RESOURCE_LOOKUP_FLAGS_NONE,
75 &size, &flags, &error);
76 g_assert (found);
77 g_assert_no_error (error);
78 g_assert_cmpint (size, ==, 6);
79 g_assert_cmpuint (flags, ==, 0);
81 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
83 data = g_resource_lookup_data (resource,
84 not_found_paths[i],
85 G_RESOURCE_LOOKUP_FLAGS_NONE,
86 &error);
87 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
88 g_clear_error (&error);
89 g_assert_null (data);
92 data = g_resource_lookup_data (resource,
93 "/test1.txt",
94 G_RESOURCE_LOOKUP_FLAGS_NONE,
95 &error);
96 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
97 g_assert_no_error (error);
98 g_bytes_unref (data);
100 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
102 in = g_resource_open_stream (resource,
103 not_found_paths[i],
104 G_RESOURCE_LOOKUP_FLAGS_NONE,
105 &error);
106 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
107 g_clear_error (&error);
108 g_assert_null (in);
111 in = g_resource_open_stream (resource,
112 "/test1.txt",
113 G_RESOURCE_LOOKUP_FLAGS_NONE,
114 &error);
115 g_assert (in != NULL);
116 g_assert_no_error (error);
118 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
119 &size,
120 NULL, &error);
121 g_assert (success);
122 g_assert_no_error (error);
123 g_assert_cmpint (size, ==, 6);
124 buffer[size] = 0;
125 g_assert_cmpstr (buffer, ==, "test1\n");
127 g_input_stream_close (in, NULL, &error);
128 g_assert_no_error (error);
129 g_clear_object (&in);
131 data = g_resource_lookup_data (resource,
132 "/a_prefix/test2.txt",
133 G_RESOURCE_LOOKUP_FLAGS_NONE,
134 &error);
135 g_assert (data != NULL);
136 g_assert_no_error (error);
137 size = g_bytes_get_size (data);
138 g_assert_cmpint (size, ==, 6);
139 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
140 g_bytes_unref (data);
142 data = g_resource_lookup_data (resource,
143 "/a_prefix/test2-alias.txt",
144 G_RESOURCE_LOOKUP_FLAGS_NONE,
145 &error);
146 g_assert (data != NULL);
147 g_assert_no_error (error);
148 size = g_bytes_get_size (data);
149 g_assert_cmpint (size, ==, 6);
150 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
151 g_bytes_unref (data);
153 for (i = 0; i < G_N_ELEMENTS (not_found_paths); i++)
155 if (g_str_equal (not_found_paths[i], "/"))
156 continue;
158 children = g_resource_enumerate_children (resource,
159 not_found_paths[i],
160 G_RESOURCE_LOOKUP_FLAGS_NONE,
161 &error);
162 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
163 g_clear_error (&error);
164 g_assert_null (children);
167 children = g_resource_enumerate_children (resource,
168 "/a_prefix",
169 G_RESOURCE_LOOKUP_FLAGS_NONE,
170 &error);
171 g_assert (children != NULL);
172 g_assert_no_error (error);
173 g_assert_cmpint (g_strv_length (children), ==, 2);
174 g_strfreev (children);
176 /* Test the preferred lookup where we have a trailing slash. */
177 children = g_resource_enumerate_children (resource,
178 "/a_prefix/",
179 G_RESOURCE_LOOKUP_FLAGS_NONE,
180 &error);
181 g_assert (children != NULL);
182 g_assert_no_error (error);
183 g_assert_cmpint (g_strv_length (children), ==, 2);
184 g_strfreev (children);
186 /* test with a path > 256 and no trailing slash to test the
187 * slow path of resources where we allocate a modified path.
189 children = g_resource_enumerate_children (resource,
190 "/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
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 "/with/no/trailing/slash",
196 G_RESOURCE_LOOKUP_FLAGS_NONE,
197 &error);
198 g_assert (children == NULL);
199 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
200 g_clear_error (&error);
203 static void
204 test_resource_file (void)
206 GResource *resource;
207 GError *error = NULL;
209 resource = g_resource_load ("not-there", &error);
210 g_assert (resource == NULL);
211 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
212 g_clear_error (&error);
214 resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
215 g_assert (resource != NULL);
216 g_assert_no_error (error);
218 test_resource (resource);
219 g_resource_unref (resource);
222 static void
223 test_resource_file_path (void)
225 static const struct {
226 const gchar *input;
227 const gchar *expected;
228 } test_uris[] = {
229 { "resource://", "resource:///" },
230 { "resource:///", "resource:///" },
231 { "resource://////", "resource:///" },
232 { "resource:///../../../", "resource:///" },
233 { "resource:///../../..", "resource:///" },
234 { "resource://abc", "resource:///abc" },
235 { "resource:///abc/", "resource:///abc" },
236 { "resource:/a/b/../c/", "resource:///a/c" },
237 { "resource://../a/b/../c/../", "resource:///a" },
238 { "resource://a/b/cc//bb//a///", "resource:///a/b/cc/bb/a" },
239 { "resource://././././", "resource:///" },
240 { "resource://././././../", "resource:///" },
241 { "resource://a/b/c/d.png", "resource:///a/b/c/d.png" },
242 { "resource://a/b/c/..png", "resource:///a/b/c/..png" },
243 { "resource://a/b/c/./png", "resource:///a/b/c/png" },
245 guint i;
247 for (i = 0; i < G_N_ELEMENTS (test_uris); i++)
249 GFile *file;
250 gchar *uri;
252 file = g_file_new_for_uri (test_uris[i].input);
253 g_assert (file != NULL);
255 uri = g_file_get_uri (file);
256 g_assert (uri != NULL);
258 g_assert_cmpstr (uri, ==, test_uris[i].expected);
260 g_object_unref (file);
261 g_free (uri);
265 static void
266 test_resource_data (void)
268 GResource *resource;
269 GError *error = NULL;
270 gboolean loaded_file;
271 char *content;
272 gsize content_size;
273 GBytes *data;
275 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
276 &content, &content_size, NULL);
277 g_assert (loaded_file);
279 data = g_bytes_new_take (content, content_size);
280 resource = g_resource_new_from_data (data, &error);
281 g_bytes_unref (data);
282 g_assert (resource != NULL);
283 g_assert_no_error (error);
285 test_resource (resource);
287 g_resource_unref (resource);
290 static void
291 test_resource_data_unaligned (void)
293 GResource *resource;
294 GError *error = NULL;
295 gboolean loaded_file;
296 char *content, *content_copy;
297 gsize content_size;
298 GBytes *data;
300 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
301 &content, &content_size, NULL);
302 g_assert (loaded_file);
304 content_copy = g_new (char, content_size + 1);
305 memcpy (content_copy + 1, content, content_size);
307 data = g_bytes_new_with_free_func (content_copy + 1, content_size,
308 (GDestroyNotify) g_free, content_copy);
309 g_free (content);
310 resource = g_resource_new_from_data (data, &error);
311 g_bytes_unref (data);
312 g_assert (resource != NULL);
313 g_assert_no_error (error);
315 test_resource (resource);
317 g_resource_unref (resource);
320 static void
321 test_resource_registered (void)
323 GResource *resource;
324 GError *error = NULL;
325 gboolean found, success;
326 gsize size;
327 guint32 flags;
328 GBytes *data;
329 char **children;
330 GInputStream *in;
331 char buffer[128];
333 resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
334 g_assert (resource != NULL);
335 g_assert_no_error (error);
337 found = g_resources_get_info ("/test1.txt",
338 G_RESOURCE_LOOKUP_FLAGS_NONE,
339 &size, &flags, &error);
340 g_assert (!found);
341 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
342 g_clear_error (&error);
344 g_resources_register (resource);
346 found = g_resources_get_info ("/test1.txt",
347 G_RESOURCE_LOOKUP_FLAGS_NONE,
348 &size, &flags, &error);
349 g_assert (found);
350 g_assert_no_error (error);
351 g_assert_cmpint (size, ==, 6);
352 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
354 found = g_resources_get_info ("/a_prefix/test2.txt",
355 G_RESOURCE_LOOKUP_FLAGS_NONE,
356 &size, &flags, &error);
357 g_assert (found);
358 g_assert_no_error (error);
359 g_assert_cmpint (size, ==, 6);
360 g_assert_cmpint (flags, ==, 0);
362 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
363 G_RESOURCE_LOOKUP_FLAGS_NONE,
364 &size, &flags, &error);
365 g_assert (found);
366 g_assert_no_error (error);
367 g_assert_cmpint (size, ==, 6);
368 g_assert_cmpuint (flags, ==, 0);
370 data = g_resources_lookup_data ("/test1.txt",
371 G_RESOURCE_LOOKUP_FLAGS_NONE,
372 &error);
373 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
374 g_assert_no_error (error);
375 g_bytes_unref (data);
377 in = g_resources_open_stream ("/test1.txt",
378 G_RESOURCE_LOOKUP_FLAGS_NONE,
379 &error);
380 g_assert (in != NULL);
381 g_assert_no_error (error);
383 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
384 &size,
385 NULL, &error);
386 g_assert (success);
387 g_assert_no_error (error);
388 g_assert_cmpint (size, ==, 6);
389 buffer[size] = 0;
390 g_assert_cmpstr (buffer, ==, "test1\n");
392 g_input_stream_close (in, NULL, &error);
393 g_assert_no_error (error);
394 g_clear_object (&in);
396 data = g_resources_lookup_data ("/a_prefix/test2.txt",
397 G_RESOURCE_LOOKUP_FLAGS_NONE,
398 &error);
399 g_assert (data != NULL);
400 g_assert_no_error (error);
401 size = g_bytes_get_size (data);
402 g_assert_cmpint (size, ==, 6);
403 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
404 g_bytes_unref (data);
406 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
407 G_RESOURCE_LOOKUP_FLAGS_NONE,
408 &error);
409 g_assert (data != NULL);
410 g_assert_no_error (error);
411 size = g_bytes_get_size (data);
412 g_assert_cmpint (size, ==, 6);
413 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
414 g_bytes_unref (data);
416 children = g_resources_enumerate_children ("/not/here",
417 G_RESOURCE_LOOKUP_FLAGS_NONE,
418 &error);
419 g_assert (children == NULL);
420 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
421 g_clear_error (&error);
423 children = g_resources_enumerate_children ("/a_prefix",
424 G_RESOURCE_LOOKUP_FLAGS_NONE,
425 &error);
426 g_assert (children != NULL);
427 g_assert_no_error (error);
428 g_assert_cmpint (g_strv_length (children), ==, 2);
429 g_strfreev (children);
431 g_resources_unregister (resource);
432 g_resource_unref (resource);
434 found = g_resources_get_info ("/test1.txt",
435 G_RESOURCE_LOOKUP_FLAGS_NONE,
436 &size, &flags, &error);
437 g_assert (!found);
438 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
439 g_clear_error (&error);
442 static void
443 test_resource_automatic (void)
445 GError *error = NULL;
446 gboolean found;
447 gsize size;
448 guint32 flags;
449 GBytes *data;
451 found = g_resources_get_info ("/auto_loaded/test1.txt",
452 G_RESOURCE_LOOKUP_FLAGS_NONE,
453 &size, &flags, &error);
454 g_assert (found);
455 g_assert_no_error (error);
456 g_assert_cmpint (size, ==, 6);
457 g_assert_cmpint (flags, ==, 0);
459 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
460 G_RESOURCE_LOOKUP_FLAGS_NONE,
461 &error);
462 g_assert (data != NULL);
463 g_assert_no_error (error);
464 size = g_bytes_get_size (data);
465 g_assert_cmpint (size, ==, 6);
466 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
467 g_bytes_unref (data);
470 static void
471 test_resource_manual (void)
473 GError *error = NULL;
474 gboolean found;
475 gsize size;
476 guint32 flags;
477 GBytes *data;
479 found = g_resources_get_info ("/manual_loaded/test1.txt",
480 G_RESOURCE_LOOKUP_FLAGS_NONE,
481 &size, &flags, &error);
482 g_assert (found);
483 g_assert_no_error (error);
484 g_assert_cmpint (size, ==, 6);
485 g_assert_cmpuint (flags, ==, 0);
487 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
488 G_RESOURCE_LOOKUP_FLAGS_NONE,
489 &error);
490 g_assert (data != NULL);
491 g_assert_no_error (error);
492 size = g_bytes_get_size (data);
493 g_assert_cmpint (size, ==, 6);
494 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
495 g_bytes_unref (data);
498 static void
499 test_resource_manual2 (void)
501 GResource *resource;
502 GBytes *data;
503 gsize size;
504 GError *error = NULL;
506 resource = _g_test2_get_resource ();
508 data = g_resource_lookup_data (resource,
509 "/manual_loaded/test1.txt",
510 G_RESOURCE_LOOKUP_FLAGS_NONE,
511 &error);
512 g_assert (data != NULL);
513 g_assert_no_error (error);
514 size = g_bytes_get_size (data);
515 g_assert_cmpint (size, ==, 6);
516 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
517 g_bytes_unref (data);
519 g_resource_unref (resource);
522 static void
523 test_resource_module (void)
525 GIOModule *module;
526 gboolean found;
527 gsize size;
528 guint32 flags;
529 GBytes *data;
530 GError *error;
532 if (g_module_supported ())
534 /* For in-tree, this will find the .la file and use it to get to the .so in .libs/ */
535 module = g_io_module_new (g_test_get_filename (G_TEST_BUILT, "libresourceplugin", NULL));
537 error = NULL;
539 found = g_resources_get_info ("/resourceplugin/test1.txt",
540 G_RESOURCE_LOOKUP_FLAGS_NONE,
541 &size, &flags, &error);
542 g_assert (!found);
543 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
544 g_clear_error (&error);
546 g_type_module_use (G_TYPE_MODULE (module));
548 found = g_resources_get_info ("/resourceplugin/test1.txt",
549 G_RESOURCE_LOOKUP_FLAGS_NONE,
550 &size, &flags, &error);
551 g_assert (found);
552 g_assert_no_error (error);
553 g_assert_cmpint (size, ==, 6);
554 g_assert_cmpuint (flags, ==, 0);
556 data = g_resources_lookup_data ("/resourceplugin/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);
566 g_type_module_unuse (G_TYPE_MODULE (module));
568 found = g_resources_get_info ("/resourceplugin/test1.txt",
569 G_RESOURCE_LOOKUP_FLAGS_NONE,
570 &size, &flags, &error);
571 g_assert (!found);
572 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
573 g_clear_error (&error);
577 static void
578 test_uri_query_info (void)
580 GResource *resource;
581 GError *error = NULL;
582 gboolean loaded_file;
583 char *content;
584 gsize content_size;
585 GBytes *data;
586 GFile *file;
587 GFileInfo *info;
588 const char *content_type;
589 gchar *mime_type = NULL;
590 const char *fs_type;
591 gboolean readonly;
593 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
594 &content, &content_size, NULL);
595 g_assert (loaded_file);
597 data = g_bytes_new_take (content, content_size);
598 resource = g_resource_new_from_data (data, &error);
599 g_bytes_unref (data);
600 g_assert (resource != NULL);
601 g_assert_no_error (error);
603 g_resources_register (resource);
605 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
607 info = g_file_query_info (file, "*", 0, NULL, &error);
608 g_assert_no_error (error);
610 content_type = g_file_info_get_content_type (info);
611 g_assert (content_type);
612 mime_type = g_content_type_get_mime_type (content_type);
613 g_assert (mime_type);
614 g_assert_cmpstr (mime_type, ==, "text/plain");
615 g_free (mime_type);
617 g_object_unref (info);
619 info = g_file_query_filesystem_info (file, "*", NULL, &error);
620 g_assert_no_error (error);
622 fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
623 g_assert_cmpstr (fs_type, ==, "resource");
624 readonly = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
625 g_assert_true (readonly);
627 g_object_unref (info);
629 g_assert_cmpuint (g_file_hash (file), !=, 0);
631 g_object_unref (file);
633 g_resources_unregister (resource);
634 g_resource_unref (resource);
637 static void
638 test_uri_file (void)
640 GResource *resource;
641 GError *error = NULL;
642 gboolean loaded_file;
643 char *content;
644 gsize content_size;
645 GBytes *data;
646 GFile *file;
647 GFileInfo *info;
648 gchar *name;
649 GFile *file2, *parent;
650 GFileEnumerator *enumerator;
651 gchar *scheme;
652 GFileAttributeInfoList *attrs;
653 GInputStream *stream;
654 gchar buf[1024];
655 gboolean ret;
656 gssize skipped;
658 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
659 &content, &content_size, NULL);
660 g_assert (loaded_file);
662 data = g_bytes_new_take (content, content_size);
663 resource = g_resource_new_from_data (data, &error);
664 g_bytes_unref (data);
665 g_assert (resource != NULL);
666 g_assert_no_error (error);
668 g_resources_register (resource);
670 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
672 g_assert (g_file_get_path (file) == NULL);
674 name = g_file_get_parse_name (file);
675 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
676 g_free (name);
678 name = g_file_get_uri (file);
679 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
680 g_free (name);
682 g_assert (!g_file_is_native (file));
683 g_assert (!g_file_has_uri_scheme (file, "http"));
684 g_assert (g_file_has_uri_scheme (file, "resource"));
685 scheme = g_file_get_uri_scheme (file);
686 g_assert_cmpstr (scheme, ==, "resource");
687 g_free (scheme);
689 file2 = g_file_dup (file);
690 g_assert (g_file_equal (file, file2));
691 g_object_unref (file2);
693 parent = g_file_get_parent (file);
694 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
695 g_assert_no_error (error);
697 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
698 g_assert_no_error (error);
699 g_assert (g_file_equal (file, file2));
700 g_object_unref (file2);
702 info = g_file_enumerator_next_file (enumerator, NULL, &error);
703 g_assert_no_error (error);
704 g_assert (info != NULL);
705 g_object_unref (info);
707 info = g_file_enumerator_next_file (enumerator, NULL, &error);
708 g_assert_no_error (error);
709 g_assert (info != NULL);
710 g_object_unref (info);
712 info = g_file_enumerator_next_file (enumerator, NULL, &error);
713 g_assert_no_error (error);
714 g_assert (info == NULL);
716 g_file_enumerator_close (enumerator, NULL, &error);
717 g_assert_no_error (error);
718 g_object_unref (enumerator);
720 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
721 g_assert (g_file_equal (file, file2));
723 g_assert (g_file_has_prefix (file, parent));
725 name = g_file_get_relative_path (parent, file);
726 g_assert_cmpstr (name, ==, "test2-alias.txt");
727 g_free (name);
729 g_object_unref (parent);
731 attrs = g_file_query_settable_attributes (file, NULL, &error);
732 g_assert_no_error (error);
733 g_file_attribute_info_list_unref (attrs);
735 attrs = g_file_query_writable_namespaces (file, NULL, &error);
736 g_assert_no_error (error);
737 g_file_attribute_info_list_unref (attrs);
739 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
740 g_assert_no_error (error);
741 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
742 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
743 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
744 g_assert (ret);
745 g_assert_no_error (error);
746 skipped = g_input_stream_skip (stream, 1, NULL, &error);
747 g_assert_cmpint (skipped, ==, 1);
748 g_assert_no_error (error);
750 memset (buf, 0, 1024);
751 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
752 g_assert (ret);
753 g_assert_no_error (error);
754 g_assert_cmpstr (buf, ==, "st2\n");
755 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
756 G_FILE_ATTRIBUTE_STANDARD_SIZE,
757 NULL,
758 &error);
759 g_assert_no_error (error);
760 g_assert (info != NULL);
761 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
762 g_object_unref (info);
764 ret = g_input_stream_close (stream, NULL, &error);
765 g_assert (ret);
766 g_assert_no_error (error);
767 g_object_unref (stream);
769 g_object_unref (file);
770 g_object_unref (file2);
772 g_resources_unregister (resource);
773 g_resource_unref (resource);
777 main (int argc,
778 char *argv[])
780 g_test_init (&argc, &argv, NULL);
782 _g_test2_register_resource ();
784 g_test_add_func ("/resource/file", test_resource_file);
785 g_test_add_func ("/resource/file-path", test_resource_file_path);
786 g_test_add_func ("/resource/data", test_resource_data);
787 g_test_add_func ("/resource/data_unaligned", test_resource_data_unaligned);
788 g_test_add_func ("/resource/registered", test_resource_registered);
789 g_test_add_func ("/resource/manual", test_resource_manual);
790 g_test_add_func ("/resource/manual2", test_resource_manual2);
791 #ifdef G_HAS_CONSTRUCTORS
792 g_test_add_func ("/resource/automatic", test_resource_automatic);
793 /* This only uses automatic resources too, so it tests the constructors and destructors */
794 g_test_add_func ("/resource/module", test_resource_module);
795 #endif
796 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
797 g_test_add_func ("/resource/uri/file", test_uri_file);
799 return g_test_run();