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