]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testipp.c
2 * "$Id: testipp.c 5753 2006-07-18 19:53:24Z mike $"
4 * IPP test program for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Main entry.
32 * Include necessary headers...
37 #include <cups/string.h>
39 #include "ipp-private.h"
52 int rpos
; /* Current position in buffer */
53 ipp_uchar_t wbuffer
[8192]; /* Write buffer */
54 int wused
; /* Number of bytes in buffer */
55 ipp_uchar_t collection
[] = /* Collection buffer */
57 0x01, 0x01, /* IPP version */
58 0x00, 0x02, /* Print-Job operation */
59 0x00, 0x00, 0x00, 0x01, /* Request ID */
64 0x00, 0x12, /* Name length + name */
65 'a','t','t','r','i','b','u','t','e','s','-',
66 'c','h','a','r','s','e','t',
67 0x00, 0x05, /* Value length + value */
71 0x00, 0x1b, /* Name length + name */
72 'a','t','t','r','i','b','u','t','e','s','-',
73 'n','a','t','u','r','a','l','-','l','a','n',
75 0x00, 0x02, /* Value length + value */
79 0x00, 0x0b, /* Name length + name */
80 'p','r','i','n','t','e','r','-','u','r','i',
81 0x00, 0x1c, /* Value length + value */
82 'i','p','p',':','/','/','l','o','c','a','l',
83 'h','o','s','t','/','p','r','i','n','t','e',
84 'r','s','/','f','o','o',
86 IPP_TAG_JOB
, /* job group tag */
88 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
89 0x00, 0x09, /* Name length + name */
90 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l',
91 0x00, 0x00, /* No value */
92 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
93 0x00, 0x00, /* No name */
94 0x00, 0x0b, /* Value length + value */
95 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
96 IPP_TAG_KEYWORD
, /* keyword tag */
97 0x00, 0x00, /* No name */
98 0x00, 0x04, /* Value length + value */
101 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
102 0x00, 0x00, /* No name */
103 0x00, 0x0a, /* Value length + value */
104 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
105 IPP_TAG_KEYWORD
, /* keyword tag */
106 0x00, 0x00, /* No name */
107 0x00, 0x05, /* Value length + value */
108 'p', 'l', 'a', 'i', 'n',
109 IPP_TAG_END_COLLECTION
, /* endCollection tag */
110 0x00, 0x00, /* No name */
111 0x00, 0x00, /* No value */
113 IPP_TAG_BEGIN_COLLECTION
, /* begCollection tag */
114 0x00, 0x00, /* No name */
115 0x00, 0x00, /* No value */
116 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
117 0x00, 0x00, /* No name */
118 0x00, 0x0b, /* Value length + value */
119 'm', 'e', 'd', 'i', 'a', '-', 'c', 'o', 'l', 'o', 'r',
120 IPP_TAG_KEYWORD
, /* keyword tag */
121 0x00, 0x00, /* No name */
122 0x00, 0x05, /* Value length + value */
123 'p', 'l', 'a', 'i', 'd',
125 IPP_TAG_MEMBERNAME
, /* memberAttrName tag */
126 0x00, 0x00, /* No name */
127 0x00, 0x0a, /* Value length + value */
128 'm', 'e', 'd', 'i', 'a', '-', 't', 'y', 'p', 'e',
129 IPP_TAG_KEYWORD
, /* keyword tag */
130 0x00, 0x00, /* No name */
131 0x00, 0x06, /* Value length + value */
132 'g', 'l', 'o', 's', 's', 'y',
133 IPP_TAG_END_COLLECTION
, /* endCollection tag */
134 0x00, 0x00, /* No name */
135 0x00, 0x00, /* No value */
137 IPP_TAG_END
/* end tag */
145 void hex_dump(const char *title
, ipp_uchar_t
*buffer
, int bytes
);
146 void print_attributes(ipp_t
*ipp
, int indent
);
147 ssize_t
read_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
148 ssize_t
write_cb(void *data
, ipp_uchar_t
*buffer
, size_t bytes
);
152 * 'main()' - Main entry.
155 int /* O - Exit status */
156 main(int argc
, /* I - Number of command-line arguments */
157 char *argv
[]) /* I - Command-line arguments */
159 ipp_t
*cols
[2]; /* Collections */
160 ipp_t
*request
; /* Request */
161 ipp_state_t state
; /* State */
162 int length
; /* Length of data */
163 int fd
; /* File descriptor */
164 int i
; /* Looping var */
165 int status
; /* Status of tests (0 = success, 1 = fail) */
173 * Test request generation code...
176 printf("Create Sample Request: ");
179 request
->request
.op
.version
[0] = 0x01;
180 request
->request
.op
.version
[1] = 0x01;
181 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
182 request
->request
.op
.request_id
= 1;
184 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
185 "attributes-charset", NULL
, "utf-8");
186 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
187 "attributes-natural-language", NULL
, "en");
188 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
189 "printer-uri", NULL
, "ipp://localhost/printers/foo");
192 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
, "blue");
193 ippAddString(cols
[0], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
, "plain");
196 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-color", NULL
, "plaid");
197 ippAddString(cols
[1], IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "media-type", NULL
, "glossy");
199 ippAddCollections(request
, IPP_TAG_JOB
, "media-col", 2, (const ipp_t
**)cols
);
201 length
= ippLength(request
);
202 if (length
!= sizeof(collection
))
204 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
205 length
, (int)sizeof(collection
));
215 printf("Write Sample to Memory: ");
218 while ((state
= ippWriteIO(wbuffer
, write_cb
, 1, NULL
, request
)) != IPP_DATA
)
219 if (state
== IPP_ERROR
)
222 if (state
!= IPP_DATA
)
224 printf("FAIL - %d bytes written.\n", wused
);
227 else if (wused
!= sizeof(collection
))
229 printf("FAIL - wrote %d bytes, expected %d bytes!\n", wused
,
230 (int)sizeof(collection
));
231 hex_dump("Bytes Written", wbuffer
, wused
);
232 hex_dump("Baseline", collection
, sizeof(collection
));
235 else if (memcmp(wbuffer
, collection
, wused
))
237 puts("FAIL - output does not match baseline!");
238 hex_dump("Bytes Written", wbuffer
, wused
);
239 hex_dump("Baseline", collection
, sizeof(collection
));
248 * Read the data back in and confirm...
251 printf("Read Sample from Memory: ");
256 while ((state
= ippReadIO(wbuffer
, read_cb
, 1, NULL
, request
)) != IPP_DATA
)
257 if (state
== IPP_ERROR
)
260 length
= ippLength(request
);
262 if (state
!= IPP_DATA
)
264 printf("FAIL - %d bytes read.\n", rpos
);
267 else if (rpos
!= wused
)
269 printf("FAIL - read %d bytes, expected %d bytes!\n", rpos
, wused
);
270 print_attributes(request
, 8);
273 else if (length
!= sizeof(collection
))
275 printf("FAIL - wrong ippLength(), %d instead of %d bytes!\n",
276 length
, (int)sizeof(collection
));
277 print_attributes(request
, 8);
286 * Test _ippFindOption() private API...
289 fputs("_ippFindOption(\"printer-type\"): ", stdout
);
290 if (_ippFindOption("printer-type"))
305 puts("Core IPP tests failed.");
307 puts("Core IPP tests passed.");
315 for (i
= 1; i
< argc
; i
++)
317 if ((fd
= open(argv
[i
], O_RDONLY
)) < 0)
319 printf("Unable to open \"%s\" - %s\n", argv
[i
], strerror(errno
));
325 while ((state
= ippReadFile(fd
, request
)) == IPP_ATTRIBUTE
);
327 if (state
!= IPP_DATA
)
329 printf("Error reading IPP message from \"%s\"!\n", argv
[i
]);
334 printf("\n%s:\n", argv
[i
]);
335 print_attributes(request
, 4);
348 * 'hex_dump()' - Produce a hex dump of a buffer.
352 hex_dump(const char *title
, /* I - Title */
353 ipp_uchar_t
*buffer
, /* I - Buffer to dump */
354 int bytes
) /* I - Number of bytes */
356 int i
, j
; /* Looping vars */
357 int ch
; /* Current ASCII char */
361 * Show lines of 16 bytes at a time...
364 printf(" %s:\n", title
);
366 for (i
= 0; i
< bytes
; i
+= 16)
375 * Then up to 16 bytes in hex...
378 for (j
= 0; j
< 16; j
++)
380 printf(" %02x", buffer
[i
+ j
]);
385 * Then the ASCII representation of the bytes...
391 for (j
= 0; j
< 16 && (i
+ j
) < bytes
; j
++)
393 ch
= buffer
[i
+ j
] & 127;
395 if (ch
< ' ' || ch
== 127)
407 * 'print_attributes()' - Print the attributes in a request...
411 print_attributes(ipp_t
*ipp
, /* I - IPP request */
412 int indent
) /* I - Indentation */
414 int i
; /* Looping var */
415 ipp_tag_t group
; /* Current group */
416 ipp_attribute_t
*attr
; /* Current attribute */
417 ipp_value_t
*val
; /* Current value */
418 static const char * const tags
[] = /* Value/group tag strings */
421 "operation-attributes-tag",
422 "job-attributes-tag",
423 "end-of-attributes-tag",
424 "printer-attributes-tag",
425 "unsupported-attributes-tag",
426 "subscription-attributes-tag",
427 "event-attributes-tag",
485 "textWithoutLanguage",
486 "nameWithoutLanguage",
498 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
500 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
502 group
= IPP_TAG_ZERO
;
507 if (group
!= attr
->group_tag
)
509 group
= attr
->group_tag
;
512 for (i
= 4; i
< indent
; i
++)
515 printf("%s:\n\n", tags
[group
]);
518 for (i
= 0; i
< indent
; i
++)
521 printf("%s (", attr
->name
);
522 if (attr
->num_values
> 1)
524 printf("%s):", tags
[attr
->value_tag
]);
526 switch (attr
->value_tag
)
529 case IPP_TAG_INTEGER
:
530 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
531 printf(" %d", val
->integer
);
535 case IPP_TAG_BOOLEAN
:
536 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
537 printf(" %s", val
->boolean
? "true" : "false");
542 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
543 printf(" %d-%d", val
->range
.lower
, val
->range
.upper
);
549 time_t vtime
; /* Date/Time value */
550 struct tm
*vdate
; /* Date info */
551 char vstring
[256]; /* Formatted time */
553 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
555 vtime
= ippDateToTime(val
->date
);
556 vdate
= localtime(&vtime
);
557 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
558 printf(" (%s)", vstring
);
564 case IPP_TAG_RESOLUTION
:
565 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
566 printf(" %dx%d%s", val
->resolution
.xres
, val
->resolution
.yres
,
567 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
571 case IPP_TAG_STRING
:
572 case IPP_TAG_TEXTLANG
:
573 case IPP_TAG_NAMELANG
:
576 case IPP_TAG_KEYWORD
:
578 case IPP_TAG_URISCHEME
:
579 case IPP_TAG_CHARSET
:
580 case IPP_TAG_LANGUAGE
:
581 case IPP_TAG_MIMETYPE
:
582 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
583 printf(" \"%s\"", val
->string
.text
);
587 case IPP_TAG_BEGIN_COLLECTION
:
590 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
594 print_attributes(val
->collection
, indent
+ 4);
599 printf("UNKNOWN (%d values)\n", attr
->num_values
);
607 * 'read_cb()' - Read data from a buffer.
610 ssize_t
/* O - Number of bytes read */
611 read_cb(void *data
, /* I - Data */
612 ipp_uchar_t
*buffer
, /* O - Buffer to read */
613 size_t bytes
) /* I - Number of bytes to read */
615 int count
; /* Number of bytes */
619 * Copy bytes from the data buffer to the read buffer...
622 for (count
= bytes
; count
> 0 && rpos
< wused
; count
--, rpos
++)
623 *buffer
++ = wbuffer
[rpos
];
626 * Return the number of bytes read...
629 return (bytes
- count
);
634 * 'write_cb()' - Write data into a buffer.
637 ssize_t
/* O - Number of bytes written */
638 write_cb(void *data
, /* I - Data */
639 ipp_uchar_t
*buffer
, /* I - Buffer to write */
640 size_t bytes
) /* I - Number of bytes to write */
642 int count
; /* Number of bytes */
646 * Loop until all bytes are written...
649 for (count
= bytes
; count
> 0 && wused
< sizeof(wbuffer
); count
--, wused
++)
650 wbuffer
[wused
] = *buffer
++;
653 * Return the number of bytes written...
656 return (bytes
- count
);
661 * End of "$Id: testipp.c 5753 2006-07-18 19:53:24Z mike $".