Support resource:/// uris
[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 <gio/gio.h>
22 #include "test_resources2.h"
23
24 static void
25 test_resource (GResource *resource)
26 {
27   GError *error = NULL;
28   gboolean found, success;
29   gsize size;
30   guint32 flags;
31   GBytes *data;
32   char **children;
33   GInputStream *in;
34   char buffer[128];
35
36   found = g_resource_get_info (resource,
37                                "/not/there",
38                                G_RESOURCE_LOOKUP_FLAGS_NONE,
39                                &size, &flags, &error);
40   g_assert (!found);
41   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
42   g_clear_error (&error);
43
44   found = g_resource_get_info (resource,
45                                "/test1.txt",
46                                G_RESOURCE_LOOKUP_FLAGS_NONE,
47                                &size, &flags, &error);
48   g_assert (found);
49   g_assert_no_error (error);
50   g_assert (size == 6);
51   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
52
53   found = g_resource_get_info (resource,
54                                "/a_prefix/test2.txt",
55                                G_RESOURCE_LOOKUP_FLAGS_NONE,
56                                &size, &flags, &error);
57   g_assert (found);
58   g_assert_no_error (error);
59   g_assert (size == 6);
60   g_assert (flags == 0);
61
62   found = g_resource_get_info (resource,
63                                "/a_prefix/test2-alias.txt",
64                                G_RESOURCE_LOOKUP_FLAGS_NONE,
65                                &size, &flags, &error);
66   g_assert (found);
67   g_assert_no_error (error);
68   g_assert (size == 6);
69   g_assert (flags == 0);
70
71   data = g_resource_lookup_data (resource,
72                                  "/test1.txt",
73                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
74                                  &error);
75   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
76   g_assert_no_error (error);
77   g_bytes_unref (data);
78
79   in = g_resource_open_stream (resource,
80                                "/test1.txt",
81                                G_RESOURCE_LOOKUP_FLAGS_NONE,
82                                &error);
83   g_assert (in != NULL);
84   g_assert_no_error (error);
85
86   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
87                                      &size,
88                                      NULL, &error);
89   g_assert (success);
90   g_assert_no_error (error);
91   g_assert (size == 6);
92   buffer[size] = 0;
93   g_assert_cmpstr (buffer, ==, "test1\n");
94
95   g_input_stream_close (in, NULL, NULL);
96
97   data = g_resource_lookup_data (resource,
98                                  "/a_prefix/test2.txt",
99                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
100                                  &error);
101   g_assert (data != NULL);
102   g_assert_no_error (error);
103   size = g_bytes_get_size (data);
104   g_assert (size == 6);
105   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
106   g_bytes_unref (data);
107
108   data = g_resource_lookup_data (resource,
109                                  "/a_prefix/test2-alias.txt",
110                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
111                                  &error);
112   g_assert (data != NULL);
113   g_assert_no_error (error);
114   size = g_bytes_get_size (data);
115   g_assert (size == 6);
116   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
117   g_bytes_unref (data);
118
119   children = g_resource_enumerate_children  (resource,
120                                              "/not/here",
121                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
122                                              &error);
123   g_assert (children == NULL);
124   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
125   g_clear_error (&error);
126
127   children = g_resource_enumerate_children  (resource,
128                                              "/a_prefix",
129                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
130                                              &error);
131   g_assert (children != NULL);
132   g_assert_no_error (error);
133   g_assert (g_strv_length (children) == 2);
134   g_strfreev (children);
135 }
136
137 static void
138 test_resource_file (void)
139 {
140   GResource *resource;
141   GError *error = NULL;
142
143   resource = g_resource_load ("not-there", &error);
144   g_assert (resource == NULL);
145   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
146   g_clear_error (&error);
147
148   resource = g_resource_load ("test.gresource", &error);
149   g_assert (resource != NULL);
150   g_assert_no_error (error);
151
152   test_resource (resource);
153   g_resource_unref (resource);
154 }
155
156 static void
157 test_resource_data (void)
158 {
159   GResource *resource;
160   GError *error = NULL;
161   gboolean loaded_file;
162   char *content;
163   gsize content_size;
164   GBytes *data;
165
166   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
167                                      NULL);
168   g_assert (loaded_file);
169
170   data = g_bytes_new_take (content, content_size);
171   resource = g_resource_new_from_data (data, &error);
172   g_assert (resource != NULL);
173   g_assert_no_error (error);
174
175   test_resource (resource);
176
177   g_resource_unref (resource);
178 }
179
180 static void
181 test_resource_registred (void)
182 {
183   GResource *resource;
184   GError *error = NULL;
185   gboolean found, success;
186   gsize size;
187   guint32 flags;
188   GBytes *data;
189   char **children;
190   GInputStream *in;
191   char buffer[128];
192
193   resource = g_resource_load ("test.gresource", &error);
194   g_assert (resource != NULL);
195   g_assert_no_error (error);
196
197   found = g_resources_get_info ("/test1.txt",
198                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
199                                 &size, &flags, &error);
200   g_assert (!found);
201   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
202   g_clear_error (&error);
203
204   g_resources_register (resource);
205
206   found = g_resources_get_info ("/test1.txt",
207                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
208                                 &size, &flags, &error);
209   g_assert (found);
210   g_assert_no_error (error);
211   g_assert (size == 6);
212   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
213
214   found = g_resources_get_info ("/a_prefix/test2.txt",
215                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
216                                 &size, &flags, &error);
217   g_assert (found);
218   g_assert_no_error (error);
219   g_assert (size == 6);
220   g_assert (flags == 0);
221
222   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
223                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
224                                 &size, &flags, &error);
225   g_assert (found);
226   g_assert_no_error (error);
227   g_assert (size == 6);
228   g_assert (flags == 0);
229
230   data = g_resources_lookup_data ("/test1.txt",
231                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
232                                   &error);
233   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
234   g_assert_no_error (error);
235   g_bytes_unref (data);
236
237   in = g_resources_open_stream ("/test1.txt",
238                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
239                                 &error);
240   g_assert (in != NULL);
241   g_assert_no_error (error);
242
243   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
244                                      &size,
245                                      NULL, &error);
246   g_assert (success);
247   g_assert_no_error (error);
248   g_assert (size == 6);
249   buffer[size] = 0;
250   g_assert_cmpstr (buffer, ==, "test1\n");
251
252   g_input_stream_close (in, NULL, NULL);
253
254
255   data = g_resources_lookup_data ("/a_prefix/test2.txt",
256                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
257                                   &error);
258   g_assert (data != NULL);
259   g_assert_no_error (error);
260   size = g_bytes_get_size (data);
261   g_assert (size == 6);
262   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
263   g_bytes_unref (data);
264
265   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
266                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
267                                   &error);
268   g_assert (data != NULL);
269   g_assert_no_error (error);
270   size = g_bytes_get_size (data);
271   g_assert (size == 6);
272   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
273   g_bytes_unref (data);
274
275   children = g_resources_enumerate_children ("/not/here",
276                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
277                                              &error);
278   g_assert (children == NULL);
279   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
280   g_clear_error (&error);
281
282   children = g_resources_enumerate_children ("/a_prefix",
283                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
284                                              &error);
285   g_assert (children != NULL);
286   g_assert_no_error (error);
287   g_assert (g_strv_length (children) == 2);
288
289   g_resources_unregister (resource);
290
291   found = g_resources_get_info ("/test1.txt",
292                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
293                                 &size, &flags, &error);
294   g_assert (!found);
295   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
296   g_clear_error (&error);
297 }
298
299 static void
300 test_resource_automatic (void)
301 {
302   GError *error = NULL;
303   gboolean found;
304   gsize size;
305   guint32 flags;
306   GBytes *data;
307
308   found = g_resources_get_info ("/auto_loaded/test1.txt",
309                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
310                                 &size, &flags, &error);
311   g_assert (found);
312   g_assert_no_error (error);
313   g_assert (size == 6);
314   g_assert (flags == 0);
315
316   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
317                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
318                                   &error);
319   g_assert (data != NULL);
320   g_assert_no_error (error);
321   size = g_bytes_get_size (data);
322   g_assert (size == 6);
323   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
324   g_bytes_unref (data);
325 }
326
327 static void
328 test_resource_manual (void)
329 {
330   GError *error = NULL;
331   gboolean found;
332   gsize size;
333   guint32 flags;
334   GBytes *data;
335
336   found = g_resources_get_info ("/manual_loaded/test1.txt",
337                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
338                                 &size, &flags, &error);
339   g_assert (found);
340   g_assert_no_error (error);
341   g_assert (size == 6);
342   g_assert (flags == 0);
343
344   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
345                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
346                                   &error);
347   g_assert (data != NULL);
348   g_assert_no_error (error);
349   size = g_bytes_get_size (data);
350   g_assert (size == 6);
351   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
352   g_bytes_unref (data);
353 }
354
355 static void
356 test_resource_module (void)
357 {
358   GIOModule *module;
359   gboolean found;
360   gsize size;
361   guint32 flags;
362   GBytes *data;
363   GError *error;
364
365   if (g_module_supported ())
366     {
367       char *dir, *path;
368
369       dir = g_get_current_dir ();
370
371       path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin",  NULL);
372       module = g_io_module_new (path);
373       g_free (path);
374
375       error = NULL;
376
377       found = g_resources_get_info ("/resourceplugin/test1.txt",
378                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
379                                     &size, &flags, &error);
380       g_assert (!found);
381       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
382       g_clear_error (&error);
383
384       g_type_module_use (G_TYPE_MODULE (module));
385
386       found = g_resources_get_info ("/resourceplugin/test1.txt",
387                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
388                                     &size, &flags, &error);
389       g_assert (found);
390       g_assert_no_error (error);
391       g_assert (size == 6);
392       g_assert (flags == 0);
393
394       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
395                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
396                                       &error);
397       g_assert (data != NULL);
398       g_assert_no_error (error);
399       size = g_bytes_get_size (data);
400       g_assert (size == 6);
401       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
402       g_bytes_unref (data);
403
404       g_type_module_unuse (G_TYPE_MODULE (module));
405
406       found = g_resources_get_info ("/resourceplugin/test1.txt",
407                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
408                                     &size, &flags, &error);
409       g_assert (!found);
410       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
411       g_clear_error (&error);
412     }
413 }
414
415 static void
416 test_uri_query_info (void)
417 {
418   GResource *resource;
419   GError *error = NULL;
420   gboolean loaded_file;
421   char *content;
422   gsize content_size;
423   GBytes *data;
424   GFile *file;
425   GFileInfo *info;
426   const char *content_type;
427
428   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
429                                      NULL);
430   g_assert (loaded_file);
431
432   data = g_bytes_new_take (content, content_size);
433   resource = g_resource_new_from_data (data, &error);
434   g_assert (resource != NULL);
435   g_assert_no_error (error);
436
437   g_resources_register (resource);
438
439   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
440
441   info = g_file_query_info (file, "*", 0, NULL, &error);
442   g_assert_no_error (error);
443   g_object_unref (file);
444
445   content_type = g_file_info_get_content_type (info);
446   g_assert (content_type);
447   g_assert_cmpstr (content_type, ==, "text/plain");
448
449   g_object_unref (info);
450
451   g_resources_unregister (resource);
452   g_resource_unref (resource);
453 }
454
455 int
456 main (int   argc,
457       char *argv[])
458 {
459   g_type_init ();
460   g_test_init (&argc, &argv, NULL);
461
462   _g_test2_register_resource ();
463
464   g_test_add_func ("/resource/file", test_resource_file);
465   g_test_add_func ("/resource/data", test_resource_data);
466   g_test_add_func ("/resource/registred", test_resource_registred);
467   g_test_add_func ("/resource/manual", test_resource_manual);
468 #ifdef G_HAS_CONSTRUCTORS
469   g_test_add_func ("/resource/automatic", test_resource_automatic);
470   /* This only uses automatic resources too, so it tests the constructors and destructors */
471   g_test_add_func ("/resource/module", test_resource_module);
472 #endif
473   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
474
475   return g_test_run();
476 }