1 | /* -*- Mode: C; c-basic-offset: 2; -*- */ |
2 | /* GdkPixbuf library - test loaders |
3 | * |
4 | * Copyright (C) 2015 Red Hat, Inc. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | * |
19 | * Author: Benjamin Otte |
20 | */ |
21 | |
22 | #include "config.h" |
23 | |
24 | #include <gdk-pixbuf/gdk-pixbuf.h> |
25 | |
26 | #include "test-common.h" |
27 | |
28 | static void |
29 | test_fail_size (GFile *file, |
30 | guint chunk_size) |
31 | { |
32 | GdkPixbufLoader *loader; |
33 | GError *error = NULL; |
34 | guchar *contents; |
35 | gsize i, contents_length; |
36 | char *filename; |
37 | gboolean success; |
38 | |
39 | if (!file_supported (file)) |
40 | { |
41 | g_test_skip (msg: "format not supported" ); |
42 | return; |
43 | } |
44 | |
45 | filename = g_file_get_path (file); |
46 | |
47 | success = g_file_load_contents (file, NULL, contents: (gchar **) &contents, length: &contents_length, NULL, error: &error); |
48 | g_assert_no_error (error); |
49 | g_assert (success); |
50 | |
51 | #ifdef GDK_PIXBUF_USE_GIO_MIME |
52 | { |
53 | char *mime_type, *content_type; |
54 | |
55 | content_type = g_content_type_guess (filename, data: contents, data_size: contents_length, NULL); |
56 | mime_type = g_content_type_get_mime_type (type: content_type); |
57 | g_assert (mime_type); |
58 | loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error: &error); |
59 | g_free (mem: mime_type); |
60 | g_free (mem: content_type); |
61 | } |
62 | #else |
63 | { |
64 | char *format; |
65 | |
66 | success = find_format (filename, &format); |
67 | g_assert_true (success); |
68 | loader = gdk_pixbuf_loader_new_with_type (format, &error); |
69 | g_free (format); |
70 | } |
71 | #endif |
72 | g_assert_no_error (error); |
73 | g_assert (loader != NULL); |
74 | |
75 | for (i = 0; i < contents_length; i += chunk_size) |
76 | { |
77 | success = gdk_pixbuf_loader_write (loader, buf: &contents[i], MIN(chunk_size, contents_length - i), error: &error); |
78 | if (!success) |
79 | { |
80 | g_assert (error); |
81 | g_clear_error (err: &error); |
82 | goto out; |
83 | } |
84 | g_assert_no_error (error); |
85 | } |
86 | |
87 | success = gdk_pixbuf_loader_close (loader, error: &error); |
88 | g_assert (!success); |
89 | g_assert (error); |
90 | g_clear_error (err: &error); |
91 | |
92 | out: |
93 | g_free (mem: contents); |
94 | g_object_unref (object: loader); |
95 | g_free (mem: filename); |
96 | } |
97 | |
98 | static void |
99 | test_fail_tiny (gconstpointer data) |
100 | { |
101 | GFile *file = (GFile *) data; |
102 | |
103 | test_fail_size (file, chunk_size: 1); |
104 | } |
105 | |
106 | static void |
107 | test_fail_huge (gconstpointer data) |
108 | { |
109 | GFile *file = (GFile *) data; |
110 | |
111 | test_fail_size (file, G_MAXUINT); |
112 | } |
113 | |
114 | int |
115 | main (int argc, char **argv) |
116 | { |
117 | |
118 | g_test_init (argc: &argc, argv: &argv, NULL); |
119 | |
120 | if (argc < 2) |
121 | { |
122 | GFile *dir; |
123 | gchar *test_images; |
124 | |
125 | test_images = g_build_filename (first_element: g_test_get_dir (file_type: G_TEST_DIST), "test-images/fail" , NULL); |
126 | dir = g_file_new_for_path (path: test_images); |
127 | |
128 | add_test_for_all_images (prefix: "/pixbuf/fail_tiny" , base: dir, file: dir, test_func: test_fail_tiny, NULL); |
129 | add_test_for_all_images (prefix: "/pixbuf/fail_huge" , base: dir, file: dir, test_func: test_fail_huge, NULL); |
130 | |
131 | g_object_unref (object: dir); |
132 | g_free (mem: test_images); |
133 | } |
134 | else |
135 | { |
136 | guint i; |
137 | |
138 | for (i = 1; i < argc; i++) |
139 | { |
140 | GFile *file = g_file_new_for_commandline_arg (arg: argv[i]); |
141 | |
142 | add_test_for_all_images (prefix: "/pixbuf/fail_tiny" , NULL, file, test_func: test_fail_tiny, NULL); |
143 | add_test_for_all_images (prefix: "/pixbuf/fail_huge" , NULL, file, test_func: test_fail_huge, NULL); |
144 | |
145 | g_object_unref (object: file); |
146 | } |
147 | } |
148 | |
149 | return g_test_run (); |
150 | } |
151 | |