]>
git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptest.c
4 * IPP test command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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
26 * main() - Parse options and do tests.
27 * do_tests() - Do tests as specified in the test file.
28 * get_tag() - Get an IPP value or group tag from a name...
29 * get_token() - Get a token from a file.
30 * print_attr() - Print an attribute on the screen.
31 * usage() - Show program usage.
35 * Include necessary headers...
40 #include <cups/string.h>
44 #include <cups/cups.h>
45 #include <cups/language.h>
52 int Verbosity
= 0; /* Show all attributes? */
53 const char * const TagNames
[] =
54 { /* Value/group tag names */
56 "operation", /* 0x01 */
60 "unsupported-group", /* 0x05 */
61 "subscription", /* 0x06 */
62 "event-notification", /* 0x07 */
63 "", "", "", "", "", "", "", "",
64 "unsupported-value", /* 0x10 */
69 "notsettable", /* 0x15 */
70 "deleteattr", /* 0x16 */
71 "anyvalue", /* 0x17 */
72 "", "", "", "", "", "", "", "", "",
76 "", "", "", "", "", "", "", "", "", "", "", "",
79 "resolution", /* 0x32 */
81 "collection", /* 0x34 */
82 "textlang", /* 0x35 */
83 "namelang", /* 0x36 */
84 "", "", "", "", "", "", "", "", "", "",
90 "urischeme", /* 0x46 */
92 "language", /* 0x48 */
103 int do_tests(const char *, const char *);
104 ipp_op_t
ippOpValue(const char *);
105 ipp_status_t
ippErrorValue(const char *);
106 ipp_tag_t
get_tag(const char *);
107 const char *get_tag_string(ipp_tag_t tag
);
108 char *get_token(FILE *, char *, int, int *linenum
);
109 void print_attr(ipp_attribute_t
*);
110 void usage(const char *option
);
114 * 'main()' - Parse options and do tests.
117 int /* O - Exit status */
118 main(int argc
, /* I - Number of command-line arguments */
119 char *argv
[]) /* I - Command-line arguments */
121 int i
; /* Looping var */
122 int status
; /* Status of tests... */
123 const char *uri
; /* URI to use */
124 const char *testfile
; /* Test file to use */
130 * testipp URL testfile
137 for (i
= 1; i
< argc
; i
++)
139 if (argv
[i
][0] == '-')
141 if (!strcmp(argv
[i
], "-v"))
146 else if (!strncmp(argv
[i
], "ipp://", 6) ||
147 !strncmp(argv
[i
], "http://", 7) ||
148 !strncmp(argv
[i
], "https://", 8))
154 if (!testfile
&& uri
)
168 if (!do_tests(uri
, testfile
))
173 if (!uri
|| !testfile
)
185 * 'do_tests()' - Do tests as specified in the test file.
188 int /* 1 = success, 0 = failure */
189 do_tests(const char *uri
, /* I - URI to connect on */
190 const char *testfile
) /* I - Test file to use */
192 int i
; /* Looping var */
193 int linenum
; /* Current line number */
194 int version
; /* IPP version number to use */
195 http_t
*http
; /* HTTP connection to server */
196 char method
[HTTP_MAX_URI
], /* URI method */
197 userpass
[HTTP_MAX_URI
], /* username:password */
198 server
[HTTP_MAX_URI
], /* Server */
199 resource
[HTTP_MAX_URI
]; /* Resource path */
200 int port
; /* Port number */
201 FILE *fp
; /* Test file */
202 char token
[1024], /* Token from file */
203 *tokenptr
, /* Pointer into token */
204 temp
[1024], /* Temporary string */
205 *tempptr
; /* Pointer into temp string */
206 ipp_t
*request
; /* IPP request */
207 ipp_t
*response
; /* IPP response */
208 ipp_op_t op
; /* Operation */
209 ipp_tag_t group
; /* Current group */
210 ipp_tag_t value
; /* Current value type */
211 ipp_attribute_t
*attrptr
; /* Attribute pointer */
212 char attr
[128]; /* Attribute name */
213 int num_statuses
; /* Number of valid status codes */
214 ipp_status_t statuses
[100]; /* Valid status codes */
215 int num_expects
; /* Number of expected attributes */
216 char *expects
[100]; /* Expected attributes */
217 int num_displayed
; /* Number of displayed attributes */
218 char *displayed
[100]; /* Displayed attributes */
219 char name
[1024]; /* Name of test */
220 char filename
[1024]; /* Filename */
221 int pass
; /* Did we pass the test? */
222 int job_id
; /* Job ID from last operation */
223 int subscription_id
; /* Subscription ID from last operation */
227 * Open the test file...
230 if ((fp
= fopen(testfile
, "r")) == NULL
)
232 printf("Unable to open test file %s - %s\n", testfile
, strerror(errno
));
237 * Connect to the server...
240 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
241 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
243 if ((http
= httpConnect(server
, port
)) == NULL
)
245 printf("Unable to connect to %s on port %d - %s\n", server
, port
,
254 printf("\"%s\":\n", testfile
);
261 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
264 * Expect an open brace...
267 if (strcmp(token
, "{"))
269 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
276 * Initialize things...
279 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
280 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
285 group
= IPP_TAG_ZERO
;
286 value
= IPP_TAG_ZERO
;
292 strcpy(name
, testfile
);
293 if (strrchr(name
, '.') != NULL
)
294 *strrchr(name
, '.') = '\0';
297 * Parse until we see a close brace...
300 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
302 if (!strcmp(token
, "}"))
304 else if (!strcasecmp(token
, "NAME"))
310 get_token(fp
, name
, sizeof(name
), &linenum
);
312 else if (!strcasecmp(token
, "VERSION"))
315 * IPP version number for test...
318 get_token(fp
, temp
, sizeof(temp
), &linenum
);
319 sscanf(temp
, "%*d.%d", &version
);
321 else if (!strcasecmp(token
, "RESOURCE"))
327 get_token(fp
, resource
, sizeof(resource
), &linenum
);
329 else if (!strcasecmp(token
, "OPERATION"))
335 get_token(fp
, token
, sizeof(token
), &linenum
);
336 op
= ippOpValue(token
);
338 else if (!strcasecmp(token
, "GROUP"))
344 get_token(fp
, token
, sizeof(token
), &linenum
);
345 value
= get_tag(token
);
348 ippAddSeparator(request
);
352 else if (!strcasecmp(token
, "DELAY"))
355 * Delay before operation...
360 get_token(fp
, token
, sizeof(token
), &linenum
);
361 if ((delay
= atoi(token
)) > 0)
364 else if (!strcasecmp(token
, "ATTR"))
370 get_token(fp
, token
, sizeof(token
), &linenum
);
371 value
= get_tag(token
);
372 get_token(fp
, attr
, sizeof(attr
), &linenum
);
373 get_token(fp
, temp
, sizeof(temp
), &linenum
);
375 token
[sizeof(token
) - 1] = '\0';
377 for (tempptr
= temp
, tokenptr
= token
;
378 *tempptr
&& tokenptr
< (token
+ sizeof(token
) - 1);)
382 * Substitute a string/number...
385 if (!strncasecmp(tempptr
+ 1, "uri", 3))
387 strlcpy(tokenptr
, uri
, sizeof(token
) - (tokenptr
- token
));
390 else if (!strncasecmp(tempptr
+ 1, "method", 6))
392 strlcpy(tokenptr
, method
, sizeof(token
) - (tokenptr
- token
));
395 else if (!strncasecmp(tempptr
+ 1, "username", 8))
397 strlcpy(tokenptr
, userpass
, sizeof(token
) - (tokenptr
- token
));
400 else if (!strncasecmp(tempptr
+ 1, "hostname", 8))
402 strlcpy(tokenptr
, server
, sizeof(token
) - (tokenptr
- token
));
405 else if (!strncasecmp(tempptr
+ 1, "port", 4))
407 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
411 else if (!strncasecmp(tempptr
+ 1, "resource", 8))
413 strlcpy(tokenptr
, resource
, sizeof(token
) - (tokenptr
- token
));
416 else if (!strncasecmp(tempptr
+ 1, "job-id", 6))
418 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
422 else if (!strncasecmp(tempptr
+ 1, "notify-subscription-id", 22))
424 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
425 "%d", subscription_id
);
428 else if (!strncasecmp(tempptr
+ 1, "user", 4))
430 strlcpy(tokenptr
, cupsUser(), sizeof(token
) - (tokenptr
- token
));
435 *tokenptr
++ = *tempptr
++;
439 tokenptr
+= strlen(tokenptr
);
443 *tokenptr
++ = *tempptr
++;
449 case IPP_TAG_BOOLEAN
:
450 if (!strcasecmp(token
, "true"))
451 ippAddBoolean(request
, group
, attr
, 1);
453 ippAddBoolean(request
, group
, attr
, atoi(token
));
456 case IPP_TAG_INTEGER
:
458 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
461 case IPP_TAG_RESOLUTION
:
462 puts(" ERROR: resolution tag not yet supported!");
466 puts(" ERROR: range tag not yet supported!");
470 if (!strchr(token
, ','))
471 ippAddString(request
, group
, value
, attr
, NULL
, token
);
475 * Multiple string values...
478 int num_values
; /* Number of values */
479 char *values
[100], /* Values */
480 *ptr
; /* Pointer to next value */
486 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
489 values
[num_values
] = ptr
;
493 ippAddStrings(request
, group
, value
, attr
, num_values
,
494 NULL
, (const char **)values
);
499 else if (!strcasecmp(token
, "FILE"))
505 get_token(fp
, filename
, sizeof(filename
), &linenum
);
507 else if (!strcasecmp(token
, "STATUS") &&
508 num_statuses
< (int)(sizeof(statuses
) / sizeof(statuses
[0])))
514 get_token(fp
, token
, sizeof(token
), &linenum
);
515 statuses
[num_statuses
] = ippErrorValue(token
);
518 else if (!strcasecmp(token
, "EXPECT") &&
519 num_expects
< (int)(sizeof(expects
) / sizeof(expects
[0])))
522 * Expected attributes...
525 get_token(fp
, token
, sizeof(token
), &linenum
);
526 expects
[num_expects
] = strdup(token
);
529 else if (!strcasecmp(token
, "DISPLAY") &&
530 num_displayed
< (int)(sizeof(displayed
) / sizeof(displayed
[0])))
533 * Display attributes...
536 get_token(fp
, token
, sizeof(token
), &linenum
);
537 displayed
[num_displayed
] = strdup(token
);
542 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
551 * Submit the IPP request...
554 request
->request
.op
.version
[1] = version
;
555 request
->request
.op
.operation_id
= op
;
556 request
->request
.op
.request_id
= 1;
560 printf("%s:\n", ippOpString(op
));
562 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
566 printf(" %-60.60s [", name
);
570 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
572 response
= cupsDoRequest(http
, request
, resource
);
574 if (response
== NULL
)
578 curtime
= time(NULL
);
581 printf(" ERROR %04x (%s) @ %s\n", cupsLastError(),
582 cupsLastErrorString(), ctime(&curtime
));
587 if ((attrptr
= ippFindAttribute(response
, "job-id",
588 IPP_TAG_INTEGER
)) != NULL
)
589 job_id
= attrptr
->values
[0].integer
;
591 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
592 IPP_TAG_INTEGER
)) != NULL
)
593 subscription_id
= attrptr
->values
[0].integer
;
595 for (i
= 0; i
< num_statuses
; i
++)
596 if (response
->request
.status
.status_code
== statuses
[i
])
599 if (i
== num_statuses
&& num_statuses
> 0)
603 for (i
= 0; i
< num_expects
; i
++)
604 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
614 printf(" RECEIVED: %lu bytes in response\n",
615 (unsigned long)ippLength(response
));
619 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
622 else if (num_displayed
> 0)
624 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
627 for (i
= 0; i
< num_displayed
; i
++)
628 if (!strcmp(displayed
[i
], attrptr
->name
))
639 printf(" RECEIVED: %lu bytes in response\n",
640 (unsigned long)ippLength(response
));
642 for (i
= 0; i
< num_statuses
; i
++)
643 if (response
->request
.status
.status_code
== statuses
[i
])
646 if (i
== num_statuses
&& num_statuses
> 0)
649 printf(" status-code = %04x (%s)\n",
650 cupsLastError(), cupsLastErrorString());
652 for (i
= 0; i
< num_expects
; i
++)
653 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
654 printf(" EXPECTED: %s\n", expects
[i
]);
656 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
663 for (i
= 0; i
< num_expects
; i
++)
678 * 'get_tag()' - Get an IPP value or group tag from a name...
681 ipp_tag_t
/* O - Value/group tag */
682 get_tag(const char *name
) /* I - Name of value/group tag */
684 int i
; /* Looping var */
687 for (i
= 0; i
< (sizeof(TagNames
) / sizeof(TagNames
[0])); i
++)
688 if (!strcasecmp(name
, TagNames
[i
]))
689 return ((ipp_tag_t
)i
);
691 return (IPP_TAG_ZERO
);
696 * 'get_tag_string()' - Get the string associated with a tag.
699 const char * /* O - Tag name string */
700 get_tag_string(ipp_tag_t tag
) /* I - IPP tag */
702 if (tag
< (ipp_tag_t
)(sizeof(TagNames
) / sizeof(TagNames
[0])))
703 return (TagNames
[tag
]);
710 * 'get_token()' - Get a token from a file.
713 char * /* O - Token from file or NULL on EOF */
714 get_token(FILE *fp
, /* I - File to read from */
715 char *buf
, /* I - Buffer to read into */
716 int buflen
, /* I - Length of buffer */
717 int *linenum
) /* IO - Current line number */
719 int ch
, /* Character from file */
720 quote
; /* Quoting character */
721 char *bufptr
, /* Pointer into buffer */
722 *bufend
; /* End of buffer */
731 while (isspace(ch
= getc(fp
)))
743 else if (ch
== '\'' || ch
== '\"')
751 bufend
= buf
+ buflen
- 1;
753 while ((ch
= getc(fp
)) != EOF
)
756 else if (bufptr
< bufend
)
768 while ((ch
= getc(fp
)) != EOF
)
777 * Whitespace delimited text...
783 bufend
= buf
+ buflen
- 1;
785 while ((ch
= getc(fp
)) != EOF
)
786 if (isspace(ch
) || ch
== '#')
788 else if (bufptr
< bufend
)
802 * 'print_attr()' - Print an attribute on the screen.
806 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
808 int i
; /* Looping var */
811 if (attr
->name
== NULL
)
813 puts(" -- separator --");
817 printf(" %s (%s) = ", attr
->name
, get_tag_string(attr
->value_tag
));
819 switch (attr
->value_tag
)
821 case IPP_TAG_INTEGER
:
823 for (i
= 0; i
< attr
->num_values
; i
++)
824 printf("%d ", attr
->values
[i
].integer
);
827 case IPP_TAG_BOOLEAN
:
828 for (i
= 0; i
< attr
->num_values
; i
++)
829 if (attr
->values
[i
].boolean
)
835 case IPP_TAG_NOVALUE
:
840 for (i
= 0; i
< attr
->num_values
; i
++)
841 printf("%d-%d ", attr
->values
[i
].range
.lower
,
842 attr
->values
[i
].range
.upper
);
845 case IPP_TAG_RESOLUTION
:
846 for (i
= 0; i
< attr
->num_values
; i
++)
847 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
848 attr
->values
[i
].resolution
.yres
,
849 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
853 case IPP_TAG_STRING
:
856 case IPP_TAG_KEYWORD
:
857 case IPP_TAG_CHARSET
:
859 case IPP_TAG_MIMETYPE
:
860 case IPP_TAG_LANGUAGE
:
861 for (i
= 0; i
< attr
->num_values
; i
++)
862 printf("\"%s\" ", attr
->values
[i
].string
.text
);
865 case IPP_TAG_TEXTLANG
:
866 case IPP_TAG_NAMELANG
:
867 for (i
= 0; i
< attr
->num_values
; i
++)
868 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
869 attr
->values
[i
].string
.charset
);
873 break; /* anti-compiler-warning-code */
881 * 'usage()' - Show program usage.
885 usage(const char *option
) /* I - Option string or NULL */
888 fprintf(stderr
, "ipptest: Unknown option \"%s\"!\n", option
);
890 fputs("Usage: ipptest [options] URL testfile [ ... testfileN ]\n", stderr
);
891 fputs("Options:\n", stderr
);
893 fputs("-v Show all attributes in response, even on success.\n", stderr
);