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