]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
dae50fa23847acec10d6d71b6ec5f17494efca48
2 * File test program for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "string-private.h"
21 #include "debug-private.h"
27 #endif /* HAVE_LIBZ */
40 static int count_lines(cups_file_t
*fp
);
41 static int random_tests(void);
42 static int read_write_tests(int compression
);
46 * 'main()' - Main entry.
49 int /* O - Exit status */
50 main(int argc
, /* I - Number of command-line arguments */
51 char *argv
[]) /* I - Command-line arguments */
53 int status
; /* Exit status */
54 char filename
[1024]; /* Filename buffer */
55 cups_file_t
*fp
; /* File pointer */
57 int fds
[2]; /* Open file descriptors */
58 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
60 int count
; /* Number of lines in file */
66 * Do uncompressed file tests...
69 status
= read_write_tests(0);
73 * Do compressed file tests...
78 status
+= read_write_tests(1);
79 #endif /* HAVE_LIBZ */
82 * Do uncompressed random I/O tests...
85 status
+= random_tests();
89 * Test fdopen and close without reading...
95 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
98 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
106 * Able to open file, now close without reading. If we don't return
107 * before the alarm fires, that is a failure and we will crash on the
112 fputs("cupsFileClose(no read): ", stdout
);
116 cupsFileClose(fdfile
);
124 * Count lines in psglyphs, rewind, then count again.
127 fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout
);
129 if ((fp
= cupsFileOpen("../data/media.defs", "r")) == NULL
)
137 fputs("cupsFileGets: ", stdout
);
139 if ((count
= count_lines(fp
)) != 201)
141 printf("FAIL (got %d lines, expected 201)\n", count
);
147 fputs("cupsFileRewind: ", stdout
);
149 if (cupsFileRewind(fp
) != 0)
157 fputs("cupsFileGets: ", stdout
);
159 if ((count
= count_lines(fp
)) != 201)
161 printf("FAIL (got %d lines, expected 201)\n", count
);
173 * Test path functions...
176 fputs("\ncupsFileFind: ", stdout
);
178 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
179 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
181 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
182 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
184 printf("PASS (%s)\n", filename
);
192 * Summarize the results and return...
196 puts("\nALL TESTS PASSED!");
198 printf("\n%d TEST(S) FAILED!\n", status
);
203 * Cat the filename on the command-line...
206 char line
[8192]; /* Line from file */
208 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
217 while (cupsFileGets(fp
, line
, sizeof(line
)))
220 if (!cupsFileEOF(fp
))
230 while ((bytes
= cupsFileRead(fp
, line
, sizeof(line
))) > 0)
231 printf("%s: %d bytes\n", argv
[1], (int)bytes
);
234 printf("%s: EOF\n", argv
[1]);
247 * 'count_lines()' - Count the number of lines in a file.
250 static int /* O - Number of lines */
251 count_lines(cups_file_t
*fp
) /* I - File to read from */
253 int count
; /* Number of lines */
254 char line
[1024]; /* Line buffer */
257 for (count
= 0; cupsFileGets(fp
, line
, sizeof(line
)); count
++);
264 * 'random_tests()' - Do random access tests.
267 static int /* O - Status */
270 int status
, /* Status of tests */
271 pass
, /* Current pass */
272 count
, /* Number of records read */
273 record
, /* Current record */
274 num_records
; /* Number of records */
275 off_t pos
; /* Position in file */
276 ssize_t expected
; /* Expected position in file */
277 cups_file_t
*fp
; /* File */
278 char buffer
[512]; /* Data buffer */
282 * Run 4 passes, each time appending to a data file and then reopening the
283 * file for reading to validate random records in the file.
286 for (status
= 0, pass
= 0; pass
< 4; pass
++)
289 * cupsFileOpen(append)
292 printf("\ncupsFileOpen(append %d): ", pass
);
294 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
296 printf("FAIL (%s)\n", strerror(errno
));
307 expected
= 256 * (ssize_t
)sizeof(buffer
) * pass
;
309 fputs("cupsFileTell(): ", stdout
);
310 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
312 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
313 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
324 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
325 for (record
= 0; record
< 256; record
++)
327 memset(buffer
, record
, sizeof(buffer
));
328 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < (ssize_t
)sizeof(buffer
))
334 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
345 expected
+= 256 * (ssize_t
)sizeof(buffer
);
347 fputs("cupsFileTell(): ", stdout
);
348 if ((pos
= cupsFileTell(fp
)) != (off_t
)expected
)
350 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
351 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
364 printf("\ncupsFileOpen(read %d): ", pass
);
366 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
368 printf("FAIL (%s)\n", strerror(errno
));
376 * cupsFileSeek, cupsFileRead
379 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
381 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256, record
= ((int)CUPS_RAND() & 65535) % num_records
;
383 count
--, record
= (record
+ ((int)CUPS_RAND() & 31) - 16 + num_records
) % num_records
)
386 * The last record is always the first...
393 * Try reading the data for the specified record, and validate the
397 expected
= (ssize_t
)sizeof(buffer
) * record
;
399 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
401 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
402 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
408 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
410 printf("FAIL (%s)\n", strerror(errno
));
414 else if ((buffer
[0] & 255) != (record
& 255) ||
415 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
417 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
432 * Remove the test file...
435 unlink("testfile.dat");
438 * Return the test status...
446 * 'read_write_tests()' - Perform read/write tests.
449 static int /* O - Status */
450 read_write_tests(int compression
) /* I - Use compression? */
452 int i
; /* Looping var */
453 cups_file_t
*fp
; /* File */
454 int status
; /* Exit status */
455 char line
[1024], /* Line from file */
456 *value
; /* Directive value from line */
457 int linenum
; /* Line number */
458 unsigned char readbuf
[8192], /* Read buffer */
459 writebuf
[8192]; /* Write buffer */
460 int byte
; /* Byte from file */
461 ssize_t bytes
; /* Number of bytes read/written */
462 off_t length
; /* Length of file */
463 static const char *partial_line
= "partial line";
468 * No errors so far...
474 * Initialize the write buffer with random data...
477 CUPS_SRAND((unsigned)time(NULL
));
479 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
480 writebuf
[i
] = (unsigned char)CUPS_RAND();
483 * cupsFileOpen(write)
486 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
488 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
489 compression
? "w9" : "w");
495 * cupsFileCompression()
498 fputs("cupsFileCompression(): ", stdout
);
500 if (cupsFileCompression(fp
) == compression
)
504 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
513 fputs("cupsFilePuts(): ", stdout
);
515 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
519 printf("FAIL (%s)\n", strerror(errno
));
527 fputs("cupsFilePrintf(): ", stdout
);
529 for (i
= 0; i
< 1000; i
++)
530 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
537 printf("FAIL (%s)\n", strerror(errno
));
545 fputs("cupsFilePutChar(): ", stdout
);
547 for (i
= 0; i
< 256; i
++)
548 if (cupsFilePutChar(fp
, i
) < 0)
555 printf("FAIL (%s)\n", strerror(errno
));
563 fputs("cupsFileWrite(): ", stdout
);
565 for (i
= 0; i
< 10000; i
++)
566 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
573 printf("FAIL (%s)\n", strerror(errno
));
578 * cupsFilePuts() with partial line...
581 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
583 if (cupsFilePuts(fp
, partial_line
) > 0)
587 printf("FAIL (%s)\n", strerror(errno
));
595 fputs("cupsFileTell(): ", stdout
);
597 if ((length
= cupsFileTell(fp
)) == 81933283)
601 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
609 fputs("cupsFileClose(): ", stdout
);
611 if (!cupsFileClose(fp
))
615 printf("FAIL (%s)\n", strerror(errno
));
621 printf("FAIL (%s)\n", strerror(errno
));
629 fputs("\ncupsFileOpen(read): ", stdout
);
631 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
640 fputs("cupsFileGets(): ", stdout
);
642 if (cupsFileGets(fp
, line
, sizeof(line
)))
648 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
654 printf("FAIL (%s)\n", strerror(errno
));
659 * cupsFileCompression()
662 fputs("cupsFileCompression(): ", stdout
);
664 if (cupsFileCompression(fp
) == compression
)
668 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
679 fputs("cupsFileGetConf(): ", stdout
);
681 for (i
= 0, value
= NULL
; i
< 1000; i
++)
682 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
684 else if (_cups_strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
692 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
693 line
, value
? value
: "(null)");
698 printf("FAIL (%s)\n", strerror(errno
));
706 fputs("cupsFileGetChar(): ", stdout
);
708 for (i
= 0, byte
= 0; i
< 256; i
++)
709 if ((byte
= cupsFileGetChar(fp
)) != i
)
716 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
721 printf("FAIL (%s)\n", strerror(errno
));
729 fputs("cupsFileRead(): ", stdout
);
731 for (i
= 0, bytes
= 0; i
< 10000; i
++)
732 if ((bytes
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
734 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
741 printf("FAIL (Pass %d, ", i
);
743 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
744 if (readbuf
[i
] != writebuf
[i
])
747 printf("match failed at offset %d - got %02X, expected %02X)\n",
748 i
, readbuf
[i
], writebuf
[i
]);
752 printf("FAIL (%s)\n", strerror(errno
));
757 * cupsFileGetChar() with partial line...
760 fputs("cupsFileGetChar(partial line): ", stdout
);
762 for (i
= 0; i
< (int)strlen(partial_line
); i
++)
763 if ((byte
= cupsFileGetChar(fp
)) < 0)
765 else if (byte
!= partial_line
[i
])
768 if (!partial_line
[i
])
772 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
780 fputs("cupsFileTell(): ", stdout
);
782 if ((length
= cupsFileTell(fp
)) == 81933283)
786 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
794 fputs("cupsFileClose(): ", stdout
);
796 if (!cupsFileClose(fp
))
800 printf("FAIL (%s)\n", strerror(errno
));
806 printf("FAIL (%s)\n", strerror(errno
));
811 * Remove the test file...
815 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
818 * Return the test status...