summaryrefslogtreecommitdiff
path: root/libpurple/tests/test_image.c
blob: caabef05999715d5f2b5cb9e747c4ab347803cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
/*
 * Purple
 *
 * Purple is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the COPYRIGHT file distributed
 * with this source distribution
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
 */

#include <glib.h>
#include <string.h>

#include <purple.h>

// generated via:
// $ cat test-image.png | hexdump -v -e '1 1 "0x%02x," " "' | xargs -n 8 echo
static const gsize test_image_data_len = 160;
static const guint8 test_image_data[] = {
	0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
	0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
	0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a,
	0x73, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59,
	0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b,
	0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00,
	0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xe0,
	0x0a, 0x02, 0x16, 0x30, 0x22, 0x28, 0xa4, 0xc9,
	0xdd, 0x00, 0x00, 0x00, 0x1d, 0x69, 0x54, 0x58,
	0x74, 0x43, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x72, 0x65,
	0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74,
	0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x64, 0x2e,
	0x65, 0x07, 0x00, 0x00, 0x00, 0x16, 0x49, 0x44,
	0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, 0xff,
	0x3f, 0x03, 0x03, 0x03, 0xe3, 0xb3, 0x4c, 0xb5,
	0x9b, 0x4e, 0x0b, 0x00, 0x2f, 0xa9, 0x06, 0x2f,
	0x8a, 0xd1, 0xc6, 0xb3, 0x00, 0x00, 0x00, 0x00,
	0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
};

/******************************************************************************
 * Helpers
 *****************************************************************************/
static void
_test_image(PurpleImage *image,
            const guint8 *edata,
            gsize elen,
            const gchar *path,
            const gchar *ext,
            const gchar *mimetype)
{
	GBytes *bytes = NULL;
	const guint8 *adata = NULL;
	gsize alen;

	g_assert(PURPLE_IS_IMAGE(image));

	bytes = purple_image_get_contents(image);
	adata = g_bytes_get_data(bytes, &alen);
	g_assert_cmpmem(adata, alen, edata, elen);
	g_bytes_unref(bytes);

	/* if the caller provided a path, check it, otherwise just make sure we
	 * have something.
	 */
	if(path != NULL) {
		g_assert_cmpstr(purple_image_get_path(image), ==, path);
	} else {
		const gchar *apath = purple_image_get_path(image);

		g_assert(apath);
		g_assert_cmpstr(apath, !=, "");
	}

	g_assert_cmpstr(purple_image_get_extension(image), ==, ext);
	g_assert_cmpstr(purple_image_get_mimetype(image), ==, mimetype);

	g_object_unref(G_OBJECT(image));
}

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_image_new_from_bytes(void) {
	GBytes *bytes = g_bytes_new(test_image_data, test_image_data_len);
	PurpleImage *image = purple_image_new_from_bytes(bytes);

	_test_image(
		image,
		g_bytes_get_data(bytes, NULL),
		g_bytes_get_size(bytes),
		NULL,
		"png",
		"image/png"
	);

	g_bytes_unref(bytes);
}


static void
test_image_new_from_data(void) {
	PurpleImage *image = purple_image_new_from_data(
		test_image_data,
		test_image_data_len
	);

	_test_image(
		image,
		test_image_data,
		test_image_data_len,
		NULL,
		"png",
		"image/png"
	);
}

static void
test_image_new_from_file(void) {
	PurpleImage *image = NULL;
	GError *error = NULL;
	gchar *path = NULL;
	gchar *edata = NULL;
	gsize elen;

	path = g_build_filename(TEST_DATA_DIR, "test-image.png", NULL);
	image = purple_image_new_from_file(path, &error);
	g_assert_no_error(error);

	g_file_get_contents(path, &edata, &elen, &error);
	g_assert_no_error(error);

	_test_image(
		image,
		(guint8 *)edata,
		elen,
		path,
		"png",
		"image/png"
	);

	g_free(edata);
	g_free(path);
}

/******************************************************************************
 * Main
 *****************************************************************************/
gint
main(gint argc, gchar **argv) {
	g_test_init(&argc, &argv, NULL);

	g_test_set_nonfatal_assertions();

	g_test_add_func("/image/new-from-bytes", test_image_new_from_bytes);
	g_test_add_func("/image/new-from-data", test_image_new_from_data);
	g_test_add_func("/image/new-from-file", test_image_new_from_file);

	return g_test_run();
}