4 * ipptool command for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Parse options and do tests.
18 * compare_vars() - Compare two variables.
19 * do_tests() - Do tests as specified in the test file.
20 * expand_variables() - Expand variables in a string.
21 * expect_matches() - Return true if the tag matches the specification.
22 * get_collection() - Get a collection value from the current test file.
23 * get_filename() - Get a filename based on the current test file.
24 * get_token() - Get a token from a file.
25 * get_variable() - Get the value of a variable.
26 * iso_date() - Return an ISO 8601 date/time string for the given IPP
28 * password_cb() - Password callback for authenticated tests.
29 * print_attr() - Print an attribute on the screen.
30 * print_col() - Print a collection attribute on the screen.
31 * print_csv() - Print a line of CSV text.
32 * print_fatal_error() - Print a fatal error message.
33 * print_line() - Print a line of formatted or CSV text.
34 * print_test_error() - Print a test error message.
35 * print_xml_header() - Print a standard XML plist header.
36 * print_xml_string() - Print an XML string with escaping.
37 * print_xml_trailer() - Print the XML trailer with success/fail value.
38 * set_variable() - Set a variable value.
39 * timeout_cb() - Handle HTTP timeouts.
40 * usage() - Show program usage.
41 * validate_attr() - Determine whether an attribute is valid.
42 * with_value() - Test a WITH-VALUE predicate.
46 * Include necessary headers...
49 #include <cups/cups-private.h>
50 #include <cups/file-private.h>
55 #endif /* !O_BINARY */
62 typedef enum _cups_transfer_e
/**** How to send request data ****/
64 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
65 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
66 _CUPS_TRANSFER_LENGTH
/* Length always */
69 typedef enum _cups_output_e
/**** Output mode ****/
71 _CUPS_OUTPUT_QUIET
, /* No output */
72 _CUPS_OUTPUT_TEST
, /* Traditional CUPS test output */
73 _CUPS_OUTPUT_PLIST
, /* XML plist test output */
74 _CUPS_OUTPUT_LIST
, /* Tabular list output */
75 _CUPS_OUTPUT_CSV
/* Comma-separated values output */
78 typedef struct _cups_expect_s
/**** Expected attribute info ****/
80 int optional
, /* Optional attribute? */
81 not_expect
; /* Don't expect attribute? */
82 char *name
, /* Attribute name */
83 *of_type
, /* Type name */
84 *same_count_as
, /* Parallel attribute name */
85 *if_defined
, /* Only required if variable defined */
86 *if_not_defined
, /* Only required if variable is not defined */
87 *with_value
, /* Attribute must include this value */
88 *define_match
, /* Variable to define on match */
89 *define_no_match
, /* Variable to define on no-match */
90 *define_value
; /* Variable to define with value */
91 int with_regex
, /* WITH-VALUE is a regular expression */
92 count
; /* Expected count if > 0 */
93 ipp_tag_t in_group
; /* IN-GROUP value */
96 typedef struct _cups_status_s
/**** Status info ****/
98 ipp_status_t status
; /* Expected status code */
99 char *if_defined
, /* Only if variable is defined */
100 *if_not_defined
; /* Only if variable is not defined */
103 typedef struct _cups_var_s
/**** Variable ****/
105 char *name
, /* Name of variable */
106 *value
; /* Value of variable */
109 typedef struct _cups_vars_s
/**** Set of variables ****/
111 const char *uri
, /* URI for printer */
112 *filename
; /* Filename */
113 char scheme
[64], /* Scheme from URI */
114 userpass
[256], /* Username/password from URI */
115 hostname
[256], /* Hostname from URI */
116 resource
[1024]; /* Resource path from URI */
117 int port
; /* Port number from URI */
118 http_encryption_t encryption
; /* Encryption for connection? */
119 double timeout
; /* Timeout for connection */
120 cups_array_t
*vars
; /* Array of variables */
128 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
129 /* How to transfer requests */
130 _cups_output_t Output
= _CUPS_OUTPUT_LIST
;
132 int IgnoreErrors
= 0, /* Ignore errors? */
133 Verbosity
= 0, /* Show all attributes? */
134 Version
= 11, /* Default IPP version */
135 XMLHeader
= 0; /* 1 if header is written */
136 char *Password
= NULL
; /* Password from URI */
137 const char * const URIStatusStrings
[] = /* URI status strings */
140 "Bad arguments to function",
141 "Bad resource in URI",
142 "Bad port number in URI",
143 "Bad hostname/address in URI",
144 "Bad username in URI",
148 "Missing scheme in URI",
149 "Unknown scheme in URI",
150 "Missing resource in URI"
158 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
159 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
160 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
163 __attribute((nonnull(1,2,3)))
164 #endif /* __GNUC__ */
166 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
167 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
168 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
170 static char *get_token(FILE *fp
, char *buf
, int buflen
,
172 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
173 static char *iso_date(ipp_uchar_t
*date
);
174 static const char *password_cb(const char *prompt
);
175 static void print_attr(ipp_attribute_t
*attr
);
176 static void print_col(ipp_t
*col
);
177 static void print_csv(ipp_attribute_t
*attr
, int num_displayed
,
178 char **displayed
, size_t *widths
);
179 static void print_fatal_error(const char *s
, ...)
181 __attribute__ ((__format__ (__printf__
, 1, 2)))
182 #endif /* __GNUC__ */
184 static void print_line(ipp_attribute_t
*attr
, int num_displayed
,
185 char **displayed
, size_t *widths
);
186 static void print_test_error(const char *s
, ...)
188 __attribute__ ((__format__ (__printf__
, 1, 2)))
189 #endif /* __GNUC__ */
191 static void print_xml_header(void);
192 static void print_xml_string(const char *element
, const char *s
);
193 static void print_xml_trailer(int success
, const char *message
);
194 static void set_variable(_cups_vars_t
*vars
, const char *name
,
196 static int timeout_cb(http_t
*http
, void *user_data
);
197 static void usage(void);
198 static int validate_attr(ipp_attribute_t
*attr
, int print
);
199 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
,
204 * 'main()' - Parse options and do tests.
207 int /* O - Exit status */
208 main(int argc
, /* I - Number of command-line args */
209 char *argv
[]) /* I - Command-line arguments */
211 int i
; /* Looping var */
212 int status
; /* Status of tests... */
213 char *opt
, /* Current option */
214 name
[1024], /* Name/value buffer */
215 *value
, /* Pointer to value */
216 filename
[1024], /* Real filename */
217 testname
[1024]; /* Real test filename */
218 const char *testfile
; /* Test file to use */
219 int interval
, /* Test interval */
220 repeat
; /* Repeat count */
221 _cups_vars_t vars
; /* Variables */
222 http_uri_status_t uri_status
; /* URI separation status */
223 _cups_globals_t
*cg
= _cupsGlobals();
229 * Initialize the locale and variables...
232 _cupsSetLocale(argv
);
234 memset(&vars
, 0, sizeof(vars
));
235 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
240 * ipptool URI testfile
248 for (i
= 1; i
< argc
; i
++)
250 if (argv
[i
][0] == '-')
252 for (opt
= argv
[i
] + 1; *opt
; opt
++)
256 case 'C' : /* Enable HTTP chunking */
257 Transfer
= _CUPS_TRANSFER_CHUNKED
;
260 case 'E' : /* Encrypt with TLS */
262 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
264 _cupsLangPrintf(stderr
,
265 _("%s: Sorry, no encryption support compiled in\n"),
267 #endif /* HAVE_SSL */
270 case 'I' : /* Ignore errors */
274 case 'L' : /* Disable HTTP chunking */
275 Transfer
= _CUPS_TRANSFER_LENGTH
;
278 case 'S' : /* Encrypt with SSL */
280 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
282 _cupsLangPrintf(stderr
,
283 _("%s: Sorry, no encryption support compiled in\n"),
285 #endif /* HAVE_SSL */
288 case 'T' : /* Set timeout */
293 _cupsLangPuts(stderr
,
294 _("ipptool: Missing timeout for \"-T\".\n"));
298 vars
.timeout
= _cupsStrScand(argv
[i
], NULL
, localeconv());
301 case 'V' : /* Set IPP version */
306 _cupsLangPuts(stderr
,
307 _("ipptool: Missing version for \"-V\".\n"));
311 if (!strcmp(argv
[i
], "1.0"))
313 else if (!strcmp(argv
[i
], "1.1"))
315 else if (!strcmp(argv
[i
], "2.0"))
317 else if (!strcmp(argv
[i
], "2.1"))
319 else if (!strcmp(argv
[i
], "2.2"))
323 _cupsLangPrintf(stderr
,
324 _("ipptool: Bad version %s for \"-V\".\n"),
330 case 'X' : /* Produce XML output */
331 Output
= _CUPS_OUTPUT_PLIST
;
333 if (interval
|| repeat
)
335 _cupsLangPuts(stderr
, _("ipptool: \"-i\" and \"-n\" are "
336 "incompatible with -X\".\n"));
341 case 'c' : /* CSV output */
342 Output
= _CUPS_OUTPUT_CSV
;
345 case 'd' : /* Define a variable */
350 _cupsLangPuts(stderr
,
351 _("ipptool: Missing name=value for \"-d\".\n"));
355 strlcpy(name
, argv
[i
], sizeof(name
));
356 if ((value
= strchr(name
, '=')) != NULL
)
359 value
= name
+ strlen(name
);
361 set_variable(&vars
, name
, value
);
364 case 'f' : /* Set the default test filename */
369 _cupsLangPuts(stderr
,
370 _("ipptool: Missing filename for \"-f\".\n"));
374 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
376 snprintf(filename
, sizeof(filename
), "%s/ipptool/%s",
377 cg
->cups_datadir
, argv
[i
]);
378 if (access(argv
[i
], 0))
379 vars
.filename
= argv
[i
];
381 vars
.filename
= filename
;
384 vars
.filename
= argv
[i
];
387 case 'i' : /* Test every N seconds */
392 _cupsLangPuts(stderr
,
393 _("ipptool: Missing seconds for \"-i\".\n"));
397 interval
= atoi(argv
[i
]);
399 if (Output
== _CUPS_OUTPUT_PLIST
&& interval
)
401 _cupsLangPuts(stderr
, _("ipptool: \"-i\" is incompatible with "
407 case 'l' : /* List as a table */
408 Output
= _CUPS_OUTPUT_LIST
;
411 case 'n' : /* Repeat count */
416 _cupsLangPuts(stderr
,
417 _("ipptool: Missing count for \"-n\".\n"));
421 repeat
= atoi(argv
[i
]);
423 if (Output
== _CUPS_OUTPUT_PLIST
&& repeat
)
425 _cupsLangPuts(stderr
, _("ipptool: \"-n\" is incompatible with "
431 case 'q' : /* Be quiet */
432 Output
= _CUPS_OUTPUT_QUIET
;
435 case 't' : /* CUPS test output */
436 Output
= _CUPS_OUTPUT_TEST
;
439 case 'v' : /* Be verbose */
444 _cupsLangPrintf(stderr
, _("ipptool: Unknown option \"-%c\".\n"),
451 else if (!strncmp(argv
[i
], "ipp://", 6) || !strncmp(argv
[i
], "http://", 7)
453 || !strncmp(argv
[i
], "ipps://", 7)
454 || !strncmp(argv
[i
], "https://", 8)
455 #endif /* HAVE_SSL */
464 _cupsLangPuts(stderr
, _("ipptool: May only specify a single URI.\n"));
469 if (!strncmp(argv
[i
], "ipps://", 7) || !strncmp(argv
[i
], "https://", 8))
470 vars
.encryption
= HTTP_ENCRYPT_ALWAYS
;
471 #endif /* HAVE_SSL */
474 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
475 vars
.scheme
, sizeof(vars
.scheme
),
476 vars
.userpass
, sizeof(vars
.userpass
),
477 vars
.hostname
, sizeof(vars
.hostname
),
479 vars
.resource
, sizeof(vars
.resource
));
481 if (uri_status
!= HTTP_URI_OK
)
483 _cupsLangPrintf(stderr
, _("ipptool: Bad URI - %s.\n"),
484 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
488 if (vars
.userpass
[0])
490 if ((Password
= strchr(vars
.userpass
, ':')) != NULL
)
493 cupsSetUser(vars
.userpass
);
494 cupsSetPasswordCB(password_cb
);
495 set_variable(&vars
, "uriuser", vars
.userpass
);
506 _cupsLangPuts(stderr
, _("ipptool: URI required before test file."));
510 if (access(argv
[i
], 0) && argv
[i
][0] != '/')
512 snprintf(testname
, sizeof(testname
), "%s/ipptool/%s", cg
->cups_datadir
,
514 if (access(testname
, 0))
522 if (!do_tests(&vars
, testfile
))
527 if (!vars
.uri
|| !testfile
)
531 * Loop if the interval is set...
534 if (Output
== _CUPS_OUTPUT_PLIST
)
535 print_xml_trailer(!status
, NULL
);
536 else if (interval
&& repeat
> 0)
541 do_tests(&vars
, testfile
);
550 do_tests(&vars
, testfile
);
563 * 'compare_vars()' - Compare two variables.
566 static int /* O - Result of comparison */
567 compare_vars(_cups_var_t
*a
, /* I - First variable */
568 _cups_var_t
*b
) /* I - Second variable */
570 return (strcasecmp(a
->name
, b
->name
));
575 * 'do_tests()' - Do tests as specified in the test file.
578 static int /* 1 = success, 0 = failure */
579 do_tests(_cups_vars_t
*vars
, /* I - Variables */
580 const char *testfile
) /* I - Test file to use */
582 int i
, /* Looping var */
583 linenum
, /* Current line number */
584 pass
, /* Did we pass the test? */
585 prev_pass
= 1, /* Did we pass the previous test? */
586 request_id
, /* Current request ID */
587 show_header
= 1, /* Show the test header? */
588 ignore_errors
, /* Ignore test failures? */
589 skip_previous
= 0; /* Skip on previous test failure? */
590 http_t
*http
= NULL
; /* HTTP connection to server */
591 FILE *fp
= NULL
; /* Test file */
592 char resource
[512], /* Resource for request */
593 token
[1024], /* Token from file */
594 *tokenptr
, /* Pointer into token */
595 temp
[1024]; /* Temporary string */
596 ipp_t
*request
= NULL
; /* IPP request */
597 ipp_t
*response
= NULL
; /* IPP response */
598 char attr
[128]; /* Attribute name */
599 ipp_op_t op
; /* Operation */
600 ipp_tag_t group
; /* Current group */
601 ipp_tag_t value
; /* Current value type */
602 ipp_attribute_t
*attrptr
, /* Attribute pointer */
603 *found
, /* Found attribute */
604 *lastcol
= NULL
; /* Last collection attribute */
605 char name
[1024]; /* Name of test */
606 char filename
[1024]; /* Filename */
607 _cups_transfer_t transfer
; /* To chunk or not to chunk */
608 int version
, /* IPP version number to use */
609 skip_test
; /* Skip this test? */
610 int num_statuses
= 0; /* Number of valid status codes */
611 _cups_status_t statuses
[100], /* Valid status codes */
612 *last_status
; /* Last STATUS (for predicates) */
613 int num_expects
= 0; /* Number of expected attributes */
614 _cups_expect_t expects
[200], /* Expected attributes */
615 *expect
, /* Current expected attribute */
616 *last_expect
; /* Last EXPECT (for predicates) */
617 int num_displayed
= 0; /* Number of displayed attributes */
618 char *displayed
[200]; /* Displayed attributes */
619 size_t widths
[200]; /* Width of columns */
623 * Open the test file...
626 if ((fp
= fopen(testfile
, "r")) == NULL
)
628 print_fatal_error("Unable to open test file %s - %s", testfile
,
635 * Connect to the server...
638 if ((http
= httpConnectEncrypt(vars
->hostname
, vars
->port
,
639 vars
->encryption
)) == NULL
)
641 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
642 vars
->port
, strerror(errno
));
647 if (vars
->timeout
> 0.0)
648 _httpSetTimeout(http
, vars
->timeout
, timeout_cb
, NULL
);
654 CUPS_SRAND(time(NULL
));
658 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
660 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
663 * Expect an open brace...
666 if (!strcmp(token
, "DEFINE"))
672 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
673 get_token(fp
, temp
, sizeof(temp
), &linenum
))
675 expand_variables(vars
, token
, temp
, sizeof(token
));
676 set_variable(vars
, attr
, token
);
680 print_fatal_error("Missing DEFINE name and/or value on line %d.",
688 else if (!strcmp(token
, "IGNORE-ERRORS"))
695 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
696 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
698 IgnoreErrors
= !strcasecmp(temp
, "yes");
702 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
709 else if (!strcmp(token
, "INCLUDE"))
716 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
719 * Map the filename to and then run the tests...
722 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
733 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
741 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
744 * SKIP-IF-DEFINED variable
747 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
749 if (get_variable(vars
, temp
))
754 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.", linenum
);
759 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
762 * SKIP-IF-NOT-DEFINED variable
765 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
767 if (!get_variable(vars
, temp
))
772 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
778 else if (!strcmp(token
, "TRANSFER"))
786 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
788 if (!strcmp(temp
, "auto"))
789 Transfer
= _CUPS_TRANSFER_AUTO
;
790 else if (!strcmp(temp
, "chunked"))
791 Transfer
= _CUPS_TRANSFER_CHUNKED
;
792 else if (!strcmp(temp
, "length"))
793 Transfer
= _CUPS_TRANSFER_LENGTH
;
796 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
804 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
811 else if (!strcmp(token
, "VERSION"))
813 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
815 if (!strcmp(temp
, "1.0"))
817 else if (!strcmp(temp
, "1.1"))
819 else if (!strcmp(temp
, "2.0"))
821 else if (!strcmp(temp
, "2.1"))
823 else if (!strcmp(temp
, "2.2"))
827 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
834 print_fatal_error("Missing VERSION number on line %d.", linenum
);
841 else if (strcmp(token
, "{"))
843 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
849 * Initialize things...
854 if (Output
== _CUPS_OUTPUT_PLIST
)
856 else if (Output
== _CUPS_OUTPUT_TEST
)
857 printf("\"%s\":\n", testfile
);
862 strlcpy(resource
, vars
->resource
, sizeof(resource
));
867 group
= IPP_TAG_ZERO
;
868 ignore_errors
= IgnoreErrors
;
876 strlcpy(name
, testfile
, sizeof(name
));
877 if (strrchr(name
, '.') != NULL
)
878 *strrchr(name
, '.') = '\0';
881 * Parse until we see a close brace...
884 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
886 if (strcasecmp(token
, "COUNT") &&
887 strcasecmp(token
, "DEFINE-MATCH") &&
888 strcasecmp(token
, "DEFINE-NO-MATCH") &&
889 strcasecmp(token
, "DEFINE-VALUE") &&
890 strcasecmp(token
, "IF-DEFINED") &&
891 strcasecmp(token
, "IF-NOT-DEFINED") &&
892 strcasecmp(token
, "IN-GROUP") &&
893 strcasecmp(token
, "OF-TYPE") &&
894 strcasecmp(token
, "SAME-COUNT-AS") &&
895 strcasecmp(token
, "WITH-VALUE"))
898 if (strcasecmp(token
, "IF-DEFINED") &&
899 strcasecmp(token
, "IF-NOT-DEFINED"))
902 if (!strcmp(token
, "}"))
904 else if (!strcmp(token
, "{") && lastcol
)
907 * Another collection value
910 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
911 /* Collection value */
915 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
919 * Reallocate memory...
922 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
923 (lastcol
->num_values
+ 1) *
924 sizeof(ipp_value_t
))) == NULL
)
926 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
931 if (tempcol
!= lastcol
)
934 * Reset pointers in the list...
938 request
->prev
->next
= tempcol
;
940 request
->attrs
= tempcol
;
942 lastcol
= request
->current
= request
->last
= tempcol
;
945 lastcol
->values
[lastcol
->num_values
].collection
= col
;
946 lastcol
->num_values
++;
954 else if (!strcmp(token
, "DEFINE"))
960 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
961 get_token(fp
, temp
, sizeof(temp
), &linenum
))
963 expand_variables(vars
, token
, temp
, sizeof(token
));
964 set_variable(vars
, attr
, token
);
968 print_fatal_error("Missing DEFINE name and/or value on line %d.",
974 else if (!strcmp(token
, "IGNORE-ERRORS"))
981 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
982 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
984 ignore_errors
= !strcasecmp(temp
, "yes");
988 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum
);
995 else if (!strcasecmp(token
, "NAME"))
1001 get_token(fp
, name
, sizeof(name
), &linenum
);
1003 else if (!strcmp(token
, "REQUEST-ID"))
1010 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1012 if (isdigit(temp
[0] & 255))
1013 request_id
= atoi(temp
);
1014 else if (!strcasecmp(temp
, "random"))
1015 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
1018 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
1026 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
1031 else if (!strcmp(token
, "SKIP-IF-DEFINED"))
1034 * SKIP-IF-DEFINED variable
1037 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1039 if (get_variable(vars
, temp
))
1044 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1050 else if (!strcmp(token
, "SKIP-IF-NOT-DEFINED"))
1053 * SKIP-IF-NOT-DEFINED variable
1056 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1058 if (!get_variable(vars
, temp
))
1063 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1069 else if (!strcmp(token
, "SKIP-PREVIOUS-ERROR"))
1072 * SKIP-PREVIOUS-ERROR yes
1073 * SKIP-PREVIOUS-ERROR no
1076 if (get_token(fp
, temp
, sizeof(temp
), &linenum
) &&
1077 (!strcasecmp(temp
, "yes") || !strcasecmp(temp
, "no")))
1079 skip_previous
= !strcasecmp(temp
, "yes");
1083 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum
);
1090 else if (!strcmp(token
, "TRANSFER"))
1098 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1100 if (!strcmp(temp
, "auto"))
1101 transfer
= _CUPS_TRANSFER_AUTO
;
1102 else if (!strcmp(temp
, "chunked"))
1103 transfer
= _CUPS_TRANSFER_CHUNKED
;
1104 else if (!strcmp(temp
, "length"))
1105 transfer
= _CUPS_TRANSFER_LENGTH
;
1108 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
1116 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
1121 else if (!strcasecmp(token
, "VERSION"))
1123 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
1125 if (!strcmp(temp
, "0.0"))
1127 else if (!strcmp(temp
, "1.0"))
1129 else if (!strcmp(temp
, "1.1"))
1131 else if (!strcmp(temp
, "2.0"))
1133 else if (!strcmp(temp
, "2.1"))
1135 else if (!strcmp(temp
, "2.2"))
1139 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
1146 print_fatal_error("Missing VERSION number on line %d.", linenum
);
1151 else if (!strcasecmp(token
, "RESOURCE"))
1157 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
1159 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
1164 else if (!strcasecmp(token
, "OPERATION"))
1170 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1172 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
1177 if ((op
= ippOpValue(token
)) < 0 && (op
= strtol(token
, NULL
, 0)) == 0)
1179 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
1185 else if (!strcasecmp(token
, "GROUP"))
1188 * Attribute group...
1191 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1193 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
1198 if ((value
= ippTagValue(token
)) < 0)
1200 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
1206 ippAddSeparator(request
);
1210 else if (!strcasecmp(token
, "DELAY"))
1213 * Delay before operation...
1218 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1220 print_fatal_error("Missing DELAY value on line %d.", linenum
);
1225 if ((delay
= atoi(token
)) <= 0)
1227 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
1234 if (Output
== _CUPS_OUTPUT_TEST
)
1235 printf(" [%d second delay]\n", delay
);
1240 else if (!strcasecmp(token
, "ATTR"))
1246 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1248 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
1253 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
1255 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
1261 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
1263 print_fatal_error("Missing ATTR name on line %d.", linenum
);
1268 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1270 print_fatal_error("Missing ATTR value on line %d.", linenum
);
1275 expand_variables(vars
, token
, temp
, sizeof(token
));
1279 case IPP_TAG_BOOLEAN
:
1280 if (!strcasecmp(token
, "true"))
1281 ippAddBoolean(request
, group
, attr
, 1);
1283 ippAddBoolean(request
, group
, attr
, atoi(token
));
1286 case IPP_TAG_INTEGER
:
1288 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
1291 case IPP_TAG_RESOLUTION
:
1293 int xres
, /* X resolution */
1294 yres
; /* Y resolution */
1295 char *ptr
; /* Pointer into value */
1297 xres
= yres
= strtol(token
, (char **)&ptr
, 10);
1298 if (ptr
> token
&& xres
> 0)
1301 yres
= strtol(ptr
+ 1, (char **)&ptr
, 10);
1304 if (ptr
<= token
|| xres
<= 0 || yres
<= 0 || !ptr
||
1305 (strcasecmp(ptr
, "dpi") && strcasecmp(ptr
, "dpc") &&
1306 strcasecmp(ptr
, "other")))
1308 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1314 if (!strcasecmp(ptr
, "dpi"))
1315 ippAddResolution(request
, group
, attr
, IPP_RES_PER_INCH
,
1317 else if (!strcasecmp(ptr
, "dpc"))
1318 ippAddResolution(request
, group
, attr
, IPP_RES_PER_CM
,
1321 ippAddResolution(request
, group
, attr
, (ipp_res_t
)0,
1326 case IPP_TAG_RANGE
:
1328 int lowers
[4], /* Lower value */
1329 uppers
[4], /* Upper values */
1330 num_vals
; /* Number of values */
1333 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
1334 lowers
+ 0, uppers
+ 0,
1335 lowers
+ 1, uppers
+ 1,
1336 lowers
+ 2, uppers
+ 2,
1337 lowers
+ 3, uppers
+ 3);
1339 if ((num_vals
& 1) || num_vals
== 0)
1341 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1342 "%d.", token
, linenum
);
1347 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1352 case IPP_TAG_BEGIN_COLLECTION
:
1353 if (!strcmp(token
, "{"))
1355 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1356 /* Collection value */
1360 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1371 print_fatal_error("Bad ATTR collection value on line %d.",
1379 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1380 ippTagString(value
), linenum
);
1384 case IPP_TAG_TEXTLANG
:
1385 case IPP_TAG_NAMELANG
:
1388 case IPP_TAG_KEYWORD
:
1390 case IPP_TAG_URISCHEME
:
1391 case IPP_TAG_CHARSET
:
1392 case IPP_TAG_LANGUAGE
:
1393 case IPP_TAG_MIMETYPE
:
1394 if (!strchr(token
, ','))
1395 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1399 * Multiple string values...
1402 int num_values
; /* Number of values */
1403 char *values
[100], /* Values */
1404 *ptr
; /* Pointer to next value */
1410 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1413 values
[num_values
] = ptr
;
1417 ippAddStrings(request
, group
, value
, attr
, num_values
,
1418 NULL
, (const char **)values
);
1423 else if (!strcasecmp(token
, "FILE"))
1429 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1431 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1436 expand_variables(vars
, token
, temp
, sizeof(token
));
1437 get_filename(testfile
, filename
, token
, sizeof(filename
));
1439 else if (!strcasecmp(token
, "STATUS"))
1445 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1447 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1452 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1454 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1459 if ((statuses
[num_statuses
].status
= ippErrorValue(token
)) < 0 &&
1460 (statuses
[num_statuses
].status
= strtol(token
, NULL
, 0)) == 0)
1462 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1468 last_status
= statuses
+ num_statuses
;
1471 last_status
->if_defined
= NULL
;
1472 last_status
->if_not_defined
= NULL
;
1474 else if (!strcasecmp(token
, "EXPECT"))
1477 * Expected attributes...
1480 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1482 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1487 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1489 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1494 last_expect
= expects
+ num_expects
;
1497 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1499 if (token
[0] == '!')
1501 last_expect
->not_expect
= 1;
1502 last_expect
->name
= strdup(token
+ 1);
1504 else if (token
[0] == '?')
1506 last_expect
->optional
= 1;
1507 last_expect
->name
= strdup(token
+ 1);
1510 last_expect
->name
= strdup(token
);
1512 else if (!strcasecmp(token
, "COUNT"))
1514 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1516 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1521 if ((i
= atoi(token
)) <= 0)
1523 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1529 last_expect
->count
= i
;
1532 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1538 else if (!strcasecmp(token
, "DEFINE-MATCH"))
1540 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1542 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1549 last_expect
->define_match
= strdup(token
);
1552 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1558 else if (!strcasecmp(token
, "DEFINE-NO-MATCH"))
1560 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1562 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1569 last_expect
->define_no_match
= strdup(token
);
1572 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1573 "line %d.", linenum
);
1578 else if (!strcasecmp(token
, "DEFINE-VALUE"))
1580 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1582 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1589 last_expect
->define_value
= strdup(token
);
1592 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1598 else if (!strcasecmp(token
, "OF-TYPE"))
1600 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1602 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1609 last_expect
->of_type
= strdup(token
);
1612 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1618 else if (!strcasecmp(token
, "IN-GROUP"))
1620 ipp_tag_t in_group
; /* IN-GROUP value */
1623 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1625 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1630 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1633 else if (last_expect
)
1634 last_expect
->in_group
= in_group
;
1637 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1643 else if (!strcasecmp(token
, "SAME-COUNT-AS"))
1645 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1647 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1653 last_expect
->same_count_as
= strdup(token
);
1656 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1662 else if (!strcasecmp(token
, "IF-DEFINED"))
1664 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1666 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1672 last_expect
->if_defined
= strdup(token
);
1673 else if (last_status
)
1674 last_status
->if_defined
= strdup(token
);
1677 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1678 "on line %d.", linenum
);
1683 else if (!strcasecmp(token
, "IF-NOT-DEFINED"))
1685 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1687 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum
);
1693 last_expect
->if_not_defined
= strdup(token
);
1694 else if (last_status
)
1695 last_status
->if_not_defined
= strdup(token
);
1698 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1699 "on line %d.", linenum
);
1704 else if (!strcasecmp(token
, "WITH-VALUE"))
1706 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1708 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
1716 * Expand any variables in the value and then save it.
1719 expand_variables(vars
, token
, temp
, sizeof(token
));
1721 tokenptr
= token
+ strlen(token
) - 1;
1723 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
1726 * WITH-VALUE is a POSIX extended regular expression.
1729 last_expect
->with_value
= calloc(1, tokenptr
- token
);
1730 last_expect
->with_regex
= 1;
1732 if (last_expect
->with_value
)
1733 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
1738 * WITH-VALUE is a literal value...
1741 last_expect
->with_value
= strdup(token
);
1746 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
1752 else if (!strcasecmp(token
, "DISPLAY"))
1755 * Display attributes...
1758 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
1760 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
1765 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1767 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
1772 displayed
[num_displayed
] = strdup(token
);
1777 print_fatal_error("Unexpected token %s seen on line %d.", token
,
1785 * Submit the IPP request...
1788 request
->request
.op
.version
[0] = version
/ 10;
1789 request
->request
.op
.version
[1] = version
% 10;
1790 request
->request
.op
.operation_id
= op
;
1791 request
->request
.op
.request_id
= request_id
;
1793 if (Output
== _CUPS_OUTPUT_PLIST
)
1796 puts("<key>Name</key>");
1797 print_xml_string("string", name
);
1798 puts("<key>Operation</key>");
1799 print_xml_string("string", ippOpString(op
));
1800 puts("<key>RequestAttributes</key>");
1802 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1803 print_attr(attrptr
);
1806 else if (Output
== _CUPS_OUTPUT_TEST
)
1810 printf(" %s:\n", ippOpString(op
));
1812 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1813 print_attr(attrptr
);
1816 printf(" %-69.69s [", name
);
1820 if ((skip_previous
&& !prev_pass
) || skip_test
)
1825 if (Output
== _CUPS_OUTPUT_PLIST
)
1827 puts("<key>Successful</key>");
1829 puts("<key>StatusCode</key>");
1830 print_xml_string("string", "skip");
1831 puts("<key>ResponseAttributes</key>");
1835 else if (Output
== _CUPS_OUTPUT_TEST
)
1841 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
1842 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
1845 * Send request using chunking...
1848 http_status_t status
= cupsSendRequest(http
, request
, resource
, 0);
1850 if (status
== HTTP_CONTINUE
&& filename
[0])
1852 int fd
; /* File to send */
1853 char buffer
[8192]; /* Copy buffer */
1854 ssize_t bytes
; /* Bytes read/written */
1856 if ((fd
= open(filename
, O_RDONLY
| O_BINARY
)) >= 0)
1858 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1859 if ((status
= cupsWriteRequestData(http
, buffer
,
1860 bytes
)) != HTTP_CONTINUE
)
1865 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
, strerror(errno
));
1866 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
1868 status
= HTTP_ERROR
;
1874 if (status
== HTTP_CONTINUE
)
1875 response
= cupsGetResponse(http
, resource
);
1879 else if (filename
[0])
1880 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
1882 response
= cupsDoRequest(http
, request
, resource
);
1888 prev_pass
= pass
= 0;
1891 if (http
->version
!= HTTP_1_1
)
1892 prev_pass
= pass
= 0;
1894 if (response
->request
.status
.request_id
!= request_id
)
1895 prev_pass
= pass
= 0;
1898 (response
->request
.status
.version
[0] != (version
/ 10) ||
1899 response
->request
.status
.version
[1] != (version
% 10)))
1900 prev_pass
= pass
= 0;
1902 if ((attrptr
= ippFindAttribute(response
, "job-id",
1903 IPP_TAG_INTEGER
)) != NULL
)
1905 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1906 set_variable(vars
, "job-id", temp
);
1909 if ((attrptr
= ippFindAttribute(response
, "job-uri",
1910 IPP_TAG_URI
)) != NULL
)
1911 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
1913 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
1914 IPP_TAG_INTEGER
)) != NULL
)
1916 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1917 set_variable(vars
, "notify-subscription-id", temp
);
1920 attrptr
= response
->attrs
;
1921 if (!attrptr
|| !attrptr
->name
||
1922 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
1923 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1924 attrptr
->num_values
!= 1 ||
1925 strcmp(attrptr
->name
, "attributes-charset"))
1926 prev_pass
= pass
= 0;
1930 attrptr
= attrptr
->next
;
1931 if (!attrptr
|| !attrptr
->name
||
1932 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
1933 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1934 attrptr
->num_values
!= 1 ||
1935 strcmp(attrptr
->name
, "attributes-natural-language"))
1936 prev_pass
= pass
= 0;
1939 if ((attrptr
= ippFindAttribute(response
, "status-message",
1940 IPP_TAG_ZERO
)) != NULL
&&
1941 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1942 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1943 attrptr
->num_values
!= 1 ||
1944 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1945 strlen(attrptr
->values
[0].string
.text
) > 255)))
1946 prev_pass
= pass
= 0;
1948 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
1949 IPP_TAG_ZERO
)) != NULL
&&
1950 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1951 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1952 attrptr
->num_values
!= 1 ||
1953 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1954 strlen(attrptr
->values
[0].string
.text
) > 1023)))
1955 prev_pass
= pass
= 0;
1957 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
1959 attrptr
= attrptr
->next
)
1961 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
1963 prev_pass
= pass
= 0;
1967 if (!validate_attr(attrptr
, 0))
1969 prev_pass
= pass
= 0;
1974 for (i
= 0; i
< num_statuses
; i
++)
1976 if (statuses
[i
].if_defined
&&
1977 !get_variable(vars
, statuses
[i
].if_defined
))
1980 if (statuses
[i
].if_not_defined
&&
1981 get_variable(vars
, statuses
[i
].if_not_defined
))
1984 if (response
->request
.status
.status_code
== statuses
[i
].status
)
1988 if (i
== num_statuses
&& num_statuses
> 0)
1989 prev_pass
= pass
= 0;
1992 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
1994 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
1997 if (expect
->if_not_defined
&&
1998 get_variable(vars
, expect
->if_not_defined
))
2001 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2003 if ((found
&& expect
->not_expect
) ||
2004 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
2005 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
2006 (found
&& expect
->in_group
&&
2007 found
->group_tag
!= expect
->in_group
))
2009 if (expect
->define_no_match
)
2010 set_variable(vars
, expect
->define_no_match
, "1");
2011 else if (!expect
->define_match
)
2012 prev_pass
= pass
= 0;
2018 !with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2020 if (expect
->define_no_match
)
2021 set_variable(vars
, expect
->define_no_match
, "1");
2022 else if (!expect
->define_match
)
2023 prev_pass
= pass
= 0;
2028 if (found
&& expect
->count
> 0 && found
->num_values
!= expect
->count
)
2030 if (expect
->define_no_match
)
2031 set_variable(vars
, expect
->define_no_match
, "1");
2032 else if (!expect
->define_match
)
2033 prev_pass
= pass
= 0;
2038 if (found
&& expect
->same_count_as
)
2040 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2043 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
2045 if (expect
->define_no_match
)
2046 set_variable(vars
, expect
->define_no_match
, "1");
2047 else if (!expect
->define_match
)
2048 prev_pass
= pass
= 0;
2054 if (found
&& expect
->define_match
)
2055 set_variable(vars
, expect
->define_match
, "1");
2057 if (found
&& expect
->define_value
)
2059 _ippAttrString(found
, token
, sizeof(token
));
2060 set_variable(vars
, expect
->define_value
, token
);
2066 if (Output
== _CUPS_OUTPUT_PLIST
)
2068 puts("<key>Successful</key>");
2069 puts(prev_pass
? "<true />" : "<false />");
2070 puts("<key>StatusCode</key>");
2071 print_xml_string("string", ippErrorString(cupsLastError()));
2072 puts("<key>ResponseAttributes</key>");
2074 for (attrptr
= response
? response
->attrs
: NULL
;
2076 attrptr
= attrptr
->next
)
2077 print_attr(attrptr
);
2080 else if (Output
== _CUPS_OUTPUT_TEST
)
2082 puts(prev_pass
? "PASS]" : "FAIL]");
2084 if (Verbosity
&& response
)
2086 printf(" RECEIVED: %lu bytes in response\n",
2087 (unsigned long)ippLength(response
));
2088 printf(" status-code = %x (%s)\n", cupsLastError(),
2089 ippErrorString(cupsLastError()));
2091 for (attrptr
= response
->attrs
;
2093 attrptr
= attrptr
->next
)
2095 print_attr(attrptr
);
2099 else if (!prev_pass
)
2100 fprintf(stderr
, "%s\n", cupsLastErrorString());
2102 if (prev_pass
&& Output
!= _CUPS_OUTPUT_PLIST
&&
2103 Output
!= _CUPS_OUTPUT_QUIET
&& !Verbosity
&& num_displayed
> 0)
2105 if (Output
>= _CUPS_OUTPUT_LIST
)
2107 size_t width
; /* Length of value */
2110 for (i
= 0; i
< num_displayed
; i
++)
2112 widths
[i
] = strlen(displayed
[i
]);
2114 for (attrptr
= ippFindAttribute(response
, displayed
[i
], IPP_TAG_ZERO
);
2116 attrptr
= ippFindNextAttribute(response
, displayed
[i
],
2119 width
= _ippAttrString(attrptr
, NULL
, 0);
2120 if (width
> widths
[i
])
2125 if (Output
== _CUPS_OUTPUT_CSV
)
2126 print_csv(NULL
, num_displayed
, displayed
, widths
);
2128 print_line(NULL
, num_displayed
, displayed
, widths
);
2130 attrptr
= response
->attrs
;
2134 while (attrptr
&& attrptr
->group_tag
<= IPP_TAG_OPERATION
)
2135 attrptr
= attrptr
->next
;
2139 if (Output
== _CUPS_OUTPUT_CSV
)
2140 print_csv(attrptr
, num_displayed
, displayed
, widths
);
2142 print_line(attrptr
, num_displayed
, displayed
, widths
);
2144 while (attrptr
&& attrptr
->group_tag
> IPP_TAG_OPERATION
)
2145 attrptr
= attrptr
->next
;
2151 for (attrptr
= response
->attrs
;
2153 attrptr
= attrptr
->next
)
2157 for (i
= 0; i
< num_displayed
; i
++)
2159 if (!strcmp(displayed
[i
], attrptr
->name
))
2161 print_attr(attrptr
);
2169 else if (!prev_pass
)
2171 if (Output
== _CUPS_OUTPUT_PLIST
)
2173 puts("<key>Errors</key>");
2177 if (http
->version
!= HTTP_1_1
)
2178 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
2179 http
->version
% 100);
2182 print_test_error("IPP request failed with status %s (%s)",
2183 ippErrorString(cupsLastError()),
2184 cupsLastErrorString());
2188 (response
->request
.status
.version
[0] != (version
/ 10) ||
2189 response
->request
.status
.version
[1] != (version
% 10)))
2190 print_test_error("Bad version %d.%d in response - expected %d.%d "
2191 "(RFC 2911 section 3.1.8).",
2192 response
->request
.status
.version
[0],
2193 response
->request
.status
.version
[1],
2194 version
/ 10, version
% 10);
2196 if (response
->request
.status
.request_id
!= request_id
)
2197 print_test_error("Bad request ID %d in response - expected %d "
2198 "(RFC 2911 section 3.1.1)",
2199 response
->request
.status
.request_id
, request_id
);
2201 attrptr
= response
->attrs
;
2203 print_test_error("Missing first attribute \"attributes-charset "
2204 "(charset)\" in group operation-attributes-tag "
2205 "(RFC 2911 section 3.1.4).");
2208 if (!attrptr
->name
||
2209 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
2210 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2211 attrptr
->num_values
!= 1 ||
2212 strcmp(attrptr
->name
, "attributes-charset"))
2213 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2214 "expected \"attributes-charset (charset)\" in "
2215 "group operation-attributes-tag (RFC 2911 section "
2217 attrptr
->name
? attrptr
->name
: "(null)",
2218 attrptr
->num_values
> 1 ? "1setOf " : "",
2219 ippTagString(attrptr
->value_tag
),
2220 ippTagString(attrptr
->group_tag
));
2222 attrptr
= attrptr
->next
;
2224 print_test_error("Missing second attribute \"attributes-natural-"
2225 "language (naturalLanguage)\" in group "
2226 "operation-attributes-tag (RFC 2911 section "
2228 else if (!attrptr
->name
||
2229 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
2230 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
2231 attrptr
->num_values
!= 1 ||
2232 strcmp(attrptr
->name
, "attributes-natural-language"))
2233 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2234 "expected \"attributes-natural-language "
2235 "(naturalLanguage)\" in group "
2236 "operation-attributes-tag (RFC 2911 section "
2238 attrptr
->name
? attrptr
->name
: "(null)",
2239 attrptr
->num_values
> 1 ? "1setOf " : "",
2240 ippTagString(attrptr
->value_tag
),
2241 ippTagString(attrptr
->group_tag
));
2244 if ((attrptr
= ippFindAttribute(response
, "status-message",
2245 IPP_TAG_ZERO
)) != NULL
)
2247 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2248 print_test_error("status-message (text(255)) has wrong value tag "
2249 "%s (RFC 2911 section 3.1.6.2).",
2250 ippTagString(attrptr
->value_tag
));
2251 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2252 print_test_error("status-message (text(255)) has wrong group tag "
2253 "%s (RFC 2911 section 3.1.6.2).",
2254 ippTagString(attrptr
->group_tag
));
2255 if (attrptr
->num_values
!= 1)
2256 print_test_error("status-message (text(255)) has %d values "
2257 "(RFC 2911 section 3.1.6.2).",
2258 attrptr
->num_values
);
2259 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2260 strlen(attrptr
->values
[0].string
.text
) > 255)
2261 print_test_error("status-message (text(255)) has bad length %d"
2262 " (RFC 2911 section 3.1.6.2).",
2263 (int)strlen(attrptr
->values
[0].string
.text
));
2266 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
2267 IPP_TAG_ZERO
)) != NULL
)
2269 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
2270 print_test_error("detailed-status-message (text(MAX)) has wrong "
2271 "value tag %s (RFC 2911 section 3.1.6.3).",
2272 ippTagString(attrptr
->value_tag
));
2273 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
2274 print_test_error("detailed-status-message (text(MAX)) has wrong "
2275 "group tag %s (RFC 2911 section 3.1.6.3).",
2276 ippTagString(attrptr
->group_tag
));
2277 if (attrptr
->num_values
!= 1)
2278 print_test_error("detailed-status-message (text(MAX)) has %d values"
2279 " (RFC 2911 section 3.1.6.3).",
2280 attrptr
->num_values
);
2281 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
2282 strlen(attrptr
->values
[0].string
.text
) > 1023)
2283 print_test_error("detailed-status-message (text(MAX)) has bad "
2284 "length %d (RFC 2911 section 3.1.6.3).",
2285 (int)strlen(attrptr
->values
[0].string
.text
));
2288 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
2290 attrptr
= attrptr
->next
)
2292 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
2293 print_test_error("Attribute groups out of order (%s < %s)",
2294 ippTagString(attrptr
->group_tag
),
2295 ippTagString(group
));
2297 validate_attr(attrptr
, 1);
2300 for (i
= 0; i
< num_statuses
; i
++)
2302 if (statuses
[i
].if_defined
&&
2303 !get_variable(vars
, statuses
[i
].if_defined
))
2306 if (statuses
[i
].if_not_defined
&&
2307 get_variable(vars
, statuses
[i
].if_not_defined
))
2310 if (response
->request
.status
.status_code
== statuses
[i
].status
)
2314 if (i
== num_statuses
&& num_statuses
> 0)
2316 print_test_error("Bad status-code (%s)",
2317 ippErrorString(cupsLastError()));
2318 print_test_error("status-message=\"%s\"", cupsLastErrorString());
2321 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2323 if (expect
->define_match
|| expect
->define_no_match
)
2326 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
2329 if (expect
->if_not_defined
&&
2330 get_variable(vars
, expect
->if_not_defined
))
2333 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
2335 if (found
&& expect
->not_expect
)
2336 print_test_error("NOT EXPECTED: %s", expect
->name
);
2337 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
2338 print_test_error("EXPECTED: %s", expect
->name
);
2341 if (!expect_matches(expect
, found
->value_tag
))
2342 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2343 expect
->name
, expect
->of_type
,
2344 ippTagString(found
->value_tag
));
2346 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
2347 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2348 expect
->name
, ippTagString(expect
->in_group
),
2349 ippTagString(found
->group_tag
));
2351 if (!with_value(expect
->with_value
, expect
->with_regex
, found
, 0))
2353 if (expect
->with_regex
)
2354 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2355 expect
->name
, expect
->with_value
);
2357 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2358 expect
->name
, expect
->with_value
);
2360 with_value(expect
->with_value
, expect
->with_regex
, found
, 1);
2363 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
2365 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
2366 expect
->count
, found
->num_values
);
2369 if (expect
->same_count_as
)
2371 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
2375 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2376 "(not returned)", expect
->name
,
2377 found
->num_values
, expect
->same_count_as
);
2378 else if (attrptr
->num_values
!= found
->num_values
)
2379 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2380 "(%d values)", expect
->name
, found
->num_values
,
2381 expect
->same_count_as
, attrptr
->num_values
);
2387 if (Output
== _CUPS_OUTPUT_PLIST
)
2391 if (Output
== _CUPS_OUTPUT_PLIST
)
2396 ippDelete(response
);
2399 for (i
= 0; i
< num_statuses
; i
++)
2401 if (statuses
[i
].if_defined
)
2402 free(statuses
[i
].if_defined
);
2403 if (statuses
[i
].if_not_defined
)
2404 free(statuses
[i
].if_not_defined
);
2408 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2411 if (expect
->of_type
)
2412 free(expect
->of_type
);
2413 if (expect
->same_count_as
)
2414 free(expect
->same_count_as
);
2415 if (expect
->if_defined
)
2416 free(expect
->if_defined
);
2417 if (expect
->if_not_defined
)
2418 free(expect
->if_not_defined
);
2419 if (expect
->with_value
)
2420 free(expect
->with_value
);
2421 if (expect
->define_match
)
2422 free(expect
->define_match
);
2423 if (expect
->define_no_match
)
2424 free(expect
->define_no_match
);
2425 if (expect
->define_value
)
2426 free(expect
->define_value
);
2430 for (i
= 0; i
< num_displayed
; i
++)
2434 if (!ignore_errors
&& !prev_pass
)
2445 ippDelete(response
);
2447 for (i
= 0; i
< num_statuses
; i
++)
2449 if (statuses
[i
].if_defined
)
2450 free(statuses
[i
].if_defined
);
2451 if (statuses
[i
].if_not_defined
)
2452 free(statuses
[i
].if_not_defined
);
2455 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
2458 if (expect
->of_type
)
2459 free(expect
->of_type
);
2460 if (expect
->same_count_as
)
2461 free(expect
->same_count_as
);
2462 if (expect
->if_defined
)
2463 free(expect
->if_defined
);
2464 if (expect
->if_not_defined
)
2465 free(expect
->if_not_defined
);
2466 if (expect
->with_value
)
2467 free(expect
->with_value
);
2468 if (expect
->define_match
)
2469 free(expect
->define_match
);
2470 if (expect
->define_no_match
)
2471 free(expect
->define_no_match
);
2472 if (expect
->define_value
)
2473 free(expect
->define_value
);
2476 for (i
= 0; i
< num_displayed
; i
++)
2484 * 'expand_variables()' - Expand variables in a string.
2488 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
2489 char *dst
, /* I - Destination string buffer */
2490 const char *src
, /* I - Source string */
2491 size_t dstsize
) /* I - Size of destination buffer */
2493 char *dstptr
, /* Pointer into destination */
2494 *dstend
, /* End of destination */
2495 temp
[256], /* Temporary string */
2496 *tempptr
; /* Pointer into temporary string */
2497 const char *value
; /* Value to substitute */
2501 dstend
= dst
+ dstsize
- 1;
2503 while (*src
&& dstptr
< dstend
)
2508 * Substitute a string/number...
2511 if (!strncmp(src
, "$$", 2))
2516 else if (!strncmp(src
, "$ENV[", 5))
2518 strlcpy(temp
, src
+ 5, sizeof(temp
));
2520 for (tempptr
= temp
; *tempptr
; tempptr
++)
2521 if (*tempptr
== ']')
2527 value
= getenv(temp
);
2528 src
+= tempptr
- temp
+ 5;
2532 strlcpy(temp
, src
+ 1, sizeof(temp
));
2534 for (tempptr
= temp
; *tempptr
; tempptr
++)
2535 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
2541 if (!strcmp(temp
, "uri"))
2543 else if (!strcmp(temp
, "filename"))
2544 value
= vars
->filename
;
2545 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
2546 value
= vars
->scheme
;
2547 else if (!strcmp(temp
, "username"))
2548 value
= vars
->userpass
;
2549 else if (!strcmp(temp
, "hostname"))
2550 value
= vars
->hostname
;
2551 else if (!strcmp(temp
, "port"))
2553 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
2556 else if (!strcmp(temp
, "resource"))
2557 value
= vars
->resource
;
2558 else if (!strcmp(temp
, "user"))
2561 value
= get_variable(vars
, temp
);
2563 src
+= tempptr
- temp
+ 1;
2573 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
2574 dstptr
+= strlen(dstptr
);
2586 * 'expect_matches()' - Return true if the tag matches the specification.
2589 static int /* O - 1 if matches, 0 otherwise */
2591 _cups_expect_t
*expect
, /* I - Expected attribute */
2592 ipp_tag_t value_tag
) /* I - Value tag for attribute */
2594 int match
; /* Match? */
2595 char *of_type
, /* Type name to match */
2596 *next
, /* Next name to match */
2597 sep
; /* Separator character */
2601 * If we don't expect a particular type, return immediately...
2604 if (!expect
->of_type
)
2608 * Parse the "of_type" value since the string can contain multiple attribute
2609 * types separated by "," or "|"...
2612 for (of_type
= expect
->of_type
, match
= 0; !match
&& *of_type
; of_type
= next
)
2615 * Find the next separator, and set it (temporarily) to nul if present.
2618 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
2620 if ((sep
= *next
) != '\0')
2624 * Support some meta-types to make it easier to write the test file.
2627 if (!strcmp(of_type
, "text"))
2628 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
2629 else if (!strcmp(of_type
, "name"))
2630 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
2631 else if (!strcmp(of_type
, "collection"))
2632 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
2634 match
= value_tag
== ippTagValue(of_type
);
2637 * Restore the separator if we have one...
2649 * 'get_collection()' - Get a collection value from the current test file.
2652 static ipp_t
* /* O - Collection value */
2653 get_collection(_cups_vars_t
*vars
, /* I - Variables */
2654 FILE *fp
, /* I - File to read from */
2655 int *linenum
) /* IO - Line number */
2657 char token
[1024], /* Token from file */
2658 temp
[1024], /* Temporary string */
2659 attr
[128]; /* Attribute name */
2660 ipp_tag_t value
; /* Current value type */
2661 ipp_t
*col
= ippNew(); /* Collection value */
2662 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
2665 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
2667 if (!strcmp(token
, "}"))
2669 else if (!strcmp(token
, "{") && lastcol
)
2672 * Another collection value
2675 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2676 /* Collection value */
2680 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
2684 * Reallocate memory...
2687 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
2688 (lastcol
->num_values
+ 1) *
2689 sizeof(ipp_value_t
))) == NULL
)
2691 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
2695 if (tempcol
!= lastcol
)
2698 * Reset pointers in the list...
2702 col
->prev
->next
= tempcol
;
2704 col
->attrs
= tempcol
;
2706 lastcol
= col
->current
= col
->last
= tempcol
;
2709 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
2710 lastcol
->num_values
++;
2715 else if (!strcasecmp(token
, "MEMBER"))
2723 if (!get_token(fp
, token
, sizeof(token
), linenum
))
2725 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
2729 if ((value
= ippTagValue(token
)) == IPP_TAG_ZERO
)
2731 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
2736 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
2738 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
2742 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
2744 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
2748 expand_variables(vars
, token
, temp
, sizeof(token
));
2752 case IPP_TAG_BOOLEAN
:
2753 if (!strcasecmp(token
, "true"))
2754 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
2756 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
2759 case IPP_TAG_INTEGER
:
2761 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
2764 case IPP_TAG_RESOLUTION
:
2766 int xres
, /* X resolution */
2767 yres
; /* Y resolution */
2768 char units
[6]; /* Units */
2770 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
2771 (strcasecmp(units
, "dpi") && strcasecmp(units
, "dpc") &&
2772 strcasecmp(units
, "other")))
2774 print_fatal_error("Bad resolution value \"%s\" on line %d.",
2779 if (!strcasecmp(units
, "dpi"))
2780 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2782 else if (!strcasecmp(units
, "dpc"))
2783 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2786 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2791 case IPP_TAG_RANGE
:
2793 int lowers
[4], /* Lower value */
2794 uppers
[4], /* Upper values */
2795 num_vals
; /* Number of values */
2798 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
2799 lowers
+ 0, uppers
+ 0,
2800 lowers
+ 1, uppers
+ 1,
2801 lowers
+ 2, uppers
+ 2,
2802 lowers
+ 3, uppers
+ 3);
2804 if ((num_vals
& 1) || num_vals
== 0)
2806 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
2811 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
2816 case IPP_TAG_BEGIN_COLLECTION
:
2817 if (!strcmp(token
, "{"))
2819 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2820 /* Collection value */
2824 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
2832 print_fatal_error("Bad collection value on line %d.", *linenum
);
2838 if (!strchr(token
, ','))
2839 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
2843 * Multiple string values...
2846 int num_values
; /* Number of values */
2847 char *values
[100], /* Values */
2848 *ptr
; /* Pointer to next value */
2854 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
2857 values
[num_values
] = ptr
;
2861 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
2862 NULL
, (const char **)values
);
2872 * If we get here there was a parse error; free memory and return.
2884 * 'get_filename()' - Get a filename based on the current test file.
2887 static char * /* O - Filename */
2888 get_filename(const char *testfile
, /* I - Current test file */
2889 char *dst
, /* I - Destination filename */
2890 const char *src
, /* I - Source filename */
2891 size_t dstsize
) /* I - Size of destination buffer */
2893 char *dstptr
; /* Pointer into destination */
2894 _cups_globals_t
*cg
= _cupsGlobals();
2898 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
2901 * Map <filename> to CUPS_DATADIR/ipptool/filename...
2904 snprintf(dst
, dstsize
, "%s/ipptool/%s", cg
->cups_datadir
, src
+ 1);
2905 dstptr
= dst
+ strlen(dst
) - 1;
2909 else if (*src
== '/' || !strchr(testfile
, '/'))
2912 * Use the path as-is...
2915 strlcpy(dst
, src
, dstsize
);
2920 * Make path relative to testfile...
2923 strlcpy(dst
, testfile
, dstsize
);
2924 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
2927 dstptr
= dst
; /* Should never happen */
2929 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
2937 * 'get_token()' - Get a token from a file.
2940 static char * /* O - Token from file or NULL on EOF */
2941 get_token(FILE *fp
, /* I - File to read from */
2942 char *buf
, /* I - Buffer to read into */
2943 int buflen
, /* I - Length of buffer */
2944 int *linenum
) /* IO - Current line number */
2946 int ch
, /* Character from file */
2947 quote
; /* Quoting character */
2948 char *bufptr
, /* Pointer into buffer */
2949 *bufend
; /* End of buffer */
2955 * Skip whitespace...
2958 while (isspace(ch
= getc(fp
)))
2970 else if (ch
== '\'' || ch
== '\"')
2973 * Quoted text or regular expression...
2978 bufend
= buf
+ buflen
- 1;
2980 while ((ch
= getc(fp
)) != EOF
)
2985 * Escape next character...
2988 if (bufptr
< bufend
)
2991 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
2994 else if (ch
== quote
)
2996 else if (bufptr
< bufend
)
3010 while ((ch
= getc(fp
)) != EOF
)
3019 * Whitespace delimited text...
3025 bufend
= buf
+ buflen
- 1;
3027 while ((ch
= getc(fp
)) != EOF
)
3028 if (isspace(ch
) || ch
== '#')
3030 else if (bufptr
< bufend
)
3035 else if (ch
== '\n')
3047 * 'get_variable()' - Get the value of a variable.
3050 static char * /* O - Value or NULL */
3051 get_variable(_cups_vars_t
*vars
, /* I - Variables */
3052 const char *name
) /* I - Variable name */
3054 _cups_var_t key
, /* Search key */
3055 *match
; /* Matching variable, if any */
3058 key
.name
= (char *)name
;
3059 match
= cupsArrayFind(vars
->vars
, &key
);
3061 return (match
? match
->value
: NULL
);
3066 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3070 static char * /* O - ISO 8601 date/time string */
3071 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
3073 unsigned year
= (date
[0] << 8) + date
[1];
3075 static char buffer
[255]; /* String buffer */
3078 if (date
[9] == 0 && date
[10] == 0)
3079 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
3080 year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
3082 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
3083 year
, date
[2], date
[3], date
[4], date
[5], date
[6],
3084 date
[8], date
[9], date
[10]);
3091 * 'password_cb()' - Password callback for authenticated tests.
3094 static const char * /* O - Password */
3095 password_cb(const char *prompt
) /* I - Prompt (unused) */
3104 * 'print_attr()' - Print an attribute on the screen.
3108 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
3110 int i
; /* Looping var */
3111 ipp_attribute_t
*colattr
; /* Collection attribute */
3114 if (Output
== _CUPS_OUTPUT_PLIST
)
3118 printf("<key>%s</key>\n<true />\n", ippTagString(attr
->group_tag
));
3122 print_xml_string("key", attr
->name
);
3123 if (attr
->num_values
> 1)
3126 else if (Output
== _CUPS_OUTPUT_TEST
)
3130 puts(" -- separator --");
3134 printf(" %s (%s%s) = ", attr
->name
,
3135 attr
->num_values
> 1 ? "1setOf " : "",
3136 ippTagString(attr
->value_tag
));
3139 switch (attr
->value_tag
)
3141 case IPP_TAG_INTEGER
:
3143 for (i
= 0; i
< attr
->num_values
; i
++)
3144 if (Output
== _CUPS_OUTPUT_PLIST
)
3145 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
3147 printf("%d ", attr
->values
[i
].integer
);
3150 case IPP_TAG_BOOLEAN
:
3151 for (i
= 0; i
< attr
->num_values
; i
++)
3152 if (Output
== _CUPS_OUTPUT_PLIST
)
3153 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
3154 else if (attr
->values
[i
].boolean
)
3155 fputs("true ", stdout
);
3157 fputs("false ", stdout
);
3160 case IPP_TAG_RANGE
:
3161 for (i
= 0; i
< attr
->num_values
; i
++)
3162 if (Output
== _CUPS_OUTPUT_PLIST
)
3163 printf("<dict><key>lower</key><integer>%d</integer>"
3164 "<key>upper</key><integer>%d</integer></dict>\n",
3165 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
3167 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3168 attr
->values
[i
].range
.upper
);
3171 case IPP_TAG_RESOLUTION
:
3172 for (i
= 0; i
< attr
->num_values
; i
++)
3173 if (Output
== _CUPS_OUTPUT_PLIST
)
3174 printf("<dict><key>xres</key><integer>%d</integer>"
3175 "<key>yres</key><integer>%d</integer>"
3176 "<key>units</key><string>%s</string></dict>\n",
3177 attr
->values
[i
].resolution
.xres
,
3178 attr
->values
[i
].resolution
.yres
,
3179 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3182 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3183 attr
->values
[i
].resolution
.yres
,
3184 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3189 for (i
= 0; i
< attr
->num_values
; i
++)
3190 if (Output
== _CUPS_OUTPUT_PLIST
)
3191 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
3193 printf("%s ", iso_date(attr
->values
[i
].date
));
3196 case IPP_TAG_STRING
:
3199 case IPP_TAG_KEYWORD
:
3200 case IPP_TAG_CHARSET
:
3202 case IPP_TAG_MIMETYPE
:
3203 case IPP_TAG_LANGUAGE
:
3204 for (i
= 0; i
< attr
->num_values
; i
++)
3205 if (Output
== _CUPS_OUTPUT_PLIST
)
3206 print_xml_string("string", attr
->values
[i
].string
.text
);
3208 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3211 case IPP_TAG_TEXTLANG
:
3212 case IPP_TAG_NAMELANG
:
3213 for (i
= 0; i
< attr
->num_values
; i
++)
3214 if (Output
== _CUPS_OUTPUT_PLIST
)
3216 fputs("<dict><key>language</key><string>", stdout
);
3217 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
3218 fputs("</string><key>string</key><string>", stdout
);
3219 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
3220 puts("</string></dict>");
3223 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3224 attr
->values
[i
].string
.charset
);
3227 case IPP_TAG_BEGIN_COLLECTION
:
3228 for (i
= 0; i
< attr
->num_values
; i
++)
3230 if (Output
== _CUPS_OUTPUT_PLIST
)
3233 for (colattr
= attr
->values
[i
].collection
->attrs
;
3235 colattr
= colattr
->next
)
3236 print_attr(colattr
);
3244 print_col(attr
->values
[i
].collection
);
3250 if (Output
== _CUPS_OUTPUT_PLIST
)
3251 printf("<string><<%s>></string>\n",
3252 ippTagString(attr
->value_tag
));
3254 fputs(ippTagString(attr
->value_tag
), stdout
);
3258 if (Output
== _CUPS_OUTPUT_PLIST
)
3260 if (attr
->num_values
> 1)
3269 * 'print_col()' - Print a collection attribute on the screen.
3273 print_col(ipp_t
*col
) /* I - Collection attribute to print */
3275 int i
; /* Looping var */
3276 ipp_attribute_t
*attr
; /* Current attribute in collection */
3279 fputs("{ ", stdout
);
3280 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
3282 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
3283 ippTagString(attr
->value_tag
));
3285 switch (attr
->value_tag
)
3287 case IPP_TAG_INTEGER
:
3289 for (i
= 0; i
< attr
->num_values
; i
++)
3290 printf("%d ", attr
->values
[i
].integer
);
3293 case IPP_TAG_BOOLEAN
:
3294 for (i
= 0; i
< attr
->num_values
; i
++)
3295 if (attr
->values
[i
].boolean
)
3301 case IPP_TAG_NOVALUE
:
3305 case IPP_TAG_RANGE
:
3306 for (i
= 0; i
< attr
->num_values
; i
++)
3307 printf("%d-%d ", attr
->values
[i
].range
.lower
,
3308 attr
->values
[i
].range
.upper
);
3311 case IPP_TAG_RESOLUTION
:
3312 for (i
= 0; i
< attr
->num_values
; i
++)
3313 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
3314 attr
->values
[i
].resolution
.yres
,
3315 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
3319 case IPP_TAG_STRING
:
3322 case IPP_TAG_KEYWORD
:
3323 case IPP_TAG_CHARSET
:
3325 case IPP_TAG_MIMETYPE
:
3326 case IPP_TAG_LANGUAGE
:
3327 for (i
= 0; i
< attr
->num_values
; i
++)
3328 printf("\"%s\" ", attr
->values
[i
].string
.text
);
3331 case IPP_TAG_TEXTLANG
:
3332 case IPP_TAG_NAMELANG
:
3333 for (i
= 0; i
< attr
->num_values
; i
++)
3334 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
3335 attr
->values
[i
].string
.charset
);
3338 case IPP_TAG_BEGIN_COLLECTION
:
3339 for (i
= 0; i
< attr
->num_values
; i
++)
3341 print_col(attr
->values
[i
].collection
);
3347 break; /* anti-compiler-warning-code */
3356 * 'print_csv()' - Print a line of CSV text.
3361 ipp_attribute_t
*attr
, /* I - First attribute for line */
3362 int num_displayed
, /* I - Number of attributes to display */
3363 char **displayed
, /* I - Attributes to display */
3364 size_t *widths
) /* I - Column widths */
3366 int i
; /* Looping var */
3367 size_t maxlength
; /* Max length of all columns */
3368 char *buffer
, /* String buffer */
3369 *bufptr
; /* Pointer into buffer */
3370 ipp_attribute_t
*current
; /* Current attribute */
3374 * Get the maximum string length we have to show and allocate...
3377 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3378 if (widths
[i
] > maxlength
)
3379 maxlength
= widths
[i
];
3383 if ((buffer
= malloc(maxlength
)) == NULL
)
3387 * Loop through the attributes to display...
3392 for (i
= 0; i
< num_displayed
; i
++)
3399 for (current
= attr
; current
; current
= current
->next
)
3403 else if (!strcmp(current
->name
, displayed
[i
]))
3405 _ippAttrString(current
, buffer
, maxlength
);
3410 if (strchr(buffer
, ',') != NULL
|| strchr(buffer
, '\"') != NULL
||
3411 strchr(buffer
, '\\') != NULL
)
3414 for (bufptr
= buffer
; *bufptr
; bufptr
++)
3416 if (*bufptr
== '\\' || *bufptr
== '\"')
3423 fputs(buffer
, stdout
);
3429 for (i
= 0; i
< num_displayed
; i
++)
3434 fputs(displayed
[i
], stdout
);
3444 * 'print_fatal_error()' - Print a fatal error message.
3448 print_fatal_error(const char *s
, /* I - Printf-style format string */
3449 ...) /* I - Additional arguments as needed */
3451 char buffer
[10240]; /* Format buffer */
3452 va_list ap
; /* Pointer to arguments */
3456 * Format the error message...
3460 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3467 if (Output
== _CUPS_OUTPUT_PLIST
)
3470 print_xml_trailer(0, buffer
);
3473 _cupsLangPrintf(stderr
, "ipptool: %s\n", buffer
);
3478 * 'print_line()' - Print a line of formatted or CSV text.
3483 ipp_attribute_t
*attr
, /* I - First attribute for line */
3484 int num_displayed
, /* I - Number of attributes to display */
3485 char **displayed
, /* I - Attributes to display */
3486 size_t *widths
) /* I - Column widths */
3488 int i
; /* Looping var */
3489 size_t maxlength
; /* Max length of all columns */
3490 char *buffer
; /* String buffer */
3491 ipp_attribute_t
*current
; /* Current attribute */
3495 * Get the maximum string length we have to show and allocate...
3498 for (i
= 1, maxlength
= widths
[0]; i
< num_displayed
; i
++)
3499 if (widths
[i
] > maxlength
)
3500 maxlength
= widths
[i
];
3504 if ((buffer
= malloc(maxlength
)) == NULL
)
3508 * Loop through the attributes to display...
3513 for (i
= 0; i
< num_displayed
; i
++)
3520 for (current
= attr
; current
; current
= current
->next
)
3524 else if (!strcmp(current
->name
, displayed
[i
]))
3526 _ippAttrString(current
, buffer
, maxlength
);
3531 printf("%*s", (int)-widths
[i
], buffer
);
3537 for (i
= 0; i
< num_displayed
; i
++)
3542 printf("%*s", (int)-widths
[i
], displayed
[i
]);
3546 for (i
= 0; i
< num_displayed
; i
++)
3551 memset(buffer
, '-', widths
[i
]);
3552 buffer
[widths
[i
]] = '\0';
3553 fputs(buffer
, stdout
);
3563 * 'print_test_error()' - Print a test error message.
3567 print_test_error(const char *s
, /* I - Printf-style format string */
3568 ...) /* I - Additional arguments as needed */
3570 char buffer
[10240]; /* Format buffer */
3571 va_list ap
; /* Pointer to arguments */
3575 * Format the error message...
3579 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
3586 if (Output
== _CUPS_OUTPUT_PLIST
)
3587 print_xml_string("string", buffer
);
3589 printf(" %s\n", buffer
);
3594 * 'print_xml_header()' - Print a standard XML plist header.
3598 print_xml_header(void)
3602 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
3603 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
3604 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
3605 puts("<plist version=\"1.0\">");
3607 puts("<key>Transfer</key>");
3608 printf("<string>%s</string>\n",
3609 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
3610 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
3611 puts("<key>Tests</key>");
3620 * 'print_xml_string()' - Print an XML string with escaping.
3624 print_xml_string(const char *element
, /* I - Element name or NULL */
3625 const char *s
) /* I - String to print */
3628 printf("<%s>", element
);
3633 fputs("&", stdout
);
3635 fputs("<", stdout
);
3637 fputs(">", stdout
);
3645 printf("</%s>\n", element
);
3650 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
3654 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
3655 const char *message
) /* I - Error message or NULL */
3660 puts("<key>Successful</key>");
3661 puts(success
? "<true />" : "<false />");
3664 puts("<key>ErrorMessage</key>");
3665 print_xml_string("string", message
);
3676 * 'set_variable()' - Set a variable value.
3680 set_variable(_cups_vars_t
*vars
, /* I - Variables */
3681 const char *name
, /* I - Variable name */
3682 const char *value
) /* I - Value string */
3684 _cups_var_t key
, /* Search key */
3685 *var
; /* New variable */
3688 key
.name
= (char *)name
;
3689 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
3692 var
->value
= strdup(value
);
3694 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
3696 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
3701 var
->name
= strdup(name
);
3702 var
->value
= strdup(value
);
3704 cupsArrayAdd(vars
->vars
, var
);
3710 * 'timeout_cb()' - Handle HTTP timeouts.
3713 static int /* O - 1 to continue, 0 to cancel */
3714 timeout_cb(http_t
*http
, /* I - Connection to server (unused) */
3715 void *user_data
) /* I - User data (unused) */
3725 * 'usage()' - Show program usage.
3731 _cupsLangPuts(stderr
,
3732 _("Usage: ipptool [options] URI filename [ ... "
3737 "-C Send requests using chunking (default)\n"
3738 "-E Test with TLS encryption.\n"
3739 "-I Ignore errors\n"
3740 "-L Send requests using content-length\n"
3741 "-S Test with SSL encryption.\n"
3742 "-T Set the receive/send timeout in seconds.\n"
3743 "-V version Set default IPP version.\n"
3744 "-X Produce XML plist instead of plain text.\n"
3745 "-d name=value Define variable.\n"
3746 "-f filename Set default request filename.\n"
3747 "-i seconds Repeat the last file with the given time "
3749 "-n count Repeat the last file the given number of "
3751 "-q Be quiet - no output except errors.\n"
3752 "-t Produce a test report.\n"
3753 "-v Show all attributes sent and received.\n"));
3760 * 'validate_attr()' - Determine whether an attribute is valid.
3763 static int /* O - 1 if valid, 0 otherwise */
3764 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
3765 int print
) /* I - 1 = report issues to stdout */
3767 int i
; /* Looping var */
3768 char scheme
[64], /* Scheme from URI */
3769 userpass
[256], /* Username/password from URI */
3770 hostname
[256], /* Hostname from URI */
3771 resource
[1024]; /* Resource from URI */
3772 int port
, /* Port number from URI */
3773 uri_status
, /* URI separation status */
3774 valid
= 1; /* Is the attribute valid? */
3775 const char *ptr
; /* Pointer into string */
3776 ipp_attribute_t
*colattr
; /* Collection attribute */
3777 regex_t re
; /* Regular expression */
3778 ipp_uchar_t
*date
; /* Current date value */
3789 * Validate the attribute name.
3792 for (ptr
= attr
->name
; *ptr
; ptr
++)
3793 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
3796 if (*ptr
|| ptr
== attr
->name
)
3801 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
3802 "2911 section 4.1.3).", attr
->name
);
3805 if ((ptr
- attr
->name
) > 255)
3810 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
3811 "section 4.1.3).", attr
->name
);
3814 switch (attr
->value_tag
)
3816 case IPP_TAG_INTEGER
:
3819 case IPP_TAG_BOOLEAN
:
3820 for (i
= 0; i
< attr
->num_values
; i
++)
3822 if (attr
->values
[i
].boolean
!= 0 &&
3823 attr
->values
[i
].boolean
!= 1)
3828 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
3829 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
3837 for (i
= 0; i
< attr
->num_values
; i
++)
3839 if (attr
->values
[i
].integer
< 1)
3844 print_test_error("\"%s\": Bad enum value %d - out of range "
3845 "(RFC 2911 section 4.1.4).", attr
->name
,
3846 attr
->values
[i
].integer
);
3853 case IPP_TAG_STRING
:
3854 for (i
= 0; i
< attr
->num_values
; i
++)
3856 if (attr
->values
[i
].unknown
.length
> 1023)
3861 print_test_error("\"%s\": Bad octetString value - bad length %d "
3862 "(RFC 2911 section 4.1.10).", attr
->name
,
3863 attr
->values
[i
].unknown
.length
);
3871 for (i
= 0; i
< attr
->num_values
; i
++)
3873 date
= attr
->values
[i
].date
;
3875 if (date
[2] < 1 || date
[2] > 12)
3880 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
3881 "section 4.1.13).", attr
->name
, date
[2]);
3886 if (date
[3] < 1 || date
[3] > 31)
3891 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
3892 "section 4.1.13).", attr
->name
, date
[3]);
3902 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
3903 "section 4.1.13).", attr
->name
, date
[4]);
3913 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
3914 "section 4.1.13).", attr
->name
, date
[5]);
3924 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
3925 "section 4.1.13).", attr
->name
, date
[6]);
3935 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
3936 "section 4.1.13).", attr
->name
, date
[7]);
3941 if (date
[8] != '-' && date
[8] != '+')
3946 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
3947 "section 4.1.13).", attr
->name
, date
[8]);
3957 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
3958 "section 4.1.13).", attr
->name
, date
[9]);
3968 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
3969 "section 4.1.13).", attr
->name
, date
[10]);
3976 case IPP_TAG_RESOLUTION
:
3977 for (i
= 0; i
< attr
->num_values
; i
++)
3979 if (attr
->values
[i
].resolution
.xres
<= 0)
3984 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
3985 "feed resolution must be positive (RFC 2911 "
3986 "section 4.1.13).", attr
->name
,
3987 attr
->values
[i
].resolution
.xres
,
3988 attr
->values
[i
].resolution
.yres
,
3989 attr
->values
[i
].resolution
.units
==
3990 IPP_RES_PER_INCH
? "dpi" :
3991 attr
->values
[i
].resolution
.units
==
3992 IPP_RES_PER_CM
? "dpc" : "unknown");
3997 if (attr
->values
[i
].resolution
.yres
<= 0)
4002 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4003 "resolution must be positive (RFC 2911 section "
4004 "4.1.13).", attr
->name
,
4005 attr
->values
[i
].resolution
.xres
,
4006 attr
->values
[i
].resolution
.yres
,
4007 attr
->values
[i
].resolution
.units
==
4008 IPP_RES_PER_INCH
? "dpi" :
4009 attr
->values
[i
].resolution
.units
==
4010 IPP_RES_PER_CM
? "dpc" : "unknown");
4015 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
4016 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
4021 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4022 "units value (RFC 2911 section 4.1.13).",
4023 attr
->name
, attr
->values
[i
].resolution
.xres
,
4024 attr
->values
[i
].resolution
.yres
,
4025 attr
->values
[i
].resolution
.units
==
4026 IPP_RES_PER_INCH
? "dpi" :
4027 attr
->values
[i
].resolution
.units
==
4028 IPP_RES_PER_CM
? "dpc" : "unknown");
4035 case IPP_TAG_RANGE
:
4036 for (i
= 0; i
< attr
->num_values
; i
++)
4038 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
4043 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4044 "greater than upper (RFC 2911 section 4.1.13).",
4045 attr
->name
, attr
->values
[i
].range
.lower
,
4046 attr
->values
[i
].range
.upper
);
4053 case IPP_TAG_BEGIN_COLLECTION
:
4054 for (i
= 0; i
< attr
->num_values
; i
++)
4056 for (colattr
= attr
->values
[i
].collection
->attrs
;
4058 colattr
= colattr
->next
)
4060 if (!validate_attr(colattr
, 0))
4067 if (colattr
&& print
)
4069 print_test_error("\"%s\": Bad collection value.", attr
->name
);
4073 validate_attr(colattr
, print
);
4074 colattr
= colattr
->next
;
4081 case IPP_TAG_TEXTLANG
:
4082 for (i
= 0; i
< attr
->num_values
; i
++)
4084 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4086 if ((*ptr
& 0xe0) == 0xc0)
4089 if ((*ptr
& 0xc0) != 0x80)
4092 else if ((*ptr
& 0xf0) == 0xe0)
4095 if ((*ptr
& 0xc0) != 0x80)
4098 if ((*ptr
& 0xc0) != 0x80)
4101 else if ((*ptr
& 0xf8) == 0xf0)
4104 if ((*ptr
& 0xc0) != 0x80)
4107 if ((*ptr
& 0xc0) != 0x80)
4110 if ((*ptr
& 0xc0) != 0x80)
4113 else if (*ptr
& 0x80)
4122 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4123 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
4124 attr
->values
[i
].string
.text
);
4129 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4134 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4135 "(RFC 2911 section 4.1.1).", attr
->name
,
4136 attr
->values
[i
].string
.text
,
4137 (int)strlen(attr
->values
[i
].string
.text
));
4145 case IPP_TAG_NAMELANG
:
4146 for (i
= 0; i
< attr
->num_values
; i
++)
4148 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4150 if ((*ptr
& 0xe0) == 0xc0)
4153 if ((*ptr
& 0xc0) != 0x80)
4156 else if ((*ptr
& 0xf0) == 0xe0)
4159 if ((*ptr
& 0xc0) != 0x80)
4162 if ((*ptr
& 0xc0) != 0x80)
4165 else if ((*ptr
& 0xf8) == 0xf0)
4168 if ((*ptr
& 0xc0) != 0x80)
4171 if ((*ptr
& 0xc0) != 0x80)
4174 if ((*ptr
& 0xc0) != 0x80)
4177 else if (*ptr
& 0x80)
4186 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4187 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
4188 attr
->values
[i
].string
.text
);
4193 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
4198 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4199 "(RFC 2911 section 4.1.2).", attr
->name
,
4200 attr
->values
[i
].string
.text
,
4201 (int)strlen(attr
->values
[i
].string
.text
));
4208 case IPP_TAG_KEYWORD
:
4209 for (i
= 0; i
< attr
->num_values
; i
++)
4211 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4212 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
4216 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4221 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4222 "character (RFC 2911 section 4.1.3).",
4223 attr
->name
, attr
->values
[i
].string
.text
);
4228 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
4233 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4234 "length %d (RFC 2911 section 4.1.3).",
4235 attr
->name
, attr
->values
[i
].string
.text
,
4236 (int)strlen(attr
->values
[i
].string
.text
));
4244 for (i
= 0; i
< attr
->num_values
; i
++)
4246 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
4247 attr
->values
[i
].string
.text
,
4248 scheme
, sizeof(scheme
),
4249 userpass
, sizeof(userpass
),
4250 hostname
, sizeof(hostname
),
4251 &port
, resource
, sizeof(resource
));
4253 if (uri_status
< HTTP_URI_OK
)
4258 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4259 "(RFC 2911 section 4.1.5).", attr
->name
,
4260 attr
->values
[i
].string
.text
,
4261 URIStatusStrings
[uri_status
-
4262 HTTP_URI_OVERFLOW
]);
4267 if (strlen(attr
->values
[i
].string
.text
) > 1023)
4272 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4273 "(RFC 2911 section 4.1.5).", attr
->name
,
4274 attr
->values
[i
].string
.text
,
4275 (int)strlen(attr
->values
[i
].string
.text
));
4282 case IPP_TAG_URISCHEME
:
4283 for (i
= 0; i
< attr
->num_values
; i
++)
4285 ptr
= attr
->values
[i
].string
.text
;
4286 if (islower(*ptr
& 255))
4288 for (ptr
++; *ptr
; ptr
++)
4289 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
4290 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
4294 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4299 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4300 "characters (RFC 2911 section 4.1.6).",
4301 attr
->name
, attr
->values
[i
].string
.text
);
4306 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
4311 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4312 "length %d (RFC 2911 section 4.1.6).",
4313 attr
->name
, attr
->values
[i
].string
.text
,
4314 (int)strlen(attr
->values
[i
].string
.text
));
4321 case IPP_TAG_CHARSET
:
4322 for (i
= 0; i
< attr
->num_values
; i
++)
4324 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
4325 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
4326 isspace(*ptr
& 255))
4329 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
4334 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4335 "characters (RFC 2911 section 4.1.7).",
4336 attr
->name
, attr
->values
[i
].string
.text
);
4341 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
4346 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4347 "length %d (RFC 2911 section 4.1.7).",
4348 attr
->name
, attr
->values
[i
].string
.text
,
4349 (int)strlen(attr
->values
[i
].string
.text
));
4356 case IPP_TAG_LANGUAGE
:
4358 * The following regular expression is derived from the ABNF for
4359 * language tags in RFC 4646. All I can say is that this is the
4360 * easiest way to check the values...
4363 if ((i
= regcomp(&re
,
4365 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4367 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4368 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4369 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4370 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4371 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4373 "x(-[a-z0-9]{1,8})+" /* privateuse */
4375 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
4377 REG_NOSUB
| REG_EXTENDED
)) != 0)
4379 char temp
[256]; /* Temporary error string */
4381 regerror(i
, &re
, temp
, sizeof(temp
));
4382 print_fatal_error("Unable to compile naturalLanguage regular "
4383 "expression: %s.", temp
);
4386 for (i
= 0; i
< attr
->num_values
; i
++)
4388 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4393 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4394 "characters (RFC 2911 section 4.1.8).",
4395 attr
->name
, attr
->values
[i
].string
.text
);
4400 if (strlen(attr
->values
[i
].string
.text
) > 63)
4405 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4406 "length %d (RFC 2911 section 4.1.8).",
4407 attr
->name
, attr
->values
[i
].string
.text
,
4408 (int)strlen(attr
->values
[i
].string
.text
));
4417 case IPP_TAG_MIMETYPE
:
4419 * The following regular expression is derived from the ABNF for
4420 * language tags in RFC 2045 and 4288. All I can say is that this is
4421 * the easiest way to check the values...
4424 if ((i
= regcomp(&re
,
4426 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
4428 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
4429 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
4430 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
4433 REG_NOSUB
| REG_EXTENDED
)) != 0)
4435 char temp
[256]; /* Temporary error string */
4437 regerror(i
, &re
, temp
, sizeof(temp
));
4438 print_fatal_error("Unable to compile mimeMediaType regular "
4439 "expression: %s.", temp
);
4442 for (i
= 0; i
< attr
->num_values
; i
++)
4444 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4449 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4450 "characters (RFC 2911 section 4.1.9).",
4451 attr
->name
, attr
->values
[i
].string
.text
);
4456 if (strlen(attr
->values
[i
].string
.text
) > 255)
4461 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4462 "length %d (RFC 2911 section 4.1.9).",
4463 attr
->name
, attr
->values
[i
].string
.text
,
4464 (int)strlen(attr
->values
[i
].string
.text
));
4480 * 'with_value()' - Test a WITH-VALUE predicate.
4483 static int /* O - 1 on match, 0 on non-match */
4484 with_value(char *value
, /* I - Value string */
4485 int regex
, /* I - Value is a regular expression */
4486 ipp_attribute_t
*attr
, /* I - Attribute to compare */
4487 int report
) /* I - 1 = report failures */
4489 int i
; /* Looping var */
4490 char *valptr
; /* Pointer into value */
4494 * NULL matches everything.
4497 if (!value
|| !*value
)
4501 * Compare the value string to the attribute value.
4504 switch (attr
->value_tag
)
4506 case IPP_TAG_INTEGER
:
4508 for (i
= 0; i
< attr
->num_values
; i
++)
4510 char op
, /* Comparison operator */
4511 *nextptr
; /* Next pointer */
4512 int intvalue
; /* Integer value */
4516 if (!strncmp(valptr
, "no-value,", 9))
4519 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
4520 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
4521 *valptr
== '=' || *valptr
== '>')
4524 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
4526 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
4534 intvalue
= strtol(valptr
, &nextptr
, 0);
4535 if (nextptr
== valptr
)
4542 if (attr
->values
[i
].integer
== intvalue
)
4546 if (attr
->values
[i
].integer
< intvalue
)
4550 if (attr
->values
[i
].integer
> intvalue
)
4559 for (i
= 0; i
< attr
->num_values
; i
++)
4560 print_test_error("GOT: %s=%d", attr
->name
, attr
->values
[i
].integer
);
4564 case IPP_TAG_BOOLEAN
:
4565 for (i
= 0; i
< attr
->num_values
; i
++)
4567 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
4573 for (i
= 0; i
< attr
->num_values
; i
++)
4574 print_test_error("GOT: %s=%s", attr
->name
,
4575 attr
->values
[i
].boolean
? "true" : "false");
4579 case IPP_TAG_NOVALUE
:
4580 return (!strcmp(value
, "no-value") || !strncmp(value
, "no-value,", 9));
4582 case IPP_TAG_CHARSET
:
4583 case IPP_TAG_KEYWORD
:
4584 case IPP_TAG_LANGUAGE
:
4585 case IPP_TAG_MIMETYPE
:
4587 case IPP_TAG_NAMELANG
:
4589 case IPP_TAG_TEXTLANG
:
4591 case IPP_TAG_URISCHEME
:
4595 * Value is an extended, case-sensitive POSIX regular expression...
4598 regex_t re
; /* Regular expression */
4600 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
4602 char temp
[256]; /* Temporary string */
4604 regerror(i
, &re
, temp
, sizeof(temp
));
4606 print_fatal_error("Unable to compile WITH-VALUE regular expression "
4607 "\"%s\" - %s", value
, temp
);
4612 * See if ALL of the values match the given regular expression.
4615 for (i
= 0; i
< attr
->num_values
; i
++)
4617 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
4620 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4621 attr
->values
[i
].string
.text
);
4629 return (i
== attr
->num_values
);
4634 * Value is a literal string, see if at least one value matches the
4638 for (i
= 0; i
< attr
->num_values
; i
++)
4640 if (!strcmp(value
, attr
->values
[i
].string
.text
))
4646 for (i
= 0; i
< attr
->num_values
; i
++)
4647 print_test_error("GOT: %s=\"%s\"", attr
->name
,
4648 attr
->values
[i
].string
.text
);