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