]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testipp.c
d6247621d53c1e69f399948a82a3e09a786d2f43
2 * "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $"
4 * IPP test program for the Common UNIX Printing System (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.
23 * Include necessary headers...
28 #include <cups/file.h>
29 #include <cups/string.h>
31 #include "ipp-private.h"
44 int rpos
; /* Current position in buffer */
45 ipp_uchar_t wbuffer
[8192]; /* Write buffer */
46 int wused
; /* Number of bytes in buffer */
47 ipp_uchar_t collection
[] = /* Collection buffer */
49 0x01, 0x01, /* IPP version */
50 0x00, 0x02, /* Print-Job operation */
51 0x00, 0x00, 0x00, 0x01, /* Request ID */
56 0x00, 0x12, /* Name length + name */
57 'a','t','t','r','i','b','u','t','e','s','-',
58 'c','h','a','r','s','e','t',
59 0x00, 0x05, /* Value length + value */
63 0x00, 0x1b, /* Name length + name */
64 'a','t','t','r','i','b','u','t','e','s','-',
65 'n','a','t','u','r','a','l','-','l','a','n',
67 0x00, 0x02, /* Value length + value */
71 0x00, 0x0b, /* Name length + name */
72 'p','r','i','n','t','e','r','-','u','r','i',
73 0x00, 0x1c, /* Value length + value */
74 'i','p','p',':','/','/','l','o','c','a','l',
75 'h','o','s','t','/','p','r','i','n','t','e',
76 'r','s','/','f','o','o',
78 IPP_TAG_JOB
, /* job group tag */
80 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
81 0x00, 0x09, /* Name length + name */
82 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l',
83 0x00, 0x00, /* No value */
84 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
85 0x00, 0x00, /* No name */
86 0x00, 0x0a, /* Value length + value */
87 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e',
88 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
89 0x00, 0x00, /* Name length + name */
90 0x00, 0x00, /* No value */
91 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
92 0x00, 0x00, /* No name */
93 0x00, 0x0b, /* Value length + value */
94 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
95 IPP_TAG_INTEGER
, /* integer tag */
96 0x00, 0x00, /* No name */
97 0x00, 0x04, /* Value length + value */
98 0x00, 0x00, 0x54, 0x56,
99 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
100 0x00, 0x00, /* No name */
101 0x00, 0x0b, /* Value length + value */
102 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
103 IPP_TAG_INTEGER
, /* integer tag */
104 0x00, 0x00, /* No name */
105 0x00, 0x04, /* Value length + value */
106 0x00, 0x00, 0x6d, 0x24,
107 IPP_TAG_END_COLLECTION
, /* endCollection tag */
108 0x00, 0x00, /* No name */
109 0x00, 0x00, /* No value */
110 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
111 0x00, 0x00, /* No name */
112 0x00, 0x0b, /* Value length + value */
113 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
114 IPP_TAG_KEYWORD
, /* keyword tag */
115 0x00, 0x00, /* No name */
116 0x00, 0x04, /* Value length + value */
119 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
120 0x00, 0x00, /* No name */
121 0x00, 0x0a, /* Value length + value */
122 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
123 IPP_TAG_KEYWORD
, /* keyword tag */
124 0x00, 0x00, /* No name */
125 0x00, 0x05, /* Value length + value */
126 'p', 'l', 'a', 'i', 'n',
127 IPP_TAG_END_COLLECTION
, /* endCollection tag */
128 0x00, 0x00, /* No name */
129 0x00, 0x00, /* No value */
131 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
132 0x00, 0x00, /* No name */
133 0x00, 0x00, /* No value */
134 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
135 0x00, 0x00, /* No name */
136 0x00, 0x0a, /* Value length + value */
137 'm', 'e', 'd', 'i', 'a', '-', 's', 'i', 'z', 'e',
138 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
139 0x00, 0x00, /* Name length + name */
140 0x00, 0x00, /* No value */
141 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
142 0x00, 0x00, /* No name */
143 0x00, 0x0b, /* Value length + value */
144 'x', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
145 IPP_TAG_INTEGER
, /* integer tag */
146 0x00, 0x00, /* No name */
147 0x00, 0x04, /* Value length + value */
148 0x00, 0x00, 0x52, 0x08,
149 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
150 0x00, 0x00, /* No name */
151 0x00, 0x0b, /* Value length + value */
152 'y', '-', 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n',
153 IPP_TAG_INTEGER
, /* integer tag */
154 0x00, 0x00, /* No name */
155 0x00, 0x04, /* Value length + value */
156 0x00, 0x00, 0x74, 0x04,
157 IPP_TAG_END_COLLECTION
, /* endCollection tag */
158 0x00, 0x00, /* No name */
159 0x00, 0x00, /* No value */
160 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
161 0x00, 0x00, /* No name */
162 0x00, 0x0b, /* Value length + value */
163 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
164 IPP_TAG_KEYWORD
, /* keyword tag */
165 0x00, 0x00, /* No name */
166 0x00, 0x05, /* Value length + value */
167 'p', 'l', 'a', 'i', 'd',
169 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
170 0x00, 0x00, /* No name */
171 0x00, 0x0a, /* Value length + value */
172 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
173 IPP_TAG_KEYWORD
, /* keyword tag */
174 0x00, 0x00, /* No name */
175 0x00, 0x06, /* Value length + value */
176 'g', 'l', 'o', 's', 's', 'y',
177 IPP_TAG_END_COLLECTION
, /* endCollection tag */
178 0x00, 0x00, /* No name */
179 0x00, 0x00, /* No value */
181 IPP_TAG_END
/* end tag */
189 void hex_dump(const char *title
, ipp_uchar_t
*buffer
, int bytes
);
190 void print_attributes(ipp_t
*ipp
, int indent
);
191 ssize_t
read_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
192 ssize_t
write_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
196 * 'main()' - Main entry.
199 int /* O - Exit status */
200 main(int argc
, /* I - Number of command-line arguments */
201 char *argv
[]) /* I - Command-line arguments */
203 ipp_t
*cols
[2], /* Collections */
204 *size
; /* media-size collection */
205 ipp_t
*request
; /* Request */
206 ipp_attribute_t
*media_col
, /* media-col attribute */
207 *media_size
, /* media-size attribute */
208 *attr
; /* Other attribute */
209 ipp_state_t state
; /* State */
210 int length
; /* Length of data */
211 cups_file_t
*fp
; /* File pointer */
212 int i
; /* Looping var */
213 int status
; /* Status of tests (0 = success, 1 = fail) */
221 * Test request generation code...
224 printf("Create Sample Request: ");
227 request
->request
.op
.version
[0] = 0x01;
228 request
->request
.op
.version
[1] = 0x01;
229 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
230 request
->request
.op
.request_id
= 1;
232 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
233 "attributes-charset", NULL
, "utf-8");
234 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
235 "attributes-natural-language", NULL
, "en");
236 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
237 "printer-uri", NULL
, "ipp://localhost/printers/foo");
241 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "x-dimension", 21590);
242 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "y-dimension", 27940);
243 ippAddCollection(cols
[0], IPP_TAG_JOB
, "media-size", size
);
244 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
,
246 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
,
251 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "x-dimension", 21000);
252 ippAddInteger(size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "y-dimension", 29700);
253 ippAddCollection(cols
[1], IPP_TAG_JOB
, "media-size", size
);
254 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
,
256 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
,
259 ippAddCollections(request
, IPP_TAG_JOB
, "media-col", 2,
260 (const ipp_t
**)cols
);
262 length
= ippLength(request
);
263 if (length
!= sizeof(collection
))
265 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
266 length
, (int)sizeof(collection
));
276 printf("Write Sample to Memory: ");
279 while ((state
= ippWriteIO(wbuffer
, write_cb
, 1, NULL
, request
)) != IPP_DATA
)
280 if (state
== IPP_ERROR
)
283 if (state
!= IPP_DATA
)
285 printf("FAIL - %d bytes written.\n", wused
);
288 else if (wused
!= sizeof(collection
))
290 printf("FAIL - wrote %d bytes, expected %d bytes!\n", wused
,
291 (int)sizeof(collection
));
292 hex_dump("Bytes Written", wbuffer
, wused
);
293 hex_dump("Baseline", collection
, sizeof(collection
));
296 else if (memcmp(wbuffer
, collection
, wused
))
298 for (i
= 0; i
< wused
; i
++)
299 if (wbuffer
[i
] != collection
[i
])
302 printf("FAIL - output does not match baseline at 0x%04x!\n", i
);
303 hex_dump("Bytes Written", wbuffer
, wused
);
304 hex_dump("Baseline", collection
, sizeof(collection
));
313 * Read the data back in and confirm...
316 printf("Read Sample from Memory: ");
321 while ((state
= ippReadIO(wbuffer
, read_cb
, 1, NULL
, request
)) != IPP_DATA
)
322 if (state
== IPP_ERROR
)
325 length
= ippLength(request
);
327 if (state
!= IPP_DATA
)
329 printf("FAIL - %d bytes read.\n", rpos
);
332 else if (rpos
!= wused
)
334 printf("FAIL - read %d bytes, expected %d bytes!\n", rpos
, wused
);
335 print_attributes(request
, 8);
338 else if (length
!= sizeof(collection
))
340 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
341 length
, (int)sizeof(collection
));
342 print_attributes(request
, 8);
348 fputs("ippFindAttribute(media-col): ", stdout
);
349 if ((media_col
= ippFindAttribute(request
, "media-col",
350 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
352 if ((media_col
= ippFindAttribute(request
, "media-col",
353 IPP_TAG_ZERO
)) == NULL
)
354 puts("FAIL (not found)");
356 printf("FAIL (wrong type - %s)\n", ippTagString(media_col
->value_tag
));
360 else if (media_col
->num_values
!= 2)
362 printf("FAIL (wrong count - %d)\n", media_col
->num_values
);
368 fputs("ippFindAttribute(media-size 1): ", stdout
);
369 if ((media_size
= ippFindAttribute(media_col
->values
[0].collection
,
371 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
373 if ((media_size
= ippFindAttribute(media_col
->values
[0].collection
,
375 IPP_TAG_ZERO
)) == NULL
)
376 puts("FAIL (not found)");
378 printf("FAIL (wrong type - %s)\n", ippTagString(media_size
->value_tag
));
384 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
385 "x-dimension", IPP_TAG_INTEGER
)) == NULL
)
387 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
388 "x-dimension", IPP_TAG_ZERO
)) == NULL
)
389 puts("FAIL (missing x-dimension)");
391 printf("FAIL (wrong type for x-dimension - %s)\n",
392 ippTagString(attr
->value_tag
));
396 else if (attr
->values
[0].integer
!= 21590)
398 printf("FAIL (wrong value for x-dimension - %d)\n",
399 attr
->values
[0].integer
);
402 else if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
404 IPP_TAG_INTEGER
)) == NULL
)
406 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
407 "y-dimension", IPP_TAG_ZERO
)) == NULL
)
408 puts("FAIL (missing y-dimension)");
410 printf("FAIL (wrong type for y-dimension - %s)\n",
411 ippTagString(attr
->value_tag
));
415 else if (attr
->values
[0].integer
!= 27940)
417 printf("FAIL (wrong value for y-dimension - %d)\n",
418 attr
->values
[0].integer
);
425 fputs("ippFindAttribute(media-size 2): ", stdout
);
426 if ((media_size
= ippFindAttribute(media_col
->values
[1].collection
,
428 IPP_TAG_BEGIN_COLLECTION
)) == NULL
)
430 if ((media_size
= ippFindAttribute(media_col
->values
[1].collection
,
432 IPP_TAG_ZERO
)) == NULL
)
433 puts("FAIL (not found)");
435 printf("FAIL (wrong type - %s)\n", ippTagString(media_size
->value_tag
));
441 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
443 IPP_TAG_INTEGER
)) == NULL
)
445 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
446 "x-dimension", IPP_TAG_ZERO
)) == NULL
)
447 puts("FAIL (missing x-dimension)");
449 printf("FAIL (wrong type for x-dimension - %s)\n",
450 ippTagString(attr
->value_tag
));
454 else if (attr
->values
[0].integer
!= 21000)
456 printf("FAIL (wrong value for x-dimension - %d)\n",
457 attr
->values
[0].integer
);
460 else if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
462 IPP_TAG_INTEGER
)) == NULL
)
464 if ((attr
= ippFindAttribute(media_size
->values
[0].collection
,
465 "y-dimension", IPP_TAG_ZERO
)) == NULL
)
466 puts("FAIL (missing y-dimension)");
468 printf("FAIL (wrong type for y-dimension - %s)\n",
469 ippTagString(attr
->value_tag
));
473 else if (attr
->values
[0].integer
!= 29700)
475 printf("FAIL (wrong value for y-dimension - %d)\n",
476 attr
->values
[0].integer
);
486 * Test _ippFindOption() private API...
489 fputs("_ippFindOption(printer-type): ", stdout
);
490 if (_ippFindOption("printer-type"))
505 puts("Core IPP tests failed.");
507 puts("Core IPP tests passed.");
515 for (i
= 1; i
< argc
; i
++)
517 if ((fp
= cupsFileOpen(argv
[i
], "r")) == NULL
)
519 printf("Unable to open \"%s\" - %s\n", argv
[i
], strerror(errno
));
525 while ((state
= ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
526 request
)) == IPP_ATTRIBUTE
);
528 if (state
!= IPP_DATA
)
530 printf("Error reading IPP message from \"%s\"!\n", argv
[i
]);
535 printf("\n%s:\n", argv
[i
]);
536 print_attributes(request
, 4);
549 * 'hex_dump()' - Produce a hex dump of a buffer.
553 hex_dump(const char *title
, /* I - Title */
554 ipp_uchar_t
*buffer
, /* I - Buffer to dump */
555 int bytes
) /* I - Number of bytes */
557 int i
, j
; /* Looping vars */
558 int ch
; /* Current ASCII char */
562 * Show lines of 16 bytes at a time...
565 printf(" %s:\n", title
);
567 for (i
= 0; i
< bytes
; i
+= 16)
576 * Then up to 16 bytes in hex...
579 for (j
= 0; j
< 16; j
++)
581 printf(" %02x", buffer
[i
+ j
]);
586 * Then the ASCII representation of the bytes...
592 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
594 ch
= buffer
[i
+ j
] & 127;
596 if (ch
< ' ' || ch
== 127)
608 * 'print_attributes()' - Print the attributes in a request...
612 print_attributes(ipp_t
*ipp
, /* I - IPP request */
613 int indent
) /* I - Indentation */
615 int i
; /* Looping var */
616 ipp_tag_t group
; /* Current group */
617 ipp_attribute_t
*attr
; /* Current attribute */
618 ipp_value_t
*val
; /* Current value */
619 static const char * const tags
[] = /* Value/group tag strings */
622 "operation-attributes-tag",
623 "job-attributes-tag",
624 "end-of-attributes-tag",
625 "printer-attributes-tag",
626 "unsupported-attributes-tag",
627 "subscription-attributes-tag",
628 "event-attributes-tag",
686 "textWithoutLanguage",
687 "nameWithoutLanguage",
699 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
701 if (!attr
->name
&& indent
== 4)
703 group
= IPP_TAG_ZERO
;
708 if (group
!= attr
->group_tag
)
710 group
= attr
->group_tag
;
712 printf("\n%*s%s:\n\n", indent
- 4, "", tags
[group
]);
715 printf("%*s%s (", indent
, "", attr
->name
? attr
->name
: "(null)");
716 if (attr
->num_values
> 1)
718 printf("%s):", tags
[attr
->value_tag
]);
720 switch (attr
->value_tag
)
723 case IPP_TAG_INTEGER
:
724 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
725 printf(" %d", val
->integer
);
729 case IPP_TAG_BOOLEAN
:
730 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
731 printf(" %s", val
->boolean
? "true" : "false");
736 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
737 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
743 time_t vtime
; /* Date/Time value */
744 struct tm
*vdate
; /* Date info */
745 char vstring
[256]; /* Formatted time */
747 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
749 vtime
= ippDateToTime(val
->date
);
750 vdate
= localtime(&vtime
);
751 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
752 printf(" (%s)", vstring
);
758 case IPP_TAG_RESOLUTION
:
759 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
760 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
761 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
765 case IPP_TAG_STRING
:
766 case IPP_TAG_TEXTLANG
:
767 case IPP_TAG_NAMELANG
:
770 case IPP_TAG_KEYWORD
:
772 case IPP_TAG_URISCHEME
:
773 case IPP_TAG_CHARSET
:
774 case IPP_TAG_LANGUAGE
:
775 case IPP_TAG_MIMETYPE
:
776 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
777 printf(" \"%s\"", val
->string
.text
);
781 case IPP_TAG_BEGIN_COLLECTION
:
784 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
788 print_attributes(val
->collection
, indent
+ 4);
793 printf("UNKNOWN (%d values)\n", attr
->num_values
);
801 * 'read_cb()' - Read data from a buffer.
804 ssize_t
/* O - Number of bytes read */
805 read_cb(void *data
, /* I - Data */
806 ipp_uchar_t
*buffer
, /* O - Buffer to read */
807 size_t bytes
) /* I - Number of bytes to read */
809 int count
; /* Number of bytes */
813 * Copy bytes from the data buffer to the read buffer...
816 for (count
= bytes
; count
> 0 && rpos
< wused
; count
--, rpos
++)
817 *buffer
++ = wbuffer
[rpos
];
820 * Return the number of bytes read...
823 return (bytes
- count
);
828 * 'write_cb()' - Write data into a buffer.
831 ssize_t
/* O - Number of bytes written */
832 write_cb(void *data
, /* I - Data */
833 ipp_uchar_t
*buffer
, /* I - Buffer to write */
834 size_t bytes
) /* I - Number of bytes to write */
836 int count
; /* Number of bytes */
840 * Loop until all bytes are written...
843 for (count
= bytes
; count
> 0 && wused
< sizeof(wbuffer
); count
--, wused
++)
844 wbuffer
[wused
] = *buffer
++;
847 * Return the number of bytes written...
850 return (bytes
- count
);
855 * End of "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $".