]>
git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptest.c
2 * "$Id: ipptest.c 5878 2006-08-24 15:55:42Z mike $"
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 */
125 int interval
; /* Test interval */
131 * testipp URL testfile
139 for (i
= 1; i
< argc
; i
++)
141 if (argv
[i
][0] == '-')
143 if (!strcmp(argv
[i
], "-v"))
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
));
459 *tokenptr
++ = *tempptr
++;
463 tokenptr
+= strlen(tokenptr
);
467 *tokenptr
++ = *tempptr
++;
473 case IPP_TAG_BOOLEAN
:
474 if (!strcasecmp(token
, "true"))
475 ippAddBoolean(request
, group
, attr
, 1);
477 ippAddBoolean(request
, group
, attr
, atoi(token
));
480 case IPP_TAG_INTEGER
:
482 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
485 case IPP_TAG_RESOLUTION
:
486 puts(" ERROR: resolution tag not yet supported!");
490 puts(" ERROR: range tag not yet supported!");
494 if (!strchr(token
, ','))
495 ippAddString(request
, group
, value
, attr
, NULL
, token
);
499 * Multiple string values...
502 int num_values
; /* Number of values */
503 char *values
[100], /* Values */
504 *ptr
; /* Pointer to next value */
510 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
513 values
[num_values
] = ptr
;
517 ippAddStrings(request
, group
, value
, attr
, num_values
,
518 NULL
, (const char **)values
);
523 else if (!strcasecmp(token
, "FILE"))
529 get_token(fp
, filename
, sizeof(filename
), &linenum
);
531 else if (!strcasecmp(token
, "STATUS") &&
532 num_statuses
< (int)(sizeof(statuses
) / sizeof(statuses
[0])))
538 get_token(fp
, token
, sizeof(token
), &linenum
);
539 statuses
[num_statuses
] = ippErrorValue(token
);
542 else if (!strcasecmp(token
, "EXPECT") &&
543 num_expects
< (int)(sizeof(expects
) / sizeof(expects
[0])))
546 * Expected attributes...
549 get_token(fp
, token
, sizeof(token
), &linenum
);
550 expects
[num_expects
] = strdup(token
);
553 else if (!strcasecmp(token
, "DISPLAY") &&
554 num_displayed
< (int)(sizeof(displayed
) / sizeof(displayed
[0])))
557 * Display attributes...
560 get_token(fp
, token
, sizeof(token
), &linenum
);
561 displayed
[num_displayed
] = strdup(token
);
566 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
575 * Submit the IPP request...
578 request
->request
.op
.version
[1] = version
;
579 request
->request
.op
.operation_id
= op
;
580 request
->request
.op
.request_id
= 1;
584 printf("%s:\n", ippOpString(op
));
586 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
590 printf(" %-60.60s [", name
);
594 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
596 response
= cupsDoRequest(http
, request
, resource
);
598 if (response
== NULL
)
602 curtime
= time(NULL
);
605 printf(" ERROR %04x (%s) @ %s\n", cupsLastError(),
606 cupsLastErrorString(), ctime(&curtime
));
611 if ((attrptr
= ippFindAttribute(response
, "job-id",
612 IPP_TAG_INTEGER
)) != NULL
)
613 job_id
= attrptr
->values
[0].integer
;
615 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
616 IPP_TAG_INTEGER
)) != NULL
)
617 subscription_id
= attrptr
->values
[0].integer
;
619 for (i
= 0; i
< num_statuses
; i
++)
620 if (response
->request
.status
.status_code
== statuses
[i
])
623 if (i
== num_statuses
&& num_statuses
> 0)
627 for (i
= 0; i
< num_expects
; i
++)
628 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
638 printf(" RECEIVED: %lu bytes in response\n",
639 (unsigned long)ippLength(response
));
643 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
646 else if (num_displayed
> 0)
648 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
651 for (i
= 0; i
< num_displayed
; i
++)
652 if (!strcmp(displayed
[i
], attrptr
->name
))
663 printf(" RECEIVED: %lu bytes in response\n",
664 (unsigned long)ippLength(response
));
666 for (i
= 0; i
< num_statuses
; i
++)
667 if (response
->request
.status
.status_code
== statuses
[i
])
670 if (i
== num_statuses
&& num_statuses
> 0)
673 printf(" status-code = %04x (%s)\n",
674 cupsLastError(), cupsLastErrorString());
676 for (i
= 0; i
< num_expects
; i
++)
677 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
678 printf(" EXPECTED: %s\n", expects
[i
]);
680 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
687 for (i
= 0; i
< num_expects
; i
++)
702 * 'get_tag()' - Get an IPP value or group tag from a name...
705 ipp_tag_t
/* O - Value/group tag */
706 get_tag(const char *name
) /* I - Name of value/group tag */
708 int i
; /* Looping var */
711 for (i
= 0; i
< (sizeof(TagNames
) / sizeof(TagNames
[0])); i
++)
712 if (!strcasecmp(name
, TagNames
[i
]))
713 return ((ipp_tag_t
)i
);
715 return (IPP_TAG_ZERO
);
720 * 'get_tag_string()' - Get the string associated with a tag.
723 const char * /* O - Tag name string */
724 get_tag_string(ipp_tag_t tag
) /* I - IPP tag */
726 if (tag
< (ipp_tag_t
)(sizeof(TagNames
) / sizeof(TagNames
[0])))
727 return (TagNames
[tag
]);
734 * 'get_token()' - Get a token from a file.
737 char * /* O - Token from file or NULL on EOF */
738 get_token(FILE *fp
, /* I - File to read from */
739 char *buf
, /* I - Buffer to read into */
740 int buflen
, /* I - Length of buffer */
741 int *linenum
) /* IO - Current line number */
743 int ch
, /* Character from file */
744 quote
; /* Quoting character */
745 char *bufptr
, /* Pointer into buffer */
746 *bufend
; /* End of buffer */
755 while (isspace(ch
= getc(fp
)))
767 else if (ch
== '\'' || ch
== '\"')
775 bufend
= buf
+ buflen
- 1;
777 while ((ch
= getc(fp
)) != EOF
)
780 else if (bufptr
< bufend
)
792 while ((ch
= getc(fp
)) != EOF
)
801 * Whitespace delimited text...
807 bufend
= buf
+ buflen
- 1;
809 while ((ch
= getc(fp
)) != EOF
)
810 if (isspace(ch
) || ch
== '#')
812 else if (bufptr
< bufend
)
826 * 'print_attr()' - Print an attribute on the screen.
830 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
832 int i
; /* Looping var */
835 if (attr
->name
== NULL
)
837 puts(" -- separator --");
841 printf(" %s (%s%s) = ", attr
->name
,
842 attr
->num_values
> 1 ? "1setOf " : "",
843 get_tag_string(attr
->value_tag
));
845 switch (attr
->value_tag
)
847 case IPP_TAG_INTEGER
:
849 for (i
= 0; i
< attr
->num_values
; i
++)
850 printf("%d ", attr
->values
[i
].integer
);
853 case IPP_TAG_BOOLEAN
:
854 for (i
= 0; i
< attr
->num_values
; i
++)
855 if (attr
->values
[i
].boolean
)
861 case IPP_TAG_NOVALUE
:
866 for (i
= 0; i
< attr
->num_values
; i
++)
867 printf("%d-%d ", attr
->values
[i
].range
.lower
,
868 attr
->values
[i
].range
.upper
);
871 case IPP_TAG_RESOLUTION
:
872 for (i
= 0; i
< attr
->num_values
; i
++)
873 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
874 attr
->values
[i
].resolution
.yres
,
875 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
879 case IPP_TAG_STRING
:
882 case IPP_TAG_KEYWORD
:
883 case IPP_TAG_CHARSET
:
885 case IPP_TAG_MIMETYPE
:
886 case IPP_TAG_LANGUAGE
:
887 for (i
= 0; i
< attr
->num_values
; i
++)
888 printf("\"%s\" ", attr
->values
[i
].string
.text
);
891 case IPP_TAG_TEXTLANG
:
892 case IPP_TAG_NAMELANG
:
893 for (i
= 0; i
< attr
->num_values
; i
++)
894 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
895 attr
->values
[i
].string
.charset
);
899 break; /* anti-compiler-warning-code */
907 * 'usage()' - Show program usage.
911 usage(const char *option
) /* I - Option string or NULL */
914 fprintf(stderr
, "ipptest: Unknown option \"%s\"!\n", option
);
916 fputs("Usage: ipptest [options] URL testfile [ ... testfileN ]\n", stderr
);
917 fputs("Options:\n", stderr
);
919 fputs("-i N Repeat the last test file once every N seconds.\n", stderr
);
920 fputs("-v Show all attributes in response, even on success.\n", stderr
);
927 * End of "$Id: ipptest.c 5878 2006-08-24 15:55:42Z mike $".