]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
43751f116e5d58126ef23b035f144adb0d11047a
2 * "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $"
4 * File test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
20 * count_lines() - Count the number of lines in a file.
21 * random_tests() - Do random access tests.
22 * read_write_tests() - Perform read/write tests.
26 * Include necessary headers...
38 #endif /* HAVE_LIBZ */
51 static int count_lines(cups_file_t
*fp
);
52 static int random_tests(void);
53 static int read_write_tests(int compression
);
57 * 'main()' - Main entry.
60 int /* O - Exit status */
61 main(int argc
, /* I - Number of command-line arguments */
62 char *argv
[]) /* I - Command-line arguments */
64 int status
; /* Exit status */
65 char filename
[1024]; /* Filename buffer */
66 cups_file_t
*fp
; /* File pointer */
67 int fds
[2]; /* Open file descriptors */
68 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
69 int count
; /* Number of lines in file */
75 * Do uncompressed file tests...
78 status
= read_write_tests(0);
82 * Do compressed file tests...
87 status
+= read_write_tests(1);
88 #endif /* HAVE_LIBZ */
91 * Do uncompressed random I/O tests...
94 status
+= random_tests();
97 * Test fdopen and close without reading...
103 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
106 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
114 * Able to open file, now close without reading. If we don't return
115 * before the alarm fires, that is a failure and we will crash on the
120 fputs("cupsFileClose(no read): ", stdout
);
124 cupsFileClose(fdfile
);
131 * Count lines in euc-jp.txt, rewind, then count again.
134 fputs("\ncupsFileOpen(\"../data/euc-jp.txt\", \"r\"): ", stdout
);
136 if ((fp
= cupsFileOpen("../data/euc-jp.txt", "r")) == NULL
)
144 fputs("cupsFileGets: ", stdout
);
146 if ((count
= count_lines(fp
)) != 15184)
148 printf("FAIL (got %d lines, expected 15184)\n", count
);
154 fputs("cupsFileRewind: ", stdout
);
156 if (cupsFileRewind(fp
) != 0)
164 fputs("cupsFileGets: ", stdout
);
166 if ((count
= count_lines(fp
)) != 15184)
168 printf("FAIL (got %d lines, expected 15184)\n", count
);
180 * Test path functions...
183 fputs("\ncupsFileFind: ", stdout
);
185 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
186 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
188 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
189 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
191 printf("PASS (%s)\n", filename
);
199 * Summarize the results and return...
203 puts("\nALL TESTS PASSED!");
205 printf("\n%d TEST(S) FAILED!\n", status
);
210 * Cat the filename on the command-line...
213 char line
[1024]; /* Line from file */
215 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
224 while (cupsFileGets(fp
, line
, sizeof(line
)))
227 if (!cupsFileEOF(fp
))
239 * 'count_lines()' - Count the number of lines in a file.
242 static int /* O - Number of lines */
243 count_lines(cups_file_t
*fp
) /* I - File to read from */
245 int count
; /* Number of lines */
246 char line
[1024]; /* Line buffer */
249 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
256 * 'random_tests()' - Do random access tests.
259 static int /* O - Status */
262 int status
, /* Status of tests */
263 pass
, /* Current pass */
264 count
, /* Number of records read */
265 record
, /* Current record */
266 num_records
; /* Number of records */
267 ssize_t pos
, /* Position in file */
268 expected
; /* Expected position in file */
269 cups_file_t
*fp
; /* File */
270 char buffer
[512]; /* Data buffer */
274 * Run 4 passes, each time appending to a data file and then reopening the
275 * file for reading to validate random records in the file.
278 for (status
= 0, pass
= 0; pass
< 4; pass
++)
281 * cupsFileOpen(append)
284 printf("\ncupsFileOpen(append %d): ", pass
);
286 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
288 printf("FAIL (%s)\n", strerror(errno
));
299 expected
= 256 * sizeof(buffer
) * pass
;
301 fputs("cupsFileTell(): ", stdout
);
302 if ((pos
= cupsFileTell(fp
)) != expected
)
304 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
305 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
316 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
317 for (record
= 0; record
< 256; record
++)
319 memset(buffer
, record
, sizeof(buffer
));
320 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < sizeof(buffer
))
326 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
337 expected
+= 256 * sizeof(buffer
);
339 fputs("cupsFileTell(): ", stdout
);
340 if ((pos
= cupsFileTell(fp
)) != expected
)
342 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
343 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
356 printf("\ncupsFileOpen(read %d): ", pass
);
358 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
360 printf("FAIL (%s)\n", strerror(errno
));
368 * cupsFileSeek, cupsFileRead
371 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
373 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256,
374 record
= rand() % num_records
;
376 count
--, record
= (record
+ (rand() & 31) - 16 + num_records
) %
380 * The last record is always the first...
387 * Try reading the data for the specified record, and validate the
391 expected
= sizeof(buffer
) * record
;
393 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
395 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
396 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
402 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
404 printf("FAIL (%s)\n", strerror(errno
));
408 else if ((buffer
[0] & 255) != (record
& 255) ||
409 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
411 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
426 * Remove the test file...
429 unlink("testfile.dat");
432 * Return the test status...
440 * 'read_write_tests()' - Perform read/write tests.
443 static int /* O - Status */
444 read_write_tests(int compression
) /* I - Use compression? */
446 int i
; /* Looping var */
447 cups_file_t
*fp
; /* File */
448 int status
; /* Exit status */
449 char line
[1024], /* Line from file */
450 *value
; /* Directive value from line */
451 int linenum
; /* Line number */
452 unsigned char readbuf
[8192], /* Read buffer */
453 writebuf
[8192]; /* Write buffer */
454 int byte
; /* Byte from file */
455 off_t length
; /* Length of file */
456 static const char *partial_line
= "partial line";
461 * No errors so far...
467 * Initialize the write buffer with random data...
471 srand((unsigned)time(NULL
));
476 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
477 writebuf
[i
] = rand();
480 * cupsFileOpen(write)
483 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
485 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
486 compression
? "w9" : "w");
492 * cupsFileCompression()
495 fputs("cupsFileCompression(): ", stdout
);
497 if (cupsFileCompression(fp
) == compression
)
501 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
510 fputs("cupsFilePuts(): ", stdout
);
512 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
516 printf("FAIL (%s)\n", strerror(errno
));
524 fputs("cupsFilePrintf(): ", stdout
);
526 for (i
= 0; i
< 1000; i
++)
527 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
534 printf("FAIL (%s)\n", strerror(errno
));
542 fputs("cupsFilePutChar(): ", stdout
);
544 for (i
= 0; i
< 256; i
++)
545 if (cupsFilePutChar(fp
, i
) < 0)
552 printf("FAIL (%s)\n", strerror(errno
));
560 fputs("cupsFileWrite(): ", stdout
);
562 for (i
= 0; i
< 10000; i
++)
563 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
570 printf("FAIL (%s)\n", strerror(errno
));
575 * cupsFilePuts() with partial line...
578 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
580 if (cupsFilePuts(fp
, partial_line
) > 0)
584 printf("FAIL (%s)\n", strerror(errno
));
592 fputs("cupsFileTell(): ", stdout
);
594 if ((length
= cupsFileTell(fp
)) == 81933283)
598 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
606 fputs("cupsFileClose(): ", stdout
);
608 if (!cupsFileClose(fp
))
612 printf("FAIL (%s)\n", strerror(errno
));
618 printf("FAIL (%s)\n", strerror(errno
));
626 fputs("\ncupsFileOpen(read): ", stdout
);
628 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
637 fputs("cupsFileGets(): ", stdout
);
639 if (cupsFileGets(fp
, line
, sizeof(line
)))
645 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
651 printf("FAIL (%s)\n", strerror(errno
));
656 * cupsFileCompression()
659 fputs("cupsFileCompression(): ", stdout
);
661 if (cupsFileCompression(fp
) == compression
)
665 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
676 fputs("cupsFileGetConf(): ", stdout
);
678 for (i
= 0; i
< 1000; i
++)
679 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
681 else if (strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
689 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
690 line
, value
? value
: "(null)");
695 printf("FAIL (%s)\n", strerror(errno
));
703 fputs("cupsFileGetChar(): ", stdout
);
705 for (i
= 0; i
< 256; i
++)
706 if ((byte
= cupsFileGetChar(fp
)) != i
)
713 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
718 printf("FAIL (%s)\n", strerror(errno
));
726 fputs("cupsFileRead(): ", stdout
);
728 for (i
= 0; i
< 10000; i
++)
729 if ((byte
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
731 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
738 printf("FAIL (Pass %d, ", i
);
740 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
741 if (readbuf
[i
] != writebuf
[i
])
744 printf("match failed at offset %d - got %02X, expected %02X)\n",
745 i
, readbuf
[i
], writebuf
[i
]);
749 printf("FAIL (%s)\n", strerror(errno
));
754 * cupsFileGetChar() with partial line...
757 fputs("cupsFileGetChar(partial line): ", stdout
);
759 for (i
= 0; i
< strlen(partial_line
); i
++)
760 if ((byte
= cupsFileGetChar(fp
)) < 0)
762 else if (byte
!= partial_line
[i
])
765 if (!partial_line
[i
])
769 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
777 fputs("cupsFileTell(): ", stdout
);
779 if ((length
= cupsFileTell(fp
)) == 81933283)
783 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
791 fputs("cupsFileClose(): ", stdout
);
793 if (!cupsFileClose(fp
))
797 printf("FAIL (%s)\n", strerror(errno
));
803 printf("FAIL (%s)\n", strerror(errno
));
808 * Remove the test file...
811 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
814 * Return the test status...
822 * End of "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $".