4 * ipptool command for CUPS.
6 * Copyright 2007-2010 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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Parse options and do tests.
20 * compare_vars() - Compare two variables.
21 * do_tests() - Do tests as specified in the test file.
22 * expand_variables() - Expand variables in a string.
23 * expect_matches() - Return true if the tag matches the specification.
24 * get_collection() - Get a collection value from the current test file.
25 * get_filename() - Get a filename based on the current test file.
26 * get_token() - Get a token from a file.
27 * get_variable() - Get the value of a variable.
28 * iso_date() - Return an ISO 8601 date/time string for the given IPP
30 * password_cb() - Password callback for authenticated tests.
31 * print_attr() - Print an attribute on the screen.
32 * print_col() - Print a collection attribute on the screen.
33 * print_csv() - Print a line of CSV text.
34 * print_fatal_error() - Print a fatal error message.
35 * print_line() - Print a line of formatted or CSV text.
36 * print_test_error() - Print a test error message.
37 * print_xml_header() - Print a standard XML plist header.
38 * print_xml_string() - Print an XML string with escaping.
39 * print_xml_trailer() - Print the XML trailer with success/fail value.
40 * set_variable() - Set a variable value.
41 * timeout_cb() - Handle HTTP timeouts.
42 * usage() - Show program usage.
43 * validate_attr() - Determine whether an attribute is valid.
44 * with_value() - Test a WITH-VALUE predicate.
48 * Include necessary headers...
51 #include <cups/cups-private.h>
52 #include <cups/file-private.h>
57 #endif /* !O_BINARY */
64 typedef enum _cups_transfer_e
/**** How to send request data ****/
66 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
67 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
68 _CUPS_TRANSFER_LENGTH
/* Length always */
71 typedef enum _cups_output_e
/**** Output mode ****/
73 _CUPS_OUTPUT_QUIET
, /* No output */
74 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
75 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
76 _CUPS_OUTPUT_LIST
, /* Tabular list output */
77 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
80 typedef struct _cups_expect_s
/**** Expected attribute info ****/
82 int optional
, /* Optional attribute? */
83 not_expect
; /* Don't expect attribute? */
84 char *name
, /* Attribute name */
85 *of_type
, /* Type name */
86 *same_count_as
, /* Parallel attribute name */
87 *if_defined
, /* Only required if variable defined */
88 *if_not_defined
, /* Only required if variable is not defined */
89 *with_value
, /* Attribute must include this value */
90 *define_match
, /* Variable to define on match */
91 *define_no_match
, /* Variable to define on no-match */
92 *define_value
; /* Variable to define with value */
93 int with_regex
, /* WITH-VALUE is a regular expression */
94 count
; /* Expected count if > 0 */
95 ipp_tag_t in_group
; /* IN-GROUP value */
98 typedef struct _cups_status_s
/**** Status info ****/
100 ipp_status_t status
; /* Expected status code */
101 char *if_defined
, /* Only if variable is defined */
102 *if_not_defined
; /* Only if variable is not defined */
105 typedef struct _cups_var_s
/**** Variable ****/
107 char *name
, /* Name of variable */
108 *value
; /* Value of variable */
111 typedef struct _cups_vars_s
/**** Set of variables ****/
113 const char *uri
, /* URI for printer */
114 *filename
; /* Filename */
115 char scheme
[64], /* Scheme from URI */
116 userpass
[256], /* Username/password from URI */
117 hostname
[256], /* Hostname from URI */
118 resource
[1024]; /* Resource path from URI */
119 int port
; /* Port number from URI */
120 http_encryption_t encryption
; /* Encryption for connection? */
121 double timeout
; /* Timeout for connection */
122 int family
; /* Address family */
123 cups_array_t
*vars
; /* Array of variables */
131 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
132 /* How to transfer requests */
133 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
135 int IgnoreErrors
= 0, /* Ignore errors? */
136 Verbosity
= 0, /* Show all attributes? */
137 Version
= 11, /* Default IPP version */
138 XMLHeader
= 0; /* 1 if header is written */
139 char *Password
= NULL
; /* Password from URI */
140 const char * const URIStatusStrings
[] = /* URI status strings */
143 "Bad arguments to function",
144 "Bad resource in URI",
145 "Bad port number in URI",
146 "Bad hostname/address in URI",
147 "Bad username in URI",
151 "Missing scheme in URI",
152 "Unknown scheme in URI",
153 "Missing resource in URI"
161 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
162 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
163 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
166 __attribute((nonnull(1,2,3)))
167 #endif /* __GNUC__ */
169 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
170 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
171 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
173 static char *get_token(FILE *fp
, char *buf
, int buflen
,
175 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
176 static char *iso_date(ipp_uchar_t
*date
);
177 static const char *password_cb(const char *prompt
);
178 static void print_attr(ipp_attribute_t
*attr
);
179 static void print_col(ipp_t
*col
);
180 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
181 char **displayed
, size_t *widths
);
182 static void print_fatal_error(const char *s
, ...)
184 __attribute__ ((__format__ (__printf__
, 1, 2)))
185 #endif /* __GNUC__ */
187 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
188 char **displayed
, size_t *widths
);
189 static void print_test_error(const char *s
, ...)
191 __attribute__ ((__format__ (__printf__
, 1, 2)))
192 #endif /* __GNUC__ */
194 static void print_xml_header(void);
195 static void print_xml_string(const char *element
, const char *s
);
196 static void print_xml_trailer(int success
, const char *message
);
197 static void set_variable(_cups_vars_t
*vars
, const char *name
,
199 static int timeout_cb(http_t
*http
, void *user_data
);
200 static void usage(void);
201 static int validate_attr(ipp_attribute_t
*attr
, int print
);
202 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
,
207 * 'main()' - Parse options and do tests.
210 int /* O - Exit status */
211 main(int argc
, /* I - Number of command-line args */
212 char *argv
[]) /* I - Command-line arguments */
214 int i
; /* Looping var */
215 int status
; /* Status of tests... */
216 char *opt
, /* Current option */
217 name
[1024], /* Name/value buffer */
218 *value
, /* Pointer to value */
219 filename
[1024], /* Real filename */
220 testname
[1024]; /* Real test filename */
221 const char *testfile
; /* Test file to use */
222 int interval
, /* Test interval in microseconds */
223 repeat
; /* Repeat count */
224 _cups_vars_t vars
; /* Variables */
225 http_uri_status_t uri_status
; /* URI separation status */
226 _cups_globals_t
*cg
= _cupsGlobals();
232 * Initialize the locale and variables...
235 _cupsSetLocale(argv
);
237 memset(&vars
, 0, sizeof(vars
));
238 vars
.family
= AF_UNSPEC
;
239 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
244 * ipptool URI testfile
252 for (i
= 1; i
< argc
; i
++)
254 if (argv
[i
][0] == '-')
256 for (opt
= argv
[i
] + 1; *opt
; opt
++)
260 case '4' : /* Connect using IPv4 only */
261 vars
.family
= AF_INET
;
265 case '6' : /* Connect using IPv6 only */
266 vars
.family
= AF_INET6
;
268 #endif /* AF_INET6 */
270 case 'C' : /* Enable HTTP chunking */
271 Transfer
= _CUPS_TRANSFER_CHUNKED
;
274 case 'E' : /* Encrypt with TLS */
276 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
278 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
280 #endif /* HAVE_SSL */
283 case 'I' : /* Ignore errors */
287 case 'L' : /* Disable HTTP chunking */
288 Transfer
= _CUPS_TRANSFER_LENGTH
;
291 case 'S' : /* Encrypt with SSL */
293 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
295 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
297 #endif /* HAVE_SSL */
300 case 'T' : /* Set timeout */
305 _cupsLangPuts(stderr
,
306 _("ipptool: Missing timeout for \"-T\"."));
310 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
313 case 'V' : /* Set IPP version */
318 _cupsLangPuts(stderr
,
319 _("ipptool: Missing version for \"-V\"."));
323 if (!strcmp(argv
[i
], "1.0"))
325 else if (!strcmp(argv
[i
], "1.1"))
327 else if (!strcmp(argv
[i
], "2.0"))
329 else if (!strcmp(argv
[i
], "2.1"))
331 else if (!strcmp(argv
[i
], "2.2"))
335 _cupsLangPrintf(stderr
,
336 _("ipptool: Bad version %s for \"-V\"."),
342 case 'X' : /* Produce XML output */
343 Output
= _CUPS_OUTPUT_PLIST
;
345 if (interval
|| repeat
)
347 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
348 "incompatible with -X\"."));
353 case 'c' : /* CSV output */
354 Output
= _CUPS_OUTPUT_CSV
;
357 case 'd' : /* Define a variable */
362 _cupsLangPuts(stderr
,
363 _("ipptool: Missing name=value for \"-d\"."));
367 strlcpy(name
, argv
[i
], sizeof(name
));
368 if ((value
= strchr(name
, '=')) != NULL
)
371 value
= name
+ strlen(name
);
373 set_variable(&vars
, name
, value
);
376 case 'f' : /* Set the default test filename */
381 _cupsLangPuts(stderr
,
382 _("ipptool: Missing filename for \"-f\"."));
386 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
388 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
389 cg
->cups_datadir
, argv
[i
]);
390 if (access(argv
[i
], 0))
391 vars
.filename
= argv
[i
];
393 vars
.filename
= filename
;
396 vars
.filename
= argv
[i
];
399 case 'i' : /* Test every N seconds */
404 _cupsLangPuts(stderr
,
405 _("ipptool: Missing seconds for \"-i\"."));
410 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
414 _cupsLangPuts(stderr
,
415 _("ipptool: Invalid seconds for \"-i\"."));
420 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
422 _cupsLangPuts(stderr
, _("ipptool: \"-i\" is incompatible with "
428 case 'l' : /* List as a table */
429 Output
= _CUPS_OUTPUT_LIST
;
432 case 'n' : /* Repeat count */
437 _cupsLangPuts(stderr
,
438 _("ipptool: Missing count for \"-n\"."));
442 repeat
= atoi(argv
[i
]);
444 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
446 _cupsLangPuts(stderr
, _("ipptool: \"-n\" is incompatible with "
452 case 'q' : /* Be quiet */
453 Output
= _CUPS_OUTPUT_QUIET
;
456 case 't' : /* CUPS test output */
457 Output
= _CUPS_OUTPUT_TEST
;
460 case 'v' : /* Be verbose */
465 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
472 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
474 || !strncmp(argv
[i
], "ipps://", 7)
475 || !strncmp(argv
[i
], "https://", 8)
476 #endif /* HAVE_SSL */
485 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
490 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
491 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
492 #endif /* HAVE_SSL */
495 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
496 vars
.scheme
, sizeof(vars
.scheme
),
497 vars
.userpass
, sizeof(vars
.userpass
),
498 vars
.hostname
, sizeof(vars
.hostname
),
500 vars
.resource
, sizeof(vars
.resource
));
502 if (uri_status
!= HTTP_URI_OK
)
504 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."),
505 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
509 if (vars
.userpass
[0])
511 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
514 cupsSetUser(vars
.userpass
);
515 cupsSetPasswordCB(password_cb
);
516 set_variable(&vars
, "uriuser", vars
.userpass
);
527 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
531 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
533 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
535 if (access(testname
, 0))
543 if (!do_tests(&vars
, testfile
))
548 if (!vars
.uri
|| !testfile
)
552 * Loop if the interval is set...
555 if (Output
== _CUPS_OUTPUT_PLIST
)
556 print_xml_trailer(!status
, NULL
);
557 else if (interval
> 0 && repeat
> 0)
562 do_tests(&vars
, testfile
);
566 else if (interval
> 0)
571 do_tests(&vars
, testfile
);
584 * 'compare_vars()' - Compare two variables.
587 static int /* O - Result of comparison */
588 compare_vars(_cups_var_t
*a
, /* I - First variable */
589 _cups_var_t
*b
) /* I - Second variable */
591 return (strcasecmp(a
->name
, b
->name
));
596 * 'do_tests()' - Do tests as specified in the test file.
599 static int /* 1 = success, 0 = failure */
600 do_tests(_cups_vars_t
*vars
, /* I - Variables */
601 const char *testfile
) /* I - Test file to use */
603 int i
, /* Looping var */
604 linenum
, /* Current line number */
605 pass
, /* Did we pass the test? */
606 prev_pass
= 1, /* Did we pass the previous test? */
607 request_id
, /* Current request ID */
608 show_header
= 1, /* Show the test header? */
609 ignore_errors
, /* Ignore test failures? */
610 skip_previous
= 0; /* Skip on previous test failure? */
611 http_t
*http
= NULL
; /* HTTP connection to server */
612 FILE *fp
= NULL
; /* Test file */
613 char resource
[512], /* Resource for request */
614 token
[1024], /* Token from file */
615 *tokenptr
, /* Pointer into token */
616 temp
[1024]; /* Temporary string */
617 ipp_t
*request
= NULL
; /* IPP request */
618 ipp_t
*response
= NULL
; /* IPP response */
619 char attr
[128]; /* Attribute name */
620 ipp_op_t op
; /* Operation */
621 ipp_tag_t group
; /* Current group */
622 ipp_tag_t value
; /* Current value type */
623 ipp_attribute_t
*attrptr
, /* Attribute pointer */
624 *found
, /* Found attribute */
625 *lastcol
= NULL
; /* Last collection attribute */
626 char name
[1024]; /* Name of test */
627 char filename
[1024]; /* Filename */
628 _cups_transfer_t transfer
; /* To chunk or not to chunk */
629 int version
, /* IPP version number to use */
630 skip_test
; /* Skip this test? */
631 int num_statuses
= 0; /* Number of valid status codes */
632 _cups_status_t statuses
[100], /* Valid status codes */
633 *last_status
; /* Last STATUS (for predicates) */
634 int num_expects
= 0; /* Number of expected attributes */
635 _cups_expect_t expects
[200], /* Expected attributes */
636 *expect
, /* Current expected attribute */
637 *last_expect
; /* Last EXPECT (for predicates) */
638 int num_displayed
= 0; /* Number of displayed attributes */
639 char *displayed
[200]; /* Displayed attributes */
640 size_t widths
[200]; /* Width of columns */
644 * Open the test file...
647 if ((fp
= fopen(testfile
, "r")) == NULL
)
649 print_fatal_error("Unable to open test file %s - %s", testfile
,
656 * Connect to the server...
659 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, vars
->encryption
,
660 vars
->family
)) == NULL
)
662 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
663 vars
->port
, strerror(errno
));
668 if (httpReconnect(http
))
670 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
671 vars
->port
, strerror(errno
));
676 if (vars
->timeout
> 0.0)
677 _httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
683 CUPS_SRAND(time(NULL
));
687 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
689 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
692 * Expect an open brace...
695 if (!strcmp(token
, "DEFINE"))
701 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
702 get_token(fp
, temp
, sizeof(temp
), &linenum
))
704 expand_variables(vars
, token
, temp
, sizeof(token
));
705 set_variable(vars
, attr
, token
);
709 print_fatal_error("Missing DEFINE name and/or value on line %d.",
717 else if (!strcmp(token
, "IGNORE-ERRORS"))
724 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
725 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
727 IgnoreErrors
= !strcasecmp(temp
, "yes");
731 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
738 else if (!strcmp(token
, "INCLUDE"))
745 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
748 * Map the filename to and then run the tests...
751 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
762 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
770 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
773 * SKIP-IF-DEFINED variable
776 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
778 if (get_variable(vars
, temp
))
783 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.", linenum
);
788 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
791 * SKIP-IF-NOT-DEFINED variable
794 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
796 if (!get_variable(vars
, temp
))
801 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
807 else if (!strcmp(token
, "TRANSFER"))
815 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
817 if (!strcmp(temp
, "auto"))
818 Transfer
= _CUPS_TRANSFER_AUTO
;
819 else if (!strcmp(temp
, "chunked"))
820 Transfer
= _CUPS_TRANSFER_CHUNKED
;
821 else if (!strcmp(temp
, "length"))
822 Transfer
= _CUPS_TRANSFER_LENGTH
;
825 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
833 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
840 else if (!strcmp(token
, "VERSION"))
842 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
844 if (!strcmp(temp
, "1.0"))
846 else if (!strcmp(temp
, "1.1"))
848 else if (!strcmp(temp
, "2.0"))
850 else if (!strcmp(temp
, "2.1"))
852 else if (!strcmp(temp
, "2.2"))
856 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
863 print_fatal_error("Missing VERSION number on line %d.", linenum
);
870 else if (strcmp(token
, "{"))
872 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
878 * Initialize things...
883 if (Output
== _CUPS_OUTPUT_PLIST
)
885 else if (Output
== _CUPS_OUTPUT_TEST
)
886 printf("\"%s\":\n", testfile
);
891 strlcpy(resource
, vars
->resource
, sizeof(resource
));
896 group
= IPP_TAG_ZERO
;
897 ignore_errors
= IgnoreErrors
;
905 strlcpy(name
, testfile
, sizeof(name
));
906 if (strrchr(name
, '.') != NULL
)
907 *strrchr(name
, '.') = '\0';
910 * Parse until we see a close brace...
913 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
915 if (strcasecmp(token
, "COUNT") &&
916 strcasecmp(token
, "DEFINE-MATCH") &&
917 strcasecmp(token
, "DEFINE-NO-MATCH") &&
918 strcasecmp(token
, "DEFINE-VALUE") &&
919 strcasecmp(token
, "IF-DEFINED") &&
920 strcasecmp(token
, "IF-NOT-DEFINED") &&
921 strcasecmp(token
, "IN-GROUP") &&
922 strcasecmp(token
, "OF-TYPE") &&
923 strcasecmp(token
, "SAME-COUNT-AS") &&
924 strcasecmp(token
, "WITH-VALUE"))
927 if (strcasecmp(token
, "IF-DEFINED") &&
928 strcasecmp(token
, "IF-NOT-DEFINED"))
931 if (!strcmp(token
, "}"))
933 else if (!strcmp(token
, "{") && lastcol
)
936 * Another collection value
939 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
940 /* Collection value */
944 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
948 * Reallocate memory...
951 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
952 (lastcol
->num_values
+ 1) *
953 sizeof(ipp_value_t
))) == NULL
)
955 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
960 if (tempcol
!= lastcol
)
963 * Reset pointers in the list...
967 request
->prev
->next
= tempcol
;
969 request
->attrs
= tempcol
;
971 lastcol
= request
->current
= request
->last
= tempcol
;
974 lastcol
->values
[lastcol
->num_values
].collection
= col
;
975 lastcol
->num_values
++;
983 else if (!strcmp(token
, "DEFINE"))
989 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
990 get_token(fp
, temp
, sizeof(temp
), &linenum
))
992 expand_variables(vars
, token
, temp
, sizeof(token
));
993 set_variable(vars
, attr
, token
);
997 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1003 else if (!strcmp(token
, "IGNORE-ERRORS"))
1010 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1011 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
1013 ignore_errors
= !strcasecmp(temp
, "yes");
1017 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1024 else if (!strcasecmp(token
, "NAME"))
1030 get_token(fp
, name
, sizeof(name
), &linenum
);
1032 else if (!strcmp(token
, "REQUEST-ID"))
1039 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1041 if (isdigit(temp
[0] & 255))
1042 request_id
= atoi(temp
);
1043 else if (!strcasecmp(temp
, "random"))
1044 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1047 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1055 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1060 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1063 * SKIP-IF-DEFINED variable
1066 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1068 if (get_variable(vars
, temp
))
1073 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1079 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1082 * SKIP-IF-NOT-DEFINED variable
1085 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1087 if (!get_variable(vars
, temp
))
1092 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1098 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1101 * SKIP-PREVIOUS-ERROR yes
1102 * SKIP-PREVIOUS-ERROR no
1105 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1106 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
1108 skip_previous
= !strcasecmp(temp
, "yes");
1112 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1119 else if (!strcmp(token
, "TRANSFER"))
1127 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1129 if (!strcmp(temp
, "auto"))
1130 transfer
= _CUPS_TRANSFER_AUTO
;
1131 else if (!strcmp(temp
, "chunked"))
1132 transfer
= _CUPS_TRANSFER_CHUNKED
;
1133 else if (!strcmp(temp
, "length"))
1134 transfer
= _CUPS_TRANSFER_LENGTH
;
1137 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1145 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1150 else if (!strcasecmp(token
, "VERSION"))
1152 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1154 if (!strcmp(temp
, "0.0"))
1156 else if (!strcmp(temp
, "1.0"))
1158 else if (!strcmp(temp
, "1.1"))
1160 else if (!strcmp(temp
, "2.0"))
1162 else if (!strcmp(temp
, "2.1"))
1164 else if (!strcmp(temp
, "2.2"))
1168 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1175 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1180 else if (!strcasecmp(token
, "RESOURCE"))
1186 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1188 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1193 else if (!strcasecmp(token
, "OPERATION"))
1199 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1201 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1206 if ((op
= ippOpValue(token
)) < 0 && (op
= strtol(token
, NULL
, 0)) == 0)
1208 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1214 else if (!strcasecmp(token
, "GROUP"))
1217 * Attribute group...
1220 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1222 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1227 if ((value
= ippTagValue(token
)) < 0)
1229 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1235 ippAddSeparator(request
);
1239 else if (!strcasecmp(token
, "DELAY"))
1242 * Delay before operation...
1247 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1249 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1254 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1256 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1263 if (Output
== _CUPS_OUTPUT_TEST
)
1264 printf(" [%g second delay]\n", delay
);
1266 usleep((int)(1000000.0 * delay
));
1269 else if (!strcasecmp(token
, "ATTR"))
1275 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1277 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1282 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1284 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1290 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1292 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1297 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1299 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1304 expand_variables(vars
, token
, temp
, sizeof(token
));
1308 case IPP_TAG_BOOLEAN
:
1309 if (!strcasecmp(token
, "true"))
1310 ippAddBoolean(request
, group
, attr
, 1);
1312 ippAddBoolean(request
, group
, attr
, atoi(token
));
1315 case IPP_TAG_INTEGER
:
1317 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
1320 case IPP_TAG_RESOLUTION
:
1322 int xres
, /* X resolution */
1323 yres
; /* Y resolution */
1324 char *ptr
; /* Pointer into value */
1326 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1327 if (ptr
> token
&& xres
> 0)
1330 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1333 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1334 (strcasecmp(ptr
, "dpi") && strcasecmp(ptr
, "dpc") &&
1335 strcasecmp(ptr
, "other")))
1337 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1343 if (!strcasecmp(ptr
, "dpi"))
1344 ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1346 else if (!strcasecmp(ptr
, "dpc"))
1347 ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1350 ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1355 case IPP_TAG_RANGE
:
1357 int lowers
[4], /* Lower value */
1358 uppers
[4], /* Upper values */
1359 num_vals
; /* Number of values */
1362 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1363 lowers
+ 0, uppers
+ 0,
1364 lowers
+ 1, uppers
+ 1,
1365 lowers
+ 2, uppers
+ 2,
1366 lowers
+ 3, uppers
+ 3);
1368 if ((num_vals
& 1) || num_vals
== 0)
1370 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1371 "%d.", token
, linenum
);
1376 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1381 case IPP_TAG_BEGIN_COLLECTION
:
1382 if (!strcmp(token
, "{"))
1384 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1385 /* Collection value */
1389 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1400 print_fatal_error("Bad ATTR collection value on line %d.",
1408 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1409 ippTagString(value
), linenum
);
1413 case IPP_TAG_TEXTLANG
:
1414 case IPP_TAG_NAMELANG
:
1417 case IPP_TAG_KEYWORD
:
1419 case IPP_TAG_URISCHEME
:
1420 case IPP_TAG_CHARSET
:
1421 case IPP_TAG_LANGUAGE
:
1422 case IPP_TAG_MIMETYPE
:
1423 if (!strchr(token
, ','))
1424 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1428 * Multiple string values...
1431 int num_values
; /* Number of values */
1432 char *values
[100], /* Values */
1433 *ptr
; /* Pointer to next value */
1439 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1442 values
[num_values
] = ptr
;
1446 ippAddStrings(request
, group
, value
, attr
, num_values
,
1447 NULL
, (const char **)values
);
1452 else if (!strcasecmp(token
, "FILE"))
1458 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1460 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1465 expand_variables(vars
, token
, temp
, sizeof(token
));
1466 get_filename(testfile
, filename
, token
, sizeof(filename
));
1468 else if (!strcasecmp(token
, "STATUS"))
1474 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1476 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1481 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1483 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1488 if ((statuses
[num_statuses
].status
= ippErrorValue(token
)) < 0 &&
1489 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1491 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1497 last_status
= statuses
+ num_statuses
;
1500 last_status
->if_defined
= NULL
;
1501 last_status
->if_not_defined
= NULL
;
1503 else if (!strcasecmp(token
, "EXPECT"))
1506 * Expected attributes...
1509 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1511 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1516 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1518 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1523 last_expect
= expects
+ num_expects
;
1526 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1528 if (token
[0] == '!')
1530 last_expect
->not_expect
= 1;
1531 last_expect
->name
= strdup(token
+ 1);
1533 else if (token
[0] == '?')
1535 last_expect
->optional
= 1;
1536 last_expect
->name
= strdup(token
+ 1);
1539 last_expect
->name
= strdup(token
);
1541 else if (!strcasecmp(token
, "COUNT"))
1543 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1545 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1550 if ((i
= atoi(token
)) <= 0)
1552 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1558 last_expect
->count
= i
;
1561 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1567 else if (!strcasecmp(token
, "DEFINE-MATCH"))
1569 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1571 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1578 last_expect
->define_match
= strdup(token
);
1581 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1587 else if (!strcasecmp(token
, "DEFINE-NO-MATCH"))
1589 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1591 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1598 last_expect
->define_no_match
= strdup(token
);
1601 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1602 "line %d.", linenum
);
1607 else if (!strcasecmp(token
, "DEFINE-VALUE"))
1609 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1611 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1618 last_expect
->define_value
= strdup(token
);
1621 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1627 else if (!strcasecmp(token
, "OF-TYPE"))
1629 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1631 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1638 last_expect
->of_type
= strdup(token
);
1641 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1647 else if (!strcasecmp(token
, "IN-GROUP"))
1649 ipp_tag_t in_group
; /* IN-GROUP value */
1652 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1654 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1659 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1662 else if (last_expect
)
1663 last_expect
->in_group
= in_group
;
1666 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1672 else if (!strcasecmp(token
, "SAME-COUNT-AS"))
1674 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1676 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1682 last_expect
->same_count_as
= strdup(token
);
1685 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1691 else if (!strcasecmp(token
, "IF-DEFINED"))
1693 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1695 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1701 last_expect
->if_defined
= strdup(token
);
1702 else if (last_status
)
1703 last_status
->if_defined
= strdup(token
);
1706 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1707 "on line %d.", linenum
);
1712 else if (!strcasecmp(token
, "IF-NOT-DEFINED"))
1714 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1716 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
1722 last_expect
->if_not_defined
= strdup(token
);
1723 else if (last_status
)
1724 last_status
->if_not_defined
= strdup(token
);
1727 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1728 "on line %d.", linenum
);
1733 else if (!strcasecmp(token
, "WITH-VALUE"))
1735 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1737 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
1745 * Expand any variables in the value and then save it.
1748 expand_variables(vars
, token
, temp
, sizeof(token
));
1750 tokenptr
= token
+ strlen(token
) - 1;
1752 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
1755 * WITH-VALUE is a POSIX extended regular expression.
1758 last_expect
->with_value
= calloc(1, tokenptr
- token
);
1759 last_expect
->with_regex
= 1;
1761 if (last_expect
->with_value
)
1762 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
1767 * WITH-VALUE is a literal value...
1770 last_expect
->with_value
= strdup(token
);
1775 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
1781 else if (!strcasecmp(token
, "DISPLAY"))
1784 * Display attributes...
1787 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
1789 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
1794 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1796 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
1801 displayed
[num_displayed
] = strdup(token
);
1806 print_fatal_error("Unexpected token %s seen on line %d.", token
,
1814 * Submit the IPP request...
1817 request
->request
.op
.version
[0] = version
/ 10;
1818 request
->request
.op
.version
[1] = version
% 10;
1819 request
->request
.op
.operation_id
= op
;
1820 request
->request
.op
.request_id
= request_id
;
1822 if (Output
== _CUPS_OUTPUT_PLIST
)
1825 puts("<key>Name</key>");
1826 print_xml_string("string", name
);
1827 puts("<key>Operation</key>");
1828 print_xml_string("string", ippOpString(op
));
1829 puts("<key>RequestAttributes</key>");
1831 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1832 print_attr(attrptr
);
1835 else if (Output
== _CUPS_OUTPUT_TEST
)
1839 printf(" %s:\n", ippOpString(op
));
1841 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1842 print_attr(attrptr
);
1845 printf(" %-69.69s [", name
);
1849 if ((skip_previous
&& !prev_pass
) || skip_test
)
1854 if (Output
== _CUPS_OUTPUT_PLIST
)
1856 puts("<key>Successful</key>");
1858 puts("<key>StatusCode</key>");
1859 print_xml_string("string", "skip");
1860 puts("<key>ResponseAttributes</key>");
1864 else if (Output
== _CUPS_OUTPUT_TEST
)
1870 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
1871 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
1874 * Send request using chunking...
1877 http_status_t status
= cupsSendRequest(http
, request
, resource
, 0);
1879 if (status
== HTTP_CONTINUE
&& filename
[0])
1881 int fd
; /* File to send */
1882 char buffer
[8192]; /* Copy buffer */
1883 ssize_t bytes
; /* Bytes read/written */
1885 if ((fd
= open(filename
, O_RDONLY
| O_BINARY
)) >= 0)
1887 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1888 if ((status
= cupsWriteRequestData(http
, buffer
,
1889 bytes
)) != HTTP_CONTINUE
)
1894 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
, strerror(errno
));
1895 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
1897 status
= HTTP_ERROR
;
1903 if (status
== HTTP_CONTINUE
)
1904 response
= cupsGetResponse(http
, resource
);
1908 else if (filename
[0])
1909 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
1911 response
= cupsDoRequest(http
, request
, resource
);
1917 prev_pass
= pass
= 0;
1920 if (http
->version
!= HTTP_1_1
)
1921 prev_pass
= pass
= 0;
1923 if (response
->request
.status
.request_id
!= request_id
)
1924 prev_pass
= pass
= 0;
1927 (response
->request
.status
.version
[0] != (version
/ 10) ||
1928 response
->request
.status
.version
[1] != (version
% 10)))
1929 prev_pass
= pass
= 0;
1931 if ((attrptr
= ippFindAttribute(response
, "job-id",
1932 IPP_TAG_INTEGER
)) != NULL
)
1934 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1935 set_variable(vars
, "job-id", temp
);
1938 if ((attrptr
= ippFindAttribute(response
, "job-uri",
1939 IPP_TAG_URI
)) != NULL
)
1940 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
1942 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
1943 IPP_TAG_INTEGER
)) != NULL
)
1945 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1946 set_variable(vars
, "notify-subscription-id", temp
);
1949 attrptr
= response
->attrs
;
1950 if (!attrptr
|| !attrptr
->name
||
1951 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
1952 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1953 attrptr
->num_values
!= 1 ||
1954 strcmp(attrptr
->name
, "attributes-charset"))
1955 prev_pass
= pass
= 0;
1959 attrptr
= attrptr
->next
;
1960 if (!attrptr
|| !attrptr
->name
||
1961 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
1962 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1963 attrptr
->num_values
!= 1 ||
1964 strcmp(attrptr
->name
, "attributes-natural-language"))
1965 prev_pass
= pass
= 0;
1968 if ((attrptr
= ippFindAttribute(response
, "status-message",
1969 IPP_TAG_ZERO
)) != NULL
&&
1970 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1971 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1972 attrptr
->num_values
!= 1 ||
1973 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1974 strlen(attrptr
->values
[0].string
.text
) > 255)))
1975 prev_pass
= pass
= 0;
1977 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
1978 IPP_TAG_ZERO
)) != NULL
&&
1979 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1980 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1981 attrptr
->num_values
!= 1 ||
1982 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1983 strlen(attrptr
->values
[0].string
.text
) > 1023)))
1984 prev_pass
= pass
= 0;
1986 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
1988 attrptr
= attrptr
->next
)
1990 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
1992 prev_pass
= pass
= 0;
1996 if (!validate_attr(attrptr
, 0))
1998 prev_pass
= pass
= 0;
2003 for (i
= 0; i
< num_statuses
; i
++)
2005 if (statuses
[i
].if_defined
&&
2006 !get_variable(vars
, statuses
[i
].if_defined
))
2009 if (statuses
[i
].if_not_defined
&&
2010 get_variable(vars
, statuses
[i
].if_not_defined
))
2013 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2017 if (i
== num_statuses
&& num_statuses
> 0)
2018 prev_pass
= pass
= 0;
2021 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2023 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2026 if (expect
->if_not_defined
&&
2027 get_variable(vars
, expect
->if_not_defined
))
2030 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2032 if ((found
&& expect
->not_expect
) ||
2033 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2034 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2035 (found
&& expect
->in_group
&&
2036 found
->group_tag
!= expect
->in_group
))
2038 if (expect
->define_no_match
)
2039 set_variable(vars
, expect
->define_no_match
, "1");
2040 else if (!expect
->define_match
)
2041 prev_pass
= pass
= 0;
2047 !with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2049 if (expect
->define_no_match
)
2050 set_variable(vars
, expect
->define_no_match
, "1");
2051 else if (!expect
->define_match
)
2052 prev_pass
= pass
= 0;
2057 if (found
&& expect
->count
> 0 && found
->num_values
!= expect
->count
)
2059 if (expect
->define_no_match
)
2060 set_variable(vars
, expect
->define_no_match
, "1");
2061 else if (!expect
->define_match
)
2062 prev_pass
= pass
= 0;
2067 if (found
&& expect
->same_count_as
)
2069 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2072 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2074 if (expect
->define_no_match
)
2075 set_variable(vars
, expect
->define_no_match
, "1");
2076 else if (!expect
->define_match
)
2077 prev_pass
= pass
= 0;
2083 if (found
&& expect
->define_match
)
2084 set_variable(vars
, expect
->define_match
, "1");
2086 if (found
&& expect
->define_value
)
2088 _ippAttrString(found
, token
, sizeof(token
));
2089 set_variable(vars
, expect
->define_value
, token
);
2095 if (Output
== _CUPS_OUTPUT_PLIST
)
2097 puts("<key>Successful</key>");
2098 puts(prev_pass
? "<true />" : "<false />");
2099 puts("<key>StatusCode</key>");
2100 print_xml_string("string", ippErrorString(cupsLastError()));
2101 puts("<key>ResponseAttributes</key>");
2103 for (attrptr
= response
? response
->attrs
: NULL
;
2105 attrptr
= attrptr
->next
)
2106 print_attr(attrptr
);
2109 else if (Output
== _CUPS_OUTPUT_TEST
)
2111 puts(prev_pass
? "PASS]" : "FAIL]");
2113 if (Verbosity
&& response
)
2115 printf(" RECEIVED: %lu bytes in response\n",
2116 (unsigned long)ippLength(response
));
2117 printf(" status-code = %x (%s)\n", cupsLastError(),
2118 ippErrorString(cupsLastError()));
2120 for (attrptr
= response
->attrs
;
2122 attrptr
= attrptr
->next
)
2124 print_attr(attrptr
);
2128 else if (!prev_pass
)
2129 fprintf(stderr
, "%s\n", cupsLastErrorString());
2131 if (prev_pass
&& Output
!= _CUPS_OUTPUT_PLIST
&&
2132 Output
!= _CUPS_OUTPUT_QUIET
&& !Verbosity
&& num_displayed
> 0)
2134 if (Output
>= _CUPS_OUTPUT_LIST
)
2136 size_t width
; /* Length of value */
2139 for (i
= 0; i
< num_displayed
; i
++)
2141 widths
[i
] = strlen(displayed
[i
]);
2143 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2145 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2148 width
= _ippAttrString(attrptr
, NULL
, 0);
2149 if (width
> widths
[i
])
2154 if (Output
== _CUPS_OUTPUT_CSV
)
2155 print_csv(NULL
, num_displayed
, displayed
, widths
);
2157 print_line(NULL
, num_displayed
, displayed
, widths
);
2159 attrptr
= response
->attrs
;
2163 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2164 attrptr
= attrptr
->next
;
2168 if (Output
== _CUPS_OUTPUT_CSV
)
2169 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2171 print_line(attrptr
, num_displayed
, displayed
, widths
);
2173 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2174 attrptr
= attrptr
->next
;
2180 for (attrptr
= response
->attrs
;
2182 attrptr
= attrptr
->next
)
2186 for (i
= 0; i
< num_displayed
; i
++)
2188 if (!strcmp(displayed
[i
], attrptr
->name
))
2190 print_attr(attrptr
);
2198 else if (!prev_pass
)
2200 if (Output
== _CUPS_OUTPUT_PLIST
)
2202 puts("<key>Errors</key>");
2206 if (http
->version
!= HTTP_1_1
)
2207 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
2208 http
->version
% 100);
2211 print_test_error("IPP request failed with status %s (%s)",
2212 ippErrorString(cupsLastError()),
2213 cupsLastErrorString());
2217 (response
->request
.status
.version
[0] != (version
/ 10) ||
2218 response
->request
.status
.version
[1] != (version
% 10)))
2219 print_test_error("Bad version %d.%d in response - expected %d.%d "
2220 "(RFC 2911 section 3.1.8).",
2221 response
->request
.status
.version
[0],
2222 response
->request
.status
.version
[1],
2223 version
/ 10, version
% 10);
2225 if (response
->request
.status
.request_id
!= request_id
)
2226 print_test_error("Bad request ID %d in response - expected %d "
2227 "(RFC 2911 section 3.1.1)",
2228 response
->request
.status
.request_id
, request_id
);
2230 attrptr
= response
->attrs
;
2232 print_test_error("Missing first attribute \"attributes-charset "
2233 "(charset)\" in group operation-attributes-tag "
2234 "(RFC 2911 section 3.1.4).");
2237 if (!attrptr
->name
||
2238 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2239 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2240 attrptr
->num_values
!= 1 ||
2241 strcmp(attrptr
->name
, "attributes-charset"))
2242 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2243 "expected \"attributes-charset (charset)\" in "
2244 "group operation-attributes-tag (RFC 2911 section "
2246 attrptr
->name
? attrptr
->name
: "(null)",
2247 attrptr
->num_values
> 1 ? "1setOf " : "",
2248 ippTagString(attrptr
->value_tag
),
2249 ippTagString(attrptr
->group_tag
));
2251 attrptr
= attrptr
->next
;
2253 print_test_error("Missing second attribute \"attributes-natural-"
2254 "language (naturalLanguage)\" in group "
2255 "operation-attributes-tag (RFC 2911 section "
2257 else if (!attrptr
->name
||
2258 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2259 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2260 attrptr
->num_values
!= 1 ||
2261 strcmp(attrptr
->name
, "attributes-natural-language"))
2262 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2263 "expected \"attributes-natural-language "
2264 "(naturalLanguage)\" in group "
2265 "operation-attributes-tag (RFC 2911 section "
2267 attrptr
->name
? attrptr
->name
: "(null)",
2268 attrptr
->num_values
> 1 ? "1setOf " : "",
2269 ippTagString(attrptr
->value_tag
),
2270 ippTagString(attrptr
->group_tag
));
2273 if ((attrptr
= ippFindAttribute(response
, "status-message",
2274 IPP_TAG_ZERO
)) != NULL
)
2276 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2277 print_test_error("status-message (text(255)) has wrong value tag "
2278 "%s (RFC 2911 section 3.1.6.2).",
2279 ippTagString(attrptr
->value_tag
));
2280 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2281 print_test_error("status-message (text(255)) has wrong group tag "
2282 "%s (RFC 2911 section 3.1.6.2).",
2283 ippTagString(attrptr
->group_tag
));
2284 if (attrptr
->num_values
!= 1)
2285 print_test_error("status-message (text(255)) has %d values "
2286 "(RFC 2911 section 3.1.6.2).",
2287 attrptr
->num_values
);
2288 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2289 strlen(attrptr
->values
[0].string
.text
) > 255)
2290 print_test_error("status-message (text(255)) has bad length %d"
2291 " (RFC 2911 section 3.1.6.2).",
2292 (int)strlen(attrptr
->values
[0].string
.text
));
2295 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2296 IPP_TAG_ZERO
)) != NULL
)
2298 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2299 print_test_error("detailed-status-message (text(MAX)) has wrong "
2300 "value tag %s (RFC 2911 section 3.1.6.3).",
2301 ippTagString(attrptr
->value_tag
));
2302 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2303 print_test_error("detailed-status-message (text(MAX)) has wrong "
2304 "group tag %s (RFC 2911 section 3.1.6.3).",
2305 ippTagString(attrptr
->group_tag
));
2306 if (attrptr
->num_values
!= 1)
2307 print_test_error("detailed-status-message (text(MAX)) has %d values"
2308 " (RFC 2911 section 3.1.6.3).",
2309 attrptr
->num_values
);
2310 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2311 strlen(attrptr
->values
[0].string
.text
) > 1023)
2312 print_test_error("detailed-status-message (text(MAX)) has bad "
2313 "length %d (RFC 2911 section 3.1.6.3).",
2314 (int)strlen(attrptr
->values
[0].string
.text
));
2317 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2319 attrptr
= attrptr
->next
)
2321 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2322 print_test_error("Attribute groups out of order (%s < %s)",
2323 ippTagString(attrptr
->group_tag
),
2324 ippTagString(group
));
2326 validate_attr(attrptr
, 1);
2329 for (i
= 0; i
< num_statuses
; i
++)
2331 if (statuses
[i
].if_defined
&&
2332 !get_variable(vars
, statuses
[i
].if_defined
))
2335 if (statuses
[i
].if_not_defined
&&
2336 get_variable(vars
, statuses
[i
].if_not_defined
))
2339 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2343 if (i
== num_statuses
&& num_statuses
> 0)
2345 print_test_error("Bad status-code (%s)",
2346 ippErrorString(cupsLastError()));
2347 print_test_error("status-message=\"%s\"", cupsLastErrorString());
2350 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2352 if (expect
->define_match
|| expect
->define_no_match
)
2355 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2358 if (expect
->if_not_defined
&&
2359 get_variable(vars
, expect
->if_not_defined
))
2362 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2364 if (found
&& expect
->not_expect
)
2365 print_test_error("NOT EXPECTED: %s", expect
->name
);
2366 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2367 print_test_error("EXPECTED: %s", expect
->name
);
2370 if (!expect_matches(expect
, found
->value_tag
))
2371 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2372 expect
->name
, expect
->of_type
,
2373 ippTagString(found
->value_tag
));
2375 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2376 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2377 expect
->name
, ippTagString(expect
->in_group
),
2378 ippTagString(found
->group_tag
));
2380 if (!with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2382 if (expect
->with_regex
)
2383 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2384 expect
->name
, expect
->with_value
);
2386 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2387 expect
->name
, expect
->with_value
);
2389 with_value(expect
->with_value
, expect
->with_regex
, found
, 1);
2392 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
2394 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2395 expect
->count
, found
->num_values
);
2398 if (expect
->same_count_as
)
2400 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2404 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2405 "(not returned)", expect
->name
,
2406 found
->num_values
, expect
->same_count_as
);
2407 else if (attrptr
->num_values
!= found
->num_values
)
2408 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2409 "(%d values)", expect
->name
, found
->num_values
,
2410 expect
->same_count_as
, attrptr
->num_values
);
2416 if (Output
== _CUPS_OUTPUT_PLIST
)
2420 if (Output
== _CUPS_OUTPUT_PLIST
)
2425 ippDelete(response
);
2428 for (i
= 0; i
< num_statuses
; i
++)
2430 if (statuses
[i
].if_defined
)
2431 free(statuses
[i
].if_defined
);
2432 if (statuses
[i
].if_not_defined
)
2433 free(statuses
[i
].if_not_defined
);
2437 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2440 if (expect
->of_type
)
2441 free(expect
->of_type
);
2442 if (expect
->same_count_as
)
2443 free(expect
->same_count_as
);
2444 if (expect
->if_defined
)
2445 free(expect
->if_defined
);
2446 if (expect
->if_not_defined
)
2447 free(expect
->if_not_defined
);
2448 if (expect
->with_value
)
2449 free(expect
->with_value
);
2450 if (expect
->define_match
)
2451 free(expect
->define_match
);
2452 if (expect
->define_no_match
)
2453 free(expect
->define_no_match
);
2454 if (expect
->define_value
)
2455 free(expect
->define_value
);
2459 for (i
= 0; i
< num_displayed
; i
++)
2463 if (!ignore_errors
&& !prev_pass
)
2474 ippDelete(response
);
2476 for (i
= 0; i
< num_statuses
; i
++)
2478 if (statuses
[i
].if_defined
)
2479 free(statuses
[i
].if_defined
);
2480 if (statuses
[i
].if_not_defined
)
2481 free(statuses
[i
].if_not_defined
);
2484 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2487 if (expect
->of_type
)
2488 free(expect
->of_type
);
2489 if (expect
->same_count_as
)
2490 free(expect
->same_count_as
);
2491 if (expect
->if_defined
)
2492 free(expect
->if_defined
);
2493 if (expect
->if_not_defined
)
2494 free(expect
->if_not_defined
);
2495 if (expect
->with_value
)
2496 free(expect
->with_value
);
2497 if (expect
->define_match
)
2498 free(expect
->define_match
);
2499 if (expect
->define_no_match
)
2500 free(expect
->define_no_match
);
2501 if (expect
->define_value
)
2502 free(expect
->define_value
);
2505 for (i
= 0; i
< num_displayed
; i
++)
2513 * 'expand_variables()' - Expand variables in a string.
2517 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
2518 char *dst
, /* I - Destination string buffer */
2519 const char *src
, /* I - Source string */
2520 size_t dstsize
) /* I - Size of destination buffer */
2522 char *dstptr
, /* Pointer into destination */
2523 *dstend
, /* End of destination */
2524 temp
[256], /* Temporary string */
2525 *tempptr
; /* Pointer into temporary string */
2526 const char *value
; /* Value to substitute */
2530 dstend
= dst
+ dstsize
- 1;
2532 while (*src
&& dstptr
< dstend
)
2537 * Substitute a string/number...
2540 if (!strncmp(src
, "$$", 2))
2545 else if (!strncmp(src
, "$ENV[", 5))
2547 strlcpy(temp
, src
+ 5, sizeof(temp
));
2549 for (tempptr
= temp
; *tempptr
; tempptr
++)
2550 if (*tempptr
== ']')
2556 value
= getenv(temp
);
2557 src
+= tempptr
- temp
+ 5;
2561 strlcpy(temp
, src
+ 1, sizeof(temp
));
2563 for (tempptr
= temp
; *tempptr
; tempptr
++)
2564 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
2570 if (!strcmp(temp
, "uri"))
2572 else if (!strcmp(temp
, "filename"))
2573 value
= vars
->filename
;
2574 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
2575 value
= vars
->scheme
;
2576 else if (!strcmp(temp
, "username"))
2577 value
= vars
->userpass
;
2578 else if (!strcmp(temp
, "hostname"))
2579 value
= vars
->hostname
;
2580 else if (!strcmp(temp
, "port"))
2582 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
2585 else if (!strcmp(temp
, "resource"))
2586 value
= vars
->resource
;
2587 else if (!strcmp(temp
, "user"))
2590 value
= get_variable(vars
, temp
);
2592 src
+= tempptr
- temp
+ 1;
2602 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
2603 dstptr
+= strlen(dstptr
);
2615 * 'expect_matches()' - Return true if the tag matches the specification.
2618 static int /* O - 1 if matches, 0 otherwise */
2620 _cups_expect_t
*expect
, /* I - Expected attribute */
2621 ipp_tag_t value_tag
) /* I - Value tag for attribute */
2623 int match
; /* Match? */
2624 char *of_type
, /* Type name to match */
2625 *next
, /* Next name to match */
2626 sep
; /* Separator character */
2630 * If we don't expect a particular type, return immediately...
2633 if (!expect
->of_type
)
2637 * Parse the "of_type" value since the string can contain multiple attribute
2638 * types separated by "," or "|"...
2641 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
2644 * Find the next separator, and set it (temporarily) to nul if present.
2647 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
2649 if ((sep
= *next
) != '\0')
2653 * Support some meta-types to make it easier to write the test file.
2656 if (!strcmp(of_type
, "text"))
2657 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
2658 else if (!strcmp(of_type
, "name"))
2659 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
2660 else if (!strcmp(of_type
, "collection"))
2661 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
2663 match
= value_tag
== ippTagValue(of_type
);
2666 * Restore the separator if we have one...
2678 * 'get_collection()' - Get a collection value from the current test file.
2681 static ipp_t
* /* O - Collection value */
2682 get_collection(_cups_vars_t
*vars
, /* I - Variables */
2683 FILE *fp
, /* I - File to read from */
2684 int *linenum
) /* IO - Line number */
2686 char token
[1024], /* Token from file */
2687 temp
[1024], /* Temporary string */
2688 attr
[128]; /* Attribute name */
2689 ipp_tag_t value
; /* Current value type */
2690 ipp_t
*col
= ippNew(); /* Collection value */
2691 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
2694 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
2696 if (!strcmp(token
, "}"))
2698 else if (!strcmp(token
, "{") && lastcol
)
2701 * Another collection value
2704 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2705 /* Collection value */
2709 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
2713 * Reallocate memory...
2716 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
2717 (lastcol
->num_values
+ 1) *
2718 sizeof(ipp_value_t
))) == NULL
)
2720 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
2724 if (tempcol
!= lastcol
)
2727 * Reset pointers in the list...
2731 col
->prev
->next
= tempcol
;
2733 col
->attrs
= tempcol
;
2735 lastcol
= col
->current
= col
->last
= tempcol
;
2738 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
2739 lastcol
->num_values
++;
2744 else if (!strcasecmp(token
, "MEMBER"))
2752 if (!get_token(fp
, token
, sizeof(token
), linenum
))
2754 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
2758 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
2760 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
2765 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
2767 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
2771 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
2773 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
2777 expand_variables(vars
, token
, temp
, sizeof(token
));
2781 case IPP_TAG_BOOLEAN
:
2782 if (!strcasecmp(token
, "true"))
2783 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
2785 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
2788 case IPP_TAG_INTEGER
:
2790 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
2793 case IPP_TAG_RESOLUTION
:
2795 int xres
, /* X resolution */
2796 yres
; /* Y resolution */
2797 char units
[6]; /* Units */
2799 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
2800 (strcasecmp(units
, "dpi") && strcasecmp(units
, "dpc") &&
2801 strcasecmp(units
, "other")))
2803 print_fatal_error("Bad resolution value \"%s\" on line %d.",
2808 if (!strcasecmp(units
, "dpi"))
2809 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2811 else if (!strcasecmp(units
, "dpc"))
2812 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2815 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2820 case IPP_TAG_RANGE
:
2822 int lowers
[4], /* Lower value */
2823 uppers
[4], /* Upper values */
2824 num_vals
; /* Number of values */
2827 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
2828 lowers
+ 0, uppers
+ 0,
2829 lowers
+ 1, uppers
+ 1,
2830 lowers
+ 2, uppers
+ 2,
2831 lowers
+ 3, uppers
+ 3);
2833 if ((num_vals
& 1) || num_vals
== 0)
2835 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
2840 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
2845 case IPP_TAG_BEGIN_COLLECTION
:
2846 if (!strcmp(token
, "{"))
2848 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2849 /* Collection value */
2853 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
2861 print_fatal_error("Bad collection value on line %d.", *linenum
);
2867 if (!strchr(token
, ','))
2868 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
2872 * Multiple string values...
2875 int num_values
; /* Number of values */
2876 char *values
[100], /* Values */
2877 *ptr
; /* Pointer to next value */
2883 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
2886 values
[num_values
] = ptr
;
2890 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
2891 NULL
, (const char **)values
);
2901 * If we get here there was a parse error; free memory and return.
2913 * 'get_filename()' - Get a filename based on the current test file.
2916 static char * /* O - Filename */
2917 get_filename(const char *testfile
, /* I - Current test file */
2918 char *dst
, /* I - Destination filename */
2919 const char *src
, /* I - Source filename */
2920 size_t dstsize
) /* I - Size of destination buffer */
2922 char *dstptr
; /* Pointer into destination */
2923 _cups_globals_t
*cg
= _cupsGlobals();
2927 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
2930 * Map <filename> to CUPS_DATADIR/ipptool/filename...
2933 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
2934 dstptr
= dst
+ strlen(dst
) - 1;
2938 else if (*src
== '/' || !strchr(testfile
, '/'))
2941 * Use the path as-is...
2944 strlcpy(dst
, src
, dstsize
);
2949 * Make path relative to testfile...
2952 strlcpy(dst
, testfile
, dstsize
);
2953 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
2956 dstptr
= dst
; /* Should never happen */
2958 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
2966 * 'get_token()' - Get a token from a file.
2969 static char * /* O - Token from file or NULL on EOF */
2970 get_token(FILE *fp
, /* I - File to read from */
2971 char *buf
, /* I - Buffer to read into */
2972 int buflen
, /* I - Length of buffer */
2973 int *linenum
) /* IO - Current line number */
2975 int ch
, /* Character from file */
2976 quote
; /* Quoting character */
2977 char *bufptr
, /* Pointer into buffer */
2978 *bufend
; /* End of buffer */
2984 * Skip whitespace...
2987 while (isspace(ch
= getc(fp
)))
2999 else if (ch
== '\'' || ch
== '\"')
3002 * Quoted text or regular expression...
3007 bufend
= buf
+ buflen
- 1;
3009 while ((ch
= getc(fp
)) != EOF
)
3014 * Escape next character...
3017 if (bufptr
< bufend
)
3020 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3023 else if (ch
== quote
)
3025 else if (bufptr
< bufend
)
3039 while ((ch
= getc(fp
)) != EOF
)
3048 * Whitespace delimited text...
3054 bufend
= buf
+ buflen
- 1;
3056 while ((ch
= getc(fp
)) != EOF
)
3057 if (isspace(ch
) || ch
== '#')
3059 else if (bufptr
< bufend
)
3064 else if (ch
== '\n')
3076 * 'get_variable()' - Get the value of a variable.
3079 static char * /* O - Value or NULL */
3080 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3081 const char *name
) /* I - Variable name */
3083 _cups_var_t key
, /* Search key */
3084 *match
; /* Matching variable, if any */
3087 key
.name
= (char *)name
;
3088 match
= cupsArrayFind(vars
->vars
, &key
);
3090 return (match
? match
->value
: NULL
);
3095 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3099 static char * /* O - ISO 8601 date/time string */
3100 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3102 unsigned year
= (date
[0] << 8) + date
[1];
3104 static char buffer
[255]; /* String buffer */
3107 if (date
[9] == 0 && date
[10] == 0)
3108 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
3109 year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
3111 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
3112 year
, date
[2], date
[3], date
[4], date
[5], date
[6],
3113 date
[8], date
[9], date
[10]);
3120 * 'password_cb()' - Password callback for authenticated tests.
3123 static const char * /* O - Password */
3124 password_cb(const char *prompt
) /* I - Prompt (unused) */
3133 * 'print_attr()' - Print an attribute on the screen.
3137 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
3139 int i
; /* Looping var */
3140 ipp_attribute_t
*colattr
; /* Collection attribute */
3143 if (Output
== _CUPS_OUTPUT_PLIST
)
3147 printf("<key>%s</key>\n<true />\n", ippTagString(attr
->group_tag
));
3151 print_xml_string("key", attr
->name
);
3152 if (attr
->num_values
> 1)
3155 else if (Output
== _CUPS_OUTPUT_TEST
)
3159 puts(" -- separator --");
3163 printf(" %s (%s%s) = ", attr
->name
,
3164 attr
->num_values
> 1 ? "1setOf " : "",
3165 ippTagString(attr
->value_tag
));
3168 switch (attr
->value_tag
)
3170 case IPP_TAG_INTEGER
:
3172 for (i
= 0; i
< attr
->num_values
; i
++)
3173 if (Output
== _CUPS_OUTPUT_PLIST
)
3174 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3176 printf("%d ", attr
->values
[i
].integer
);
3179 case IPP_TAG_BOOLEAN
:
3180 for (i
= 0; i
< attr
->num_values
; i
++)
3181 if (Output
== _CUPS_OUTPUT_PLIST
)
3182 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3183 else if (attr
->values
[i
].boolean
)
3184 fputs("true ", stdout
);
3186 fputs("false ", stdout
);
3189 case IPP_TAG_RANGE
:
3190 for (i
= 0; i
< attr
->num_values
; i
++)
3191 if (Output
== _CUPS_OUTPUT_PLIST
)
3192 printf("<dict><key>lower</key><integer>%d</integer>"
3193 "<key>upper</key><integer>%d</integer></dict>\n",
3194 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3196 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3197 attr
->values
[i
].range
.upper
);
3200 case IPP_TAG_RESOLUTION
:
3201 for (i
= 0; i
< attr
->num_values
; i
++)
3202 if (Output
== _CUPS_OUTPUT_PLIST
)
3203 printf("<dict><key>xres</key><integer>%d</integer>"
3204 "<key>yres</key><integer>%d</integer>"
3205 "<key>units</key><string>%s</string></dict>\n",
3206 attr
->values
[i
].resolution
.xres
,
3207 attr
->values
[i
].resolution
.yres
,
3208 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3211 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3212 attr
->values
[i
].resolution
.yres
,
3213 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3218 for (i
= 0; i
< attr
->num_values
; i
++)
3219 if (Output
== _CUPS_OUTPUT_PLIST
)
3220 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3222 printf("%s ", iso_date(attr
->values
[i
].date
));
3225 case IPP_TAG_STRING
:
3228 case IPP_TAG_KEYWORD
:
3229 case IPP_TAG_CHARSET
:
3231 case IPP_TAG_MIMETYPE
:
3232 case IPP_TAG_LANGUAGE
:
3233 for (i
= 0; i
< attr
->num_values
; i
++)
3234 if (Output
== _CUPS_OUTPUT_PLIST
)
3235 print_xml_string("string", attr
->values
[i
].string
.text
);
3237 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3240 case IPP_TAG_TEXTLANG
:
3241 case IPP_TAG_NAMELANG
:
3242 for (i
= 0; i
< attr
->num_values
; i
++)
3243 if (Output
== _CUPS_OUTPUT_PLIST
)
3245 fputs("<dict><key>language</key><string>", stdout
);
3246 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
3247 fputs("</string><key>string</key><string>", stdout
);
3248 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3249 puts("</string></dict>");
3252 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3253 attr
->values
[i
].string
.charset
);
3256 case IPP_TAG_BEGIN_COLLECTION
:
3257 for (i
= 0; i
< attr
->num_values
; i
++)
3259 if (Output
== _CUPS_OUTPUT_PLIST
)
3262 for (colattr
= attr
->values
[i
].collection
->attrs
;
3264 colattr
= colattr
->next
)
3265 print_attr(colattr
);
3273 print_col(attr
->values
[i
].collection
);
3279 if (Output
== _CUPS_OUTPUT_PLIST
)
3280 printf("<string><<%s>></string>\n",
3281 ippTagString(attr
->value_tag
));
3283 fputs(ippTagString(attr
->value_tag
), stdout
);
3287 if (Output
== _CUPS_OUTPUT_PLIST
)
3289 if (attr
->num_values
> 1)
3298 * 'print_col()' - Print a collection attribute on the screen.
3302 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3304 int i
; /* Looping var */
3305 ipp_attribute_t
*attr
; /* Current attribute in collection */
3308 fputs("{ ", stdout
);
3309 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3311 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3312 ippTagString(attr
->value_tag
));
3314 switch (attr
->value_tag
)
3316 case IPP_TAG_INTEGER
:
3318 for (i
= 0; i
< attr
->num_values
; i
++)
3319 printf("%d ", attr
->values
[i
].integer
);
3322 case IPP_TAG_BOOLEAN
:
3323 for (i
= 0; i
< attr
->num_values
; i
++)
3324 if (attr
->values
[i
].boolean
)
3330 case IPP_TAG_NOVALUE
:
3334 case IPP_TAG_RANGE
:
3335 for (i
= 0; i
< attr
->num_values
; i
++)
3336 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3337 attr
->values
[i
].range
.upper
);
3340 case IPP_TAG_RESOLUTION
:
3341 for (i
= 0; i
< attr
->num_values
; i
++)
3342 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3343 attr
->values
[i
].resolution
.yres
,
3344 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3348 case IPP_TAG_STRING
:
3351 case IPP_TAG_KEYWORD
:
3352 case IPP_TAG_CHARSET
:
3354 case IPP_TAG_MIMETYPE
:
3355 case IPP_TAG_LANGUAGE
:
3356 for (i
= 0; i
< attr
->num_values
; i
++)
3357 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3360 case IPP_TAG_TEXTLANG
:
3361 case IPP_TAG_NAMELANG
:
3362 for (i
= 0; i
< attr
->num_values
; i
++)
3363 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3364 attr
->values
[i
].string
.charset
);
3367 case IPP_TAG_BEGIN_COLLECTION
:
3368 for (i
= 0; i
< attr
->num_values
; i
++)
3370 print_col(attr
->values
[i
].collection
);
3376 break; /* anti-compiler-warning-code */
3385 * 'print_csv()' - Print a line of CSV text.
3390 ipp_attribute_t
*attr
, /* I - First attribute for line */
3391 int num_displayed
, /* I - Number of attributes to display */
3392 char **displayed
, /* I - Attributes to display */
3393 size_t *widths
) /* I - Column widths */
3395 int i
; /* Looping var */
3396 size_t maxlength
; /* Max length of all columns */
3397 char *buffer
, /* String buffer */
3398 *bufptr
; /* Pointer into buffer */
3399 ipp_attribute_t
*current
; /* Current attribute */
3403 * Get the maximum string length we have to show and allocate...
3406 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3407 if (widths
[i
] > maxlength
)
3408 maxlength
= widths
[i
];
3412 if ((buffer
= malloc(maxlength
)) == NULL
)
3416 * Loop through the attributes to display...
3421 for (i
= 0; i
< num_displayed
; i
++)
3428 for (current
= attr
; current
; current
= current
->next
)
3432 else if (!strcmp(current
->name
, displayed
[i
]))
3434 _ippAttrString(current
, buffer
, maxlength
);
3439 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
3440 strchr(buffer
, '\\') != NULL
)
3443 for (bufptr
= buffer
; *bufptr
; bufptr
++)
3445 if (*bufptr
== '\\' || *bufptr
== '\"')
3452 fputs(buffer
, stdout
);
3458 for (i
= 0; i
< num_displayed
; i
++)
3463 fputs(displayed
[i
], stdout
);
3473 * 'print_fatal_error()' - Print a fatal error message.
3477 print_fatal_error(const char *s
, /* I - Printf-style format string */
3478 ...) /* I - Additional arguments as needed */
3480 char buffer
[10240]; /* Format buffer */
3481 va_list ap
; /* Pointer to arguments */
3485 * Format the error message...
3489 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3496 if (Output
== _CUPS_OUTPUT_PLIST
)
3499 print_xml_trailer(0, buffer
);
3502 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
3507 * 'print_line()' - Print a line of formatted or CSV text.
3512 ipp_attribute_t
*attr
, /* I - First attribute for line */
3513 int num_displayed
, /* I - Number of attributes to display */
3514 char **displayed
, /* I - Attributes to display */
3515 size_t *widths
) /* I - Column widths */
3517 int i
; /* Looping var */
3518 size_t maxlength
; /* Max length of all columns */
3519 char *buffer
; /* String buffer */
3520 ipp_attribute_t
*current
; /* Current attribute */
3524 * Get the maximum string length we have to show and allocate...
3527 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3528 if (widths
[i
] > maxlength
)
3529 maxlength
= widths
[i
];
3533 if ((buffer
= malloc(maxlength
)) == NULL
)
3537 * Loop through the attributes to display...
3542 for (i
= 0; i
< num_displayed
; i
++)
3549 for (current
= attr
; current
; current
= current
->next
)
3553 else if (!strcmp(current
->name
, displayed
[i
]))
3555 _ippAttrString(current
, buffer
, maxlength
);
3560 printf("%*s", (int)-widths
[i
], buffer
);
3566 for (i
= 0; i
< num_displayed
; i
++)
3571 printf("%*s", (int)-widths
[i
], displayed
[i
]);
3575 for (i
= 0; i
< num_displayed
; i
++)
3580 memset(buffer
, '-', widths
[i
]);
3581 buffer
[widths
[i
]] = '\0';
3582 fputs(buffer
, stdout
);
3592 * 'print_test_error()' - Print a test error message.
3596 print_test_error(const char *s
, /* I - Printf-style format string */
3597 ...) /* I - Additional arguments as needed */
3599 char buffer
[10240]; /* Format buffer */
3600 va_list ap
; /* Pointer to arguments */
3604 * Format the error message...
3608 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3615 if (Output
== _CUPS_OUTPUT_PLIST
)
3616 print_xml_string("string", buffer
);
3618 printf(" %s\n", buffer
);
3623 * 'print_xml_header()' - Print a standard XML plist header.
3627 print_xml_header(void)
3631 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
3632 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
3633 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
3634 puts("<plist version=\"1.0\">");
3636 puts("<key>Transfer</key>");
3637 printf("<string>%s</string>\n",
3638 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
3639 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
3640 puts("<key>Tests</key>");
3649 * 'print_xml_string()' - Print an XML string with escaping.
3653 print_xml_string(const char *element
, /* I - Element name or NULL */
3654 const char *s
) /* I - String to print */
3657 printf("<%s>", element
);
3662 fputs("&", stdout
);
3664 fputs("<", stdout
);
3666 fputs(">", stdout
);
3674 printf("</%s>\n", element
);
3679 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
3683 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
3684 const char *message
) /* I - Error message or NULL */
3689 puts("<key>Successful</key>");
3690 puts(success
? "<true />" : "<false />");
3693 puts("<key>ErrorMessage</key>");
3694 print_xml_string("string", message
);
3705 * 'set_variable()' - Set a variable value.
3709 set_variable(_cups_vars_t
*vars
, /* I - Variables */
3710 const char *name
, /* I - Variable name */
3711 const char *value
) /* I - Value string */
3713 _cups_var_t key
, /* Search key */
3714 *var
; /* New variable */
3717 key
.name
= (char *)name
;
3718 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
3721 var
->value
= strdup(value
);
3723 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
3725 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
3730 var
->name
= strdup(name
);
3731 var
->value
= strdup(value
);
3733 cupsArrayAdd(vars
->vars
, var
);
3739 * 'timeout_cb()' - Handle HTTP timeouts.
3742 static int /* O - 1 to continue, 0 to cancel */
3743 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3744 void *user_data
) /* I - User data (unused) */
3754 * 'usage()' - Show program usage.
3760 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
3762 _cupsLangPuts(stderr
, _("Options:"));
3763 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
3764 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
3765 _cupsLangPuts(stderr
, _(" -C Send requests using chunking "
3767 _cupsLangPuts(stderr
, _(" -E Test with TLS encryption."));
3768 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
3769 _cupsLangPuts(stderr
, _(" -L Send requests using "
3770 "content-length."));
3771 _cupsLangPuts(stderr
, _(" -S Test with SSL encryption."));
3772 _cupsLangPuts(stderr
, _(" -T Set the receive/send timeout in "
3774 _cupsLangPuts(stderr
, _(" -V version Set default IPP version."));
3775 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead of plain "
3777 _cupsLangPuts(stderr
, _(" -d name=value Define variable."));
3778 _cupsLangPuts(stderr
, _(" -f filename Set default request filename."));
3779 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with the "
3780 "given time interval."));
3781 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the given "
3782 "number of times."));
3783 _cupsLangPuts(stderr
, _(" -q Be quiet - no output except "
3785 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
3786 _cupsLangPuts(stderr
, _(" -v Show all attributes sent and "
3794 * 'validate_attr()' - Determine whether an attribute is valid.
3797 static int /* O - 1 if valid, 0 otherwise */
3798 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
3799 int print
) /* I - 1 = report issues to stdout */
3801 int i
; /* Looping var */
3802 char scheme
[64], /* Scheme from URI */
3803 userpass
[256], /* Username/password from URI */
3804 hostname
[256], /* Hostname from URI */
3805 resource
[1024]; /* Resource from URI */
3806 int port
, /* Port number from URI */
3807 uri_status
, /* URI separation status */
3808 valid
= 1; /* Is the attribute valid? */
3809 const char *ptr
; /* Pointer into string */
3810 ipp_attribute_t
*colattr
; /* Collection attribute */
3811 regex_t re
; /* Regular expression */
3812 ipp_uchar_t
*date
; /* Current date value */
3823 * Validate the attribute name.
3826 for (ptr
= attr
->name
; *ptr
; ptr
++)
3827 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
3830 if (*ptr
|| ptr
== attr
->name
)
3835 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
3836 "2911 section 4.1.3).", attr
->name
);
3839 if ((ptr
- attr
->name
) > 255)
3844 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
3845 "section 4.1.3).", attr
->name
);
3848 switch (attr
->value_tag
)
3850 case IPP_TAG_INTEGER
:
3853 case IPP_TAG_BOOLEAN
:
3854 for (i
= 0; i
< attr
->num_values
; i
++)
3856 if (attr
->values
[i
].boolean
!= 0 &&
3857 attr
->values
[i
].boolean
!= 1)
3862 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
3863 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
3871 for (i
= 0; i
< attr
->num_values
; i
++)
3873 if (attr
->values
[i
].integer
< 1)
3878 print_test_error("\"%s\": Bad enum value %d - out of range "
3879 "(RFC 2911 section 4.1.4).", attr
->name
,
3880 attr
->values
[i
].integer
);
3887 case IPP_TAG_STRING
:
3888 for (i
= 0; i
< attr
->num_values
; i
++)
3890 if (attr
->values
[i
].unknown
.length
> 1023)
3895 print_test_error("\"%s\": Bad octetString value - bad length %d "
3896 "(RFC 2911 section 4.1.10).", attr
->name
,
3897 attr
->values
[i
].unknown
.length
);
3905 for (i
= 0; i
< attr
->num_values
; i
++)
3907 date
= attr
->values
[i
].date
;
3909 if (date
[2] < 1 || date
[2] > 12)
3914 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
3915 "section 4.1.13).", attr
->name
, date
[2]);
3920 if (date
[3] < 1 || date
[3] > 31)
3925 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
3926 "section 4.1.13).", attr
->name
, date
[3]);
3936 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
3937 "section 4.1.13).", attr
->name
, date
[4]);
3947 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
3948 "section 4.1.13).", attr
->name
, date
[5]);
3958 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
3959 "section 4.1.13).", attr
->name
, date
[6]);
3969 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
3970 "section 4.1.13).", attr
->name
, date
[7]);
3975 if (date
[8] != '-' && date
[8] != '+')
3980 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
3981 "section 4.1.13).", attr
->name
, date
[8]);
3991 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
3992 "section 4.1.13).", attr
->name
, date
[9]);
4002 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
4003 "section 4.1.13).", attr
->name
, date
[10]);
4010 case IPP_TAG_RESOLUTION
:
4011 for (i
= 0; i
< attr
->num_values
; i
++)
4013 if (attr
->values
[i
].resolution
.xres
<= 0)
4018 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
4019 "feed resolution must be positive (RFC 2911 "
4020 "section 4.1.13).", attr
->name
,
4021 attr
->values
[i
].resolution
.xres
,
4022 attr
->values
[i
].resolution
.yres
,
4023 attr
->values
[i
].resolution
.units
==
4024 IPP_RES_PER_INCH
? "dpi" :
4025 attr
->values
[i
].resolution
.units
==
4026 IPP_RES_PER_CM
? "dpc" : "unknown");
4031 if (attr
->values
[i
].resolution
.yres
<= 0)
4036 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4037 "resolution must be positive (RFC 2911 section "
4038 "4.1.13).", attr
->name
,
4039 attr
->values
[i
].resolution
.xres
,
4040 attr
->values
[i
].resolution
.yres
,
4041 attr
->values
[i
].resolution
.units
==
4042 IPP_RES_PER_INCH
? "dpi" :
4043 attr
->values
[i
].resolution
.units
==
4044 IPP_RES_PER_CM
? "dpc" : "unknown");
4049 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4050 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4055 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4056 "units value (RFC 2911 section 4.1.13).",
4057 attr
->name
, attr
->values
[i
].resolution
.xres
,
4058 attr
->values
[i
].resolution
.yres
,
4059 attr
->values
[i
].resolution
.units
==
4060 IPP_RES_PER_INCH
? "dpi" :
4061 attr
->values
[i
].resolution
.units
==
4062 IPP_RES_PER_CM
? "dpc" : "unknown");
4069 case IPP_TAG_RANGE
:
4070 for (i
= 0; i
< attr
->num_values
; i
++)
4072 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4077 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4078 "greater than upper (RFC 2911 section 4.1.13).",
4079 attr
->name
, attr
->values
[i
].range
.lower
,
4080 attr
->values
[i
].range
.upper
);
4087 case IPP_TAG_BEGIN_COLLECTION
:
4088 for (i
= 0; i
< attr
->num_values
; i
++)
4090 for (colattr
= attr
->values
[i
].collection
->attrs
;
4092 colattr
= colattr
->next
)
4094 if (!validate_attr(colattr
, 0))
4101 if (colattr
&& print
)
4103 print_test_error("\"%s\": Bad collection value.", attr
->name
);
4107 validate_attr(colattr
, print
);
4108 colattr
= colattr
->next
;
4115 case IPP_TAG_TEXTLANG
:
4116 for (i
= 0; i
< attr
->num_values
; i
++)
4118 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4120 if ((*ptr
& 0xe0) == 0xc0)
4123 if ((*ptr
& 0xc0) != 0x80)
4126 else if ((*ptr
& 0xf0) == 0xe0)
4129 if ((*ptr
& 0xc0) != 0x80)
4132 if ((*ptr
& 0xc0) != 0x80)
4135 else if ((*ptr
& 0xf8) == 0xf0)
4138 if ((*ptr
& 0xc0) != 0x80)
4141 if ((*ptr
& 0xc0) != 0x80)
4144 if ((*ptr
& 0xc0) != 0x80)
4147 else if (*ptr
& 0x80)
4156 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4157 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4158 attr
->values
[i
].string
.text
);
4163 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4168 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4169 "(RFC 2911 section 4.1.1).", attr
->name
,
4170 attr
->values
[i
].string
.text
,
4171 (int)strlen(attr
->values
[i
].string
.text
));
4179 case IPP_TAG_NAMELANG
:
4180 for (i
= 0; i
< attr
->num_values
; i
++)
4182 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4184 if ((*ptr
& 0xe0) == 0xc0)
4187 if ((*ptr
& 0xc0) != 0x80)
4190 else if ((*ptr
& 0xf0) == 0xe0)
4193 if ((*ptr
& 0xc0) != 0x80)
4196 if ((*ptr
& 0xc0) != 0x80)
4199 else if ((*ptr
& 0xf8) == 0xf0)
4202 if ((*ptr
& 0xc0) != 0x80)
4205 if ((*ptr
& 0xc0) != 0x80)
4208 if ((*ptr
& 0xc0) != 0x80)
4211 else if (*ptr
& 0x80)
4220 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4221 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4222 attr
->values
[i
].string
.text
);
4227 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4232 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4233 "(RFC 2911 section 4.1.2).", attr
->name
,
4234 attr
->values
[i
].string
.text
,
4235 (int)strlen(attr
->values
[i
].string
.text
));
4242 case IPP_TAG_KEYWORD
:
4243 for (i
= 0; i
< attr
->num_values
; i
++)
4245 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4246 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4250 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4255 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4256 "character (RFC 2911 section 4.1.3).",
4257 attr
->name
, attr
->values
[i
].string
.text
);
4262 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4267 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4268 "length %d (RFC 2911 section 4.1.3).",
4269 attr
->name
, attr
->values
[i
].string
.text
,
4270 (int)strlen(attr
->values
[i
].string
.text
));
4278 for (i
= 0; i
< attr
->num_values
; i
++)
4280 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4281 attr
->values
[i
].string
.text
,
4282 scheme
, sizeof(scheme
),
4283 userpass
, sizeof(userpass
),
4284 hostname
, sizeof(hostname
),
4285 &port
, resource
, sizeof(resource
));
4287 if (uri_status
< HTTP_URI_OK
)
4292 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4293 "(RFC 2911 section 4.1.5).", attr
->name
,
4294 attr
->values
[i
].string
.text
,
4295 URIStatusStrings
[uri_status
-
4296 HTTP_URI_OVERFLOW
]);
4301 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4306 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4307 "(RFC 2911 section 4.1.5).", attr
->name
,
4308 attr
->values
[i
].string
.text
,
4309 (int)strlen(attr
->values
[i
].string
.text
));
4316 case IPP_TAG_URISCHEME
:
4317 for (i
= 0; i
< attr
->num_values
; i
++)
4319 ptr
= attr
->values
[i
].string
.text
;
4320 if (islower(*ptr
& 255))
4322 for (ptr
++; *ptr
; ptr
++)
4323 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4324 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4328 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4333 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4334 "characters (RFC 2911 section 4.1.6).",
4335 attr
->name
, attr
->values
[i
].string
.text
);
4340 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4345 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4346 "length %d (RFC 2911 section 4.1.6).",
4347 attr
->name
, attr
->values
[i
].string
.text
,
4348 (int)strlen(attr
->values
[i
].string
.text
));
4355 case IPP_TAG_CHARSET
:
4356 for (i
= 0; i
< attr
->num_values
; i
++)
4358 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4359 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4360 isspace(*ptr
& 255))
4363 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4368 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4369 "characters (RFC 2911 section 4.1.7).",
4370 attr
->name
, attr
->values
[i
].string
.text
);
4375 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4380 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4381 "length %d (RFC 2911 section 4.1.7).",
4382 attr
->name
, attr
->values
[i
].string
.text
,
4383 (int)strlen(attr
->values
[i
].string
.text
));
4390 case IPP_TAG_LANGUAGE
:
4392 * The following regular expression is derived from the ABNF for
4393 * language tags in RFC 4646. All I can say is that this is the
4394 * easiest way to check the values...
4397 if ((i
= regcomp(&re
,
4399 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4401 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4402 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4403 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4404 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4405 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4407 "x(-[a-z0-9]{1,8})+" /* privateuse */
4409 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
4411 REG_NOSUB
| REG_EXTENDED
)) != 0)
4413 char temp
[256]; /* Temporary error string */
4415 regerror(i
, &re
, temp
, sizeof(temp
));
4416 print_fatal_error("Unable to compile naturalLanguage regular "
4417 "expression: %s.", temp
);
4420 for (i
= 0; i
< attr
->num_values
; i
++)
4422 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4427 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4428 "characters (RFC 2911 section 4.1.8).",
4429 attr
->name
, attr
->values
[i
].string
.text
);
4434 if (strlen(attr
->values
[i
].string
.text
) > 63)
4439 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4440 "length %d (RFC 2911 section 4.1.8).",
4441 attr
->name
, attr
->values
[i
].string
.text
,
4442 (int)strlen(attr
->values
[i
].string
.text
));
4451 case IPP_TAG_MIMETYPE
:
4453 * The following regular expression is derived from the ABNF for
4454 * language tags in RFC 2045 and 4288. All I can say is that this is
4455 * the easiest way to check the values...
4458 if ((i
= regcomp(&re
,
4460 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
4462 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
4463 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
4464 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
4467 REG_NOSUB
| REG_EXTENDED
)) != 0)
4469 char temp
[256]; /* Temporary error string */
4471 regerror(i
, &re
, temp
, sizeof(temp
));
4472 print_fatal_error("Unable to compile mimeMediaType regular "
4473 "expression: %s.", temp
);
4476 for (i
= 0; i
< attr
->num_values
; i
++)
4478 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4483 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4484 "characters (RFC 2911 section 4.1.9).",
4485 attr
->name
, attr
->values
[i
].string
.text
);
4490 if (strlen(attr
->values
[i
].string
.text
) > 255)
4495 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4496 "length %d (RFC 2911 section 4.1.9).",
4497 attr
->name
, attr
->values
[i
].string
.text
,
4498 (int)strlen(attr
->values
[i
].string
.text
));
4514 * 'with_value()' - Test a WITH-VALUE predicate.
4517 static int /* O - 1 on match, 0 on non-match */
4518 with_value(char *value
, /* I - Value string */
4519 int regex
, /* I - Value is a regular expression */
4520 ipp_attribute_t
*attr
, /* I - Attribute to compare */
4521 int report
) /* I - 1 = report failures */
4523 int i
; /* Looping var */
4524 char *valptr
; /* Pointer into value */
4528 * NULL matches everything.
4531 if (!value
|| !*value
)
4535 * Compare the value string to the attribute value.
4538 switch (attr
->value_tag
)
4540 case IPP_TAG_INTEGER
:
4542 for (i
= 0; i
< attr
->num_values
; i
++)
4544 char op
, /* Comparison operator */
4545 *nextptr
; /* Next pointer */
4546 int intvalue
; /* Integer value */
4550 if (!strncmp(valptr
, "no-value,", 9))
4553 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
4554 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
4555 *valptr
== '=' || *valptr
== '>')
4558 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
4560 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
4568 intvalue
= strtol(valptr
, &nextptr
, 0);
4569 if (nextptr
== valptr
)
4576 if (attr
->values
[i
].integer
== intvalue
)
4580 if (attr
->values
[i
].integer
< intvalue
)
4584 if (attr
->values
[i
].integer
> intvalue
)
4593 for (i
= 0; i
< attr
->num_values
; i
++)
4594 print_test_error("GOT: %s=%d", attr
->name
, attr
->values
[i
].integer
);
4598 case IPP_TAG_RANGE
:
4599 for (i
= 0; i
< attr
->num_values
; i
++)
4601 char op
, /* Comparison operator */
4602 *nextptr
; /* Next pointer */
4603 int intvalue
; /* Integer value */
4607 if (!strncmp(valptr
, "no-value,", 9))
4610 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
4611 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
4612 *valptr
== '=' || *valptr
== '>')
4615 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
4617 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
4625 intvalue
= strtol(valptr
, &nextptr
, 0);
4626 if (nextptr
== valptr
)
4633 if (attr
->values
[i
].range
.upper
== intvalue
)
4637 if (attr
->values
[i
].range
.upper
< intvalue
)
4641 if (attr
->values
[i
].range
.upper
> intvalue
)
4650 for (i
= 0; i
< attr
->num_values
; i
++)
4651 print_test_error("GOT: %s=%d-%d", attr
->name
,
4652 attr
->values
[i
].range
.lower
,
4653 attr
->values
[i
].range
.upper
);
4657 case IPP_TAG_BOOLEAN
:
4658 for (i
= 0; i
< attr
->num_values
; i
++)
4660 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
4666 for (i
= 0; i
< attr
->num_values
; i
++)
4667 print_test_error("GOT: %s=%s", attr
->name
,
4668 attr
->values
[i
].boolean
? "true" : "false");
4672 case IPP_TAG_NOVALUE
:
4673 return (!strcmp(value
, "no-value") || !strncmp(value
, "no-value,", 9));
4675 case IPP_TAG_CHARSET
:
4676 case IPP_TAG_KEYWORD
:
4677 case IPP_TAG_LANGUAGE
:
4678 case IPP_TAG_MIMETYPE
:
4680 case IPP_TAG_NAMELANG
:
4682 case IPP_TAG_TEXTLANG
:
4684 case IPP_TAG_URISCHEME
:
4688 * Value is an extended, case-sensitive POSIX regular expression...
4691 regex_t re
; /* Regular expression */
4693 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
4695 char temp
[256]; /* Temporary string */
4697 regerror(i
, &re
, temp
, sizeof(temp
));
4699 print_fatal_error("Unable to compile WITH-VALUE regular expression "
4700 "\"%s\" - %s", value
, temp
);
4705 * See if ALL of the values match the given regular expression.
4708 for (i
= 0; i
< attr
->num_values
; i
++)
4710 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4713 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4714 attr
->values
[i
].string
.text
);
4722 return (i
== attr
->num_values
);
4727 * Value is a literal string, see if at least one value matches the
4731 for (i
= 0; i
< attr
->num_values
; i
++)
4733 if (!strcmp(value
, attr
->values
[i
].string
.text
))
4739 for (i
= 0; i
< attr
->num_values
; i
++)
4740 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4741 attr
->values
[i
].string
.text
);