]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
9dbd8d549d357cc8953668c7dff2d6f40fc69f39
4 * File test program for CUPS.
6 * Copyright 2007-2015 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 * Include necessary headers...
22 #include "string-private.h"
23 #include "debug-private.h"
29 #endif /* HAVE_LIBZ */
42 static int count_lines(cups_file_t
*fp
);
43 static int random_tests(void);
44 static int read_write_tests(int compression
);
48 * 'main()' - Main entry.
51 int /* O - Exit status */
52 main(int argc
, /* I - Number of command-line arguments */
53 char *argv
[]) /* I - Command-line arguments */
55 int status
; /* Exit status */
56 char filename
[1024]; /* Filename buffer */
57 cups_file_t
*fp
; /* File pointer */
59 int fds
[2]; /* Open file descriptors */
60 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
62 int count
; /* Number of lines in file */
68 * Do uncompressed file tests...
71 status
= read_write_tests(0);
75 * Do compressed file tests...
80 status
+= read_write_tests(1);
81 #endif /* HAVE_LIBZ */
84 * Do uncompressed random I/O tests...
87 status
+= random_tests();
91 * Test fdopen and close without reading...
97 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
100 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
108 * Able to open file, now close without reading. If we don't return
109 * before the alarm fires, that is a failure and we will crash on the
114 fputs("cupsFileClose(no read): ", stdout
);
118 cupsFileClose(fdfile
);
126 * Count lines in psglyphs, rewind, then count again.
129 fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout
);
131 if ((fp
= cupsFileOpen("../data/media.defs", "r")) == NULL
)
139 fputs("cupsFileGets: ", stdout
);
141 if ((count
= count_lines(fp
)) != 208)
143 printf("FAIL (got %d lines, expected 208)\n", count
);
149 fputs("cupsFileRewind: ", stdout
);
151 if (cupsFileRewind(fp
) != 0)
159 fputs("cupsFileGets: ", stdout
);
161 if ((count
= count_lines(fp
)) != 208)
163 printf("FAIL (got %d lines, expected 208)\n", count
);
175 * Test path functions...
178 fputs("\ncupsFileFind: ", stdout
);
180 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
181 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
183 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
184 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
186 printf("PASS (%s)\n", filename
);
194 * Summarize the results and return...
198 puts("\nALL TESTS PASSED!");
200 printf("\n%d TEST(S) FAILED!\n", status
);
205 * Cat the filename on the command-line...
208 char line
[8192]; /* Line from file */
210 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
219 while (cupsFileGets(fp
, line
, sizeof(line
)))
222 if (!cupsFileEOF(fp
))
232 while ((bytes
= cupsFileRead(fp
, line
, sizeof(line
))) > 0)
233 printf("%s: %d bytes\n", argv
[1], (int)bytes
);
236 printf("%s: EOF\n", argv
[1]);
249 * 'count_lines()' - Count the number of lines in a file.
252 static int /* O - Number of lines */
253 count_lines(cups_file_t
*fp
) /* I - File to read from */
255 int count
; /* Number of lines */
256 char line
[1024]; /* Line buffer */
259 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
266 * 'random_tests()' - Do random access tests.
269 static int /* O - Status */
272 int status
, /* Status of tests */
273 pass
, /* Current pass */
274 count
, /* Number of records read */
275 record
, /* Current record */
276 num_records
; /* Number of records */
277 off_t pos
; /* Position in file */
278 ssize_t expected
; /* Expected position in file */
279 cups_file_t
*fp
; /* File */
280 char buffer
[512]; /* Data buffer */
284 * Run 4 passes, each time appending to a data file and then reopening the
285 * file for reading to validate random records in the file.
288 for (status
= 0, pass
= 0; pass
< 4; pass
++)
291 * cupsFileOpen(append)
294 printf("\ncupsFileOpen(append %d): ", pass
);
296 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
298 printf("FAIL (%s)\n", strerror(errno
));
309 expected
= 256 * (ssize_t
)sizeof(buffer
) * pass
;
311 fputs("cupsFileTell(): ", stdout
);
312 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
314 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
315 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
326 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
327 for (record
= 0; record
< 256; record
++)
329 memset(buffer
, record
, sizeof(buffer
));
330 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < (ssize_t
)sizeof(buffer
))
336 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
347 expected
+= 256 * (ssize_t
)sizeof(buffer
);
349 fputs("cupsFileTell(): ", stdout
);
350 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
352 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
353 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
366 printf("\ncupsFileOpen(read %d): ", pass
);
368 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
370 printf("FAIL (%s)\n", strerror(errno
));
378 * cupsFileSeek, cupsFileRead
381 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
383 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256, record
= ((int)CUPS_RAND() & 65535) % num_records
;
385 count
--, record
= (record
+ ((int)CUPS_RAND() & 31) - 16 + num_records
) % num_records
)
388 * The last record is always the first...
395 * Try reading the data for the specified record, and validate the
399 expected
= (ssize_t
)sizeof(buffer
) * record
;
401 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
403 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
404 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
410 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
412 printf("FAIL (%s)\n", strerror(errno
));
416 else if ((buffer
[0] & 255) != (record
& 255) ||
417 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
419 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
434 * Remove the test file...
437 unlink("testfile.dat");
440 * Return the test status...
448 * 'read_write_tests()' - Perform read/write tests.
451 static int /* O - Status */
452 read_write_tests(int compression
) /* I - Use compression? */
454 int i
; /* Looping var */
455 cups_file_t
*fp
; /* File */
456 int status
; /* Exit status */
457 char line
[1024], /* Line from file */
458 *value
; /* Directive value from line */
459 int linenum
; /* Line number */
460 unsigned char readbuf
[8192], /* Read buffer */
461 writebuf
[8192]; /* Write buffer */
462 int byte
; /* Byte from file */
463 ssize_t bytes
; /* Number of bytes read/written */
464 off_t length
; /* Length of file */
465 static const char *partial_line
= "partial line";
470 * No errors so far...
476 * Initialize the write buffer with random data...
479 CUPS_SRAND((unsigned)time(NULL
));
481 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
482 writebuf
[i
] = (unsigned char)CUPS_RAND();
485 * cupsFileOpen(write)
488 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
490 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
491 compression
? "w9" : "w");
497 * cupsFileCompression()
500 fputs("cupsFileCompression(): ", stdout
);
502 if (cupsFileCompression(fp
) == compression
)
506 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
515 fputs("cupsFilePuts(): ", stdout
);
517 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
521 printf("FAIL (%s)\n", strerror(errno
));
529 fputs("cupsFilePrintf(): ", stdout
);
531 for (i
= 0; i
< 1000; i
++)
532 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
539 printf("FAIL (%s)\n", strerror(errno
));
547 fputs("cupsFilePutChar(): ", stdout
);
549 for (i
= 0; i
< 256; i
++)
550 if (cupsFilePutChar(fp
, i
) < 0)
557 printf("FAIL (%s)\n", strerror(errno
));
565 fputs("cupsFileWrite(): ", stdout
);
567 for (i
= 0; i
< 10000; i
++)
568 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
575 printf("FAIL (%s)\n", strerror(errno
));
580 * cupsFilePuts() with partial line...
583 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
585 if (cupsFilePuts(fp
, partial_line
) > 0)
589 printf("FAIL (%s)\n", strerror(errno
));
597 fputs("cupsFileTell(): ", stdout
);
599 if ((length
= cupsFileTell(fp
)) == 81933283)
603 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
611 fputs("cupsFileClose(): ", stdout
);
613 if (!cupsFileClose(fp
))
617 printf("FAIL (%s)\n", strerror(errno
));
623 printf("FAIL (%s)\n", strerror(errno
));
631 fputs("\ncupsFileOpen(read): ", stdout
);
633 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
642 fputs("cupsFileGets(): ", stdout
);
644 if (cupsFileGets(fp
, line
, sizeof(line
)))
650 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
656 printf("FAIL (%s)\n", strerror(errno
));
661 * cupsFileCompression()
664 fputs("cupsFileCompression(): ", stdout
);
666 if (cupsFileCompression(fp
) == compression
)
670 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
681 fputs("cupsFileGetConf(): ", stdout
);
683 for (i
= 0, value
= NULL
; i
< 1000; i
++)
684 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
686 else if (_cups_strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
694 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
695 line
, value
? value
: "(null)");
700 printf("FAIL (%s)\n", strerror(errno
));
708 fputs("cupsFileGetChar(): ", stdout
);
710 for (i
= 0, byte
= 0; i
< 256; i
++)
711 if ((byte
= cupsFileGetChar(fp
)) != i
)
718 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
723 printf("FAIL (%s)\n", strerror(errno
));
731 fputs("cupsFileRead(): ", stdout
);
733 for (i
= 0, bytes
= 0; i
< 10000; i
++)
734 if ((bytes
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
736 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
743 printf("FAIL (Pass %d, ", i
);
745 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
746 if (readbuf
[i
] != writebuf
[i
])
749 printf("match failed at offset %d - got %02X, expected %02X)\n",
750 i
, readbuf
[i
], writebuf
[i
]);
754 printf("FAIL (%s)\n", strerror(errno
));
759 * cupsFileGetChar() with partial line...
762 fputs("cupsFileGetChar(partial line): ", stdout
);
764 for (i
= 0; i
< (int)strlen(partial_line
); i
++)
765 if ((byte
= cupsFileGetChar(fp
)) < 0)
767 else if (byte
!= partial_line
[i
])
770 if (!partial_line
[i
])
774 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
782 fputs("cupsFileTell(): ", stdout
);
784 if ((length
= cupsFileTell(fp
)) == 81933283)
788 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
796 fputs("cupsFileClose(): ", stdout
);
798 if (!cupsFileClose(fp
))
802 printf("FAIL (%s)\n", strerror(errno
));
808 printf("FAIL (%s)\n", strerror(errno
));
813 * Remove the test file...
817 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
820 * Return the test status...