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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gconstructor.h"
24 #include "test_resources2.h"
27 test_resource (GResource *resource)
30 gboolean found, success;
38 found = g_resource_get_info (resource,
40 G_RESOURCE_LOOKUP_FLAGS_NONE,
41 &size, &flags, &error);
43 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
44 g_clear_error (&error);
46 found = g_resource_get_info (resource,
48 G_RESOURCE_LOOKUP_FLAGS_NONE,
49 &size, &flags, &error);
51 g_assert_no_error (error);
52 g_assert_cmpint (size, ==, 6);
53 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
55 found = g_resource_get_info (resource,
56 "/a_prefix/test2.txt",
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, ==, 0);
64 found = g_resource_get_info (resource,
65 "/a_prefix/test2-alias.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 data = g_resource_lookup_data (resource,
75 G_RESOURCE_LOOKUP_FLAGS_NONE,
77 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
78 g_assert_no_error (error);
81 in = g_resource_open_stream (resource,
83 G_RESOURCE_LOOKUP_FLAGS_NONE,
85 g_assert (in != NULL);
86 g_assert_no_error (error);
88 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
92 g_assert_no_error (error);
93 g_assert_cmpint (size, ==, 6);
95 g_assert_cmpstr (buffer, ==, "test1\n");
97 g_input_stream_close (in, NULL, &error);
98 g_assert_no_error (error);
101 data = g_resource_lookup_data (resource,
102 "/a_prefix/test2.txt",
103 G_RESOURCE_LOOKUP_FLAGS_NONE,
105 g_assert (data != NULL);
106 g_assert_no_error (error);
107 size = g_bytes_get_size (data);
108 g_assert_cmpint (size, ==, 6);
109 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
110 g_bytes_unref (data);
112 data = g_resource_lookup_data (resource,
113 "/a_prefix/test2-alias.txt",
114 G_RESOURCE_LOOKUP_FLAGS_NONE,
116 g_assert (data != NULL);
117 g_assert_no_error (error);
118 size = g_bytes_get_size (data);
119 g_assert_cmpint (size, ==, 6);
120 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
121 g_bytes_unref (data);
123 children = g_resource_enumerate_children (resource,
125 G_RESOURCE_LOOKUP_FLAGS_NONE,
127 g_assert (children == NULL);
128 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
129 g_clear_error (&error);
131 children = g_resource_enumerate_children (resource,
133 G_RESOURCE_LOOKUP_FLAGS_NONE,
135 g_assert (children != NULL);
136 g_assert_no_error (error);
137 g_assert_cmpint (g_strv_length (children), ==, 2);
138 g_strfreev (children);
142 test_resource_file (void)
145 GError *error = NULL;
148 resource = g_resource_load ("not-there", &error);
149 g_assert (resource == NULL);
150 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
151 g_clear_error (&error);
153 path = g_test_build_filename (G_TEST_BUILT, "test.gresource", NULL);
154 resource = g_resource_load (path, &error);
155 g_assert (resource != NULL);
156 g_assert_no_error (error);
159 test_resource (resource);
160 g_resource_unref (resource);
164 test_resource_data (void)
167 GError *error = NULL;
168 gboolean loaded_file;
174 path = g_test_build_filename (G_TEST_BUILT, "test.gresource", NULL);
175 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
176 g_assert (loaded_file);
179 data = g_bytes_new_take (content, content_size);
180 resource = g_resource_new_from_data (data, &error);
181 g_bytes_unref (data);
182 g_assert (resource != NULL);
183 g_assert_no_error (error);
185 test_resource (resource);
187 g_resource_unref (resource);
191 test_resource_registered (void)
194 GError *error = NULL;
195 gboolean found, success;
204 path = g_test_build_filename (G_TEST_BUILT, "test.gresource", NULL);
205 resource = g_resource_load (path, &error);
206 g_assert (resource != NULL);
207 g_assert_no_error (error);
210 found = g_resources_get_info ("/test1.txt",
211 G_RESOURCE_LOOKUP_FLAGS_NONE,
212 &size, &flags, &error);
214 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
215 g_clear_error (&error);
217 g_resources_register (resource);
219 found = g_resources_get_info ("/test1.txt",
220 G_RESOURCE_LOOKUP_FLAGS_NONE,
221 &size, &flags, &error);
223 g_assert_no_error (error);
224 g_assert_cmpint (size, ==, 6);
225 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
227 found = g_resources_get_info ("/a_prefix/test2.txt",
228 G_RESOURCE_LOOKUP_FLAGS_NONE,
229 &size, &flags, &error);
231 g_assert_no_error (error);
232 g_assert_cmpint (size, ==, 6);
233 g_assert_cmpint (flags, ==, 0);
235 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
236 G_RESOURCE_LOOKUP_FLAGS_NONE,
237 &size, &flags, &error);
239 g_assert_no_error (error);
240 g_assert_cmpint (size, ==, 6);
241 g_assert_cmpuint (flags, ==, 0);
243 data = g_resources_lookup_data ("/test1.txt",
244 G_RESOURCE_LOOKUP_FLAGS_NONE,
246 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
247 g_assert_no_error (error);
248 g_bytes_unref (data);
250 in = g_resources_open_stream ("/test1.txt",
251 G_RESOURCE_LOOKUP_FLAGS_NONE,
253 g_assert (in != NULL);
254 g_assert_no_error (error);
256 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
260 g_assert_no_error (error);
261 g_assert_cmpint (size, ==, 6);
263 g_assert_cmpstr (buffer, ==, "test1\n");
265 g_input_stream_close (in, NULL, &error);
266 g_assert_no_error (error);
267 g_clear_object (&in);
269 data = g_resources_lookup_data ("/a_prefix/test2.txt",
270 G_RESOURCE_LOOKUP_FLAGS_NONE,
272 g_assert (data != NULL);
273 g_assert_no_error (error);
274 size = g_bytes_get_size (data);
275 g_assert_cmpint (size, ==, 6);
276 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
277 g_bytes_unref (data);
279 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
280 G_RESOURCE_LOOKUP_FLAGS_NONE,
282 g_assert (data != NULL);
283 g_assert_no_error (error);
284 size = g_bytes_get_size (data);
285 g_assert_cmpint (size, ==, 6);
286 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
287 g_bytes_unref (data);
289 children = g_resources_enumerate_children ("/not/here",
290 G_RESOURCE_LOOKUP_FLAGS_NONE,
292 g_assert (children == NULL);
293 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
294 g_clear_error (&error);
296 children = g_resources_enumerate_children ("/a_prefix",
297 G_RESOURCE_LOOKUP_FLAGS_NONE,
299 g_assert (children != NULL);
300 g_assert_no_error (error);
301 g_assert_cmpint (g_strv_length (children), ==, 2);
302 g_strfreev (children);
304 g_resources_unregister (resource);
305 g_resource_unref (resource);
307 found = g_resources_get_info ("/test1.txt",
308 G_RESOURCE_LOOKUP_FLAGS_NONE,
309 &size, &flags, &error);
311 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
312 g_clear_error (&error);
316 test_resource_automatic (void)
318 GError *error = NULL;
324 found = g_resources_get_info ("/auto_loaded/test1.txt",
325 G_RESOURCE_LOOKUP_FLAGS_NONE,
326 &size, &flags, &error);
328 g_assert_no_error (error);
329 g_assert_cmpint (size, ==, 6);
330 g_assert_cmpint (flags, ==, 0);
332 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
333 G_RESOURCE_LOOKUP_FLAGS_NONE,
335 g_assert (data != NULL);
336 g_assert_no_error (error);
337 size = g_bytes_get_size (data);
338 g_assert_cmpint (size, ==, 6);
339 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
340 g_bytes_unref (data);
344 test_resource_manual (void)
346 GError *error = NULL;
352 found = g_resources_get_info ("/manual_loaded/test1.txt",
353 G_RESOURCE_LOOKUP_FLAGS_NONE,
354 &size, &flags, &error);
356 g_assert_no_error (error);
357 g_assert_cmpint (size, ==, 6);
358 g_assert_cmpuint (flags, ==, 0);
360 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
361 G_RESOURCE_LOOKUP_FLAGS_NONE,
363 g_assert (data != NULL);
364 g_assert_no_error (error);
365 size = g_bytes_get_size (data);
366 g_assert_cmpint (size, ==, 6);
367 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
368 g_bytes_unref (data);
372 test_resource_manual2 (void)
377 GError *error = NULL;
379 resource = _g_test2_get_resource ();
381 data = g_resource_lookup_data (resource,
382 "/manual_loaded/test1.txt",
383 G_RESOURCE_LOOKUP_FLAGS_NONE,
385 g_assert (data != NULL);
386 g_assert_no_error (error);
387 size = g_bytes_get_size (data);
388 g_assert_cmpint (size, ==, 6);
389 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
390 g_bytes_unref (data);
392 g_resource_unref (resource);
396 test_resource_module (void)
405 if (g_module_supported ())
409 path = g_test_build_filename (G_TEST_BUILT, "libresourceplugin", NULL);
410 module = g_io_module_new (path);
415 found = g_resources_get_info ("/resourceplugin/test1.txt",
416 G_RESOURCE_LOOKUP_FLAGS_NONE,
417 &size, &flags, &error);
419 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
420 g_clear_error (&error);
422 g_type_module_use (G_TYPE_MODULE (module));
424 found = g_resources_get_info ("/resourceplugin/test1.txt",
425 G_RESOURCE_LOOKUP_FLAGS_NONE,
426 &size, &flags, &error);
428 g_assert_no_error (error);
429 g_assert_cmpint (size, ==, 6);
430 g_assert_cmpuint (flags, ==, 0);
432 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
433 G_RESOURCE_LOOKUP_FLAGS_NONE,
435 g_assert (data != NULL);
436 g_assert_no_error (error);
437 size = g_bytes_get_size (data);
438 g_assert_cmpint (size, ==, 6);
439 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
440 g_bytes_unref (data);
442 g_type_module_unuse (G_TYPE_MODULE (module));
444 found = g_resources_get_info ("/resourceplugin/test1.txt",
445 G_RESOURCE_LOOKUP_FLAGS_NONE,
446 &size, &flags, &error);
448 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
449 g_clear_error (&error);
454 test_uri_query_info (void)
457 GError *error = NULL;
458 gboolean loaded_file;
465 const char *content_type;
467 path = g_test_build_filename (G_TEST_BUILT, "test.gresource", NULL);
468 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
469 g_assert (loaded_file);
472 data = g_bytes_new_take (content, content_size);
473 resource = g_resource_new_from_data (data, &error);
474 g_bytes_unref (data);
475 g_assert (resource != NULL);
476 g_assert_no_error (error);
478 g_resources_register (resource);
480 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
482 info = g_file_query_info (file, "*", 0, NULL, &error);
483 g_assert_no_error (error);
485 content_type = g_file_info_get_content_type (info);
486 g_assert (content_type);
487 g_assert_cmpstr (content_type, ==, "text/plain");
489 g_object_unref (info);
491 g_object_unref (file);
493 g_resources_unregister (resource);
494 g_resource_unref (resource);
501 GError *error = NULL;
502 gboolean loaded_file;
509 GFile *file2, *parent;
510 GFileEnumerator *enumerator;
512 GFileAttributeInfoList *attrs;
513 GInputStream *stream;
519 path = g_test_build_filename (G_TEST_BUILT, "test.gresource", NULL);
520 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
521 g_assert (loaded_file);
524 data = g_bytes_new_take (content, content_size);
525 resource = g_resource_new_from_data (data, &error);
526 g_bytes_unref (data);
527 g_assert (resource != NULL);
528 g_assert_no_error (error);
530 g_resources_register (resource);
532 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
534 g_assert (g_file_get_path (file) == NULL);
536 name = g_file_get_parse_name (file);
537 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
540 name = g_file_get_uri (file);
541 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
544 g_assert (!g_file_is_native (file));
545 g_assert (!g_file_has_uri_scheme (file, "http"));
546 g_assert (g_file_has_uri_scheme (file, "resource"));
547 scheme = g_file_get_uri_scheme (file);
548 g_assert_cmpstr (scheme, ==, "resource");
551 file2 = g_file_dup (file);
552 g_assert (g_file_equal (file, file2));
553 g_object_unref (file2);
555 parent = g_file_get_parent (file);
556 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
557 g_assert_no_error (error);
559 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
560 g_assert_no_error (error);
561 g_assert (g_file_equal (file, file2));
562 g_object_unref (file2);
564 info = g_file_enumerator_next_file (enumerator, NULL, &error);
565 g_assert_no_error (error);
566 g_assert (info != NULL);
567 g_object_unref (info);
569 info = g_file_enumerator_next_file (enumerator, NULL, &error);
570 g_assert_no_error (error);
571 g_assert (info != NULL);
572 g_object_unref (info);
574 info = g_file_enumerator_next_file (enumerator, NULL, &error);
575 g_assert_no_error (error);
576 g_assert (info == NULL);
578 g_file_enumerator_close (enumerator, NULL, &error);
579 g_assert_no_error (error);
580 g_object_unref (enumerator);
582 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
583 g_assert (g_file_equal (file, file2));
585 g_assert (g_file_has_prefix (file, parent));
587 name = g_file_get_relative_path (parent, file);
588 g_assert_cmpstr (name, ==, "test2-alias.txt");
591 g_object_unref (parent);
593 attrs = g_file_query_settable_attributes (file, NULL, &error);
594 g_assert_no_error (error);
595 g_file_attribute_info_list_unref (attrs);
597 attrs = g_file_query_writable_namespaces (file, NULL, &error);
598 g_assert_no_error (error);
599 g_file_attribute_info_list_unref (attrs);
601 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
602 g_assert_no_error (error);
603 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
604 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
605 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
607 g_assert_no_error (error);
608 skipped = g_input_stream_skip (stream, 1, NULL, &error);
609 g_assert_cmpint (skipped, ==, 1);
610 g_assert_no_error (error);
612 memset (buf, 0, 1024);
613 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
615 g_assert_no_error (error);
616 g_assert_cmpstr (buf, ==, "st2\n");
617 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
618 G_FILE_ATTRIBUTE_STANDARD_SIZE,
621 g_assert_no_error (error);
622 g_assert (info != NULL);
623 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
624 g_object_unref (info);
626 ret = g_input_stream_close (stream, NULL, &error);
628 g_assert_no_error (error);
629 g_object_unref (stream);
631 g_object_unref (file);
632 g_object_unref (file2);
634 g_resources_unregister (resource);
635 g_resource_unref (resource);
642 g_test_init (&argc, &argv, NULL);
644 _g_test2_register_resource ();
646 g_test_add_func ("/resource/file", test_resource_file);
647 g_test_add_func ("/resource/data", test_resource_data);
648 g_test_add_func ("/resource/registered", test_resource_registered);
649 g_test_add_func ("/resource/manual", test_resource_manual);
650 g_test_add_func ("/resource/manual2", test_resource_manual2);
651 #ifdef G_HAS_CONSTRUCTORS
652 g_test_add_func ("/resource/automatic", test_resource_automatic);
653 /* This only uses automatic resources too, so it tests the constructors and destructors */
654 g_test_add_func ("/resource/module", test_resource_module);
656 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
657 g_test_add_func ("/resource/uri/file", test_uri_file);