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