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