Add some more cases to the app-id unit tests
[glib.git] / gio / tests / resources.c
blob4d2649d12545007fc6a044f2f717b56c82fce838
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/>.
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];
36 found = g_resource_get_info (resource,
37 "/not/there",
38 G_RESOURCE_LOOKUP_FLAGS_NONE,
39 &size, &flags, &error);
40 g_assert (!found);
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,
45 "/test1.txt",
46 G_RESOURCE_LOOKUP_FLAGS_NONE,
47 &size, &flags, &error);
48 g_assert (found);
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);
57 g_assert (found);
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);
66 g_assert (found);
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,
72 "/test1.txt",
73 G_RESOURCE_LOOKUP_FLAGS_NONE,
74 &error);
75 g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
76 g_assert_no_error (error);
77 g_bytes_unref (data);
79 in = g_resource_open_stream (resource,
80 "/test1.txt",
81 G_RESOURCE_LOOKUP_FLAGS_NONE,
82 &error);
83 g_assert (in != NULL);
84 g_assert_no_error (error);
86 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
87 &size,
88 NULL, &error);
89 g_assert (success);
90 g_assert_no_error (error);
91 g_assert_cmpint (size, ==, 6);
92 buffer[size] = 0;
93 g_assert_cmpstr (buffer, ==, "test1\n");
95 g_input_stream_close (in, NULL, &error);
96 g_assert_no_error (error);
97 g_clear_object (&in);
99 data = g_resource_lookup_data (resource,
100 "/a_prefix/test2.txt",
101 G_RESOURCE_LOOKUP_FLAGS_NONE,
102 &error);
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,
113 &error);
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,
122 "/not/here",
123 G_RESOURCE_LOOKUP_FLAGS_NONE,
124 &error);
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,
130 "/a_prefix",
131 G_RESOURCE_LOOKUP_FLAGS_NONE,
132 &error);
133 g_assert (children != NULL);
134 g_assert_no_error (error);
135 g_assert_cmpint (g_strv_length (children), ==, 2);
136 g_strfreev (children);
139 static void
140 test_resource_file (void)
142 GResource *resource;
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);
158 static void
159 test_resource_data (void)
161 GResource *resource;
162 GError *error = NULL;
163 gboolean loaded_file;
164 char *content;
165 gsize content_size;
166 GBytes *data;
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);
183 static void
184 test_resource_registered (void)
186 GResource *resource;
187 GError *error = NULL;
188 gboolean found, success;
189 gsize size;
190 guint32 flags;
191 GBytes *data;
192 char **children;
193 GInputStream *in;
194 char buffer[128];
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);
203 g_assert (!found);
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);
212 g_assert (found);
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);
220 g_assert (found);
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);
228 g_assert (found);
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,
235 &error);
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,
242 &error);
243 g_assert (in != NULL);
244 g_assert_no_error (error);
246 success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
247 &size,
248 NULL, &error);
249 g_assert (success);
250 g_assert_no_error (error);
251 g_assert_cmpint (size, ==, 6);
252 buffer[size] = 0;
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,
261 &error);
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,
271 &error);
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,
281 &error);
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,
288 &error);
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);
300 g_assert (!found);
301 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
302 g_clear_error (&error);
305 static void
306 test_resource_automatic (void)
308 GError *error = NULL;
309 gboolean found;
310 gsize size;
311 guint32 flags;
312 GBytes *data;
314 found = g_resources_get_info ("/auto_loaded/test1.txt",
315 G_RESOURCE_LOOKUP_FLAGS_NONE,
316 &size, &flags, &error);
317 g_assert (found);
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,
324 &error);
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);
333 static void
334 test_resource_manual (void)
336 GError *error = NULL;
337 gboolean found;
338 gsize size;
339 guint32 flags;
340 GBytes *data;
342 found = g_resources_get_info ("/manual_loaded/test1.txt",
343 G_RESOURCE_LOOKUP_FLAGS_NONE,
344 &size, &flags, &error);
345 g_assert (found);
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,
352 &error);
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);
361 static void
362 test_resource_manual2 (void)
364 GResource *resource;
365 GBytes *data;
366 gsize size;
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,
374 &error);
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);
385 static void
386 test_resource_module (void)
388 GIOModule *module;
389 gboolean found;
390 gsize size;
391 guint32 flags;
392 GBytes *data;
393 GError *error;
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));
400 error = NULL;
402 found = g_resources_get_info ("/resourceplugin/test1.txt",
403 G_RESOURCE_LOOKUP_FLAGS_NONE,
404 &size, &flags, &error);
405 g_assert (!found);
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);
414 g_assert (found);
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,
421 &error);
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);
434 g_assert (!found);
435 g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
436 g_clear_error (&error);
440 static void
441 test_uri_query_info (void)
443 GResource *resource;
444 GError *error = NULL;
445 gboolean loaded_file;
446 char *content;
447 gsize content_size;
448 GBytes *data;
449 GFile *file;
450 GFileInfo *info;
451 const char *content_type, *mime_type;
452 const char *fs_type;
453 gboolean readonly;
455 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
456 &content, &content_size, NULL);
457 g_assert (loaded_file);
459 data = g_bytes_new_take (content, content_size);
460 resource = g_resource_new_from_data (data, &error);
461 g_bytes_unref (data);
462 g_assert (resource != NULL);
463 g_assert_no_error (error);
465 g_resources_register (resource);
467 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
469 info = g_file_query_info (file, "*", 0, NULL, &error);
470 g_assert_no_error (error);
472 content_type = g_file_info_get_content_type (info);
473 g_assert (content_type);
474 mime_type = g_content_type_get_mime_type (content_type);
475 g_assert (mime_type);
476 g_assert_cmpstr (mime_type, ==, "text/plain");
478 g_object_unref (info);
480 info = g_file_query_filesystem_info (file, "*", NULL, &error);
481 g_assert_no_error (error);
483 fs_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE);
484 g_assert_cmpstr (fs_type, ==, "resource");
485 readonly = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
486 g_assert_true (readonly);
488 g_object_unref (info);
490 g_assert_cmpuint (g_file_hash (file), !=, 0);
492 g_object_unref (file);
494 g_resources_unregister (resource);
495 g_resource_unref (resource);
498 static void
499 test_uri_file (void)
501 GResource *resource;
502 GError *error = NULL;
503 gboolean loaded_file;
504 char *content;
505 gsize content_size;
506 GBytes *data;
507 GFile *file;
508 GFileInfo *info;
509 gchar *name;
510 GFile *file2, *parent;
511 GFileEnumerator *enumerator;
512 gchar *scheme;
513 GFileAttributeInfoList *attrs;
514 GInputStream *stream;
515 gchar buf[1024];
516 gboolean ret;
517 gssize skipped;
519 loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
520 &content, &content_size, NULL);
521 g_assert (loaded_file);
523 data = g_bytes_new_take (content, content_size);
524 resource = g_resource_new_from_data (data, &error);
525 g_bytes_unref (data);
526 g_assert (resource != NULL);
527 g_assert_no_error (error);
529 g_resources_register (resource);
531 file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
533 g_assert (g_file_get_path (file) == NULL);
535 name = g_file_get_parse_name (file);
536 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
537 g_free (name);
539 name = g_file_get_uri (file);
540 g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
541 g_free (name);
543 g_assert (!g_file_is_native (file));
544 g_assert (!g_file_has_uri_scheme (file, "http"));
545 g_assert (g_file_has_uri_scheme (file, "resource"));
546 scheme = g_file_get_uri_scheme (file);
547 g_assert_cmpstr (scheme, ==, "resource");
548 g_free (scheme);
550 file2 = g_file_dup (file);
551 g_assert (g_file_equal (file, file2));
552 g_object_unref (file2);
554 parent = g_file_get_parent (file);
555 enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
556 g_assert_no_error (error);
558 file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
559 g_assert_no_error (error);
560 g_assert (g_file_equal (file, file2));
561 g_object_unref (file2);
563 info = g_file_enumerator_next_file (enumerator, NULL, &error);
564 g_assert_no_error (error);
565 g_assert (info != NULL);
566 g_object_unref (info);
568 info = g_file_enumerator_next_file (enumerator, NULL, &error);
569 g_assert_no_error (error);
570 g_assert (info != NULL);
571 g_object_unref (info);
573 info = g_file_enumerator_next_file (enumerator, NULL, &error);
574 g_assert_no_error (error);
575 g_assert (info == NULL);
577 g_file_enumerator_close (enumerator, NULL, &error);
578 g_assert_no_error (error);
579 g_object_unref (enumerator);
581 file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
582 g_assert (g_file_equal (file, file2));
584 g_assert (g_file_has_prefix (file, parent));
586 name = g_file_get_relative_path (parent, file);
587 g_assert_cmpstr (name, ==, "test2-alias.txt");
588 g_free (name);
590 g_object_unref (parent);
592 attrs = g_file_query_settable_attributes (file, NULL, &error);
593 g_assert_no_error (error);
594 g_file_attribute_info_list_unref (attrs);
596 attrs = g_file_query_writable_namespaces (file, NULL, &error);
597 g_assert_no_error (error);
598 g_file_attribute_info_list_unref (attrs);
600 stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
601 g_assert_no_error (error);
602 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
603 g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
604 ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
605 g_assert (ret);
606 g_assert_no_error (error);
607 skipped = g_input_stream_skip (stream, 1, NULL, &error);
608 g_assert_cmpint (skipped, ==, 1);
609 g_assert_no_error (error);
611 memset (buf, 0, 1024);
612 ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
613 g_assert (ret);
614 g_assert_no_error (error);
615 g_assert_cmpstr (buf, ==, "st2\n");
616 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
617 G_FILE_ATTRIBUTE_STANDARD_SIZE,
618 NULL,
619 &error);
620 g_assert_no_error (error);
621 g_assert (info != NULL);
622 g_assert_cmpint (g_file_info_get_size (info), ==, 6);
623 g_object_unref (info);
625 ret = g_input_stream_close (stream, NULL, &error);
626 g_assert (ret);
627 g_assert_no_error (error);
628 g_object_unref (stream);
630 g_object_unref (file);
631 g_object_unref (file2);
633 g_resources_unregister (resource);
634 g_resource_unref (resource);
638 main (int argc,
639 char *argv[])
641 g_test_init (&argc, &argv, NULL);
643 _g_test2_register_resource ();
645 g_test_add_func ("/resource/file", test_resource_file);
646 g_test_add_func ("/resource/data", test_resource_data);
647 g_test_add_func ("/resource/registered", test_resource_registered);
648 g_test_add_func ("/resource/manual", test_resource_manual);
649 g_test_add_func ("/resource/manual2", test_resource_manual2);
650 #ifdef G_HAS_CONSTRUCTORS
651 g_test_add_func ("/resource/automatic", test_resource_automatic);
652 /* This only uses automatic resources too, so it tests the constructors and destructors */
653 g_test_add_func ("/resource/module", test_resource_module);
654 #endif
655 g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
656 g_test_add_func ("/resource/uri/file", test_uri_file);
658 return g_test_run();