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