]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testfile.c
Merge CUPS 1.4svn-r7493.
[thirdparty/cups.git] / cups / testfile.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $"
ef416fc2 3 *
4 * File test program for the Common UNIX Printing System (CUPS).
5 *
ae71f5de 6 * Copyright 2007-2008 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
c277e2f8
MS
19 * main() - Main entry.
20 * read_write_tests() - Perform read/write tests.
ef416fc2 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"
b423cd4c 34#ifdef HAVE_LIBZ
35# include <zlib.h>
36#endif /* HAVE_LIBZ */
ae71f5de 37#include <fcntl.h>
ef416fc2 38
39
40/*
41 * Local functions...
42 */
43
634763e8 44static int random_tests(void);
ef416fc2 45static int read_write_tests(int compression);
46
47
48/*
49 * 'main()' - Main entry.
50 */
51
52int /* O - Exit status */
53main(int argc, /* I - Number of command-line arguments */
54 char *argv[]) /* I - Command-line arguments */
55{
ae71f5de
MS
56 int status; /* Exit status */
57 char filename[1024]; /* Filename buffer */
58 int fds[2]; /* Open file descriptors */
59 cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */
ef416fc2 60
61
fa73b229 62 if (argc == 1)
63 {
64 /*
65 * Do uncompressed file tests...
66 */
ef416fc2 67
fa73b229 68 status = read_write_tests(0);
ef416fc2 69
70#ifdef HAVE_LIBZ
fa73b229 71 /*
72 * Do compressed file tests...
73 */
ef416fc2 74
fa73b229 75 putchar('\n');
ef416fc2 76
fa73b229 77 status += read_write_tests(1);
ef416fc2 78#endif /* HAVE_LIBZ */
79
634763e8
MS
80 /*
81 * Do uncompressed random I/O tests...
82 */
83
84 status = random_tests();
85
ae71f5de
MS
86 /*
87 * Test fdopen and close without reading...
88 */
89
90 pipe(fds);
91 close(fds[1]);
92
634763e8 93 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout);
ae71f5de
MS
94 fflush(stdout);
95
96 if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL)
97 {
98 puts("FAIL");
99 status ++;
100 }
101 else
102 {
103 /*
104 * Able to open file, now close without reading. If we don't return
105 * before the alarm fires, that is a failure and we will crash on the
106 * alarm signal...
107 */
108
109 puts("PASS");
110 fputs("cupsFileClose(no read): ", stdout);
111 fflush(stdout);
112
113 alarm(5);
114 cupsFileClose(fdfile);
115 alarm(0);
116
117 puts("PASS");
118 }
119
fa73b229 120 /*
121 * Test path functions...
122 */
123
634763e8 124 fputs("\ncupsFileFind: ", stdout);
b86bc4cf 125#ifdef WIN32
126 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) &&
127 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename)))
128#else
4400e98d 129 if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
130 cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
b86bc4cf 131#endif /* WIN32 */
fa73b229 132 printf("PASS (%s)\n", filename);
133 else
134 {
135 puts("FAIL");
136 status ++;
137 }
ef416fc2 138
fa73b229 139 /*
140 * Summarize the results and return...
141 */
142
143 if (!status)
144 puts("\nALL TESTS PASSED!");
145 else
146 printf("\n%d TEST(S) FAILED!\n", status);
147 }
ef416fc2 148 else
fa73b229 149 {
150 /*
151 * Cat the filename on the command-line...
152 */
153
154 cups_file_t *fp; /* File pointer */
155 char line[1024]; /* Line from file */
156
157
158 if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
159 {
160 perror(argv[1]);
161 status = 1;
162 }
163 else
164 {
165 status = 0;
166
167 while (cupsFileGets(fp, line, sizeof(line)))
168 puts(line);
169
170 if (!cupsFileEOF(fp))
171 perror(argv[1]);
172
173 cupsFileClose(fp);
174 }
175 }
ef416fc2 176
177 return (status);
178}
179
180
634763e8
MS
181/*
182 * 'random_tests()' - Do random access tests.
183 */
184
185static int /* O - Status */
186random_tests(void)
187{
188 int status, /* Status of tests */
189 pass, /* Current pass */
190 count, /* Number of records read */
191 record, /* Current record */
192 num_records; /* Number of records */
193 ssize_t pos, /* Position in file */
194 expected; /* Expected position in file */
195 cups_file_t *fp; /* File */
196 char buffer[512]; /* Data buffer */
197
198
199 /*
200 * Run 4 passes, each time appending to a data file and then reopening the
201 * file for reading to validate random records in the file.
202 */
203
204 for (status = 0, pass = 0; pass < 4; pass ++)
205 {
206 /*
207 * cupsFileOpen(append)
208 */
209
210 printf("\ncupsFileOpen(append %d): ", pass);
211
212 if ((fp = cupsFileOpen("testfile.dat", "a")) == NULL)
213 {
214 printf("FAIL (%s)\n", strerror(errno));
215 status ++;
216 break;
217 }
218 else
219 puts("PASS");
220
221 /*
222 * cupsFileTell()
223 */
224
225 expected = 256 * sizeof(buffer) * pass;
226
227 fputs("cupsFileTell(): ", stdout);
228 if ((pos = cupsFileTell(fp)) != expected)
229 {
230 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
231 CUPS_LLCAST pos, CUPS_LLCAST expected);
232 status ++;
233 break;
234 }
235 else
236 puts("PASS");
237
238 /*
239 * cupsFileWrite()
240 */
241
242 fputs("cupsFileWrite(256 512-byte records): ", stdout);
243 for (record = 0; record < 256; record ++)
244 {
245 memset(buffer, record, sizeof(buffer));
246 if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer))
247 break;
248 }
249
250 if (record < 256)
251 {
252 printf("FAIL (%d: %s)\n", record, strerror(errno));
253 status ++;
254 break;
255 }
256 else
257 puts("PASS");
258
259 /*
260 * cupsFileTell()
261 */
262
263 expected += 256 * sizeof(buffer);
264
265 fputs("cupsFileTell(): ", stdout);
266 if ((pos = cupsFileTell(fp)) != expected)
267 {
268 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
269 CUPS_LLCAST pos, CUPS_LLCAST expected);
270 status ++;
271 break;
272 }
273 else
274 puts("PASS");
275
276 cupsFileClose(fp);
277
278 /*
279 * cupsFileOpen(read)
280 */
281
282 printf("\ncupsFileOpen(read %d): ", pass);
283
284 if ((fp = cupsFileOpen("testfile.dat", "r")) == NULL)
285 {
286 printf("FAIL (%s)\n", strerror(errno));
287 status ++;
288 break;
289 }
290 else
291 puts("PASS");
292
293 /*
294 * cupsFileSeek, cupsFileRead
295 */
296
297 fputs("cupsFileSeek(), cupsFileRead(): ", stdout);
298
299 for (num_records = (pass + 1) * 256, count = (pass + 1) * 256,
300 record = rand() % num_records;
301 count > 0;
302 count --, record = (record + (rand() & 31) - 16 + num_records) %
303 num_records)
304 {
305 /*
306 * The last record is always the first...
307 */
308
309 if (count == 1)
310 record = 0;
311
312 /*
313 * Try reading the data for the specified record, and validate the
314 * contents...
315 */
316
317 expected = sizeof(buffer) * record;
318
319 if ((pos = cupsFileSeek(fp, expected)) != expected)
320 {
321 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
322 CUPS_LLCAST pos, CUPS_LLCAST expected);
323 status ++;
324 break;
325 }
326 else
327 {
328 if (cupsFileRead(fp, buffer, sizeof(buffer)) != sizeof(buffer))
329 {
330 printf("FAIL (%s)\n", strerror(errno));
331 status ++;
332 break;
333 }
334 else if ((buffer[0] & 255) != (record & 255) ||
335 memcmp(buffer, buffer + 1, sizeof(buffer) - 1))
336 {
337 printf("FAIL (Bad Data - %d instead of %d)\n", buffer[0] & 255,
338 record & 255);
339 status ++;
340 break;
341 }
342 }
343 }
344
345 if (count == 0)
346 puts("PASS");
347
348 cupsFileClose(fp);
349 }
350
351 /*
352 * Remove the test file...
353 */
354
355 unlink("testfile.dat");
356
357 /*
358 * Return the test status...
359 */
360
361 return (status);
362}
363
364
ef416fc2 365/*
366 * 'read_write_tests()' - Perform read/write tests.
367 */
368
369static int /* O - Status */
370read_write_tests(int compression) /* I - Use compression? */
371{
372 int i; /* Looping var */
634763e8 373 cups_file_t *fp; /* File */
ef416fc2 374 int status; /* Exit status */
375 char line[1024], /* Line from file */
376 *value; /* Directive value from line */
377 int linenum; /* Line number */
378 unsigned char readbuf[8192], /* Read buffer */
379 writebuf[8192]; /* Write buffer */
380 int byte; /* Byte from file */
634763e8 381 off_t length; /* Length of file */
c277e2f8
MS
382 static const char *partial_line = "partial line";
383 /* Partial line */
ef416fc2 384
385
386 /*
387 * No errors so far...
388 */
389
390 status = 0;
391
392 /*
393 * Initialize the write buffer with random data...
394 */
395
b86bc4cf 396#ifdef WIN32
397 srand((unsigned)time(NULL));
398#else
ef416fc2 399 srand(time(NULL));
b86bc4cf 400#endif /* WIN32 */
401
ef416fc2 402 for (i = 0; i < (int)sizeof(writebuf); i ++)
403 writebuf[i] = rand();
404
405 /*
406 * cupsFileOpen(write)
407 */
408
409 printf("cupsFileOpen(write%s): ", compression ? " compressed" : "");
410
411 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat",
412 compression ? "w9" : "w");
413 if (fp)
414 {
415 puts("PASS");
416
417 /*
418 * cupsFileCompression()
419 */
420
421 fputs("cupsFileCompression(): ", stdout);
422
423 if (cupsFileCompression(fp) == compression)
424 puts("PASS");
425 else
426 {
427 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
428 compression);
429 status ++;
430 }
431
432 /*
433 * cupsFilePuts()
434 */
435
436 fputs("cupsFilePuts(): ", stdout);
437
438 if (cupsFilePuts(fp, "# Hello, World\n") > 0)
439 puts("PASS");
440 else
441 {
442 printf("FAIL (%s)\n", strerror(errno));
443 status ++;
444 }
445
446 /*
447 * cupsFilePrintf()
448 */
449
450 fputs("cupsFilePrintf(): ", stdout);
451
452 for (i = 0; i < 1000; i ++)
634763e8 453 if (cupsFilePrintf(fp, "TestLine %03d\n", i) < 0)
ef416fc2 454 break;
455
456 if (i >= 1000)
457 puts("PASS");
458 else
459 {
460 printf("FAIL (%s)\n", strerror(errno));
461 status ++;
462 }
463
464 /*
465 * cupsFilePutChar()
466 */
467
468 fputs("cupsFilePutChar(): ", stdout);
469
470 for (i = 0; i < 256; i ++)
471 if (cupsFilePutChar(fp, i) < 0)
472 break;
473
474 if (i >= 256)
475 puts("PASS");
476 else
477 {
478 printf("FAIL (%s)\n", strerror(errno));
479 status ++;
480 }
481
482 /*
483 * cupsFileWrite()
484 */
485
486 fputs("cupsFileWrite(): ", stdout);
487
c277e2f8 488 for (i = 0; i < 10000; i ++)
ef416fc2 489 if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0)
490 break;
491
c277e2f8
MS
492 if (i >= 10000)
493 puts("PASS");
494 else
495 {
496 printf("FAIL (%s)\n", strerror(errno));
497 status ++;
498 }
499
500 /*
501 * cupsFilePuts() with partial line...
502 */
503
504 fputs("cupsFilePuts(\"partial line\"): ", stdout);
505
506 if (cupsFilePuts(fp, partial_line) > 0)
ef416fc2 507 puts("PASS");
508 else
509 {
510 printf("FAIL (%s)\n", strerror(errno));
511 status ++;
512 }
513
634763e8
MS
514 /*
515 * cupsFileTell()
516 */
517
518 fputs("cupsFileTell(): ", stdout);
519
520 if ((length = cupsFileTell(fp)) == 81933283)
521 puts("PASS");
522 else
523 {
524 printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
525 status ++;
526 }
527
ef416fc2 528 /*
529 * cupsFileClose()
530 */
531
532 fputs("cupsFileClose(): ", stdout);
533
534 if (!cupsFileClose(fp))
535 puts("PASS");
536 else
537 {
538 printf("FAIL (%s)\n", strerror(errno));
539 status ++;
540 }
541 }
542 else
543 {
544 printf("FAIL (%s)\n", strerror(errno));
545 status ++;
546 }
547
548 /*
549 * cupsFileOpen(read)
550 */
551
634763e8 552 fputs("\ncupsFileOpen(read): ", stdout);
ef416fc2 553
554 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r");
555 if (fp)
556 {
557 puts("PASS");
558
559 /*
560 * cupsFileGets()
561 */
562
563 fputs("cupsFileGets(): ", stdout);
564
565 if (cupsFileGets(fp, line, sizeof(line)))
566 {
567 if (line[0] == '#')
568 puts("PASS");
569 else
570 {
571 printf("FAIL (Got line \"%s\", expected comment line)\n", line);
572 status ++;
573 }
574 }
575 else
576 {
577 printf("FAIL (%s)\n", strerror(errno));
578 status ++;
579 }
580
581 /*
582 * cupsFileCompression()
583 */
584
585 fputs("cupsFileCompression(): ", stdout);
586
587 if (cupsFileCompression(fp) == compression)
588 puts("PASS");
589 else
590 {
591 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
592 compression);
593 status ++;
594 }
595
596 /*
597 * cupsFileGetConf()
598 */
599
600 linenum = 1;
601
602 fputs("cupsFileGetConf(): ", stdout);
603
604 for (i = 0; i < 1000; i ++)
605 if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
606 break;
607 else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
608 linenum != (i + 2))
609 break;
610
611 if (i >= 1000)
612 puts("PASS");
613 else if (line[0])
614 {
615 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum,
616 line, value ? value : "(null)");
617 status ++;
618 }
619 else
620 {
621 printf("FAIL (%s)\n", strerror(errno));
622 status ++;
623 }
624
625 /*
b86bc4cf 626 * cupsFileGetChar()
ef416fc2 627 */
628
b86bc4cf 629 fputs("cupsFileGetChar(): ", stdout);
630
ef416fc2 631 for (i = 0; i < 256; i ++)
632 if ((byte = cupsFileGetChar(fp)) != i)
633 break;
634
635 if (i >= 256)
636 puts("PASS");
637 else if (byte >= 0)
638 {
639 printf("FAIL (Got %d, expected %d)\n", byte, i);
640 status ++;
641 }
642 else
643 {
644 printf("FAIL (%s)\n", strerror(errno));
645 status ++;
646 }
647
648 /*
649 * cupsFileRead()
650 */
651
652 fputs("cupsFileRead(): ", stdout);
653
c277e2f8 654 for (i = 0; i < 10000; i ++)
ef416fc2 655 if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
656 break;
657 else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
658 break;
659
c277e2f8 660 if (i >= 10000)
ef416fc2 661 puts("PASS");
662 else if (byte > 0)
663 {
664 printf("FAIL (Pass %d, ", i);
665
666 for (i = 0; i < (int)sizeof(readbuf); i ++)
667 if (readbuf[i] != writebuf[i])
668 break;
669
670 printf("match failed at offset %d - got %02X, expected %02X)\n",
671 i, readbuf[i], writebuf[i]);
672 }
673 else
674 {
675 printf("FAIL (%s)\n", strerror(errno));
676 status ++;
677 }
678
c277e2f8
MS
679 /*
680 * cupsFileGetChar() with partial line...
681 */
682
683 fputs("cupsFileGetChar(partial line): ", stdout);
684
685 for (i = 0; i < strlen(partial_line); i ++)
686 if ((byte = cupsFileGetChar(fp)) < 0)
687 break;
688 else if (byte != partial_line[i])
689 break;
690
691 if (!partial_line[i])
692 puts("PASS");
693 else
694 {
695 printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]);
696 status ++;
697 }
698
634763e8
MS
699 /*
700 * cupsFileTell()
701 */
702
703 fputs("cupsFileTell(): ", stdout);
704
705 if ((length = cupsFileTell(fp)) == 81933283)
706 puts("PASS");
707 else
708 {
709 printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
710 status ++;
711 }
712
ef416fc2 713 /*
714 * cupsFileClose()
715 */
716
717 fputs("cupsFileClose(): ", stdout);
718
719 if (!cupsFileClose(fp))
720 puts("PASS");
721 else
722 {
723 printf("FAIL (%s)\n", strerror(errno));
724 status ++;
725 }
726 }
727 else
728 {
729 printf("FAIL (%s)\n", strerror(errno));
730 status ++;
731 }
732
634763e8
MS
733 /*
734 * Remove the test file...
735 */
736
737 unlink(compression ? "testfile.dat.gz" : "testfile.dat");
738
ef416fc2 739 /*
740 * Return the test status...
741 */
742
743 return (status);
744}
745
746
747/*
2e4ff8af 748 * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".
ef416fc2 749 */