]>
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 the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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/string.h>
30 #include "ipp-private.h"
43 int rpos
; /* Current position in buffer */
44 ipp_uchar_t wbuffer
[8192]; /* Write buffer */
45 int wused
; /* Number of bytes in buffer */
46 ipp_uchar_t collection
[] = /* Collection buffer */
48 0x01, 0x01, /* IPP version */
49 0x00, 0x02, /* Print-Job operation */
50 0x00, 0x00, 0x00, 0x01, /* Request ID */
55 0x00, 0x12, /* Name length + name */
56 'a','t','t','r','i','b','u','t','e','s','-',
57 'c','h','a','r','s','e','t',
58 0x00, 0x05, /* Value length + value */
62 0x00, 0x1b, /* Name length + name */
63 'a','t','t','r','i','b','u','t','e','s','-',
64 'n','a','t','u','r','a','l','-','l','a','n',
66 0x00, 0x02, /* Value length + value */
70 0x00, 0x0b, /* Name length + name */
71 'p','r','i','n','t','e','r','-','u','r','i',
72 0x00, 0x1c, /* Value length + value */
73 'i','p','p',':','/','/','l','o','c','a','l',
74 'h','o','s','t','/','p','r','i','n','t','e',
75 'r','s','/','f','o','o',
77 IPP_TAG_JOB
, /* job group tag */
79 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
80 0x00, 0x09, /* Name length + name */
81 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l',
82 0x00, 0x00, /* No value */
83 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
84 0x00, 0x00, /* No name */
85 0x00, 0x0b, /* Value length + value */
86 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
87 IPP_TAG_KEYWORD
, /* keyword tag */
88 0x00, 0x00, /* No name */
89 0x00, 0x04, /* Value length + value */
92 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
93 0x00, 0x00, /* No name */
94 0x00, 0x0a, /* Value length + value */
95 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
96 IPP_TAG_KEYWORD
, /* keyword tag */
97 0x00, 0x00, /* No name */
98 0x00, 0x05, /* Value length + value */
99 'p', 'l', 'a', 'i', 'n',
100 IPP_TAG_END_COLLECTION
, /* endCollection tag */
101 0x00, 0x00, /* No name */
102 0x00, 0x00, /* No value */
104 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
105 0x00, 0x00, /* No name */
106 0x00, 0x00, /* No value */
107 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
108 0x00, 0x00, /* No name */
109 0x00, 0x0b, /* Value length + value */
110 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
111 IPP_TAG_KEYWORD
, /* keyword tag */
112 0x00, 0x00, /* No name */
113 0x00, 0x05, /* Value length + value */
114 'p', 'l', 'a', 'i', 'd',
116 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
117 0x00, 0x00, /* No name */
118 0x00, 0x0a, /* Value length + value */
119 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
120 IPP_TAG_KEYWORD
, /* keyword tag */
121 0x00, 0x00, /* No name */
122 0x00, 0x06, /* Value length + value */
123 'g', 'l', 'o', 's', 's', 'y',
124 IPP_TAG_END_COLLECTION
, /* endCollection tag */
125 0x00, 0x00, /* No name */
126 0x00, 0x00, /* No value */
128 IPP_TAG_END
/* end tag */
136 void hex_dump(const char *title
, ipp_uchar_t
*buffer
, int bytes
);
137 void print_attributes(ipp_t
*ipp
, int indent
);
138 ssize_t
read_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
139 ssize_t
write_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
143 * 'main()' - Main entry.
146 int /* O - Exit status */
147 main(int argc
, /* I - Number of command-line arguments */
148 char *argv
[]) /* I - Command-line arguments */
150 ipp_t
*cols
[2]; /* Collections */
151 ipp_t
*request
; /* Request */
152 ipp_state_t state
; /* State */
153 int length
; /* Length of data */
154 int fd
; /* File descriptor */
155 int i
; /* Looping var */
156 int status
; /* Status of tests (0 = success, 1 = fail) */
164 * Test request generation code...
167 printf("Create Sample Request: ");
170 request
->request
.op
.version
[0] = 0x01;
171 request
->request
.op
.version
[1] = 0x01;
172 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
173 request
->request
.op
.request_id
= 1;
175 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
176 "attributes-charset", NULL
, "utf-8");
177 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
178 "attributes-natural-language", NULL
, "en");
179 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
180 "printer-uri", NULL
, "ipp://localhost/printers/foo");
183 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
, "blue");
184 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
, "plain");
187 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
, "plaid");
188 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
, "glossy");
190 ippAddCollections(request
, IPP_TAG_JOB
, "media-col", 2, (const ipp_t
**)cols
);
192 length
= ippLength(request
);
193 if (length
!= sizeof(collection
))
195 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
196 length
, (int)sizeof(collection
));
206 printf("Write Sample to Memory: ");
209 while ((state
= ippWriteIO(wbuffer
, write_cb
, 1, NULL
, request
)) != IPP_DATA
)
210 if (state
== IPP_ERROR
)
213 if (state
!= IPP_DATA
)
215 printf("FAIL - %d bytes written.\n", wused
);
218 else if (wused
!= sizeof(collection
))
220 printf("FAIL - wrote %d bytes, expected %d bytes!\n", wused
,
221 (int)sizeof(collection
));
222 hex_dump("Bytes Written", wbuffer
, wused
);
223 hex_dump("Baseline", collection
, sizeof(collection
));
226 else if (memcmp(wbuffer
, collection
, wused
))
228 puts("FAIL - output does not match baseline!");
229 hex_dump("Bytes Written", wbuffer
, wused
);
230 hex_dump("Baseline", collection
, sizeof(collection
));
239 * Read the data back in and confirm...
242 printf("Read Sample from Memory: ");
247 while ((state
= ippReadIO(wbuffer
, read_cb
, 1, NULL
, request
)) != IPP_DATA
)
248 if (state
== IPP_ERROR
)
251 length
= ippLength(request
);
253 if (state
!= IPP_DATA
)
255 printf("FAIL - %d bytes read.\n", rpos
);
258 else if (rpos
!= wused
)
260 printf("FAIL - read %d bytes, expected %d bytes!\n", rpos
, wused
);
261 print_attributes(request
, 8);
264 else if (length
!= sizeof(collection
))
266 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
267 length
, (int)sizeof(collection
));
268 print_attributes(request
, 8);
277 * Test _ippFindOption() private API...
280 fputs("_ippFindOption(\"printer-type\"): ", stdout
);
281 if (_ippFindOption("printer-type"))
296 puts("Core IPP tests failed.");
298 puts("Core IPP tests passed.");
306 for (i
= 1; i
< argc
; i
++)
308 if ((fd
= open(argv
[i
], O_RDONLY
)) < 0)
310 printf("Unable to open \"%s\" - %s\n", argv
[i
], strerror(errno
));
316 while ((state
= ippReadFile(fd
, request
)) == IPP_ATTRIBUTE
);
318 if (state
!= IPP_DATA
)
320 printf("Error reading IPP message from \"%s\"!\n", argv
[i
]);
325 printf("\n%s:\n", argv
[i
]);
326 print_attributes(request
, 4);
339 * 'hex_dump()' - Produce a hex dump of a buffer.
343 hex_dump(const char *title
, /* I - Title */
344 ipp_uchar_t
*buffer
, /* I - Buffer to dump */
345 int bytes
) /* I - Number of bytes */
347 int i
, j
; /* Looping vars */
348 int ch
; /* Current ASCII char */
352 * Show lines of 16 bytes at a time...
355 printf(" %s:\n", title
);
357 for (i
= 0; i
< bytes
; i
+= 16)
366 * Then up to 16 bytes in hex...
369 for (j
= 0; j
< 16; j
++)
371 printf(" %02x", buffer
[i
+ j
]);
376 * Then the ASCII representation of the bytes...
382 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
384 ch
= buffer
[i
+ j
] & 127;
386 if (ch
< ' ' || ch
== 127)
398 * 'print_attributes()' - Print the attributes in a request...
402 print_attributes(ipp_t
*ipp
, /* I - IPP request */
403 int indent
) /* I - Indentation */
405 int i
; /* Looping var */
406 ipp_tag_t group
; /* Current group */
407 ipp_attribute_t
*attr
; /* Current attribute */
408 ipp_value_t
*val
; /* Current value */
409 static const char * const tags
[] = /* Value/group tag strings */
412 "operation-attributes-tag",
413 "job-attributes-tag",
414 "end-of-attributes-tag",
415 "printer-attributes-tag",
416 "unsupported-attributes-tag",
417 "subscription-attributes-tag",
418 "event-attributes-tag",
476 "textWithoutLanguage",
477 "nameWithoutLanguage",
489 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
491 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
493 group
= IPP_TAG_ZERO
;
498 if (group
!= attr
->group_tag
)
500 group
= attr
->group_tag
;
503 for (i
= 4; i
< indent
; i
++)
506 printf("%s:\n\n", tags
[group
]);
509 for (i
= 0; i
< indent
; i
++)
512 printf("%s (", attr
->name
);
513 if (attr
->num_values
> 1)
515 printf("%s):", tags
[attr
->value_tag
]);
517 switch (attr
->value_tag
)
520 case IPP_TAG_INTEGER
:
521 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
522 printf(" %d", val
->integer
);
526 case IPP_TAG_BOOLEAN
:
527 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
528 printf(" %s", val
->boolean
? "true" : "false");
533 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
534 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
540 time_t vtime
; /* Date/Time value */
541 struct tm
*vdate
; /* Date info */
542 char vstring
[256]; /* Formatted time */
544 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
546 vtime
= ippDateToTime(val
->date
);
547 vdate
= localtime(&vtime
);
548 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
549 printf(" (%s)", vstring
);
555 case IPP_TAG_RESOLUTION
:
556 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
557 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
558 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
562 case IPP_TAG_STRING
:
563 case IPP_TAG_TEXTLANG
:
564 case IPP_TAG_NAMELANG
:
567 case IPP_TAG_KEYWORD
:
569 case IPP_TAG_URISCHEME
:
570 case IPP_TAG_CHARSET
:
571 case IPP_TAG_LANGUAGE
:
572 case IPP_TAG_MIMETYPE
:
573 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
574 printf(" \"%s\"", val
->string
.text
);
578 case IPP_TAG_BEGIN_COLLECTION
:
581 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
585 print_attributes(val
->collection
, indent
+ 4);
590 printf("UNKNOWN (%d values)\n", attr
->num_values
);
598 * 'read_cb()' - Read data from a buffer.
601 ssize_t
/* O - Number of bytes read */
602 read_cb(void *data
, /* I - Data */
603 ipp_uchar_t
*buffer
, /* O - Buffer to read */
604 size_t bytes
) /* I - Number of bytes to read */
606 int count
; /* Number of bytes */
610 * Copy bytes from the data buffer to the read buffer...
613 for (count
= bytes
; count
> 0 && rpos
< wused
; count
--, rpos
++)
614 *buffer
++ = wbuffer
[rpos
];
617 * Return the number of bytes read...
620 return (bytes
- count
);
625 * 'write_cb()' - Write data into a buffer.
628 ssize_t
/* O - Number of bytes written */
629 write_cb(void *data
, /* I - Data */
630 ipp_uchar_t
*buffer
, /* I - Buffer to write */
631 size_t bytes
) /* I - Number of bytes to write */
633 int count
; /* Number of bytes */
637 * Loop until all bytes are written...
640 for (count
= bytes
; count
> 0 && wused
< sizeof(wbuffer
); count
--, wused
++)
641 wbuffer
[wused
] = *buffer
++;
644 * Return the number of bytes written...
647 return (bytes
- count
);
652 * End of "$Id: testipp.c 6649 2007-07-11 21:46:42Z mike $".