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, see <http://www.gnu.org/licenses/>.
21 #include "gconstructor.h"
22 #include "test_resources2.h"
25 test_resource (GResource *resource)
28 gboolean found, success;
36 found = g_resource_get_info (resource,
38 G_RESOURCE_LOOKUP_FLAGS_NONE,
39 &size, &flags, &error);
41 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
42 g_clear_error (&error);
44 found = g_resource_get_info (resource,
46 G_RESOURCE_LOOKUP_FLAGS_NONE,
47 &size, &flags, &error);
49 g_assert_no_error (error);
50 g_assert_cmpint (size, ==, 6);
51 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
53 found = g_resource_get_info (resource,
54 "/a_prefix/test2.txt",
55 G_RESOURCE_LOOKUP_FLAGS_NONE,
56 &size, &flags, &error);
58 g_assert_no_error (error);
59 g_assert_cmpint (size, ==, 6);
60 g_assert_cmpuint (flags, ==, 0);
62 found = g_resource_get_info (resource,
63 "/a_prefix/test2-alias.txt",
64 G_RESOURCE_LOOKUP_FLAGS_NONE,
65 &size, &flags, &error);
67 g_assert_no_error (error);
68 g_assert_cmpint (size, ==, 6);
69 g_assert_cmpuint (flags, ==, 0);
71 data = g_resource_lookup_data (resource,
73 G_RESOURCE_LOOKUP_FLAGS_NONE,
75 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
76 g_assert_no_error (error);
79 in = g_resource_open_stream (resource,
81 G_RESOURCE_LOOKUP_FLAGS_NONE,
83 g_assert (in != NULL);
84 g_assert_no_error (error);
86 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
90 g_assert_no_error (error);
91 g_assert_cmpint (size, ==, 6);
93 g_assert_cmpstr (buffer, ==, "test1\n");
95 g_input_stream_close (in, NULL, &error);
96 g_assert_no_error (error);
99 data = g_resource_lookup_data (resource,
100 "/a_prefix/test2.txt",
101 G_RESOURCE_LOOKUP_FLAGS_NONE,
103 g_assert (data != NULL);
104 g_assert_no_error (error);
105 size = g_bytes_get_size (data);
106 g_assert_cmpint (size, ==, 6);
107 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
108 g_bytes_unref (data);
110 data = g_resource_lookup_data (resource,
111 "/a_prefix/test2-alias.txt",
112 G_RESOURCE_LOOKUP_FLAGS_NONE,
114 g_assert (data != NULL);
115 g_assert_no_error (error);
116 size = g_bytes_get_size (data);
117 g_assert_cmpint (size, ==, 6);
118 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
119 g_bytes_unref (data);
121 children = g_resource_enumerate_children (resource,
123 G_RESOURCE_LOOKUP_FLAGS_NONE,
125 g_assert (children == NULL);
126 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
127 g_clear_error (&error);
129 children = g_resource_enumerate_children (resource,
131 G_RESOURCE_LOOKUP_FLAGS_NONE,
133 g_assert (children != NULL);
134 g_assert_no_error (error);
135 g_assert_cmpint (g_strv_length (children), ==, 2);
136 g_strfreev (children);
140 test_resource_file (void)
143 GError *error = NULL;
145 resource = g_resource_load ("not-there", &error);
146 g_assert (resource == NULL);
147 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
148 g_clear_error (&error);
150 resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
151 g_assert (resource != NULL);
152 g_assert_no_error (error);
154 test_resource (resource);
155 g_resource_unref (resource);
159 test_resource_data (void)
162 GError *error = NULL;
163 gboolean loaded_file;
168 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
169 &content, &content_size, NULL);
170 g_assert (loaded_file);
172 data = g_bytes_new_take (content, content_size);
173 resource = g_resource_new_from_data (data, &error);
174 g_bytes_unref (data);
175 g_assert (resource != NULL);
176 g_assert_no_error (error);
178 test_resource (resource);
180 g_resource_unref (resource);
184 test_resource_registered (void)
187 GError *error = NULL;
188 gboolean found, success;
196 resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
197 g_assert (resource != NULL);
198 g_assert_no_error (error);
200 found = g_resources_get_info ("/test1.txt",
201 G_RESOURCE_LOOKUP_FLAGS_NONE,
202 &size, &flags, &error);
204 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
205 g_clear_error (&error);
207 g_resources_register (resource);
209 found = g_resources_get_info ("/test1.txt",
210 G_RESOURCE_LOOKUP_FLAGS_NONE,
211 &size, &flags, &error);
213 g_assert_no_error (error);
214 g_assert_cmpint (size, ==, 6);
215 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
217 found = g_resources_get_info ("/a_prefix/test2.txt",
218 G_RESOURCE_LOOKUP_FLAGS_NONE,
219 &size, &flags, &error);
221 g_assert_no_error (error);
222 g_assert_cmpint (size, ==, 6);
223 g_assert_cmpint (flags, ==, 0);
225 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
226 G_RESOURCE_LOOKUP_FLAGS_NONE,
227 &size, &flags, &error);
229 g_assert_no_error (error);
230 g_assert_cmpint (size, ==, 6);
231 g_assert_cmpuint (flags, ==, 0);
233 data = g_resources_lookup_data ("/test1.txt",
234 G_RESOURCE_LOOKUP_FLAGS_NONE,
236 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
237 g_assert_no_error (error);
238 g_bytes_unref (data);
240 in = g_resources_open_stream ("/test1.txt",
241 G_RESOURCE_LOOKUP_FLAGS_NONE,
243 g_assert (in != NULL);
244 g_assert_no_error (error);
246 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
250 g_assert_no_error (error);
251 g_assert_cmpint (size, ==, 6);
253 g_assert_cmpstr (buffer, ==, "test1\n");
255 g_input_stream_close (in, NULL, &error);
256 g_assert_no_error (error);
257 g_clear_object (&in);
259 data = g_resources_lookup_data ("/a_prefix/test2.txt",
260 G_RESOURCE_LOOKUP_FLAGS_NONE,
262 g_assert (data != NULL);
263 g_assert_no_error (error);
264 size = g_bytes_get_size (data);
265 g_assert_cmpint (size, ==, 6);
266 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
267 g_bytes_unref (data);
269 data = g_resources_lookup_data ("/a_prefix/test2-alias.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 children = g_resources_enumerate_children ("/not/here",
280 G_RESOURCE_LOOKUP_FLAGS_NONE,
282 g_assert (children == NULL);
283 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
284 g_clear_error (&error);
286 children = g_resources_enumerate_children ("/a_prefix",
287 G_RESOURCE_LOOKUP_FLAGS_NONE,
289 g_assert (children != NULL);
290 g_assert_no_error (error);
291 g_assert_cmpint (g_strv_length (children), ==, 2);
292 g_strfreev (children);
294 g_resources_unregister (resource);
295 g_resource_unref (resource);
297 found = g_resources_get_info ("/test1.txt",
298 G_RESOURCE_LOOKUP_FLAGS_NONE,
299 &size, &flags, &error);
301 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
302 g_clear_error (&error);
306 test_resource_automatic (void)
308 GError *error = NULL;
314 found = g_resources_get_info ("/auto_loaded/test1.txt",
315 G_RESOURCE_LOOKUP_FLAGS_NONE,
316 &size, &flags, &error);
318 g_assert_no_error (error);
319 g_assert_cmpint (size, ==, 6);
320 g_assert_cmpint (flags, ==, 0);
322 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
323 G_RESOURCE_LOOKUP_FLAGS_NONE,
325 g_assert (data != NULL);
326 g_assert_no_error (error);
327 size = g_bytes_get_size (data);
328 g_assert_cmpint (size, ==, 6);
329 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
330 g_bytes_unref (data);
334 test_resource_manual (void)
336 GError *error = NULL;
342 found = g_resources_get_info ("/manual_loaded/test1.txt",
343 G_RESOURCE_LOOKUP_FLAGS_NONE,
344 &size, &flags, &error);
346 g_assert_no_error (error);
347 g_assert_cmpint (size, ==, 6);
348 g_assert_cmpuint (flags, ==, 0);
350 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
351 G_RESOURCE_LOOKUP_FLAGS_NONE,
353 g_assert (data != NULL);
354 g_assert_no_error (error);
355 size = g_bytes_get_size (data);
356 g_assert_cmpint (size, ==, 6);
357 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
358 g_bytes_unref (data);
362 test_resource_manual2 (void)
367 GError *error = NULL;
369 resource = _g_test2_get_resource ();
371 data = g_resource_lookup_data (resource,
372 "/manual_loaded/test1.txt",
373 G_RESOURCE_LOOKUP_FLAGS_NONE,
375 g_assert (data != NULL);
376 g_assert_no_error (error);
377 size = g_bytes_get_size (data);
378 g_assert_cmpint (size, ==, 6);
379 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
380 g_bytes_unref (data);
382 g_resource_unref (resource);
386 test_resource_module (void)
395 if (g_module_supported ())
397 /* For in-tree, this will find the .la file and use it to get to the .so in .libs/ */
398 module = g_io_module_new (g_test_get_filename (G_TEST_BUILT, "libresourceplugin", NULL));
402 found = g_resources_get_info ("/resourceplugin/test1.txt",
403 G_RESOURCE_LOOKUP_FLAGS_NONE,
404 &size, &flags, &error);
406 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
407 g_clear_error (&error);
409 g_type_module_use (G_TYPE_MODULE (module));
411 found = g_resources_get_info ("/resourceplugin/test1.txt",
412 G_RESOURCE_LOOKUP_FLAGS_NONE,
413 &size, &flags, &error);
415 g_assert_no_error (error);
416 g_assert_cmpint (size, ==, 6);
417 g_assert_cmpuint (flags, ==, 0);
419 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
420 G_RESOURCE_LOOKUP_FLAGS_NONE,
422 g_assert (data != NULL);
423 g_assert_no_error (error);
424 size = g_bytes_get_size (data);
425 g_assert_cmpint (size, ==, 6);
426 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
427 g_bytes_unref (data);
429 g_type_module_unuse (G_TYPE_MODULE (module));
431 found = g_resources_get_info ("/resourceplugin/test1.txt",
432 G_RESOURCE_LOOKUP_FLAGS_NONE,
433 &size, &flags, &error);
435 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
436 g_clear_error (&error);
441 test_uri_query_info (void)
444 GError *error = NULL;
445 gboolean loaded_file;
451 const char *content_type, *mime_type;
453 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
454 &content, &content_size, NULL);
455 g_assert (loaded_file);
457 data = g_bytes_new_take (content, content_size);
458 resource = g_resource_new_from_data (data, &error);
459 g_bytes_unref (data);
460 g_assert (resource != NULL);
461 g_assert_no_error (error);
463 g_resources_register (resource);
465 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
467 info = g_file_query_info (file, "*", 0, NULL, &error);
468 g_assert_no_error (error);
470 content_type = g_file_info_get_content_type (info);
471 g_assert (content_type);
472 mime_type = g_content_type_get_mime_type (content_type);
473 g_assert (mime_type);
474 g_assert_cmpstr (mime_type, ==, "text/plain");
476 g_object_unref (info);
478 g_assert_cmpuint (g_file_hash (file), !=, 0);
480 g_object_unref (file);
482 g_resources_unregister (resource);
483 g_resource_unref (resource);
490 GError *error = NULL;
491 gboolean loaded_file;
498 GFile *file2, *parent;
499 GFileEnumerator *enumerator;
501 GFileAttributeInfoList *attrs;
502 GInputStream *stream;
507 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
508 &content, &content_size, NULL);
509 g_assert (loaded_file);
511 data = g_bytes_new_take (content, content_size);
512 resource = g_resource_new_from_data (data, &error);
513 g_bytes_unref (data);
514 g_assert (resource != NULL);
515 g_assert_no_error (error);
517 g_resources_register (resource);
519 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
521 g_assert (g_file_get_path (file) == NULL);
523 name = g_file_get_parse_name (file);
524 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
527 name = g_file_get_uri (file);
528 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
531 g_assert (!g_file_is_native (file));
532 g_assert (!g_file_has_uri_scheme (file, "http"));
533 g_assert (g_file_has_uri_scheme (file, "resource"));
534 scheme = g_file_get_uri_scheme (file);
535 g_assert_cmpstr (scheme, ==, "resource");
538 file2 = g_file_dup (file);
539 g_assert (g_file_equal (file, file2));
540 g_object_unref (file2);
542 parent = g_file_get_parent (file);
543 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
544 g_assert_no_error (error);
546 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
547 g_assert_no_error (error);
548 g_assert (g_file_equal (file, file2));
549 g_object_unref (file2);
551 info = g_file_enumerator_next_file (enumerator, NULL, &error);
552 g_assert_no_error (error);
553 g_assert (info != NULL);
554 g_object_unref (info);
556 info = g_file_enumerator_next_file (enumerator, NULL, &error);
557 g_assert_no_error (error);
558 g_assert (info != NULL);
559 g_object_unref (info);
561 info = g_file_enumerator_next_file (enumerator, NULL, &error);
562 g_assert_no_error (error);
563 g_assert (info == NULL);
565 g_file_enumerator_close (enumerator, NULL, &error);
566 g_assert_no_error (error);
567 g_object_unref (enumerator);
569 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
570 g_assert (g_file_equal (file, file2));
572 g_assert (g_file_has_prefix (file, parent));
574 name = g_file_get_relative_path (parent, file);
575 g_assert_cmpstr (name, ==, "test2-alias.txt");
578 g_object_unref (parent);
580 attrs = g_file_query_settable_attributes (file, NULL, &error);
581 g_assert_no_error (error);
582 g_file_attribute_info_list_unref (attrs);
584 attrs = g_file_query_writable_namespaces (file, NULL, &error);
585 g_assert_no_error (error);
586 g_file_attribute_info_list_unref (attrs);
588 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
589 g_assert_no_error (error);
590 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
591 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
592 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
594 g_assert_no_error (error);
595 skipped = g_input_stream_skip (stream, 1, NULL, &error);
596 g_assert_cmpint (skipped, ==, 1);
597 g_assert_no_error (error);
599 memset (buf, 0, 1024);
600 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
602 g_assert_no_error (error);
603 g_assert_cmpstr (buf, ==, "st2\n");
604 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
605 G_FILE_ATTRIBUTE_STANDARD_SIZE,
608 g_assert_no_error (error);
609 g_assert (info != NULL);
610 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
611 g_object_unref (info);
613 ret = g_input_stream_close (stream, NULL, &error);
615 g_assert_no_error (error);
616 g_object_unref (stream);
618 g_object_unref (file);
619 g_object_unref (file2);
621 g_resources_unregister (resource);
622 g_resource_unref (resource);
629 g_test_init (&argc, &argv, NULL);
631 _g_test2_register_resource ();
633 g_test_add_func ("/resource/file", test_resource_file);
634 g_test_add_func ("/resource/data", test_resource_data);
635 g_test_add_func ("/resource/registered", test_resource_registered);
636 g_test_add_func ("/resource/manual", test_resource_manual);
637 g_test_add_func ("/resource/manual2", test_resource_manual2);
638 #ifdef G_HAS_CONSTRUCTORS
639 g_test_add_func ("/resource/automatic", test_resource_automatic);
640 /* This only uses automatic resources too, so it tests the constructors and destructors */
641 g_test_add_func ("/resource/module", test_resource_module);
643 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
644 g_test_add_func ("/resource/uri/file", test_uri_file);