]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
2 * File test program for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "string-private.h"
15 #include "debug-private.h"
21 #endif /* HAVE_LIBZ */
34 static int count_lines(cups_file_t
*fp
);
35 static int random_tests(void);
36 static int read_write_tests(int compression
);
40 * 'main()' - Main entry.
43 int /* O - Exit status */
44 main(int argc
, /* I - Number of command-line arguments */
45 char *argv
[]) /* I - Command-line arguments */
47 int status
; /* Exit status */
48 char filename
[1024]; /* Filename buffer */
49 cups_file_t
*fp
; /* File pointer */
51 int fds
[2]; /* Open file descriptors */
52 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
54 int count
; /* Number of lines in file */
60 * Do uncompressed file tests...
63 status
= read_write_tests(0);
67 * Do compressed file tests...
72 status
+= read_write_tests(1);
73 #endif /* HAVE_LIBZ */
76 * Do uncompressed random I/O tests...
79 status
+= random_tests();
83 * Test fdopen and close without reading...
89 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
92 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
100 * Able to open file, now close without reading. If we don't return
101 * before the alarm fires, that is a failure and we will crash on the
106 fputs("cupsFileClose(no read): ", stdout
);
110 cupsFileClose(fdfile
);
118 * Count lines in psglyphs, rewind, then count again.
121 fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout
);
123 if ((fp
= cupsFileOpen("../data/media.defs", "r")) == NULL
)
131 fputs("cupsFileGets: ", stdout
);
133 if ((count
= count_lines(fp
)) != 201)
135 printf("FAIL (got %d lines, expected 201)\n", count
);
141 fputs("cupsFileRewind: ", stdout
);
143 if (cupsFileRewind(fp
) != 0)
151 fputs("cupsFileGets: ", stdout
);
153 if ((count
= count_lines(fp
)) != 201)
155 printf("FAIL (got %d lines, expected 201)\n", count
);
167 * Test path functions...
170 fputs("\ncupsFileFind: ", stdout
);
172 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
173 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
175 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
176 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
178 printf("PASS (%s)\n", filename
);
186 * Summarize the results and return...
190 puts("\nALL TESTS PASSED!");
192 printf("\n%d TEST(S) FAILED!\n", status
);
197 * Cat the filename on the command-line...
200 char line
[8192]; /* Line from file */
202 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
211 while (cupsFileGets(fp
, line
, sizeof(line
)))
214 if (!cupsFileEOF(fp
))
224 while ((bytes
= cupsFileRead(fp
, line
, sizeof(line
))) > 0)
225 printf("%s: %d bytes\n", argv
[1], (int)bytes
);
228 printf("%s: EOF\n", argv
[1]);
241 * 'count_lines()' - Count the number of lines in a file.
244 static int /* O - Number of lines */
245 count_lines(cups_file_t
*fp
) /* I - File to read from */
247 int count
; /* Number of lines */
248 char line
[1024]; /* Line buffer */
251 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
258 * 'random_tests()' - Do random access tests.
261 static int /* O - Status */
264 int status
, /* Status of tests */
265 pass
, /* Current pass */
266 count
, /* Number of records read */
267 record
, /* Current record */
268 num_records
; /* Number of records */
269 off_t pos
; /* Position in file */
270 ssize_t expected
; /* Expected position in file */
271 cups_file_t
*fp
; /* File */
272 char buffer
[512]; /* Data buffer */
276 * Run 4 passes, each time appending to a data file and then reopening the
277 * file for reading to validate random records in the file.
280 for (status
= 0, pass
= 0; pass
< 4; pass
++)
283 * cupsFileOpen(append)
286 printf("\ncupsFileOpen(append %d): ", pass
);
288 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
290 printf("FAIL (%s)\n", strerror(errno
));
301 expected
= 256 * (ssize_t
)sizeof(buffer
) * pass
;
303 fputs("cupsFileTell(): ", stdout
);
304 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
306 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
307 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
318 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
319 for (record
= 0; record
< 256; record
++)
321 memset(buffer
, record
, sizeof(buffer
));
322 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < (ssize_t
)sizeof(buffer
))
328 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
339 expected
+= 256 * (ssize_t
)sizeof(buffer
);
341 fputs("cupsFileTell(): ", stdout
);
342 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
344 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
345 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
358 printf("\ncupsFileOpen(read %d): ", pass
);
360 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
362 printf("FAIL (%s)\n", strerror(errno
));
370 * cupsFileSeek, cupsFileRead
373 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
375 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256, record
= ((int)CUPS_RAND() & 65535) % num_records
;
377 count
--, record
= (record
+ ((int)CUPS_RAND() & 31) - 16 + num_records
) % num_records
)
380 * The last record is always the first...
387 * Try reading the data for the specified record, and validate the
391 expected
= (ssize_t
)sizeof(buffer
) * record
;
393 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
395 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
396 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
402 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
404 printf("FAIL (%s)\n", strerror(errno
));
408 else if ((buffer
[0] & 255) != (record
& 255) ||
409 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
411 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
426 * Remove the test file...
429 unlink("testfile.dat");
432 * Return the test status...
440 * 'read_write_tests()' - Perform read/write tests.
443 static int /* O - Status */
444 read_write_tests(int compression
) /* I - Use compression? */
446 int i
; /* Looping var */
447 cups_file_t
*fp
; /* File */
448 int status
; /* Exit status */
449 char line
[1024], /* Line from file */
450 *value
; /* Directive value from line */
451 int linenum
; /* Line number */
452 unsigned char readbuf
[8192], /* Read buffer */
453 writebuf
[8192]; /* Write buffer */
454 int byte
; /* Byte from file */
455 ssize_t bytes
; /* Number of bytes read/written */
456 off_t length
; /* Length of file */
457 static const char *partial_line
= "partial line";
462 * No errors so far...
468 * Initialize the write buffer with random data...
471 CUPS_SRAND((unsigned)time(NULL
));
473 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
474 writebuf
[i
] = (unsigned char)CUPS_RAND();
477 * cupsFileOpen(write)
480 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
482 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
483 compression
? "w9" : "w");
489 * cupsFileCompression()
492 fputs("cupsFileCompression(): ", stdout
);
494 if (cupsFileCompression(fp
) == compression
)
498 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
507 fputs("cupsFilePuts(): ", stdout
);
509 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
513 printf("FAIL (%s)\n", strerror(errno
));
521 fputs("cupsFilePrintf(): ", stdout
);
523 for (i
= 0; i
< 1000; i
++)
524 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
531 printf("FAIL (%s)\n", strerror(errno
));
539 fputs("cupsFilePutChar(): ", stdout
);
541 for (i
= 0; i
< 256; i
++)
542 if (cupsFilePutChar(fp
, i
) < 0)
549 printf("FAIL (%s)\n", strerror(errno
));
557 fputs("cupsFileWrite(): ", stdout
);
559 for (i
= 0; i
< 10000; i
++)
560 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
567 printf("FAIL (%s)\n", strerror(errno
));
572 * cupsFilePuts() with partial line...
575 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
577 if (cupsFilePuts(fp
, partial_line
) > 0)
581 printf("FAIL (%s)\n", strerror(errno
));
589 fputs("cupsFileTell(): ", stdout
);
591 if ((length
= cupsFileTell(fp
)) == 81933283)
595 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
603 fputs("cupsFileClose(): ", stdout
);
605 if (!cupsFileClose(fp
))
609 printf("FAIL (%s)\n", strerror(errno
));
615 printf("FAIL (%s)\n", strerror(errno
));
623 fputs("\ncupsFileOpen(read): ", stdout
);
625 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
634 fputs("cupsFileGets(): ", stdout
);
636 if (cupsFileGets(fp
, line
, sizeof(line
)))
642 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
648 printf("FAIL (%s)\n", strerror(errno
));
653 * cupsFileCompression()
656 fputs("cupsFileCompression(): ", stdout
);
658 if (cupsFileCompression(fp
) == compression
)
662 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
673 fputs("cupsFileGetConf(): ", stdout
);
675 for (i
= 0, value
= NULL
; i
< 1000; i
++)
676 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
678 else if (_cups_strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
686 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
687 line
, value
? value
: "(null)");
692 printf("FAIL (%s)\n", strerror(errno
));
700 fputs("cupsFileGetChar(): ", stdout
);
702 for (i
= 0, byte
= 0; i
< 256; i
++)
703 if ((byte
= cupsFileGetChar(fp
)) != i
)
710 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
715 printf("FAIL (%s)\n", strerror(errno
));
723 fputs("cupsFileRead(): ", stdout
);
725 for (i
= 0, bytes
= 0; i
< 10000; i
++)
726 if ((bytes
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
728 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
735 printf("FAIL (Pass %d, ", i
);
737 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
738 if (readbuf
[i
] != writebuf
[i
])
741 printf("match failed at offset %d - got %02X, expected %02X)\n",
742 i
, readbuf
[i
], writebuf
[i
]);
746 printf("FAIL (%s)\n", strerror(errno
));
751 * cupsFileGetChar() with partial line...
754 fputs("cupsFileGetChar(partial line): ", stdout
);
756 for (i
= 0; i
< (int)strlen(partial_line
); i
++)
757 if ((byte
= cupsFileGetChar(fp
)) < 0)
759 else if (byte
!= partial_line
[i
])
762 if (!partial_line
[i
])
766 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
774 fputs("cupsFileTell(): ", stdout
);
776 if ((length
= cupsFileTell(fp
)) == 81933283)
780 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
788 fputs("cupsFileClose(): ", stdout
);
790 if (!cupsFileClose(fp
))
794 printf("FAIL (%s)\n", strerror(errno
));
800 printf("FAIL (%s)\n", strerror(errno
));
805 * Remove the test file...
809 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
812 * Return the test status...