]> 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/*
b423cd4c 2 * "$Id: testfile.c 5194 2006-02-27 20:57:07Z mike $"
ef416fc2 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"
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);
4400e98d 89 if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
90 cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
fa73b229 91 printf("PASS (%s)\n", filename);
92 else
93 {
94 puts("FAIL");
95 status ++;
96 }
ef416fc2 97
fa73b229 98 /*
99 * Summarize the results and return...
100 */
101
102 if (!status)
103 puts("\nALL TESTS PASSED!");
104 else
105 printf("\n%d TEST(S) FAILED!\n", status);
106 }
ef416fc2 107 else
fa73b229 108 {
109 /*
110 * Cat the filename on the command-line...
111 */
112
113 cups_file_t *fp; /* File pointer */
114 char line[1024]; /* Line from file */
115
116
117 if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
118 {
119 perror(argv[1]);
120 status = 1;
121 }
122 else
123 {
124 status = 0;
125
126 while (cupsFileGets(fp, line, sizeof(line)))
127 puts(line);
128
129 if (!cupsFileEOF(fp))
130 perror(argv[1]);
131
132 cupsFileClose(fp);
133 }
134 }
ef416fc2 135
136 return (status);
137}
138
139
140/*
141 * 'read_write_tests()' - Perform read/write tests.
142 */
143
144static int /* O - Status */
145read_write_tests(int compression) /* I - Use compression? */
146{
147 int i; /* Looping var */
148 cups_file_t *fp; /* First file */
149 int status; /* Exit status */
150 char line[1024], /* Line from file */
151 *value; /* Directive value from line */
152 int linenum; /* Line number */
153 unsigned char readbuf[8192], /* Read buffer */
154 writebuf[8192]; /* Write buffer */
155 int byte; /* Byte from file */
156
157
158 /*
159 * No errors so far...
160 */
161
162 status = 0;
163
164 /*
165 * Initialize the write buffer with random data...
166 */
167
168 srand(time(NULL));
169 for (i = 0; i < (int)sizeof(writebuf); i ++)
170 writebuf[i] = rand();
171
172 /*
173 * cupsFileOpen(write)
174 */
175
176 printf("cupsFileOpen(write%s): ", compression ? " compressed" : "");
177
178 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat",
179 compression ? "w9" : "w");
180 if (fp)
181 {
182 puts("PASS");
183
184 /*
185 * cupsFileCompression()
186 */
187
188 fputs("cupsFileCompression(): ", stdout);
189
190 if (cupsFileCompression(fp) == compression)
191 puts("PASS");
192 else
193 {
194 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
195 compression);
196 status ++;
197 }
198
199 /*
200 * cupsFilePuts()
201 */
202
203 fputs("cupsFilePuts(): ", stdout);
204
205 if (cupsFilePuts(fp, "# Hello, World\n") > 0)
206 puts("PASS");
207 else
208 {
209 printf("FAIL (%s)\n", strerror(errno));
210 status ++;
211 }
212
213 /*
214 * cupsFilePrintf()
215 */
216
217 fputs("cupsFilePrintf(): ", stdout);
218
219 for (i = 0; i < 1000; i ++)
220 if (cupsFilePrintf(fp, "TestLine %d\n", i) < 0)
221 break;
222
223 if (i >= 1000)
224 puts("PASS");
225 else
226 {
227 printf("FAIL (%s)\n", strerror(errno));
228 status ++;
229 }
230
231 /*
232 * cupsFilePutChar()
233 */
234
235 fputs("cupsFilePutChar(): ", stdout);
236
237 for (i = 0; i < 256; i ++)
238 if (cupsFilePutChar(fp, i) < 0)
239 break;
240
241 if (i >= 256)
242 puts("PASS");
243 else
244 {
245 printf("FAIL (%s)\n", strerror(errno));
246 status ++;
247 }
248
249 /*
250 * cupsFileWrite()
251 */
252
253 fputs("cupsFileWrite(): ", stdout);
254
255 for (i = 0; i < 100; i ++)
256 if (cupsFileWrite(fp, (char *)writebuf, sizeof(writebuf)) < 0)
257 break;
258
259 if (i >= 100)
260 puts("PASS");
261 else
262 {
263 printf("FAIL (%s)\n", strerror(errno));
264 status ++;
265 }
266
267 /*
268 * cupsFileClose()
269 */
270
271 fputs("cupsFileClose(): ", stdout);
272
273 if (!cupsFileClose(fp))
274 puts("PASS");
275 else
276 {
277 printf("FAIL (%s)\n", strerror(errno));
278 status ++;
279 }
280 }
281 else
282 {
283 printf("FAIL (%s)\n", strerror(errno));
284 status ++;
285 }
286
287 /*
288 * cupsFileOpen(read)
289 */
290
291 fputs("cupsFileOpen(read): ", stdout);
292
293 fp = cupsFileOpen(compression ? "testfile.dat.gz" : "testfile.dat", "r");
294 if (fp)
295 {
296 puts("PASS");
297
298 /*
299 * cupsFileGets()
300 */
301
302 fputs("cupsFileGets(): ", stdout);
303
304 if (cupsFileGets(fp, line, sizeof(line)))
305 {
306 if (line[0] == '#')
307 puts("PASS");
308 else
309 {
310 printf("FAIL (Got line \"%s\", expected comment line)\n", line);
311 status ++;
312 }
313 }
314 else
315 {
316 printf("FAIL (%s)\n", strerror(errno));
317 status ++;
318 }
319
320 /*
321 * cupsFileCompression()
322 */
323
324 fputs("cupsFileCompression(): ", stdout);
325
326 if (cupsFileCompression(fp) == compression)
327 puts("PASS");
328 else
329 {
330 printf("FAIL (Got %d, expected %d)\n", cupsFileCompression(fp),
331 compression);
332 status ++;
333 }
334
335 /*
336 * cupsFileGetConf()
337 */
338
339 linenum = 1;
340
341 fputs("cupsFileGetConf(): ", stdout);
342
343 for (i = 0; i < 1000; i ++)
344 if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
345 break;
346 else if (strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
347 linenum != (i + 2))
348 break;
349
350 if (i >= 1000)
351 puts("PASS");
352 else if (line[0])
353 {
354 printf("FAIL (Line %d, directive \"%s\", value \"%s\")\n", linenum,
355 line, value ? value : "(null)");
356 status ++;
357 }
358 else
359 {
360 printf("FAIL (%s)\n", strerror(errno));
361 status ++;
362 }
363
364 /*
365 * cupsGetChar()
366 */
367
368 fputs("cupsGetChar(): ", stdout);
369
370 for (i = 0; i < 256; i ++)
371 if ((byte = cupsFileGetChar(fp)) != i)
372 break;
373
374 if (i >= 256)
375 puts("PASS");
376 else if (byte >= 0)
377 {
378 printf("FAIL (Got %d, expected %d)\n", byte, i);
379 status ++;
380 }
381 else
382 {
383 printf("FAIL (%s)\n", strerror(errno));
384 status ++;
385 }
386
387 /*
388 * cupsFileRead()
389 */
390
391 fputs("cupsFileRead(): ", stdout);
392
393 for (i = 0; i < 100; i ++)
394 if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
395 break;
396 else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
397 break;
398
399 if (i >= 100)
400 puts("PASS");
401 else if (byte > 0)
402 {
403 printf("FAIL (Pass %d, ", i);
404
405 for (i = 0; i < (int)sizeof(readbuf); i ++)
406 if (readbuf[i] != writebuf[i])
407 break;
408
409 printf("match failed at offset %d - got %02X, expected %02X)\n",
410 i, readbuf[i], writebuf[i]);
411 }
412 else
413 {
414 printf("FAIL (%s)\n", strerror(errno));
415 status ++;
416 }
417
418 /*
419 * cupsFileClose()
420 */
421
422 fputs("cupsFileClose(): ", stdout);
423
424 if (!cupsFileClose(fp))
425 puts("PASS");
426 else
427 {
428 printf("FAIL (%s)\n", strerror(errno));
429 status ++;
430 }
431 }
432 else
433 {
434 printf("FAIL (%s)\n", strerror(errno));
435 status ++;
436 }
437
438 /*
439 * Return the test status...
440 */
441
442 return (status);
443}
444
445
446/*
b423cd4c 447 * End of "$Id: testfile.c 5194 2006-02-27 20:57:07Z mike $".
ef416fc2 448 */