]>
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 */
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 */
68 int fds
[2]; /* Open file descriptors */
69 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
71 int count
; /* Number of lines in file */
77 * Do uncompressed file tests...
80 status
= read_write_tests(0);
84 * Do compressed file tests...
89 status
+= read_write_tests(1);
90 #endif /* HAVE_LIBZ */
93 * Do uncompressed random I/O tests...
96 status
+= random_tests();
100 * Test fdopen and close without reading...
106 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
109 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
117 * Able to open file, now close without reading. If we don't return
118 * before the alarm fires, that is a failure and we will crash on the
123 fputs("cupsFileClose(no read): ", stdout
);
127 cupsFileClose(fdfile
);
135 * Count lines in euc-jp.txt, rewind, then count again.
138 fputs("\ncupsFileOpen(\"../data/euc-jp.txt\", \"r\"): ", stdout
);
140 if ((fp
= cupsFileOpen("../data/euc-jp.txt", "r")) == NULL
)
148 fputs("cupsFileGets: ", stdout
);
150 if ((count
= count_lines(fp
)) != 15184)
152 printf("FAIL (got %d lines, expected 15184)\n", count
);
158 fputs("cupsFileRewind: ", stdout
);
160 if (cupsFileRewind(fp
) != 0)
168 fputs("cupsFileGets: ", stdout
);
170 if ((count
= count_lines(fp
)) != 15184)
172 printf("FAIL (got %d lines, expected 15184)\n", count
);
184 * Test path functions...
187 fputs("\ncupsFileFind: ", stdout
);
189 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
190 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
192 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
193 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
195 printf("PASS (%s)\n", filename
);
203 * Summarize the results and return...
207 puts("\nALL TESTS PASSED!");
209 printf("\n%d TEST(S) FAILED!\n", status
);
214 * Cat the filename on the command-line...
217 char line
[1024]; /* Line from file */
219 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
228 while (cupsFileGets(fp
, line
, sizeof(line
)))
231 if (!cupsFileEOF(fp
))
243 * 'count_lines()' - Count the number of lines in a file.
246 static int /* O - Number of lines */
247 count_lines(cups_file_t
*fp
) /* I - File to read from */
249 int count
; /* Number of lines */
250 char line
[1024]; /* Line buffer */
253 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
260 * 'random_tests()' - Do random access tests.
263 static int /* O - Status */
266 int status
, /* Status of tests */
267 pass
, /* Current pass */
268 count
, /* Number of records read */
269 record
, /* Current record */
270 num_records
; /* Number of records */
271 ssize_t pos
, /* Position in file */
272 expected
; /* Expected position in file */
273 cups_file_t
*fp
; /* File */
274 char buffer
[512]; /* Data buffer */
278 * Run 4 passes, each time appending to a data file and then reopening the
279 * file for reading to validate random records in the file.
282 for (status
= 0, pass
= 0; pass
< 4; pass
++)
285 * cupsFileOpen(append)
288 printf("\ncupsFileOpen(append %d): ", pass
);
290 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
292 printf("FAIL (%s)\n", strerror(errno
));
303 expected
= 256 * sizeof(buffer
) * pass
;
305 fputs("cupsFileTell(): ", stdout
);
306 if ((pos
= cupsFileTell(fp
)) != expected
)
308 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
309 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
320 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
321 for (record
= 0; record
< 256; record
++)
323 memset(buffer
, record
, sizeof(buffer
));
324 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < sizeof(buffer
))
330 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
341 expected
+= 256 * sizeof(buffer
);
343 fputs("cupsFileTell(): ", stdout
);
344 if ((pos
= cupsFileTell(fp
)) != expected
)
346 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
347 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
360 printf("\ncupsFileOpen(read %d): ", pass
);
362 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
364 printf("FAIL (%s)\n", strerror(errno
));
372 * cupsFileSeek, cupsFileRead
375 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
377 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256,
378 record
= CUPS_RAND() % num_records
;
380 count
--, record
= (record
+ (CUPS_RAND() & 31) - 16 + num_records
) %
384 * The last record is always the first...
391 * Try reading the data for the specified record, and validate the
395 expected
= sizeof(buffer
) * record
;
397 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
399 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
400 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
406 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
408 printf("FAIL (%s)\n", strerror(errno
));
412 else if ((buffer
[0] & 255) != (record
& 255) ||
413 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
415 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
430 * Remove the test file...
433 unlink("testfile.dat");
436 * Return the test status...
444 * 'read_write_tests()' - Perform read/write tests.
447 static int /* O - Status */
448 read_write_tests(int compression
) /* I - Use compression? */
450 int i
; /* Looping var */
451 cups_file_t
*fp
; /* File */
452 int status
; /* Exit status */
453 char line
[1024], /* Line from file */
454 *value
; /* Directive value from line */
455 int linenum
; /* Line number */
456 unsigned char readbuf
[8192], /* Read buffer */
457 writebuf
[8192]; /* Write buffer */
458 int byte
; /* Byte from file */
459 off_t length
; /* Length of file */
460 static const char *partial_line
= "partial line";
465 * No errors so far...
471 * Initialize the write buffer with random data...
474 CUPS_SRAND(time(NULL
));
476 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
477 writebuf
[i
] = CUPS_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
< (int)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 $".