]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
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 */
47 static int count_lines(cups_file_t
*fp
);
48 static int random_tests(void);
49 static int read_write_tests(int compression
);
53 * 'main()' - Main entry.
56 int /* O - Exit status */
57 main(int argc
, /* I - Number of command-line arguments */
58 char *argv
[]) /* I - Command-line arguments */
60 int status
; /* Exit status */
61 char filename
[1024]; /* Filename buffer */
62 cups_file_t
*fp
; /* File pointer */
63 int fds
[2]; /* Open file descriptors */
64 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
65 int count
; /* Number of lines in file */
71 * Do uncompressed file tests...
74 status
= read_write_tests(0);
78 * Do compressed file tests...
83 status
+= read_write_tests(1);
84 #endif /* HAVE_LIBZ */
87 * Do uncompressed random I/O tests...
90 status
+= random_tests();
93 * Test fdopen and close without reading...
99 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
102 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
110 * Able to open file, now close without reading. If we don't return
111 * before the alarm fires, that is a failure and we will crash on the
116 fputs("cupsFileClose(no read): ", stdout
);
120 cupsFileClose(fdfile
);
127 * Count lines in euc-jp.txt, rewind, then count again.
130 fputs("\ncupsFileOpen(\"../data/euc-jp.txt\", \"r\"): ", stdout
);
132 if ((fp
= cupsFileOpen("../data/euc-jp.txt", "r")) == NULL
)
140 fputs("cupsFileGets: ", stdout
);
142 if ((count
= count_lines(fp
)) != 15184)
144 printf("FAIL (got %d lines, expected 15184)\n", count
);
150 fputs("cupsFileRewind: ", stdout
);
152 if (cupsFileRewind(fp
) != 0)
160 fputs("cupsFileGets: ", stdout
);
162 if ((count
= count_lines(fp
)) != 15184)
164 printf("FAIL (got %d lines, expected 15184)\n", count
);
176 * Test path functions...
179 fputs("\ncupsFileFind: ", stdout
);
181 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
182 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
184 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
185 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
187 printf("PASS (%s)\n", filename
);
195 * Summarize the results and return...
199 puts("\nALL TESTS PASSED!");
201 printf("\n%d TEST(S) FAILED!\n", status
);
206 * Cat the filename on the command-line...
209 char line
[1024]; /* Line from file */
211 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
220 while (cupsFileGets(fp
, line
, sizeof(line
)))
223 if (!cupsFileEOF(fp
))
235 * 'count_lines()' - Count the number of lines in a file.
238 static int /* O - Number of lines */
239 count_lines(cups_file_t
*fp
) /* I - File to read from */
241 int count
; /* Number of lines */
242 char line
[1024]; /* Line buffer */
245 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
252 * 'random_tests()' - Do random access tests.
255 static int /* O - Status */
258 int status
, /* Status of tests */
259 pass
, /* Current pass */
260 count
, /* Number of records read */
261 record
, /* Current record */
262 num_records
; /* Number of records */
263 ssize_t pos
, /* Position in file */
264 expected
; /* Expected position in file */
265 cups_file_t
*fp
; /* File */
266 char buffer
[512]; /* Data buffer */
270 * Run 4 passes, each time appending to a data file and then reopening the
271 * file for reading to validate random records in the file.
274 for (status
= 0, pass
= 0; pass
< 4; pass
++)
277 * cupsFileOpen(append)
280 printf("\ncupsFileOpen(append %d): ", pass
);
282 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
284 printf("FAIL (%s)\n", strerror(errno
));
295 expected
= 256 * sizeof(buffer
) * pass
;
297 fputs("cupsFileTell(): ", stdout
);
298 if ((pos
= cupsFileTell(fp
)) != expected
)
300 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
301 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
312 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
313 for (record
= 0; record
< 256; record
++)
315 memset(buffer
, record
, sizeof(buffer
));
316 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < sizeof(buffer
))
322 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
333 expected
+= 256 * sizeof(buffer
);
335 fputs("cupsFileTell(): ", stdout
);
336 if ((pos
= cupsFileTell(fp
)) != expected
)
338 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
339 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
352 printf("\ncupsFileOpen(read %d): ", pass
);
354 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
356 printf("FAIL (%s)\n", strerror(errno
));
364 * cupsFileSeek, cupsFileRead
367 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
369 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256,
370 record
= rand() % num_records
;
372 count
--, record
= (record
+ (rand() & 31) - 16 + num_records
) %
376 * The last record is always the first...
383 * Try reading the data for the specified record, and validate the
387 expected
= sizeof(buffer
) * record
;
389 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
391 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
392 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
398 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
400 printf("FAIL (%s)\n", strerror(errno
));
404 else if ((buffer
[0] & 255) != (record
& 255) ||
405 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
407 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
422 * Remove the test file...
425 unlink("testfile.dat");
428 * Return the test status...
436 * 'read_write_tests()' - Perform read/write tests.
439 static int /* O - Status */
440 read_write_tests(int compression
) /* I - Use compression? */
442 int i
; /* Looping var */
443 cups_file_t
*fp
; /* File */
444 int status
; /* Exit status */
445 char line
[1024], /* Line from file */
446 *value
; /* Directive value from line */
447 int linenum
; /* Line number */
448 unsigned char readbuf
[8192], /* Read buffer */
449 writebuf
[8192]; /* Write buffer */
450 int byte
; /* Byte from file */
451 off_t length
; /* Length of file */
452 static const char *partial_line
= "partial line";
457 * No errors so far...
463 * Initialize the write buffer with random data...
467 srand((unsigned)time(NULL
));
472 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
473 writebuf
[i
] = rand();
476 * cupsFileOpen(write)
479 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
481 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
482 compression
? "w9" : "w");
488 * cupsFileCompression()
491 fputs("cupsFileCompression(): ", stdout
);
493 if (cupsFileCompression(fp
) == compression
)
497 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
506 fputs("cupsFilePuts(): ", stdout
);
508 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
512 printf("FAIL (%s)\n", strerror(errno
));
520 fputs("cupsFilePrintf(): ", stdout
);
522 for (i
= 0; i
< 1000; i
++)
523 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
530 printf("FAIL (%s)\n", strerror(errno
));
538 fputs("cupsFilePutChar(): ", stdout
);
540 for (i
= 0; i
< 256; i
++)
541 if (cupsFilePutChar(fp
, i
) < 0)
548 printf("FAIL (%s)\n", strerror(errno
));
556 fputs("cupsFileWrite(): ", stdout
);
558 for (i
= 0; i
< 10000; i
++)
559 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
566 printf("FAIL (%s)\n", strerror(errno
));
571 * cupsFilePuts() with partial line...
574 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
576 if (cupsFilePuts(fp
, partial_line
) > 0)
580 printf("FAIL (%s)\n", strerror(errno
));
588 fputs("cupsFileTell(): ", stdout
);
590 if ((length
= cupsFileTell(fp
)) == 81933283)
594 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
602 fputs("cupsFileClose(): ", stdout
);
604 if (!cupsFileClose(fp
))
608 printf("FAIL (%s)\n", strerror(errno
));
614 printf("FAIL (%s)\n", strerror(errno
));
622 fputs("\ncupsFileOpen(read): ", stdout
);
624 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
633 fputs("cupsFileGets(): ", stdout
);
635 if (cupsFileGets(fp
, line
, sizeof(line
)))
641 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
647 printf("FAIL (%s)\n", strerror(errno
));
652 * cupsFileCompression()
655 fputs("cupsFileCompression(): ", stdout
);
657 if (cupsFileCompression(fp
) == compression
)
661 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
672 fputs("cupsFileGetConf(): ", stdout
);
674 for (i
= 0; i
< 1000; i
++)
675 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
677 else if (strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
685 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
686 line
, value
? value
: "(null)");
691 printf("FAIL (%s)\n", strerror(errno
));
699 fputs("cupsFileGetChar(): ", stdout
);
701 for (i
= 0; i
< 256; i
++)
702 if ((byte
= cupsFileGetChar(fp
)) != i
)
709 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
714 printf("FAIL (%s)\n", strerror(errno
));
722 fputs("cupsFileRead(): ", stdout
);
724 for (i
= 0; i
< 10000; i
++)
725 if ((byte
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
727 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
734 printf("FAIL (Pass %d, ", i
);
736 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
737 if (readbuf
[i
] != writebuf
[i
])
740 printf("match failed at offset %d - got %02X, expected %02X)\n",
741 i
, readbuf
[i
], writebuf
[i
]);
745 printf("FAIL (%s)\n", strerror(errno
));
750 * cupsFileGetChar() with partial line...
753 fputs("cupsFileGetChar(partial line): ", stdout
);
755 for (i
= 0; i
< strlen(partial_line
); i
++)
756 if ((byte
= cupsFileGetChar(fp
)) < 0)
758 else if (byte
!= partial_line
[i
])
761 if (!partial_line
[i
])
765 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
773 fputs("cupsFileTell(): ", stdout
);
775 if ((length
= cupsFileTell(fp
)) == 81933283)
779 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
787 fputs("cupsFileClose(): ", stdout
);
789 if (!cupsFileClose(fp
))
793 printf("FAIL (%s)\n", strerror(errno
));
799 printf("FAIL (%s)\n", strerror(errno
));
804 * Remove the test file...
807 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
810 * Return the test status...
818 * End of "$Id: testfile.c 7720 2008-07-11 22:46:21Z mike $".