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