]>
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 read_write_tests(int compression
);
48 * 'main()' - Main entry.
51 int /* O - Exit status */
52 main(int argc
, /* I - Number of command-line arguments */
53 char *argv
[]) /* I - Command-line arguments */
55 int status
; /* Exit status */
56 char filename
[1024]; /* Filename buffer */
57 int fds
[2]; /* Open file descriptors */
58 cups_file_t
*fdfile
; /* File opened with cupsFileOpenFd() */
64 * Do uncompressed file tests...
67 status
= read_write_tests(0);
71 * Do compressed file tests...
76 status
+= read_write_tests(1);
77 #endif /* HAVE_LIBZ */
80 * Test fdopen and close without reading...
86 fputs("cupsFileOpenFd(fd, \"r\"): ", stdout
);
89 if ((fdfile
= cupsFileOpenFd(fds
[0], "r")) == NULL
)
97 * Able to open file, now close without reading. If we don't return
98 * before the alarm fires, that is a failure and we will crash on the
103 fputs("cupsFileClose(no read): ", stdout
);
107 cupsFileClose(fdfile
);
114 * Test path functions...
117 fputs("cupsFileFind: ", stdout
);
119 if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename
, sizeof(filename
)) &&
120 cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename
, sizeof(filename
)))
122 if (cupsFileFind("cat", "/bin", 1, filename
, sizeof(filename
)) &&
123 cupsFileFind("cat", "/bin:/usr/bin", 1, filename
, sizeof(filename
)))
125 printf("PASS (%s)\n", filename
);
133 * Summarize the results and return...
137 puts("\nALL TESTS PASSED!");
139 printf("\n%d TEST(S) FAILED!\n", status
);
144 * Cat the filename on the command-line...
147 cups_file_t
*fp
; /* File pointer */
148 char line
[1024]; /* Line from file */
151 if ((fp
= cupsFileOpen(argv
[1], "r")) == NULL
)
160 while (cupsFileGets(fp
, line
, sizeof(line
)))
163 if (!cupsFileEOF(fp
))
175 * 'read_write_tests()' - Perform read/write tests.
178 static int /* O - Status */
179 read_write_tests(int compression
) /* I - Use compression? */
181 int i
; /* Looping var */
182 cups_file_t
*fp
; /* First file */
183 int status
; /* Exit status */
184 char line
[1024], /* Line from file */
185 *value
; /* Directive value from line */
186 int linenum
; /* Line number */
187 unsigned char readbuf
[8192], /* Read buffer */
188 writebuf
[8192]; /* Write buffer */
189 int byte
; /* Byte from file */
190 static const char *partial_line
= "partial line";
195 * No errors so far...
201 * Initialize the write buffer with random data...
205 srand((unsigned)time(NULL
));
210 for (i
= 0; i
< (int)sizeof(writebuf
); i
++)
211 writebuf
[i
] = rand();
214 * cupsFileOpen(write)
217 printf("cupsFileOpen(write%s): ", compression
? " compressed" : "");
219 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat",
220 compression
? "w9" : "w");
226 * cupsFileCompression()
229 fputs("cupsFileCompression(): ", stdout
);
231 if (cupsFileCompression(fp
) == compression
)
235 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
244 fputs("cupsFilePuts(): ", stdout
);
246 if (cupsFilePuts(fp
, "# Hello, World\n") > 0)
250 printf("FAIL (%s)\n", strerror(errno
));
258 fputs("cupsFilePrintf(): ", stdout
);
260 for (i
= 0; i
< 1000; i
++)
261 if (cupsFilePrintf(fp
, "TestLine %d\n", i
) < 0)
268 printf("FAIL (%s)\n", strerror(errno
));
276 fputs("cupsFilePutChar(): ", stdout
);
278 for (i
= 0; i
< 256; i
++)
279 if (cupsFilePutChar(fp
, i
) < 0)
286 printf("FAIL (%s)\n", strerror(errno
));
294 fputs("cupsFileWrite(): ", stdout
);
296 for (i
= 0; i
< 10000; i
++)
297 if (cupsFileWrite(fp
, (char *)writebuf
, sizeof(writebuf
)) < 0)
304 printf("FAIL (%s)\n", strerror(errno
));
309 * cupsFilePuts() with partial line...
312 fputs("cupsFilePuts(\"partial line\"): ", stdout
);
314 if (cupsFilePuts(fp
, partial_line
) > 0)
318 printf("FAIL (%s)\n", strerror(errno
));
326 fputs("cupsFileClose(): ", stdout
);
328 if (!cupsFileClose(fp
))
332 printf("FAIL (%s)\n", strerror(errno
));
338 printf("FAIL (%s)\n", strerror(errno
));
346 fputs("cupsFileOpen(read): ", stdout
);
348 fp
= cupsFileOpen(compression
? "testfile.dat.gz" : "testfile.dat", "r");
357 fputs("cupsFileGets(): ", stdout
);
359 if (cupsFileGets(fp
, line
, sizeof(line
)))
365 printf("FAIL (Got line \"%s\", expected comment line)\n", line
);
371 printf("FAIL (%s)\n", strerror(errno
));
376 * cupsFileCompression()
379 fputs("cupsFileCompression(): ", stdout
);
381 if (cupsFileCompression(fp
) == compression
)
385 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp
),
396 fputs("cupsFileGetConf(): ", stdout
);
398 for (i
= 0; i
< 1000; i
++)
399 if (!cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
401 else if (strcasecmp(line
, "TestLine") || !value
|| atoi(value
) != i
||
409 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum
,
410 line
, value
? value
: "(null)");
415 printf("FAIL (%s)\n", strerror(errno
));
423 fputs("cupsFileGetChar(): ", stdout
);
426 puts("\ni byte\n----- -----");
428 for (i
= 0; i
< 256; i
++)
430 byte
= cupsFileGetChar(fp
);
432 printf("%-5d %-5d\n", i
, byte
);
438 for (i
= 0; i
< 256; i
++)
439 if ((byte
= cupsFileGetChar(fp
)) != i
)
447 printf("FAIL (Got %d, expected %d)\n", byte
, i
);
452 printf("FAIL (%s)\n", strerror(errno
));
460 fputs("cupsFileRead(): ", stdout
);
462 for (i
= 0; i
< 10000; i
++)
463 if ((byte
= cupsFileRead(fp
, (char *)readbuf
, sizeof(readbuf
))) < 0)
465 else if (memcmp(readbuf
, writebuf
, sizeof(readbuf
)))
472 printf("FAIL (Pass %d, ", i
);
474 for (i
= 0; i
< (int)sizeof(readbuf
); i
++)
475 if (readbuf
[i
] != writebuf
[i
])
478 printf("match failed at offset %d - got %02X, expected %02X)\n",
479 i
, readbuf
[i
], writebuf
[i
]);
483 printf("FAIL (%s)\n", strerror(errno
));
488 * cupsFileGetChar() with partial line...
491 fputs("cupsFileGetChar(partial line): ", stdout
);
493 for (i
= 0; i
< strlen(partial_line
); i
++)
494 if ((byte
= cupsFileGetChar(fp
)) < 0)
496 else if (byte
!= partial_line
[i
])
499 if (!partial_line
[i
])
503 printf("FAIL (got '%c', expected '%c')\n", byte
, partial_line
[i
]);
511 fputs("cupsFileClose(): ", stdout
);
513 if (!cupsFileClose(fp
))
517 printf("FAIL (%s)\n", strerror(errno
));
523 printf("FAIL (%s)\n", strerror(errno
));
528 * Return the test status...
536 * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $".