495c05b29cb7f155952524fe24a3c7847bf76873
[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
147   resource = g_resource_load ("not-there", &error);
148   g_assert (resource == NULL);
149   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
150   g_clear_error (&error);
151
152   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
153   g_assert (resource != NULL);
154   g_assert_no_error (error);
155
156   test_resource (resource);
157   g_resource_unref (resource);
158 }
159
160 static void
161 test_resource_data (void)
162 {
163   GResource *resource;
164   GError *error = NULL;
165   gboolean loaded_file;
166   char *content;
167   gsize content_size;
168   GBytes *data;
169
170   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
171                                      &content, &content_size, NULL);
172   g_assert (loaded_file);
173
174   data = g_bytes_new_take (content, content_size);
175   resource = g_resource_new_from_data (data, &error);
176   g_bytes_unref (data);
177   g_assert (resource != NULL);
178   g_assert_no_error (error);
179
180   test_resource (resource);
181
182   g_resource_unref (resource);
183 }
184
185 static void
186 test_resource_registered (void)
187 {
188   GResource *resource;
189   GError *error = NULL;
190   gboolean found, success;
191   gsize size;
192   guint32 flags;
193   GBytes *data;
194   char **children;
195   GInputStream *in;
196   char buffer[128];
197
198   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
199   g_assert (resource != NULL);
200   g_assert_no_error (error);
201
202   found = g_resources_get_info ("/test1.txt",
203                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
204                                 &size, &flags, &error);
205   g_assert (!found);
206   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
207   g_clear_error (&error);
208
209   g_resources_register (resource);
210
211   found = g_resources_get_info ("/test1.txt",
212                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
213                                 &size, &flags, &error);
214   g_assert (found);
215   g_assert_no_error (error);
216   g_assert_cmpint (size, ==, 6);
217   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
218
219   found = g_resources_get_info ("/a_prefix/test2.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_cmpint (flags, ==, 0);
226
227   found = g_resources_get_info ("/a_prefix/test2-alias.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_cmpuint (flags, ==, 0);
234
235   data = g_resources_lookup_data ("/test1.txt",
236                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
237                                   &error);
238   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
239   g_assert_no_error (error);
240   g_bytes_unref (data);
241
242   in = g_resources_open_stream ("/test1.txt",
243                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
244                                 &error);
245   g_assert (in != NULL);
246   g_assert_no_error (error);
247
248   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
249                                      &size,
250                                      NULL, &error);
251   g_assert (success);
252   g_assert_no_error (error);
253   g_assert_cmpint (size, ==, 6);
254   buffer[size] = 0;
255   g_assert_cmpstr (buffer, ==, "test1\n");
256
257   g_input_stream_close (in, NULL, &error);
258   g_assert_no_error (error);
259   g_clear_object (&in);
260
261   data = g_resources_lookup_data ("/a_prefix/test2.txt",
262                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
263                                   &error);
264   g_assert (data != NULL);
265   g_assert_no_error (error);
266   size = g_bytes_get_size (data);
267   g_assert_cmpint (size, ==, 6);
268   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
269   g_bytes_unref (data);
270
271   data = g_resources_lookup_data ("/a_prefix/test2-alias.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   children = g_resources_enumerate_children ("/not/here",
282                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
283                                              &error);
284   g_assert (children == NULL);
285   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
286   g_clear_error (&error);
287
288   children = g_resources_enumerate_children ("/a_prefix",
289                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
290                                              &error);
291   g_assert (children != NULL);
292   g_assert_no_error (error);
293   g_assert_cmpint (g_strv_length (children), ==, 2);
294   g_strfreev (children);
295
296   g_resources_unregister (resource);
297   g_resource_unref (resource);
298
299   found = g_resources_get_info ("/test1.txt",
300                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
301                                 &size, &flags, &error);
302   g_assert (!found);
303   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
304   g_clear_error (&error);
305 }
306
307 static void
308 test_resource_automatic (void)
309 {
310   GError *error = NULL;
311   gboolean found;
312   gsize size;
313   guint32 flags;
314   GBytes *data;
315
316   found = g_resources_get_info ("/auto_loaded/test1.txt",
317                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
318                                 &size, &flags, &error);
319   g_assert (found);
320   g_assert_no_error (error);
321   g_assert_cmpint (size, ==, 6);
322   g_assert_cmpint (flags, ==, 0);
323
324   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
325                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
326                                   &error);
327   g_assert (data != NULL);
328   g_assert_no_error (error);
329   size = g_bytes_get_size (data);
330   g_assert_cmpint (size, ==, 6);
331   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
332   g_bytes_unref (data);
333 }
334
335 static void
336 test_resource_manual (void)
337 {
338   GError *error = NULL;
339   gboolean found;
340   gsize size;
341   guint32 flags;
342   GBytes *data;
343
344   found = g_resources_get_info ("/manual_loaded/test1.txt",
345                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
346                                 &size, &flags, &error);
347   g_assert (found);
348   g_assert_no_error (error);
349   g_assert_cmpint (size, ==, 6);
350   g_assert_cmpuint (flags, ==, 0);
351
352   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
353                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
354                                   &error);
355   g_assert (data != NULL);
356   g_assert_no_error (error);
357   size = g_bytes_get_size (data);
358   g_assert_cmpint (size, ==, 6);
359   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
360   g_bytes_unref (data);
361 }
362
363 static void
364 test_resource_manual2 (void)
365 {
366   GResource *resource;
367   GBytes *data;
368   gsize size;
369   GError *error = NULL;
370
371   resource = _g_test2_get_resource ();
372
373   data = g_resource_lookup_data (resource,
374                                  "/manual_loaded/test1.txt",
375                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
376                                  &error);
377   g_assert (data != NULL);
378   g_assert_no_error (error);
379   size = g_bytes_get_size (data);
380   g_assert_cmpint (size, ==, 6);
381   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
382   g_bytes_unref (data);
383
384   g_resource_unref (resource);
385 }
386
387 static void
388 test_resource_module (void)
389 {
390   GIOModule *module;
391   gboolean found;
392   gsize size;
393   guint32 flags;
394   GBytes *data;
395   GError *error;
396
397   if (g_module_supported ())
398     {
399       /* For in-tree, this will find the .la file and use it to get to the .so in .libs/ */
400       module = g_io_module_new (g_test_get_filename (G_TEST_BUILT, "libresourceplugin",  NULL));
401
402       error = NULL;
403
404       found = g_resources_get_info ("/resourceplugin/test1.txt",
405                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
406                                     &size, &flags, &error);
407       g_assert (!found);
408       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
409       g_clear_error (&error);
410
411       g_type_module_use (G_TYPE_MODULE (module));
412
413       found = g_resources_get_info ("/resourceplugin/test1.txt",
414                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
415                                     &size, &flags, &error);
416       g_assert (found);
417       g_assert_no_error (error);
418       g_assert_cmpint (size, ==, 6);
419       g_assert_cmpuint (flags, ==, 0);
420
421       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
422                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
423                                       &error);
424       g_assert (data != NULL);
425       g_assert_no_error (error);
426       size = g_bytes_get_size (data);
427       g_assert_cmpint (size, ==, 6);
428       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
429       g_bytes_unref (data);
430
431       g_type_module_unuse (G_TYPE_MODULE (module));
432
433       found = g_resources_get_info ("/resourceplugin/test1.txt",
434                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
435                                     &size, &flags, &error);
436       g_assert (!found);
437       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
438       g_clear_error (&error);
439     }
440 }
441
442 static void
443 test_uri_query_info (void)
444 {
445   GResource *resource;
446   GError *error = NULL;
447   gboolean loaded_file;
448   char *content;
449   gsize content_size;
450   GBytes *data;
451   GFile *file;
452   GFileInfo *info;
453   const char *content_type;
454
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);
458
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);
464
465   g_resources_register (resource);
466
467   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
468
469   info = g_file_query_info (file, "*", 0, NULL, &error);
470   g_assert_no_error (error);
471
472   content_type = g_file_info_get_content_type (info);
473   g_assert (content_type);
474   g_assert_cmpstr (content_type, ==, "text/plain");
475
476   g_object_unref (info);
477
478   g_object_unref (file);
479
480   g_resources_unregister (resource);
481   g_resource_unref (resource);
482 }
483
484 static void
485 test_uri_file (void)
486 {
487   GResource *resource;
488   GError *error = NULL;
489   gboolean loaded_file;
490   char *content;
491   gsize content_size;
492   GBytes *data;
493   GFile *file;
494   GFileInfo *info;
495   gchar *name;
496   GFile *file2, *parent;
497   GFileEnumerator *enumerator;
498   gchar *scheme;
499   GFileAttributeInfoList *attrs;
500   GInputStream *stream;
501   gchar buf[1024];
502   gboolean ret;
503   gssize skipped;
504
505   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
506                                      &content, &content_size, NULL);
507   g_assert (loaded_file);
508
509   data = g_bytes_new_take (content, content_size);
510   resource = g_resource_new_from_data (data, &error);
511   g_bytes_unref (data);
512   g_assert (resource != NULL);
513   g_assert_no_error (error);
514
515   g_resources_register (resource);
516
517   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
518
519   g_assert (g_file_get_path (file) == NULL);
520
521   name = g_file_get_parse_name (file);
522   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
523   g_free (name);
524
525   name = g_file_get_uri (file);
526   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
527   g_free (name);
528
529   g_assert (!g_file_is_native (file));
530   g_assert (!g_file_has_uri_scheme (file, "http"));
531   g_assert (g_file_has_uri_scheme (file, "resource"));
532   scheme = g_file_get_uri_scheme (file);
533   g_assert_cmpstr (scheme, ==, "resource");
534   g_free (scheme);
535
536   file2 = g_file_dup (file);
537   g_assert (g_file_equal (file, file2));
538   g_object_unref (file2);
539
540   parent = g_file_get_parent (file);
541   enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
542   g_assert_no_error (error);
543
544   file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
545   g_assert_no_error (error);
546   g_assert (g_file_equal (file, file2));
547   g_object_unref (file2);
548
549   info = g_file_enumerator_next_file (enumerator, NULL, &error);
550   g_assert_no_error (error);
551   g_assert (info != NULL);
552   g_object_unref (info);
553
554   info = g_file_enumerator_next_file (enumerator, NULL, &error);
555   g_assert_no_error (error);
556   g_assert (info != NULL);
557   g_object_unref (info);
558
559   info = g_file_enumerator_next_file (enumerator, NULL, &error);
560   g_assert_no_error (error);
561   g_assert (info == NULL);
562
563   g_file_enumerator_close (enumerator, NULL, &error);
564   g_assert_no_error (error);
565   g_object_unref (enumerator);
566
567   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
568   g_assert (g_file_equal (file, file2));
569
570   g_assert (g_file_has_prefix (file, parent));
571
572   name = g_file_get_relative_path (parent, file);
573   g_assert_cmpstr (name, ==, "test2-alias.txt");
574   g_free (name);
575
576   g_object_unref (parent);
577
578   attrs = g_file_query_settable_attributes (file, NULL, &error);
579   g_assert_no_error (error);
580   g_file_attribute_info_list_unref (attrs);
581
582   attrs = g_file_query_writable_namespaces (file, NULL, &error);
583   g_assert_no_error (error);
584   g_file_attribute_info_list_unref (attrs);
585
586   stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
587   g_assert_no_error (error);
588   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
589   g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
590   ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
591   g_assert (ret);
592   g_assert_no_error (error);
593   skipped = g_input_stream_skip (stream, 1, NULL, &error);
594   g_assert_cmpint (skipped, ==, 1);
595   g_assert_no_error (error);
596
597   memset (buf, 0, 1024);
598   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
599   g_assert (ret);
600   g_assert_no_error (error);
601   g_assert_cmpstr (buf, ==, "st2\n");
602   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
603                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
604                                          NULL,
605                                          &error);
606   g_assert_no_error (error);
607   g_assert (info != NULL);
608   g_assert_cmpint (g_file_info_get_size (info), ==, 6);
609   g_object_unref (info);
610
611   ret = g_input_stream_close (stream, NULL, &error);
612   g_assert (ret);
613   g_assert_no_error (error);
614   g_object_unref (stream);
615
616   g_object_unref (file);
617   g_object_unref (file2);
618
619   g_resources_unregister (resource);
620   g_resource_unref (resource);
621 }
622
623 int
624 main (int   argc,
625       char *argv[])
626 {
627   g_test_init (&argc, &argv, NULL);
628
629   _g_test2_register_resource ();
630
631   g_test_add_func ("/resource/file", test_resource_file);
632   g_test_add_func ("/resource/data", test_resource_data);
633   g_test_add_func ("/resource/registered", test_resource_registered);
634   g_test_add_func ("/resource/manual", test_resource_manual);
635   g_test_add_func ("/resource/manual2", test_resource_manual2);
636 #ifdef G_HAS_CONSTRUCTORS
637   g_test_add_func ("/resource/automatic", test_resource_automatic);
638   /* This only uses automatic resources too, so it tests the constructors and destructors */
639   g_test_add_func ("/resource/module", test_resource_module);
640 #endif
641   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
642   g_test_add_func ("/resource/uri/file", test_uri_file);
643
644   return g_test_run();
645 }