]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
2 * File test program for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "string-private.h"
16 #include "debug-private.h"
32 static int count_lines(cups_file_t
*fp
);
33 static int random_tests(void);
34 static int read_write_tests(int compression
);
38 * 'main()' - Main entry.
41 int /* O - Exit status */
42 main(int argc
, /* I - Number of command-line arguments */
43 char *argv
[]) /* I - Command-line arguments */
45 int status
; /* Exit status */
46 char filename
[1024]; /* Filename buffer */
47 cups_file_t
*fp
; /* File pointer */
49 int fds
[2]; /* Open file descriptors */
50 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
52 int count
; /* Number of lines in file */
58 * Do uncompressed file tests...
61 status
= read_write_tests(0);
65 * Do compressed file tests...
70 status
+= read_write_tests(1);
71 #endif /* HAVE_LIBZ */
74 * Do uncompressed random I/O tests...
77 status
+= random_tests();
81 * Test fdopen and close without reading...
87 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
90 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
98 * Able to open file, now close without reading. If we don't return
99 * before the alarm fires, that is a failure and we will crash on the
104 fputs("cupsFileClose(no read): ", stdout
);
108 cupsFileClose(fdfile
);
116 * Count lines in test file, rewind, then count again.
119 fputs("\ncupsFileOpen(\"testfile.txt\", \"r\"): ", stdout
);
121 if ((fp
= cupsFileOpen("testfile.txt", "r")) == NULL
)
129 fputs("cupsFileGets: ", stdout
);
131 if ((count
= count_lines(fp
)) != 477)
133 printf("FAIL (got %d lines, expected 477)\n", count
);
139 fputs("cupsFileRewind: ", stdout
);
141 if (cupsFileRewind(fp
) != 0)
149 fputs("cupsFileGets: ", stdout
);
151 if ((count
= count_lines(fp
)) != 477)
153 printf("FAIL (got %d lines, expected 477)\n", count
);
165 * Test path functions...
168 fputs("\ncupsFileFind: ", stdout
);
170 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
171 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
173 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
174 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
176 printf("PASS (%s)\n", filename
);
184 * Summarize the results and return...
188 puts("\nALL TESTS PASSED!");
190 printf("\n%d TEST(S) FAILED!\n", status
);
195 * Cat the filename on the command-line...
198 char line
[8192]; /* Line from file */
200 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
209 while (cupsFileGets(fp
, line
, sizeof(line
)))
212 if (!cupsFileEOF(fp
))
222 while ((bytes
= cupsFileRead(fp
, line
, sizeof(line
))) > 0)
223 printf("%s: %d bytes\n", argv
[1], (int)bytes
);
226 printf("%s: EOF\n", argv
[1]);
239 * 'count_lines()' - Count the number of lines in a file.
242 static int /* O - Number of lines */
243 count_lines(cups_file_t
*fp
) /* I - File to read from */
245 int count
; /* Number of lines */
246 char line
[1024]; /* Line buffer */
249 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
256 * 'random_tests()' - Do random access tests.
259 static int /* O - Status */
262 int status
, /* Status of tests */
263 pass
, /* Current pass */
264 count
, /* Number of records read */
265 record
, /* Current record */
266 num_records
; /* Number of records */
267 off_t pos
; /* Position in file */
268 ssize_t expected
; /* Expected position in file */
269 cups_file_t
*fp
; /* File */
270 char buffer
[512]; /* Data buffer */
274 * Run 4 passes, each time appending to a data file and then reopening the
275 * file for reading to validate random records in the file.
278 for (status
= 0, pass
= 0; pass
< 4; pass
++)
281 * cupsFileOpen(append)
284 printf("\ncupsFileOpen(append %d): ", pass
);
286 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
288 printf("FAIL (%s)\n", strerror(errno
));
299 expected
= 256 * (ssize_t
)sizeof(buffer
) * pass
;
301 fputs("cupsFileTell(): ", stdout
);
302 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
304 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
305 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
316 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
317 for (record
= 0; record
< 256; record
++)
319 memset(buffer
, record
, sizeof(buffer
));
320 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < (ssize_t
)sizeof(buffer
))
326 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
337 expected
+= 256 * (ssize_t
)sizeof(buffer
);
339 fputs("cupsFileTell(): ", stdout
);
340 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
342 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
343 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
356 printf("\ncupsFileOpen(read %d): ", pass
);
358 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
360 printf("FAIL (%s)\n", strerror(errno
));
368 * cupsFileSeek, cupsFileRead
371 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
373 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256, record
= ((int)CUPS_RAND() & 65535) % num_records
;
375 count
--, record
= (record
+ ((int)CUPS_RAND() & 31) - 16 + num_records
) % num_records
)
378 * The last record is always the first...
385 * Try reading the data for the specified record, and validate the
389 expected
= (ssize_t
)sizeof(buffer
) * record
;
391 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
393 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
394 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
400 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
402 printf("FAIL (%s)\n", strerror(errno
));
406 else if ((buffer
[0] & 255) != (record
& 255) ||
407 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
409 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
424 * Remove the test file...
427 unlink("testfile.dat");
430 * Return the test status...
438 * 'read_write_tests()' - Perform read/write tests.
441 static int /* O - Status */
442 read_write_tests(int compression
) /* I - Use compression? */
444 int i
; /* Looping var */
445 cups_file_t
*fp
; /* File */
446 int status
; /* Exit status */
447 char line
[1024], /* Line from file */
448 *value
; /* Directive value from line */
449 int linenum
; /* Line number */
450 unsigned char readbuf
[8192], /* Read buffer */
451 writebuf
[8192]; /* Write buffer */
452 int byte
; /* Byte from file */
453 ssize_t bytes
; /* Number of bytes read/written */
454 off_t length
; /* Length of file */
455 static const char *partial_line
= "partial line";
460 * No errors so far...
466 * Initialize the write buffer with random data...
469 CUPS_SRAND((unsigned)time(NULL
));
471 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
472 writebuf
[i
] = (unsigned char)CUPS_RAND();
475 * cupsFileOpen(write)
478 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
480 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
481 compression
? "w9" : "w");
487 * cupsFileCompression()
490 fputs("cupsFileCompression(): ", stdout
);
492 if (cupsFileCompression(fp
) == compression
)
496 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
505 fputs("cupsFilePuts(): ", stdout
);
507 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
511 printf("FAIL (%s)\n", strerror(errno
));
519 fputs("cupsFilePrintf(): ", stdout
);
521 for (i
= 0; i
< 1000; i
++)
522 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
529 printf("FAIL (%s)\n", strerror(errno
));
537 fputs("cupsFilePutChar(): ", stdout
);
539 for (i
= 0; i
< 256; i
++)
540 if (cupsFilePutChar(fp
, i
) < 0)
547 printf("FAIL (%s)\n", strerror(errno
));
555 fputs("cupsFileWrite(): ", stdout
);
557 for (i
= 0; i
< 10000; i
++)
558 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
565 printf("FAIL (%s)\n", strerror(errno
));
570 * cupsFilePuts() with partial line...
573 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
575 if (cupsFilePuts(fp
, partial_line
) > 0)
579 printf("FAIL (%s)\n", strerror(errno
));
587 fputs("cupsFileTell(): ", stdout
);
589 if ((length
= cupsFileTell(fp
)) == 81933283)
593 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
601 fputs("cupsFileClose(): ", stdout
);
603 if (!cupsFileClose(fp
))
607 printf("FAIL (%s)\n", strerror(errno
));
613 printf("FAIL (%s)\n", strerror(errno
));
621 fputs("\ncupsFileOpen(read): ", stdout
);
623 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
632 fputs("cupsFileGets(): ", stdout
);
634 if (cupsFileGets(fp
, line
, sizeof(line
)))
640 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
646 printf("FAIL (%s)\n", strerror(errno
));
651 * cupsFileCompression()
654 fputs("cupsFileCompression(): ", stdout
);
656 if (cupsFileCompression(fp
) == compression
)
660 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
671 fputs("cupsFileGetConf(): ", stdout
);
673 for (i
= 0, value
= NULL
; i
< 1000; i
++)
674 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
676 else if (_cups_strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
684 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
685 line
, value
? value
: "(null)");
690 printf("FAIL (%s)\n", strerror(errno
));
698 fputs("cupsFileGetChar(): ", stdout
);
700 for (i
= 0, byte
= 0; i
< 256; i
++)
701 if ((byte
= cupsFileGetChar(fp
)) != i
)
708 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
713 printf("FAIL (%s)\n", strerror(errno
));
721 fputs("cupsFileRead(): ", stdout
);
723 for (i
= 0, bytes
= 0; i
< 10000; i
++)
724 if ((bytes
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
726 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
733 printf("FAIL (Pass %d, ", i
);
735 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
736 if (readbuf
[i
] != writebuf
[i
])
739 printf("match failed at offset %d - got %02X, expected %02X)\n",
740 i
, readbuf
[i
], writebuf
[i
]);
744 printf("FAIL (%s)\n", strerror(errno
));
749 * cupsFileGetChar() with partial line...
752 fputs("cupsFileGetChar(partial line): ", stdout
);
754 for (i
= 0; i
< (int)strlen(partial_line
); i
++)
755 if ((byte
= cupsFileGetChar(fp
)) < 0)
757 else if (byte
!= partial_line
[i
])
760 if (!partial_line
[i
])
764 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
772 fputs("cupsFileTell(): ", stdout
);
774 if ((length
= cupsFileTell(fp
)) == 81933283)
778 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
786 fputs("cupsFileClose(): ", stdout
);
788 if (!cupsFileClose(fp
))
792 printf("FAIL (%s)\n", strerror(errno
));
798 printf("FAIL (%s)\n", strerror(errno
));
803 * Remove the test file...
807 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
810 * Return the test status...