]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
Import CUPS 1.4svn-r7464.
[thirdparty/cups.git] / cups / testfile.c
1 /*
2 * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $"
3 *
4 * File test program for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Main entry.
20 * read_write_tests() - Perform read/write tests.
21 */
22
23 /*
24 * Include necessary headers...
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <time.h>
31 #include "string.h"
32 #include "file.h"
33 #include "debug.h"
34 #ifdef HAVE_LIBZ
35 # include <zlib.h>
36 #endif /* HAVE_LIBZ */
37 #include <fcntl.h>
38
39
40 /*
41 * Local functions...
42 */
43
44 static int read_write_tests(int compression);
45
46
47 /*
48 * 'main()' - Main entry.
49 */
50
51 int /* O - Exit status */
52 main(int argc, /* I - Number of command-line arguments */
53 char *argv[]) /* I - Command-line arguments */
54 {
55 int status; /* Exit status */
56 char filename[1024]; /* Filename buffer */
57 int fds[2]; /* Open file descriptors */
58 cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */
59
60
61 if (argc == 1)
62 {
63 /*
64 * Do uncompressed file tests...
65 */
66
67 status = read_write_tests(0);
68
69 #ifdef HAVE_LIBZ
70 /*
71 * Do compressed file tests...
72 */
73
74 putchar('\n');
75
76 status += read_write_tests(1);
77 #endif /* HAVE_LIBZ */
78
79 /*
80 * Test fdopen and close without reading...
81 */
82
83 pipe(fds);
84 close(fds[1]);
85
86 fputs("cupsFileOpenFd(fd, \"r\"): ", stdout);
87 fflush(stdout);
88
89 if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL)
90 {
91 puts("FAIL");
92 status ++;
93 }
94 else
95 {
96 /*
97 * Able to open file, now close without reading. If we don't return
98 * before the alarm fires, that is a failure and we will crash on the
99 * alarm signal...
100 */
101
102 puts("PASS");
103 fputs("cupsFileClose(no read): ", stdout);
104 fflush(stdout);
105
106 alarm(5);
107 cupsFileClose(fdfile);
108 alarm(0);
109
110 puts("PASS");
111 }
112
113 /*
114 * Test path functions...
115 */
116
117 fputs("cupsFileFind: ", stdout);
118 #ifdef WIN32
119 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) &&
120 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename)))
121 #else
122 if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
123 cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
124 #endif /* WIN32 */
125 printf("PASS (%s)\n", filename);
126 else
127 {
128 puts("FAIL");
129 status ++;
130 }
131
132 /*
133 * Summarize the results and return...
134 */
135
136 if (!status)
137 puts("\nALL TESTS PASSED!");
138 else
139 printf("\n%d TEST(S) FAILED!\n", status);
140 }
141 else
142 {
143 /*
144 * Cat the filename on the command-line...
145 */
146
147 cups_file_t *fp; /* File pointer */
148 char line[1024]; /* Line from file */
149
150
151 if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
152 {
153 perror(argv[1]);
154 status = 1;
155 }
156 else
157 {
158 status = 0;
159
160 while (cupsFileGets(fp, line, sizeof(line)))
161 puts(line);
162
163 if (!cupsFileEOF(fp))
164 perror(argv[1]);
165
166 cupsFileClose(fp);
167 }
168 }
169
170 return (status);
171 }
172
173
174 /*
175 * 'read_write_tests()' - Perform read/write tests.
176 */
177
178 static int /* O - Status */
179 read_write_tests(int compression) /* I - Use compression? */
180 {
181 int i; /* Looping var */
182 cups_file_t *fp; /* First file */
183 int status; /* Exit status */
184 char line[1024], /* Line from file */
185 *value; /* Directive value from line */
186 int linenum; /* Line number */
187 unsigned char readbuf[8192], /* Read buffer */
188 writebuf[8192]; /* Write buffer */
189 int byte; /* Byte from file */
190 static const char *partial_line = "partial line";
191 /* Partial line */
192
193
194 /*
195 * No errors so far...
196 */
197
198 status = 0;
199
200 /*
201 * Initialize the write buffer with random data...
202 */
203
204 #ifdef WIN32
205 srand((unsigned)time(NULL));
206 #else
207 srand(time(NULL));
208 #endif /* WIN32 */
209
210 for (i = 0; i < (int)sizeof(writebuf); i ++)
211 writebuf[i] = rand();
212
213 /*
214 * cupsFileOpen(write)
215 */
216
217 printf("cupsFileOpen(write%s): ", compression ? " compressed" : "");
218
219 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat",
220 compression ? "w9" : "w");
221 if (fp)
222 {
223 puts("PASS");
224
225 /*
226 * cupsFileCompression()
227 */
228
229 fputs("cupsFileCompression(): ", stdout);
230
231 if (cupsFileCompression(fp) == compression)
232 puts("PASS");
233 else
234 {
235 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
236 compression);
237 status ++;
238 }
239
240 /*
241 * cupsFilePuts()
242 */
243
244 fputs("cupsFilePuts(): ", stdout);
245
246 if (cupsFilePuts(fp, "# Hello, World\n") > 0)
247 puts("PASS");
248 else
249 {
250 printf("FAIL (%s)\n", strerror(errno));
251 status ++;
252 }
253
254 /*
255 * cupsFilePrintf()
256 */
257
258 fputs("cupsFilePrintf(): ", stdout);
259
260 for (i = 0; i < 1000; i ++)
261 if (cupsFilePrintf(fp, "TestLine %d\n", i) < 0)
262 break;
263
264 if (i >= 1000)
265 puts("PASS");
266 else
267 {
268 printf("FAIL (%s)\n", strerror(errno));
269 status ++;
270 }
271
272 /*
273 * cupsFilePutChar()
274 */
275
276 fputs("cupsFilePutChar(): ", stdout);
277
278 for (i = 0; i < 256; i ++)
279 if (cupsFilePutChar(fp, i) < 0)
280 break;
281
282 if (i >= 256)
283 puts("PASS");
284 else
285 {
286 printf("FAIL (%s)\n", strerror(errno));
287 status ++;
288 }
289
290 /*
291 * cupsFileWrite()
292 */
293
294 fputs("cupsFileWrite(): ", stdout);
295
296 for (i = 0; i < 10000; i ++)
297 if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0)
298 break;
299
300 if (i >= 10000)
301 puts("PASS");
302 else
303 {
304 printf("FAIL (%s)\n", strerror(errno));
305 status ++;
306 }
307
308 /*
309 * cupsFilePuts() with partial line...
310 */
311
312 fputs("cupsFilePuts(\"partial line\"): ", stdout);
313
314 if (cupsFilePuts(fp, partial_line) > 0)
315 puts("PASS");
316 else
317 {
318 printf("FAIL (%s)\n", strerror(errno));
319 status ++;
320 }
321
322 /*
323 * cupsFileClose()
324 */
325
326 fputs("cupsFileClose(): ", stdout);
327
328 if (!cupsFileClose(fp))
329 puts("PASS");
330 else
331 {
332 printf("FAIL (%s)\n", strerror(errno));
333 status ++;
334 }
335 }
336 else
337 {
338 printf("FAIL (%s)\n", strerror(errno));
339 status ++;
340 }
341
342 /*
343 * cupsFileOpen(read)
344 */
345
346 fputs("cupsFileOpen(read): ", stdout);
347
348 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r");
349 if (fp)
350 {
351 puts("PASS");
352
353 /*
354 * cupsFileGets()
355 */
356
357 fputs("cupsFileGets(): ", stdout);
358
359 if (cupsFileGets(fp, line, sizeof(line)))
360 {
361 if (line[0] == '#')
362 puts("PASS");
363 else
364 {
365 printf("FAIL (Got line \"%s\", expected comment line)\n", line);
366 status ++;
367 }
368 }
369 else
370 {
371 printf("FAIL (%s)\n", strerror(errno));
372 status ++;
373 }
374
375 /*
376 * cupsFileCompression()
377 */
378
379 fputs("cupsFileCompression(): ", stdout);
380
381 if (cupsFileCompression(fp) == compression)
382 puts("PASS");
383 else
384 {
385 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
386 compression);
387 status ++;
388 }
389
390 /*
391 * cupsFileGetConf()
392 */
393
394 linenum = 1;
395
396 fputs("cupsFileGetConf(): ", stdout);
397
398 for (i = 0; i < 1000; i ++)
399 if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
400 break;
401 else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
402 linenum != (i + 2))
403 break;
404
405 if (i >= 1000)
406 puts("PASS");
407 else if (line[0])
408 {
409 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum,
410 line, value ? value : "(null)");
411 status ++;
412 }
413 else
414 {
415 printf("FAIL (%s)\n", strerror(errno));
416 status ++;
417 }
418
419 /*
420 * cupsFileGetChar()
421 */
422
423 fputs("cupsFileGetChar(): ", stdout);
424
425 #ifdef DEBUG
426 puts("\ni byte\n----- -----");
427
428 for (i = 0; i < 256; i ++)
429 {
430 byte = cupsFileGetChar(fp);
431
432 printf("%-5d %-5d\n", i, byte);
433
434 if (byte != i)
435 break;
436 }
437 #else
438 for (i = 0; i < 256; i ++)
439 if ((byte = cupsFileGetChar(fp)) != i)
440 break;
441 #endif /* DEBUG */
442
443 if (i >= 256)
444 puts("PASS");
445 else if (byte >= 0)
446 {
447 printf("FAIL (Got %d, expected %d)\n", byte, i);
448 status ++;
449 }
450 else
451 {
452 printf("FAIL (%s)\n", strerror(errno));
453 status ++;
454 }
455
456 /*
457 * cupsFileRead()
458 */
459
460 fputs("cupsFileRead(): ", stdout);
461
462 for (i = 0; i < 10000; i ++)
463 if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
464 break;
465 else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
466 break;
467
468 if (i >= 10000)
469 puts("PASS");
470 else if (byte > 0)
471 {
472 printf("FAIL (Pass %d, ", i);
473
474 for (i = 0; i < (int)sizeof(readbuf); i ++)
475 if (readbuf[i] != writebuf[i])
476 break;
477
478 printf("match failed at offset %d - got %02X, expected %02X)\n",
479 i, readbuf[i], writebuf[i]);
480 }
481 else
482 {
483 printf("FAIL (%s)\n", strerror(errno));
484 status ++;
485 }
486
487 /*
488 * cupsFileGetChar() with partial line...
489 */
490
491 fputs("cupsFileGetChar(partial line): ", stdout);
492
493 for (i = 0; i < strlen(partial_line); i ++)
494 if ((byte = cupsFileGetChar(fp)) < 0)
495 break;
496 else if (byte != partial_line[i])
497 break;
498
499 if (!partial_line[i])
500 puts("PASS");
501 else
502 {
503 printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]);
504 status ++;
505 }
506
507 /*
508 * cupsFileClose()
509 */
510
511 fputs("cupsFileClose(): ", stdout);
512
513 if (!cupsFileClose(fp))
514 puts("PASS");
515 else
516 {
517 printf("FAIL (%s)\n", strerror(errno));
518 status ++;
519 }
520 }
521 else
522 {
523 printf("FAIL (%s)\n", strerror(errno));
524 status ++;
525 }
526
527 /*
528 * Return the test status...
529 */
530
531 return (status);
532 }
533
534
535 /*
536 * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".
537 */