tests: use new g_test_build_filename() API
[platform/upstream/glib.git] / gio / tests / resources.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2011 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <string.h>
22 #include <gio/gio.h>
23 #include "gconstructor.h"
24 #include "test_resources2.h"
25
26 static void
27 test_resource (GResource *resource)
28 {
29   GError *error = NULL;
30   gboolean found, success;
31   gsize size;
32   guint32 flags;
33   GBytes *data;
34   char **children;
35   GInputStream *in;
36   char buffer[128];
37
38   found = g_resource_get_info (resource,
39                                "/not/there",
40                                G_RESOURCE_LOOKUP_FLAGS_NONE,
41                                &size, &flags, &error);
42   g_assert (!found);
43   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
44   g_clear_error (&error);
45
46   found = g_resource_get_info (resource,
47                                "/test1.txt",
48                                G_RESOURCE_LOOKUP_FLAGS_NONE,
49                                &size, &flags, &error);
50   g_assert (found);
51   g_assert_no_error (error);
52   g_assert_cmpint (size, ==, 6);
53   g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
54
55   found = g_resource_get_info (resource,
56                                "/a_prefix/test2.txt",
57                                G_RESOURCE_LOOKUP_FLAGS_NONE,
58                                &size, &flags, &error);
59   g_assert (found);
60   g_assert_no_error (error);
61   g_assert_cmpint (size, ==, 6);
62   g_assert_cmpuint (flags, ==, 0);
63
64   found = g_resource_get_info (resource,
65                                "/a_prefix/test2-alias.txt",
66                                G_RESOURCE_LOOKUP_FLAGS_NONE,
67                                &size, &flags, &error);
68   g_assert (found);
69   g_assert_no_error (error);
70   g_assert_cmpint (size, ==, 6);
71   g_assert_cmpuint (flags, ==, 0);
72
73   data = g_resource_lookup_data (resource,
74                                  "/test1.txt",
75                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
76                                  &error);
77   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
78   g_assert_no_error (error);
79   g_bytes_unref (data);
80
81   in = g_resource_open_stream (resource,
82                                "/test1.txt",
83                                G_RESOURCE_LOOKUP_FLAGS_NONE,
84                                &error);
85   g_assert (in != NULL);
86   g_assert_no_error (error);
87
88   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
89                                      &size,
90                                      NULL, &error);
91   g_assert (success);
92   g_assert_no_error (error);
93   g_assert_cmpint (size, ==, 6);
94   buffer[size] = 0;
95   g_assert_cmpstr (buffer, ==, "test1\n");
96
97   g_input_stream_close (in, NULL, &error);
98   g_assert_no_error (error);
99   g_clear_object (&in);
100
101   data = g_resource_lookup_data (resource,
102                                  "/a_prefix/test2.txt",
103                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
104                                  &error);
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);
111
112   data = g_resource_lookup_data (resource,
113                                  "/a_prefix/test2-alias.txt",
114                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
115                                  &error);
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);
122
123   children = g_resource_enumerate_children  (resource,
124                                              "/not/here",
125                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
126                                              &error);
127   g_assert (children == NULL);
128   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
129   g_clear_error (&error);
130
131   children = g_resource_enumerate_children  (resource,
132                                              "/a_prefix",
133                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
134                                              &error);
135   g_assert (children != NULL);
136   g_assert_no_error (error);
137   g_assert_cmpint (g_strv_length (children), ==, 2);
138   g_strfreev (children);
139 }
140
141 static void
142 test_resource_file (void)
143 {
144   GResource *resource;
145   GError *error = NULL;
146   gchar *path;
147
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);
152
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);
157   g_free (path);
158
159   test_resource (resource);
160   g_resource_unref (resource);
161 }
162
163 static void
164 test_resource_data (void)
165 {
166   GResource *resource;
167   GError *error = NULL;
168   gboolean loaded_file;
169   char *content;
170   gsize content_size;
171   GBytes *data;
172   gchar *path;
173
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);
177   g_free (path);
178
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);
184
185   test_resource (resource);
186
187   g_resource_unref (resource);
188 }
189
190 static void
191 test_resource_registered (void)
192 {
193   GResource *resource;
194   GError *error = NULL;
195   gboolean found, success;
196   gsize size;
197   guint32 flags;
198   GBytes *data;
199   char **children;
200   GInputStream *in;
201   char buffer[128];
202   gchar *path;
203
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);
208   g_free (path);
209
210   found = g_resources_get_info ("/test1.txt",
211                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
212                                 &size, &flags, &error);
213   g_assert (!found);
214   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
215   g_clear_error (&error);
216
217   g_resources_register (resource);
218
219   found = g_resources_get_info ("/test1.txt",
220                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
221                                 &size, &flags, &error);
222   g_assert (found);
223   g_assert_no_error (error);
224   g_assert_cmpint (size, ==, 6);
225   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
226
227   found = g_resources_get_info ("/a_prefix/test2.txt",
228                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
229                                 &size, &flags, &error);
230   g_assert (found);
231   g_assert_no_error (error);
232   g_assert_cmpint (size, ==, 6);
233   g_assert_cmpint (flags, ==, 0);
234
235   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
236                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
237                                 &size, &flags, &error);
238   g_assert (found);
239   g_assert_no_error (error);
240   g_assert_cmpint (size, ==, 6);
241   g_assert_cmpuint (flags, ==, 0);
242
243   data = g_resources_lookup_data ("/test1.txt",
244                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
245                                   &error);
246   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
247   g_assert_no_error (error);
248   g_bytes_unref (data);
249
250   in = g_resources_open_stream ("/test1.txt",
251                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
252                                 &error);
253   g_assert (in != NULL);
254   g_assert_no_error (error);
255
256   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
257                                      &size,
258                                      NULL, &error);
259   g_assert (success);
260   g_assert_no_error (error);
261   g_assert_cmpint (size, ==, 6);
262   buffer[size] = 0;
263   g_assert_cmpstr (buffer, ==, "test1\n");
264
265   g_input_stream_close (in, NULL, &error);
266   g_assert_no_error (error);
267   g_clear_object (&in);
268
269   data = g_resources_lookup_data ("/a_prefix/test2.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);
278
279   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
280                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
281                                   &error);
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);
288
289   children = g_resources_enumerate_children ("/not/here",
290                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
291                                              &error);
292   g_assert (children == NULL);
293   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
294   g_clear_error (&error);
295
296   children = g_resources_enumerate_children ("/a_prefix",
297                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
298                                              &error);
299   g_assert (children != NULL);
300   g_assert_no_error (error);
301   g_assert_cmpint (g_strv_length (children), ==, 2);
302   g_strfreev (children);
303
304   g_resources_unregister (resource);
305   g_resource_unref (resource);
306
307   found = g_resources_get_info ("/test1.txt",
308                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
309                                 &size, &flags, &error);
310   g_assert (!found);
311   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
312   g_clear_error (&error);
313 }
314
315 static void
316 test_resource_automatic (void)
317 {
318   GError *error = NULL;
319   gboolean found;
320   gsize size;
321   guint32 flags;
322   GBytes *data;
323
324   found = g_resources_get_info ("/auto_loaded/test1.txt",
325                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
326                                 &size, &flags, &error);
327   g_assert (found);
328   g_assert_no_error (error);
329   g_assert_cmpint (size, ==, 6);
330   g_assert_cmpint (flags, ==, 0);
331
332   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
333                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
334                                   &error);
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);
341 }
342
343 static void
344 test_resource_manual (void)
345 {
346   GError *error = NULL;
347   gboolean found;
348   gsize size;
349   guint32 flags;
350   GBytes *data;
351
352   found = g_resources_get_info ("/manual_loaded/test1.txt",
353                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
354                                 &size, &flags, &error);
355   g_assert (found);
356   g_assert_no_error (error);
357   g_assert_cmpint (size, ==, 6);
358   g_assert_cmpuint (flags, ==, 0);
359
360   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
361                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
362                                   &error);
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);
369 }
370
371 static void
372 test_resource_manual2 (void)
373 {
374   GResource *resource;
375   GBytes *data;
376   gsize size;
377   GError *error = NULL;
378
379   resource = _g_test2_get_resource ();
380
381   data = g_resource_lookup_data (resource,
382                                  "/manual_loaded/test1.txt",
383                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
384                                  &error);
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);
391
392   g_resource_unref (resource);
393 }
394
395 static void
396 test_resource_module (void)
397 {
398   GIOModule *module;
399   gboolean found;
400   gsize size;
401   guint32 flags;
402   GBytes *data;
403   GError *error;
404
405   if (g_module_supported ())
406     {
407       char *path;
408
409       path = g_test_build_filename (G_TEST_BUILT, "libresourceplugin",  NULL);
410       module = g_io_module_new (path);
411       g_free (path);
412
413       error = NULL;
414
415       found = g_resources_get_info ("/resourceplugin/test1.txt",
416                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
417                                     &size, &flags, &error);
418       g_assert (!found);
419       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
420       g_clear_error (&error);
421
422       g_type_module_use (G_TYPE_MODULE (module));
423
424       found = g_resources_get_info ("/resourceplugin/test1.txt",
425                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
426                                     &size, &flags, &error);
427       g_assert (found);
428       g_assert_no_error (error);
429       g_assert_cmpint (size, ==, 6);
430       g_assert_cmpuint (flags, ==, 0);
431
432       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
433                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
434                                       &error);
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);
441
442       g_type_module_unuse (G_TYPE_MODULE (module));
443
444       found = g_resources_get_info ("/resourceplugin/test1.txt",
445                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
446                                     &size, &flags, &error);
447       g_assert (!found);
448       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
449       g_clear_error (&error);
450     }
451 }
452
453 static void
454 test_uri_query_info (void)
455 {
456   GResource *resource;
457   GError *error = NULL;
458   gboolean loaded_file;
459   char *content;
460   gsize content_size;
461   GBytes *data;
462   GFile *file;
463   GFileInfo *info;
464   gchar *path;
465   const char *content_type;
466
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);
470   g_free (path);
471
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);
477
478   g_resources_register (resource);
479
480   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
481
482   info = g_file_query_info (file, "*", 0, NULL, &error);
483   g_assert_no_error (error);
484
485   content_type = g_file_info_get_content_type (info);
486   g_assert (content_type);
487   g_assert_cmpstr (content_type, ==, "text/plain");
488
489   g_object_unref (info);
490
491   g_object_unref (file);
492
493   g_resources_unregister (resource);
494   g_resource_unref (resource);
495 }
496
497 static void
498 test_uri_file (void)
499 {
500   GResource *resource;
501   GError *error = NULL;
502   gboolean loaded_file;
503   char *content;
504   gsize content_size;
505   GBytes *data;
506   GFile *file;
507   GFileInfo *info;
508   gchar *name;
509   GFile *file2, *parent;
510   GFileEnumerator *enumerator;
511   gchar *scheme;
512   GFileAttributeInfoList *attrs;
513   GInputStream *stream;
514   gchar buf[1024];
515   gboolean ret;
516   gssize skipped;
517   gchar *path;
518
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);
522   g_free (path);
523
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);
529
530   g_resources_register (resource);
531
532   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
533
534   g_assert (g_file_get_path (file) == NULL);
535
536   name = g_file_get_parse_name (file);
537   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
538   g_free (name);
539
540   name = g_file_get_uri (file);
541   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
542   g_free (name);
543
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");
549   g_free (scheme);
550
551   file2 = g_file_dup (file);
552   g_assert (g_file_equal (file, file2));
553   g_object_unref (file2);
554
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);
558
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);
563
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);
568
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);
573
574   info = g_file_enumerator_next_file (enumerator, NULL, &error);
575   g_assert_no_error (error);
576   g_assert (info == NULL);
577
578   g_file_enumerator_close (enumerator, NULL, &error);
579   g_assert_no_error (error);
580   g_object_unref (enumerator);
581
582   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
583   g_assert (g_file_equal (file, file2));
584
585   g_assert (g_file_has_prefix (file, parent));
586
587   name = g_file_get_relative_path (parent, file);
588   g_assert_cmpstr (name, ==, "test2-alias.txt");
589   g_free (name);
590
591   g_object_unref (parent);
592
593   attrs = g_file_query_settable_attributes (file, NULL, &error);
594   g_assert_no_error (error);
595   g_file_attribute_info_list_unref (attrs);
596
597   attrs = g_file_query_writable_namespaces (file, NULL, &error);
598   g_assert_no_error (error);
599   g_file_attribute_info_list_unref (attrs);
600
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);
606   g_assert (ret);
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);
611
612   memset (buf, 0, 1024);
613   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
614   g_assert (ret);
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,
619                                          NULL,
620                                          &error);
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);
625
626   ret = g_input_stream_close (stream, NULL, &error);
627   g_assert (ret);
628   g_assert_no_error (error);
629   g_object_unref (stream);
630
631   g_object_unref (file);
632   g_object_unref (file2);
633
634   g_resources_unregister (resource);
635   g_resource_unref (resource);
636 }
637
638 int
639 main (int   argc,
640       char *argv[])
641 {
642   g_test_init (&argc, &argv, NULL);
643
644   _g_test2_register_resource ();
645
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);
655 #endif
656   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
657   g_test_add_func ("/resource/uri/file", test_uri_file);
658
659   return g_test_run();
660 }