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