]>
Commit | Line | Data |
---|---|---|
09a101d6 | 1 | /* |
2 | * "$Id$" | |
3 | * | |
71e16022 | 4 | * CUPS API test program for CUPS. |
09a101d6 | 5 | * |
71e16022 | 6 | * Copyright 2007-2010 by Apple Inc. |
09a101d6 | 7 | * Copyright 2007 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
09a101d6 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
a4924f6c MS |
19 | * main() - Main entry. |
20 | * dests_equal() - Determine whether two destinations are equal. | |
09a101d6 | 21 | */ |
22 | ||
23 | /* | |
24 | * Include necessary headers... | |
25 | */ | |
26 | ||
71e16022 | 27 | #include "string-private.h" |
09a101d6 | 28 | #include "cups.h" |
71e16022 | 29 | #include <stdlib.h> |
09a101d6 | 30 | |
31 | ||
a4924f6c MS |
32 | /* |
33 | * Local functions... | |
34 | */ | |
35 | ||
36 | static int dests_equal(cups_dest_t *a, cups_dest_t *b); | |
37 | static void show_diffs(cups_dest_t *a, cups_dest_t *b); | |
38 | ||
39 | ||
09a101d6 | 40 | /* |
41 | * 'main()' - Main entry. | |
42 | */ | |
43 | ||
44 | int /* O - Exit status */ | |
45 | main(int argc, /* I - Number of command-line arguments */ | |
46 | char *argv[]) /* I - Command-line arguments */ | |
47 | { | |
48 | int status = 0, /* Exit status */ | |
a4924f6c | 49 | i, /* Looping var */ |
09a101d6 | 50 | num_dests; /* Number of destinations */ |
51 | cups_dest_t *dests, /* Destinations */ | |
a4924f6c MS |
52 | *dest, /* Current destination */ |
53 | *named_dest; /* Current named destination */ | |
09a101d6 | 54 | const char *ppdfile; /* PPD file */ |
55 | ppd_file_t *ppd; /* PPD file data */ | |
56 | int num_jobs; /* Number of jobs for queue */ | |
57 | cups_job_t *jobs; /* Jobs for queue */ | |
58 | ||
59 | ||
dfd5680b MS |
60 | if (argc > 1) |
61 | { | |
62 | /* | |
63 | * ./testcups printer file interval | |
64 | */ | |
65 | ||
66 | int interval, /* Interval between writes */ | |
67 | job_id; /* Job ID */ | |
68 | cups_file_t *fp; /* Print file */ | |
69 | char buffer[16384]; /* Read/write buffer */ | |
70 | ssize_t bytes; /* Bytes read/written */ | |
71 | ||
72 | ||
73 | if (argc != 4) | |
74 | { | |
75 | puts("Usage: ./testcups"); | |
76 | puts(" ./testcups printer file interval"); | |
77 | return (1); | |
78 | } | |
79 | ||
80 | if ((fp = cupsFileOpen(argv[2], "r")) == NULL) | |
81 | { | |
82 | printf("Unable to open \"%s\": %s\n", argv[2], strerror(errno)); | |
83 | return (1); | |
84 | } | |
85 | ||
86 | if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, argv[1], "testcups", 0, | |
87 | NULL)) <= 0) | |
88 | { | |
89 | printf("Unable to create print job on %s: %s\n", argv[1], | |
90 | cupsLastErrorString()); | |
91 | return (1); | |
92 | } | |
93 | ||
94 | interval = atoi(argv[3]); | |
95 | ||
96 | if (cupsStartDocument(CUPS_HTTP_DEFAULT, argv[1], job_id, argv[2], | |
97 | CUPS_FORMAT_AUTO, 1) != HTTP_CONTINUE) | |
98 | { | |
99 | puts("Unable to start document!"); | |
100 | return (1); | |
101 | } | |
102 | ||
103 | while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) | |
104 | { | |
105 | printf("Writing %d bytes...\n", (int)bytes); | |
106 | ||
107 | if (cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, | |
108 | bytes) != HTTP_CONTINUE) | |
109 | { | |
110 | puts("Unable to write bytes!"); | |
111 | return (1); | |
112 | } | |
113 | ||
114 | sleep(interval); | |
115 | } | |
116 | ||
117 | cupsFileClose(fp); | |
118 | ||
119 | if (cupsFinishDocument(CUPS_HTTP_DEFAULT, argv[1]) != HTTP_OK) | |
120 | { | |
121 | puts("Unable to finish document!"); | |
122 | return (1); | |
123 | } | |
124 | ||
125 | return (0); | |
126 | } | |
127 | ||
09a101d6 | 128 | /* |
129 | * cupsGetDests() | |
130 | */ | |
131 | ||
132 | fputs("cupsGetDests: ", stdout); | |
133 | fflush(stdout); | |
134 | ||
135 | num_dests = cupsGetDests(&dests); | |
136 | ||
137 | if (num_dests == 0) | |
138 | { | |
139 | puts("FAIL"); | |
140 | return (1); | |
141 | } | |
142 | else | |
a4924f6c MS |
143 | { |
144 | printf("PASS (%d dests)\n", num_dests); | |
145 | ||
146 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
147 | { | |
148 | printf(" %s", dest->name); | |
149 | ||
150 | if (dest->instance) | |
151 | printf(" /%s", dest->instance); | |
152 | ||
153 | if (dest->is_default) | |
154 | puts(" ***DEFAULT***"); | |
155 | else | |
156 | putchar('\n'); | |
157 | } | |
158 | } | |
159 | ||
160 | /* | |
161 | * cupsGetDest(NULL) | |
162 | */ | |
163 | ||
164 | fputs("cupsGetDest(NULL): ", stdout); | |
165 | fflush(stdout); | |
166 | ||
167 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL) | |
168 | { | |
169 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
170 | if (dest->is_default) | |
171 | break; | |
172 | ||
173 | if (i) | |
174 | { | |
175 | status = 1; | |
176 | puts("FAIL"); | |
177 | } | |
178 | else | |
179 | puts("PASS (no default)"); | |
180 | ||
181 | dest = NULL; | |
182 | } | |
183 | else | |
184 | printf("PASS (%s)\n", dest->name); | |
185 | ||
186 | /* | |
187 | * cupsGetNamedDest(NULL, NULL, NULL) | |
188 | */ | |
189 | ||
190 | fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout); | |
191 | fflush(stdout); | |
192 | ||
193 | if ((named_dest = cupsGetNamedDest(NULL, NULL, NULL)) == NULL || | |
194 | !dests_equal(dest, named_dest)) | |
195 | { | |
196 | if (!dest) | |
197 | puts("PASS (no default)"); | |
198 | else if (named_dest) | |
199 | { | |
200 | puts("FAIL (different values)"); | |
201 | show_diffs(dest, named_dest); | |
202 | status = 1; | |
203 | } | |
204 | else | |
205 | { | |
206 | puts("FAIL (no default)"); | |
207 | status = 1; | |
208 | } | |
209 | } | |
210 | else | |
211 | printf("PASS (%s)\n", named_dest->name); | |
212 | ||
213 | if (named_dest) | |
214 | cupsFreeDests(1, named_dest); | |
09a101d6 | 215 | |
216 | /* | |
217 | * cupsGetDest(printer) | |
218 | */ | |
219 | ||
220 | printf("cupsGetDest(\"%s\"): ", dests[num_dests / 2].name); | |
221 | fflush(stdout); | |
222 | ||
223 | if ((dest = cupsGetDest(dests[num_dests / 2].name, NULL, num_dests, | |
224 | dests)) == NULL) | |
225 | { | |
09a101d6 | 226 | puts("FAIL"); |
91c84a35 | 227 | return (1); |
09a101d6 | 228 | } |
229 | else | |
230 | puts("PASS"); | |
231 | ||
232 | /* | |
a4924f6c | 233 | * cupsGetNamedDest(NULL, printer, instance) |
09a101d6 | 234 | */ |
235 | ||
a4924f6c MS |
236 | printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest->name, |
237 | dest->instance ? dest->instance : "(null)"); | |
09a101d6 | 238 | fflush(stdout); |
239 | ||
a4924f6c MS |
240 | if ((named_dest = cupsGetNamedDest(NULL, dest->name, |
241 | dest->instance)) == NULL || | |
242 | !dests_equal(dest, named_dest)) | |
09a101d6 | 243 | { |
a4924f6c MS |
244 | if (named_dest) |
245 | { | |
246 | puts("FAIL (different values)"); | |
247 | show_diffs(dest, named_dest); | |
248 | } | |
249 | else | |
250 | puts("FAIL (no destination)"); | |
251 | ||
252 | ||
09a101d6 | 253 | status = 1; |
09a101d6 | 254 | } |
255 | else | |
256 | puts("PASS"); | |
257 | ||
a4924f6c MS |
258 | if (named_dest) |
259 | cupsFreeDests(1, named_dest); | |
260 | ||
09a101d6 | 261 | /* |
262 | * cupsPrintFile() | |
263 | */ | |
264 | ||
265 | fputs("cupsPrintFile: ", stdout); | |
266 | fflush(stdout); | |
267 | ||
426c6a59 | 268 | if (cupsPrintFile(dest->name, "../data/testprint", "Test Page", |
09a101d6 | 269 | dest->num_options, dest->options) <= 0) |
270 | { | |
426c6a59 | 271 | printf("FAIL (%s)\n", cupsLastErrorString()); |
1f0275e3 | 272 | return (1); |
09a101d6 | 273 | } |
274 | else | |
275 | puts("PASS"); | |
276 | ||
277 | /* | |
278 | * cupsGetPPD(printer) | |
279 | */ | |
280 | ||
281 | fputs("cupsGetPPD(): ", stdout); | |
282 | fflush(stdout); | |
283 | ||
284 | if ((ppdfile = cupsGetPPD(dest->name)) == NULL) | |
285 | { | |
09a101d6 | 286 | puts("FAIL"); |
287 | } | |
288 | else | |
289 | { | |
290 | puts("PASS"); | |
291 | ||
292 | /* | |
293 | * ppdOpenFile() | |
294 | */ | |
295 | ||
296 | fputs("ppdOpenFile(): ", stdout); | |
297 | fflush(stdout); | |
298 | ||
299 | if ((ppd = ppdOpenFile(ppdfile)) == NULL) | |
300 | { | |
301 | puts("FAIL"); | |
302 | return (1); | |
303 | } | |
304 | else | |
305 | puts("PASS"); | |
306 | ||
307 | ppdClose(ppd); | |
308 | unlink(ppdfile); | |
309 | } | |
310 | ||
311 | /* | |
312 | * cupsGetJobs() | |
313 | */ | |
314 | ||
315 | fputs("cupsGetJobs: ", stdout); | |
316 | fflush(stdout); | |
317 | ||
318 | num_jobs = cupsGetJobs(&jobs, NULL, 0, -1); | |
319 | ||
320 | if (num_jobs == 0) | |
321 | { | |
322 | puts("FAIL"); | |
323 | return (1); | |
324 | } | |
325 | else | |
326 | puts("PASS"); | |
327 | ||
328 | cupsFreeJobs(num_jobs, jobs); | |
329 | cupsFreeDests(num_dests, dests); | |
330 | ||
331 | return (status); | |
332 | } | |
333 | ||
334 | ||
a4924f6c MS |
335 | /* |
336 | * 'dests_equal()' - Determine whether two destinations are equal. | |
337 | */ | |
338 | ||
339 | static int /* O - 1 if equal, 0 if not equal */ | |
340 | dests_equal(cups_dest_t *a, /* I - First destination */ | |
341 | cups_dest_t *b) /* I - Second destination */ | |
342 | { | |
343 | int i; /* Looping var */ | |
344 | cups_option_t *aoption; /* Current option */ | |
345 | const char *bval; /* Option value */ | |
346 | ||
347 | ||
348 | if (a == b) | |
349 | return (1); | |
350 | ||
1f0275e3 | 351 | if (!a || !b) |
a4924f6c MS |
352 | return (0); |
353 | ||
354 | if (strcasecmp(a->name, b->name) || | |
355 | (a->instance && !b->instance) || | |
356 | (!a->instance && b->instance) || | |
357 | (a->instance && strcasecmp(a->instance, b->instance)) || | |
358 | a->num_options != b->num_options) | |
359 | return (0); | |
360 | ||
361 | for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++) | |
362 | if ((bval = cupsGetOption(aoption->name, b->num_options, | |
363 | b->options)) == NULL || | |
364 | strcmp(aoption->value, bval)) | |
365 | return (0); | |
366 | ||
367 | return (1); | |
368 | } | |
369 | ||
370 | ||
371 | /* | |
372 | * 'show_diffs()' - Show differences between two destinations. | |
373 | */ | |
374 | ||
375 | static void | |
376 | show_diffs(cups_dest_t *a, /* I - First destination */ | |
377 | cups_dest_t *b) /* I - Second destination */ | |
378 | { | |
379 | int i; /* Looping var */ | |
380 | cups_option_t *aoption; /* Current option */ | |
381 | const char *bval; /* Option value */ | |
382 | ||
383 | ||
384 | if (!a || !b) | |
385 | return; | |
386 | ||
387 | puts(" Item cupsGetDest cupsGetNamedDest"); | |
388 | puts(" -------------------- -------------------- --------------------"); | |
389 | ||
390 | if (strcasecmp(a->name, b->name)) | |
391 | printf(" name %-20.20s %-20.20s\n", a->name, b->name); | |
392 | ||
393 | if ((a->instance && !b->instance) || | |
394 | (!a->instance && b->instance) || | |
395 | (a->instance && strcasecmp(a->instance, b->instance))) | |
396 | printf(" instance %-20.20s %-20.20s\n", | |
397 | a->instance ? a->instance : "(null)", | |
398 | b->instance ? b->instance : "(null)"); | |
399 | ||
400 | if (a->num_options != b->num_options) | |
401 | printf(" num_options %-20d %-20d\n", a->num_options, | |
402 | b->num_options); | |
403 | ||
404 | for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++) | |
405 | if ((bval = cupsGetOption(aoption->name, b->num_options, | |
406 | b->options)) == NULL || | |
407 | strcmp(aoption->value, bval)) | |
408 | printf(" %-20.20s %-20.20s %-20.20s\n", aoption->name, | |
409 | aoption->value, bval ? bval : "(null)"); | |
410 | } | |
411 | ||
412 | ||
09a101d6 | 413 | /* |
1f0275e3 | 414 | * End of "$Id$". |
09a101d6 | 415 | */ |