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"
26 const gchar *datapath, *libpath;
29 test_resource (GResource *resource)
32 gboolean found, success;
40 found = g_resource_get_info (resource,
42 G_RESOURCE_LOOKUP_FLAGS_NONE,
43 &size, &flags, &error);
45 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
46 g_clear_error (&error);
48 found = g_resource_get_info (resource,
50 G_RESOURCE_LOOKUP_FLAGS_NONE,
51 &size, &flags, &error);
53 g_assert_no_error (error);
54 g_assert_cmpint (size, ==, 6);
55 g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
57 found = g_resource_get_info (resource,
58 "/a_prefix/test2.txt",
59 G_RESOURCE_LOOKUP_FLAGS_NONE,
60 &size, &flags, &error);
62 g_assert_no_error (error);
63 g_assert_cmpint (size, ==, 6);
64 g_assert_cmpuint (flags, ==, 0);
66 found = g_resource_get_info (resource,
67 "/a_prefix/test2-alias.txt",
68 G_RESOURCE_LOOKUP_FLAGS_NONE,
69 &size, &flags, &error);
71 g_assert_no_error (error);
72 g_assert_cmpint (size, ==, 6);
73 g_assert_cmpuint (flags, ==, 0);
75 data = g_resource_lookup_data (resource,
77 G_RESOURCE_LOOKUP_FLAGS_NONE,
79 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
80 g_assert_no_error (error);
83 in = g_resource_open_stream (resource,
85 G_RESOURCE_LOOKUP_FLAGS_NONE,
87 g_assert (in != NULL);
88 g_assert_no_error (error);
90 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
94 g_assert_no_error (error);
95 g_assert_cmpint (size, ==, 6);
97 g_assert_cmpstr (buffer, ==, "test1\n");
99 g_input_stream_close (in, NULL, &error);
100 g_assert_no_error (error);
101 g_clear_object (&in);
103 data = g_resource_lookup_data (resource,
104 "/a_prefix/test2.txt",
105 G_RESOURCE_LOOKUP_FLAGS_NONE,
107 g_assert (data != NULL);
108 g_assert_no_error (error);
109 size = g_bytes_get_size (data);
110 g_assert_cmpint (size, ==, 6);
111 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
112 g_bytes_unref (data);
114 data = g_resource_lookup_data (resource,
115 "/a_prefix/test2-alias.txt",
116 G_RESOURCE_LOOKUP_FLAGS_NONE,
118 g_assert (data != NULL);
119 g_assert_no_error (error);
120 size = g_bytes_get_size (data);
121 g_assert_cmpint (size, ==, 6);
122 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
123 g_bytes_unref (data);
125 children = g_resource_enumerate_children (resource,
127 G_RESOURCE_LOOKUP_FLAGS_NONE,
129 g_assert (children == NULL);
130 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
131 g_clear_error (&error);
133 children = g_resource_enumerate_children (resource,
135 G_RESOURCE_LOOKUP_FLAGS_NONE,
137 g_assert (children != NULL);
138 g_assert_no_error (error);
139 g_assert_cmpint (g_strv_length (children), ==, 2);
140 g_strfreev (children);
144 test_resource_file (void)
147 GError *error = NULL;
150 resource = g_resource_load ("not-there", &error);
151 g_assert (resource == NULL);
152 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
153 g_clear_error (&error);
155 path = g_build_filename (datapath, "test.gresource", NULL);
156 resource = g_resource_load (path, &error);
157 g_assert (resource != NULL);
158 g_assert_no_error (error);
161 test_resource (resource);
162 g_resource_unref (resource);
166 test_resource_data (void)
169 GError *error = NULL;
170 gboolean loaded_file;
176 path = g_build_filename (datapath, "test.gresource", NULL);
177 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
178 g_assert (loaded_file);
181 data = g_bytes_new_take (content, content_size);
182 resource = g_resource_new_from_data (data, &error);
183 g_bytes_unref (data);
184 g_assert (resource != NULL);
185 g_assert_no_error (error);
187 test_resource (resource);
189 g_resource_unref (resource);
193 test_resource_registered (void)
196 GError *error = NULL;
197 gboolean found, success;
206 path = g_build_filename (datapath, "test.gresource", NULL);
207 resource = g_resource_load (path, &error);
208 g_assert (resource != NULL);
209 g_assert_no_error (error);
212 found = g_resources_get_info ("/test1.txt",
213 G_RESOURCE_LOOKUP_FLAGS_NONE,
214 &size, &flags, &error);
216 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
217 g_clear_error (&error);
219 g_resources_register (resource);
221 found = g_resources_get_info ("/test1.txt",
222 G_RESOURCE_LOOKUP_FLAGS_NONE,
223 &size, &flags, &error);
225 g_assert_no_error (error);
226 g_assert_cmpint (size, ==, 6);
227 g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
229 found = g_resources_get_info ("/a_prefix/test2.txt",
230 G_RESOURCE_LOOKUP_FLAGS_NONE,
231 &size, &flags, &error);
233 g_assert_no_error (error);
234 g_assert_cmpint (size, ==, 6);
235 g_assert_cmpint (flags, ==, 0);
237 found = g_resources_get_info ("/a_prefix/test2-alias.txt",
238 G_RESOURCE_LOOKUP_FLAGS_NONE,
239 &size, &flags, &error);
241 g_assert_no_error (error);
242 g_assert_cmpint (size, ==, 6);
243 g_assert_cmpuint (flags, ==, 0);
245 data = g_resources_lookup_data ("/test1.txt",
246 G_RESOURCE_LOOKUP_FLAGS_NONE,
248 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
249 g_assert_no_error (error);
250 g_bytes_unref (data);
252 in = g_resources_open_stream ("/test1.txt",
253 G_RESOURCE_LOOKUP_FLAGS_NONE,
255 g_assert (in != NULL);
256 g_assert_no_error (error);
258 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
262 g_assert_no_error (error);
263 g_assert_cmpint (size, ==, 6);
265 g_assert_cmpstr (buffer, ==, "test1\n");
267 g_input_stream_close (in, NULL, &error);
268 g_assert_no_error (error);
269 g_clear_object (&in);
271 data = g_resources_lookup_data ("/a_prefix/test2.txt",
272 G_RESOURCE_LOOKUP_FLAGS_NONE,
274 g_assert (data != NULL);
275 g_assert_no_error (error);
276 size = g_bytes_get_size (data);
277 g_assert_cmpint (size, ==, 6);
278 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
279 g_bytes_unref (data);
281 data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
282 G_RESOURCE_LOOKUP_FLAGS_NONE,
284 g_assert (data != NULL);
285 g_assert_no_error (error);
286 size = g_bytes_get_size (data);
287 g_assert_cmpint (size, ==, 6);
288 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
289 g_bytes_unref (data);
291 children = g_resources_enumerate_children ("/not/here",
292 G_RESOURCE_LOOKUP_FLAGS_NONE,
294 g_assert (children == NULL);
295 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
296 g_clear_error (&error);
298 children = g_resources_enumerate_children ("/a_prefix",
299 G_RESOURCE_LOOKUP_FLAGS_NONE,
301 g_assert (children != NULL);
302 g_assert_no_error (error);
303 g_assert_cmpint (g_strv_length (children), ==, 2);
304 g_strfreev (children);
306 g_resources_unregister (resource);
307 g_resource_unref (resource);
309 found = g_resources_get_info ("/test1.txt",
310 G_RESOURCE_LOOKUP_FLAGS_NONE,
311 &size, &flags, &error);
313 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
314 g_clear_error (&error);
318 test_resource_automatic (void)
320 GError *error = NULL;
326 found = g_resources_get_info ("/auto_loaded/test1.txt",
327 G_RESOURCE_LOOKUP_FLAGS_NONE,
328 &size, &flags, &error);
330 g_assert_no_error (error);
331 g_assert_cmpint (size, ==, 6);
332 g_assert_cmpint (flags, ==, 0);
334 data = g_resources_lookup_data ("/auto_loaded/test1.txt",
335 G_RESOURCE_LOOKUP_FLAGS_NONE,
337 g_assert (data != NULL);
338 g_assert_no_error (error);
339 size = g_bytes_get_size (data);
340 g_assert_cmpint (size, ==, 6);
341 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
342 g_bytes_unref (data);
346 test_resource_manual (void)
348 GError *error = NULL;
354 found = g_resources_get_info ("/manual_loaded/test1.txt",
355 G_RESOURCE_LOOKUP_FLAGS_NONE,
356 &size, &flags, &error);
358 g_assert_no_error (error);
359 g_assert_cmpint (size, ==, 6);
360 g_assert_cmpuint (flags, ==, 0);
362 data = g_resources_lookup_data ("/manual_loaded/test1.txt",
363 G_RESOURCE_LOOKUP_FLAGS_NONE,
365 g_assert (data != NULL);
366 g_assert_no_error (error);
367 size = g_bytes_get_size (data);
368 g_assert_cmpint (size, ==, 6);
369 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
370 g_bytes_unref (data);
374 test_resource_manual2 (void)
379 GError *error = NULL;
381 resource = _g_test2_get_resource ();
383 data = g_resource_lookup_data (resource,
384 "/manual_loaded/test1.txt",
385 G_RESOURCE_LOOKUP_FLAGS_NONE,
387 g_assert (data != NULL);
388 g_assert_no_error (error);
389 size = g_bytes_get_size (data);
390 g_assert_cmpint (size, ==, 6);
391 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
392 g_bytes_unref (data);
394 g_resource_unref (resource);
398 test_resource_module (void)
407 if (g_module_supported ())
411 path = g_build_filename (libpath, "libresourceplugin", NULL);
412 module = g_io_module_new (path);
417 found = g_resources_get_info ("/resourceplugin/test1.txt",
418 G_RESOURCE_LOOKUP_FLAGS_NONE,
419 &size, &flags, &error);
421 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
422 g_clear_error (&error);
424 g_type_module_use (G_TYPE_MODULE (module));
426 found = g_resources_get_info ("/resourceplugin/test1.txt",
427 G_RESOURCE_LOOKUP_FLAGS_NONE,
428 &size, &flags, &error);
430 g_assert_no_error (error);
431 g_assert_cmpint (size, ==, 6);
432 g_assert_cmpuint (flags, ==, 0);
434 data = g_resources_lookup_data ("/resourceplugin/test1.txt",
435 G_RESOURCE_LOOKUP_FLAGS_NONE,
437 g_assert (data != NULL);
438 g_assert_no_error (error);
439 size = g_bytes_get_size (data);
440 g_assert_cmpint (size, ==, 6);
441 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
442 g_bytes_unref (data);
444 g_type_module_unuse (G_TYPE_MODULE (module));
446 found = g_resources_get_info ("/resourceplugin/test1.txt",
447 G_RESOURCE_LOOKUP_FLAGS_NONE,
448 &size, &flags, &error);
450 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
451 g_clear_error (&error);
456 test_uri_query_info (void)
459 GError *error = NULL;
460 gboolean loaded_file;
467 const char *content_type;
469 path = g_build_filename (datapath, "test.gresource", NULL);
470 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
471 g_assert (loaded_file);
474 data = g_bytes_new_take (content, content_size);
475 resource = g_resource_new_from_data (data, &error);
476 g_bytes_unref (data);
477 g_assert (resource != NULL);
478 g_assert_no_error (error);
480 g_resources_register (resource);
482 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
484 info = g_file_query_info (file, "*", 0, NULL, &error);
485 g_assert_no_error (error);
487 content_type = g_file_info_get_content_type (info);
488 g_assert (content_type);
489 g_assert_cmpstr (content_type, ==, "text/plain");
491 g_object_unref (info);
493 g_object_unref (file);
495 g_resources_unregister (resource);
496 g_resource_unref (resource);
503 GError *error = NULL;
504 gboolean loaded_file;
511 GFile *file2, *parent;
512 GFileEnumerator *enumerator;
514 GFileAttributeInfoList *attrs;
515 GInputStream *stream;
521 path = g_build_filename (datapath, "test.gresource", NULL);
522 loaded_file = g_file_get_contents (path, &content, &content_size, NULL);
523 g_assert (loaded_file);
526 data = g_bytes_new_take (content, content_size);
527 resource = g_resource_new_from_data (data, &error);
528 g_bytes_unref (data);
529 g_assert (resource != NULL);
530 g_assert_no_error (error);
532 g_resources_register (resource);
534 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
536 g_assert (g_file_get_path (file) == NULL);
538 name = g_file_get_parse_name (file);
539 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
542 name = g_file_get_uri (file);
543 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
546 g_assert (!g_file_is_native (file));
547 g_assert (!g_file_has_uri_scheme (file, "http"));
548 g_assert (g_file_has_uri_scheme (file, "resource"));
549 scheme = g_file_get_uri_scheme (file);
550 g_assert_cmpstr (scheme, ==, "resource");
553 file2 = g_file_dup (file);
554 g_assert (g_file_equal (file, file2));
555 g_object_unref (file2);
557 parent = g_file_get_parent (file);
558 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
559 g_assert_no_error (error);
561 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
562 g_assert_no_error (error);
563 g_assert (g_file_equal (file, file2));
564 g_object_unref (file2);
566 info = g_file_enumerator_next_file (enumerator, NULL, &error);
567 g_assert_no_error (error);
568 g_assert (info != NULL);
569 g_object_unref (info);
571 info = g_file_enumerator_next_file (enumerator, NULL, &error);
572 g_assert_no_error (error);
573 g_assert (info != NULL);
574 g_object_unref (info);
576 info = g_file_enumerator_next_file (enumerator, NULL, &error);
577 g_assert_no_error (error);
578 g_assert (info == NULL);
580 g_file_enumerator_close (enumerator, NULL, &error);
581 g_assert_no_error (error);
582 g_object_unref (enumerator);
584 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
585 g_assert (g_file_equal (file, file2));
587 g_assert (g_file_has_prefix (file, parent));
589 name = g_file_get_relative_path (parent, file);
590 g_assert_cmpstr (name, ==, "test2-alias.txt");
593 g_object_unref (parent);
595 attrs = g_file_query_settable_attributes (file, NULL, &error);
596 g_assert_no_error (error);
597 g_file_attribute_info_list_unref (attrs);
599 attrs = g_file_query_writable_namespaces (file, NULL, &error);
600 g_assert_no_error (error);
601 g_file_attribute_info_list_unref (attrs);
603 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
604 g_assert_no_error (error);
605 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
606 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
607 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
609 g_assert_no_error (error);
610 skipped = g_input_stream_skip (stream, 1, NULL, &error);
611 g_assert_cmpint (skipped, ==, 1);
612 g_assert_no_error (error);
614 memset (buf, 0, 1024);
615 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
617 g_assert_no_error (error);
618 g_assert_cmpstr (buf, ==, "st2\n");
619 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
620 G_FILE_ATTRIBUTE_STANDARD_SIZE,
623 g_assert_no_error (error);
624 g_assert (info != NULL);
625 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
626 g_object_unref (info);
628 ret = g_input_stream_close (stream, NULL, &error);
630 g_assert_no_error (error);
631 g_object_unref (stream);
633 g_object_unref (file);
634 g_object_unref (file2);
636 g_resources_unregister (resource);
637 g_resource_unref (resource);
644 if (g_getenv ("G_TEST_DATA"))
646 datapath = g_getenv ("G_TEST_DATA");
647 libpath = g_getenv ("G_TEST_DATA");
655 g_test_init (&argc, &argv, NULL);
657 _g_test2_register_resource ();
659 g_test_add_func ("/resource/file", test_resource_file);
660 g_test_add_func ("/resource/data", test_resource_data);
661 g_test_add_func ("/resource/registered", test_resource_registered);
662 g_test_add_func ("/resource/manual", test_resource_manual);
663 g_test_add_func ("/resource/manual2", test_resource_manual2);
664 #ifdef G_HAS_CONSTRUCTORS
665 g_test_add_func ("/resource/automatic", test_resource_automatic);
666 /* This only uses automatic resources too, so it tests the constructors and destructors */
667 g_test_add_func ("/resource/module", test_resource_module);
669 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
670 g_test_add_func ("/resource/uri/file", test_uri_file);