[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <string.h>
20 #include <gio/gio.h>
21 #include "gconstructor.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_cmpint (size, ==, 6);
51   g_assert_cmpuint (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_cmpint (size, ==, 6);
60   g_assert_cmpuint (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_cmpint (size, ==, 6);
69   g_assert_cmpuint (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_cmpint (size, ==, 6);
92   buffer[size] = 0;
93   g_assert_cmpstr (buffer, ==, "test1\n");
94
95   g_input_stream_close (in, NULL, &error);
96   g_assert_no_error (error);
97   g_clear_object (&in);
98
99   data = g_resource_lookup_data (resource,
100                                  "/a_prefix/test2.txt",
101                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
102                                  &error);
103   g_assert (data != NULL);
104   g_assert_no_error (error);
105   size = g_bytes_get_size (data);
106   g_assert_cmpint (size, ==, 6);
107   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
108   g_bytes_unref (data);
109
110   data = g_resource_lookup_data (resource,
111                                  "/a_prefix/test2-alias.txt",
112                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
113                                  &error);
114   g_assert (data != NULL);
115   g_assert_no_error (error);
116   size = g_bytes_get_size (data);
117   g_assert_cmpint (size, ==, 6);
118   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
119   g_bytes_unref (data);
120
121   children = g_resource_enumerate_children  (resource,
122                                              "/not/here",
123                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
124                                              &error);
125   g_assert (children == NULL);
126   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
127   g_clear_error (&error);
128
129   children = g_resource_enumerate_children  (resource,
130                                              "/a_prefix",
131                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
132                                              &error);
133   g_assert (children != NULL);
134   g_assert_no_error (error);
135   g_assert_cmpint (g_strv_length (children), ==, 2);
136   g_strfreev (children);
137 }
138
139 static void
140 test_resource_file (void)
141 {
142   GResource *resource;
143   GError *error = NULL;
144
145   resource = g_resource_load ("not-there", &error);
146   g_assert (resource == NULL);
147   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
148   g_clear_error (&error);
149
150   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
151   g_assert (resource != NULL);
152   g_assert_no_error (error);
153
154   test_resource (resource);
155   g_resource_unref (resource);
156 }
157
158 static void
159 test_resource_data (void)
160 {
161   GResource *resource;
162   GError *error = NULL;
163   gboolean loaded_file;
164   char *content;
165   gsize content_size;
166   GBytes *data;
167
168   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
169                                      &content, &content_size, NULL);
170   g_assert (loaded_file);
171
172   data = g_bytes_new_take (content, content_size);
173   resource = g_resource_new_from_data (data, &error);
174   g_bytes_unref (data);
175   g_assert (resource != NULL);
176   g_assert_no_error (error);
177
178   test_resource (resource);
179
180   g_resource_unref (resource);
181 }
182
183 static void
184 test_resource_registered (void)
185 {
186   GResource *resource;
187   GError *error = NULL;
188   gboolean found, success;
189   gsize size;
190   guint32 flags;
191   GBytes *data;
192   char **children;
193   GInputStream *in;
194   char buffer[128];
195
196   resource = g_resource_load (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL), &error);
197   g_assert (resource != NULL);
198   g_assert_no_error (error);
199
200   found = g_resources_get_info ("/test1.txt",
201                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
202                                 &size, &flags, &error);
203   g_assert (!found);
204   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
205   g_clear_error (&error);
206
207   g_resources_register (resource);
208
209   found = g_resources_get_info ("/test1.txt",
210                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
211                                 &size, &flags, &error);
212   g_assert (found);
213   g_assert_no_error (error);
214   g_assert_cmpint (size, ==, 6);
215   g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));
216
217   found = g_resources_get_info ("/a_prefix/test2.txt",
218                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
219                                 &size, &flags, &error);
220   g_assert (found);
221   g_assert_no_error (error);
222   g_assert_cmpint (size, ==, 6);
223   g_assert_cmpint (flags, ==, 0);
224
225   found = g_resources_get_info ("/a_prefix/test2-alias.txt",
226                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
227                                 &size, &flags, &error);
228   g_assert (found);
229   g_assert_no_error (error);
230   g_assert_cmpint (size, ==, 6);
231   g_assert_cmpuint (flags, ==, 0);
232
233   data = g_resources_lookup_data ("/test1.txt",
234                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
235                                   &error);
236   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
237   g_assert_no_error (error);
238   g_bytes_unref (data);
239
240   in = g_resources_open_stream ("/test1.txt",
241                                 G_RESOURCE_LOOKUP_FLAGS_NONE,
242                                 &error);
243   g_assert (in != NULL);
244   g_assert_no_error (error);
245
246   success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
247                                      &size,
248                                      NULL, &error);
249   g_assert (success);
250   g_assert_no_error (error);
251   g_assert_cmpint (size, ==, 6);
252   buffer[size] = 0;
253   g_assert_cmpstr (buffer, ==, "test1\n");
254
255   g_input_stream_close (in, NULL, &error);
256   g_assert_no_error (error);
257   g_clear_object (&in);
258
259   data = g_resources_lookup_data ("/a_prefix/test2.txt",
260                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
261                                   &error);
262   g_assert (data != NULL);
263   g_assert_no_error (error);
264   size = g_bytes_get_size (data);
265   g_assert_cmpint (size, ==, 6);
266   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
267   g_bytes_unref (data);
268
269   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
270                                   G_RESOURCE_LOOKUP_FLAGS_NONE,
271                                   &error);
272   g_assert (data != NULL);
273   g_assert_no_error (error);
274   size = g_bytes_get_size (data);
275   g_assert_cmpint (size, ==, 6);
276   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
277   g_bytes_unref (data);
278
279   children = g_resources_enumerate_children ("/not/here",
280                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
281                                              &error);
282   g_assert (children == NULL);
283   g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
284   g_clear_error (&error);
285
286   children = g_resources_enumerate_children ("/a_prefix",
287                                              G_RESOURCE_LOOKUP_FLAGS_NONE,
288                                              &error);
289   g_assert (children != NULL);
290   g_assert_no_error (error);
291   g_assert_cmpint (g_strv_length (children), ==, 2);
292   g_strfreev (children);
293
294   g_resources_unregister (resource);
295   g_resource_unref (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_manual2 (void)
363 {
364   GResource *resource;
365   GBytes *data;
366   gsize size;
367   GError *error = NULL;
368
369   resource = _g_test2_get_resource ();
370
371   data = g_resource_lookup_data (resource,
372                                  "/manual_loaded/test1.txt",
373                                  G_RESOURCE_LOOKUP_FLAGS_NONE,
374                                  &error);
375   g_assert (data != NULL);
376   g_assert_no_error (error);
377   size = g_bytes_get_size (data);
378   g_assert_cmpint (size, ==, 6);
379   g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
380   g_bytes_unref (data);
381
382   g_resource_unref (resource);
383 }
384
385 static void
386 test_resource_module (void)
387 {
388   GIOModule *module;
389   gboolean found;
390   gsize size;
391   guint32 flags;
392   GBytes *data;
393   GError *error;
394
395   if (g_module_supported ())
396     {
397       /* For in-tree, this will find the .la file and use it to get to the .so in .libs/ */
398       module = g_io_module_new (g_test_get_filename (G_TEST_BUILT, "libresourceplugin",  NULL));
399
400       error = NULL;
401
402       found = g_resources_get_info ("/resourceplugin/test1.txt",
403                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
404                                     &size, &flags, &error);
405       g_assert (!found);
406       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
407       g_clear_error (&error);
408
409       g_type_module_use (G_TYPE_MODULE (module));
410
411       found = g_resources_get_info ("/resourceplugin/test1.txt",
412                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
413                                     &size, &flags, &error);
414       g_assert (found);
415       g_assert_no_error (error);
416       g_assert_cmpint (size, ==, 6);
417       g_assert_cmpuint (flags, ==, 0);
418
419       data = g_resources_lookup_data ("/resourceplugin/test1.txt",
420                                       G_RESOURCE_LOOKUP_FLAGS_NONE,
421                                       &error);
422       g_assert (data != NULL);
423       g_assert_no_error (error);
424       size = g_bytes_get_size (data);
425       g_assert_cmpint (size, ==, 6);
426       g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
427       g_bytes_unref (data);
428
429       g_type_module_unuse (G_TYPE_MODULE (module));
430
431       found = g_resources_get_info ("/resourceplugin/test1.txt",
432                                     G_RESOURCE_LOOKUP_FLAGS_NONE,
433                                     &size, &flags, &error);
434       g_assert (!found);
435       g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
436       g_clear_error (&error);
437     }
438 }
439
440 static void
441 test_uri_query_info (void)
442 {
443   GResource *resource;
444   GError *error = NULL;
445   gboolean loaded_file;
446   char *content;
447   gsize content_size;
448   GBytes *data;
449   GFile *file;
450   GFileInfo *info;
451   const char *content_type, *mime_type;
452
453   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
454                                      &content, &content_size, NULL);
455   g_assert (loaded_file);
456
457   data = g_bytes_new_take (content, content_size);
458   resource = g_resource_new_from_data (data, &error);
459   g_bytes_unref (data);
460   g_assert (resource != NULL);
461   g_assert_no_error (error);
462
463   g_resources_register (resource);
464
465   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
466
467   info = g_file_query_info (file, "*", 0, NULL, &error);
468   g_assert_no_error (error);
469
470   content_type = g_file_info_get_content_type (info);
471   g_assert (content_type);
472   mime_type = g_content_type_get_mime_type (content_type);
473   g_assert (mime_type);
474   g_assert_cmpstr (mime_type, ==, "text/plain");
475
476   g_object_unref (info);
477
478   g_assert_cmpuint  (g_file_hash (file), !=, 0);
479
480   g_object_unref (file);
481
482   g_resources_unregister (resource);
483   g_resource_unref (resource);
484 }
485
486 static void
487 test_uri_file (void)
488 {
489   GResource *resource;
490   GError *error = NULL;
491   gboolean loaded_file;
492   char *content;
493   gsize content_size;
494   GBytes *data;
495   GFile *file;
496   GFileInfo *info;
497   gchar *name;
498   GFile *file2, *parent;
499   GFileEnumerator *enumerator;
500   gchar *scheme;
501   GFileAttributeInfoList *attrs;
502   GInputStream *stream;
503   gchar buf[1024];
504   gboolean ret;
505   gssize skipped;
506
507   loaded_file = g_file_get_contents (g_test_get_filename (G_TEST_BUILT, "test.gresource", NULL),
508                                      &content, &content_size, NULL);
509   g_assert (loaded_file);
510
511   data = g_bytes_new_take (content, content_size);
512   resource = g_resource_new_from_data (data, &error);
513   g_bytes_unref (data);
514   g_assert (resource != NULL);
515   g_assert_no_error (error);
516
517   g_resources_register (resource);
518
519   file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");
520
521   g_assert (g_file_get_path (file) == NULL);
522
523   name = g_file_get_parse_name (file);
524   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
525   g_free (name);
526
527   name = g_file_get_uri (file);
528   g_assert_cmpstr (name, ==, "resource:///a_prefix/test2-alias.txt");
529   g_free (name);
530
531   g_assert (!g_file_is_native (file));
532   g_assert (!g_file_has_uri_scheme (file, "http"));
533   g_assert (g_file_has_uri_scheme (file, "resource"));
534   scheme = g_file_get_uri_scheme (file);
535   g_assert_cmpstr (scheme, ==, "resource");
536   g_free (scheme);
537
538   file2 = g_file_dup (file);
539   g_assert (g_file_equal (file, file2));
540   g_object_unref (file2);
541
542   parent = g_file_get_parent (file);
543   enumerator = g_file_enumerate_children (parent, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
544   g_assert_no_error (error);
545
546   file2 = g_file_get_child_for_display_name (parent, "test2-alias.txt", &error);
547   g_assert_no_error (error);
548   g_assert (g_file_equal (file, file2));
549   g_object_unref (file2);
550
551   info = g_file_enumerator_next_file (enumerator, NULL, &error);
552   g_assert_no_error (error);
553   g_assert (info != NULL);
554   g_object_unref (info);
555
556   info = g_file_enumerator_next_file (enumerator, NULL, &error);
557   g_assert_no_error (error);
558   g_assert (info != NULL);
559   g_object_unref (info);
560
561   info = g_file_enumerator_next_file (enumerator, NULL, &error);
562   g_assert_no_error (error);
563   g_assert (info == NULL);
564
565   g_file_enumerator_close (enumerator, NULL, &error);
566   g_assert_no_error (error);
567   g_object_unref (enumerator);
568
569   file2 = g_file_new_for_uri ("resource://" "a_prefix/../a_prefix//test2-alias.txt");
570   g_assert (g_file_equal (file, file2));
571
572   g_assert (g_file_has_prefix (file, parent));
573
574   name = g_file_get_relative_path (parent, file);
575   g_assert_cmpstr (name, ==, "test2-alias.txt");
576   g_free (name);
577
578   g_object_unref (parent);
579
580   attrs = g_file_query_settable_attributes (file, NULL, &error);
581   g_assert_no_error (error);
582   g_file_attribute_info_list_unref (attrs);
583
584   attrs = g_file_query_writable_namespaces (file, NULL, &error);
585   g_assert_no_error (error);
586   g_file_attribute_info_list_unref (attrs);
587
588   stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
589   g_assert_no_error (error);
590   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 0);
591   g_assert (g_seekable_can_seek (G_SEEKABLE (G_SEEKABLE (stream))));
592   ret = g_seekable_seek (G_SEEKABLE (stream), 1, G_SEEK_SET, NULL, &error);
593   g_assert (ret);
594   g_assert_no_error (error);
595   skipped = g_input_stream_skip (stream, 1, NULL, &error);
596   g_assert_cmpint (skipped, ==, 1);
597   g_assert_no_error (error);
598
599   memset (buf, 0, 1024);
600   ret = g_input_stream_read_all (stream, &buf, 1024, NULL, NULL, &error);
601   g_assert (ret);
602   g_assert_no_error (error);
603   g_assert_cmpstr (buf, ==, "st2\n");
604   info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (stream),
605                                          G_FILE_ATTRIBUTE_STANDARD_SIZE,
606                                          NULL,
607                                          &error);
608   g_assert_no_error (error);
609   g_assert (info != NULL);
610   g_assert_cmpint (g_file_info_get_size (info), ==, 6);
611   g_object_unref (info);
612
613   ret = g_input_stream_close (stream, NULL, &error);
614   g_assert (ret);
615   g_assert_no_error (error);
616   g_object_unref (stream);
617
618   g_object_unref (file);
619   g_object_unref (file2);
620
621   g_resources_unregister (resource);
622   g_resource_unref (resource);
623 }
624
625 int
626 main (int   argc,
627       char *argv[])
628 {
629   g_test_init (&argc, &argv, NULL);
630
631   _g_test2_register_resource ();
632
633   g_test_add_func ("/resource/file", test_resource_file);
634   g_test_add_func ("/resource/data", test_resource_data);
635   g_test_add_func ("/resource/registered", test_resource_registered);
636   g_test_add_func ("/resource/manual", test_resource_manual);
637   g_test_add_func ("/resource/manual2", test_resource_manual2);
638 #ifdef G_HAS_CONSTRUCTORS
639   g_test_add_func ("/resource/automatic", test_resource_automatic);
640   /* This only uses automatic resources too, so it tests the constructors and destructors */
641   g_test_add_func ("/resource/module", test_resource_module);
642 #endif
643   g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
644   g_test_add_func ("/resource/uri/file", test_uri_file);
645
646   return g_test_run();
647 }