]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
6543e0a2f9e6ee5e016e1b984edc4ce128ba9b46
2 * "$Id: testfile.c 11558 2014-02-06 18:33:34Z msweet $"
4 * File test program for CUPS.
6 * Copyright 2007-2014 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
[1024]; /* Line from file */
210 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
219 while (cupsFileGets(fp
, line
, sizeof(line
)))
222 if (!cupsFileEOF(fp
))
234 * 'count_lines()' - Count the number of lines in a file.
237 static int /* O - Number of lines */
238 count_lines(cups_file_t
*fp
) /* I - File to read from */
240 int count
; /* Number of lines */
241 char line
[1024]; /* Line buffer */
244 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
251 * 'random_tests()' - Do random access tests.
254 static int /* O - Status */
257 int status
, /* Status of tests */
258 pass
, /* Current pass */
259 count
, /* Number of records read */
260 record
, /* Current record */
261 num_records
; /* Number of records */
262 off_t pos
; /* Position in file */
263 ssize_t expected
; /* Expected position in file */
264 cups_file_t
*fp
; /* File */
265 char buffer
[512]; /* Data buffer */
269 * Run 4 passes, each time appending to a data file and then reopening the
270 * file for reading to validate random records in the file.
273 for (status
= 0, pass
= 0; pass
< 4; pass
++)
276 * cupsFileOpen(append)
279 printf("\ncupsFileOpen(append %d): ", pass
);
281 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
283 printf("FAIL (%s)\n", strerror(errno
));
294 expected
= 256 * (ssize_t
)sizeof(buffer
) * pass
;
296 fputs("cupsFileTell(): ", stdout
);
297 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
299 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
300 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
311 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
312 for (record
= 0; record
< 256; record
++)
314 memset(buffer
, record
, sizeof(buffer
));
315 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < (ssize_t
)sizeof(buffer
))
321 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
332 expected
+= 256 * (ssize_t
)sizeof(buffer
);
334 fputs("cupsFileTell(): ", stdout
);
335 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
337 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
338 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
351 printf("\ncupsFileOpen(read %d): ", pass
);
353 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
355 printf("FAIL (%s)\n", strerror(errno
));
363 * cupsFileSeek, cupsFileRead
366 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
368 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256, record
= ((int)CUPS_RAND() & 65535) % num_records
;
370 count
--, record
= (record
+ ((int)CUPS_RAND() & 31) - 16 + num_records
) % num_records
)
373 * The last record is always the first...
380 * Try reading the data for the specified record, and validate the
384 expected
= (ssize_t
)sizeof(buffer
) * record
;
386 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
388 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
389 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
395 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
397 printf("FAIL (%s)\n", strerror(errno
));
401 else if ((buffer
[0] & 255) != (record
& 255) ||
402 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
404 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
419 * Remove the test file...
422 unlink("testfile.dat");
425 * Return the test status...
433 * 'read_write_tests()' - Perform read/write tests.
436 static int /* O - Status */
437 read_write_tests(int compression
) /* I - Use compression? */
439 int i
; /* Looping var */
440 cups_file_t
*fp
; /* File */
441 int status
; /* Exit status */
442 char line
[1024], /* Line from file */
443 *value
; /* Directive value from line */
444 int linenum
; /* Line number */
445 unsigned char readbuf
[8192], /* Read buffer */
446 writebuf
[8192]; /* Write buffer */
447 int byte
; /* Byte from file */
448 ssize_t bytes
; /* Number of bytes read/written */
449 off_t length
; /* Length of file */
450 static const char *partial_line
= "partial line";
455 * No errors so far...
461 * Initialize the write buffer with random data...
464 CUPS_SRAND((unsigned)time(NULL
));
466 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
467 writebuf
[i
] = (unsigned char)CUPS_RAND();
470 * cupsFileOpen(write)
473 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
475 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
476 compression
? "w9" : "w");
482 * cupsFileCompression()
485 fputs("cupsFileCompression(): ", stdout
);
487 if (cupsFileCompression(fp
) == compression
)
491 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
500 fputs("cupsFilePuts(): ", stdout
);
502 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
506 printf("FAIL (%s)\n", strerror(errno
));
514 fputs("cupsFilePrintf(): ", stdout
);
516 for (i
= 0; i
< 1000; i
++)
517 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
524 printf("FAIL (%s)\n", strerror(errno
));
532 fputs("cupsFilePutChar(): ", stdout
);
534 for (i
= 0; i
< 256; i
++)
535 if (cupsFilePutChar(fp
, i
) < 0)
542 printf("FAIL (%s)\n", strerror(errno
));
550 fputs("cupsFileWrite(): ", stdout
);
552 for (i
= 0; i
< 10000; i
++)
553 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
560 printf("FAIL (%s)\n", strerror(errno
));
565 * cupsFilePuts() with partial line...
568 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
570 if (cupsFilePuts(fp
, partial_line
) > 0)
574 printf("FAIL (%s)\n", strerror(errno
));
582 fputs("cupsFileTell(): ", stdout
);
584 if ((length
= cupsFileTell(fp
)) == 81933283)
588 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
596 fputs("cupsFileClose(): ", stdout
);
598 if (!cupsFileClose(fp
))
602 printf("FAIL (%s)\n", strerror(errno
));
608 printf("FAIL (%s)\n", strerror(errno
));
616 fputs("\ncupsFileOpen(read): ", stdout
);
618 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
627 fputs("cupsFileGets(): ", stdout
);
629 if (cupsFileGets(fp
, line
, sizeof(line
)))
635 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
641 printf("FAIL (%s)\n", strerror(errno
));
646 * cupsFileCompression()
649 fputs("cupsFileCompression(): ", stdout
);
651 if (cupsFileCompression(fp
) == compression
)
655 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
666 fputs("cupsFileGetConf(): ", stdout
);
668 for (i
= 0, value
= NULL
; i
< 1000; i
++)
669 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
671 else if (_cups_strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
679 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
680 line
, value
? value
: "(null)");
685 printf("FAIL (%s)\n", strerror(errno
));
693 fputs("cupsFileGetChar(): ", stdout
);
695 for (i
= 0, byte
= 0; i
< 256; i
++)
696 if ((byte
= cupsFileGetChar(fp
)) != i
)
703 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
708 printf("FAIL (%s)\n", strerror(errno
));
716 fputs("cupsFileRead(): ", stdout
);
718 for (i
= 0, bytes
= 0; i
< 10000; i
++)
719 if ((bytes
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
721 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
728 printf("FAIL (Pass %d, ", i
);
730 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
731 if (readbuf
[i
] != writebuf
[i
])
734 printf("match failed at offset %d - got %02X, expected %02X)\n",
735 i
, readbuf
[i
], writebuf
[i
]);
739 printf("FAIL (%s)\n", strerror(errno
));
744 * cupsFileGetChar() with partial line...
747 fputs("cupsFileGetChar(partial line): ", stdout
);
749 for (i
= 0; i
< (int)strlen(partial_line
); i
++)
750 if ((byte
= cupsFileGetChar(fp
)) < 0)
752 else if (byte
!= partial_line
[i
])
755 if (!partial_line
[i
])
759 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
767 fputs("cupsFileTell(): ", stdout
);
769 if ((length
= cupsFileTell(fp
)) == 81933283)
773 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
781 fputs("cupsFileClose(): ", stdout
);
783 if (!cupsFileClose(fp
))
787 printf("FAIL (%s)\n", strerror(errno
));
793 printf("FAIL (%s)\n", strerror(errno
));
798 * Remove the test file...
801 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
804 * Return the test status...
812 * End of "$Id: testfile.c 11558 2014-02-06 18:33:34Z msweet $".