4 * ipptool command for CUPS.
6 * Copyright 2007-2011 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 * sigterm_handler() - Handle SIGINT and SIGTERM.
42 * timeout_cb() - Handle HTTP timeouts.
43 * usage() - Show program usage.
44 * validate_attr() - Determine whether an attribute is valid.
45 * with_value() - Test a WITH-VALUE predicate.
49 * Include necessary headers...
52 #include <cups/cups-private.h>
53 #include <cups/file-private.h>
61 #endif /* !O_BINARY */
68 typedef enum _cups_transfer_e
/**** How to send request data ****/
70 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
71 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
72 _CUPS_TRANSFER_LENGTH
/* Length always */
75 typedef enum _cups_output_e
/**** Output mode ****/
77 _CUPS_OUTPUT_QUIET
, /* No output */
78 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
79 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
80 _CUPS_OUTPUT_LIST
, /* Tabular list output */
81 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
84 typedef struct _cups_expect_s
/**** Expected attribute info ****/
86 int optional
, /* Optional attribute? */
87 not_expect
; /* Don't expect attribute? */
88 char *name
, /* Attribute name */
89 *of_type
, /* Type name */
90 *same_count_as
, /* Parallel attribute name */
91 *if_defined
, /* Only required if variable defined */
92 *if_not_defined
, /* Only required if variable is not defined */
93 *with_value
, /* Attribute must include this value */
94 *define_match
, /* Variable to define on match */
95 *define_no_match
, /* Variable to define on no-match */
96 *define_value
; /* Variable to define with value */
97 int repeat_match
, /* Repeat test on match */
98 repeat_no_match
, /* Repeat test on no match */
99 with_regex
, /* WITH-VALUE is a regular expression */
100 count
; /* Expected count if > 0 */
101 ipp_tag_t in_group
; /* IN-GROUP value */
104 typedef struct _cups_status_s
/**** Status info ****/
106 ipp_status_t status
; /* Expected status code */
107 char *if_defined
, /* Only if variable is defined */
108 *if_not_defined
; /* Only if variable is not defined */
109 int repeat_match
, /* Repeat the test when it does not match */
110 repeat_no_match
; /* Repeat the test when it matches */
113 typedef struct _cups_var_s
/**** Variable ****/
115 char *name
, /* Name of variable */
116 *value
; /* Value of variable */
119 typedef struct _cups_vars_s
/**** Set of variables ****/
121 char *uri
, /* URI for printer */
122 *filename
, /* Filename */
123 scheme
[64], /* Scheme from URI */
124 userpass
[256], /* Username/password from URI */
125 hostname
[256], /* Hostname from URI */
126 resource
[1024]; /* Resource path from URI */
127 int port
; /* Port number from URI */
128 http_encryption_t encryption
; /* Encryption for connection? */
129 double timeout
; /* Timeout for connection */
130 int family
; /* Address family */
131 cups_array_t
*vars
; /* Array of variables */
139 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
140 /* How to transfer requests */
141 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
143 int Cancel
= 0, /* Cancel test? */
144 IgnoreErrors
= 0, /* Ignore errors? */
145 Verbosity
= 0, /* Show all attributes? */
146 Version
= 11, /* Default IPP version */
147 XMLHeader
= 0, /* 1 if header is written */
148 TestCount
= 0, /* Number of tests run */
149 PassCount
= 0, /* Number of passing tests */
150 FailCount
= 0, /* Number of failing tests */
151 SkipCount
= 0; /* Number of skipped tests */
152 char *Password
= NULL
; /* Password from URI */
153 const char * const URIStatusStrings
[] = /* URI status strings */
156 "Bad arguments to function",
157 "Bad resource in URI",
158 "Bad port number in URI",
159 "Bad hostname/address in URI",
160 "Bad username in URI",
164 "Missing scheme in URI",
165 "Unknown scheme in URI",
166 "Missing resource in URI"
174 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
175 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
176 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
177 size_t dstsize
) __attribute__((nonnull(1,2,3)));
178 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
179 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
180 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
182 static char *get_token(FILE *fp
, char *buf
, int buflen
,
184 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
185 static char *iso_date(ipp_uchar_t
*date
);
186 static const char *password_cb(const char *prompt
);
187 static void print_attr(ipp_attribute_t
*attr
, ipp_tag_t
*group
);
188 static void print_col(ipp_t
*col
);
189 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
190 char **displayed
, size_t *widths
);
191 static void print_fatal_error(const char *s
, ...)
192 __attribute__ ((__format__ (__printf__
, 1, 2)));
193 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
194 char **displayed
, size_t *widths
);
195 static void print_test_error(const char *s
, ...)
196 __attribute__ ((__format__ (__printf__
, 1, 2)));
197 static void print_xml_header(void);
198 static void print_xml_string(const char *element
, const char *s
);
199 static void print_xml_trailer(int success
, const char *message
);
200 static void set_variable(_cups_vars_t
*vars
, const char *name
,
203 static void sigterm_handler(int sig
);
205 static int timeout_cb(http_t
*http
, void *user_data
);
206 static void usage(void) __attribute__((noreturn
));
207 static int validate_attr(ipp_attribute_t
*attr
, int print
);
208 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
,
209 int report
, char *matchbuf
, size_t matchlen
);
213 * 'main()' - Parse options and do tests.
216 int /* O - Exit status */
217 main(int argc
, /* I - Number of command-line args */
218 char *argv
[]) /* I - Command-line arguments */
220 int i
; /* Looping var */
221 int status
; /* Status of tests... */
222 char *opt
, /* Current option */
223 name
[1024], /* Name/value buffer */
224 *value
, /* Pointer to value */
225 filename
[1024], /* Real filename */
226 testname
[1024], /* Real test filename */
227 uri
[1024]; /* Copy of printer URI */
228 const char *ext
, /* Extension on filename */
229 *testfile
; /* Test file to use */
230 int interval
, /* Test interval in microseconds */
231 repeat
; /* Repeat count */
232 _cups_vars_t vars
; /* Variables */
233 http_uri_status_t uri_status
; /* URI separation status */
234 _cups_globals_t
*cg
= _cupsGlobals();
240 * Catch SIGINT and SIGTERM...
243 signal(SIGINT
, sigterm_handler
);
244 signal(SIGTERM
, sigterm_handler
);
248 * Initialize the locale and variables...
251 _cupsSetLocale(argv
);
253 memset(&vars
, 0, sizeof(vars
));
254 vars
.family
= AF_UNSPEC
;
255 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
260 * ipptool URI testfile
268 for (i
= 1; i
< argc
; i
++)
270 if (argv
[i
][0] == '-')
272 for (opt
= argv
[i
] + 1; *opt
; opt
++)
276 case '4' : /* Connect using IPv4 only */
277 vars
.family
= AF_INET
;
281 case '6' : /* Connect using IPv6 only */
282 vars
.family
= AF_INET6
;
284 #endif /* AF_INET6 */
286 case 'C' : /* Enable HTTP chunking */
287 Transfer
= _CUPS_TRANSFER_CHUNKED
;
290 case 'E' : /* Encrypt with TLS */
292 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
294 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
296 #endif /* HAVE_SSL */
299 case 'I' : /* Ignore errors */
303 case 'L' : /* Disable HTTP chunking */
304 Transfer
= _CUPS_TRANSFER_LENGTH
;
307 case 'S' : /* Encrypt with SSL */
309 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
311 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
313 #endif /* HAVE_SSL */
316 case 'T' : /* Set timeout */
321 _cupsLangPuts(stderr
,
322 _("ipptool: Missing timeout for \"-T\"."));
326 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
329 case 'V' : /* Set IPP version */
334 _cupsLangPuts(stderr
,
335 _("ipptool: Missing version for \"-V\"."));
339 if (!strcmp(argv
[i
], "1.0"))
341 else if (!strcmp(argv
[i
], "1.1"))
343 else if (!strcmp(argv
[i
], "2.0"))
345 else if (!strcmp(argv
[i
], "2.1"))
347 else if (!strcmp(argv
[i
], "2.2"))
351 _cupsLangPrintf(stderr
,
352 _("ipptool: Bad version %s for \"-V\"."),
358 case 'X' : /* Produce XML output */
359 Output
= _CUPS_OUTPUT_PLIST
;
361 if (interval
|| repeat
)
363 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
364 "incompatible with -X\"."));
369 case 'c' : /* CSV output */
370 Output
= _CUPS_OUTPUT_CSV
;
373 case 'd' : /* Define a variable */
378 _cupsLangPuts(stderr
,
379 _("ipptool: Missing name=value for \"-d\"."));
383 strlcpy(name
, argv
[i
], sizeof(name
));
384 if ((value
= strchr(name
, '=')) != NULL
)
387 value
= name
+ strlen(name
);
389 set_variable(&vars
, name
, value
);
392 case 'f' : /* Set the default test filename */
397 _cupsLangPuts(stderr
,
398 _("ipptool: Missing filename for \"-f\"."));
405 if (access(argv
[i
], 0))
411 snprintf(filename
, sizeof(filename
), "%s.gz", argv
[i
]);
412 if (access(filename
, 0) && filename
[0] != '/')
414 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
415 cg
->cups_datadir
, argv
[i
]);
416 if (access(filename
, 0))
418 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s.gz",
419 cg
->cups_datadir
, argv
[i
]);
420 if (access(filename
, 0))
421 vars
.filename
= strdup(argv
[i
]);
424 vars
.filename
= strdup(filename
);
427 vars
.filename
= strdup(filename
);
430 vars
.filename
= strdup(argv
[i
]);
432 if ((ext
= strrchr(vars
.filename
, '.')) != NULL
)
435 * Guess the MIME media type based on the extension...
438 if (!_cups_strcasecmp(ext
, ".gif"))
439 set_variable(&vars
, "filetype", "image/gif");
440 else if (!_cups_strcasecmp(ext
, ".htm") ||
441 !_cups_strcasecmp(ext
, ".htm.gz") ||
442 !_cups_strcasecmp(ext
, ".html") ||
443 !_cups_strcasecmp(ext
, ".html.gz"))
444 set_variable(&vars
, "filetype", "text/html");
445 else if (!_cups_strcasecmp(ext
, ".jpg"))
446 set_variable(&vars
, "filetype", "image/jpeg");
447 else if (!_cups_strcasecmp(ext
, ".pdf"))
448 set_variable(&vars
, "filetype", "application/pdf");
449 else if (!_cups_strcasecmp(ext
, ".png"))
450 set_variable(&vars
, "filetype", "image/png");
451 else if (!_cups_strcasecmp(ext
, ".ps") ||
452 !_cups_strcasecmp(ext
, ".ps.gz"))
453 set_variable(&vars
, "filetype", "application/postscript");
454 else if (!_cups_strcasecmp(ext
, ".ras") ||
455 !_cups_strcasecmp(ext
, ".ras.gz"))
456 set_variable(&vars
, "filetype", "image/pwg-raster");
457 else if (!_cups_strcasecmp(ext
, ".txt") ||
458 !_cups_strcasecmp(ext
, ".txt.gz"))
459 set_variable(&vars
, "filetype", "text/plain");
460 else if (!_cups_strcasecmp(ext
, ".xps"))
461 set_variable(&vars
, "filetype", "application/openxps");
463 set_variable(&vars
, "filetype", "application/octet-stream");
468 * Use the "auto-type" MIME media type...
471 set_variable(&vars
, "filetype", "application/octet-stream");
475 case 'i' : /* Test every N seconds */
480 _cupsLangPuts(stderr
,
481 _("ipptool: Missing seconds for \"-i\"."));
486 interval
= (int)(_cupsStrScand(argv
[i
], NULL
, localeconv()) *
490 _cupsLangPuts(stderr
,
491 _("ipptool: Invalid seconds for \"-i\"."));
496 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
498 _cupsLangPuts(stderr
, _("ipptool: \"-i\" is incompatible with "
504 case 'l' : /* List as a table */
505 Output
= _CUPS_OUTPUT_LIST
;
508 case 'n' : /* Repeat count */
513 _cupsLangPuts(stderr
,
514 _("ipptool: Missing count for \"-n\"."));
518 repeat
= atoi(argv
[i
]);
520 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
522 _cupsLangPuts(stderr
, _("ipptool: \"-n\" is incompatible with "
528 case 'q' : /* Be quiet */
529 Output
= _CUPS_OUTPUT_QUIET
;
532 case 't' : /* CUPS test output */
533 Output
= _CUPS_OUTPUT_TEST
;
536 case 'v' : /* Be verbose */
541 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\"."),
548 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
550 || !strncmp(argv
[i
], "ipps://", 7)
551 || !strncmp(argv
[i
], "https://", 8)
552 #endif /* HAVE_SSL */
561 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI."));
566 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
567 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
568 #endif /* HAVE_SSL */
571 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
572 vars
.scheme
, sizeof(vars
.scheme
),
573 vars
.userpass
, sizeof(vars
.userpass
),
574 vars
.hostname
, sizeof(vars
.hostname
),
576 vars
.resource
, sizeof(vars
.resource
));
578 if (uri_status
!= HTTP_URI_OK
)
580 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s."),
581 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
585 if (vars
.userpass
[0])
587 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
590 cupsSetUser(vars
.userpass
);
591 cupsSetPasswordCB(password_cb
);
592 set_variable(&vars
, "uriuser", vars
.userpass
);
595 httpAssembleURI(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), vars
.scheme
, NULL
,
596 vars
.hostname
, vars
.port
, vars
.resource
);
607 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
611 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
613 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
615 if (access(testname
, 0))
623 if (!do_tests(&vars
, testfile
))
628 if (!vars
.uri
|| !testfile
)
632 * Loop if the interval is set...
635 if (Output
== _CUPS_OUTPUT_PLIST
)
636 print_xml_trailer(!status
, NULL
);
637 else if (interval
> 0 && repeat
> 0)
642 do_tests(&vars
, testfile
);
646 else if (interval
> 0)
651 do_tests(&vars
, testfile
);
654 else if (Output
== _CUPS_OUTPUT_TEST
&& TestCount
> 1)
657 * Show a summary report if there were multiple tests...
660 printf("\nSummary: %d tests, %d passed, %d failed, %d skipped\n"
661 "Score: %d%%\n", TestCount
, PassCount
, FailCount
, SkipCount
,
662 100 * (PassCount
+ SkipCount
) / TestCount
);
674 * 'compare_vars()' - Compare two variables.
677 static int /* O - Result of comparison */
678 compare_vars(_cups_var_t
*a
, /* I - First variable */
679 _cups_var_t
*b
) /* I - Second variable */
681 return (_cups_strcasecmp(a
->name
, b
->name
));
686 * 'do_tests()' - Do tests as specified in the test file.
689 static int /* 1 = success, 0 = failure */
690 do_tests(_cups_vars_t
*vars
, /* I - Variables */
691 const char *testfile
) /* I - Test file to use */
693 int i
, /* Looping var */
694 linenum
, /* Current line number */
695 pass
, /* Did we pass the test? */
696 prev_pass
= 1, /* Did we pass the previous test? */
697 request_id
, /* Current request ID */
698 show_header
= 1, /* Show the test header? */
699 ignore_errors
, /* Ignore test failures? */
700 skip_previous
= 0, /* Skip on previous test failure? */
701 repeat_test
; /* Repeat a test? */
702 http_t
*http
= NULL
; /* HTTP connection to server */
703 FILE *fp
= NULL
; /* Test file */
704 char resource
[512], /* Resource for request */
705 token
[1024], /* Token from file */
706 *tokenptr
, /* Pointer into token */
707 temp
[1024], /* Temporary string */
708 buffer
[8192]; /* Copy buffer */
709 ipp_t
*request
= NULL
, /* IPP request */
710 *response
= NULL
; /* IPP response */
711 size_t length
; /* Length of IPP request */
712 http_status_t status
; /* HTTP status */
713 cups_file_t
*reqfile
; /* File to send */
714 ssize_t bytes
; /* Bytes read/written */
715 char attr
[128]; /* Attribute name */
716 ipp_op_t op
; /* Operation */
717 ipp_tag_t group
; /* Current group */
718 ipp_tag_t value
; /* Current value type */
719 ipp_attribute_t
*attrptr
, /* Attribute pointer */
720 *found
, /* Found attribute */
721 *lastcol
= NULL
; /* Last collection attribute */
722 char name
[1024]; /* Name of test */
723 char filename
[1024]; /* Filename */
724 _cups_transfer_t transfer
; /* To chunk or not to chunk */
725 int version
, /* IPP version number to use */
726 skip_test
; /* Skip this test? */
727 int num_statuses
= 0; /* Number of valid status codes */
728 _cups_status_t statuses
[100], /* Valid status codes */
729 *last_status
; /* Last STATUS (for predicates) */
730 int num_expects
= 0; /* Number of expected attributes */
731 _cups_expect_t expects
[200], /* Expected attributes */
732 *expect
, /* Current expected attribute */
733 *last_expect
; /* Last EXPECT (for predicates) */
734 int num_displayed
= 0; /* Number of displayed attributes */
735 char *displayed
[200]; /* Displayed attributes */
736 size_t widths
[200]; /* Width of columns */
737 cups_array_t
*a
; /* Duplicate attribute array */
741 * Open the test file...
744 if ((fp
= fopen(testfile
, "r")) == NULL
)
746 print_fatal_error("Unable to open test file %s - %s", testfile
,
753 * Connect to the server...
756 if ((http
= _httpCreate(vars
->hostname
, vars
->port
, NULL
, vars
->encryption
,
757 vars
->family
)) == NULL
)
759 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
760 vars
->port
, cupsLastErrorString());
765 if (httpReconnect(http
))
767 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
768 vars
->port
, cupsLastErrorString());
773 if (vars
->timeout
> 0.0)
774 httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
780 CUPS_SRAND(time(NULL
));
784 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
786 while (!Cancel
&& get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
789 * Expect an open brace...
792 if (!strcmp(token
, "DEFINE"))
798 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
799 get_token(fp
, temp
, sizeof(temp
), &linenum
))
801 expand_variables(vars
, token
, temp
, sizeof(token
));
802 set_variable(vars
, attr
, token
);
806 print_fatal_error("Missing DEFINE name and/or value on line %d.",
814 else if (!strcmp(token
, "DEFINE-DEFAULT"))
817 * DEFINE-DEFAULT name value
820 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
821 get_token(fp
, temp
, sizeof(temp
), &linenum
))
823 expand_variables(vars
, token
, temp
, sizeof(token
));
824 if (!get_variable(vars
, attr
))
825 set_variable(vars
, attr
, token
);
829 print_fatal_error("Missing DEFINE-DEFAULT name and/or value on line "
837 else if (!strcmp(token
, "IGNORE-ERRORS"))
844 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
845 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
847 IgnoreErrors
= !_cups_strcasecmp(temp
, "yes");
851 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
858 else if (!strcmp(token
, "INCLUDE"))
865 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
868 * Map the filename to and then run the tests...
871 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
882 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
890 else if (!strcmp(token
, "INCLUDE-IF-DEFINED"))
893 * INCLUDE-IF-DEFINED name "filename"
894 * INCLUDE-IF-DEFINED name <filename>
897 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
898 get_token(fp
, temp
, sizeof(temp
), &linenum
))
901 * Map the filename to and then run the tests...
904 if (get_variable(vars
, attr
) &&
905 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
916 print_fatal_error("Missing INCLUDE-IF-DEFINED name or filename on line "
925 else if (!strcmp(token
, "INCLUDE-IF-NOT-DEFINED"))
928 * INCLUDE-IF-NOT-DEFINED name "filename"
929 * INCLUDE-IF-NOT-DEFINED name <filename>
932 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
933 get_token(fp
, temp
, sizeof(temp
), &linenum
))
936 * Map the filename to and then run the tests...
939 if (!get_variable(vars
, attr
) &&
940 !do_tests(vars
, get_filename(testfile
, filename
, temp
,
951 print_fatal_error("Missing INCLUDE-IF-NOT-DEFINED name or filename on "
952 "line %d.", linenum
);
960 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
963 * SKIP-IF-DEFINED variable
966 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
968 if (get_variable(vars
, temp
))
973 print_fatal_error("Missing SKIP-IF-DEFINED variable on line %d.",
979 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
982 * SKIP-IF-NOT-DEFINED variable
985 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
987 if (!get_variable(vars
, temp
))
992 print_fatal_error("Missing SKIP-IF-NOT-DEFINED variable on line %d.",
998 else if (!strcmp(token
, "TRANSFER"))
1006 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1008 if (!strcmp(temp
, "auto"))
1009 Transfer
= _CUPS_TRANSFER_AUTO
;
1010 else if (!strcmp(temp
, "chunked"))
1011 Transfer
= _CUPS_TRANSFER_CHUNKED
;
1012 else if (!strcmp(temp
, "length"))
1013 Transfer
= _CUPS_TRANSFER_LENGTH
;
1016 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1024 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1031 else if (!strcmp(token
, "VERSION"))
1033 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1035 if (!strcmp(temp
, "1.0"))
1037 else if (!strcmp(temp
, "1.1"))
1039 else if (!strcmp(temp
, "2.0"))
1041 else if (!strcmp(temp
, "2.1"))
1043 else if (!strcmp(temp
, "2.2"))
1047 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1054 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1061 else if (strcmp(token
, "{"))
1063 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
1069 * Initialize things...
1074 if (Output
== _CUPS_OUTPUT_PLIST
)
1076 else if (Output
== _CUPS_OUTPUT_TEST
)
1077 printf("\"%s\":\n", testfile
);
1082 strlcpy(resource
, vars
->resource
, sizeof(resource
));
1087 group
= IPP_TAG_ZERO
;
1088 ignore_errors
= IgnoreErrors
;
1095 transfer
= Transfer
;
1097 strlcpy(name
, testfile
, sizeof(name
));
1098 if (strrchr(name
, '.') != NULL
)
1099 *strrchr(name
, '.') = '\0';
1102 * Parse until we see a close brace...
1105 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
1107 if (_cups_strcasecmp(token
, "COUNT") &&
1108 _cups_strcasecmp(token
, "DEFINE-MATCH") &&
1109 _cups_strcasecmp(token
, "DEFINE-NO-MATCH") &&
1110 _cups_strcasecmp(token
, "DEFINE-VALUE") &&
1111 _cups_strcasecmp(token
, "IF-DEFINED") &&
1112 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1113 _cups_strcasecmp(token
, "IN-GROUP") &&
1114 _cups_strcasecmp(token
, "OF-TYPE") &&
1115 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1116 _cups_strcasecmp(token
, "REPEAT-NO-MATCH") &&
1117 _cups_strcasecmp(token
, "SAME-COUNT-AS") &&
1118 _cups_strcasecmp(token
, "WITH-VALUE"))
1121 if (_cups_strcasecmp(token
, "IF-DEFINED") &&
1122 _cups_strcasecmp(token
, "IF-NOT-DEFINED") &&
1123 _cups_strcasecmp(token
, "REPEAT-MATCH") &&
1124 _cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1127 if (!strcmp(token
, "}"))
1129 else if (!strcmp(token
, "{") && lastcol
)
1132 * Another collection value
1135 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1136 /* Collection value */
1140 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
1144 * Reallocate memory...
1147 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
1148 (lastcol
->num_values
+ 1) *
1149 sizeof(ipp_value_t
))) == NULL
)
1151 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
1156 if (tempcol
!= lastcol
)
1159 * Reset pointers in the list...
1163 request
->prev
->next
= tempcol
;
1165 request
->attrs
= tempcol
;
1167 lastcol
= request
->current
= request
->last
= tempcol
;
1170 lastcol
->values
[lastcol
->num_values
].collection
= col
;
1171 lastcol
->num_values
++;
1179 else if (!strcmp(token
, "DEFINE"))
1185 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
1186 get_token(fp
, temp
, sizeof(temp
), &linenum
))
1188 expand_variables(vars
, token
, temp
, sizeof(token
));
1189 set_variable(vars
, attr
, token
);
1193 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1199 else if (!strcmp(token
, "IGNORE-ERRORS"))
1206 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1207 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1209 ignore_errors
= !_cups_strcasecmp(temp
, "yes");
1213 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
1220 else if (!_cups_strcasecmp(token
, "NAME"))
1226 get_token(fp
, name
, sizeof(name
), &linenum
);
1228 else if (!strcmp(token
, "REQUEST-ID"))
1235 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1237 if (isdigit(temp
[0] & 255))
1238 request_id
= atoi(temp
);
1239 else if (!_cups_strcasecmp(temp
, "random"))
1240 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1243 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1251 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1256 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1259 * SKIP-IF-DEFINED variable
1262 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1264 if (get_variable(vars
, temp
))
1269 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1275 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1278 * SKIP-IF-NOT-DEFINED variable
1281 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1283 if (!get_variable(vars
, temp
))
1288 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1294 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1297 * SKIP-PREVIOUS-ERROR yes
1298 * SKIP-PREVIOUS-ERROR no
1301 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1302 (!_cups_strcasecmp(temp
, "yes") || !_cups_strcasecmp(temp
, "no")))
1304 skip_previous
= !_cups_strcasecmp(temp
, "yes");
1308 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1315 else if (!strcmp(token
, "TRANSFER"))
1323 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1325 if (!strcmp(temp
, "auto"))
1326 transfer
= _CUPS_TRANSFER_AUTO
;
1327 else if (!strcmp(temp
, "chunked"))
1328 transfer
= _CUPS_TRANSFER_CHUNKED
;
1329 else if (!strcmp(temp
, "length"))
1330 transfer
= _CUPS_TRANSFER_LENGTH
;
1333 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1341 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1346 else if (!_cups_strcasecmp(token
, "VERSION"))
1348 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1350 if (!strcmp(temp
, "0.0"))
1352 else if (!strcmp(temp
, "1.0"))
1354 else if (!strcmp(temp
, "1.1"))
1356 else if (!strcmp(temp
, "2.0"))
1358 else if (!strcmp(temp
, "2.1"))
1360 else if (!strcmp(temp
, "2.2"))
1364 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1371 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1376 else if (!_cups_strcasecmp(token
, "RESOURCE"))
1382 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1384 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1389 else if (!_cups_strcasecmp(token
, "OPERATION"))
1395 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1397 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1402 if ((op
= ippOpValue(token
)) == (ipp_op_t
)-1 &&
1403 (op
= strtol(token
, NULL
, 0)) == 0)
1405 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1411 else if (!_cups_strcasecmp(token
, "GROUP"))
1414 * Attribute group...
1417 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1419 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1424 if ((value
= ippTagValue(token
)) < 0)
1426 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1432 ippAddSeparator(request
);
1436 else if (!_cups_strcasecmp(token
, "DELAY"))
1439 * Delay before operation...
1444 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1446 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1451 if ((delay
= _cupsStrScand(token
, NULL
, localeconv())) <= 0.0)
1453 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1460 if (Output
== _CUPS_OUTPUT_TEST
)
1461 printf(" [%g second delay]\n", delay
);
1463 usleep((int)(1000000.0 * delay
));
1466 else if (!_cups_strcasecmp(token
, "ATTR"))
1472 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1474 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1479 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1481 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1487 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1489 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1494 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1496 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1501 expand_variables(vars
, token
, temp
, sizeof(token
));
1505 case IPP_TAG_BOOLEAN
:
1506 if (!_cups_strcasecmp(token
, "true"))
1507 ippAddBoolean(request
, group
, attr
, 1);
1509 ippAddBoolean(request
, group
, attr
, atoi(token
));
1512 case IPP_TAG_INTEGER
:
1514 if (!strchr(token
, ','))
1515 ippAddInteger(request
, group
, value
, attr
,
1516 strtol(token
, &tokenptr
, 0));
1519 int values
[100], /* Values */
1520 num_values
= 1; /* Number of values */
1522 values
[0] = strtol(token
, &tokenptr
, 10);
1523 while (tokenptr
&& *tokenptr
&&
1524 num_values
< (int)(sizeof(values
) / sizeof(values
[0])))
1526 if (*tokenptr
== ',')
1528 else if (!isdigit(*tokenptr
& 255) && *tokenptr
!= '-')
1531 values
[num_values
] = strtol(tokenptr
, &tokenptr
, 0);
1535 ippAddIntegers(request
, group
, value
, attr
, num_values
, values
);
1538 if (!tokenptr
|| *tokenptr
)
1540 print_fatal_error("Bad %s value \"%s\" on line %d.",
1541 ippTagString(value
), token
, linenum
);
1547 case IPP_TAG_RESOLUTION
:
1549 int xres
, /* X resolution */
1550 yres
; /* Y resolution */
1551 char *ptr
; /* Pointer into value */
1553 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1554 if (ptr
> token
&& xres
> 0)
1557 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1560 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1561 (_cups_strcasecmp(ptr
, "dpi") && _cups_strcasecmp(ptr
, "dpc") &&
1562 _cups_strcasecmp(ptr
, "other")))
1564 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1570 if (!_cups_strcasecmp(ptr
, "dpi"))
1571 ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1573 else if (!_cups_strcasecmp(ptr
, "dpc"))
1574 ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1577 ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1582 case IPP_TAG_RANGE
:
1584 int lowers
[4], /* Lower value */
1585 uppers
[4], /* Upper values */
1586 num_vals
; /* Number of values */
1589 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1590 lowers
+ 0, uppers
+ 0,
1591 lowers
+ 1, uppers
+ 1,
1592 lowers
+ 2, uppers
+ 2,
1593 lowers
+ 3, uppers
+ 3);
1595 if ((num_vals
& 1) || num_vals
== 0)
1597 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1598 "%d.", token
, linenum
);
1603 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1608 case IPP_TAG_BEGIN_COLLECTION
:
1609 if (!strcmp(token
, "{"))
1611 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1612 /* Collection value */
1616 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1627 print_fatal_error("Bad ATTR collection value on line %d.",
1635 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1636 ippTagString(value
), linenum
);
1640 case IPP_TAG_TEXTLANG
:
1641 case IPP_TAG_NAMELANG
:
1644 case IPP_TAG_KEYWORD
:
1646 case IPP_TAG_URISCHEME
:
1647 case IPP_TAG_CHARSET
:
1648 case IPP_TAG_LANGUAGE
:
1649 case IPP_TAG_MIMETYPE
:
1650 if (!strchr(token
, ','))
1651 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1655 * Multiple string values...
1658 int num_values
; /* Number of values */
1659 char *values
[100], /* Values */
1660 *ptr
; /* Pointer to next value */
1666 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1669 values
[num_values
] = ptr
;
1673 ippAddStrings(request
, group
, value
, attr
, num_values
,
1674 NULL
, (const char **)values
);
1679 else if (!_cups_strcasecmp(token
, "FILE"))
1685 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1687 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1692 expand_variables(vars
, token
, temp
, sizeof(token
));
1693 get_filename(testfile
, filename
, token
, sizeof(filename
));
1695 else if (!_cups_strcasecmp(token
, "STATUS"))
1701 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1703 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1708 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1710 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1715 if ((statuses
[num_statuses
].status
= ippErrorValue(token
))
1716 == (ipp_status_t
)-1 &&
1717 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1719 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1725 last_status
= statuses
+ num_statuses
;
1728 last_status
->if_defined
= NULL
;
1729 last_status
->if_not_defined
= NULL
;
1730 last_status
->repeat_match
= 0;
1731 last_status
->repeat_no_match
= 0;
1733 else if (!_cups_strcasecmp(token
, "EXPECT"))
1736 * Expected attributes...
1739 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1741 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1746 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1748 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1753 last_expect
= expects
+ num_expects
;
1756 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1758 if (token
[0] == '!')
1760 last_expect
->not_expect
= 1;
1761 last_expect
->name
= strdup(token
+ 1);
1763 else if (token
[0] == '?')
1765 last_expect
->optional
= 1;
1766 last_expect
->name
= strdup(token
+ 1);
1769 last_expect
->name
= strdup(token
);
1771 else if (!_cups_strcasecmp(token
, "COUNT"))
1773 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1775 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1780 if ((i
= atoi(token
)) <= 0)
1782 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1788 last_expect
->count
= i
;
1791 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1797 else if (!_cups_strcasecmp(token
, "DEFINE-MATCH"))
1799 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1801 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1808 last_expect
->define_match
= strdup(token
);
1811 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1817 else if (!_cups_strcasecmp(token
, "DEFINE-NO-MATCH"))
1819 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1821 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1828 last_expect
->define_no_match
= strdup(token
);
1831 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1832 "line %d.", linenum
);
1837 else if (!_cups_strcasecmp(token
, "DEFINE-VALUE"))
1839 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1841 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1848 last_expect
->define_value
= strdup(token
);
1851 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1857 else if (!_cups_strcasecmp(token
, "OF-TYPE"))
1859 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1861 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1868 last_expect
->of_type
= strdup(token
);
1871 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1877 else if (!_cups_strcasecmp(token
, "IN-GROUP"))
1879 ipp_tag_t in_group
; /* IN-GROUP value */
1882 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1884 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1889 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1892 else if (last_expect
)
1893 last_expect
->in_group
= in_group
;
1896 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1902 else if (!_cups_strcasecmp(token
, "REPEAT-MATCH"))
1905 last_status
->repeat_match
= 1;
1906 else if (last_expect
)
1907 last_expect
->repeat_match
= 1;
1910 print_fatal_error("REPEAT-MATCH without a preceding EXPECT or STATUS "
1911 "on line %d.", linenum
);
1916 else if (!_cups_strcasecmp(token
, "REPEAT-NO-MATCH"))
1919 last_status
->repeat_no_match
= 1;
1920 else if (last_expect
)
1921 last_expect
->repeat_no_match
= 1;
1924 print_fatal_error("REPEAT-NO-MATCH without a preceding EXPECT or "
1925 "STATUS on ine %d.", linenum
);
1930 else if (!_cups_strcasecmp(token
, "SAME-COUNT-AS"))
1932 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1934 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1940 last_expect
->same_count_as
= strdup(token
);
1943 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1949 else if (!_cups_strcasecmp(token
, "IF-DEFINED"))
1951 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1953 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1959 last_expect
->if_defined
= strdup(token
);
1960 else if (last_status
)
1961 last_status
->if_defined
= strdup(token
);
1964 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1965 "on line %d.", linenum
);
1970 else if (!_cups_strcasecmp(token
, "IF-NOT-DEFINED"))
1972 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1974 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
1980 last_expect
->if_not_defined
= strdup(token
);
1981 else if (last_status
)
1982 last_status
->if_not_defined
= strdup(token
);
1985 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1986 "on line %d.", linenum
);
1991 else if (!_cups_strcasecmp(token
, "WITH-VALUE"))
1993 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1995 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
2003 * Expand any variables in the value and then save it.
2006 expand_variables(vars
, token
, temp
, sizeof(token
));
2008 tokenptr
= token
+ strlen(token
) - 1;
2010 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
2013 * WITH-VALUE is a POSIX extended regular expression.
2016 last_expect
->with_value
= calloc(1, tokenptr
- token
);
2017 last_expect
->with_regex
= 1;
2019 if (last_expect
->with_value
)
2020 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
2025 * WITH-VALUE is a literal value...
2028 last_expect
->with_value
= strdup(token
);
2033 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
2039 else if (!_cups_strcasecmp(token
, "DISPLAY"))
2042 * Display attributes...
2045 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
2047 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
2052 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
2054 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
2059 displayed
[num_displayed
] = strdup(token
);
2064 print_fatal_error("Unexpected token %s seen on line %d.", token
,
2072 * Submit the IPP request...
2077 request
->request
.op
.version
[0] = version
/ 10;
2078 request
->request
.op
.version
[1] = version
% 10;
2079 request
->request
.op
.operation_id
= op
;
2080 request
->request
.op
.request_id
= request_id
;
2082 if (Output
== _CUPS_OUTPUT_PLIST
)
2085 puts("<key>Name</key>");
2086 print_xml_string("string", name
);
2087 puts("<key>Operation</key>");
2088 print_xml_string("string", ippOpString(op
));
2089 puts("<key>RequestAttributes</key>");
2094 for (attrptr
= request
->attrs
, group
= attrptr
->group_tag
;
2096 attrptr
= attrptr
->next
)
2097 print_attr(attrptr
, &group
);
2102 else if (Output
== _CUPS_OUTPUT_TEST
)
2106 printf(" %s:\n", ippOpString(op
));
2108 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
2109 print_attr(attrptr
, NULL
);
2112 printf(" %-68.68s [", name
);
2116 if ((skip_previous
&& !prev_pass
) || skip_test
)
2123 if (Output
== _CUPS_OUTPUT_PLIST
)
2125 puts("<key>Successful</key>");
2127 puts("<key>StatusCode</key>");
2128 print_xml_string("string", "skip");
2129 puts("<key>ResponseAttributes</key>");
2133 else if (Output
== _CUPS_OUTPUT_TEST
)
2143 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
2144 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
2147 * Send request using chunking - a 0 length means "chunk".
2155 * Send request using content length...
2158 length
= ippLength(request
);
2160 if (filename
[0] && (reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2163 * Read the file to get the uncompressed file size...
2166 while ((bytes
= cupsFileRead(reqfile
, buffer
, sizeof(buffer
))) > 0)
2169 cupsFileClose(reqfile
);
2174 * Send the request...
2181 if (status
!= HTTP_ERROR
)
2183 while (!response
&& !Cancel
&& prev_pass
)
2185 status
= cupsSendRequest(http
, request
, resource
, length
);
2187 if (!Cancel
&& status
== HTTP_CONTINUE
&&
2188 request
->state
== IPP_DATA
&& filename
[0])
2190 if ((reqfile
= cupsFileOpen(filename
, "r")) != NULL
)
2193 (bytes
= cupsFileRead(reqfile
, buffer
,
2194 sizeof(buffer
))) > 0)
2195 if ((status
= cupsWriteRequestData(http
, buffer
,
2196 bytes
)) != HTTP_CONTINUE
)
2199 cupsFileClose(reqfile
);
2203 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
,
2205 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
2207 status
= HTTP_ERROR
;
2212 * Get the server's response...
2215 if (!Cancel
&& status
!= HTTP_ERROR
)
2217 response
= cupsGetResponse(http
, resource
);
2218 status
= httpGetStatus(http
);
2221 if (!Cancel
&& status
== HTTP_ERROR
&&
2223 http
->error
!= WSAETIMEDOUT
)
2225 http
->error
!= ETIMEDOUT
)
2228 if (httpReconnect(http
))
2231 else if (status
== HTTP_ERROR
)
2234 httpReconnect(http
);
2242 * Check results of request...
2246 prev_pass
= pass
= 0;
2249 if (http
->version
!= HTTP_1_1
)
2250 prev_pass
= pass
= 0;
2252 if (response
->state
!= IPP_DATA
)
2253 prev_pass
= pass
= 0;
2255 if (response
->request
.status
.request_id
!= request_id
)
2256 prev_pass
= pass
= 0;
2259 (response
->request
.status
.version
[0] != (version
/ 10) ||
2260 response
->request
.status
.version
[1] != (version
% 10)))
2261 prev_pass
= pass
= 0;
2263 if ((attrptr
= ippFindAttribute(response
, "job-id",
2264 IPP_TAG_INTEGER
)) != NULL
)
2266 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2267 set_variable(vars
, "job-id", temp
);
2270 if ((attrptr
= ippFindAttribute(response
, "job-uri",
2271 IPP_TAG_URI
)) != NULL
)
2272 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
2274 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
2275 IPP_TAG_INTEGER
)) != NULL
)
2277 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
2278 set_variable(vars
, "notify-subscription-id", temp
);
2281 attrptr
= response
->attrs
;
2282 if (!attrptr
|| !attrptr
->name
||
2283 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2284 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2285 attrptr
->num_values
!= 1 ||
2286 strcmp(attrptr
->name
, "attributes-charset"))
2287 prev_pass
= pass
= 0;
2291 attrptr
= attrptr
->next
;
2292 if (!attrptr
|| !attrptr
->name
||
2293 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2294 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2295 attrptr
->num_values
!= 1 ||
2296 strcmp(attrptr
->name
, "attributes-natural-language"))
2297 prev_pass
= pass
= 0;
2300 if ((attrptr
= ippFindAttribute(response
, "status-message",
2301 IPP_TAG_ZERO
)) != NULL
&&
2302 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2303 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2304 attrptr
->num_values
!= 1 ||
2305 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2306 strlen(attrptr
->values
[0].string
.text
) > 255)))
2307 prev_pass
= pass
= 0;
2309 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2310 IPP_TAG_ZERO
)) != NULL
&&
2311 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
2312 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2313 attrptr
->num_values
!= 1 ||
2314 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2315 strlen(attrptr
->values
[0].string
.text
) > 1023)))
2316 prev_pass
= pass
= 0;
2318 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2320 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2322 attrptr
= attrptr
->next
)
2324 if (attrptr
->group_tag
!= group
)
2328 switch (attrptr
->group_tag
)
2330 case IPP_TAG_OPERATION
:
2331 prev_pass
= pass
= 0;
2334 case IPP_TAG_UNSUPPORTED_GROUP
:
2335 if (group
!= IPP_TAG_OPERATION
)
2336 prev_pass
= pass
= 0;
2340 case IPP_TAG_PRINTER
:
2341 if (group
!= IPP_TAG_OPERATION
&&
2342 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2343 prev_pass
= pass
= 0;
2346 case IPP_TAG_SUBSCRIPTION
:
2347 if (group
> attrptr
->group_tag
&&
2348 group
!= IPP_TAG_DOCUMENT
)
2349 prev_pass
= pass
= 0;
2353 if (group
> attrptr
->group_tag
)
2354 prev_pass
= pass
= 0;
2361 group
= attrptr
->group_tag
;
2364 if (!validate_attr(attrptr
, 0))
2366 prev_pass
= pass
= 0;
2372 if (cupsArrayFind(a
, attrptr
->name
))
2374 prev_pass
= pass
= 0;
2378 cupsArrayAdd(a
, attrptr
->name
);
2384 for (i
= 0; i
< num_statuses
; i
++)
2386 if (statuses
[i
].if_defined
&&
2387 !get_variable(vars
, statuses
[i
].if_defined
))
2390 if (statuses
[i
].if_not_defined
&&
2391 get_variable(vars
, statuses
[i
].if_not_defined
))
2394 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2396 if (statuses
[i
].repeat_match
)
2401 else if (statuses
[i
].repeat_no_match
)
2405 if (i
== num_statuses
&& num_statuses
> 0)
2406 prev_pass
= pass
= 0;
2408 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2410 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2413 if (expect
->if_not_defined
&&
2414 get_variable(vars
, expect
->if_not_defined
))
2417 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2419 if ((found
&& expect
->not_expect
) ||
2420 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2421 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2422 (found
&& expect
->in_group
&&
2423 found
->group_tag
!= expect
->in_group
))
2425 if (expect
->define_no_match
)
2426 set_variable(vars
, expect
->define_no_match
, "1");
2427 else if (!expect
->define_match
&& !expect
->define_value
)
2428 prev_pass
= pass
= 0;
2430 if (expect
->repeat_no_match
)
2437 _ippAttrString(found
, buffer
, sizeof(buffer
));
2440 !with_value(expect
->with_value
, expect
->with_regex
, found
, 0,
2441 buffer
, sizeof(buffer
)))
2443 if (expect
->define_no_match
)
2444 set_variable(vars
, expect
->define_no_match
, "1");
2445 else if (!expect
->define_match
&& !expect
->define_value
)
2446 prev_pass
= pass
= 0;
2448 if (expect
->repeat_no_match
)
2454 if (found
&& expect
->count
> 0 &&
2455 found
->num_values
!= expect
->count
)
2457 if (expect
->define_no_match
)
2458 set_variable(vars
, expect
->define_no_match
, "1");
2459 else if (!expect
->define_match
&& !expect
->define_value
)
2460 prev_pass
= pass
= 0;
2462 if (expect
->repeat_no_match
)
2468 if (found
&& expect
->same_count_as
)
2470 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2473 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2475 if (expect
->define_no_match
)
2476 set_variable(vars
, expect
->define_no_match
, "1");
2477 else if (!expect
->define_match
&& !expect
->define_value
)
2478 prev_pass
= pass
= 0;
2480 if (expect
->repeat_no_match
)
2487 if (found
&& expect
->define_match
)
2488 set_variable(vars
, expect
->define_match
, "1");
2490 if (found
&& expect
->define_value
)
2491 set_variable(vars
, expect
->define_value
, buffer
);
2493 if (found
&& expect
->repeat_match
)
2499 * If we are going to repeat this test, sleep 1 second so we don't flood
2500 * the printer with requests...
2506 while (repeat_test
);
2517 if (Output
== _CUPS_OUTPUT_PLIST
)
2519 puts("<key>Successful</key>");
2520 puts(prev_pass
? "<true />" : "<false />");
2521 puts("<key>StatusCode</key>");
2522 print_xml_string("string", ippErrorString(cupsLastError()));
2523 puts("<key>ResponseAttributes</key>");
2526 for (attrptr
= response
? response
->attrs
: NULL
,
2527 group
= attrptr
? attrptr
->group_tag
: IPP_TAG_ZERO
;
2529 attrptr
= attrptr
->next
)
2530 print_attr(attrptr
, &group
);
2534 else if (Output
== _CUPS_OUTPUT_TEST
)
2536 puts(prev_pass
? "PASS]" : "FAIL]");
2538 if (Verbosity
&& response
)
2540 printf(" RECEIVED: %lu bytes in response\n",
2541 (unsigned long)ippLength(response
));
2542 printf(" status-code = %x (%s)\n", cupsLastError(),
2543 ippErrorString(cupsLastError()));
2545 for (attrptr
= response
->attrs
;
2547 attrptr
= attrptr
->next
)
2549 print_attr(attrptr
, NULL
);
2553 else if (!prev_pass
)
2554 fprintf(stderr
, "%s\n", cupsLastErrorString());
2556 if (prev_pass
&& Output
>= _CUPS_OUTPUT_LIST
&& !Verbosity
&&
2559 size_t width
; /* Length of value */
2561 for (i
= 0; i
< num_displayed
; i
++)
2563 widths
[i
] = strlen(displayed
[i
]);
2565 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2567 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2570 width
= _ippAttrString(attrptr
, NULL
, 0);
2571 if (width
> widths
[i
])
2576 if (Output
== _CUPS_OUTPUT_CSV
)
2577 print_csv(NULL
, num_displayed
, displayed
, widths
);
2579 print_line(NULL
, num_displayed
, displayed
, widths
);
2581 attrptr
= response
->attrs
;
2585 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2586 attrptr
= attrptr
->next
;
2590 if (Output
== _CUPS_OUTPUT_CSV
)
2591 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2593 print_line(attrptr
, num_displayed
, displayed
, widths
);
2595 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2596 attrptr
= attrptr
->next
;
2600 else if (!prev_pass
)
2602 if (Output
== _CUPS_OUTPUT_PLIST
)
2604 puts("<key>Errors</key>");
2608 if (http
->version
!= HTTP_1_1
)
2609 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
2610 http
->version
% 100);
2613 print_test_error("IPP request failed with status %s (%s)",
2614 ippErrorString(cupsLastError()),
2615 cupsLastErrorString());
2618 if (response
->state
!= IPP_DATA
)
2619 print_test_error("Missing end-of-attributes-tag in response "
2620 "(RFC 2910 section 3.5.1)");
2623 (response
->request
.status
.version
[0] != (version
/ 10) ||
2624 response
->request
.status
.version
[1] != (version
% 10)))
2625 print_test_error("Bad version %d.%d in response - expected %d.%d "
2626 "(RFC 2911 section 3.1.8).",
2627 response
->request
.status
.version
[0],
2628 response
->request
.status
.version
[1],
2629 version
/ 10, version
% 10);
2631 if (response
->request
.status
.request_id
!= request_id
)
2632 print_test_error("Bad request ID %d in response - expected %d "
2633 "(RFC 2911 section 3.1.1)",
2634 response
->request
.status
.request_id
, request_id
);
2636 attrptr
= response
->attrs
;
2638 print_test_error("Missing first attribute \"attributes-charset "
2639 "(charset)\" in group operation-attributes-tag "
2640 "(RFC 2911 section 3.1.4).");
2643 if (!attrptr
->name
||
2644 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2645 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2646 attrptr
->num_values
!= 1 ||
2647 strcmp(attrptr
->name
, "attributes-charset"))
2648 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2649 "expected \"attributes-charset (charset)\" in "
2650 "group operation-attributes-tag (RFC 2911 section "
2652 attrptr
->name
? attrptr
->name
: "(null)",
2653 attrptr
->num_values
> 1 ? "1setOf " : "",
2654 ippTagString(attrptr
->value_tag
),
2655 ippTagString(attrptr
->group_tag
));
2657 attrptr
= attrptr
->next
;
2659 print_test_error("Missing second attribute \"attributes-natural-"
2660 "language (naturalLanguage)\" in group "
2661 "operation-attributes-tag (RFC 2911 section "
2663 else if (!attrptr
->name
||
2664 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2665 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2666 attrptr
->num_values
!= 1 ||
2667 strcmp(attrptr
->name
, "attributes-natural-language"))
2668 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2669 "expected \"attributes-natural-language "
2670 "(naturalLanguage)\" in group "
2671 "operation-attributes-tag (RFC 2911 section "
2673 attrptr
->name
? attrptr
->name
: "(null)",
2674 attrptr
->num_values
> 1 ? "1setOf " : "",
2675 ippTagString(attrptr
->value_tag
),
2676 ippTagString(attrptr
->group_tag
));
2679 if ((attrptr
= ippFindAttribute(response
, "status-message",
2680 IPP_TAG_ZERO
)) != NULL
)
2682 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2683 print_test_error("status-message (text(255)) has wrong value tag "
2684 "%s (RFC 2911 section 3.1.6.2).",
2685 ippTagString(attrptr
->value_tag
));
2686 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2687 print_test_error("status-message (text(255)) has wrong group tag "
2688 "%s (RFC 2911 section 3.1.6.2).",
2689 ippTagString(attrptr
->group_tag
));
2690 if (attrptr
->num_values
!= 1)
2691 print_test_error("status-message (text(255)) has %d values "
2692 "(RFC 2911 section 3.1.6.2).",
2693 attrptr
->num_values
);
2694 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2695 strlen(attrptr
->values
[0].string
.text
) > 255)
2696 print_test_error("status-message (text(255)) has bad length %d"
2697 " (RFC 2911 section 3.1.6.2).",
2698 (int)strlen(attrptr
->values
[0].string
.text
));
2701 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2702 IPP_TAG_ZERO
)) != NULL
)
2704 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2705 print_test_error("detailed-status-message (text(MAX)) has wrong "
2706 "value tag %s (RFC 2911 section 3.1.6.3).",
2707 ippTagString(attrptr
->value_tag
));
2708 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2709 print_test_error("detailed-status-message (text(MAX)) has wrong "
2710 "group tag %s (RFC 2911 section 3.1.6.3).",
2711 ippTagString(attrptr
->group_tag
));
2712 if (attrptr
->num_values
!= 1)
2713 print_test_error("detailed-status-message (text(MAX)) has %d values"
2714 " (RFC 2911 section 3.1.6.3).",
2715 attrptr
->num_values
);
2716 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2717 strlen(attrptr
->values
[0].string
.text
) > 1023)
2718 print_test_error("detailed-status-message (text(MAX)) has bad "
2719 "length %d (RFC 2911 section 3.1.6.3).",
2720 (int)strlen(attrptr
->values
[0].string
.text
));
2723 a
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
2725 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2727 attrptr
= attrptr
->next
)
2729 if (attrptr
->group_tag
!= group
)
2733 switch (attrptr
->group_tag
)
2735 case IPP_TAG_OPERATION
:
2736 prev_pass
= pass
= 0;
2739 case IPP_TAG_UNSUPPORTED_GROUP
:
2740 if (group
!= IPP_TAG_OPERATION
)
2741 print_test_error("Attribute groups out of order (%s < %s)",
2742 ippTagString(attrptr
->group_tag
),
2743 ippTagString(group
));
2747 case IPP_TAG_PRINTER
:
2748 if (group
!= IPP_TAG_OPERATION
&&
2749 group
!= IPP_TAG_UNSUPPORTED_GROUP
)
2750 print_test_error("Attribute groups out of order (%s < %s)",
2751 ippTagString(attrptr
->group_tag
),
2752 ippTagString(group
));
2755 case IPP_TAG_SUBSCRIPTION
:
2756 if (group
> attrptr
->group_tag
&&
2757 group
!= IPP_TAG_DOCUMENT
)
2758 print_test_error("Attribute groups out of order (%s < %s)",
2759 ippTagString(attrptr
->group_tag
),
2760 ippTagString(group
));
2764 if (group
> attrptr
->group_tag
)
2765 print_test_error("Attribute groups out of order (%s < %s)",
2766 ippTagString(attrptr
->group_tag
),
2767 ippTagString(group
));
2771 group
= attrptr
->group_tag
;
2774 validate_attr(attrptr
, 1);
2778 if (cupsArrayFind(a
, attrptr
->name
))
2779 print_test_error("Duplicate \"%s\" attribute in %s group",
2780 attrptr
->name
, ippTagString(group
));
2782 cupsArrayAdd(a
, attrptr
->name
);
2788 for (i
= 0; i
< num_statuses
; i
++)
2790 if (statuses
[i
].if_defined
&&
2791 !get_variable(vars
, statuses
[i
].if_defined
))
2794 if (statuses
[i
].if_not_defined
&&
2795 get_variable(vars
, statuses
[i
].if_not_defined
))
2798 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2802 if (i
== num_statuses
&& num_statuses
> 0)
2804 for (i
= 0; i
< num_statuses
; i
++)
2806 if (statuses
[i
].if_defined
&&
2807 !get_variable(vars
, statuses
[i
].if_defined
))
2810 if (statuses
[i
].if_not_defined
&&
2811 get_variable(vars
, statuses
[i
].if_not_defined
))
2814 print_test_error("EXPECTED: STATUS %s (got %s)",
2815 ippErrorString(statuses
[i
].status
),
2816 ippErrorString(cupsLastError()));
2819 if ((attrptr
= ippFindAttribute(response
, "status-message",
2820 IPP_TAG_TEXT
)) != NULL
)
2821 print_test_error("status-message=\"%s\"",
2822 attrptr
->values
[0].string
.text
);
2825 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2827 if (expect
->define_match
|| expect
->define_no_match
||
2828 expect
->define_value
)
2831 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2834 if (expect
->if_not_defined
&&
2835 get_variable(vars
, expect
->if_not_defined
))
2838 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2840 if (found
&& expect
->not_expect
)
2841 print_test_error("NOT EXPECTED: %s", expect
->name
);
2842 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2843 print_test_error("EXPECTED: %s", expect
->name
);
2846 if (!expect_matches(expect
, found
->value_tag
))
2847 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2848 expect
->name
, expect
->of_type
,
2849 ippTagString(found
->value_tag
));
2851 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2852 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2853 expect
->name
, ippTagString(expect
->in_group
),
2854 ippTagString(found
->group_tag
));
2856 if (!with_value(expect
->with_value
, expect
->with_regex
, found
, 0,
2857 buffer
, sizeof(buffer
)))
2859 if (expect
->with_regex
)
2860 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2861 expect
->name
, expect
->with_value
);
2863 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2864 expect
->name
, expect
->with_value
);
2866 with_value(expect
->with_value
, expect
->with_regex
, found
, 1,
2867 buffer
, sizeof(buffer
));
2870 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
2872 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2873 expect
->count
, found
->num_values
);
2876 if (expect
->same_count_as
)
2878 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2882 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2883 "(not returned)", expect
->name
,
2884 found
->num_values
, expect
->same_count_as
);
2885 else if (attrptr
->num_values
!= found
->num_values
)
2886 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2887 "(%d values)", expect
->name
, found
->num_values
,
2888 expect
->same_count_as
, attrptr
->num_values
);
2894 if (Output
== _CUPS_OUTPUT_PLIST
)
2898 if (num_displayed
> 0 && !Verbosity
&&
2899 (Output
== _CUPS_OUTPUT_TEST
|| Output
== _CUPS_OUTPUT_PLIST
))
2901 for (attrptr
= response
->attrs
;
2903 attrptr
= attrptr
->next
)
2907 for (i
= 0; i
< num_displayed
; i
++)
2909 if (!strcmp(displayed
[i
], attrptr
->name
))
2911 print_attr(attrptr
, NULL
);
2921 if (Output
== _CUPS_OUTPUT_PLIST
)
2926 ippDelete(response
);
2929 for (i
= 0; i
< num_statuses
; i
++)
2931 if (statuses
[i
].if_defined
)
2932 free(statuses
[i
].if_defined
);
2933 if (statuses
[i
].if_not_defined
)
2934 free(statuses
[i
].if_not_defined
);
2938 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2941 if (expect
->of_type
)
2942 free(expect
->of_type
);
2943 if (expect
->same_count_as
)
2944 free(expect
->same_count_as
);
2945 if (expect
->if_defined
)
2946 free(expect
->if_defined
);
2947 if (expect
->if_not_defined
)
2948 free(expect
->if_not_defined
);
2949 if (expect
->with_value
)
2950 free(expect
->with_value
);
2951 if (expect
->define_match
)
2952 free(expect
->define_match
);
2953 if (expect
->define_no_match
)
2954 free(expect
->define_no_match
);
2955 if (expect
->define_value
)
2956 free(expect
->define_value
);
2960 for (i
= 0; i
< num_displayed
; i
++)
2964 if (!ignore_errors
&& !prev_pass
)
2975 ippDelete(response
);
2977 for (i
= 0; i
< num_statuses
; i
++)
2979 if (statuses
[i
].if_defined
)
2980 free(statuses
[i
].if_defined
);
2981 if (statuses
[i
].if_not_defined
)
2982 free(statuses
[i
].if_not_defined
);
2985 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2988 if (expect
->of_type
)
2989 free(expect
->of_type
);
2990 if (expect
->same_count_as
)
2991 free(expect
->same_count_as
);
2992 if (expect
->if_defined
)
2993 free(expect
->if_defined
);
2994 if (expect
->if_not_defined
)
2995 free(expect
->if_not_defined
);
2996 if (expect
->with_value
)
2997 free(expect
->with_value
);
2998 if (expect
->define_match
)
2999 free(expect
->define_match
);
3000 if (expect
->define_no_match
)
3001 free(expect
->define_no_match
);
3002 if (expect
->define_value
)
3003 free(expect
->define_value
);
3006 for (i
= 0; i
< num_displayed
; i
++)
3014 * 'expand_variables()' - Expand variables in a string.
3018 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
3019 char *dst
, /* I - Destination string buffer */
3020 const char *src
, /* I - Source string */
3021 size_t dstsize
) /* I - Size of destination buffer */
3023 char *dstptr
, /* Pointer into destination */
3024 *dstend
, /* End of destination */
3025 temp
[256], /* Temporary string */
3026 *tempptr
; /* Pointer into temporary string */
3027 const char *value
; /* Value to substitute */
3031 dstend
= dst
+ dstsize
- 1;
3033 while (*src
&& dstptr
< dstend
)
3038 * Substitute a string/number...
3041 if (!strncmp(src
, "$$", 2))
3046 else if (!strncmp(src
, "$ENV[", 5))
3048 strlcpy(temp
, src
+ 5, sizeof(temp
));
3050 for (tempptr
= temp
; *tempptr
; tempptr
++)
3051 if (*tempptr
== ']')
3057 value
= getenv(temp
);
3058 src
+= tempptr
- temp
+ 5;
3062 strlcpy(temp
, src
+ 1, sizeof(temp
));
3064 for (tempptr
= temp
; *tempptr
; tempptr
++)
3065 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
3071 if (!strcmp(temp
, "uri"))
3073 else if (!strcmp(temp
, "filename"))
3074 value
= vars
->filename
;
3075 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
3076 value
= vars
->scheme
;
3077 else if (!strcmp(temp
, "username"))
3078 value
= vars
->userpass
;
3079 else if (!strcmp(temp
, "hostname"))
3080 value
= vars
->hostname
;
3081 else if (!strcmp(temp
, "port"))
3083 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
3086 else if (!strcmp(temp
, "resource"))
3087 value
= vars
->resource
;
3088 else if (!strcmp(temp
, "user"))
3091 value
= get_variable(vars
, temp
);
3093 src
+= tempptr
- temp
+ 1;
3103 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
3104 dstptr
+= strlen(dstptr
);
3116 * 'expect_matches()' - Return true if the tag matches the specification.
3119 static int /* O - 1 if matches, 0 otherwise */
3121 _cups_expect_t
*expect
, /* I - Expected attribute */
3122 ipp_tag_t value_tag
) /* I - Value tag for attribute */
3124 int match
; /* Match? */
3125 char *of_type
, /* Type name to match */
3126 *next
, /* Next name to match */
3127 sep
; /* Separator character */
3131 * If we don't expect a particular type, return immediately...
3134 if (!expect
->of_type
)
3138 * Parse the "of_type" value since the string can contain multiple attribute
3139 * types separated by "," or "|"...
3142 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
3145 * Find the next separator, and set it (temporarily) to nul if present.
3148 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
3150 if ((sep
= *next
) != '\0')
3154 * Support some meta-types to make it easier to write the test file.
3157 if (!strcmp(of_type
, "text"))
3158 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
3159 else if (!strcmp(of_type
, "name"))
3160 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
3161 else if (!strcmp(of_type
, "collection"))
3162 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
3164 match
= value_tag
== ippTagValue(of_type
);
3167 * Restore the separator if we have one...
3179 * 'get_collection()' - Get a collection value from the current test file.
3182 static ipp_t
* /* O - Collection value */
3183 get_collection(_cups_vars_t
*vars
, /* I - Variables */
3184 FILE *fp
, /* I - File to read from */
3185 int *linenum
) /* IO - Line number */
3187 char token
[1024], /* Token from file */
3188 temp
[1024], /* Temporary string */
3189 attr
[128]; /* Attribute name */
3190 ipp_tag_t value
; /* Current value type */
3191 ipp_t
*col
= ippNew(); /* Collection value */
3192 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
3195 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
3197 if (!strcmp(token
, "}"))
3199 else if (!strcmp(token
, "{") && lastcol
)
3202 * Another collection value
3205 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3206 /* Collection value */
3210 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
3214 * Reallocate memory...
3217 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
3218 (lastcol
->num_values
+ 1) *
3219 sizeof(ipp_value_t
))) == NULL
)
3221 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
3225 if (tempcol
!= lastcol
)
3228 * Reset pointers in the list...
3232 col
->prev
->next
= tempcol
;
3234 col
->attrs
= tempcol
;
3236 lastcol
= col
->current
= col
->last
= tempcol
;
3239 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
3240 lastcol
->num_values
++;
3245 else if (!_cups_strcasecmp(token
, "MEMBER"))
3253 if (!get_token(fp
, token
, sizeof(token
), linenum
))
3255 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
3259 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
3261 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
3266 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
3268 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
3272 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
3274 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
3278 expand_variables(vars
, token
, temp
, sizeof(token
));
3282 case IPP_TAG_BOOLEAN
:
3283 if (!_cups_strcasecmp(token
, "true"))
3284 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
3286 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
3289 case IPP_TAG_INTEGER
:
3291 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
3294 case IPP_TAG_RESOLUTION
:
3296 int xres
, /* X resolution */
3297 yres
; /* Y resolution */
3298 char units
[6]; /* Units */
3300 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
3301 (_cups_strcasecmp(units
, "dpi") && _cups_strcasecmp(units
, "dpc") &&
3302 _cups_strcasecmp(units
, "other")))
3304 print_fatal_error("Bad resolution value \"%s\" on line %d.",
3309 if (!_cups_strcasecmp(units
, "dpi"))
3310 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3312 else if (!_cups_strcasecmp(units
, "dpc"))
3313 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3316 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
3321 case IPP_TAG_RANGE
:
3323 int lowers
[4], /* Lower value */
3324 uppers
[4], /* Upper values */
3325 num_vals
; /* Number of values */
3328 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
3329 lowers
+ 0, uppers
+ 0,
3330 lowers
+ 1, uppers
+ 1,
3331 lowers
+ 2, uppers
+ 2,
3332 lowers
+ 3, uppers
+ 3);
3334 if ((num_vals
& 1) || num_vals
== 0)
3336 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
3341 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
3346 case IPP_TAG_BEGIN_COLLECTION
:
3347 if (!strcmp(token
, "{"))
3349 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
3350 /* Collection value */
3354 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
3362 print_fatal_error("Bad collection value on line %d.", *linenum
);
3368 if (!strchr(token
, ','))
3369 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
3373 * Multiple string values...
3376 int num_values
; /* Number of values */
3377 char *values
[100], /* Values */
3378 *ptr
; /* Pointer to next value */
3384 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
3387 values
[num_values
] = ptr
;
3391 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
3392 NULL
, (const char **)values
);
3402 * If we get here there was a parse error; free memory and return.
3414 * 'get_filename()' - Get a filename based on the current test file.
3417 static char * /* O - Filename */
3418 get_filename(const char *testfile
, /* I - Current test file */
3419 char *dst
, /* I - Destination filename */
3420 const char *src
, /* I - Source filename */
3421 size_t dstsize
) /* I - Size of destination buffer */
3423 char *dstptr
; /* Pointer into destination */
3424 _cups_globals_t
*cg
= _cupsGlobals();
3428 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
3431 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3434 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
3435 dstptr
= dst
+ strlen(dst
) - 1;
3439 else if (*src
== '/' || !strchr(testfile
, '/'))
3442 * Use the path as-is...
3445 strlcpy(dst
, src
, dstsize
);
3450 * Make path relative to testfile...
3453 strlcpy(dst
, testfile
, dstsize
);
3454 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
3457 dstptr
= dst
; /* Should never happen */
3459 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
3467 * 'get_token()' - Get a token from a file.
3470 static char * /* O - Token from file or NULL on EOF */
3471 get_token(FILE *fp
, /* I - File to read from */
3472 char *buf
, /* I - Buffer to read into */
3473 int buflen
, /* I - Length of buffer */
3474 int *linenum
) /* IO - Current line number */
3476 int ch
, /* Character from file */
3477 quote
; /* Quoting character */
3478 char *bufptr
, /* Pointer into buffer */
3479 *bufend
; /* End of buffer */
3485 * Skip whitespace...
3488 while (isspace(ch
= getc(fp
)))
3500 else if (ch
== '\'' || ch
== '\"')
3503 * Quoted text or regular expression...
3508 bufend
= buf
+ buflen
- 1;
3510 while ((ch
= getc(fp
)) != EOF
)
3515 * Escape next character...
3518 if (bufptr
< bufend
)
3521 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
3524 else if (ch
== quote
)
3526 else if (bufptr
< bufend
)
3540 while ((ch
= getc(fp
)) != EOF
)
3549 * Whitespace delimited text...
3555 bufend
= buf
+ buflen
- 1;
3557 while ((ch
= getc(fp
)) != EOF
)
3558 if (isspace(ch
) || ch
== '#')
3560 else if (bufptr
< bufend
)
3565 else if (ch
== '\n')
3577 * 'get_variable()' - Get the value of a variable.
3580 static char * /* O - Value or NULL */
3581 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3582 const char *name
) /* I - Variable name */
3584 _cups_var_t key
, /* Search key */
3585 *match
; /* Matching variable, if any */
3588 key
.name
= (char *)name
;
3589 match
= cupsArrayFind(vars
->vars
, &key
);
3591 return (match
? match
->value
: NULL
);
3596 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3600 static char * /* O - ISO 8601 date/time string */
3601 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3603 time_t utctime
; /* UTC time since 1970 */
3604 struct tm
*utcdate
; /* UTC date/time */
3605 static char buffer
[255]; /* String buffer */
3608 utctime
= ippDateToTime(date
);
3609 utcdate
= gmtime(&utctime
);
3611 snprintf(buffer
, sizeof(buffer
), "%04d-%02d-%02dT%02d:%02d:%02dZ",
3612 utcdate
->tm_year
+ 1900, utcdate
->tm_mon
+ 1, utcdate
->tm_mday
,
3613 utcdate
->tm_hour
, utcdate
->tm_min
, utcdate
->tm_sec
);
3620 * 'password_cb()' - Password callback for authenticated tests.
3623 static const char * /* O - Password */
3624 password_cb(const char *prompt
) /* I - Prompt (unused) */
3633 * 'print_attr()' - Print an attribute on the screen.
3637 print_attr(ipp_attribute_t
*attr
, /* I - Attribute to print */
3638 ipp_tag_t
*group
) /* IO - Current group */
3640 int i
; /* Looping var */
3641 ipp_attribute_t
*colattr
; /* Collection attribute */
3644 if (Output
== _CUPS_OUTPUT_PLIST
)
3646 if (!attr
->name
|| (group
&& *group
!= attr
->group_tag
))
3652 *group
= attr
->group_tag
;
3658 print_xml_string("key", attr
->name
);
3659 if (attr
->num_values
> 1)
3662 switch (attr
->value_tag
)
3664 case IPP_TAG_INTEGER
:
3666 for (i
= 0; i
< attr
->num_values
; i
++)
3667 if (Output
== _CUPS_OUTPUT_PLIST
)
3668 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3670 printf("%d ", attr
->values
[i
].integer
);
3673 case IPP_TAG_BOOLEAN
:
3674 for (i
= 0; i
< attr
->num_values
; i
++)
3675 if (Output
== _CUPS_OUTPUT_PLIST
)
3676 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3677 else if (attr
->values
[i
].boolean
)
3678 fputs("true ", stdout
);
3680 fputs("false ", stdout
);
3683 case IPP_TAG_RANGE
:
3684 for (i
= 0; i
< attr
->num_values
; i
++)
3685 if (Output
== _CUPS_OUTPUT_PLIST
)
3686 printf("<dict><key>lower</key><integer>%d</integer>"
3687 "<key>upper</key><integer>%d</integer></dict>\n",
3688 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3690 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3691 attr
->values
[i
].range
.upper
);
3694 case IPP_TAG_RESOLUTION
:
3695 for (i
= 0; i
< attr
->num_values
; i
++)
3696 if (Output
== _CUPS_OUTPUT_PLIST
)
3697 printf("<dict><key>xres</key><integer>%d</integer>"
3698 "<key>yres</key><integer>%d</integer>"
3699 "<key>units</key><string>%s</string></dict>\n",
3700 attr
->values
[i
].resolution
.xres
,
3701 attr
->values
[i
].resolution
.yres
,
3702 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3705 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3706 attr
->values
[i
].resolution
.yres
,
3707 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3712 for (i
= 0; i
< attr
->num_values
; i
++)
3713 if (Output
== _CUPS_OUTPUT_PLIST
)
3714 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3716 printf("%s ", iso_date(attr
->values
[i
].date
));
3719 case IPP_TAG_STRING
:
3722 case IPP_TAG_KEYWORD
:
3723 case IPP_TAG_CHARSET
:
3725 case IPP_TAG_MIMETYPE
:
3726 case IPP_TAG_LANGUAGE
:
3727 for (i
= 0; i
< attr
->num_values
; i
++)
3728 if (Output
== _CUPS_OUTPUT_PLIST
)
3729 print_xml_string("string", attr
->values
[i
].string
.text
);
3731 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3734 case IPP_TAG_TEXTLANG
:
3735 case IPP_TAG_NAMELANG
:
3736 for (i
= 0; i
< attr
->num_values
; i
++)
3737 if (Output
== _CUPS_OUTPUT_PLIST
)
3739 fputs("<dict><key>language</key><string>", stdout
);
3740 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
3741 fputs("</string><key>string</key><string>", stdout
);
3742 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3743 puts("</string></dict>");
3746 printf("\"%s\"(%s) ", attr
->values
[i
].string
.text
,
3747 attr
->values
[i
].string
.charset
);
3750 case IPP_TAG_BEGIN_COLLECTION
:
3751 for (i
= 0; i
< attr
->num_values
; i
++)
3753 if (Output
== _CUPS_OUTPUT_PLIST
)
3756 for (colattr
= attr
->values
[i
].collection
->attrs
;
3758 colattr
= colattr
->next
)
3759 print_attr(colattr
, NULL
);
3767 print_col(attr
->values
[i
].collection
);
3773 if (Output
== _CUPS_OUTPUT_PLIST
)
3774 printf("<string><<%s>></string>\n",
3775 ippTagString(attr
->value_tag
));
3777 fputs(ippTagString(attr
->value_tag
), stdout
);
3781 if (attr
->num_values
> 1)
3786 char buffer
[8192]; /* Value buffer */
3788 if (Output
== _CUPS_OUTPUT_TEST
)
3792 puts(" -- separator --");
3796 printf(" %s (%s%s) = ", attr
->name
,
3797 attr
->num_values
> 1 ? "1setOf " : "",
3798 ippTagString(attr
->value_tag
));
3801 _ippAttrString(attr
, buffer
, sizeof(buffer
));
3808 * 'print_col()' - Print a collection attribute on the screen.
3812 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3814 int i
; /* Looping var */
3815 ipp_attribute_t
*attr
; /* Current attribute in collection */
3818 fputs("{ ", stdout
);
3819 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3821 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3822 ippTagString(attr
->value_tag
));
3824 switch (attr
->value_tag
)
3826 case IPP_TAG_INTEGER
:
3828 for (i
= 0; i
< attr
->num_values
; i
++)
3829 printf("%d ", attr
->values
[i
].integer
);
3832 case IPP_TAG_BOOLEAN
:
3833 for (i
= 0; i
< attr
->num_values
; i
++)
3834 if (attr
->values
[i
].boolean
)
3840 case IPP_TAG_NOVALUE
:
3844 case IPP_TAG_RANGE
:
3845 for (i
= 0; i
< attr
->num_values
; i
++)
3846 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3847 attr
->values
[i
].range
.upper
);
3850 case IPP_TAG_RESOLUTION
:
3851 for (i
= 0; i
< attr
->num_values
; i
++)
3852 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3853 attr
->values
[i
].resolution
.yres
,
3854 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3858 case IPP_TAG_STRING
:
3861 case IPP_TAG_KEYWORD
:
3862 case IPP_TAG_CHARSET
:
3864 case IPP_TAG_MIMETYPE
:
3865 case IPP_TAG_LANGUAGE
:
3866 for (i
= 0; i
< attr
->num_values
; i
++)
3867 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3870 case IPP_TAG_TEXTLANG
:
3871 case IPP_TAG_NAMELANG
:
3872 for (i
= 0; i
< attr
->num_values
; i
++)
3873 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3874 attr
->values
[i
].string
.charset
);
3877 case IPP_TAG_BEGIN_COLLECTION
:
3878 for (i
= 0; i
< attr
->num_values
; i
++)
3880 print_col(attr
->values
[i
].collection
);
3886 break; /* anti-compiler-warning-code */
3895 * 'print_csv()' - Print a line of CSV text.
3900 ipp_attribute_t
*attr
, /* I - First attribute for line */
3901 int num_displayed
, /* I - Number of attributes to display */
3902 char **displayed
, /* I - Attributes to display */
3903 size_t *widths
) /* I - Column widths */
3905 int i
; /* Looping var */
3906 size_t maxlength
; /* Max length of all columns */
3907 char *buffer
, /* String buffer */
3908 *bufptr
; /* Pointer into buffer */
3909 ipp_attribute_t
*current
; /* Current attribute */
3913 * Get the maximum string length we have to show and allocate...
3916 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3917 if (widths
[i
] > maxlength
)
3918 maxlength
= widths
[i
];
3922 if ((buffer
= malloc(maxlength
)) == NULL
)
3926 * Loop through the attributes to display...
3931 for (i
= 0; i
< num_displayed
; i
++)
3938 for (current
= attr
; current
; current
= current
->next
)
3942 else if (!strcmp(current
->name
, displayed
[i
]))
3944 _ippAttrString(current
, buffer
, maxlength
);
3949 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
3950 strchr(buffer
, '\\') != NULL
)
3953 for (bufptr
= buffer
; *bufptr
; bufptr
++)
3955 if (*bufptr
== '\\' || *bufptr
== '\"')
3962 fputs(buffer
, stdout
);
3968 for (i
= 0; i
< num_displayed
; i
++)
3973 fputs(displayed
[i
], stdout
);
3983 * 'print_fatal_error()' - Print a fatal error message.
3987 print_fatal_error(const char *s
, /* I - Printf-style format string */
3988 ...) /* I - Additional arguments as needed */
3990 char buffer
[10240]; /* Format buffer */
3991 va_list ap
; /* Pointer to arguments */
3995 * Format the error message...
3999 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4006 if (Output
== _CUPS_OUTPUT_PLIST
)
4009 print_xml_trailer(0, buffer
);
4012 _cupsLangPrintf(stderr
, "ipptool: %s", buffer
);
4017 * 'print_line()' - Print a line of formatted or CSV text.
4022 ipp_attribute_t
*attr
, /* I - First attribute for line */
4023 int num_displayed
, /* I - Number of attributes to display */
4024 char **displayed
, /* I - Attributes to display */
4025 size_t *widths
) /* I - Column widths */
4027 int i
; /* Looping var */
4028 size_t maxlength
; /* Max length of all columns */
4029 char *buffer
; /* String buffer */
4030 ipp_attribute_t
*current
; /* Current attribute */
4034 * Get the maximum string length we have to show and allocate...
4037 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
4038 if (widths
[i
] > maxlength
)
4039 maxlength
= widths
[i
];
4043 if ((buffer
= malloc(maxlength
)) == NULL
)
4047 * Loop through the attributes to display...
4052 for (i
= 0; i
< num_displayed
; i
++)
4059 for (current
= attr
; current
; current
= current
->next
)
4063 else if (!strcmp(current
->name
, displayed
[i
]))
4065 _ippAttrString(current
, buffer
, maxlength
);
4070 printf("%*s", (int)-widths
[i
], buffer
);
4076 for (i
= 0; i
< num_displayed
; i
++)
4081 printf("%*s", (int)-widths
[i
], displayed
[i
]);
4085 for (i
= 0; i
< num_displayed
; i
++)
4090 memset(buffer
, '-', widths
[i
]);
4091 buffer
[widths
[i
]] = '\0';
4092 fputs(buffer
, stdout
);
4102 * 'print_test_error()' - Print a test error message.
4106 print_test_error(const char *s
, /* I - Printf-style format string */
4107 ...) /* I - Additional arguments as needed */
4109 char buffer
[10240]; /* Format buffer */
4110 va_list ap
; /* Pointer to arguments */
4114 * Format the error message...
4118 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
4125 if (Output
== _CUPS_OUTPUT_PLIST
)
4126 print_xml_string("string", buffer
);
4128 printf(" %s\n", buffer
);
4133 * 'print_xml_header()' - Print a standard XML plist header.
4137 print_xml_header(void)
4141 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
4142 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
4143 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
4144 puts("<plist version=\"1.0\">");
4146 puts("<key>Transfer</key>");
4147 printf("<string>%s</string>\n",
4148 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
4149 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
4150 puts("<key>Tests</key>");
4159 * 'print_xml_string()' - Print an XML string with escaping.
4163 print_xml_string(const char *element
, /* I - Element name or NULL */
4164 const char *s
) /* I - String to print */
4167 printf("<%s>", element
);
4172 fputs("&", stdout
);
4174 fputs("<", stdout
);
4176 fputs(">", stdout
);
4177 else if ((*s
& 0xe0) == 0xc0)
4180 * Validate UTF-8 two-byte sequence...
4183 if ((s
[1] & 0xc0) != 0x80)
4194 else if ((*s
& 0xf0) == 0xe0)
4197 * Validate UTF-8 three-byte sequence...
4200 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80)
4212 else if ((*s
& 0xf8) == 0xf0)
4215 * Validate UTF-8 four-byte sequence...
4218 if ((s
[1] & 0xc0) != 0x80 || (s
[2] & 0xc0) != 0x80 ||
4219 (s
[3] & 0xc0) != 0x80)
4232 else if ((*s
& 0x80) || (*s
< ' ' && !isspace(*s
& 255)))
4235 * Invalid control character...
4247 printf("</%s>\n", element
);
4252 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
4256 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
4257 const char *message
) /* I - Error message or NULL */
4262 puts("<key>Successful</key>");
4263 puts(success
? "<true />" : "<false />");
4266 puts("<key>ErrorMessage</key>");
4267 print_xml_string("string", message
);
4278 * 'set_variable()' - Set a variable value.
4282 set_variable(_cups_vars_t
*vars
, /* I - Variables */
4283 const char *name
, /* I - Variable name */
4284 const char *value
) /* I - Value string */
4286 _cups_var_t key
, /* Search key */
4287 *var
; /* New variable */
4290 if (!_cups_strcasecmp(name
, "filename"))
4293 free(vars
->filename
);
4295 vars
->filename
= strdup(value
);
4299 key
.name
= (char *)name
;
4300 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
4303 var
->value
= strdup(value
);
4305 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
4307 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
4312 var
->name
= strdup(name
);
4313 var
->value
= strdup(value
);
4315 cupsArrayAdd(vars
->vars
, var
);
4322 * 'sigterm_handler()' - Handle SIGINT and SIGTERM.
4326 sigterm_handler(int sig
) /* I - Signal number (unused) */
4332 signal(SIGINT
, SIG_DFL
);
4333 signal(SIGTERM
, SIG_DFL
);
4339 * 'timeout_cb()' - Handle HTTP timeouts.
4342 static int /* O - 1 to continue, 0 to cancel */
4343 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
4344 void *user_data
) /* I - User data (unused) */
4349 /* Always cancel on timeout */
4355 * 'usage()' - Show program usage.
4361 _cupsLangPuts(stderr
, _("Usage: ipptool [options] URI filename [ ... "
4363 _cupsLangPuts(stderr
, _("Options:"));
4364 _cupsLangPuts(stderr
, _(" -4 Connect using IPv4."));
4365 _cupsLangPuts(stderr
, _(" -6 Connect using IPv6."));
4366 _cupsLangPuts(stderr
, _(" -C Send requests using "
4367 "chunking (default)."));
4368 _cupsLangPuts(stderr
, _(" -E Test with TLS "
4370 _cupsLangPuts(stderr
, _(" -I Ignore errors."));
4371 _cupsLangPuts(stderr
, _(" -L Send requests using "
4372 "content-length."));
4373 _cupsLangPuts(stderr
, _(" -S Test with SSL "
4375 _cupsLangPuts(stderr
, _(" -T Set the receive/send "
4376 "timeout in seconds."));
4377 _cupsLangPuts(stderr
, _(" -V version Set default IPP "
4379 _cupsLangPuts(stderr
, _(" -X Produce XML plist instead "
4381 _cupsLangPuts(stderr
, _(" -d name=value Set named variable to "
4383 _cupsLangPuts(stderr
, _(" -f filename Set default request "
4385 _cupsLangPuts(stderr
, _(" -i seconds Repeat the last file with "
4386 "the given time interval."));
4387 _cupsLangPuts(stderr
, _(" -n count Repeat the last file the "
4388 "given number of times."));
4389 _cupsLangPuts(stderr
, _(" -q Be quiet - no output "
4391 _cupsLangPuts(stderr
, _(" -t Produce a test report."));
4392 _cupsLangPuts(stderr
, _(" -v Show all attributes sent "
4400 * 'validate_attr()' - Determine whether an attribute is valid.
4403 static int /* O - 1 if valid, 0 otherwise */
4404 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
4405 int print
) /* I - 1 = report issues to stdout */
4407 int i
; /* Looping var */
4408 char scheme
[64], /* Scheme from URI */
4409 userpass
[256], /* Username/password from URI */
4410 hostname
[256], /* Hostname from URI */
4411 resource
[1024]; /* Resource from URI */
4412 int port
, /* Port number from URI */
4413 uri_status
, /* URI separation status */
4414 valid
= 1; /* Is the attribute valid? */
4415 const char *ptr
; /* Pointer into string */
4416 ipp_attribute_t
*colattr
; /* Collection attribute */
4417 regex_t re
; /* Regular expression */
4418 ipp_uchar_t
*date
; /* Current date value */
4429 * Validate the attribute name.
4432 for (ptr
= attr
->name
; *ptr
; ptr
++)
4433 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
4436 if (*ptr
|| ptr
== attr
->name
)
4441 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
4442 "2911 section 4.1.3).", attr
->name
);
4445 if ((ptr
- attr
->name
) > 255)
4450 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
4451 "section 4.1.3).", attr
->name
);
4454 switch (attr
->value_tag
)
4456 case IPP_TAG_INTEGER
:
4459 case IPP_TAG_BOOLEAN
:
4460 for (i
= 0; i
< attr
->num_values
; i
++)
4462 if (attr
->values
[i
].boolean
!= 0 &&
4463 attr
->values
[i
].boolean
!= 1)
4468 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
4469 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
4477 for (i
= 0; i
< attr
->num_values
; i
++)
4479 if (attr
->values
[i
].integer
< 1)
4484 print_test_error("\"%s\": Bad enum value %d - out of range "
4485 "(RFC 2911 section 4.1.4).", attr
->name
,
4486 attr
->values
[i
].integer
);
4493 case IPP_TAG_STRING
:
4494 for (i
= 0; i
< attr
->num_values
; i
++)
4496 if (attr
->values
[i
].unknown
.length
> 1023)
4501 print_test_error("\"%s\": Bad octetString value - bad length %d "
4502 "(RFC 2911 section 4.1.10).", attr
->name
,
4503 attr
->values
[i
].unknown
.length
);
4511 for (i
= 0; i
< attr
->num_values
; i
++)
4513 date
= attr
->values
[i
].date
;
4515 if (date
[2] < 1 || date
[2] > 12)
4520 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
4521 "section 4.1.13).", attr
->name
, date
[2]);
4526 if (date
[3] < 1 || date
[3] > 31)
4531 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
4532 "section 4.1.13).", attr
->name
, date
[3]);
4542 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
4543 "section 4.1.13).", attr
->name
, date
[4]);
4553 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
4554 "section 4.1.13).", attr
->name
, date
[5]);
4564 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
4565 "section 4.1.13).", attr
->name
, date
[6]);
4575 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
4576 "section 4.1.13).", attr
->name
, date
[7]);
4581 if (date
[8] != '-' && date
[8] != '+')
4586 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
4587 "section 4.1.13).", attr
->name
, date
[8]);
4597 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
4598 "section 4.1.13).", attr
->name
, date
[9]);
4608 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
4609 "section 4.1.13).", attr
->name
, date
[10]);
4616 case IPP_TAG_RESOLUTION
:
4617 for (i
= 0; i
< attr
->num_values
; i
++)
4619 if (attr
->values
[i
].resolution
.xres
<= 0)
4624 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
4625 "feed resolution must be positive (RFC 2911 "
4626 "section 4.1.13).", attr
->name
,
4627 attr
->values
[i
].resolution
.xres
,
4628 attr
->values
[i
].resolution
.yres
,
4629 attr
->values
[i
].resolution
.units
==
4630 IPP_RES_PER_INCH
? "dpi" :
4631 attr
->values
[i
].resolution
.units
==
4632 IPP_RES_PER_CM
? "dpc" : "unknown");
4637 if (attr
->values
[i
].resolution
.yres
<= 0)
4642 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4643 "resolution must be positive (RFC 2911 section "
4644 "4.1.13).", attr
->name
,
4645 attr
->values
[i
].resolution
.xres
,
4646 attr
->values
[i
].resolution
.yres
,
4647 attr
->values
[i
].resolution
.units
==
4648 IPP_RES_PER_INCH
? "dpi" :
4649 attr
->values
[i
].resolution
.units
==
4650 IPP_RES_PER_CM
? "dpc" : "unknown");
4655 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4656 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4661 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4662 "units value (RFC 2911 section 4.1.13).",
4663 attr
->name
, attr
->values
[i
].resolution
.xres
,
4664 attr
->values
[i
].resolution
.yres
,
4665 attr
->values
[i
].resolution
.units
==
4666 IPP_RES_PER_INCH
? "dpi" :
4667 attr
->values
[i
].resolution
.units
==
4668 IPP_RES_PER_CM
? "dpc" : "unknown");
4675 case IPP_TAG_RANGE
:
4676 for (i
= 0; i
< attr
->num_values
; i
++)
4678 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4683 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4684 "greater than upper (RFC 2911 section 4.1.13).",
4685 attr
->name
, attr
->values
[i
].range
.lower
,
4686 attr
->values
[i
].range
.upper
);
4693 case IPP_TAG_BEGIN_COLLECTION
:
4694 for (i
= 0; i
< attr
->num_values
; i
++)
4696 for (colattr
= attr
->values
[i
].collection
->attrs
;
4698 colattr
= colattr
->next
)
4700 if (!validate_attr(colattr
, 0))
4707 if (colattr
&& print
)
4709 print_test_error("\"%s\": Bad collection value.", attr
->name
);
4713 validate_attr(colattr
, print
);
4714 colattr
= colattr
->next
;
4721 case IPP_TAG_TEXTLANG
:
4722 for (i
= 0; i
< attr
->num_values
; i
++)
4724 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4726 if ((*ptr
& 0xe0) == 0xc0)
4729 if ((*ptr
& 0xc0) != 0x80)
4732 else if ((*ptr
& 0xf0) == 0xe0)
4735 if ((*ptr
& 0xc0) != 0x80)
4738 if ((*ptr
& 0xc0) != 0x80)
4741 else if ((*ptr
& 0xf8) == 0xf0)
4744 if ((*ptr
& 0xc0) != 0x80)
4747 if ((*ptr
& 0xc0) != 0x80)
4750 if ((*ptr
& 0xc0) != 0x80)
4753 else if (*ptr
& 0x80)
4762 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4763 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4764 attr
->values
[i
].string
.text
);
4769 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4774 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4775 "(RFC 2911 section 4.1.1).", attr
->name
,
4776 attr
->values
[i
].string
.text
,
4777 (int)strlen(attr
->values
[i
].string
.text
));
4785 case IPP_TAG_NAMELANG
:
4786 for (i
= 0; i
< attr
->num_values
; i
++)
4788 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4790 if ((*ptr
& 0xe0) == 0xc0)
4793 if ((*ptr
& 0xc0) != 0x80)
4796 else if ((*ptr
& 0xf0) == 0xe0)
4799 if ((*ptr
& 0xc0) != 0x80)
4802 if ((*ptr
& 0xc0) != 0x80)
4805 else if ((*ptr
& 0xf8) == 0xf0)
4808 if ((*ptr
& 0xc0) != 0x80)
4811 if ((*ptr
& 0xc0) != 0x80)
4814 if ((*ptr
& 0xc0) != 0x80)
4817 else if (*ptr
& 0x80)
4826 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4827 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4828 attr
->values
[i
].string
.text
);
4833 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4838 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4839 "(RFC 2911 section 4.1.2).", attr
->name
,
4840 attr
->values
[i
].string
.text
,
4841 (int)strlen(attr
->values
[i
].string
.text
));
4848 case IPP_TAG_KEYWORD
:
4849 for (i
= 0; i
< attr
->num_values
; i
++)
4851 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4852 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4856 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4861 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4862 "character (RFC 2911 section 4.1.3).",
4863 attr
->name
, attr
->values
[i
].string
.text
);
4868 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4873 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4874 "length %d (RFC 2911 section 4.1.3).",
4875 attr
->name
, attr
->values
[i
].string
.text
,
4876 (int)strlen(attr
->values
[i
].string
.text
));
4884 for (i
= 0; i
< attr
->num_values
; i
++)
4886 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4887 attr
->values
[i
].string
.text
,
4888 scheme
, sizeof(scheme
),
4889 userpass
, sizeof(userpass
),
4890 hostname
, sizeof(hostname
),
4891 &port
, resource
, sizeof(resource
));
4893 if (uri_status
< HTTP_URI_OK
)
4898 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4899 "(RFC 2911 section 4.1.5).", attr
->name
,
4900 attr
->values
[i
].string
.text
,
4901 URIStatusStrings
[uri_status
-
4902 HTTP_URI_OVERFLOW
]);
4907 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4912 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4913 "(RFC 2911 section 4.1.5).", attr
->name
,
4914 attr
->values
[i
].string
.text
,
4915 (int)strlen(attr
->values
[i
].string
.text
));
4922 case IPP_TAG_URISCHEME
:
4923 for (i
= 0; i
< attr
->num_values
; i
++)
4925 ptr
= attr
->values
[i
].string
.text
;
4926 if (islower(*ptr
& 255))
4928 for (ptr
++; *ptr
; ptr
++)
4929 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4930 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4934 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4939 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4940 "characters (RFC 2911 section 4.1.6).",
4941 attr
->name
, attr
->values
[i
].string
.text
);
4946 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4951 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4952 "length %d (RFC 2911 section 4.1.6).",
4953 attr
->name
, attr
->values
[i
].string
.text
,
4954 (int)strlen(attr
->values
[i
].string
.text
));
4961 case IPP_TAG_CHARSET
:
4962 for (i
= 0; i
< attr
->num_values
; i
++)
4964 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4965 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4966 isspace(*ptr
& 255))
4969 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4974 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4975 "characters (RFC 2911 section 4.1.7).",
4976 attr
->name
, attr
->values
[i
].string
.text
);
4981 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4986 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4987 "length %d (RFC 2911 section 4.1.7).",
4988 attr
->name
, attr
->values
[i
].string
.text
,
4989 (int)strlen(attr
->values
[i
].string
.text
));
4996 case IPP_TAG_LANGUAGE
:
4998 * The following regular expression is derived from the ABNF for
4999 * language tags in RFC 4646. All I can say is that this is the
5000 * easiest way to check the values...
5003 if ((i
= regcomp(&re
,
5005 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
5007 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
5008 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
5009 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
5010 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
5011 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
5013 "x(-[a-z0-9]{1,8})+" /* privateuse */
5015 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
5017 REG_NOSUB
| REG_EXTENDED
)) != 0)
5019 char temp
[256]; /* Temporary error string */
5021 regerror(i
, &re
, temp
, sizeof(temp
));
5022 print_fatal_error("Unable to compile naturalLanguage regular "
5023 "expression: %s.", temp
);
5026 for (i
= 0; i
< attr
->num_values
; i
++)
5028 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5033 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
5034 "characters (RFC 2911 section 4.1.8).",
5035 attr
->name
, attr
->values
[i
].string
.text
);
5040 if (strlen(attr
->values
[i
].string
.text
) > 63)
5045 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
5046 "length %d (RFC 2911 section 4.1.8).",
5047 attr
->name
, attr
->values
[i
].string
.text
,
5048 (int)strlen(attr
->values
[i
].string
.text
));
5057 case IPP_TAG_MIMETYPE
:
5059 * The following regular expression is derived from the ABNF for
5060 * language tags in RFC 2045 and 4288. All I can say is that this is
5061 * the easiest way to check the values...
5064 if ((i
= regcomp(&re
,
5066 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
5068 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
5069 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
5070 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
5073 REG_NOSUB
| REG_EXTENDED
)) != 0)
5075 char temp
[256]; /* Temporary error string */
5077 regerror(i
, &re
, temp
, sizeof(temp
));
5078 print_fatal_error("Unable to compile mimeMediaType regular "
5079 "expression: %s.", temp
);
5082 for (i
= 0; i
< attr
->num_values
; i
++)
5084 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5089 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
5090 "characters (RFC 2911 section 4.1.9).",
5091 attr
->name
, attr
->values
[i
].string
.text
);
5096 if (strlen(attr
->values
[i
].string
.text
) > 255)
5101 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
5102 "length %d (RFC 2911 section 4.1.9).",
5103 attr
->name
, attr
->values
[i
].string
.text
,
5104 (int)strlen(attr
->values
[i
].string
.text
));
5120 * 'with_value()' - Test a WITH-VALUE predicate.
5123 static int /* O - 1 on match, 0 on non-match */
5124 with_value(char *value
, /* I - Value string */
5125 int regex
, /* I - Value is a regular expression */
5126 ipp_attribute_t
*attr
, /* I - Attribute to compare */
5127 int report
, /* I - 1 = report failures */
5128 char *matchbuf
, /* I - Buffer to hold matching value */
5129 size_t matchlen
) /* I - Length of match buffer */
5131 int i
; /* Looping var */
5132 char *valptr
; /* Pointer into value */
5138 * NULL matches everything.
5141 if (!value
|| !*value
)
5145 * Compare the value string to the attribute value.
5148 switch (attr
->value_tag
)
5150 case IPP_TAG_INTEGER
:
5152 for (i
= 0; i
< attr
->num_values
; i
++)
5154 char op
, /* Comparison operator */
5155 *nextptr
; /* Next pointer */
5156 int intvalue
; /* Integer value */
5161 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5162 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5163 *valptr
== '=' || *valptr
== '>')
5166 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5168 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5176 intvalue
= strtol(valptr
, &nextptr
, 0);
5177 if (nextptr
== valptr
)
5184 if (attr
->values
[i
].integer
== intvalue
)
5186 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5191 if (attr
->values
[i
].integer
< intvalue
)
5193 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5198 if (attr
->values
[i
].integer
> intvalue
)
5200 snprintf(matchbuf
, matchlen
, "%d", attr
->values
[i
].integer
);
5210 for (i
= 0; i
< attr
->num_values
; i
++)
5211 print_test_error("GOT: %s=%d", attr
->name
, attr
->values
[i
].integer
);
5215 case IPP_TAG_RANGE
:
5216 for (i
= 0; i
< attr
->num_values
; i
++)
5218 char op
, /* Comparison operator */
5219 *nextptr
; /* Next pointer */
5220 int intvalue
; /* Integer value */
5225 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
5226 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
5227 *valptr
== '=' || *valptr
== '>')
5230 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
5232 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
5240 intvalue
= strtol(valptr
, &nextptr
, 0);
5241 if (nextptr
== valptr
)
5248 if (attr
->values
[i
].range
.lower
== intvalue
||
5249 attr
->values
[i
].range
.upper
== intvalue
)
5251 snprintf(matchbuf
, matchlen
, "%d-%d",
5252 attr
->values
[i
].range
.lower
,
5253 attr
->values
[i
].range
.upper
);
5258 if (attr
->values
[i
].range
.upper
< intvalue
)
5260 snprintf(matchbuf
, matchlen
, "%d-%d",
5261 attr
->values
[i
].range
.lower
,
5262 attr
->values
[i
].range
.upper
);
5267 if (attr
->values
[i
].range
.lower
> intvalue
)
5269 snprintf(matchbuf
, matchlen
, "%d-%d",
5270 attr
->values
[i
].range
.lower
,
5271 attr
->values
[i
].range
.upper
);
5281 for (i
= 0; i
< attr
->num_values
; i
++)
5282 print_test_error("GOT: %s=%d-%d", attr
->name
,
5283 attr
->values
[i
].range
.lower
,
5284 attr
->values
[i
].range
.upper
);
5288 case IPP_TAG_BOOLEAN
:
5289 for (i
= 0; i
< attr
->num_values
; i
++)
5291 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
5293 strlcpy(matchbuf
, value
, matchlen
);
5300 for (i
= 0; i
< attr
->num_values
; i
++)
5301 print_test_error("GOT: %s=%s", attr
->name
,
5302 attr
->values
[i
].boolean
? "true" : "false");
5306 case IPP_TAG_NOVALUE
:
5307 case IPP_TAG_UNKNOWN
:
5310 case IPP_TAG_CHARSET
:
5311 case IPP_TAG_KEYWORD
:
5312 case IPP_TAG_LANGUAGE
:
5313 case IPP_TAG_MIMETYPE
:
5315 case IPP_TAG_NAMELANG
:
5317 case IPP_TAG_TEXTLANG
:
5319 case IPP_TAG_URISCHEME
:
5323 * Value is an extended, case-sensitive POSIX regular expression...
5326 regex_t re
; /* Regular expression */
5328 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
5330 char temp
[256]; /* Temporary string */
5332 regerror(i
, &re
, temp
, sizeof(temp
));
5334 print_fatal_error("Unable to compile WITH-VALUE regular expression "
5335 "\"%s\" - %s", value
, temp
);
5340 * See if ALL of the values match the given regular expression.
5343 for (i
= 0; i
< attr
->num_values
; i
++)
5345 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
5348 print_test_error("GOT: %s=\"%s\"", attr
->name
,
5349 attr
->values
[i
].string
.text
);
5357 if (i
== attr
->num_values
)
5358 strlcpy(matchbuf
, attr
->values
[0].string
.text
, matchlen
);
5360 return (i
== attr
->num_values
);
5365 * Value is a literal string, see if at least one value matches the
5369 for (i
= 0; i
< attr
->num_values
; i
++)
5371 if (!strcmp(value
, attr
->values
[i
].string
.text
))
5373 strlcpy(matchbuf
, attr
->values
[i
].string
.text
, matchlen
);
5380 for (i
= 0; i
< attr
->num_values
; i
++)
5381 print_test_error("GOT: %s=\"%s\"", attr
->name
,
5382 attr
->values
[i
].string
.text
);