build: Add missing "static" keyword where it should be used
[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 ("test.gresource", &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 ("test.gresource", &content, &content_size,
171                                      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 ("test.gresource", &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       char *dir, *path;
400
401       dir = g_get_current_dir ();
402
403       path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin",  NULL);
404       module = g_io_module_new (path);
405       g_free (path);
406       g_free (dir);
407
408       error = NULL;
409
410       found = g_resources_get_info ("/resourceplugin/test1.txt",
411                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
412                                     &size, &flags, &error);
413       g_assert (!found);
414       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
415       g_clear_error (&error);
416
417       g_type_module_use (G_TYPE_MODULE (module));
418
419       found = g_resources_get_info ("/resourceplugin/test1.txt",
420                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
421                                     &size, &flags, &error);
422       g_assert (found);
423       g_assert_no_error (error);
424       g_assert_cmpint (size, ==, 6);
425       g_assert_cmpuint (flags, ==, 0);
426
427       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
428                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
429                                       &error);
430       g_assert (data != NULL);
431       g_assert_no_error (error);
432       size = g_bytes_get_size (data);
433       g_assert_cmpint (size, ==, 6);
434       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
435       g_bytes_unref (data);
436
437       g_type_module_unuse (G_TYPE_MODULE (module));
438
439       found = g_resources_get_info ("/resourceplugin/test1.txt",
440                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
441                                     &size, &flags, &error);
442       g_assert (!found);
443       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
444       g_clear_error (&error);
445     }
446 }
447
448 static void
449 test_uri_query_info (void)
450 {
451   GResource *resource;
452   GError *error = NULL;
453   gboolean loaded_file;
454   char *content;
455   gsize content_size;
456   GBytes *data;
457   GFile *file;
458   GFileInfo *info;
459   const char *content_type;
460
461   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
462                                      NULL);
463   g_assert (loaded_file);
464
465   data = g_bytes_new_take (content, content_size);
466   resource = g_resource_new_from_data (data, &error);
467   g_bytes_unref (data);
468   g_assert (resource != NULL);
469   g_assert_no_error (error);
470
471   g_resources_register (resource);
472
473   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
474
475   info = g_file_query_info (file, "*", 0, NULL, &error);
476   g_assert_no_error (error);
477
478   content_type = g_file_info_get_content_type (info);
479   g_assert (content_type);
480   g_assert_cmpstr (content_type, ==, "text/plain");
481
482   g_object_unref (info);
483
484   g_object_unref (file);
485
486   g_resources_unregister (resource);
487   g_resource_unref (resource);
488 }
489
490 static void
491 test_uri_file (void)
492 {
493   GResource *resource;
494   GError *error = NULL;
495   gboolean loaded_file;
496   char *content;
497   gsize content_size;
498   GBytes *data;
499   GFile *file;
500   GFileInfo *info;
501   gchar *name;
502   GFile *file2, *parent;
503   GFileEnumerator *enumerator;
504   gchar *scheme;
505   GFileAttributeInfoList *attrs;
506   GInputStream *stream;
507   gchar buf[1024];
508   gboolean ret;
509   gssize skipped;
510
511   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
512                                      NULL);
513   g_assert (loaded_file);
514
515   data = g_bytes_new_take (content, content_size);
516   resource = g_resource_new_from_data (data, &error);
517   g_bytes_unref (data);
518   g_assert (resource != NULL);
519   g_assert_no_error (error);
520
521   g_resources_register (resource);
522
523   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
524
525   g_assert (g_file_get_path (file) == NULL);
526
527   name = g_file_get_parse_name (file);
528   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
529   g_free (name);
530
531   name = g_file_get_uri (file);
532   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
533   g_free (name);
534
535   g_assert (!g_file_is_native (file));
536   g_assert (!g_file_has_uri_scheme (file, "http"));
537   g_assert (g_file_has_uri_scheme (file, "resource"));
538   scheme = g_file_get_uri_scheme (file);
539   g_assert_cmpstr (scheme, ==, "resource");
540   g_free (scheme);
541
542   file2 = g_file_dup (file);
543   g_assert (g_file_equal (file, file2));
544   g_object_unref (file2);
545
546   parent = g_file_get_parent (file);
547   enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
548   g_assert_no_error (error);
549
550   file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
551   g_assert_no_error (error);
552   g_assert (g_file_equal (file, file2));
553   g_object_unref (file2);
554
555   info = g_file_enumerator_next_file (enumerator, NULL, &error);
556   g_assert_no_error (error);
557   g_assert (info != NULL);
558   g_object_unref (info);
559
560   info = g_file_enumerator_next_file (enumerator, NULL, &error);
561   g_assert_no_error (error);
562   g_assert (info != NULL);
563   g_object_unref (info);
564
565   info = g_file_enumerator_next_file (enumerator, NULL, &error);
566   g_assert_no_error (error);
567   g_assert (info == NULL);
568
569   g_file_enumerator_close (enumerator, NULL, &error);
570   g_assert_no_error (error);
571   g_object_unref (enumerator);
572
573   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
574   g_assert (g_file_equal (file, file2));
575
576   g_assert (g_file_has_prefix (file, parent));
577
578   name = g_file_get_relative_path (parent, file);
579   g_assert_cmpstr (name, ==, "test2-alias.txt");
580   g_free (name);
581
582   g_object_unref (parent);
583
584   attrs = g_file_query_settable_attributes (file, NULL, &error);
585   g_assert_no_error (error);
586   g_file_attribute_info_list_unref (attrs);
587
588   attrs = g_file_query_writable_namespaces (file, NULL, &error);
589   g_assert_no_error (error);
590   g_file_attribute_info_list_unref (attrs);
591
592   stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
593   g_assert_no_error (error);
594   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
595   g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
596   ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
597   g_assert (ret);
598   g_assert_no_error (error);
599   skipped = g_input_stream_skip (stream, 1, NULL, &error);
600   g_assert_cmpint (skipped, ==, 1);
601   g_assert_no_error (error);
602
603   memset (buf, 0, 1024);
604   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
605   g_assert (ret);
606   g_assert_no_error (error);
607   g_assert_cmpstr (buf, ==, "st2\n");
608   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
609                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
610                                          NULL,
611                                          &error);
612   g_assert_no_error (error);
613   g_assert (info != NULL);
614   g_assert_cmpint (g_file_info_get_size (info), ==, 6);
615   g_object_unref (info);
616
617   ret = g_input_stream_close (stream, NULL, &error);
618   g_assert (ret);
619   g_assert_no_error (error);
620   g_object_unref (stream);
621
622   g_object_unref (file);
623   g_object_unref (file2);
624
625   g_resources_unregister (resource);
626   g_resource_unref (resource);
627 }
628
629 int
630 main (int   argc,
631       char *argv[])
632 {
633   g_test_init (&argc, &argv, NULL);
634
635   _g_test2_register_resource ();
636
637   g_test_add_func ("/resource/file", test_resource_file);
638   g_test_add_func ("/resource/data", test_resource_data);
639   g_test_add_func ("/resource/registered", test_resource_registered);
640   g_test_add_func ("/resource/manual", test_resource_manual);
641   g_test_add_func ("/resource/manual2", test_resource_manual2);
642 #ifdef G_HAS_CONSTRUCTORS
643   g_test_add_func ("/resource/automatic", test_resource_automatic);
644   /* This only uses automatic resources too, so it tests the constructors and destructors */
645   g_test_add_func ("/resource/module", test_resource_module);
646 #endif
647   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
648   g_test_add_func ("/resource/uri/file", test_uri_file);
649
650   return g_test_run();
651 }