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, &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
297   found = g_resources_get_info ("/test1.txt",
298                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
299                                 &size, &flags, &error);
300   g_assert (!found);
301   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
302   g_clear_error (&error);
303 }
304
305 static void
306 test_resource_automatic (void)
307 {
308   GError *error = NULL;
309   gboolean found;
310   gsize size;
311   guint32 flags;
312   GBytes *data;
313
314   found = g_resources_get_info ("/auto_loaded/test1.txt",
315                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
316                                 &size, &flags, &error);
317   g_assert (found);
318   g_assert_no_error (error);
319   g_assert_cmpint (size, ==, 6);
320   g_assert_cmpint (flags, ==, 0);
321
322   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
323                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
324                                   &error);
325   g_assert (data != NULL);
326   g_assert_no_error (error);
327   size = g_bytes_get_size (data);
328   g_assert_cmpint (size, ==, 6);
329   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
330   g_bytes_unref (data);
331 }
332
333 static void
334 test_resource_manual (void)
335 {
336   GError *error = NULL;
337   gboolean found;
338   gsize size;
339   guint32 flags;
340   GBytes *data;
341
342   found = g_resources_get_info ("/manual_loaded/test1.txt",
343                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
344                                 &size, &flags, &error);
345   g_assert (found);
346   g_assert_no_error (error);
347   g_assert_cmpint (size, ==, 6);
348   g_assert_cmpuint (flags, ==, 0);
349
350   data = g_resources_lookup_data ("/manual_loaded/test1.txt",
351                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
352                                   &error);
353   g_assert (data != NULL);
354   g_assert_no_error (error);
355   size = g_bytes_get_size (data);
356   g_assert_cmpint (size, ==, 6);
357   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
358   g_bytes_unref (data);
359 }
360
361 static void
362 test_resource_module (void)
363 {
364   GIOModule *module;
365   gboolean found;
366   gsize size;
367   guint32 flags;
368   GBytes *data;
369   GError *error;
370
371   if (g_module_supported ())
372     {
373       char *dir, *path;
374
375       dir = g_get_current_dir ();
376
377       path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin",  NULL);
378       module = g_io_module_new (path);
379       g_free (path);
380       g_free (dir);
381
382       error = NULL;
383
384       found = g_resources_get_info ("/resourceplugin/test1.txt",
385                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
386                                     &size, &flags, &error);
387       g_assert (!found);
388       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
389       g_clear_error (&error);
390
391       g_type_module_use (G_TYPE_MODULE (module));
392
393       found = g_resources_get_info ("/resourceplugin/test1.txt",
394                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
395                                     &size, &flags, &error);
396       g_assert (found);
397       g_assert_no_error (error);
398       g_assert_cmpint (size, ==, 6);
399       g_assert_cmpuint (flags, ==, 0);
400
401       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
402                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
403                                       &error);
404       g_assert (data != NULL);
405       g_assert_no_error (error);
406       size = g_bytes_get_size (data);
407       g_assert_cmpint (size, ==, 6);
408       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
409       g_bytes_unref (data);
410
411       g_type_module_unuse (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_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
418       g_clear_error (&error);
419     }
420 }
421
422 static void
423 test_uri_query_info (void)
424 {
425   GResource *resource;
426   GError *error = NULL;
427   gboolean loaded_file;
428   char *content;
429   gsize content_size;
430   GBytes *data;
431   GFile *file;
432   GFileInfo *info;
433   const char *content_type;
434
435   loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
436                                      NULL);
437   g_assert (loaded_file);
438
439   data = g_bytes_new_take (content, content_size);
440   resource = g_resource_new_from_data (data, &error);
441   g_bytes_unref (data);
442   g_assert (resource != NULL);
443   g_assert_no_error (error);
444
445   g_resources_register (resource);
446
447   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
448
449   info = g_file_query_info (file, "*", 0, NULL, &error);
450   g_assert_no_error (error);
451   g_object_unref (file);
452
453   content_type = g_file_info_get_content_type (info);
454   g_assert (content_type);
455   g_assert_cmpstr (content_type, ==, "text/plain");
456
457   g_object_unref (info);
458
459   g_resources_unregister (resource);
460   g_resource_unref (resource);
461 }
462
463 int
464 main (int   argc,
465       char *argv[])
466 {
467   g_type_init ();
468   g_test_init (&argc, &argv, NULL);
469
470   _g_test2_register_resource ();
471
472   g_test_add_func ("/resource/file", test_resource_file);
473   g_test_add_func ("/resource/data", test_resource_data);
474   g_test_add_func ("/resource/registred", test_resource_registred);
475   g_test_add_func ("/resource/manual", test_resource_manual);
476 #ifdef G_HAS_CONSTRUCTORS
477   g_test_add_func ("/resource/automatic", test_resource_automatic);
478   /* This only uses automatic resources too, so it tests the constructors and destructors */
479   g_test_add_func ("/resource/module", test_resource_module);
480 #endif
481   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
482
483   return g_test_run();
484 }