resources.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GLib testing framework examples and tests
 *
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gio/gio.h>
22
#include "gconstructor.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "test_resources2.h"

static void
test_resource (GResource *resource)
{
  GError *error = NULL;
  gboolean found, success;
  gsize size;
  guint32 flags;
  GBytes *data;
  char **children;
  GInputStream *in;
  char buffer[128];

  found = g_resource_get_info (resource,
			       "/not/there",
			       G_RESOURCE_LOOKUP_FLAGS_NONE,
			       &size, &flags, &error);
  g_assert (!found);
  g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
  g_clear_error (&error);

  found = g_resource_get_info (resource,
			       "/test1.txt",
			       G_RESOURCE_LOOKUP_FLAGS_NONE,
			       &size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
51 52
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpuint (flags, ==, G_RESOURCE_FLAGS_COMPRESSED);
53 54 55 56 57 58 59

  found = g_resource_get_info (resource,
			       "/a_prefix/test2.txt",
			       G_RESOURCE_LOOKUP_FLAGS_NONE,
			       &size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
60 61
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpuint (flags, ==, 0);
62 63 64 65 66 67 68

  found = g_resource_get_info (resource,
			       "/a_prefix/test2-alias.txt",
			       G_RESOURCE_LOOKUP_FLAGS_NONE,
			       &size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
69 70
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpuint (flags, ==, 0);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  data = g_resource_lookup_data (resource,
				 "/test1.txt",
				 G_RESOURCE_LOOKUP_FLAGS_NONE,
				 &error);
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
  g_assert_no_error (error);
  g_bytes_unref (data);

  in = g_resource_open_stream (resource,
			       "/test1.txt",
			       G_RESOURCE_LOOKUP_FLAGS_NONE,
			       &error);
  g_assert (in != NULL);
  g_assert_no_error (error);

  success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
				     &size,
				     NULL, &error);
  g_assert (success);
  g_assert_no_error (error);
92
  g_assert_cmpint (size, ==, 6);
93 94 95
  buffer[size] = 0;
  g_assert_cmpstr (buffer, ==, "test1\n");

96 97 98
  g_input_stream_close (in, NULL, &error);
  g_assert_no_error (error);
  g_clear_object (&in);
99 100 101 102 103 104 105 106

  data = g_resource_lookup_data (resource,
				 "/a_prefix/test2.txt",
				 G_RESOURCE_LOOKUP_FLAGS_NONE,
				 &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
107
  g_assert_cmpint (size, ==, 6);
108 109 110 111 112 113 114 115 116 117
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
  g_bytes_unref (data);

  data = g_resource_lookup_data (resource,
				 "/a_prefix/test2-alias.txt",
				 G_RESOURCE_LOOKUP_FLAGS_NONE,
				 &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
118
  g_assert_cmpint (size, ==, 6);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
  g_bytes_unref (data);

  children = g_resource_enumerate_children  (resource,
					     "/not/here",
					     G_RESOURCE_LOOKUP_FLAGS_NONE,
					     &error);
  g_assert (children == NULL);
  g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
  g_clear_error (&error);

  children = g_resource_enumerate_children  (resource,
					     "/a_prefix",
					     G_RESOURCE_LOOKUP_FLAGS_NONE,
					     &error);
  g_assert (children != NULL);
  g_assert_no_error (error);
136
  g_assert_cmpint (g_strv_length (children), ==, 2);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  g_strfreev (children);
}

static void
test_resource_file (void)
{
  GResource *resource;
  GError *error = NULL;

  resource = g_resource_load ("not-there", &error);
  g_assert (resource == NULL);
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
  g_clear_error (&error);

  resource = g_resource_load ("test.gresource", &error);
  g_assert (resource != NULL);
  g_assert_no_error (error);

  test_resource (resource);
  g_resource_unref (resource);
}

static void
test_resource_data (void)
{
  GResource *resource;
  GError *error = NULL;
  gboolean loaded_file;
  char *content;
  gsize content_size;
  GBytes *data;

  loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
				     NULL);
  g_assert (loaded_file);

  data = g_bytes_new_take (content, content_size);
  resource = g_resource_new_from_data (data, &error);
175
  g_bytes_unref (data);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  g_assert (resource != NULL);
  g_assert_no_error (error);

  test_resource (resource);

  g_resource_unref (resource);
}

static void
test_resource_registred (void)
{
  GResource *resource;
  GError *error = NULL;
  gboolean found, success;
  gsize size;
  guint32 flags;
  GBytes *data;
  char **children;
  GInputStream *in;
  char buffer[128];

  resource = g_resource_load ("test.gresource", &error);
  g_assert (resource != NULL);
  g_assert_no_error (error);

  found = g_resources_get_info ("/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (!found);
  g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
  g_clear_error (&error);

  g_resources_register (resource);

  found = g_resources_get_info ("/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
215
  g_assert_cmpint (size, ==, 6);
216 217 218 219 220 221 222
  g_assert (flags == (G_RESOURCE_FLAGS_COMPRESSED));

  found = g_resources_get_info ("/a_prefix/test2.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
223 224
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpint (flags, ==, 0);
225 226 227 228 229 230

  found = g_resources_get_info ("/a_prefix/test2-alias.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
231 232
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpuint (flags, ==, 0);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

  data = g_resources_lookup_data ("/test1.txt",
				  G_RESOURCE_LOOKUP_FLAGS_NONE,
				  &error);
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
  g_assert_no_error (error);
  g_bytes_unref (data);

  in = g_resources_open_stream ("/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&error);
  g_assert (in != NULL);
  g_assert_no_error (error);

  success = g_input_stream_read_all (in, buffer, sizeof (buffer) - 1,
				     &size,
				     NULL, &error);
  g_assert (success);
  g_assert_no_error (error);
252
  g_assert_cmpint (size, ==, 6);
253 254 255
  buffer[size] = 0;
  g_assert_cmpstr (buffer, ==, "test1\n");

256 257 258
  g_input_stream_close (in, NULL, &error);
  g_assert_no_error (error);
  g_clear_object (&in);
259 260 261 262 263 264 265

  data = g_resources_lookup_data ("/a_prefix/test2.txt",
				  G_RESOURCE_LOOKUP_FLAGS_NONE,
				  &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
266
  g_assert_cmpint (size, ==, 6);
267 268 269 270 271 272 273 274 275
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
  g_bytes_unref (data);

  data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
				  G_RESOURCE_LOOKUP_FLAGS_NONE,
				  &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
276
  g_assert_cmpint (size, ==, 6);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
  g_bytes_unref (data);

  children = g_resources_enumerate_children ("/not/here",
					     G_RESOURCE_LOOKUP_FLAGS_NONE,
					     &error);
  g_assert (children == NULL);
  g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
  g_clear_error (&error);

  children = g_resources_enumerate_children ("/a_prefix",
					     G_RESOURCE_LOOKUP_FLAGS_NONE,
					     &error);
  g_assert (children != NULL);
  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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

  found = g_resources_get_info ("/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (!found);
  g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
  g_clear_error (&error);
}

static void
test_resource_automatic (void)
{
  GError *error = NULL;
  gboolean found;
  gsize size;
  guint32 flags;
  GBytes *data;

  found = g_resources_get_info ("/auto_loaded/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
320 321
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpint (flags, ==, 0);
322 323 324 325 326 327 328

  data = g_resources_lookup_data ("/auto_loaded/test1.txt",
				  G_RESOURCE_LOOKUP_FLAGS_NONE,
				  &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
329
  g_assert_cmpint (size, ==, 6);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
  g_bytes_unref (data);
}

static void
test_resource_manual (void)
{
  GError *error = NULL;
  gboolean found;
  gsize size;
  guint32 flags;
  GBytes *data;

  found = g_resources_get_info ("/manual_loaded/test1.txt",
				G_RESOURCE_LOOKUP_FLAGS_NONE,
				&size, &flags, &error);
  g_assert (found);
  g_assert_no_error (error);
348 349
  g_assert_cmpint (size, ==, 6);
  g_assert_cmpuint (flags, ==, 0);
350 351 352 353 354 355 356

  data = g_resources_lookup_data ("/manual_loaded/test1.txt",
				  G_RESOURCE_LOOKUP_FLAGS_NONE,
				  &error);
  g_assert (data != NULL);
  g_assert_no_error (error);
  size = g_bytes_get_size (data);
357
  g_assert_cmpint (size, ==, 6);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
  g_bytes_unref (data);
}

static void
test_resource_module (void)
{
  GIOModule *module;
  gboolean found;
  gsize size;
  guint32 flags;
  GBytes *data;
  GError *error;

  if (g_module_supported ())
    {
      char *dir, *path;

      dir = g_get_current_dir ();

      path = g_strconcat (dir, G_DIR_SEPARATOR_S "libresourceplugin",  NULL);
      module = g_io_module_new (path);
      g_free (path);
381
      g_free (dir);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

      error = NULL;

      found = g_resources_get_info ("/resourceplugin/test1.txt",
				    G_RESOURCE_LOOKUP_FLAGS_NONE,
				    &size, &flags, &error);
      g_assert (!found);
      g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
      g_clear_error (&error);

      g_type_module_use (G_TYPE_MODULE (module));

      found = g_resources_get_info ("/resourceplugin/test1.txt",
				    G_RESOURCE_LOOKUP_FLAGS_NONE,
				    &size, &flags, &error);
      g_assert (found);
      g_assert_no_error (error);
399 400
      g_assert_cmpint (size, ==, 6);
      g_assert_cmpuint (flags, ==, 0);
401 402 403 404 405 406 407

      data = g_resources_lookup_data ("/resourceplugin/test1.txt",
				      G_RESOURCE_LOOKUP_FLAGS_NONE,
				      &error);
      g_assert (data != NULL);
      g_assert_no_error (error);
      size = g_bytes_get_size (data);
408
      g_assert_cmpint (size, ==, 6);
409 410 411 412 413 414 415 416 417 418 419 420 421 422
      g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
      g_bytes_unref (data);

      g_type_module_unuse (G_TYPE_MODULE (module));

      found = g_resources_get_info ("/resourceplugin/test1.txt",
				    G_RESOURCE_LOOKUP_FLAGS_NONE,
				    &size, &flags, &error);
      g_assert (!found);
      g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
      g_clear_error (&error);
    }
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
static void
test_uri_query_info (void)
{
  GResource *resource;
  GError *error = NULL;
  gboolean loaded_file;
  char *content;
  gsize content_size;
  GBytes *data;
  GFile *file;
  GFileInfo *info;
  const char *content_type;

  loaded_file = g_file_get_contents ("test.gresource", &content, &content_size,
                                     NULL);
  g_assert (loaded_file);

  data = g_bytes_new_take (content, content_size);
  resource = g_resource_new_from_data (data, &error);
442
  g_bytes_unref (data);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  g_assert (resource != NULL);
  g_assert_no_error (error);

  g_resources_register (resource);

  file = g_file_new_for_uri ("resource://" "/a_prefix/test2-alias.txt");

  info = g_file_query_info (file, "*", 0, NULL, &error);
  g_assert_no_error (error);
  g_object_unref (file);

  content_type = g_file_info_get_content_type (info);
  g_assert (content_type);
  g_assert_cmpstr (content_type, ==, "text/plain");

  g_object_unref (info);

  g_resources_unregister (resource);
  g_resource_unref (resource);
}
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

int
main (int   argc,
      char *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);

  _g_test2_register_resource ();

  g_test_add_func ("/resource/file", test_resource_file);
  g_test_add_func ("/resource/data", test_resource_data);
  g_test_add_func ("/resource/registred", test_resource_registred);
  g_test_add_func ("/resource/manual", test_resource_manual);
#ifdef G_HAS_CONSTRUCTORS
  g_test_add_func ("/resource/automatic", test_resource_automatic);
  /* This only uses automatic resources too, so it tests the constructors and destructors */
  g_test_add_func ("/resource/module", test_resource_module);
#endif
482
  g_test_add_func ("/resource/uri/query-info", test_uri_query_info);
483 484 485

  return g_test_run();
}