]>
git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptest.c
2 * "$Id: ipptest.c 6649 2007-07-11 21:46:42Z mike $"
4 * IPP test command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 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/".
17 * main() - Parse options and do tests.
18 * do_tests() - Do tests as specified in the test file.
19 * get_tag() - Get an IPP value or group tag from a name...
20 * get_token() - Get a token from a file.
21 * print_attr() - Print an attribute on the screen.
22 * usage() - Show program usage.
26 * Include necessary headers...
31 #include <cups/string.h>
35 #include <cups/cups.h>
36 #include <cups/language.h>
43 int Verbosity
= 0; /* Show all attributes? */
44 const char * const TagNames
[] =
45 { /* Value/group tag names */
47 "operation", /* 0x01 */
51 "unsupported-group", /* 0x05 */
52 "subscription", /* 0x06 */
53 "event-notification", /* 0x07 */
54 "", "", "", "", "", "", "", "",
55 "unsupported-value", /* 0x10 */
60 "notsettable", /* 0x15 */
61 "deleteattr", /* 0x16 */
62 "anyvalue", /* 0x17 */
63 "", "", "", "", "", "", "", "", "",
67 "", "", "", "", "", "", "", "", "", "", "", "",
70 "resolution", /* 0x32 */
72 "collection", /* 0x34 */
73 "textlang", /* 0x35 */
74 "namelang", /* 0x36 */
75 "", "", "", "", "", "", "", "", "", "",
81 "urischeme", /* 0x46 */
83 "language", /* 0x48 */
94 int do_tests(const char *, const char *);
95 ipp_op_t
ippOpValue(const char *);
96 ipp_status_t
ippErrorValue(const char *);
97 ipp_tag_t
get_tag(const char *);
98 const char *get_tag_string(ipp_tag_t tag
);
99 char *get_token(FILE *, char *, int, int *linenum
);
100 void print_attr(ipp_attribute_t
*);
101 void usage(const char *option
);
105 * 'main()' - Parse options and do tests.
108 int /* O - Exit status */
109 main(int argc
, /* I - Number of command-line arguments */
110 char *argv
[]) /* I - Command-line arguments */
112 int i
; /* Looping var */
113 int status
; /* Status of tests... */
114 const char *uri
; /* URI to use */
115 const char *testfile
; /* Test file to use */
116 int interval
; /* Test interval */
122 * testipp URL testfile
130 for (i
= 1; i
< argc
; i
++)
132 if (argv
[i
][0] == '-')
134 if (!strcmp(argv
[i
], "-v"))
136 else if (!strcmp(argv
[i
], "-d"))
145 else if (!strcmp(argv
[i
], "-i"))
152 interval
= atoi(argv
[i
]);
157 else if (!strncmp(argv
[i
], "ipp://", 6) ||
158 !strncmp(argv
[i
], "http://", 7) ||
159 !strncmp(argv
[i
], "https://", 8))
165 if (!testfile
&& uri
)
179 if (!do_tests(uri
, testfile
))
184 if (!uri
|| !testfile
)
188 * Loop if the interval is set...
196 do_tests(uri
, testfile
);
209 * 'do_tests()' - Do tests as specified in the test file.
212 int /* 1 = success, 0 = failure */
213 do_tests(const char *uri
, /* I - URI to connect on */
214 const char *testfile
) /* I - Test file to use */
216 int i
; /* Looping var */
217 int linenum
; /* Current line number */
218 int version
; /* IPP version number to use */
219 http_t
*http
; /* HTTP connection to server */
220 char method
[HTTP_MAX_URI
], /* URI method */
221 userpass
[HTTP_MAX_URI
], /* username:password */
222 server
[HTTP_MAX_URI
], /* Server */
223 resource
[HTTP_MAX_URI
]; /* Resource path */
224 int port
; /* Port number */
225 FILE *fp
; /* Test file */
226 char token
[1024], /* Token from file */
227 *tokenptr
, /* Pointer into token */
228 temp
[1024], /* Temporary string */
229 *tempptr
; /* Pointer into temp string */
230 ipp_t
*request
; /* IPP request */
231 ipp_t
*response
; /* IPP response */
232 ipp_op_t op
; /* Operation */
233 ipp_tag_t group
; /* Current group */
234 ipp_tag_t value
; /* Current value type */
235 ipp_attribute_t
*attrptr
; /* Attribute pointer */
236 char attr
[128]; /* Attribute name */
237 int num_statuses
; /* Number of valid status codes */
238 ipp_status_t statuses
[100]; /* Valid status codes */
239 int num_expects
; /* Number of expected attributes */
240 char *expects
[100]; /* Expected attributes */
241 int num_displayed
; /* Number of displayed attributes */
242 char *displayed
[100]; /* Displayed attributes */
243 char name
[1024]; /* Name of test */
244 char filename
[1024]; /* Filename */
245 int pass
; /* Did we pass the test? */
246 int job_id
; /* Job ID from last operation */
247 int subscription_id
; /* Subscription ID from last operation */
251 * Open the test file...
254 if ((fp
= fopen(testfile
, "r")) == NULL
)
256 printf("Unable to open test file %s - %s\n", testfile
, strerror(errno
));
261 * Connect to the server...
264 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
265 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
267 if ((http
= httpConnect(server
, port
)) == NULL
)
269 printf("Unable to connect to %s on port %d - %s\n", server
, port
,
278 printf("\"%s\":\n", testfile
);
285 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
288 * Expect an open brace...
291 if (strcmp(token
, "{"))
293 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
300 * Initialize things...
303 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
304 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
309 group
= IPP_TAG_ZERO
;
310 value
= IPP_TAG_ZERO
;
316 strcpy(name
, testfile
);
317 if (strrchr(name
, '.') != NULL
)
318 *strrchr(name
, '.') = '\0';
321 * Parse until we see a close brace...
324 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
326 if (!strcmp(token
, "}"))
328 else if (!strcasecmp(token
, "NAME"))
334 get_token(fp
, name
, sizeof(name
), &linenum
);
336 else if (!strcasecmp(token
, "VERSION"))
339 * IPP version number for test...
342 get_token(fp
, temp
, sizeof(temp
), &linenum
);
343 sscanf(temp
, "%*d.%d", &version
);
345 else if (!strcasecmp(token
, "RESOURCE"))
351 get_token(fp
, resource
, sizeof(resource
), &linenum
);
353 else if (!strcasecmp(token
, "OPERATION"))
359 get_token(fp
, token
, sizeof(token
), &linenum
);
360 op
= ippOpValue(token
);
362 else if (!strcasecmp(token
, "GROUP"))
368 get_token(fp
, token
, sizeof(token
), &linenum
);
369 value
= get_tag(token
);
372 ippAddSeparator(request
);
376 else if (!strcasecmp(token
, "DELAY"))
379 * Delay before operation...
384 get_token(fp
, token
, sizeof(token
), &linenum
);
385 if ((delay
= atoi(token
)) > 0)
388 else if (!strcasecmp(token
, "ATTR"))
394 get_token(fp
, token
, sizeof(token
), &linenum
);
395 value
= get_tag(token
);
396 get_token(fp
, attr
, sizeof(attr
), &linenum
);
397 get_token(fp
, temp
, sizeof(temp
), &linenum
);
399 token
[sizeof(token
) - 1] = '\0';
401 for (tempptr
= temp
, tokenptr
= token
;
402 *tempptr
&& tokenptr
< (token
+ sizeof(token
) - 1);)
406 * Substitute a string/number...
409 if (!strncasecmp(tempptr
+ 1, "uri", 3))
411 strlcpy(tokenptr
, uri
, sizeof(token
) - (tokenptr
- token
));
414 else if (!strncasecmp(tempptr
+ 1, "method", 6))
416 strlcpy(tokenptr
, method
, sizeof(token
) - (tokenptr
- token
));
419 else if (!strncasecmp(tempptr
+ 1, "username", 8))
421 strlcpy(tokenptr
, userpass
, sizeof(token
) - (tokenptr
- token
));
424 else if (!strncasecmp(tempptr
+ 1, "hostname", 8))
426 strlcpy(tokenptr
, server
, sizeof(token
) - (tokenptr
- token
));
429 else if (!strncasecmp(tempptr
+ 1, "port", 4))
431 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
435 else if (!strncasecmp(tempptr
+ 1, "resource", 8))
437 strlcpy(tokenptr
, resource
, sizeof(token
) - (tokenptr
- token
));
440 else if (!strncasecmp(tempptr
+ 1, "job-id", 6))
442 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
446 else if (!strncasecmp(tempptr
+ 1, "notify-subscription-id", 22))
448 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
449 "%d", subscription_id
);
452 else if (!strncasecmp(tempptr
+ 1, "user", 4))
454 strlcpy(tokenptr
, cupsUser(), sizeof(token
) - (tokenptr
- token
));
457 else if (!strncasecmp(tempptr
+ 1, "ENV[", 4))
459 char *end
; /* End of $ENV[name] */
462 if ((end
= strchr(tempptr
+ 5, ']')) != NULL
)
466 getenv(tempptr
+ 5) ? getenv(tempptr
+ 5) : tempptr
+ 5,
467 sizeof(token
) - (tokenptr
- token
));
472 *tokenptr
++ = *tempptr
++;
478 *tokenptr
++ = *tempptr
++;
482 tokenptr
+= strlen(tokenptr
);
486 *tokenptr
++ = *tempptr
++;
492 case IPP_TAG_BOOLEAN
:
493 if (!strcasecmp(token
, "true"))
494 ippAddBoolean(request
, group
, attr
, 1);
496 ippAddBoolean(request
, group
, attr
, atoi(token
));
499 case IPP_TAG_INTEGER
:
501 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
504 case IPP_TAG_RESOLUTION
:
505 puts(" ERROR: resolution tag not yet supported!");
509 puts(" ERROR: range tag not yet supported!");
513 if (!strchr(token
, ','))
514 ippAddString(request
, group
, value
, attr
, NULL
, token
);
518 * Multiple string values...
521 int num_values
; /* Number of values */
522 char *values
[100], /* Values */
523 *ptr
; /* Pointer to next value */
529 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
532 values
[num_values
] = ptr
;
536 ippAddStrings(request
, group
, value
, attr
, num_values
,
537 NULL
, (const char **)values
);
542 else if (!strcasecmp(token
, "FILE"))
548 get_token(fp
, filename
, sizeof(filename
), &linenum
);
550 else if (!strcasecmp(token
, "STATUS") &&
551 num_statuses
< (int)(sizeof(statuses
) / sizeof(statuses
[0])))
557 get_token(fp
, token
, sizeof(token
), &linenum
);
558 statuses
[num_statuses
] = ippErrorValue(token
);
561 else if (!strcasecmp(token
, "EXPECT") &&
562 num_expects
< (int)(sizeof(expects
) / sizeof(expects
[0])))
565 * Expected attributes...
568 get_token(fp
, token
, sizeof(token
), &linenum
);
569 expects
[num_expects
] = strdup(token
);
572 else if (!strcasecmp(token
, "DISPLAY") &&
573 num_displayed
< (int)(sizeof(displayed
) / sizeof(displayed
[0])))
576 * Display attributes...
579 get_token(fp
, token
, sizeof(token
), &linenum
);
580 displayed
[num_displayed
] = strdup(token
);
585 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
594 * Submit the IPP request...
597 request
->request
.op
.version
[1] = version
;
598 request
->request
.op
.operation_id
= op
;
599 request
->request
.op
.request_id
= 1;
603 printf("%s:\n", ippOpString(op
));
605 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
609 printf(" %-60.60s [", name
);
613 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
615 response
= cupsDoIORequest(http
, request
, resource
, -1,
618 if (response
== NULL
)
622 curtime
= time(NULL
);
625 printf(" ERROR %04x (%s) @ %s\n", cupsLastError(),
626 cupsLastErrorString(), ctime(&curtime
));
631 if ((attrptr
= ippFindAttribute(response
, "job-id",
632 IPP_TAG_INTEGER
)) != NULL
)
633 job_id
= attrptr
->values
[0].integer
;
635 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
636 IPP_TAG_INTEGER
)) != NULL
)
637 subscription_id
= attrptr
->values
[0].integer
;
639 for (i
= 0; i
< num_statuses
; i
++)
640 if (response
->request
.status
.status_code
== statuses
[i
])
643 if (i
== num_statuses
&& num_statuses
> 0)
647 for (i
= 0; i
< num_expects
; i
++)
648 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
658 printf(" RECEIVED: %lu bytes in response\n",
659 (unsigned long)ippLength(response
));
663 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
666 else if (num_displayed
> 0)
668 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
671 for (i
= 0; i
< num_displayed
; i
++)
672 if (!strcmp(displayed
[i
], attrptr
->name
))
683 printf(" RECEIVED: %lu bytes in response\n",
684 (unsigned long)ippLength(response
));
686 for (i
= 0; i
< num_statuses
; i
++)
687 if (response
->request
.status
.status_code
== statuses
[i
])
690 if (i
== num_statuses
&& num_statuses
> 0)
693 printf(" status-code = %04x (%s)\n",
694 cupsLastError(), ippErrorString(cupsLastError()));
696 for (i
= 0; i
< num_expects
; i
++)
697 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
698 printf(" EXPECTED: %s\n", expects
[i
]);
700 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
707 for (i
= 0; i
< num_expects
; i
++)
722 * 'get_tag()' - Get an IPP value or group tag from a name...
725 ipp_tag_t
/* O - Value/group tag */
726 get_tag(const char *name
) /* I - Name of value/group tag */
728 int i
; /* Looping var */
731 for (i
= 0; i
< (sizeof(TagNames
) / sizeof(TagNames
[0])); i
++)
732 if (!strcasecmp(name
, TagNames
[i
]))
733 return ((ipp_tag_t
)i
);
735 return (IPP_TAG_ZERO
);
740 * 'get_tag_string()' - Get the string associated with a tag.
743 const char * /* O - Tag name string */
744 get_tag_string(ipp_tag_t tag
) /* I - IPP tag */
746 if (tag
< (ipp_tag_t
)(sizeof(TagNames
) / sizeof(TagNames
[0])))
747 return (TagNames
[tag
]);
754 * 'get_token()' - Get a token from a file.
757 char * /* O - Token from file or NULL on EOF */
758 get_token(FILE *fp
, /* I - File to read from */
759 char *buf
, /* I - Buffer to read into */
760 int buflen
, /* I - Length of buffer */
761 int *linenum
) /* IO - Current line number */
763 int ch
, /* Character from file */
764 quote
; /* Quoting character */
765 char *bufptr
, /* Pointer into buffer */
766 *bufend
; /* End of buffer */
775 while (isspace(ch
= getc(fp
)))
787 else if (ch
== '\'' || ch
== '\"')
795 bufend
= buf
+ buflen
- 1;
797 while ((ch
= getc(fp
)) != EOF
)
800 else if (bufptr
< bufend
)
812 while ((ch
= getc(fp
)) != EOF
)
821 * Whitespace delimited text...
827 bufend
= buf
+ buflen
- 1;
829 while ((ch
= getc(fp
)) != EOF
)
830 if (isspace(ch
) || ch
== '#')
832 else if (bufptr
< bufend
)
846 * 'print_attr()' - Print an attribute on the screen.
850 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
852 int i
; /* Looping var */
855 if (attr
->name
== NULL
)
857 puts(" -- separator --");
861 printf(" %s (%s%s) = ", attr
->name
,
862 attr
->num_values
> 1 ? "1setOf " : "",
863 get_tag_string(attr
->value_tag
));
865 switch (attr
->value_tag
)
867 case IPP_TAG_INTEGER
:
869 for (i
= 0; i
< attr
->num_values
; i
++)
870 printf("%d ", attr
->values
[i
].integer
);
873 case IPP_TAG_BOOLEAN
:
874 for (i
= 0; i
< attr
->num_values
; i
++)
875 if (attr
->values
[i
].boolean
)
881 case IPP_TAG_NOVALUE
:
886 for (i
= 0; i
< attr
->num_values
; i
++)
887 printf("%d-%d ", attr
->values
[i
].range
.lower
,
888 attr
->values
[i
].range
.upper
);
891 case IPP_TAG_RESOLUTION
:
892 for (i
= 0; i
< attr
->num_values
; i
++)
893 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
894 attr
->values
[i
].resolution
.yres
,
895 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
899 case IPP_TAG_STRING
:
902 case IPP_TAG_KEYWORD
:
903 case IPP_TAG_CHARSET
:
905 case IPP_TAG_MIMETYPE
:
906 case IPP_TAG_LANGUAGE
:
907 for (i
= 0; i
< attr
->num_values
; i
++)
908 printf("\"%s\" ", attr
->values
[i
].string
.text
);
911 case IPP_TAG_TEXTLANG
:
912 case IPP_TAG_NAMELANG
:
913 for (i
= 0; i
< attr
->num_values
; i
++)
914 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
915 attr
->values
[i
].string
.charset
);
919 break; /* anti-compiler-warning-code */
927 * 'usage()' - Show program usage.
931 usage(const char *option
) /* I - Option string or NULL */
934 fprintf(stderr
, "ipptest: Unknown option \"%s\"!\n", option
);
936 fputs("Usage: ipptest [options] URL testfile [ ... testfileN ]\n", stderr
);
937 fputs("Options:\n", stderr
);
939 fputs("-i N Repeat the last test file once every N seconds.\n", stderr
);
940 fputs("-v Show all attributes in response, even on success.\n", stderr
);
947 * End of "$Id: ipptest.c 6649 2007-07-11 21:46:42Z mike $".