]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testipp.c
2 * "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $"
4 * IPP test program for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
20 * hex_dump() - Produce a hex dump of a buffer.
21 * print_attributes() - Print the attributes in a request...
22 * read_cb() - Read data from a buffer.
23 * write_cb() - Write data into a buffer.
27 * Include necessary headers...
31 #include "string-private.h"
32 #include "ipp-private.h"
45 int rpos
; /* Current position in buffer */
46 ipp_uchar_t wbuffer
[8192]; /* Write buffer */
47 int wused
; /* Number of bytes in buffer */
48 ipp_uchar_t collection
[] = /* Collection buffer */
50 0x01, 0x01, /* IPP version */
51 0x00, 0x02, /* Print-Job operation */
52 0x00, 0x00, 0x00, 0x01, /* Request ID */
57 0x00, 0x12, /* Name length + name */
58 'a','t','t','r','i','b','u','t','e','s','-',
59 'c','h','a','r','s','e','t',
60 0x00, 0x05, /* Value length + value */
64 0x00, 0x1b, /* Name length + name */
65 'a','t','t','r','i','b','u','t','e','s','-',
66 'n','a','t','u','r','a','l','-','l','a','n',
68 0x00, 0x02, /* Value length + value */
72 0x00, 0x0b, /* Name length + name */
73 'p','r','i','n','t','e','r','-','u','r','i',
74 0x00, 0x1c, /* Value length + value */
75 'i','p','p',':','/','/','l','o','c','a','l',
76 'h','o','s','t','/','p','r','i','n','t','e',
77 'r','s','/','f','o','o',
79 IPP_TAG_JOB
, /* job group tag */
81 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
82 0x00, 0x09, /* Name length + name */
83 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l',
84 0x00, 0x00, /* No value */
85 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
86 0x00, 0x00, /* No name */
87 0x00, 0x0a, /* Value length + value */
88 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e',
89 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
90 0x00, 0x00, /* Name length + name */
91 0x00, 0x00, /* No value */
92 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
93 0x00, 0x00, /* No name */
94 0x00, 0x0b, /* Value length + value */
95 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
96 IPP_TAG_INTEGER
, /* integer tag */
97 0x00, 0x00, /* No name */
98 0x00, 0x04, /* Value length + value */
99 0x00, 0x00, 0x54, 0x56,
100 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
101 0x00, 0x00, /* No name */
102 0x00, 0x0b, /* Value length + value */
103 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
104 IPP_TAG_INTEGER
, /* integer tag */
105 0x00, 0x00, /* No name */
106 0x00, 0x04, /* Value length + value */
107 0x00, 0x00, 0x6d, 0x24,
108 IPP_TAG_END_COLLECTION
, /* endCollection tag */
109 0x00, 0x00, /* No name */
110 0x00, 0x00, /* No value */
111 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
112 0x00, 0x00, /* No name */
113 0x00, 0x0b, /* Value length + value */
114 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
115 IPP_TAG_KEYWORD
, /* keyword tag */
116 0x00, 0x00, /* No name */
117 0x00, 0x04, /* Value length + value */
120 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
121 0x00, 0x00, /* No name */
122 0x00, 0x0a, /* Value length + value */
123 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
124 IPP_TAG_KEYWORD
, /* keyword tag */
125 0x00, 0x00, /* No name */
126 0x00, 0x05, /* Value length + value */
127 'p', 'l', 'a', 'i', 'n',
128 IPP_TAG_END_COLLECTION
, /* endCollection tag */
129 0x00, 0x00, /* No name */
130 0x00, 0x00, /* No value */
132 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
133 0x00, 0x00, /* No name */
134 0x00, 0x00, /* No value */
135 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
136 0x00, 0x00, /* No name */
137 0x00, 0x0a, /* Value length + value */
138 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e',
139 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
140 0x00, 0x00, /* Name length + name */
141 0x00, 0x00, /* No value */
142 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
143 0x00, 0x00, /* No name */
144 0x00, 0x0b, /* Value length + value */
145 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
146 IPP_TAG_INTEGER
, /* integer tag */
147 0x00, 0x00, /* No name */
148 0x00, 0x04, /* Value length + value */
149 0x00, 0x00, 0x52, 0x08,
150 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
151 0x00, 0x00, /* No name */
152 0x00, 0x0b, /* Value length + value */
153 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
154 IPP_TAG_INTEGER
, /* integer tag */
155 0x00, 0x00, /* No name */
156 0x00, 0x04, /* Value length + value */
157 0x00, 0x00, 0x74, 0x04,
158 IPP_TAG_END_COLLECTION
, /* endCollection tag */
159 0x00, 0x00, /* No name */
160 0x00, 0x00, /* No value */
161 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
162 0x00, 0x00, /* No name */
163 0x00, 0x0b, /* Value length + value */
164 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
165 IPP_TAG_KEYWORD
, /* keyword tag */
166 0x00, 0x00, /* No name */
167 0x00, 0x05, /* Value length + value */
168 'p', 'l', 'a', 'i', 'd',
170 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
171 0x00, 0x00, /* No name */
172 0x00, 0x0a, /* Value length + value */
173 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
174 IPP_TAG_KEYWORD
, /* keyword tag */
175 0x00, 0x00, /* No name */
176 0x00, 0x06, /* Value length + value */
177 'g', 'l', 'o', 's', 's', 'y',
178 IPP_TAG_END_COLLECTION
, /* endCollection tag */
179 0x00, 0x00, /* No name */
180 0x00, 0x00, /* No value */
182 IPP_TAG_END
/* end tag */
190 void hex_dump(const char *title
, ipp_uchar_t
*buffer
, int bytes
);
191 void print_attributes(ipp_t
*ipp
, int indent
);
192 ssize_t
read_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
193 ssize_t
write_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
197 * 'main()' - Main entry.
200 int /* O - Exit status */
201 main(int argc
, /* I - Number of command-line arguments */
202 char *argv
[]) /* I - Command-line arguments */
204 ipp_t
*cols
[2], /* Collections */
205 *size
; /* media-size collection */
206 ipp_t
*request
; /* Request */
207 ipp_attribute_t
*media_col
, /* media-col attribute */
208 *media_size
, /* media-size attribute */
209 *attr
; /* Other attribute */
210 ipp_state_t state
; /* State */
211 int length
; /* Length of data */
212 cups_file_t
*fp
; /* File pointer */
213 int i
; /* Looping var */
214 int status
; /* Status of tests (0 = success, 1 = fail) */
222 * Test request generation code...
225 printf("Create Sample Request: ");
228 request
->request
.op
.version
[0] = 0x01;
229 request
->request
.op
.version
[1] = 0x01;
230 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
231 request
->request
.op
.request_id
= 1;
233 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
234 "attributes-charset", NULL
, "utf-8");
235 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
236 "attributes-natural-language", NULL
, "en");
237 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
238 "printer-uri", NULL
, "ipp://localhost/printers/foo");
242 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "x-dimension", 21590);
243 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "y-dimension", 27940);
244 ippAddCollection(cols
[0], IPP_TAG_JOB
, "media-size", size
);
246 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
,
248 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
,
253 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "x-dimension", 21000);
254 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "y-dimension", 29700);
255 ippAddCollection(cols
[1], IPP_TAG_JOB
, "media-size", size
);
257 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
,
259 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
,
262 ippAddCollections(request
, IPP_TAG_JOB
, "media-col", 2,
263 (const ipp_t
**)cols
);
267 length
= ippLength(request
);
268 if (length
!= sizeof(collection
))
270 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
271 length
, (int)sizeof(collection
));
281 printf("Write Sample to Memory: ");
284 while ((state
= ippWriteIO(wbuffer
, write_cb
, 1, NULL
,
285 request
)) != IPP_DATA
)
286 if (state
== IPP_ERROR
)
289 if (state
!= IPP_DATA
)
291 printf("FAIL - %d bytes written.\n", wused
);
294 else if (wused
!= sizeof(collection
))
296 printf("FAIL - wrote %d bytes, expected %d bytes!\n", wused
,
297 (int)sizeof(collection
));
298 hex_dump("Bytes Written", wbuffer
, wused
);
299 hex_dump("Baseline", collection
, sizeof(collection
));
302 else if (memcmp(wbuffer
, collection
, wused
))
304 for (i
= 0; i
< wused
; i
++)
305 if (wbuffer
[i
] != collection
[i
])
308 printf("FAIL - output does not match baseline at 0x%04x!\n", i
);
309 hex_dump("Bytes Written", wbuffer
, wused
);
310 hex_dump("Baseline", collection
, sizeof(collection
));
319 * Read the data back in and confirm...
322 printf("Read Sample from Memory: ");
327 while ((state
= ippReadIO(wbuffer
, read_cb
, 1, NULL
, request
)) != IPP_DATA
)
328 if (state
== IPP_ERROR
)
331 length
= ippLength(request
);
333 if (state
!= IPP_DATA
)
335 printf("FAIL - %d bytes read.\n", rpos
);
338 else if (rpos
!= wused
)
340 printf("FAIL - read %d bytes, expected %d bytes!\n", rpos
, wused
);
341 print_attributes(request
, 8);
344 else if (length
!= sizeof(collection
))
346 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
347 length
, (int)sizeof(collection
));
348 print_attributes(request
, 8);
354 fputs("ippFindAttribute(media-col): ", stdout
);
355 if ((media_col
= ippFindAttribute(request
, "media-col",
356 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
358 if ((media_col
= ippFindAttribute(request
, "media-col",
359 IPP_TAG_ZERO
)) == NULL
)
360 puts("FAIL (not found)");
362 printf("FAIL (wrong type - %s)\n", ippTagString(media_col
->value_tag
));
366 else if (media_col
->num_values
!= 2)
368 printf("FAIL (wrong count - %d)\n", media_col
->num_values
);
376 fputs("ippFindAttribute(media-size 1): ", stdout
);
377 if ((media_size
= ippFindAttribute(media_col
->values
[0].collection
,
379 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
381 if ((media_size
= ippFindAttribute(media_col
->values
[0].collection
,
383 IPP_TAG_ZERO
)) == NULL
)
384 puts("FAIL (not found)");
386 printf("FAIL (wrong type - %s)\n",
387 ippTagString(media_size
->value_tag
));
393 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
394 "x-dimension", IPP_TAG_INTEGER
)) == NULL
)
396 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
397 "x-dimension", IPP_TAG_ZERO
)) == NULL
)
398 puts("FAIL (missing x-dimension)");
400 printf("FAIL (wrong type for x-dimension - %s)\n",
401 ippTagString(attr
->value_tag
));
405 else if (attr
->values
[0].integer
!= 21590)
407 printf("FAIL (wrong value for x-dimension - %d)\n",
408 attr
->values
[0].integer
);
411 else if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
413 IPP_TAG_INTEGER
)) == NULL
)
415 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
416 "y-dimension", IPP_TAG_ZERO
)) == NULL
)
417 puts("FAIL (missing y-dimension)");
419 printf("FAIL (wrong type for y-dimension - %s)\n",
420 ippTagString(attr
->value_tag
));
424 else if (attr
->values
[0].integer
!= 27940)
426 printf("FAIL (wrong value for y-dimension - %d)\n",
427 attr
->values
[0].integer
);
434 fputs("ippFindAttribute(media-size 2): ", stdout
);
435 if ((media_size
= ippFindAttribute(media_col
->values
[1].collection
,
437 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
439 if ((media_size
= ippFindAttribute(media_col
->values
[1].collection
,
441 IPP_TAG_ZERO
)) == NULL
)
442 puts("FAIL (not found)");
444 printf("FAIL (wrong type - %s)\n",
445 ippTagString(media_size
->value_tag
));
451 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
453 IPP_TAG_INTEGER
)) == NULL
)
455 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
456 "x-dimension", IPP_TAG_ZERO
)) == NULL
)
457 puts("FAIL (missing x-dimension)");
459 printf("FAIL (wrong type for x-dimension - %s)\n",
460 ippTagString(attr
->value_tag
));
464 else if (attr
->values
[0].integer
!= 21000)
466 printf("FAIL (wrong value for x-dimension - %d)\n",
467 attr
->values
[0].integer
);
470 else if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
472 IPP_TAG_INTEGER
)) == NULL
)
474 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
475 "y-dimension", IPP_TAG_ZERO
)) == NULL
)
476 puts("FAIL (missing y-dimension)");
478 printf("FAIL (wrong type for y-dimension - %s)\n",
479 ippTagString(attr
->value_tag
));
483 else if (attr
->values
[0].integer
!= 29700)
485 printf("FAIL (wrong value for y-dimension - %d)\n",
486 attr
->values
[0].integer
);
497 * Test _ippFindOption() private API...
500 fputs("_ippFindOption(printer-type): ", stdout
);
501 if (_ippFindOption("printer-type"))
516 puts("Core IPP tests failed.");
518 puts("Core IPP tests passed.");
526 for (i
= 1; i
< argc
; i
++)
528 if ((fp
= cupsFileOpen(argv
[i
], "r")) == NULL
)
530 printf("Unable to open \"%s\" - %s\n", argv
[i
], strerror(errno
));
536 while ((state
= ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
537 request
)) == IPP_ATTRIBUTE
);
539 if (state
!= IPP_DATA
)
541 printf("Error reading IPP message from \"%s\"!\n", argv
[i
]);
546 printf("\n%s:\n", argv
[i
]);
547 print_attributes(request
, 4);
560 * 'hex_dump()' - Produce a hex dump of a buffer.
564 hex_dump(const char *title
, /* I - Title */
565 ipp_uchar_t
*buffer
, /* I - Buffer to dump */
566 int bytes
) /* I - Number of bytes */
568 int i
, j
; /* Looping vars */
569 int ch
; /* Current ASCII char */
573 * Show lines of 16 bytes at a time...
576 printf(" %s:\n", title
);
578 for (i
= 0; i
< bytes
; i
+= 16)
587 * Then up to 16 bytes in hex...
590 for (j
= 0; j
< 16; j
++)
592 printf(" %02x", buffer
[i
+ j
]);
597 * Then the ASCII representation of the bytes...
603 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
605 ch
= buffer
[i
+ j
] & 127;
607 if (ch
< ' ' || ch
== 127)
619 * 'print_attributes()' - Print the attributes in a request...
623 print_attributes(ipp_t
*ipp
, /* I - IPP request */
624 int indent
) /* I - Indentation */
626 int i
; /* Looping var */
627 ipp_tag_t group
; /* Current group */
628 ipp_attribute_t
*attr
; /* Current attribute */
629 ipp_value_t
*val
; /* Current value */
630 static const char * const tags
[] = /* Value/group tag strings */
633 "operation-attributes-tag",
634 "job-attributes-tag",
635 "end-of-attributes-tag",
636 "printer-attributes-tag",
637 "unsupported-attributes-tag",
638 "subscription-attributes-tag",
639 "event-attributes-tag",
697 "textWithoutLanguage",
698 "nameWithoutLanguage",
710 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
712 if (!attr
->name
&& indent
== 4)
714 group
= IPP_TAG_ZERO
;
719 if (group
!= attr
->group_tag
)
721 group
= attr
->group_tag
;
723 printf("\n%*s%s:\n\n", indent
- 4, "", tags
[group
]);
726 printf("%*s%s (", indent
, "", attr
->name
? attr
->name
: "(null)");
727 if (attr
->num_values
> 1)
729 printf("%s):", tags
[attr
->value_tag
]);
731 switch (attr
->value_tag
)
734 case IPP_TAG_INTEGER
:
735 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
736 printf(" %d", val
->integer
);
740 case IPP_TAG_BOOLEAN
:
741 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
742 printf(" %s", val
->boolean
? "true" : "false");
747 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
748 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
754 time_t vtime
; /* Date/Time value */
755 struct tm
*vdate
; /* Date info */
756 char vstring
[256]; /* Formatted time */
758 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
760 vtime
= ippDateToTime(val
->date
);
761 vdate
= localtime(&vtime
);
762 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
763 printf(" (%s)", vstring
);
769 case IPP_TAG_RESOLUTION
:
770 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
771 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
772 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
776 case IPP_TAG_STRING
:
777 case IPP_TAG_TEXTLANG
:
778 case IPP_TAG_NAMELANG
:
781 case IPP_TAG_KEYWORD
:
783 case IPP_TAG_URISCHEME
:
784 case IPP_TAG_CHARSET
:
785 case IPP_TAG_LANGUAGE
:
786 case IPP_TAG_MIMETYPE
:
787 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
788 printf(" \"%s\"", val
->string
.text
);
792 case IPP_TAG_BEGIN_COLLECTION
:
795 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
799 print_attributes(val
->collection
, indent
+ 4);
804 printf("UNKNOWN (%d values)\n", attr
->num_values
);
812 * 'read_cb()' - Read data from a buffer.
815 ssize_t
/* O - Number of bytes read */
816 read_cb(void *data
, /* I - Data */
817 ipp_uchar_t
*buffer
, /* O - Buffer to read */
818 size_t bytes
) /* I - Number of bytes to read */
820 int count
; /* Number of bytes */
824 * Copy bytes from the data buffer to the read buffer...
827 for (count
= bytes
; count
> 0 && rpos
< wused
; count
--, rpos
++)
828 *buffer
++ = wbuffer
[rpos
];
831 * Return the number of bytes read...
834 return (bytes
- count
);
839 * 'write_cb()' - Write data into a buffer.
842 ssize_t
/* O - Number of bytes written */
843 write_cb(void *data
, /* I - Data */
844 ipp_uchar_t
*buffer
, /* I - Buffer to write */
845 size_t bytes
) /* I - Number of bytes to write */
847 int count
; /* Number of bytes */
851 * Loop until all bytes are written...
854 for (count
= bytes
; count
> 0 && wused
< sizeof(wbuffer
); count
--, wused
++)
855 wbuffer
[wused
] = *buffer
++;
858 * Return the number of bytes written...
861 return (bytes
- count
);
866 * End of "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $".