]>
git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptest.c
2 * "$Id: ipptest.c 6503 2007-05-01 23:06:44Z mike $"
4 * IPP test command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 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
], "-d"))
154 else if (!strcmp(argv
[i
], "-i"))
161 interval
= atoi(argv
[i
]);
166 else if (!strncmp(argv
[i
], "ipp://", 6) ||
167 !strncmp(argv
[i
], "http://", 7) ||
168 !strncmp(argv
[i
], "https://", 8))
174 if (!testfile
&& uri
)
188 if (!do_tests(uri
, testfile
))
193 if (!uri
|| !testfile
)
197 * Loop if the interval is set...
205 do_tests(uri
, testfile
);
218 * 'do_tests()' - Do tests as specified in the test file.
221 int /* 1 = success, 0 = failure */
222 do_tests(const char *uri
, /* I - URI to connect on */
223 const char *testfile
) /* I - Test file to use */
225 int i
; /* Looping var */
226 int linenum
; /* Current line number */
227 int version
; /* IPP version number to use */
228 http_t
*http
; /* HTTP connection to server */
229 char method
[HTTP_MAX_URI
], /* URI method */
230 userpass
[HTTP_MAX_URI
], /* username:password */
231 server
[HTTP_MAX_URI
], /* Server */
232 resource
[HTTP_MAX_URI
]; /* Resource path */
233 int port
; /* Port number */
234 FILE *fp
; /* Test file */
235 char token
[1024], /* Token from file */
236 *tokenptr
, /* Pointer into token */
237 temp
[1024], /* Temporary string */
238 *tempptr
; /* Pointer into temp string */
239 ipp_t
*request
; /* IPP request */
240 ipp_t
*response
; /* IPP response */
241 ipp_op_t op
; /* Operation */
242 ipp_tag_t group
; /* Current group */
243 ipp_tag_t value
; /* Current value type */
244 ipp_attribute_t
*attrptr
; /* Attribute pointer */
245 char attr
[128]; /* Attribute name */
246 int num_statuses
; /* Number of valid status codes */
247 ipp_status_t statuses
[100]; /* Valid status codes */
248 int num_expects
; /* Number of expected attributes */
249 char *expects
[100]; /* Expected attributes */
250 int num_displayed
; /* Number of displayed attributes */
251 char *displayed
[100]; /* Displayed attributes */
252 char name
[1024]; /* Name of test */
253 char filename
[1024]; /* Filename */
254 int pass
; /* Did we pass the test? */
255 int job_id
; /* Job ID from last operation */
256 int subscription_id
; /* Subscription ID from last operation */
260 * Open the test file...
263 if ((fp
= fopen(testfile
, "r")) == NULL
)
265 printf("Unable to open test file %s - %s\n", testfile
, strerror(errno
));
270 * Connect to the server...
273 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
274 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
276 if ((http
= httpConnect(server
, port
)) == NULL
)
278 printf("Unable to connect to %s on port %d - %s\n", server
, port
,
287 printf("\"%s\":\n", testfile
);
294 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
297 * Expect an open brace...
300 if (strcmp(token
, "{"))
302 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
309 * Initialize things...
312 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
313 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
318 group
= IPP_TAG_ZERO
;
319 value
= IPP_TAG_ZERO
;
325 strcpy(name
, testfile
);
326 if (strrchr(name
, '.') != NULL
)
327 *strrchr(name
, '.') = '\0';
330 * Parse until we see a close brace...
333 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
335 if (!strcmp(token
, "}"))
337 else if (!strcasecmp(token
, "NAME"))
343 get_token(fp
, name
, sizeof(name
), &linenum
);
345 else if (!strcasecmp(token
, "VERSION"))
348 * IPP version number for test...
351 get_token(fp
, temp
, sizeof(temp
), &linenum
);
352 sscanf(temp
, "%*d.%d", &version
);
354 else if (!strcasecmp(token
, "RESOURCE"))
360 get_token(fp
, resource
, sizeof(resource
), &linenum
);
362 else if (!strcasecmp(token
, "OPERATION"))
368 get_token(fp
, token
, sizeof(token
), &linenum
);
369 op
= ippOpValue(token
);
371 else if (!strcasecmp(token
, "GROUP"))
377 get_token(fp
, token
, sizeof(token
), &linenum
);
378 value
= get_tag(token
);
381 ippAddSeparator(request
);
385 else if (!strcasecmp(token
, "DELAY"))
388 * Delay before operation...
393 get_token(fp
, token
, sizeof(token
), &linenum
);
394 if ((delay
= atoi(token
)) > 0)
397 else if (!strcasecmp(token
, "ATTR"))
403 get_token(fp
, token
, sizeof(token
), &linenum
);
404 value
= get_tag(token
);
405 get_token(fp
, attr
, sizeof(attr
), &linenum
);
406 get_token(fp
, temp
, sizeof(temp
), &linenum
);
408 token
[sizeof(token
) - 1] = '\0';
410 for (tempptr
= temp
, tokenptr
= token
;
411 *tempptr
&& tokenptr
< (token
+ sizeof(token
) - 1);)
415 * Substitute a string/number...
418 if (!strncasecmp(tempptr
+ 1, "uri", 3))
420 strlcpy(tokenptr
, uri
, sizeof(token
) - (tokenptr
- token
));
423 else if (!strncasecmp(tempptr
+ 1, "method", 6))
425 strlcpy(tokenptr
, method
, sizeof(token
) - (tokenptr
- token
));
428 else if (!strncasecmp(tempptr
+ 1, "username", 8))
430 strlcpy(tokenptr
, userpass
, sizeof(token
) - (tokenptr
- token
));
433 else if (!strncasecmp(tempptr
+ 1, "hostname", 8))
435 strlcpy(tokenptr
, server
, sizeof(token
) - (tokenptr
- token
));
438 else if (!strncasecmp(tempptr
+ 1, "port", 4))
440 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
444 else if (!strncasecmp(tempptr
+ 1, "resource", 8))
446 strlcpy(tokenptr
, resource
, sizeof(token
) - (tokenptr
- token
));
449 else if (!strncasecmp(tempptr
+ 1, "job-id", 6))
451 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
455 else if (!strncasecmp(tempptr
+ 1, "notify-subscription-id", 22))
457 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
458 "%d", subscription_id
);
461 else if (!strncasecmp(tempptr
+ 1, "user", 4))
463 strlcpy(tokenptr
, cupsUser(), sizeof(token
) - (tokenptr
- token
));
466 else if (!strncasecmp(tempptr
+ 1, "ENV[", 4))
468 char *end
; /* End of $ENV[name] */
471 if ((end
= strchr(tempptr
+ 5, ']')) != NULL
)
475 getenv(tempptr
+ 5) ? getenv(tempptr
+ 5) : tempptr
+ 5,
476 sizeof(token
) - (tokenptr
- token
));
481 *tokenptr
++ = *tempptr
++;
487 *tokenptr
++ = *tempptr
++;
491 tokenptr
+= strlen(tokenptr
);
495 *tokenptr
++ = *tempptr
++;
501 case IPP_TAG_BOOLEAN
:
502 if (!strcasecmp(token
, "true"))
503 ippAddBoolean(request
, group
, attr
, 1);
505 ippAddBoolean(request
, group
, attr
, atoi(token
));
508 case IPP_TAG_INTEGER
:
510 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
513 case IPP_TAG_RESOLUTION
:
514 puts(" ERROR: resolution tag not yet supported!");
518 puts(" ERROR: range tag not yet supported!");
522 if (!strchr(token
, ','))
523 ippAddString(request
, group
, value
, attr
, NULL
, token
);
527 * Multiple string values...
530 int num_values
; /* Number of values */
531 char *values
[100], /* Values */
532 *ptr
; /* Pointer to next value */
538 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
541 values
[num_values
] = ptr
;
545 ippAddStrings(request
, group
, value
, attr
, num_values
,
546 NULL
, (const char **)values
);
551 else if (!strcasecmp(token
, "FILE"))
557 get_token(fp
, filename
, sizeof(filename
), &linenum
);
559 else if (!strcasecmp(token
, "STATUS") &&
560 num_statuses
< (int)(sizeof(statuses
) / sizeof(statuses
[0])))
566 get_token(fp
, token
, sizeof(token
), &linenum
);
567 statuses
[num_statuses
] = ippErrorValue(token
);
570 else if (!strcasecmp(token
, "EXPECT") &&
571 num_expects
< (int)(sizeof(expects
) / sizeof(expects
[0])))
574 * Expected attributes...
577 get_token(fp
, token
, sizeof(token
), &linenum
);
578 expects
[num_expects
] = strdup(token
);
581 else if (!strcasecmp(token
, "DISPLAY") &&
582 num_displayed
< (int)(sizeof(displayed
) / sizeof(displayed
[0])))
585 * Display attributes...
588 get_token(fp
, token
, sizeof(token
), &linenum
);
589 displayed
[num_displayed
] = strdup(token
);
594 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
603 * Submit the IPP request...
606 request
->request
.op
.version
[1] = version
;
607 request
->request
.op
.operation_id
= op
;
608 request
->request
.op
.request_id
= 1;
612 printf("%s:\n", ippOpString(op
));
614 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
618 printf(" %-60.60s [", name
);
622 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
624 response
= cupsDoIORequest(http
, request
, resource
, -1,
627 if (response
== NULL
)
631 curtime
= time(NULL
);
634 printf(" ERROR %04x (%s) @ %s\n", cupsLastError(),
635 cupsLastErrorString(), ctime(&curtime
));
640 if ((attrptr
= ippFindAttribute(response
, "job-id",
641 IPP_TAG_INTEGER
)) != NULL
)
642 job_id
= attrptr
->values
[0].integer
;
644 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
645 IPP_TAG_INTEGER
)) != NULL
)
646 subscription_id
= attrptr
->values
[0].integer
;
648 for (i
= 0; i
< num_statuses
; i
++)
649 if (response
->request
.status
.status_code
== statuses
[i
])
652 if (i
== num_statuses
&& num_statuses
> 0)
656 for (i
= 0; i
< num_expects
; i
++)
657 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
667 printf(" RECEIVED: %lu bytes in response\n",
668 (unsigned long)ippLength(response
));
672 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
675 else if (num_displayed
> 0)
677 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
680 for (i
= 0; i
< num_displayed
; i
++)
681 if (!strcmp(displayed
[i
], attrptr
->name
))
692 printf(" RECEIVED: %lu bytes in response\n",
693 (unsigned long)ippLength(response
));
695 for (i
= 0; i
< num_statuses
; i
++)
696 if (response
->request
.status
.status_code
== statuses
[i
])
699 if (i
== num_statuses
&& num_statuses
> 0)
702 printf(" status-code = %04x (%s)\n",
703 cupsLastError(), ippErrorString(cupsLastError()));
705 for (i
= 0; i
< num_expects
; i
++)
706 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
707 printf(" EXPECTED: %s\n", expects
[i
]);
709 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
716 for (i
= 0; i
< num_expects
; i
++)
731 * 'get_tag()' - Get an IPP value or group tag from a name...
734 ipp_tag_t
/* O - Value/group tag */
735 get_tag(const char *name
) /* I - Name of value/group tag */
737 int i
; /* Looping var */
740 for (i
= 0; i
< (sizeof(TagNames
) / sizeof(TagNames
[0])); i
++)
741 if (!strcasecmp(name
, TagNames
[i
]))
742 return ((ipp_tag_t
)i
);
744 return (IPP_TAG_ZERO
);
749 * 'get_tag_string()' - Get the string associated with a tag.
752 const char * /* O - Tag name string */
753 get_tag_string(ipp_tag_t tag
) /* I - IPP tag */
755 if (tag
< (ipp_tag_t
)(sizeof(TagNames
) / sizeof(TagNames
[0])))
756 return (TagNames
[tag
]);
763 * 'get_token()' - Get a token from a file.
766 char * /* O - Token from file or NULL on EOF */
767 get_token(FILE *fp
, /* I - File to read from */
768 char *buf
, /* I - Buffer to read into */
769 int buflen
, /* I - Length of buffer */
770 int *linenum
) /* IO - Current line number */
772 int ch
, /* Character from file */
773 quote
; /* Quoting character */
774 char *bufptr
, /* Pointer into buffer */
775 *bufend
; /* End of buffer */
784 while (isspace(ch
= getc(fp
)))
796 else if (ch
== '\'' || ch
== '\"')
804 bufend
= buf
+ buflen
- 1;
806 while ((ch
= getc(fp
)) != EOF
)
809 else if (bufptr
< bufend
)
821 while ((ch
= getc(fp
)) != EOF
)
830 * Whitespace delimited text...
836 bufend
= buf
+ buflen
- 1;
838 while ((ch
= getc(fp
)) != EOF
)
839 if (isspace(ch
) || ch
== '#')
841 else if (bufptr
< bufend
)
855 * 'print_attr()' - Print an attribute on the screen.
859 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
861 int i
; /* Looping var */
864 if (attr
->name
== NULL
)
866 puts(" -- separator --");
870 printf(" %s (%s%s) = ", attr
->name
,
871 attr
->num_values
> 1 ? "1setOf " : "",
872 get_tag_string(attr
->value_tag
));
874 switch (attr
->value_tag
)
876 case IPP_TAG_INTEGER
:
878 for (i
= 0; i
< attr
->num_values
; i
++)
879 printf("%d ", attr
->values
[i
].integer
);
882 case IPP_TAG_BOOLEAN
:
883 for (i
= 0; i
< attr
->num_values
; i
++)
884 if (attr
->values
[i
].boolean
)
890 case IPP_TAG_NOVALUE
:
895 for (i
= 0; i
< attr
->num_values
; i
++)
896 printf("%d-%d ", attr
->values
[i
].range
.lower
,
897 attr
->values
[i
].range
.upper
);
900 case IPP_TAG_RESOLUTION
:
901 for (i
= 0; i
< attr
->num_values
; i
++)
902 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
903 attr
->values
[i
].resolution
.yres
,
904 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
908 case IPP_TAG_STRING
:
911 case IPP_TAG_KEYWORD
:
912 case IPP_TAG_CHARSET
:
914 case IPP_TAG_MIMETYPE
:
915 case IPP_TAG_LANGUAGE
:
916 for (i
= 0; i
< attr
->num_values
; i
++)
917 printf("\"%s\" ", attr
->values
[i
].string
.text
);
920 case IPP_TAG_TEXTLANG
:
921 case IPP_TAG_NAMELANG
:
922 for (i
= 0; i
< attr
->num_values
; i
++)
923 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
924 attr
->values
[i
].string
.charset
);
928 break; /* anti-compiler-warning-code */
936 * 'usage()' - Show program usage.
940 usage(const char *option
) /* I - Option string or NULL */
943 fprintf(stderr
, "ipptest: Unknown option \"%s\"!\n", option
);
945 fputs("Usage: ipptest [options] URL testfile [ ... testfileN ]\n", stderr
);
946 fputs("Options:\n", stderr
);
948 fputs("-i N Repeat the last test file once every N seconds.\n", stderr
);
949 fputs("-v Show all attributes in response, even on success.\n", stderr
);
956 * End of "$Id: ipptest.c 6503 2007-05-01 23:06:44Z mike $".