]>
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 * read_write_tests() - Perform read/write tests.
24 * Include necessary headers...
36 #endif /* HAVE_LIBZ */
44 static int random_tests(void);
45 static int read_write_tests(int compression
);
49 * 'main()' - Main entry.
52 int /* O - Exit status */
53 main(int argc
, /* I - Number of command-line arguments */
54 char *argv
[]) /* I - Command-line arguments */
56 int status
; /* Exit status */
57 char filename
[1024]; /* Filename buffer */
58 int fds
[2]; /* Open file descriptors */
59 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
65 * Do uncompressed file tests...
68 status
= read_write_tests(0);
72 * Do compressed file tests...
77 status
+= read_write_tests(1);
78 #endif /* HAVE_LIBZ */
81 * Do uncompressed random I/O tests...
84 status
= random_tests();
87 * Test fdopen and close without reading...
93 fputs("\ncupsFileOpenFd(fd, \"r\"): ", stdout
);
96 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
104 * Able to open file, now close without reading. If we don't return
105 * before the alarm fires, that is a failure and we will crash on the
110 fputs("cupsFileClose(no read): ", stdout
);
114 cupsFileClose(fdfile
);
121 * Test path functions...
124 fputs("\ncupsFileFind: ", stdout
);
126 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
127 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
129 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
130 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
132 printf("PASS (%s)\n", filename
);
140 * Summarize the results and return...
144 puts("\nALL TESTS PASSED!");
146 printf("\n%d TEST(S) FAILED!\n", status
);
151 * Cat the filename on the command-line...
154 cups_file_t
*fp
; /* File pointer */
155 char line
[1024]; /* Line from file */
158 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
167 while (cupsFileGets(fp
, line
, sizeof(line
)))
170 if (!cupsFileEOF(fp
))
182 * 'random_tests()' - Do random access tests.
185 static int /* O - Status */
188 int status
, /* Status of tests */
189 pass
, /* Current pass */
190 count
, /* Number of records read */
191 record
, /* Current record */
192 num_records
; /* Number of records */
193 ssize_t pos
, /* Position in file */
194 expected
; /* Expected position in file */
195 cups_file_t
*fp
; /* File */
196 char buffer
[512]; /* Data buffer */
200 * Run 4 passes, each time appending to a data file and then reopening the
201 * file for reading to validate random records in the file.
204 for (status
= 0, pass
= 0; pass
< 4; pass
++)
207 * cupsFileOpen(append)
210 printf("\ncupsFileOpen(append %d): ", pass
);
212 if ((fp
= cupsFileOpen("testfile.dat", "a")) == NULL
)
214 printf("FAIL (%s)\n", strerror(errno
));
225 expected
= 256 * sizeof(buffer
) * pass
;
227 fputs("cupsFileTell(): ", stdout
);
228 if ((pos
= cupsFileTell(fp
)) != expected
)
230 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
231 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
242 fputs("cupsFileWrite(256 512-byte records): ", stdout
);
243 for (record
= 0; record
< 256; record
++)
245 memset(buffer
, record
, sizeof(buffer
));
246 if (cupsFileWrite(fp
, buffer
, sizeof(buffer
)) < sizeof(buffer
))
252 printf("FAIL (%d: %s)\n", record
, strerror(errno
));
263 expected
+= 256 * sizeof(buffer
);
265 fputs("cupsFileTell(): ", stdout
);
266 if ((pos
= cupsFileTell(fp
)) != expected
)
268 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
269 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
282 printf("\ncupsFileOpen(read %d): ", pass
);
284 if ((fp
= cupsFileOpen("testfile.dat", "r")) == NULL
)
286 printf("FAIL (%s)\n", strerror(errno
));
294 * cupsFileSeek, cupsFileRead
297 fputs("cupsFileSeek(), cupsFileRead(): ", stdout
);
299 for (num_records
= (pass
+ 1) * 256, count
= (pass
+ 1) * 256,
300 record
= rand() % num_records
;
302 count
--, record
= (record
+ (rand() & 31) - 16 + num_records
) %
306 * The last record is always the first...
313 * Try reading the data for the specified record, and validate the
317 expected
= sizeof(buffer
) * record
;
319 if ((pos
= cupsFileSeek(fp
, expected
)) != expected
)
321 printf("FAIL (" CUPS_LLFMT
" instead of " CUPS_LLFMT
")\n",
322 CUPS_LLCAST pos
, CUPS_LLCAST expected
);
328 if (cupsFileRead(fp
, buffer
, sizeof(buffer
)) != sizeof(buffer
))
330 printf("FAIL (%s)\n", strerror(errno
));
334 else if ((buffer
[0] & 255) != (record
& 255) ||
335 memcmp(buffer
, buffer
+ 1, sizeof(buffer
) - 1))
337 printf("FAIL (Bad Data - %d instead of %d)\n", buffer
[0] & 255,
352 * Remove the test file...
355 unlink("testfile.dat");
358 * Return the test status...
366 * 'read_write_tests()' - Perform read/write tests.
369 static int /* O - Status */
370 read_write_tests(int compression
) /* I - Use compression? */
372 int i
; /* Looping var */
373 cups_file_t
*fp
; /* File */
374 int status
; /* Exit status */
375 char line
[1024], /* Line from file */
376 *value
; /* Directive value from line */
377 int linenum
; /* Line number */
378 unsigned char readbuf
[8192], /* Read buffer */
379 writebuf
[8192]; /* Write buffer */
380 int byte
; /* Byte from file */
381 off_t length
; /* Length of file */
382 static const char *partial_line
= "partial line";
387 * No errors so far...
393 * Initialize the write buffer with random data...
397 srand((unsigned)time(NULL
));
402 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
403 writebuf
[i
] = rand();
406 * cupsFileOpen(write)
409 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
411 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
412 compression
? "w9" : "w");
418 * cupsFileCompression()
421 fputs("cupsFileCompression(): ", stdout
);
423 if (cupsFileCompression(fp
) == compression
)
427 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
436 fputs("cupsFilePuts(): ", stdout
);
438 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
442 printf("FAIL (%s)\n", strerror(errno
));
450 fputs("cupsFilePrintf(): ", stdout
);
452 for (i
= 0; i
< 1000; i
++)
453 if (cupsFilePrintf(fp
, "TestLine %03d\n", i
) < 0)
460 printf("FAIL (%s)\n", strerror(errno
));
468 fputs("cupsFilePutChar(): ", stdout
);
470 for (i
= 0; i
< 256; i
++)
471 if (cupsFilePutChar(fp
, i
) < 0)
478 printf("FAIL (%s)\n", strerror(errno
));
486 fputs("cupsFileWrite(): ", stdout
);
488 for (i
= 0; i
< 10000; i
++)
489 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
496 printf("FAIL (%s)\n", strerror(errno
));
501 * cupsFilePuts() with partial line...
504 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
506 if (cupsFilePuts(fp
, partial_line
) > 0)
510 printf("FAIL (%s)\n", strerror(errno
));
518 fputs("cupsFileTell(): ", stdout
);
520 if ((length
= cupsFileTell(fp
)) == 81933283)
524 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
532 fputs("cupsFileClose(): ", stdout
);
534 if (!cupsFileClose(fp
))
538 printf("FAIL (%s)\n", strerror(errno
));
544 printf("FAIL (%s)\n", strerror(errno
));
552 fputs("\ncupsFileOpen(read): ", stdout
);
554 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
563 fputs("cupsFileGets(): ", stdout
);
565 if (cupsFileGets(fp
, line
, sizeof(line
)))
571 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
577 printf("FAIL (%s)\n", strerror(errno
));
582 * cupsFileCompression()
585 fputs("cupsFileCompression(): ", stdout
);
587 if (cupsFileCompression(fp
) == compression
)
591 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
602 fputs("cupsFileGetConf(): ", stdout
);
604 for (i
= 0; i
< 1000; i
++)
605 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
607 else if (strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
615 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
616 line
, value
? value
: "(null)");
621 printf("FAIL (%s)\n", strerror(errno
));
629 fputs("cupsFileGetChar(): ", stdout
);
631 for (i
= 0; i
< 256; i
++)
632 if ((byte
= cupsFileGetChar(fp
)) != i
)
639 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
644 printf("FAIL (%s)\n", strerror(errno
));
652 fputs("cupsFileRead(): ", stdout
);
654 for (i
= 0; i
< 10000; i
++)
655 if ((byte
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
657 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
664 printf("FAIL (Pass %d, ", i
);
666 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
667 if (readbuf
[i
] != writebuf
[i
])
670 printf("match failed at offset %d - got %02X, expected %02X)\n",
671 i
, readbuf
[i
], writebuf
[i
]);
675 printf("FAIL (%s)\n", strerror(errno
));
680 * cupsFileGetChar() with partial line...
683 fputs("cupsFileGetChar(partial line): ", stdout
);
685 for (i
= 0; i
< strlen(partial_line
); i
++)
686 if ((byte
= cupsFileGetChar(fp
)) < 0)
688 else if (byte
!= partial_line
[i
])
691 if (!partial_line
[i
])
695 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
703 fputs("cupsFileTell(): ", stdout
);
705 if ((length
= cupsFileTell(fp
)) == 81933283)
709 printf("FAIL (" CUPS_LLFMT
" instead of 81933283)\n", CUPS_LLCAST length
);
717 fputs("cupsFileClose(): ", stdout
);
719 if (!cupsFileClose(fp
))
723 printf("FAIL (%s)\n", strerror(errno
));
729 printf("FAIL (%s)\n", strerror(errno
));
734 * Remove the test file...
737 unlink(compression
? "testfile.dat.gz" : "testfile.dat");
740 * Return the test status...
748 * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".