Convert some gio tests to installed tests
[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 const gchar *datapath;
27
28 static void
29 test_resource (GResource *resource)
30 {
31   GError *error = NULL;
32   gboolean found, success;
33   gsize size;
34   guint32 flags;
35   GBytes *data;
36   char **children;
37   GInputStream *in;
38   char buffer[128];
39
40   found = g_resource_get_info (resource,
41                                "/not/there",
42                                G_RESOURCE_LOOKUP_FLAGS_NONE,
43                                &size, &flags, &error);
44   g_assert (!found);
45   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
46   g_clear_error (&error);
47
48   found = g_resource_get_info (resource,
49                                "/test1.txt",
50                                G_RESOURCE_LOOKUP_FLAGS_NONE,
51                                &size, &flags, &error);
52   g_assert (found);
53   g_assert_no_error (error);
54   g_assert_cmpint (size, ==, 6);
55   g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
56
57   found = g_resource_get_info (resource,
58                                "/a_prefix/test2.txt",
59                                G_RESOURCE_LOOKUP_FLAGS_NONE,
60                                &size, &flags, &error);
61   g_assert (found);
62   g_assert_no_error (error);
63   g_assert_cmpint (size, ==, 6);
64   g_assert_cmpuint (flags, ==, 0);
65
66   found = g_resource_get_info (resource,
67                                "/a_prefix/test2-alias.txt",
68                                G_RESOURCE_LOOKUP_FLAGS_NONE,
69                                &size, &flags, &error);
70   g_assert (found);
71   g_assert_no_error (error);
72   g_assert_cmpint (size, ==, 6);
73   g_assert_cmpuint (flags, ==, 0);
74
75   data = g_resource_lookup_data (resource,
76                                  "/test1.txt",
77                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
78                                  &error);
79   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
80   g_assert_no_error (error);
81   g_bytes_unref (data);
82
83   in = g_resource_open_stream (resource,
84                                "/test1.txt",
85                                G_RESOURCE_LOOKUP_FLAGS_NONE,
86                                &error);
87   g_assert (in != NULL);
88   g_assert_no_error (error);
89
90   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
91                                      &size,
92                                      NULL, &error);
93   g_assert (success);
94   g_assert_no_error (error);
95   g_assert_cmpint (size, ==, 6);
96   buffer[size] = 0;
97   g_assert_cmpstr (buffer, ==, "test1\n");
98
99   g_input_stream_close (in, NULL, &error);
100   g_assert_no_error (error);
101   g_clear_object (&in);
102
103   data = g_resource_lookup_data (resource,
104                                  "/a_prefix/test2.txt",
105                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
106                                  &error);
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);
113
114   data = g_resource_lookup_data (resource,
115                                  "/a_prefix/test2-alias.txt",
116                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
117                                  &error);
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);
124
125   children = g_resource_enumerate_children  (resource,
126                                              "/not/here",
127                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
128                                              &error);
129   g_assert (children == NULL);
130   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
131   g_clear_error (&error);
132
133   children = g_resource_enumerate_children  (resource,
134                                              "/a_prefix",
135                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
136                                              &error);
137   g_assert (children != NULL);
138   g_assert_no_error (error);
139   g_assert_cmpint (g_strv_length (children), ==, 2);
140   g_strfreev (children);
141 }
142
143 static void
144 test_resource_file (void)
145 {
146   GResource *resource;
147   GError *error = NULL;
148   gchar *path;
149
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);
154
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);
159   g_free (path);
160
161   test_resource (resource);
162   g_resource_unref (resource);
163 }
164
165 static void
166 test_resource_data (void)
167 {
168   GResource *resource;
169   GError *error = NULL;
170   gboolean loaded_file;
171   char *content;
172   gsize content_size;
173   GBytes *data;
174   gchar *path;
175
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);
179   g_free (path);
180
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);
186
187   test_resource (resource);
188
189   g_resource_unref (resource);
190 }
191
192 static void
193 test_resource_registered (void)
194 {
195   GResource *resource;
196   GError *error = NULL;
197   gboolean found, success;
198   gsize size;
199   guint32 flags;
200   GBytes *data;
201   char **children;
202   GInputStream *in;
203   char buffer[128];
204   gchar *path;
205
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);
210   g_free (path);
211
212   found = g_resources_get_info ("/test1.txt",
213                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
214                                 &size, &flags, &error);
215   g_assert (!found);
216   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
217   g_clear_error (&error);
218
219   g_resources_register (resource);
220
221   found = g_resources_get_info ("/test1.txt",
222                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
223                                 &size, &flags, &error);
224   g_assert (found);
225   g_assert_no_error (error);
226   g_assert_cmpint (size, ==, 6);
227   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
228
229   found = g_resources_get_info ("/a_prefix/test2.txt",
230                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
231                                 &size, &flags, &error);
232   g_assert (found);
233   g_assert_no_error (error);
234   g_assert_cmpint (size, ==, 6);
235   g_assert_cmpint (flags, ==, 0);
236
237   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
238                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
239                                 &size, &flags, &error);
240   g_assert (found);
241   g_assert_no_error (error);
242   g_assert_cmpint (size, ==, 6);
243   g_assert_cmpuint (flags, ==, 0);
244
245   data = g_resources_lookup_data ("/test1.txt",
246                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
247                                   &error);
248   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
249   g_assert_no_error (error);
250   g_bytes_unref (data);
251
252   in = g_resources_open_stream ("/test1.txt",
253                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
254                                 &error);
255   g_assert (in != NULL);
256   g_assert_no_error (error);
257
258   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
259                                      &size,
260                                      NULL, &error);
261   g_assert (success);
262   g_assert_no_error (error);
263   g_assert_cmpint (size, ==, 6);
264   buffer[size] = 0;
265   g_assert_cmpstr (buffer, ==, "test1\n");
266
267   g_input_stream_close (in, NULL, &error);
268   g_assert_no_error (error);
269   g_clear_object (&in);
270
271   data = g_resources_lookup_data ("/a_prefix/test2.txt",
272                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
273                                   &error);
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);
280
281   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
282                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
283                                   &error);
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);
290
291   children = g_resources_enumerate_children ("/not/here",
292                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
293                                              &error);
294   g_assert (children == NULL);
295   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
296   g_clear_error (&error);
297
298   children = g_resources_enumerate_children ("/a_prefix",
299                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
300                                              &error);
301   g_assert (children != NULL);
302   g_assert_no_error (error);
303   g_assert_cmpint (g_strv_length (children), ==, 2);
304   g_strfreev (children);
305
306   g_resources_unregister (resource);
307   g_resource_unref (resource);
308
309   found = g_resources_get_info ("/test1.txt",
310                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
311                                 &size, &flags, &error);
312   g_assert (!found);
313   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
314   g_clear_error (&error);
315 }
316
317 static void
318 test_resource_automatic (void)
319 {
320   GError *error = NULL;
321   gboolean found;
322   gsize size;
323   guint32 flags;
324   GBytes *data;
325
326   found = g_resources_get_info ("/auto_loaded/test1.txt",
327                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
328                                 &size, &flags, &error);
329   g_assert (found);
330   g_assert_no_error (error);
331   g_assert_cmpint (size, ==, 6);
332   g_assert_cmpint (flags, ==, 0);
333
334   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
335                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
336                                   &error);
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);
343 }
344
345 static void
346 test_resource_manual (void)
347 {
348   GError *error = NULL;
349   gboolean found;
350   gsize size;
351   guint32 flags;
352   GBytes *data;
353
354   found = g_resources_get_info ("/manual_loaded/test1.txt",
355                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
356                                 &size, &flags, &error);
357   g_assert (found);
358   g_assert_no_error (error);
359   g_assert_cmpint (size, ==, 6);
360   g_assert_cmpuint (flags, ==, 0);
361
362   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
363                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
364                                   &error);
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);
371 }
372
373 static void
374 test_resource_manual2 (void)
375 {
376   GResource *resource;
377   GBytes *data;
378   gsize size;
379   GError *error = NULL;
380
381   resource = _g_test2_get_resource ();
382
383   data = g_resource_lookup_data (resource,
384                                  "/manual_loaded/test1.txt",
385                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
386                                  &error);
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);
393
394   g_resource_unref (resource);
395 }
396
397 static void
398 test_resource_module (void)
399 {
400   GIOModule *module;
401   gboolean found;
402   gsize size;
403   guint32 flags;
404   GBytes *data;
405   GError *error;
406
407   if (g_module_supported ())
408     {
409       char *path;
410
411       path = g_build_filename (datapath, "libresourceplugin",  NULL);
412       module = g_io_module_new (path);
413       g_free (path);
414
415       error = NULL;
416
417       found = g_resources_get_info ("/resourceplugin/test1.txt",
418                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
419                                     &size, &flags, &error);
420       g_assert (!found);
421       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
422       g_clear_error (&error);
423
424       g_type_module_use (G_TYPE_MODULE (module));
425
426       found = g_resources_get_info ("/resourceplugin/test1.txt",
427                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
428                                     &size, &flags, &error);
429       g_assert (found);
430       g_assert_no_error (error);
431       g_assert_cmpint (size, ==, 6);
432       g_assert_cmpuint (flags, ==, 0);
433
434       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
435                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
436                                       &error);
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);
443
444       g_type_module_unuse (G_TYPE_MODULE (module));
445
446       found = g_resources_get_info ("/resourceplugin/test1.txt",
447                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
448                                     &size, &flags, &error);
449       g_assert (!found);
450       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
451       g_clear_error (&error);
452     }
453 }
454
455 static void
456 test_uri_query_info (void)
457 {
458   GResource *resource;
459   GError *error = NULL;
460   gboolean loaded_file;
461   char *content;
462   gsize content_size;
463   GBytes *data;
464   GFile *file;
465   GFileInfo *info;
466   gchar *path;
467   const char *content_type;
468
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);
472   g_free (path);
473
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);
479
480   g_resources_register (resource);
481
482   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
483
484   info = g_file_query_info (file, "*", 0, NULL, &error);
485   g_assert_no_error (error);
486
487   content_type = g_file_info_get_content_type (info);
488   g_assert (content_type);
489   g_assert_cmpstr (content_type, ==, "text/plain");
490
491   g_object_unref (info);
492
493   g_object_unref (file);
494
495   g_resources_unregister (resource);
496   g_resource_unref (resource);
497 }
498
499 static void
500 test_uri_file (void)
501 {
502   GResource *resource;
503   GError *error = NULL;
504   gboolean loaded_file;
505   char *content;
506   gsize content_size;
507   GBytes *data;
508   GFile *file;
509   GFileInfo *info;
510   gchar *name;
511   GFile *file2, *parent;
512   GFileEnumerator *enumerator;
513   gchar *scheme;
514   GFileAttributeInfoList *attrs;
515   GInputStream *stream;
516   gchar buf[1024];
517   gboolean ret;
518   gssize skipped;
519   gchar *path;
520
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);
524   g_free (path);
525
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);
531
532   g_resources_register (resource);
533
534   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
535
536   g_assert (g_file_get_path (file) == NULL);
537
538   name = g_file_get_parse_name (file);
539   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
540   g_free (name);
541
542   name = g_file_get_uri (file);
543   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
544   g_free (name);
545
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");
551   g_free (scheme);
552
553   file2 = g_file_dup (file);
554   g_assert (g_file_equal (file, file2));
555   g_object_unref (file2);
556
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);
560
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);
565
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);
570
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);
575
576   info = g_file_enumerator_next_file (enumerator, NULL, &error);
577   g_assert_no_error (error);
578   g_assert (info == NULL);
579
580   g_file_enumerator_close (enumerator, NULL, &error);
581   g_assert_no_error (error);
582   g_object_unref (enumerator);
583
584   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
585   g_assert (g_file_equal (file, file2));
586
587   g_assert (g_file_has_prefix (file, parent));
588
589   name = g_file_get_relative_path (parent, file);
590   g_assert_cmpstr (name, ==, "test2-alias.txt");
591   g_free (name);
592
593   g_object_unref (parent);
594
595   attrs = g_file_query_settable_attributes (file, NULL, &error);
596   g_assert_no_error (error);
597   g_file_attribute_info_list_unref (attrs);
598
599   attrs = g_file_query_writable_namespaces (file, NULL, &error);
600   g_assert_no_error (error);
601   g_file_attribute_info_list_unref (attrs);
602
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);
608   g_assert (ret);
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);
613
614   memset (buf, 0, 1024);
615   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
616   g_assert (ret);
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,
621                                          NULL,
622                                          &error);
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);
627
628   ret = g_input_stream_close (stream, NULL, &error);
629   g_assert (ret);
630   g_assert_no_error (error);
631   g_object_unref (stream);
632
633   g_object_unref (file);
634   g_object_unref (file2);
635
636   g_resources_unregister (resource);
637   g_resource_unref (resource);
638 }
639
640 int
641 main (int   argc,
642       char *argv[])
643 {
644   if (g_getenv ("G_TEST_DATA"))
645     datapath = g_getenv ("G_TEST_DATA");
646   else
647     datapath = SRCDIR;
648
649   g_test_init (&argc, &argv, NULL);
650
651   _g_test2_register_resource ();
652
653   g_test_add_func ("/resource/file", test_resource_file);
654   g_test_add_func ("/resource/data", test_resource_data);
655   g_test_add_func ("/resource/registered", test_resource_registered);
656   g_test_add_func ("/resource/manual", test_resource_manual);
657   g_test_add_func ("/resource/manual2", test_resource_manual2);
658 #ifdef G_HAS_CONSTRUCTORS
659   g_test_add_func ("/resource/automatic", test_resource_automatic);
660   /* This only uses automatic resources too, so it tests the constructors and destructors */
661   g_test_add_func ("/resource/module", test_resource_module);
662 #endif
663   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
664   g_test_add_func ("/resource/uri/file", test_uri_file);
665
666   return g_test_run();
667 }