]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testfile.c
2 * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $"
4 * File test program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * main() - Main entry.
20 * random_tests() - Do random access tests.
21 * read_write_tests() - Perform read/write tests.
25 * Include necessary headers...
37 #endif /* HAVE_LIBZ */
46 static int random_tests(void);
47 static int read_write_tests(int compression
);
51 * 'main()' - Main entry.
54 int /* O - Exit status */
55 main(int argc
, /* I - Number of command-line arguments */
56 char *argv
[]) /* I - Command-line arguments */
58 int status
; /* Exit status */
59 char filename
[1024]; /* Filename buffer */
60 int fds
[2]; /* Open file descriptors */
61 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
67 * Do uncompressed file tests...
70 status
= read_write_tests(0);
74 * Do compressed file tests...
79 status
+= read_write_tests(1);
80 #endif /* HAVE_LIBZ */
83 * Do uncompressed random I/O tests...
86 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
);
123 * Test path functions...
126 fputs("\ncupsFileFind: ", stdout
);
128 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
129 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
131 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
132 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
134 printf("PASS (%s)\n", filename
);
142 * Summarize the results and return...
146 puts("\nALL TESTS PASSED!");
148 printf("\n%d TEST(S) FAILED!\n", status
);
153 * Cat the filename on the command-line...
156 cups_file_t
*fp
; /* File pointer */
157 char line
[1024]; /* Line from file */
160 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
169 while (cupsFileGets(fp
, line
, sizeof(line
)))
172 if (!cupsFileEOF(fp
))
184 * 'random_tests()' - Do random access tests.
187 static int /* O - Status */
190 int status
, /* Status of tests */
191 pass
, /* Current pass */
192 count
, /* Number of records read */
193 record
, /* Current record */
194 num_records
; /* Number of records */
195 ssize_t pos
, /* Position in file */
196 expected
; /* Expected position in file */
197 cups_file_t
*fp
; /* File */
198 char buffer
[512]; /* Data buffer */
202 * Run 4 passes, each time appending to a data file and then reopening the
203 * file for reading to validate random records in the file.
206 for (status
= 0, pass
= 0; pass
< 4; pass
++)
209 * cupsFileOpen(append)
212 printf("\ncupsFileOpen(append %d): ", pass
);
214 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
216 printf("FAIL (%s)\n", strerror(errno
));
227 expected
= 256 * sizeof(buffer
) * pass
;
229 fputs("cupsFileTell(): ", stdout
);
230 if ((pos
= cupsFileTell(fp
)) != expected
)
232 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
233 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
244 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
245 for (record
= 0; record
< 256; record
++)
247 memset(buffer
, record
, sizeof(buffer
));
248 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < sizeof(buffer
))
254 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
265 expected
+= 256 * sizeof(buffer
);
267 fputs("cupsFileTell(): ", stdout
);
268 if ((pos
= cupsFileTell(fp
)) != expected
)
270 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
271 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
284 printf("\ncupsFileOpen(read %d): ", pass
);
286 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
288 printf("FAIL (%s)\n", strerror(errno
));
296 * cupsFileSeek, cupsFileRead
299 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
301 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256,
302 record
= rand() % num_records
;
304 count
--, record
= (record
+ (rand() & 31) - 16 + num_records
) %
308 * The last record is always the first...
315 * Try reading the data for the specified record, and validate the
319 expected
= sizeof(buffer
) * record
;
321 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
323 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
324 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
330 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
332 printf("FAIL (%s)\n", strerror(errno
));
336 else if ((buffer
[0] & 255) != (record
& 255) ||
337 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
339 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
354 * Remove the test file...
357 unlink("testfile.dat");
360 * Return the test status...
368 * 'read_write_tests()' - Perform read/write tests.
371 static int /* O - Status */
372 read_write_tests(int compression
) /* I - Use compression? */
374 int i
; /* Looping var */
375 cups_file_t
*fp
; /* File */
376 int status
; /* Exit status */
377 char line
[1024], /* Line from file */
378 *value
; /* Directive value from line */
379 int linenum
; /* Line number */
380 unsigned char readbuf
[8192], /* Read buffer */
381 writebuf
[8192]; /* Write buffer */
382 int byte
; /* Byte from file */
383 off_t length
; /* Length of file */
384 static const char *partial_line
= "partial line";
389 * No errors so far...
395 * Initialize the write buffer with random data...
399 srand((unsigned)time(NULL
));
404 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
405 writebuf
[i
] = rand();
408 * cupsFileOpen(write)
411 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
413 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
414 compression
? "w9" : "w");
420 * cupsFileCompression()
423 fputs("cupsFileCompression(): ", stdout
);
425 if (cupsFileCompression(fp
) == compression
)
429 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
438 fputs("cupsFilePuts(): ", stdout
);
440 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
444 printf("FAIL (%s)\n", strerror(errno
));
452 fputs("cupsFilePrintf(): ", stdout
);
454 for (i
= 0; i
< 1000; i
++)
455 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
462 printf("FAIL (%s)\n", strerror(errno
));
470 fputs("cupsFilePutChar(): ", stdout
);
472 for (i
= 0; i
< 256; i
++)
473 if (cupsFilePutChar(fp
, i
) < 0)
480 printf("FAIL (%s)\n", strerror(errno
));
488 fputs("cupsFileWrite(): ", stdout
);
490 for (i
= 0; i
< 10000; i
++)
491 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
498 printf("FAIL (%s)\n", strerror(errno
));
503 * cupsFilePuts() with partial line...
506 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
508 if (cupsFilePuts(fp
, partial_line
) > 0)
512 printf("FAIL (%s)\n", strerror(errno
));
520 fputs("cupsFileTell(): ", stdout
);
522 if ((length
= cupsFileTell(fp
)) == 81933283)
526 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
534 fputs("cupsFileClose(): ", stdout
);
536 if (!cupsFileClose(fp
))
540 printf("FAIL (%s)\n", strerror(errno
));
546 printf("FAIL (%s)\n", strerror(errno
));
554 fputs("\ncupsFileOpen(read): ", stdout
);
556 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
565 fputs("cupsFileGets(): ", stdout
);
567 if (cupsFileGets(fp
, line
, sizeof(line
)))
573 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
579 printf("FAIL (%s)\n", strerror(errno
));
584 * cupsFileCompression()
587 fputs("cupsFileCompression(): ", stdout
);
589 if (cupsFileCompression(fp
) == compression
)
593 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
604 fputs("cupsFileGetConf(): ", stdout
);
606 for (i
= 0; i
< 1000; i
++)
607 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
609 else if (strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
617 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
618 line
, value
? value
: "(null)");
623 printf("FAIL (%s)\n", strerror(errno
));
631 fputs("cupsFileGetChar(): ", stdout
);
633 for (i
= 0; i
< 256; i
++)
634 if ((byte
= cupsFileGetChar(fp
)) != i
)
641 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
646 printf("FAIL (%s)\n", strerror(errno
));
654 fputs("cupsFileRead(): ", stdout
);
656 for (i
= 0; i
< 10000; i
++)
657 if ((byte
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
659 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
666 printf("FAIL (Pass %d, ", i
);
668 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
669 if (readbuf
[i
] != writebuf
[i
])
672 printf("match failed at offset %d - got %02X, expected %02X)\n",
673 i
, readbuf
[i
], writebuf
[i
]);
677 printf("FAIL (%s)\n", strerror(errno
));
682 * cupsFileGetChar() with partial line...
685 fputs("cupsFileGetChar(partial line): ", stdout
);
687 for (i
= 0; i
< strlen(partial_line
); i
++)
688 if ((byte
= cupsFileGetChar(fp
)) < 0)
690 else if (byte
!= partial_line
[i
])
693 if (!partial_line
[i
])
697 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
705 fputs("cupsFileTell(): ", stdout
);
707 if ((length
= cupsFileTell(fp
)) == 81933283)
711 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
719 fputs("cupsFileClose(): ", stdout
);
721 if (!cupsFileClose(fp
))
725 printf("FAIL (%s)\n", strerror(errno
));
731 printf("FAIL (%s)\n", strerror(errno
));
736 * Remove the test file...
739 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
742 * Return the test status...
750 * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".