Blame tests/pixbuf-fail.c

Packit 979760
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit 979760
/* GdkPixbuf library - test loaders
Packit 979760
 *
Packit 979760
 * Copyright (C) 2015 Red Hat, Inc.
Packit 979760
 *
Packit 979760
 * This program is free software; you can redistribute it and/or modify
Packit 979760
 * it under the terms of the GNU General Public License as published by
Packit 979760
 * the Free Software Foundation; either version 2 of the License, or
Packit 979760
 * (at your option) any later version.
Packit 979760
 *
Packit 979760
 * This program is distributed in the hope that it will be useful,
Packit 979760
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 979760
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 979760
 * GNU General Public License for more details.
Packit 979760
 *
Packit 979760
 * You should have received a copy of the GNU General Public License
Packit 979760
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 979760
 *
Packit 979760
 * Author: Benjamin Otte
Packit 979760
 */
Packit 979760
Packit 979760
#include "config.h"
Packit 979760
Packit 979760
#include <gdk-pixbuf/gdk-pixbuf.h>
Packit 979760
Packit 979760
#include "test-common.h"
Packit 979760
Packit 979760
static void
Packit 979760
test_fail_size (GFile *file,
Packit 979760
		guint  chunk_size)
Packit 979760
{
Packit 979760
  GdkPixbufLoader *loader;
Packit 979760
  GError *error = NULL;
Packit 979760
  guchar *contents;
Packit 979760
  gsize i, contents_length;
Packit 979760
  char *filename, *content_type, *mime_type;
Packit 979760
  gboolean success;
Packit 979760
Packit 979760
  if (!file_supported (file))
Packit 979760
    {
Packit 979760
      g_test_skip ("format not supported");
Packit 979760
      return;
Packit 979760
    }
Packit 979760
Packit 979760
  filename = g_file_get_path (file);
Packit 979760
Packit 979760
  success = g_file_load_contents (file, NULL, (gchar **) &contents, &contents_length, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
  g_assert (success);
Packit 979760
Packit 979760
  content_type = g_content_type_guess (filename, contents, contents_length, NULL);
Packit 979760
  mime_type = g_content_type_get_mime_type (content_type);
Packit 979760
  g_assert (mime_type);
Packit 979760
Packit 979760
  loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
  g_assert (loader != NULL);
Packit 979760
Packit 979760
  for (i = 0; i < contents_length; i += chunk_size)
Packit 979760
    {
Packit 979760
      success = gdk_pixbuf_loader_write (loader, &contents[i], MIN(chunk_size, contents_length - i), &error);
Packit 979760
      if (!success)
Packit 979760
        {
Packit 979760
          g_assert (error);
Packit 979760
          g_clear_error (&error);
Packit 979760
          goto out;
Packit 979760
        }
Packit 979760
      g_assert_no_error (error);
Packit 979760
    }
Packit 979760
  
Packit 979760
  success = gdk_pixbuf_loader_close (loader, &error);
Packit 979760
  g_assert (!success);
Packit 979760
  g_assert (error);
Packit 979760
  g_clear_error (&error);
Packit 979760
Packit 979760
out:
Packit 979760
  g_free (mime_type);
Packit 979760
  g_free (content_type);
Packit 979760
  g_free (contents);
Packit 979760
  g_object_unref (loader);
Packit 979760
  g_free (filename);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_fail_tiny (gconstpointer data)
Packit 979760
{
Packit 979760
  GFile *file = (GFile *) data;
Packit 979760
Packit 979760
  test_fail_size (file, 1);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_fail_huge (gconstpointer data)
Packit 979760
{
Packit 979760
  GFile *file = (GFile *) data;
Packit 979760
Packit 979760
  test_fail_size (file, G_MAXUINT);
Packit 979760
}
Packit 979760
Packit 979760
int
Packit 979760
main (int argc, char **argv)
Packit 979760
{
Packit 979760
Packit 979760
  g_test_init (&argc, &argv, NULL);
Packit 979760
Packit 979760
  if (argc < 2)
Packit 979760
    {
Packit 979760
      GFile *dir;
Packit 979760
      gchar *test_images;
Packit 979760
Packit 979760
      test_images = g_build_filename (g_test_get_dir (G_TEST_DIST), "test-images/fail", NULL);
Packit 979760
      dir = g_file_new_for_path (test_images);
Packit 979760
      
Packit 979760
      add_test_for_all_images ("/pixbuf/fail_tiny", dir, dir, test_fail_tiny, NULL);
Packit 979760
      add_test_for_all_images ("/pixbuf/fail_huge", dir, dir, test_fail_huge, NULL);
Packit 979760
Packit 979760
      g_object_unref (dir);
Packit 979760
      g_free (test_images);
Packit 979760
    }
Packit 979760
  else
Packit 979760
    {
Packit 979760
      guint i;
Packit 979760
Packit 979760
      for (i = 1; i < argc; i++)
Packit 979760
        {
Packit 979760
          GFile *file = g_file_new_for_commandline_arg (argv[i]);
Packit 979760
Packit 979760
          add_test_for_all_images ("/pixbuf/fail_tiny", NULL, file, test_fail_tiny, NULL);
Packit 979760
          add_test_for_all_images ("/pixbuf/fail_huge", NULL, file, test_fail_huge, NULL);
Packit 979760
Packit 979760
          g_object_unref (file);
Packit 979760
        }
Packit 979760
    }
Packit 979760
Packit 979760
  return g_test_run ();
Packit 979760
}