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