• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * PWG unit test program for CUPS.
3  *
4  * Copyright 2009-2016 by Apple Inc.
5  *
6  * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
7  */
8 
9 /*
10  * Include necessary headers...
11  */
12 
13 #include "ppd-private.h"
14 #include "file-private.h"
15 
16 
17 /*
18  * Local functions...
19  */
20 
21 static int	test_pagesize(_ppd_cache_t *pc, ppd_file_t *ppd,
22 		              const char *ppdsize);
23 static int	test_ppd_cache(_ppd_cache_t *pc, ppd_file_t *ppd);
24 
25 
26 /*
27  * 'main()' - Main entry.
28  */
29 
30 int					/* O - Exit status */
main(int argc,char * argv[])31 main(int  argc,				/* I - Number of command-line args */
32      char *argv[])			/* I - Command-line arguments */
33 {
34   int			status;		/* Status of tests (0 = success, 1 = fail) */
35   const char		*ppdfile;	/* PPD filename */
36   ppd_file_t		*ppd;		/* PPD file */
37   _ppd_cache_t		*pc;		/* PPD cache and PWG mapping data */
38   const pwg_media_t	*pwgmedia;	/* PWG media size */
39   size_t		i,		/* Looping var */
40 			num_media;	/* Number of media sizes */
41   const pwg_media_t	*mediatable;	/* Media size table */
42   int			dupmedia = 0;	/* Duplicate media sizes? */
43 
44 
45   status = 0;
46 
47   if (argc < 2 || argc > 3)
48   {
49     puts("Usage: ./testpwg filename.ppd [jobfile]");
50     return (1);
51   }
52 
53   ppdfile = argv[1];
54 
55   printf("ppdOpenFile(%s): ", ppdfile);
56   if ((ppd = ppdOpenFile(ppdfile)) == NULL)
57   {
58     ppd_status_t err;			/* Last error in file */
59     int		line;			/* Line number in file */
60 
61 
62     err = ppdLastError(&line);
63 
64     printf("FAIL (%s on line %d)\n", ppdErrorString(err), line);
65 
66     return (1);
67   }
68   else
69     puts("PASS");
70 
71   fputs("_ppdCacheCreateWithPPD(ppd): ", stdout);
72   if ((pc = _ppdCacheCreateWithPPD(ppd)) == NULL)
73   {
74     puts("FAIL");
75     status ++;
76   }
77   else
78   {
79     puts("PASS");
80     status += test_ppd_cache(pc, ppd);
81 
82     if (argc == 3)
83     {
84      /*
85       * Test PageSize mapping code.
86       */
87 
88       int		fd;		/* Job file descriptor */
89       const char	*pagesize;	/* PageSize value */
90       ipp_t		*job;		/* Job attributes */
91       ipp_attribute_t	*media;		/* Media attribute */
92 
93       if ((fd = open(argv[2], O_RDONLY)) >= 0)
94       {
95 	job = ippNew();
96 	ippReadFile(fd, job);
97 	close(fd);
98 
99         if ((media = ippFindAttribute(job, "media", IPP_TAG_ZERO)) != NULL &&
100 	    media->value_tag != IPP_TAG_NAME &&
101 	    media->value_tag != IPP_TAG_KEYWORD)
102 	  media = NULL;
103 
104 	if (media)
105 	  printf("_ppdCacheGetPageSize(media=%s): ",
106 	         media->values[0].string.text);
107 	else
108 	  fputs("_ppdCacheGetPageSize(media-col): ", stdout);
109 
110         fflush(stdout);
111 
112 	if ((pagesize = _ppdCacheGetPageSize(pc, job, NULL, NULL)) == NULL)
113 	{
114 	  puts("FAIL (Not Found)");
115 	  status = 1;
116 	}
117 	else if (media && _cups_strcasecmp(pagesize, media->values[0].string.text))
118 	{
119 	  printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize,
120 		 media->values[0].string.text);
121 	  status = 1;
122 	}
123 	else
124 	  printf("PASS (%s)\n", pagesize);
125 
126 	ippDelete(job);
127       }
128       else
129       {
130         perror(argv[2]);
131 	status = 1;
132       }
133     }
134 
135    /*
136     * _ppdCacheDestroy should never fail...
137     */
138 
139     fputs("_ppdCacheDestroy(pc): ", stdout);
140     _ppdCacheDestroy(pc);
141     puts("PASS");
142   }
143 
144   fputs("pwgMediaForPWG(\"iso_a4_210x297mm\"): ", stdout);
145   if ((pwgmedia = pwgMediaForPWG("iso_a4_210x297mm")) == NULL)
146   {
147     puts("FAIL (not found)");
148     status ++;
149   }
150   else if (strcmp(pwgmedia->pwg, "iso_a4_210x297mm"))
151   {
152     printf("FAIL (%s)\n", pwgmedia->pwg);
153     status ++;
154   }
155   else if (pwgmedia->width != 21000 || pwgmedia->length != 29700)
156   {
157     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
158     status ++;
159   }
160   else
161     puts("PASS");
162 
163   fputs("pwgMediaForPWG(\"roll_max_36.1025x3622.0472in\"): ", stdout);
164   if ((pwgmedia = pwgMediaForPWG("roll_max_36.1025x3622.0472in")) == NULL)
165   {
166     puts("FAIL (not found)");
167     status ++;
168   }
169   else if (pwgmedia->width != 91700 || pwgmedia->length != 9199999)
170   {
171     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
172     status ++;
173   }
174   else
175     printf("PASS (%dx%d)\n", pwgmedia->width, pwgmedia->length);
176 
177   fputs("pwgMediaForPWG(\"disc_test_10x100mm\"): ", stdout);
178   if ((pwgmedia = pwgMediaForPWG("disc_test_10x100mm")) == NULL)
179   {
180     puts("FAIL (not found)");
181     status ++;
182   }
183   else if (pwgmedia->width != 10000 || pwgmedia->length != 10000)
184   {
185     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
186     status ++;
187   }
188   else
189     printf("PASS (%dx%d)\n", pwgmedia->width, pwgmedia->length);
190 
191   fputs("pwgMediaForLegacy(\"na-letter\"): ", stdout);
192   if ((pwgmedia = pwgMediaForLegacy("na-letter")) == NULL)
193   {
194     puts("FAIL (not found)");
195     status ++;
196   }
197   else if (strcmp(pwgmedia->pwg, "na_letter_8.5x11in"))
198   {
199     printf("FAIL (%s)\n", pwgmedia->pwg);
200     status ++;
201   }
202   else if (pwgmedia->width != 21590 || pwgmedia->length != 27940)
203   {
204     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
205     status ++;
206   }
207   else
208     puts("PASS");
209 
210   fputs("pwgMediaForPPD(\"4x6\"): ", stdout);
211   if ((pwgmedia = pwgMediaForPPD("4x6")) == NULL)
212   {
213     puts("FAIL (not found)");
214     status ++;
215   }
216   else if (strcmp(pwgmedia->pwg, "na_index-4x6_4x6in"))
217   {
218     printf("FAIL (%s)\n", pwgmedia->pwg);
219     status ++;
220   }
221   else if (pwgmedia->width != 10160 || pwgmedia->length != 15240)
222   {
223     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
224     status ++;
225   }
226   else
227     puts("PASS");
228 
229   fputs("pwgMediaForPPD(\"10x15cm\"): ", stdout);
230   if ((pwgmedia = pwgMediaForPPD("10x15cm")) == NULL)
231   {
232     puts("FAIL (not found)");
233     status ++;
234   }
235   else if (strcmp(pwgmedia->pwg, "om_100x150mm_100x150mm"))
236   {
237     printf("FAIL (%s)\n", pwgmedia->pwg);
238     status ++;
239   }
240   else if (pwgmedia->width != 10000 || pwgmedia->length != 15000)
241   {
242     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
243     status ++;
244   }
245   else
246     puts("PASS");
247 
248   fputs("pwgMediaForPPD(\"Custom.10x15cm\"): ", stdout);
249   if ((pwgmedia = pwgMediaForPPD("Custom.10x15cm")) == NULL)
250   {
251     puts("FAIL (not found)");
252     status ++;
253   }
254   else if (strcmp(pwgmedia->pwg, "custom_10x15cm_100x150mm"))
255   {
256     printf("FAIL (%s)\n", pwgmedia->pwg);
257     status ++;
258   }
259   else if (pwgmedia->width != 10000 || pwgmedia->length != 15000)
260   {
261     printf("FAIL (%dx%d)\n", pwgmedia->width, pwgmedia->length);
262     status ++;
263   }
264   else
265     puts("PASS");
266 
267   fputs("pwgMediaForSize(29700, 42000): ", stdout);
268   if ((pwgmedia = pwgMediaForSize(29700, 42000)) == NULL)
269   {
270     puts("FAIL (not found)");
271     status ++;
272   }
273   else if (strcmp(pwgmedia->pwg, "iso_a3_297x420mm"))
274   {
275     printf("FAIL (%s)\n", pwgmedia->pwg);
276     status ++;
277   }
278   else
279     puts("PASS");
280 
281   fputs("pwgMediaForSize(9842, 19050): ", stdout);
282   if ((pwgmedia = pwgMediaForSize(9842, 19050)) == NULL)
283   {
284     puts("FAIL (not found)");
285     status ++;
286   }
287   else if (strcmp(pwgmedia->pwg, "na_monarch_3.875x7.5in"))
288   {
289     printf("FAIL (%s)\n", pwgmedia->pwg);
290     status ++;
291   }
292   else
293     printf("PASS (%s)\n", pwgmedia->pwg);
294 
295   fputs("pwgMediaForSize(9800, 19000): ", stdout);
296   if ((pwgmedia = pwgMediaForSize(9800, 19000)) == NULL)
297   {
298     puts("FAIL (not found)");
299     status ++;
300   }
301   else if (strcmp(pwgmedia->pwg, "jpn_you6_98x190mm"))
302   {
303     printf("FAIL (%s)\n", pwgmedia->pwg);
304     status ++;
305   }
306   else
307     printf("PASS (%s)\n", pwgmedia->pwg);
308 
309   fputs("Duplicate size test: ", stdout);
310   for (mediatable = _pwgMediaTable(&num_media);
311        num_media > 1;
312        num_media --, mediatable ++)
313   {
314     for (i = num_media - 1, pwgmedia = mediatable + 1; i > 0; i --, pwgmedia ++)
315     {
316       if (pwgmedia->width == mediatable->width &&
317           pwgmedia->length == mediatable->length)
318       {
319         if (!dupmedia)
320         {
321           dupmedia = 1;
322           status ++;
323           puts("FAIL");
324         }
325 
326         printf("    %s and %s have the same dimensions (%dx%d)\n",
327                pwgmedia->pwg, mediatable->pwg, pwgmedia->width,
328                pwgmedia->length);
329       }
330     }
331   }
332   if (!dupmedia)
333     puts("PASS");
334 
335 
336   return (status);
337 }
338 
339 
340 /*
341  * 'test_pagesize()' - Test the PWG mapping functions.
342  */
343 
344 static int				/* O - 1 on failure, 0 on success */
test_pagesize(_ppd_cache_t * pc,ppd_file_t * ppd,const char * ppdsize)345 test_pagesize(_ppd_cache_t *pc,		/* I - PWG mapping data */
346               ppd_file_t   *ppd,	/* I - PPD file */
347 	      const char   *ppdsize)	/* I - PPD page size */
348 {
349   int		status = 0;		/* Return status */
350   ipp_t		*job;			/* Job attributes */
351   const char	*pagesize;		/* PageSize value */
352 
353 
354   if (ppdPageSize(ppd, ppdsize))
355   {
356     printf("_ppdCacheGetPageSize(keyword=%s): ", ppdsize);
357     fflush(stdout);
358 
359     if ((pagesize = _ppdCacheGetPageSize(pc, NULL, ppdsize, NULL)) == NULL)
360     {
361       puts("FAIL (Not Found)");
362       status = 1;
363     }
364     else if (_cups_strcasecmp(pagesize, ppdsize))
365     {
366       printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize, ppdsize);
367       status = 1;
368     }
369     else
370       puts("PASS");
371 
372     job = ippNew();
373     ippAddString(job, IPP_TAG_JOB, IPP_TAG_KEYWORD, "media", NULL, ppdsize);
374 
375     printf("_ppdCacheGetPageSize(media=%s): ", ppdsize);
376     fflush(stdout);
377 
378     if ((pagesize = _ppdCacheGetPageSize(pc, job, NULL, NULL)) == NULL)
379     {
380       puts("FAIL (Not Found)");
381       status = 1;
382     }
383     else if (_cups_strcasecmp(pagesize, ppdsize))
384     {
385       printf("FAIL (Got \"%s\", Expected \"%s\")\n", pagesize, ppdsize);
386       status = 1;
387     }
388     else
389       puts("PASS");
390 
391     ippDelete(job);
392   }
393 
394   return (status);
395 }
396 
397 
398 /*
399  * 'test_ppd_cache()' - Test the PPD cache functions.
400  */
401 
402 static int				/* O - 1 on failure, 0 on success */
test_ppd_cache(_ppd_cache_t * pc,ppd_file_t * ppd)403 test_ppd_cache(_ppd_cache_t *pc,	/* I - PWG mapping data */
404                ppd_file_t   *ppd)	/* I - PPD file */
405 {
406   int		i,			/* Looping var */
407 		status = 0;		/* Return status */
408   _ppd_cache_t	*pc2;			/* Loaded data */
409   pwg_size_t	*size,			/* Size from original */
410 		*size2;			/* Size from saved */
411   pwg_map_t	*map,			/* Map from original */
412 		*map2;			/* Map from saved */
413 
414 
415  /*
416   * Verify that we can write and read back the same data...
417   */
418 
419   fputs("_ppdCacheWriteFile(test.pwg): ", stdout);
420   if (!_ppdCacheWriteFile(pc, "test.pwg", NULL))
421   {
422     puts("FAIL");
423     status ++;
424   }
425   else
426     puts("PASS");
427 
428   fputs("_ppdCacheCreateWithFile(test.pwg): ", stdout);
429   if ((pc2 = _ppdCacheCreateWithFile("test.pwg", NULL)) == NULL)
430   {
431     puts("FAIL");
432     status ++;
433   }
434   else
435   {
436     // TODO: FINISH ADDING ALL VALUES IN STRUCTURE
437     if (pc2->num_sizes != pc->num_sizes)
438     {
439       if (!status)
440         puts("FAIL");
441 
442       printf("    SAVED num_sizes=%d, ORIG num_sizes=%d\n", pc2->num_sizes,
443              pc->num_sizes);
444 
445       status ++;
446     }
447     else
448     {
449       for (i = pc->num_sizes, size = pc->sizes, size2 = pc2->sizes;
450            i > 0;
451 	   i --, size ++, size2 ++)
452       {
453         if (strcmp(size2->map.pwg, size->map.pwg) ||
454 	    strcmp(size2->map.ppd, size->map.ppd) ||
455 	    size2->width != size->width ||
456 	    size2->length != size->length ||
457 	    size2->left != size->left ||
458 	    size2->bottom != size->bottom ||
459 	    size2->right != size->right ||
460 	    size2->top != size->top)
461 	{
462 	  if (!status)
463 	    puts("FAIL");
464 
465 	  if (strcmp(size->map.pwg, size2->map.pwg))
466 	    printf("    SAVED size->map.pwg=\"%s\", ORIG "
467 	           "size->map.pwg=\"%s\"\n", size2->map.pwg, size->map.pwg);
468 
469 	  if (strcmp(size2->map.ppd, size->map.ppd))
470 	    printf("    SAVED size->map.ppd=\"%s\", ORIG "
471 	           "size->map.ppd=\"%s\"\n", size2->map.ppd, size->map.ppd);
472 
473 	  if (size2->width != size->width)
474 	    printf("    SAVED size->width=%d, ORIG size->width=%d\n",
475 		   size2->width, size->width);
476 
477 	  if (size2->length != size->length)
478 	    printf("    SAVED size->length=%d, ORIG size->length=%d\n",
479 		   size2->length, size->length);
480 
481 	  if (size2->left != size->left)
482 	    printf("    SAVED size->left=%d, ORIG size->left=%d\n",
483 		   size2->left, size->left);
484 
485 	  if (size2->bottom != size->bottom)
486 	    printf("    SAVED size->bottom=%d, ORIG size->bottom=%d\n",
487 		   size2->bottom, size->bottom);
488 
489 	  if (size2->right != size->right)
490 	    printf("    SAVED size->right=%d, ORIG size->right=%d\n",
491 		   size2->right, size->right);
492 
493 	  if (size2->top != size->top)
494 	    printf("    SAVED size->top=%d, ORIG size->top=%d\n",
495 		   size2->top, size->top);
496 
497 	  status ++;
498 	  break;
499 	}
500       }
501 
502       for (i = pc->num_sources, map = pc->sources, map2 = pc2->sources;
503            i > 0;
504 	   i --, map ++, map2 ++)
505       {
506         if (strcmp(map2->pwg, map->pwg) ||
507 	    strcmp(map2->ppd, map->ppd))
508 	{
509 	  if (!status)
510 	    puts("FAIL");
511 
512 	  if (strcmp(map->pwg, map2->pwg))
513 	    printf("    SAVED source->pwg=\"%s\", ORIG source->pwg=\"%s\"\n",
514 	           map2->pwg, map->pwg);
515 
516 	  if (strcmp(map2->ppd, map->ppd))
517 	    printf("    SAVED source->ppd=\"%s\", ORIG source->ppd=\"%s\"\n",
518 	           map2->ppd, map->ppd);
519 
520 	  status ++;
521 	  break;
522 	}
523       }
524 
525       for (i = pc->num_types, map = pc->types, map2 = pc2->types;
526            i > 0;
527 	   i --, map ++, map2 ++)
528       {
529         if (strcmp(map2->pwg, map->pwg) ||
530 	    strcmp(map2->ppd, map->ppd))
531 	{
532 	  if (!status)
533 	    puts("FAIL");
534 
535 	  if (strcmp(map->pwg, map2->pwg))
536 	    printf("    SAVED type->pwg=\"%s\", ORIG type->pwg=\"%s\"\n",
537 	           map2->pwg, map->pwg);
538 
539 	  if (strcmp(map2->ppd, map->ppd))
540 	    printf("    SAVED type->ppd=\"%s\", ORIG type->ppd=\"%s\"\n",
541 	           map2->ppd, map->ppd);
542 
543 	  status ++;
544 	  break;
545 	}
546       }
547     }
548 
549     if (!status)
550       puts("PASS");
551 
552     _ppdCacheDestroy(pc2);
553   }
554 
555  /*
556   * Test PageSize mapping code...
557   */
558 
559   status += test_pagesize(pc, ppd, "Letter");
560   status += test_pagesize(pc, ppd, "na-letter");
561   status += test_pagesize(pc, ppd, "A4");
562   status += test_pagesize(pc, ppd, "iso-a4");
563 
564   return (status);
565 }
566