1 /* test_picture - Simple tester for picture routines in grabbag
2  * Copyright (C) 2006-2009  Josh Coalson
3  * Copyright (C) 2011-2016  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 
24 #include <string.h>
25 #include "FLAC/assert.h"
26 #include "share/grabbag.h"
27 
28 typedef struct {
29 	const char *path;
30 	const char *mime_type;
31 	const char *description;
32 	FLAC__uint32 width;
33 	FLAC__uint32 height;
34 	FLAC__uint32 depth;
35 	FLAC__uint32 colors;
36 	FLAC__StreamMetadata_Picture_Type type;
37 } PictureFile;
38 
39 PictureFile picturefiles[] = {
40 	{ "0.gif", "image/gif" , "", 24, 24, 24, 2, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
41 	{ "1.gif", "image/gif" , "", 12,  8, 24, 256, FLAC__STREAM_METADATA_PICTURE_TYPE_BACK_COVER },
42 	{ "2.gif", "image/gif" , "", 16, 14, 24, 128, FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER },
43 	{ "0.jpg", "image/jpeg", "", 30, 20,  8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
44 	{ "4.jpg", "image/jpeg", "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
45 	{ "0.png", "image/png" , "", 30, 20,  8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
46 	{ "1.png", "image/png" , "", 30, 20,  8, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
47 	{ "2.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
48 	{ "3.png", "image/png" , "", 30, 20, 24, 7, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
49 	{ "4.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
50 	{ "5.png", "image/png" , "", 31, 47, 24, 0, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
51 	{ "6.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
52 	{ "7.png", "image/png" , "", 31, 47, 24, 23, FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER },
53 	{ "8.png", "image/png" , "", 32, 32, 32, 0, 999 }
54 };
55 
56 static FLAC__bool debug_ = false;
57 
failed_(const char * msg)58 static FLAC__bool failed_(const char *msg)
59 {
60 	if(msg)
61 		printf("FAILED, %s\n", msg);
62 	else
63 		printf("FAILED\n");
64 
65 	return false;
66 }
67 
test_one_picture(const char * prefix,const PictureFile * pf,const PictureResolution * res,FLAC__bool fn_only)68 static FLAC__bool test_one_picture(const char *prefix, const PictureFile *pf, const PictureResolution * res, FLAC__bool fn_only)
69 {
70 	FLAC__StreamMetadata *obj;
71 	const char *error;
72 	char s[4096];
73 	if(fn_only)
74 		flac_snprintf(s, sizeof(s), "pictures/%s", pf->path);
75 	else if (res == NULL)
76 		flac_snprintf(s, sizeof(s), "%u|%s|%s||pictures/%s", (unsigned)pf->type, pf->mime_type, pf->description, pf->path);
77 	else
78 		flac_snprintf(s, sizeof(s), "%u|%s|%s|%dx%dx%d/%d|pictures/%s", (unsigned)pf->type, pf->mime_type, pf->description, res->width, res->height, res->depth, res->colors, pf->path);
79 
80 	printf("testing grabbag__picture_parse_specification(\"%s\")... ", s);
81 
82 	flac_snprintf(s, sizeof(s), "%s/%s", prefix, pf->path);
83 	if((obj = grabbag__picture_from_specification(fn_only? FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER : pf->type, pf->mime_type, pf->description, res, s, &error)) == 0)
84 		return failed_(error);
85 	if(debug_) {
86 		printf("\ntype=%u (%s)\nmime_type=%s\ndescription=%s\nwidth=%u\nheight=%u\ndepth=%u\ncolors=%u\ndata_length=%u\n",
87 			obj->data.picture.type,
88 			obj->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED?
89 				FLAC__StreamMetadata_Picture_TypeString[obj->data.picture.type] : "UNDEFINED",
90 			obj->data.picture.mime_type,
91 			obj->data.picture.description,
92 			obj->data.picture.width,
93 			obj->data.picture.height,
94 			obj->data.picture.depth,
95 			obj->data.picture.colors,
96 			obj->data.picture.data_length
97 		);
98 	}
99 	if(obj->data.picture.type != (fn_only? FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER : pf->type))
100 		return failed_("picture type mismatch");
101 	if(strcmp(obj->data.picture.mime_type, pf->mime_type))
102 		return failed_("picture MIME type mismatch");
103 	if(strcmp((const char *)obj->data.picture.description, (const char *)pf->description))
104 		return failed_("picture description mismatch");
105 	if(obj->data.picture.width != pf->width)
106 		return failed_("picture width mismatch");
107 	if(obj->data.picture.height != pf->height)
108 		return failed_("picture height mismatch");
109 	if(obj->data.picture.depth != pf->depth)
110 		return failed_("picture depth mismatch");
111 	if(obj->data.picture.colors != pf->colors)
112 		return failed_("picture colors mismatch");
113 	printf("OK\n");
114 	FLAC__metadata_object_delete(obj);
115 	return true;
116 }
117 
do_picture(const char * prefix)118 static FLAC__bool do_picture(const char *prefix)
119 {
120 	FLAC__StreamMetadata *obj;
121 	PictureResolution res;
122 	const char *error;
123 	size_t i;
124 
125 	printf("\n+++ grabbag unit test: picture\n\n");
126 
127 	/* invalid spec: no filename */
128 	printf("testing grabbag__picture_parse_specification(\"\")... ");
129 	if(0 != (obj = grabbag__picture_parse_specification("", &error)))
130 		return failed_("expected error, got object");
131 	printf("OK (failed as expected, error: %s)\n", error);
132 
133 	/* invalid spec: no filename */
134 	printf("testing grabbag__picture_parse_specification(\"||||\")... ");
135 	if(0 != (obj = grabbag__picture_parse_specification("||||", &error)))
136 		return failed_("expected error, got object");
137 	printf("OK (failed as expected: %s)\n", error);
138 
139 	/* invalid spec: no filename */
140 	printf("testing grabbag__picture_parse_specification(\"|image/gif|||\")... ");
141 	if(0 != (obj = grabbag__picture_parse_specification("|image/gif|||", &error)))
142 		return failed_("expected error, got object");
143 	printf("OK (failed as expected: %s)\n", error);
144 
145 	/* invalid spec: bad resolution */
146 	printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320|0.gif\")... ");
147 	if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320|0.gif", &error)))
148 		return failed_("expected error, got object");
149 	printf("OK (failed as expected: %s)\n", error);
150 
151 	/* invalid spec: bad resolution */
152 	printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240|0.gif\")... ");
153 	if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240|0.gif", &error)))
154 		return failed_("expected error, got object");
155 	printf("OK (failed as expected: %s)\n", error);
156 
157 	/* invalid spec: no filename */
158 	printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9|\")... ");
159 	if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9|", &error)))
160 		return failed_("expected error, got object");
161 	printf("OK (failed as expected: %s)\n", error);
162 
163 	/* invalid spec: #colors exceeds color depth */
164 	printf("testing grabbag__picture_parse_specification(\"|image/gif|desc|320x240x9/2345|0.gif\")... ");
165 	if(0 != (obj = grabbag__picture_parse_specification("|image/gif|desc|320x240x9/2345|0.gif", &error)))
166 		return failed_("expected error, got object");
167 	printf("OK (failed as expected: %s)\n", error);
168 
169 	/* invalid spec: standard icon has to be 32x32 PNG */
170 	printf("testing grabbag__picture_parse_specification(\"1|-->|desc|32x24x9|0.gif\")... ");
171 	if(0 != (obj = grabbag__picture_parse_specification("1|-->|desc|32x24x9|0.gif", &error)))
172 		return failed_("expected error, got object");
173 	printf("OK (failed as expected: %s)\n", error);
174 
175 	/* invalid spec: need resolution for linked URL */
176 	printf("testing grabbag__picture_parse_specification(\"|-->|desc||http://blah.blah.blah/z.gif\")... ");
177 	if(0 != (obj = grabbag__picture_parse_specification("|-->|desc||http://blah.blah.blah/z.gif", &error)))
178 		return failed_("expected error, got object");
179 	printf("OK (failed as expected: %s)\n", error);
180 
181 	printf("testing grabbag__picture_parse_specification(\"|-->|desc|320x240x9|http://blah.blah.blah/z.gif\")... ");
182 	if(0 == (obj = grabbag__picture_parse_specification("|-->|desc|320x240x9|http://blah.blah.blah/z.gif", &error)))
183 		return failed_(error);
184 	printf("OK\n");
185 	FLAC__metadata_object_delete(obj);
186 
187 	/* test automatic parsing of picture files from only the file name */
188 	for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
189 		if(!test_one_picture(prefix, picturefiles+i, NULL, /*fn_only=*/true))
190 			return false;
191 
192 	/* test automatic parsing of picture files to get resolution/color info */
193 	for(i = 0; i < sizeof(picturefiles)/sizeof(picturefiles[0]); i++)
194 		if(!test_one_picture(prefix, picturefiles+i, NULL, /*fn_only=*/false))
195 			return false;
196 
197 	res.width = picturefiles[0].width = 320;
198 	res.height = picturefiles[0].height = 240;
199 	res.depth = picturefiles[0].depth = 3;
200 	res.colors = picturefiles[0].colors = 2;
201 	if(!test_one_picture(prefix, picturefiles+0, &res, /*fn_only=*/false))
202 		return false;
203 
204 	return true;
205 }
206 
main(int argc,char * argv[])207 int main(int argc, char *argv[])
208 {
209 	const char *usage = "usage: test_pictures path_prefix\n";
210 
211 	if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
212 		puts(usage);
213 		return 0;
214 	}
215 
216 	if(argc != 2) {
217 		fputs(usage, stderr);
218 		return 255;
219 	}
220 
221 	return do_picture(argv[1])? 0 : 1;
222 }
223