Debian Codegraph @

tests/randomtest.c

raw symbols SCIP
1 /* Copyright (C) 2005 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gfile.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifndef _MSC_VER
36 #include <strings.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39 #endif
40 #include <dirent.h>
41 #include <math.h>
42 #include <signal.h>
43 #include <time.h>
44
45 #ifdef TEST_FREETYPE
46 # include <ft2build.h>
47 # include FT_FREETYPE_H
48 # include FT_OUTLINE_H
49 #endif
50
51 #define true 1
52 #define false 0
53
54
55 #ifdef TEST_FREETYPE
56 static int check_outlines = false;
57 static int nohints = false;
58 static int rasterize = false;
59 #else
60 static char *command = "fontforge";
61 #endif
62 static char *results_dir = "results";
63 #define GOOD_FONTS_DIR "/home/gww/myfonts/fontforge/others"
64 static char *default_dir_list[] = {
65 GOOD_FONTS_DIR,
66 NULL
67 };
68 static char *default_ext_list[] = {
69 "ttf",
70 "otf",
71 "ttc",
72 "cid",
73 "pfb",
74 "pfa",
75 "bdf",
76 #ifdef TEST_FREETYPE
77 #else
78 "sfd",
79 "svg",
80 "pt3",
81 #endif
82 NULL
83 };
84 static int error_count = 1;
85 static int error_fraction = 0;
86
87 static struct fontlist {
88 char *name;
89 int len;
90 unsigned int isbinary: 1;
91 unsigned int isascii: 1;
92 unsigned int ishex: 1;
93 } *fontlist;
94 static int fcnt;
95
96 static int extmatch(char *filename,char **extensions) {
97 int i;
98 char *pt;
99
100 if ( extensions==NULL )
101 return( true );
102 pt = strrchr(filename,'.');
103 if ( pt==NULL )
104 return( false );
105 if ( pt<strrchr(filename,'/'))
106 return( false );
107 for ( i=0; extensions[i]!=NULL; ++i )
108 if ( strcasecmp(pt+1,extensions[i])==0 || strcasecmp(pt,extensions[i])==0 )
109 return( true );
110
111 return( false );
112 }
113
114 static void figurefiletype(struct fontlist *item) {
115 FILE *foo;
116
117 item->isbinary = item->isascii = item->ishex = false;
118
119 foo = fopen(item->name,"rb");
120 if ( foo!=NULL ) {
121 /* Try to guess the file type from the first few characters... */
122 int ch1 = getc(foo);
123 int ch2 = getc(foo);
124 int ch3 = getc(foo);
125 int ch4 = getc(foo);
126 int ch5 = getc(foo);
127 int ch6 = getc(foo);
128 int ch7 = getc(foo);
129 fclose(foo);
130 if (( ch1==0 && ch2==1 && ch3==0 && ch4==0 ) ||
131 (ch1=='O' && ch2=='T' && ch3=='T' && ch4=='O') ||
132 (ch1=='t' && ch2=='r' && ch3=='u' && ch4=='e') ||
133 (ch1=='t' && ch2=='t' && ch3=='c' && ch4=='f') ) {
134 /* ttf, otf, ttc files */
135 item->isbinary = true;
136 } else if ( ch1==0x80 && ch2=='\01' ) {
137 /* PFB header */
138 item->isbinary = true;
139 } else if ( ch1=='%' && ch2=='!' ) {
140 /* Random PostScript */
141 if ( strstr(item->name,".pfa")!=NULL || strstr(item->name,".PFA")!=NULL )
142 item->ishex = true;
143 else
144 item->isascii = true;
145 } else if ( ch1==1 && ch2==0 && ch3==4 ) {
146 /* Bare CFF */
147 item->isbinary = true;
148 } else if ( ch1=='<' && ch2=='?' && (ch3=='x'||ch3=='X') && (ch4=='m'||ch4=='M') ) {
149 /* SVG */
150 item->isascii = true;
151 } else if ( ch1==0xef && ch2==0xbb && ch3==0xbf &&
152 ch4=='<' && ch5=='?' && (ch6=='x'||ch6=='X') && (ch7=='m'||ch7=='M') ) {
153 /* UTF-8 SVG with initial byte ordering mark */
154 item->isascii = true;
155 } else if ( ch1=='S' && ch2=='p' && ch3=='l' && ch4=='i' ) {
156 /* SFD */
157 item->isascii = true;
158 } else if ( ch1=='S' && ch2=='T' && ch3=='A' && ch4=='R' ) {
159 /* BDF */
160 item->ishex = true;
161 } else if ( ch1=='\1' && ch2=='f' && ch3=='c' && ch4=='p' ) {
162 /* Windows FON */
163 item->isbinary = true;
164 } else {
165 fprintf( stderr, "Didn't recognize file type of %s, assumed binary\n", item->name );
166 item->isbinary = true;
167 }
168 } else {
169 fprintf( stderr, "Could not open %s for typing the file.\n", item->name );
170 item->isbinary = true;
171 }
172 }
173
174 static void FindFonts(char **fontdirs,char **extensions) {
175 DIR *examples;
176 struct dirent *ent;
177 int i, max;
178 char buffer[1025];
179 off_t filesize;
180
181 max = 0;
182 fcnt = 0;
183
184 for ( i=0; fontdirs[i]!=NULL; ++i ) {
185 examples = opendir(fontdirs[i]);
186 if ( examples==NULL ) {
187 fprintf( stderr, "Could not open example font directory.\n\t%s\n", fontdirs[i] );
188 exit(1);
189 }
190
191 while ( (ent = readdir(examples))!=NULL ) {
192 snprintf(buffer,sizeof(buffer),"%s/%s", fontdirs[i], ent->d_name );
193 filesize = GFileGetSize(buffer);
194 if ( filesize==-1 || GFileIsDir(buffer))
195 continue;
196 if ( extensions==NULL || extmatch(buffer,extensions)) {
197 if ( fcnt>=max ) {
198 max += 100;
199 fontlist = realloc(fontlist,max*sizeof(struct fontlist));
200 if ( fontlist==NULL ) {
201 fprintf( stderr, "Couldn't allocate memory\n" );
202 exit(1);
203 }
204 }
205 fontlist[fcnt].name = strdup(buffer);
206 fontlist[fcnt].len = filesize;
207 figurefiletype(&fontlist[fcnt]);
208 ++fcnt;
209 }
210 }
211 closedir(examples);
212 }
213 if ( fcnt==0 ) {
214 fprintf( stderr, "No matching font files found.\n" );
215 exit(1);
216 }
217 fontlist[fcnt].name = NULL;
218 }
219
220 static int getErrorCnt(struct fontlist *item) {
221 if ( error_count==0 && error_fraction==0 )
222 return( 0 );
223
224 return( error_count + ceil(error_fraction*item->len));
225 }
226
227 static int getRandom(int low, int high) {
228 if ( low-high<0x10000 )
229 return( low + ((g_random_int()>>8)%(high+1-low)) );
230
231 return( low + (g_random_int()%(high+1-low)) );
232 }
233
234 static int copyfont(struct fontlist *item,char *newfont) {
235 static char buffer[8096];
236 FILE *good, *new;
237 int len;
238 int i, err_cnt;
239
240 good = fopen(item->name,"r");
241 if ( good==NULL ) {
242 fprintf( stderr, "Could not open %s\n", item->name );
243 return( false );
244 }
245 new = fopen(newfont,"w+");
246 if ( new==NULL ) {
247 fprintf( stderr, "Could not create temporary output file %s\n", newfont );
248 exit( 1 );
249 }
250
251 while ( (len = fread(buffer,1,sizeof(buffer), good ))>0 )
252 fwrite(buffer,1,len,new);
253 fclose(good);
254
255 err_cnt = getErrorCnt(item);
256 for ( i=0; i<err_cnt; ++i ) {
257 fseek(new,getRandom(0,item->len-1),SEEK_SET);
258 if ( item->isbinary )
259 putc(getRandom(0,0xff),new);
260 else if ( item->isascii )
261 putc(getRandom(0x20,0x7e),new);
262 else {
263 int hex = getRandom(0,15);
264 if ( hex<10 ) hex += '0';
265 else hex += 'A'-10;
266 putc(hex,new);
267 }
268 }
269 if ( ferror(new)) {
270 fclose(new);
271 unlink(newfont);
272 return( false );
273 }
274 fclose(new);
275 return( true );
276 }
277
278 #ifdef TEST_FREETYPE
279 static int FT_MoveTo(FT_Vector *to,void *user) {
280 return( 0 );
281 }
282
283 static int FT_LineTo(FT_Vector *to,void *user) {
284 return( 0 );
285 }
286
287 static int FT_ConicTo(FT_Vector *_cp, FT_Vector *to,void *user) {
288 return( 0 );
289 }
290
291 static int FT_CubicTo(FT_Vector *cp1, FT_Vector *cp2,FT_Vector *to,void *user) {
292 return( 0 );
293 }
294
295 static FT_Outline_Funcs outlinefuncs = {
296 FT_MoveTo,
297 FT_LineTo,
298 FT_ConicTo,
299 FT_CubicTo,
300 0,0 /* No shift, no delta */
301 };
302
303 static void TestFace(FT_Face face) {
304 int gid;
305 int load_flags = FT_LOAD_DEFAULT;
306
307 if ( check_outlines && (face->face_flags&FT_FACE_FLAG_SCALABLE))
308 load_flags = FT_LOAD_NO_BITMAP;
309 if ( nohints )
310 load_flags |= FT_LOAD_NO_HINTING;
311
312 FT_Set_Char_Size(face,0,(int) (12*64), 72, 72);
313
314 for ( gid=0; gid<face->num_glyphs; ++gid ) {
315 if ( check_outlines && (face->face_flags&FT_FACE_FLAG_SCALABLE)) {
316 if ( !FT_Load_Glyph(face,gid,load_flags))
317 FT_Outline_Decompose(&face->glyph->outline,&outlinefuncs,NULL);
318 } else {
319 FT_Load_Glyph(face,gid,load_flags);
320 }
321 if ( rasterize )
322 FT_Render_Glyph(face->glyph,ft_render_mode_normal);
323 }
324 FT_Done_Face(face);
325 }
326
327 static void ExecuteTest(char *testfont) {
328 FT_Library context;
329 FT_Face face;
330 int i,num;
331
332 if ( FT_Init_FreeType( &context )) {
333 fprintf( stderr, "Could not initialize freetype.\n" );
334 exit(1);
335 }
336 if ( FT_New_Face(context,testfont,0,&face)) {
337 /* The font is erroneous, so if this fails that's ok */
338 exit( 0 );
339 }
340 if ( face->num_faces==1 )
341 TestFace(face);
342 else {
343 num = face->num_faces;
344 FT_Done_Face(face);
345 for ( i=0; i<num; ++i ) {
346 if ( !FT_New_Face(context,testfont,i,&face)) {
347 TestFace(face);
348 }
349 }
350 }
351
352 exit(0);
353 }
354 #else
355 static void ExecuteTest(char *testfont) {
356 /* Test FontForge */
357 int error_fd;
358
359 freopen("/dev/null","w",stdout);
360 error_fd = dup(2);
361 freopen("/dev/null","w",stderr);
362
363 execlp(command,command,"-c","Open($1)",testfont,NULL);
364 write(error_fd,"execlp failed\n", 14);
365 abort();
366 }
367 #endif
368
369 static int child_pid;
370
371 static void abort_test(int sig) {
372 /* If a time-out happens, then kill the child */
373 kill(child_pid,SIGFPE);
374 write(2,"Timeout... ",11);
375 }
376
377 static void do_test(void) {
378 int i = getRandom(0,fcnt-1);
379 static int test_num=0;
380 char buffer[1024];
381
382 sprintf( buffer, "%s/test%d", results_dir, test_num++ );
383 if ( copyfont(&fontlist[i],buffer) ) {
384 signal(SIGALRM,abort_test);
385 alarm(60*60); /* Anything that takes more than an hour to parse is an error */
386 if (( child_pid = fork()) == 0 ) {
387 ExecuteTest(buffer);
388 } else if ( child_pid!=-1 ) {
389 int status;
390 waitpid(child_pid,&status,0);
391 alarm(0);
392 if ( WIFSIGNALED(status))
393 printf( "Error found in file: %s\n", buffer );
394 else
395 unlink(buffer);
396 } else {
397 fprintf( stderr, "Could not fork test case.\n" );
398 exit(1);
399 }
400 alarm(0);
401 }
402 }
403
404 int main( int argc, char **argv) {
405 char **dirs, **exts;
406 char *pt, *end;
407 int dcnt = 0, ecnt=0, rset=false, allexts = false;
408 int i;
409 time_t now;
410 char *testfile = NULL;
411
412 dirs = calloc(argc+1,sizeof(char **));
413 exts = calloc(argc+1,sizeof(char **));
414 for ( i=1; i<argc; ++i ) {
415 pt = argv[i];
416 if ( pt[0]=='-' && pt[1]=='-' )
417 ++pt;
418 if ( strcmp(pt,"-dir")==0 ) {
419 dirs[dcnt++] = argv[++i];
420 } else if ( strcmp(pt,"-ext")==0 ) {
421 exts[ecnt++] = argv[++i];
422 } else if ( strcmp(pt,"-all")==0 ) {
423 allexts = true;
424 } else if ( strcmp(pt,"-results")==0 ) {
425 results_dir = argv[++i];
426 } else if ( strcmp(pt,"-test")==0 ) {
427 testfile = argv[++i];
428 } else if ( strcmp(pt,"-error-count")==0 ) {
429 if ( !rset ) error_fraction = 0;
430 rset = true;
431 error_count = strtol(argv[++i],&end,10);
432 if ( *end!='\0' ) {
433 fprintf( stderr, "Bad value for error-count: %s\n", argv[i] );
434 exit( 1 );
435 }
436 } else if ( strcmp(pt,"-error-fraction")==0 ) {
437 if ( !rset ) error_count = 0;
438 rset = true;
439 error_fraction = strtod(argv[++i],&end);
440 if ( *end!='\0' ) {
441 fprintf( stderr, "Bad value for error-fraction: %s\n", argv[i] );
442 exit( 1 );
443 }
444 #ifdef TEST_FREETYPE
445 } else if ( strcmp(pt,"-check-outlines")==0 ) {
446 check_outlines = true;
447 } else if ( strcmp(pt,"-nohints")==0 ) {
448 nohints = true;
449 } else if ( strcmp(pt,"-rasterize")==0 ) {
450 rasterize = true;
451 #else
452 } else if ( strcmp(pt,"-command")==0 ) {
453 command = argv[++i];
454 #endif
455 } else {
456 fprintf( stderr, "%s [options]\tGenerate random erroneous fonts and attempt to parse them\n", argv[0] );
457 fprintf( stderr, "\t--dir <path-spec>\tAdds <path-spec> to list of dirs searched for fonts\n" );
458 fprintf( stderr, "\t--ext <extension>\tAdds <extension> to list of extension indicating fonts\n" );
459 fprintf( stderr, "\t--all \t\tAll non-directory files are assumed to be fonts\n" );
460 fprintf( stderr, "\t--results <dir>\tDirectory in which to place the test fonts.\n" );
461 fprintf( stderr, "\t--test <file>\tRun a single test on an already existing file.\n" );
462 fprintf( stderr, "\t--error-count <cnt>\tIntroduce <cnt> single byte errors into each font.\n" );
463 fprintf( stderr, "\t--error-fraction <frac>\tIntroduce <frac>*filesize single byte errors into each font.\n" );
464 #ifdef TEST_FREETYPE
465 fprintf( stderr, "\t--rasterize \t\tAttempt to rasterize each glyph.\n" );
466 fprintf( stderr, "\t--nohints \t\tTurn off hinting.\n" );
467 fprintf( stderr, "\t--check-outlines \t\tMake sure we can parse the outlines of each glyph.\n" );
468 #else
469 fprintf( stderr, "\t--command <cmd> \tProgram to execute.\n" );
470 #endif
471 fprintf( stderr, "\t--usage \t\tPrint this.\n" );
472 exit(1);
473 }
474 }
475 if ( allexts )
476 exts = NULL;
477 else if ( ecnt==0 )
478 exts = default_ext_list;
479 if ( dcnt==0 )
480 dirs = default_dir_list;
481
482 if ( testfile!=NULL )
483 ExecuteTest(testfile); /* This should never return */
484
485 time(&now);
486 g_random_set_seed(now);
487
488 FindFonts(dirs,exts);
489 mkdir(results_dir,0755);
490 for (;;)
491 do_test();
492
493 return( 0 );
494 }