]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $" |
ef416fc2 | 3 | * |
4 | * File test program for the Common UNIX Printing System (CUPS). | |
5 | * | |
ae71f5de | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
c277e2f8 MS |
19 | * main() - Main entry. |
20 | * read_write_tests() - Perform read/write tests. | |
ef416fc2 | 21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <errno.h> | |
30 | #include <time.h> | |
31 | #include "string.h" | |
32 | #include "file.h" | |
33 | #include "debug.h" | |
b423cd4c | 34 | #ifdef HAVE_LIBZ |
35 | # include <zlib.h> | |
36 | #endif /* HAVE_LIBZ */ | |
ae71f5de | 37 | #include <fcntl.h> |
ef416fc2 | 38 | |
39 | ||
40 | /* | |
41 | * Local functions... | |
42 | */ | |
43 | ||
44 | static int read_write_tests(int compression); | |
45 | ||
46 | ||
47 | /* | |
48 | * 'main()' - Main entry. | |
49 | */ | |
50 | ||
51 | int /* O - Exit status */ | |
52 | main(int argc, /* I - Number of command-line arguments */ | |
53 | char *argv[]) /* I - Command-line arguments */ | |
54 | { | |
ae71f5de MS |
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() */ | |
ef416fc2 | 59 | |
60 | ||
fa73b229 | 61 | if (argc == 1) |
62 | { | |
63 | /* | |
64 | * Do uncompressed file tests... | |
65 | */ | |
ef416fc2 | 66 | |
fa73b229 | 67 | status = read_write_tests(0); |
ef416fc2 | 68 | |
69 | #ifdef HAVE_LIBZ | |
fa73b229 | 70 | /* |
71 | * Do compressed file tests... | |
72 | */ | |
ef416fc2 | 73 | |
fa73b229 | 74 | putchar('\n'); |
ef416fc2 | 75 | |
fa73b229 | 76 | status += read_write_tests(1); |
ef416fc2 | 77 | #endif /* HAVE_LIBZ */ |
78 | ||
ae71f5de MS |
79 | /* |
80 | * Test fdopen and close without reading... | |
81 | */ | |
82 | ||
83 | pipe(fds); | |
84 | close(fds[1]); | |
85 | ||
86 | fputs("cupsFileOpenFd(fd, \"r\"): ", stdout); | |
87 | fflush(stdout); | |
88 | ||
89 | if ((fdfile = cupsFileOpenFd(fds[0], "r")) == NULL) | |
90 | { | |
91 | puts("FAIL"); | |
92 | status ++; | |
93 | } | |
94 | else | |
95 | { | |
96 | /* | |
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 | |
99 | * alarm signal... | |
100 | */ | |
101 | ||
102 | puts("PASS"); | |
103 | fputs("cupsFileClose(no read): ", stdout); | |
104 | fflush(stdout); | |
105 | ||
106 | alarm(5); | |
107 | cupsFileClose(fdfile); | |
108 | alarm(0); | |
109 | ||
110 | puts("PASS"); | |
111 | } | |
112 | ||
fa73b229 | 113 | /* |
114 | * Test path functions... | |
115 | */ | |
116 | ||
117 | fputs("cupsFileFind: ", stdout); | |
b86bc4cf | 118 | #ifdef WIN32 |
119 | if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) && | |
120 | cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename))) | |
121 | #else | |
4400e98d | 122 | if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) && |
123 | cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename))) | |
b86bc4cf | 124 | #endif /* WIN32 */ |
fa73b229 | 125 | printf("PASS (%s)\n", filename); |
126 | else | |
127 | { | |
128 | puts("FAIL"); | |
129 | status ++; | |
130 | } | |
ef416fc2 | 131 | |
fa73b229 | 132 | /* |
133 | * Summarize the results and return... | |
134 | */ | |
135 | ||
136 | if (!status) | |
137 | puts("\nALL TESTS PASSED!"); | |
138 | else | |
139 | printf("\n%d TEST(S) FAILED!\n", status); | |
140 | } | |
ef416fc2 | 141 | else |
fa73b229 | 142 | { |
143 | /* | |
144 | * Cat the filename on the command-line... | |
145 | */ | |
146 | ||
147 | cups_file_t *fp; /* File pointer */ | |
148 | char line[1024]; /* Line from file */ | |
149 | ||
150 | ||
151 | if ((fp = cupsFileOpen(argv[1], "r")) == NULL) | |
152 | { | |
153 | perror(argv[1]); | |
154 | status = 1; | |
155 | } | |
156 | else | |
157 | { | |
158 | status = 0; | |
159 | ||
160 | while (cupsFileGets(fp, line, sizeof(line))) | |
161 | puts(line); | |
162 | ||
163 | if (!cupsFileEOF(fp)) | |
164 | perror(argv[1]); | |
165 | ||
166 | cupsFileClose(fp); | |
167 | } | |
168 | } | |
ef416fc2 | 169 | |
170 | return (status); | |
171 | } | |
172 | ||
173 | ||
174 | /* | |
175 | * 'read_write_tests()' - Perform read/write tests. | |
176 | */ | |
177 | ||
178 | static int /* O - Status */ | |
179 | read_write_tests(int compression) /* I - Use compression? */ | |
180 | { | |
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 */ | |
c277e2f8 MS |
190 | static const char *partial_line = "partial line"; |
191 | /* Partial line */ | |
ef416fc2 | 192 | |
193 | ||
194 | /* | |
195 | * No errors so far... | |
196 | */ | |
197 | ||
198 | status = 0; | |
199 | ||
200 | /* | |
201 | * Initialize the write buffer with random data... | |
202 | */ | |
203 | ||
b86bc4cf | 204 | #ifdef WIN32 |
205 | srand((unsigned)time(NULL)); | |
206 | #else | |
ef416fc2 | 207 | srand(time(NULL)); |
b86bc4cf | 208 | #endif /* WIN32 */ |
209 | ||
ef416fc2 | 210 | for (i = 0; i < (int)sizeof(writebuf); i ++) |
211 | writebuf[i] = rand(); | |
212 | ||
213 | /* | |
214 | * cupsFileOpen(write) | |
215 | */ | |
216 | ||
217 | printf("cupsFileOpen(write%s): ", compression ? " compressed" : ""); | |
218 | ||
219 | fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", | |
220 | compression ? "w9" : "w"); | |
221 | if (fp) | |
222 | { | |
223 | puts("PASS"); | |
224 | ||
225 | /* | |
226 | * cupsFileCompression() | |
227 | */ | |
228 | ||
229 | fputs("cupsFileCompression(): ", stdout); | |
230 | ||
231 | if (cupsFileCompression(fp) == compression) | |
232 | puts("PASS"); | |
233 | else | |
234 | { | |
235 | printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), | |
236 | compression); | |
237 | status ++; | |
238 | } | |
239 | ||
240 | /* | |
241 | * cupsFilePuts() | |
242 | */ | |
243 | ||
244 | fputs("cupsFilePuts(): ", stdout); | |
245 | ||
246 | if (cupsFilePuts(fp, "# Hello, World\n") > 0) | |
247 | puts("PASS"); | |
248 | else | |
249 | { | |
250 | printf("FAIL (%s)\n", strerror(errno)); | |
251 | status ++; | |
252 | } | |
253 | ||
254 | /* | |
255 | * cupsFilePrintf() | |
256 | */ | |
257 | ||
258 | fputs("cupsFilePrintf(): ", stdout); | |
259 | ||
260 | for (i = 0; i < 1000; i ++) | |
261 | if (cupsFilePrintf(fp, "TestLine %d\n", i) < 0) | |
262 | break; | |
263 | ||
264 | if (i >= 1000) | |
265 | puts("PASS"); | |
266 | else | |
267 | { | |
268 | printf("FAIL (%s)\n", strerror(errno)); | |
269 | status ++; | |
270 | } | |
271 | ||
272 | /* | |
273 | * cupsFilePutChar() | |
274 | */ | |
275 | ||
276 | fputs("cupsFilePutChar(): ", stdout); | |
277 | ||
278 | for (i = 0; i < 256; i ++) | |
279 | if (cupsFilePutChar(fp, i) < 0) | |
280 | break; | |
281 | ||
282 | if (i >= 256) | |
283 | puts("PASS"); | |
284 | else | |
285 | { | |
286 | printf("FAIL (%s)\n", strerror(errno)); | |
287 | status ++; | |
288 | } | |
289 | ||
290 | /* | |
291 | * cupsFileWrite() | |
292 | */ | |
293 | ||
294 | fputs("cupsFileWrite(): ", stdout); | |
295 | ||
c277e2f8 | 296 | for (i = 0; i < 10000; i ++) |
ef416fc2 | 297 | if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0) |
298 | break; | |
299 | ||
c277e2f8 MS |
300 | if (i >= 10000) |
301 | puts("PASS"); | |
302 | else | |
303 | { | |
304 | printf("FAIL (%s)\n", strerror(errno)); | |
305 | status ++; | |
306 | } | |
307 | ||
308 | /* | |
309 | * cupsFilePuts() with partial line... | |
310 | */ | |
311 | ||
312 | fputs("cupsFilePuts(\"partial line\"): ", stdout); | |
313 | ||
314 | if (cupsFilePuts(fp, partial_line) > 0) | |
ef416fc2 | 315 | puts("PASS"); |
316 | else | |
317 | { | |
318 | printf("FAIL (%s)\n", strerror(errno)); | |
319 | status ++; | |
320 | } | |
321 | ||
322 | /* | |
323 | * cupsFileClose() | |
324 | */ | |
325 | ||
326 | fputs("cupsFileClose(): ", stdout); | |
327 | ||
328 | if (!cupsFileClose(fp)) | |
329 | puts("PASS"); | |
330 | else | |
331 | { | |
332 | printf("FAIL (%s)\n", strerror(errno)); | |
333 | status ++; | |
334 | } | |
335 | } | |
336 | else | |
337 | { | |
338 | printf("FAIL (%s)\n", strerror(errno)); | |
339 | status ++; | |
340 | } | |
341 | ||
342 | /* | |
343 | * cupsFileOpen(read) | |
344 | */ | |
345 | ||
346 | fputs("cupsFileOpen(read): ", stdout); | |
347 | ||
348 | fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r"); | |
349 | if (fp) | |
350 | { | |
351 | puts("PASS"); | |
352 | ||
353 | /* | |
354 | * cupsFileGets() | |
355 | */ | |
356 | ||
357 | fputs("cupsFileGets(): ", stdout); | |
358 | ||
359 | if (cupsFileGets(fp, line, sizeof(line))) | |
360 | { | |
361 | if (line[0] == '#') | |
362 | puts("PASS"); | |
363 | else | |
364 | { | |
365 | printf("FAIL (Got line \"%s\", expected comment line)\n", line); | |
366 | status ++; | |
367 | } | |
368 | } | |
369 | else | |
370 | { | |
371 | printf("FAIL (%s)\n", strerror(errno)); | |
372 | status ++; | |
373 | } | |
374 | ||
375 | /* | |
376 | * cupsFileCompression() | |
377 | */ | |
378 | ||
379 | fputs("cupsFileCompression(): ", stdout); | |
380 | ||
381 | if (cupsFileCompression(fp) == compression) | |
382 | puts("PASS"); | |
383 | else | |
384 | { | |
385 | printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp), | |
386 | compression); | |
387 | status ++; | |
388 | } | |
389 | ||
390 | /* | |
391 | * cupsFileGetConf() | |
392 | */ | |
393 | ||
394 | linenum = 1; | |
395 | ||
396 | fputs("cupsFileGetConf(): ", stdout); | |
397 | ||
398 | for (i = 0; i < 1000; i ++) | |
399 | if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
400 | break; | |
401 | else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i || | |
402 | linenum != (i + 2)) | |
403 | break; | |
404 | ||
405 | if (i >= 1000) | |
406 | puts("PASS"); | |
407 | else if (line[0]) | |
408 | { | |
409 | printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum, | |
410 | line, value ? value : "(null)"); | |
411 | status ++; | |
412 | } | |
413 | else | |
414 | { | |
415 | printf("FAIL (%s)\n", strerror(errno)); | |
416 | status ++; | |
417 | } | |
418 | ||
419 | /* | |
b86bc4cf | 420 | * cupsFileGetChar() |
ef416fc2 | 421 | */ |
422 | ||
b86bc4cf | 423 | fputs("cupsFileGetChar(): ", stdout); |
424 | ||
425 | #ifdef DEBUG | |
426 | puts("\ni byte\n----- -----"); | |
427 | ||
428 | for (i = 0; i < 256; i ++) | |
429 | { | |
430 | byte = cupsFileGetChar(fp); | |
431 | ||
432 | printf("%-5d %-5d\n", i, byte); | |
ef416fc2 | 433 | |
b86bc4cf | 434 | if (byte != i) |
435 | break; | |
436 | } | |
437 | #else | |
ef416fc2 | 438 | for (i = 0; i < 256; i ++) |
439 | if ((byte = cupsFileGetChar(fp)) != i) | |
440 | break; | |
b86bc4cf | 441 | #endif /* DEBUG */ |
ef416fc2 | 442 | |
443 | if (i >= 256) | |
444 | puts("PASS"); | |
445 | else if (byte >= 0) | |
446 | { | |
447 | printf("FAIL (Got %d, expected %d)\n", byte, i); | |
448 | status ++; | |
449 | } | |
450 | else | |
451 | { | |
452 | printf("FAIL (%s)\n", strerror(errno)); | |
453 | status ++; | |
454 | } | |
455 | ||
456 | /* | |
457 | * cupsFileRead() | |
458 | */ | |
459 | ||
460 | fputs("cupsFileRead(): ", stdout); | |
461 | ||
c277e2f8 | 462 | for (i = 0; i < 10000; i ++) |
ef416fc2 | 463 | if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0) |
464 | break; | |
465 | else if (memcmp(readbuf, writebuf, sizeof(readbuf))) | |
466 | break; | |
467 | ||
c277e2f8 | 468 | if (i >= 10000) |
ef416fc2 | 469 | puts("PASS"); |
470 | else if (byte > 0) | |
471 | { | |
472 | printf("FAIL (Pass %d, ", i); | |
473 | ||
474 | for (i = 0; i < (int)sizeof(readbuf); i ++) | |
475 | if (readbuf[i] != writebuf[i]) | |
476 | break; | |
477 | ||
478 | printf("match failed at offset %d - got %02X, expected %02X)\n", | |
479 | i, readbuf[i], writebuf[i]); | |
480 | } | |
481 | else | |
482 | { | |
483 | printf("FAIL (%s)\n", strerror(errno)); | |
484 | status ++; | |
485 | } | |
486 | ||
c277e2f8 MS |
487 | /* |
488 | * cupsFileGetChar() with partial line... | |
489 | */ | |
490 | ||
491 | fputs("cupsFileGetChar(partial line): ", stdout); | |
492 | ||
493 | for (i = 0; i < strlen(partial_line); i ++) | |
494 | if ((byte = cupsFileGetChar(fp)) < 0) | |
495 | break; | |
496 | else if (byte != partial_line[i]) | |
497 | break; | |
498 | ||
499 | if (!partial_line[i]) | |
500 | puts("PASS"); | |
501 | else | |
502 | { | |
503 | printf("FAIL (got '%c', expected '%c')\n", byte, partial_line[i]); | |
504 | status ++; | |
505 | } | |
506 | ||
ef416fc2 | 507 | /* |
508 | * cupsFileClose() | |
509 | */ | |
510 | ||
511 | fputs("cupsFileClose(): ", stdout); | |
512 | ||
513 | if (!cupsFileClose(fp)) | |
514 | puts("PASS"); | |
515 | else | |
516 | { | |
517 | printf("FAIL (%s)\n", strerror(errno)); | |
518 | status ++; | |
519 | } | |
520 | } | |
521 | else | |
522 | { | |
523 | printf("FAIL (%s)\n", strerror(errno)); | |
524 | status ++; | |
525 | } | |
526 | ||
527 | /* | |
528 | * Return the test status... | |
529 | */ | |
530 | ||
531 | return (status); | |
532 | } | |
533 | ||
534 | ||
535 | /* | |
2e4ff8af | 536 | * End of "$Id: testfile.c 6962 2007-09-17 20:35:47Z mike $". |
ef416fc2 | 537 | */ |