2 * "$Id: testfile.c 9793 2011-05-20 03:49:49Z mike $"
4 * File test program for CUPS.
6 * Copyright 2007-2011 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...
29 #include "string-private.h"
30 #include "debug-private.h"
36 #endif /* HAVE_LIBZ */
49 static int count_lines(cups_file_t *fp);
50 static int random_tests(void);
51 static int read_write_tests(int compression);
55 * 'main()' - Main entry.
58 int /* O - Exit status */
59 main(int argc, /* I - Number of command-line arguments */
60 char *argv[]) /* I - Command-line arguments */
62 int status; /* Exit status */
63 char filename[1024]; /* Filename buffer */
64 cups_file_t *fp; /* File pointer */
66 int fds[2]; /* Open file descriptors */
67 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();
98 * Test fdopen and close without reading...
104 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout);
107 if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL)
115 * Able to open file, now close without reading. If we don't return
116 * before the alarm fires, that is a failure and we will crash on the
121 fputs("cupsFileClose(no read): ", stdout);
125 cupsFileClose(fdfile);
133 * Count lines in psglyphs, rewind, then count again.
136 fputs("\ncupsFileOpen(\"../data/psglyphs\", \"r\"): ", stdout);
138 if ((fp = cupsFileOpen("../data/psglyphs", "r")) == NULL)
146 fputs("cupsFileGets: ", stdout);
148 if ((count = count_lines(fp)) != 1051)
150 printf("FAIL (got %d lines, expected 1051)\n", count);
156 fputs("cupsFileRewind: ", stdout);
158 if (cupsFileRewind(fp) != 0)
166 fputs("cupsFileGets: ", stdout);
168 if ((count = count_lines(fp)) != 1051)
170 printf("FAIL (got %d lines, expected 1051)\n", count);
182 * Test path functions...
185 fputs("\ncupsFileFind: ", stdout);
187 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) &&
188 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename)))
190 if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
191 cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
193 printf("PASS (%s)\n", filename);
201 * Summarize the results and return...
205 puts("\nALL TESTS PASSED!");
207 printf("\n%d TEST(S) FAILED!\n", status);
212 * Cat the filename on the command-line...
215 char line[1024]; /* Line from file */
217 if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
226 while (cupsFileGets(fp, line, sizeof(line)))
229 if (!cupsFileEOF(fp))
241 * 'count_lines()' - Count the number of lines in a file.
244 static int /* O - Number of lines */
245 count_lines(cups_file_t *fp) /* I - File to read from */
247 int count; /* Number of lines */
248 char line[1024]; /* Line buffer */
251 for (count = 0; cupsFileGets(fp, line, sizeof(line)); count ++);
258 * 'random_tests()' - Do random access tests.
261 static int /* O - Status */
264 int status, /* Status of tests */
265 pass, /* Current pass */
266 count, /* Number of records read */
267 record, /* Current record */
268 num_records; /* Number of records */
269 ssize_t pos, /* Position in file */
270 expected; /* Expected position in file */
271 cups_file_t *fp; /* File */
272 char buffer[512]; /* Data buffer */
276 * Run 4 passes, each time appending to a data file and then reopening the
277 * file for reading to validate random records in the file.
280 for (status = 0, pass = 0; pass < 4; pass ++)
283 * cupsFileOpen(append)
286 printf("\ncupsFileOpen(append %d): ", pass);
288 if ((fp = cupsFileOpen("testfile.dat", "a")) == NULL)
290 printf("FAIL (%s)\n", strerror(errno));
301 expected = 256 * sizeof(buffer) * pass;
303 fputs("cupsFileTell(): ", stdout);
304 if ((pos = cupsFileTell(fp)) != expected)
306 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
307 CUPS_LLCAST pos, CUPS_LLCAST expected);
318 fputs("cupsFileWrite(256 512-byte records): ", stdout);
319 for (record = 0; record < 256; record ++)
321 memset(buffer, record, sizeof(buffer));
322 if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer))
328 printf("FAIL (%d: %s)\n", record, strerror(errno));
339 expected += 256 * sizeof(buffer);
341 fputs("cupsFileTell(): ", stdout);
342 if ((pos = cupsFileTell(fp)) != expected)
344 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
345 CUPS_LLCAST pos, CUPS_LLCAST expected);
358 printf("\ncupsFileOpen(read %d): ", pass);
360 if ((fp = cupsFileOpen("testfile.dat", "r")) == NULL)
362 printf("FAIL (%s)\n", strerror(errno));
370 * cupsFileSeek, cupsFileRead
373 fputs("cupsFileSeek(), cupsFileRead(): ", stdout);
375 for (num_records = (pass + 1) * 256, count = (pass + 1) * 256,
376 record = CUPS_RAND() % num_records;
378 count --, record = (record + (CUPS_RAND() & 31) - 16 + num_records) %
382 * The last record is always the first...
389 * Try reading the data for the specified record, and validate the
393 expected = sizeof(buffer) * record;
395 if ((pos = cupsFileSeek(fp, expected)) != expected)
397 printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
398 CUPS_LLCAST pos, CUPS_LLCAST expected);
404 if (cupsFileRead(fp, buffer, sizeof(buffer)) != sizeof(buffer))
406 printf("FAIL (%s)\n", strerror(errno));
410 else if ((buffer[0] & 255) != (record & 255) ||
411 memcmp(buffer, buffer + 1, sizeof(buffer) - 1))
413 printf("FAIL (Bad Data - %d instead of %d)\n", buffer[0] & 255,
428 * Remove the test file...
431 unlink("testfile.dat");
434 * Return the test status...
442 * 'read_write_tests()' - Perform read/write tests.
445 static int /* O - Status */
446 read_write_tests(int compression) /* I - Use compression? */
448 int i; /* Looping var */
449 cups_file_t *fp; /* File */
450 int status; /* Exit status */
451 char line[1024], /* Line from file */
452 *value; /* Directive value from line */
453 int linenum; /* Line number */
454 unsigned char readbuf[8192], /* Read buffer */
455 writebuf[8192]; /* Write buffer */
456 int byte; /* Byte from file */
457 off_t length; /* Length of file */
458 static const char *partial_line = "partial line";
463 * No errors so far...
469 * Initialize the write buffer with random data...
472 CUPS_SRAND((unsigned)time(NULL));
474 for (i = 0; i < (int)sizeof(writebuf); i ++)
475 writebuf[i] = CUPS_RAND();
478 * cupsFileOpen(write)
481 printf("cupsFileOpen(write%s): ", compression ? " compressed" : "");
483 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat",
484 compression ? "w9" : "w");
490 * cupsFileCompression()
493 fputs("cupsFileCompression(): ", stdout);
495 if (cupsFileCompression(fp) == compression)
499 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
508 fputs("cupsFilePuts(): ", stdout);
510 if (cupsFilePuts(fp, "# Hello, World\n") > 0)
514 printf("FAIL (%s)\n", strerror(errno));
522 fputs("cupsFilePrintf(): ", stdout);
524 for (i = 0; i < 1000; i ++)
525 if (cupsFilePrintf(fp, "TestLine %03d\n", i) < 0)
532 printf("FAIL (%s)\n", strerror(errno));
540 fputs("cupsFilePutChar(): ", stdout);
542 for (i = 0; i < 256; i ++)
543 if (cupsFilePutChar(fp, i) < 0)
550 printf("FAIL (%s)\n", strerror(errno));
558 fputs("cupsFileWrite(): ", stdout);
560 for (i = 0; i < 10000; i ++)
561 if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0)
568 printf("FAIL (%s)\n", strerror(errno));
573 * cupsFilePuts() with partial line...
576 fputs("cupsFilePuts(\"partial line\"): ", stdout);
578 if (cupsFilePuts(fp, partial_line) > 0)
582 printf("FAIL (%s)\n", strerror(errno));
590 fputs("cupsFileTell(): ", stdout);
592 if ((length = cupsFileTell(fp)) == 81933283)
596 printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
604 fputs("cupsFileClose(): ", stdout);
606 if (!cupsFileClose(fp))
610 printf("FAIL (%s)\n", strerror(errno));
616 printf("FAIL (%s)\n", strerror(errno));
624 fputs("\ncupsFileOpen(read): ", stdout);
626 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r");
635 fputs("cupsFileGets(): ", stdout);
637 if (cupsFileGets(fp, line, sizeof(line)))
643 printf("FAIL (Got line \"%s\", expected comment line)\n", line);
649 printf("FAIL (%s)\n", strerror(errno));
654 * cupsFileCompression()
657 fputs("cupsFileCompression(): ", stdout);
659 if (cupsFileCompression(fp) == compression)
663 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
674 fputs("cupsFileGetConf(): ", stdout);
676 for (i = 0; i < 1000; i ++)
677 if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
679 else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
687 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum,
688 line, value ? value : "(null)");
693 printf("FAIL (%s)\n", strerror(errno));
701 fputs("cupsFileGetChar(): ", stdout);
703 for (i = 0; i < 256; i ++)
704 if ((byte = cupsFileGetChar(fp)) != i)
711 printf("FAIL (Got %d, expected %d)\n", byte, i);
716 printf("FAIL (%s)\n", strerror(errno));
724 fputs("cupsFileRead(): ", stdout);
726 for (i = 0; i < 10000; i ++)
727 if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
729 else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
736 printf("FAIL (Pass %d, ", i);
738 for (i = 0; i < (int)sizeof(readbuf); i ++)
739 if (readbuf[i] != writebuf[i])
742 printf("match failed at offset %d - got %02X, expected %02X)\n",
743 i, readbuf[i], writebuf[i]);
747 printf("FAIL (%s)\n", strerror(errno));
752 * cupsFileGetChar() with partial line...
755 fputs("cupsFileGetChar(partial line): ", stdout);
757 for (i = 0; i < (int)strlen(partial_line); i ++)
758 if ((byte = cupsFileGetChar(fp)) < 0)
760 else if (byte != partial_line[i])
763 if (!partial_line[i])
767 printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]);
775 fputs("cupsFileTell(): ", stdout);
777 if ((length = cupsFileTell(fp)) == 81933283)
781 printf("FAIL (" CUPS_LLFMT " instead of 81933283)\n", CUPS_LLCAST length);
789 fputs("cupsFileClose(): ", stdout);
791 if (!cupsFileClose(fp))
795 printf("FAIL (%s)\n", strerror(errno));
801 printf("FAIL (%s)\n", strerror(errno));
806 * Remove the test file...
809 unlink(compression ? "testfile.dat.gz" : "testfile.dat");
812 * Return the test status...
820 * End of "$Id: testfile.c 9793 2011-05-20 03:49:49Z mike $".