]>
git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptest.c
2 * "$Id: ipptest.c 7847 2008-08-19 04:22:14Z mike $"
4 * IPP test command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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 * ippTagValue() - 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? */
50 int do_tests(const char *, const char *);
51 ipp_op_t
ippOpValue(const char *);
52 ipp_status_t
ippErrorValue(const char *);
53 char *get_token(FILE *, char *, int, int *linenum
);
54 void print_attr(ipp_attribute_t
*);
55 void usage(const char *option
);
59 * 'main()' - Parse options and do tests.
62 int /* O - Exit status */
63 main(int argc
, /* I - Number of command-line arguments */
64 char *argv
[]) /* I - Command-line arguments */
66 int i
; /* Looping var */
67 int status
; /* Status of tests... */
68 const char *uri
; /* URI to use */
69 const char *testfile
; /* Test file to use */
70 int interval
; /* Test interval */
76 * testipp URL testfile
84 for (i
= 1; i
< argc
; i
++)
86 if (argv
[i
][0] == '-')
88 if (!strcmp(argv
[i
], "-v"))
90 else if (!strcmp(argv
[i
], "-d"))
99 else if (!strcmp(argv
[i
], "-i"))
106 interval
= atoi(argv
[i
]);
111 else if (!strncmp(argv
[i
], "ipp://", 6) ||
112 !strncmp(argv
[i
], "http://", 7) ||
113 !strncmp(argv
[i
], "https://", 8))
119 if (!testfile
&& uri
)
133 if (!do_tests(uri
, testfile
))
138 if (!uri
|| !testfile
)
142 * Loop if the interval is set...
150 do_tests(uri
, testfile
);
163 * 'do_tests()' - Do tests as specified in the test file.
166 int /* 1 = success, 0 = failure */
167 do_tests(const char *uri
, /* I - URI to connect on */
168 const char *testfile
) /* I - Test file to use */
170 int i
; /* Looping var */
171 int linenum
; /* Current line number */
172 int version
; /* IPP version number to use */
173 http_t
*http
; /* HTTP connection to server */
174 char method
[HTTP_MAX_URI
], /* URI method */
175 userpass
[HTTP_MAX_URI
], /* username:password */
176 server
[HTTP_MAX_URI
], /* Server */
177 resource
[HTTP_MAX_URI
]; /* Resource path */
178 int port
; /* Port number */
179 FILE *fp
; /* Test file */
180 char token
[1024], /* Token from file */
181 *tokenptr
, /* Pointer into token */
182 temp
[1024], /* Temporary string */
183 *tempptr
; /* Pointer into temp string */
184 ipp_t
*request
; /* IPP request */
185 ipp_t
*response
; /* IPP response */
186 ipp_op_t op
; /* Operation */
187 ipp_tag_t group
; /* Current group */
188 ipp_tag_t value
; /* Current value type */
189 ipp_attribute_t
*attrptr
; /* Attribute pointer */
190 char attr
[128]; /* Attribute name */
191 int num_statuses
; /* Number of valid status codes */
192 ipp_status_t statuses
[100]; /* Valid status codes */
193 int num_expects
; /* Number of expected attributes */
194 char *expects
[100]; /* Expected attributes */
195 int num_displayed
; /* Number of displayed attributes */
196 char *displayed
[100]; /* Displayed attributes */
197 char name
[1024]; /* Name of test */
198 char filename
[1024]; /* Filename */
199 int pass
; /* Did we pass the test? */
200 int job_id
; /* Job ID from last operation */
201 int subscription_id
; /* Subscription ID from last operation */
205 * Open the test file...
208 if ((fp
= fopen(testfile
, "r")) == NULL
)
210 printf("Unable to open test file %s - %s\n", testfile
, strerror(errno
));
215 * Connect to the server...
218 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
219 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
221 if ((http
= httpConnect(server
, port
)) == NULL
)
223 printf("Unable to connect to %s on port %d - %s\n", server
, port
,
233 printf("\"%s\":\n", testfile
);
240 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
243 * Expect an open brace...
246 if (strcmp(token
, "{"))
248 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
255 * Initialize things...
258 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, method
, sizeof(method
), userpass
,
259 sizeof(userpass
), server
, sizeof(server
), &port
, resource
,
264 group
= IPP_TAG_ZERO
;
270 strcpy(name
, testfile
);
271 if (strrchr(name
, '.') != NULL
)
272 *strrchr(name
, '.') = '\0';
275 * Parse until we see a close brace...
278 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
280 if (!strcmp(token
, "}"))
282 else if (!strcasecmp(token
, "NAME"))
288 get_token(fp
, name
, sizeof(name
), &linenum
);
290 else if (!strcasecmp(token
, "VERSION"))
293 * IPP version number for test...
296 get_token(fp
, temp
, sizeof(temp
), &linenum
);
297 sscanf(temp
, "%*d.%d", &version
);
299 else if (!strcasecmp(token
, "RESOURCE"))
305 get_token(fp
, resource
, sizeof(resource
), &linenum
);
307 else if (!strcasecmp(token
, "OPERATION"))
313 get_token(fp
, token
, sizeof(token
), &linenum
);
314 op
= ippOpValue(token
);
316 else if (!strcasecmp(token
, "GROUP"))
322 get_token(fp
, token
, sizeof(token
), &linenum
);
323 value
= ippTagValue(token
);
326 ippAddSeparator(request
);
330 else if (!strcasecmp(token
, "DELAY"))
333 * Delay before operation...
338 get_token(fp
, token
, sizeof(token
), &linenum
);
339 if ((delay
= atoi(token
)) > 0)
342 else if (!strcasecmp(token
, "ATTR"))
348 get_token(fp
, token
, sizeof(token
), &linenum
);
349 value
= ippTagValue(token
);
350 get_token(fp
, attr
, sizeof(attr
), &linenum
);
351 get_token(fp
, temp
, sizeof(temp
), &linenum
);
353 token
[sizeof(token
) - 1] = '\0';
355 for (tempptr
= temp
, tokenptr
= token
;
356 *tempptr
&& tokenptr
< (token
+ sizeof(token
) - 1);)
360 * Substitute a string/number...
363 if (!strncasecmp(tempptr
+ 1, "uri", 3))
365 strlcpy(tokenptr
, uri
, sizeof(token
) - (tokenptr
- token
));
368 else if (!strncasecmp(tempptr
+ 1, "method", 6))
370 strlcpy(tokenptr
, method
, sizeof(token
) - (tokenptr
- token
));
373 else if (!strncasecmp(tempptr
+ 1, "username", 8))
375 strlcpy(tokenptr
, userpass
, sizeof(token
) - (tokenptr
- token
));
378 else if (!strncasecmp(tempptr
+ 1, "hostname", 8))
380 strlcpy(tokenptr
, server
, sizeof(token
) - (tokenptr
- token
));
383 else if (!strncasecmp(tempptr
+ 1, "port", 4))
385 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
389 else if (!strncasecmp(tempptr
+ 1, "resource", 8))
391 strlcpy(tokenptr
, resource
, sizeof(token
) - (tokenptr
- token
));
394 else if (!strncasecmp(tempptr
+ 1, "job-id", 6))
396 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
400 else if (!strncasecmp(tempptr
+ 1, "notify-subscription-id", 22))
402 snprintf(tokenptr
, sizeof(token
) - (tokenptr
- token
),
403 "%d", subscription_id
);
406 else if (!strncasecmp(tempptr
+ 1, "user", 4))
408 strlcpy(tokenptr
, cupsUser(), sizeof(token
) - (tokenptr
- token
));
411 else if (!strncasecmp(tempptr
+ 1, "ENV[", 4))
413 char *end
; /* End of $ENV[name] */
416 if ((end
= strchr(tempptr
+ 5, ']')) != NULL
)
420 getenv(tempptr
+ 5) ? getenv(tempptr
+ 5) : tempptr
+ 5,
421 sizeof(token
) - (tokenptr
- token
));
426 *tokenptr
++ = *tempptr
++;
432 *tokenptr
++ = *tempptr
++;
436 tokenptr
+= strlen(tokenptr
);
440 *tokenptr
++ = *tempptr
++;
446 case IPP_TAG_BOOLEAN
:
447 if (!strcasecmp(token
, "true"))
448 ippAddBoolean(request
, group
, attr
, 1);
450 ippAddBoolean(request
, group
, attr
, atoi(token
));
453 case IPP_TAG_INTEGER
:
455 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
458 case IPP_TAG_RESOLUTION
:
459 puts(" ERROR: resolution tag not yet supported!");
463 puts(" ERROR: range tag not yet supported!");
467 if (!strchr(token
, ','))
468 ippAddString(request
, group
, value
, attr
, NULL
, token
);
472 * Multiple string values...
475 int num_values
; /* Number of values */
476 char *values
[100], /* Values */
477 *ptr
; /* Pointer to next value */
483 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
486 values
[num_values
] = ptr
;
490 ippAddStrings(request
, group
, value
, attr
, num_values
,
491 NULL
, (const char **)values
);
496 else if (!strcasecmp(token
, "FILE"))
502 get_token(fp
, filename
, sizeof(filename
), &linenum
);
504 else if (!strcasecmp(token
, "STATUS") &&
505 num_statuses
< (int)(sizeof(statuses
) / sizeof(statuses
[0])))
511 get_token(fp
, token
, sizeof(token
), &linenum
);
512 statuses
[num_statuses
] = ippErrorValue(token
);
515 else if (!strcasecmp(token
, "EXPECT") &&
516 num_expects
< (int)(sizeof(expects
) / sizeof(expects
[0])))
519 * Expected attributes...
522 get_token(fp
, token
, sizeof(token
), &linenum
);
523 expects
[num_expects
] = strdup(token
);
526 else if (!strcasecmp(token
, "DISPLAY") &&
527 num_displayed
< (int)(sizeof(displayed
) / sizeof(displayed
[0])))
530 * Display attributes...
533 get_token(fp
, token
, sizeof(token
), &linenum
);
534 displayed
[num_displayed
] = strdup(token
);
539 printf("Unexpected token %s seen on line %d - aborting test!\n", token
,
548 * Submit the IPP request...
551 request
->request
.op
.version
[1] = version
;
552 request
->request
.op
.operation_id
= op
;
553 request
->request
.op
.request_id
= 1;
557 printf("%s:\n", ippOpString(op
));
559 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
563 printf(" %-60.60s [", name
);
567 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
569 response
= cupsDoIORequest(http
, request
, resource
, -1,
572 if (response
== NULL
)
576 curtime
= time(NULL
);
579 printf(" ERROR %04x (%s) @ %s\n", cupsLastError(),
580 cupsLastErrorString(), ctime(&curtime
));
585 if ((attrptr
= ippFindAttribute(response
, "job-id",
586 IPP_TAG_INTEGER
)) != NULL
)
587 job_id
= attrptr
->values
[0].integer
;
589 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
590 IPP_TAG_INTEGER
)) != NULL
)
591 subscription_id
= attrptr
->values
[0].integer
;
593 for (i
= 0; i
< num_statuses
; i
++)
594 if (response
->request
.status
.status_code
== statuses
[i
])
597 if (i
== num_statuses
&& num_statuses
> 0)
601 for (i
= 0; i
< num_expects
; i
++)
602 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
612 printf(" RECEIVED: %lu bytes in response\n",
613 (unsigned long)ippLength(response
));
617 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
620 else if (num_displayed
> 0)
622 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
625 for (i
= 0; i
< num_displayed
; i
++)
626 if (!strcmp(displayed
[i
], attrptr
->name
))
637 printf(" RECEIVED: %lu bytes in response\n",
638 (unsigned long)ippLength(response
));
640 for (i
= 0; i
< num_statuses
; i
++)
641 if (response
->request
.status
.status_code
== statuses
[i
])
644 if (i
== num_statuses
&& num_statuses
> 0)
647 printf(" status-code = %04x (%s)\n",
648 cupsLastError(), ippErrorString(cupsLastError()));
650 for (i
= 0; i
< num_expects
; i
++)
651 if (ippFindAttribute(response
, expects
[i
], IPP_TAG_ZERO
) == NULL
)
652 printf(" EXPECTED: %s\n", expects
[i
]);
654 for (attrptr
= response
->attrs
; attrptr
!= NULL
; attrptr
= attrptr
->next
)
661 for (i
= 0; i
< num_expects
; i
++)
676 * 'get_token()' - Get a token from a file.
679 char * /* O - Token from file or NULL on EOF */
680 get_token(FILE *fp
, /* I - File to read from */
681 char *buf
, /* I - Buffer to read into */
682 int buflen
, /* I - Length of buffer */
683 int *linenum
) /* IO - Current line number */
685 int ch
, /* Character from file */
686 quote
; /* Quoting character */
687 char *bufptr
, /* Pointer into buffer */
688 *bufend
; /* End of buffer */
697 while (isspace(ch
= getc(fp
)))
709 else if (ch
== '\'' || ch
== '\"')
717 bufend
= buf
+ buflen
- 1;
719 while ((ch
= getc(fp
)) != EOF
)
722 else if (bufptr
< bufend
)
734 while ((ch
= getc(fp
)) != EOF
)
743 * Whitespace delimited text...
749 bufend
= buf
+ buflen
- 1;
751 while ((ch
= getc(fp
)) != EOF
)
752 if (isspace(ch
) || ch
== '#')
754 else if (bufptr
< bufend
)
768 * 'print_attr()' - Print an attribute on the screen.
772 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
774 int i
; /* Looping var */
777 if (attr
->name
== NULL
)
779 puts(" -- separator --");
783 printf(" %s (%s%s) = ", attr
->name
,
784 attr
->num_values
> 1 ? "1setOf " : "",
785 ippTagString(attr
->value_tag
));
787 switch (attr
->value_tag
)
789 case IPP_TAG_INTEGER
:
791 for (i
= 0; i
< attr
->num_values
; i
++)
792 printf("%d ", attr
->values
[i
].integer
);
795 case IPP_TAG_BOOLEAN
:
796 for (i
= 0; i
< attr
->num_values
; i
++)
797 if (attr
->values
[i
].boolean
)
803 case IPP_TAG_NOVALUE
:
808 for (i
= 0; i
< attr
->num_values
; i
++)
809 printf("%d-%d ", attr
->values
[i
].range
.lower
,
810 attr
->values
[i
].range
.upper
);
813 case IPP_TAG_RESOLUTION
:
814 for (i
= 0; i
< attr
->num_values
; i
++)
815 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
816 attr
->values
[i
].resolution
.yres
,
817 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
821 case IPP_TAG_STRING
:
824 case IPP_TAG_KEYWORD
:
825 case IPP_TAG_CHARSET
:
827 case IPP_TAG_MIMETYPE
:
828 case IPP_TAG_LANGUAGE
:
829 for (i
= 0; i
< attr
->num_values
; i
++)
830 printf("\"%s\" ", attr
->values
[i
].string
.text
);
833 case IPP_TAG_TEXTLANG
:
834 case IPP_TAG_NAMELANG
:
835 for (i
= 0; i
< attr
->num_values
; i
++)
836 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
837 attr
->values
[i
].string
.charset
);
841 break; /* anti-compiler-warning-code */
849 * 'usage()' - Show program usage.
853 usage(const char *option
) /* I - Option string or NULL */
856 fprintf(stderr
, "ipptest: Unknown option \"%s\"!\n", option
);
858 fputs("Usage: ipptest [options] URL testfile [ ... testfileN ]\n", stderr
);
859 fputs("Options:\n", stderr
);
861 fputs("-i N Repeat the last test file once every N seconds.\n", stderr
);
862 fputs("-v Show all attributes in response, even on success.\n", stderr
);
869 * End of "$Id: ipptest.c 7847 2008-08-19 04:22:14Z mike $".