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