2 * "$Id: ipptest.c 9000 2010-02-24 06:29:38Z mike $"
4 * IPP test command for the Common UNIX Printing System (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 * print_attr() - Print an attribute on the screen.
29 * print_col() - Print a collection attribute on the screen.
30 * print_fatal_error() - Print a fatal error message.
31 * print_test_error() - Print a test error message.
32 * print_xml_header() - Print a standard XML plist header.
33 * print_xml_string() - Print an XML string with escaping.
34 * print_xml_trailer() - Print the XML trailer with success/fail value.
35 * set_variable() - Set a variable value.
36 * usage() - Show program usage.
37 * validate_attr() - Determine whether an attribute is valid.
38 * with_value() - Test a WITH-VALUE predicate.
42 * Include necessary headers...
51 #include <cups/globals.h>
54 #endif /* !O_BINARY */
61 typedef enum /**** How to send request data ****/
63 _CUPS_TRANSFER_AUTO
, /* Chunk for files, length for static */
64 _CUPS_TRANSFER_CHUNKED
, /* Chunk always */
65 _CUPS_TRANSFER_LENGTH
/* Length always */
68 typedef struct _cups_expect_s
/**** Expected attribute info ****/
70 int optional
, /* Optional attribute? */
71 not_expect
; /* Don't expect attribute? */
72 char *name
, /* Attribute name */
73 *of_type
, /* Type name */
74 *same_count_as
, /* Parallel attribute name */
75 *if_defined
, /* Only required if variable defined */
76 *if_undefined
, /* Only required if variable is not defined */
77 *with_value
; /* Attribute must include this value */
78 int with_regex
, /* WITH-VALUE is a regular expression */
79 count
; /* Expected count if > 0 */
80 ipp_tag_t in_group
; /* IN-GROUP value */
83 typedef struct _cups_status_s
/**** Status info ****/
85 ipp_status_t status
; /* Expected status code */
86 char *if_defined
, /* Only if variable is defined */
87 *if_undefined
; /* Only if variable is not defined */
90 typedef struct _cups_var_s
/**** Variable ****/
92 char *name
, /* Name of variable */
93 *value
; /* Value of variable */
96 typedef struct _cups_vars_s
/**** Set of variables ****/
98 const char *uri
, /* URI for printer */
99 *filename
; /* Filename */
100 char scheme
[64], /* Scheme from URI */
101 userpass
[256], /* Username/password from URI */
102 hostname
[256], /* Hostname from URI */
103 resource
[1024]; /* Resource path from URI */
104 int port
; /* Port number from URI */
105 http_encryption_t encryption
; /* Encryption for connection? */
106 cups_array_t
*vars
; /* Array of variables */
114 _cups_transfer_t Transfer
= _CUPS_TRANSFER_AUTO
;
115 /* How to transfer requests */
116 int Verbosity
= 0, /* Show all attributes? */
117 Version
= 11, /* Default IPP version */
118 XML
= 0, /* Produce XML output? */
119 XMLHeader
= 0; /* 1 if header is written */
120 const char * const URIStatusStrings
[] = /* URI status strings */
123 "Bad arguments to function",
124 "Bad resource in URI",
125 "Bad port number in URI",
126 "Bad hostname/address in URI",
127 "Bad username in URI",
131 "Missing scheme in URI",
132 "Unknown scheme in URI",
133 "Missing resource in URI"
141 static int compare_vars(_cups_var_t
*a
, _cups_var_t
*b
);
142 static int do_tests(_cups_vars_t
*vars
, const char *testfile
);
143 static void expand_variables(_cups_vars_t
*vars
, char *dst
, const char *src
,
146 __attribute((nonnull(1,2,3)))
147 #endif /* __GNUC__ */
149 static int expect_matches(_cups_expect_t
*expect
, ipp_tag_t value_tag
);
150 static ipp_t
*get_collection(_cups_vars_t
*vars
, FILE *fp
, int *linenum
);
151 static char *get_filename(const char *testfile
, char *dst
, const char *src
,
153 static char *get_token(FILE *fp
, char *buf
, int buflen
,
155 static char *get_variable(_cups_vars_t
*vars
, const char *name
);
156 static char *iso_date(ipp_uchar_t
*date
);
157 static void print_attr(ipp_attribute_t
*attr
);
158 static void print_col(ipp_t
*col
);
159 static void print_fatal_error(const char *s
, ...)
161 __attribute__ ((__format__ (__printf__
, 1, 2)))
162 #endif /* __GNUC__ */
164 static void print_test_error(const char *s
, ...)
166 __attribute__ ((__format__ (__printf__
, 1, 2)))
167 #endif /* __GNUC__ */
169 static void print_xml_header(void);
170 static void print_xml_string(const char *element
, const char *s
);
171 static void print_xml_trailer(int success
, const char *message
);
172 static void set_variable(_cups_vars_t
*vars
, const char *name
,
174 static void usage(void);
175 static int validate_attr(ipp_attribute_t
*attr
, int print
);
176 static int with_value(char *value
, int regex
, ipp_attribute_t
*attr
);
180 * 'main()' - Parse options and do tests.
183 int /* O - Exit status */
184 main(int argc
, /* I - Number of command-line args */
185 char *argv
[]) /* I - Command-line arguments */
187 int i
; /* Looping var */
188 int status
; /* Status of tests... */
189 char *opt
, /* Current option */
190 name
[1024], /* Name/value buffer */
191 *value
; /* Pointer to value */
192 const char *testfile
; /* Test file to use */
193 int interval
; /* Test interval */
194 _cups_vars_t vars
; /* Variables */
195 http_uri_status_t uri_status
; /* URI separation status */
199 * Initialize the locale and variables...
202 _cupsSetLocale(argv
);
204 memset(&vars
, 0, sizeof(vars
));
205 vars
.vars
= cupsArrayNew((cups_array_func_t
)compare_vars
, NULL
);
210 * ipptest URI testfile
217 for (i
= 1; i
< argc
; i
++)
219 if (argv
[i
][0] == '-')
221 for (opt
= argv
[i
] + 1; *opt
; opt
++)
225 case 'E' : /* Encrypt */
227 vars
.encryption
= HTTP_ENCRYPT_REQUIRED
;
229 _cupsLangPrintf(stderr
,
230 _("%s: Sorry, no encryption support compiled in\n"),
232 #endif /* HAVE_SSL */
235 case 'V' : /* Set IPP version */
240 _cupsLangPuts(stderr
,
241 _("ipptest: Missing version for \"-V\".\n"));
245 if (!strcmp(argv
[i
], "1.0"))
247 else if (!strcmp(argv
[i
], "1.1"))
249 else if (!strcmp(argv
[i
], "2.0"))
251 else if (!strcmp(argv
[i
], "2.1"))
253 else if (!strcmp(argv
[i
], "2.2"))
257 _cupsLangPrintf(stderr
,
258 _("ipptest: Bad version %s for \"-V\".\n"),
264 case 'X' : /* Produce XML output */
269 _cupsLangPuts(stderr
, _("ipptest: \"-i\" is incompatible with "
275 case 'c' : /* Enable HTTP chunking */
276 Transfer
= _CUPS_TRANSFER_CHUNKED
;
279 case 'd' : /* Define a variable */
284 _cupsLangPuts(stderr
,
285 _("ipptest: Missing name=value for \"-d\".\n"));
289 strlcpy(name
, argv
[i
], sizeof(name
));
290 if ((value
= strchr(name
, '=')) != NULL
)
293 value
= name
+ strlen(name
);
295 set_variable(&vars
, name
, value
);
298 case 'f' : /* Set the default test filename */
303 _cupsLangPuts(stderr
,
304 _("ipptest: Missing filename for \"-f\".\n"));
308 vars
.filename
= argv
[i
];
311 case 'i' : /* Test every N seconds */
316 _cupsLangPuts(stderr
,
317 _("ipptest: Missing seconds for \"-i\".\n"));
321 interval
= atoi(argv
[i
]);
325 _cupsLangPuts(stderr
, _("ipptest: \"-i\" is incompatible with "
331 case 'l' : /* Disable HTTP chunking */
332 Transfer
= _CUPS_TRANSFER_LENGTH
;
335 case 'v' : /* Be verbose */
340 _cupsLangPrintf(stderr
, _("ipptest: Unknown option \"-%c\".\n"),
347 else if (!strncmp(argv
[i
], "ipp://", 6) ||
348 !strncmp(argv
[i
], "http://", 7) ||
349 !strncmp(argv
[i
], "https://", 8))
357 _cupsLangPuts(stderr
, _("ipptest: May only specify a single URI.\n"));
362 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
, vars
.uri
,
363 vars
.scheme
, sizeof(vars
.scheme
),
364 vars
.userpass
, sizeof(vars
.userpass
),
365 vars
.hostname
, sizeof(vars
.hostname
),
367 vars
.resource
, sizeof(vars
.resource
));
369 if (uri_status
!= HTTP_URI_OK
)
371 _cupsLangPrintf(stderr
, _("ipptest: Bad URI - %s.\n"),
372 URIStatusStrings
[uri_status
- HTTP_URI_OVERFLOW
]);
376 if (strcmp(vars
.scheme
, "http") && strcmp(vars
.scheme
, "https") &&
377 strcmp(vars
.scheme
, "ipp"))
379 _cupsLangPuts(stderr
, _("ipptest: Only http, https, and ipp URIs are "
392 _cupsLangPuts(stderr
, _("ipptest: URI required before test file."));
398 if (!do_tests(&vars
, testfile
))
403 if (!vars
.uri
|| !testfile
)
407 * Loop if the interval is set...
411 print_xml_trailer(!status
, NULL
);
417 do_tests(&vars
, testfile
);
430 * 'compare_vars()' - Compare two variables.
433 static int /* O - Result of comparison */
434 compare_vars(_cups_var_t
*a
, /* I - First variable */
435 _cups_var_t
*b
) /* I - Second variable */
437 return (strcasecmp(a
->name
, b
->name
));
442 * 'do_tests()' - Do tests as specified in the test file.
445 static int /* 1 = success, 0 = failure */
446 do_tests(_cups_vars_t
*vars
, /* I - Variables */
447 const char *testfile
) /* I - Test file to use */
449 int i
, /* Looping var */
450 linenum
, /* Current line number */
451 pass
, /* Did we pass the test? */
452 request_id
; /* Current request ID */
453 http_t
*http
= NULL
; /* HTTP connection to server */
454 FILE *fp
= NULL
; /* Test file */
455 char resource
[512], /* Resource for request */
456 token
[1024], /* Token from file */
457 *tokenptr
, /* Pointer into token */
458 temp
[1024]; /* Temporary string */
459 ipp_t
*request
= NULL
; /* IPP request */
460 ipp_t
*response
= NULL
; /* IPP response */
461 char attr
[128]; /* Attribute name */
462 ipp_op_t op
; /* Operation */
463 ipp_tag_t group
; /* Current group */
464 ipp_tag_t value
; /* Current value type */
465 ipp_attribute_t
*attrptr
, /* Attribute pointer */
466 *found
, /* Found attribute */
467 *lastcol
= NULL
; /* Last collection attribute */
468 char name
[1024]; /* Name of test */
469 char filename
[1024]; /* Filename */
470 _cups_transfer_t transfer
; /* To chunk or not to chunk */
471 int version
; /* IPP version number to use */
472 int num_statuses
= 0; /* Number of valid status codes */
473 _cups_status_t statuses
[100], /* Valid status codes */
474 *last_status
; /* Last STATUS (for predicates) */
475 int num_expects
= 0; /* Number of expected attributes */
476 _cups_expect_t expects
[200], /* Expected attributes */
477 *expect
, /* Current expected attribute */
478 *last_expect
; /* Last EXPECT (for predicates) */
479 int num_displayed
= 0; /* Number of displayed attributes */
480 char *displayed
[100]; /* Displayed attributes */
484 * Open the test file...
487 if ((fp
= fopen(testfile
, "r")) == NULL
)
489 print_fatal_error("Unable to open test file %s - %s", testfile
,
495 * Connect to the server...
498 if ((http
= httpConnectEncrypt(vars
->hostname
, vars
->port
,
499 vars
->encryption
)) == NULL
)
501 print_fatal_error("Unable to connect to %s on port %d - %s", vars
->hostname
,
502 vars
->port
, strerror(errno
));
513 printf("\"%s\":\n", testfile
);
515 CUPS_SRAND(time(NULL
));
519 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
521 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
524 * Expect an open brace...
527 if (!strcmp(token
, "DEFINE"))
533 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
534 get_token(fp
, temp
, sizeof(temp
), &linenum
))
536 expand_variables(vars
, token
, temp
, sizeof(token
));
537 set_variable(vars
, attr
, token
);
541 print_fatal_error("Missing DEFINE name and/or value on line %d.",
548 else if (!strcmp(token
, "INCLUDE"))
555 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
558 * Map the filename to and then run the tests...
561 if (!do_tests(vars
, get_filename(testfile
, filename
, temp
,
567 print_fatal_error("Missing INCLUDE filename on line %d.", linenum
);
573 else if (!strcmp(token
, "TRANSFER"))
581 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
583 if (!strcmp(temp
, "auto"))
584 Transfer
= _CUPS_TRANSFER_AUTO
;
585 else if (!strcmp(temp
, "chunked"))
586 Transfer
= _CUPS_TRANSFER_CHUNKED
;
587 else if (!strcmp(temp
, "length"))
588 Transfer
= _CUPS_TRANSFER_LENGTH
;
591 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp
,
598 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
604 else if (!strcmp(token
, "VERSION"))
606 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
608 if (!strcmp(temp
, "1.0"))
610 else if (!strcmp(temp
, "1.1"))
612 else if (!strcmp(temp
, "2.0"))
614 else if (!strcmp(temp
, "2.1"))
616 else if (!strcmp(temp
, "2.2"))
620 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
626 print_fatal_error("Missing VERSION number on line %d.", linenum
);
632 else if (strcmp(token
, "{"))
634 print_fatal_error("Unexpected token %s seen on line %d.", token
, linenum
);
639 * Initialize things...
642 strlcpy(resource
, vars
->resource
, sizeof(resource
));
647 group
= IPP_TAG_ZERO
;
654 strlcpy(name
, testfile
, sizeof(name
));
655 if (strrchr(name
, '.') != NULL
)
656 *strrchr(name
, '.') = '\0';
659 * Parse until we see a close brace...
662 while (get_token(fp
, token
, sizeof(token
), &linenum
) != NULL
)
664 if (strcasecmp(token
, "COUNT") &&
665 strcasecmp(token
, "IF-DEFINED") &&
666 strcasecmp(token
, "IF-UNDEFINED") &&
667 strcasecmp(token
, "IN-GROUP") &&
668 strcasecmp(token
, "OF-TYPE") &&
669 strcasecmp(token
, "SAME-COUNT-AS") &&
670 strcasecmp(token
, "WITH-VALUE"))
673 if (strcasecmp(token
, "IF-DEFINED") &&
674 strcasecmp(token
, "IF-UNDEFINED"))
677 if (!strcmp(token
, "}"))
679 else if (!strcmp(token
, "{") && lastcol
)
682 * Another collection value
685 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
686 /* Collection value */
690 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
694 * Reallocate memory...
697 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
698 (lastcol
->num_values
+ 1) *
699 sizeof(ipp_value_t
))) == NULL
)
701 print_fatal_error("Unable to allocate memory on line %d.", linenum
);
705 if (tempcol
!= lastcol
)
708 * Reset pointers in the list...
712 request
->prev
->next
= tempcol
;
714 request
->attrs
= tempcol
;
716 lastcol
= request
->current
= request
->last
= tempcol
;
719 lastcol
->values
[lastcol
->num_values
].collection
= col
;
720 lastcol
->num_values
++;
725 else if (!strcmp(token
, "DEFINE"))
731 if (get_token(fp
, attr
, sizeof(attr
), &linenum
) &&
732 get_token(fp
, temp
, sizeof(temp
), &linenum
))
734 expand_variables(vars
, token
, temp
, sizeof(token
));
735 set_variable(vars
, attr
, token
);
739 print_fatal_error("Missing DEFINE name and/or value on line %d.",
744 else if (!strcasecmp(token
, "NAME"))
750 get_token(fp
, name
, sizeof(name
), &linenum
);
752 else if (!strcmp(token
, "REQUEST-ID"))
759 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
761 if (isdigit(temp
[0] & 255))
762 request_id
= atoi(temp
);
763 else if (!strcasecmp(temp
, "random"))
764 request_id
= (CUPS_RAND() % 1000) * 137 + 1;
767 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp
,
774 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum
);
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
,
803 print_fatal_error("Missing TRANSFER value on line %d.", linenum
);
807 else if (!strcasecmp(token
, "VERSION"))
809 if (get_token(fp
, temp
, sizeof(temp
), &linenum
))
811 if (!strcmp(temp
, "0.0"))
813 else if (!strcmp(temp
, "1.0"))
815 else if (!strcmp(temp
, "1.1"))
817 else if (!strcmp(temp
, "2.0"))
819 else if (!strcmp(temp
, "2.1"))
821 else if (!strcmp(temp
, "2.2"))
825 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp
, linenum
);
831 print_fatal_error("Missing VERSION number on line %d.", linenum
);
835 else if (!strcasecmp(token
, "RESOURCE"))
841 if (!get_token(fp
, resource
, sizeof(resource
), &linenum
))
843 print_fatal_error("Missing RESOURCE path on line %d.", linenum
);
847 else if (!strcasecmp(token
, "OPERATION"))
853 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
855 print_fatal_error("Missing OPERATION code on line %d.", linenum
);
859 if ((op
= ippOpValue(token
)) < 0)
861 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token
,
866 else if (!strcasecmp(token
, "GROUP"))
872 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
874 print_fatal_error("Missing GROUP tag on line %d.", linenum
);
878 if ((value
= ippTagValue(token
)) < 0)
880 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token
, linenum
);
885 ippAddSeparator(request
);
889 else if (!strcasecmp(token
, "DELAY"))
892 * Delay before operation...
897 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
899 print_fatal_error("Missing DELAY value on line %d.", linenum
);
903 if ((delay
= atoi(token
)) <= 0)
905 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token
,
912 else if (!strcasecmp(token
, "ATTR"))
918 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
920 print_fatal_error("Missing ATTR value tag on line %d.", linenum
);
924 if ((value
= ippTagValue(token
)) < 0)
926 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token
,
931 printf("%d: value=%s\n", linenum
, ippTagString(value
));
933 if (!get_token(fp
, attr
, sizeof(attr
), &linenum
))
935 print_fatal_error("Missing ATTR name on line %d.", linenum
);
939 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
941 print_fatal_error("Missing ATTR value on line %d.", linenum
);
945 expand_variables(vars
, token
, temp
, sizeof(token
));
949 case IPP_TAG_BOOLEAN
:
950 if (!strcasecmp(token
, "true"))
951 ippAddBoolean(request
, group
, attr
, 1);
953 ippAddBoolean(request
, group
, attr
, atoi(token
));
956 case IPP_TAG_INTEGER
:
958 ippAddInteger(request
, group
, value
, attr
, atoi(token
));
961 case IPP_TAG_RESOLUTION
:
963 int xres
, /* X resolution */
964 yres
; /* Y resolution */
965 char units
[6]; /* Units */
967 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
968 (strcasecmp(units
, "dpi") && strcasecmp(units
, "dpc") &&
969 strcasecmp(units
, "other")))
971 print_fatal_error("Bad resolution value \"%s\" on line %d.",
976 if (!strcasecmp(units
, "dpi"))
977 ippAddResolution(request
, group
, attr
, xres
, yres
,
979 else if (!strcasecmp(units
, "dpc"))
980 ippAddResolution(request
, group
, attr
, xres
, yres
,
983 ippAddResolution(request
, group
, attr
, xres
, yres
,
990 int lowers
[4], /* Lower value */
991 uppers
[4], /* Upper values */
992 num_vals
; /* Number of values */
995 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
996 lowers
+ 0, uppers
+ 0,
997 lowers
+ 1, uppers
+ 1,
998 lowers
+ 2, uppers
+ 2,
999 lowers
+ 3, uppers
+ 3);
1001 if ((num_vals
& 1) || num_vals
== 0)
1003 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1004 "%d.", token
, linenum
);
1008 ippAddRanges(request
, group
, attr
, num_vals
/ 2, lowers
,
1013 case IPP_TAG_BEGIN_COLLECTION
:
1014 if (!strcmp(token
, "{"))
1016 ipp_t
*col
= get_collection(vars
, fp
, &linenum
);
1017 /* Collection value */
1020 lastcol
= ippAddCollection(request
, group
, attr
, col
);
1026 print_fatal_error("Bad ATTR collection value on line %d.",
1033 if (!strchr(token
, ','))
1034 ippAddString(request
, group
, value
, attr
, NULL
, token
);
1038 * Multiple string values...
1041 int num_values
; /* Number of values */
1042 char *values
[100], /* Values */
1043 *ptr
; /* Pointer to next value */
1049 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
1052 values
[num_values
] = ptr
;
1056 ippAddStrings(request
, group
, value
, attr
, num_values
,
1057 NULL
, (const char **)values
);
1062 else if (!strcasecmp(token
, "FILE"))
1068 if (!get_token(fp
, temp
, sizeof(temp
), &linenum
))
1070 print_fatal_error("Missing FILE filename on line %d.", linenum
);
1074 expand_variables(vars
, token
, temp
, sizeof(token
));
1075 get_filename(testfile
, filename
, token
, sizeof(filename
));
1077 else if (!strcasecmp(token
, "STATUS"))
1083 if (num_statuses
>= (int)(sizeof(statuses
) / sizeof(statuses
[0])))
1085 print_fatal_error("Too many STATUS's on line %d.", linenum
);
1089 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1091 print_fatal_error("Missing STATUS code on line %d.", linenum
);
1095 if ((statuses
[num_statuses
].status
= ippErrorValue(token
)) < 0)
1097 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token
,
1102 last_status
= statuses
+ num_statuses
;
1105 last_status
->if_defined
= NULL
;
1106 last_status
->if_undefined
= NULL
;
1108 else if (!strcasecmp(token
, "EXPECT"))
1111 * Expected attributes...
1114 if (num_expects
>= (int)(sizeof(expects
) / sizeof(expects
[0])))
1116 print_fatal_error("Too many EXPECT's on line %d.", linenum
);
1120 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1122 print_fatal_error("Missing EXPECT name on line %d.", linenum
);
1126 last_expect
= expects
+ num_expects
;
1129 memset(last_expect
, 0, sizeof(_cups_expect_t
));
1131 if (token
[0] == '!')
1133 last_expect
->not_expect
= 1;
1134 last_expect
->name
= strdup(token
+ 1);
1136 else if (token
[0] == '?')
1138 last_expect
->optional
= 1;
1139 last_expect
->name
= strdup(token
+ 1);
1142 last_expect
->name
= strdup(token
);
1144 else if (!strcasecmp(token
, "COUNT"))
1146 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1148 print_fatal_error("Missing COUNT number on line %d.", linenum
);
1152 if ((i
= atoi(token
)) <= 0)
1154 print_fatal_error("Bad COUNT \"%s\" on line %d.", token
, linenum
);
1159 last_expect
->count
= i
;
1162 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1167 else if (!strcasecmp(token
, "OF-TYPE"))
1169 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1171 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1177 last_expect
->of_type
= strdup(token
);
1180 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1185 else if (!strcasecmp(token
, "IN-GROUP"))
1187 ipp_tag_t in_group
; /* IN-GROUP value */
1190 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1192 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum
);
1196 if ((in_group
= ippTagValue(token
)) == (ipp_tag_t
)-1)
1199 else if (last_expect
)
1200 last_expect
->in_group
= in_group
;
1203 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1208 else if (!strcasecmp(token
, "SAME-COUNT-AS"))
1210 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1212 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum
);
1217 last_expect
->same_count_as
= strdup(token
);
1220 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1225 else if (!strcasecmp(token
, "IF-DEFINED"))
1227 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1229 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum
);
1234 last_expect
->if_defined
= strdup(token
);
1235 else if (last_status
)
1236 last_status
->if_defined
= strdup(token
);
1239 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1240 "on line %d.", linenum
);
1244 else if (!strcasecmp(token
, "IF-UNDEFINED"))
1246 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1248 print_fatal_error("Missing IF-UNDEFINED name on line %d.", linenum
);
1253 last_expect
->if_undefined
= strdup(token
);
1254 else if (last_status
)
1255 last_status
->if_undefined
= strdup(token
);
1258 print_fatal_error("IF-UNDEFINED without a preceding EXPECT or STATUS "
1259 "on line %d.", linenum
);
1263 else if (!strcasecmp(token
, "WITH-VALUE"))
1265 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1267 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum
);
1273 tokenptr
= token
+ strlen(token
) - 1;
1274 if (token
[0] == '/' && tokenptr
> token
&& *tokenptr
== '/')
1277 * WITH-VALUE is a POSIX extended regular expression.
1280 last_expect
->with_value
= calloc(1, tokenptr
- token
);
1281 last_expect
->with_regex
= 1;
1283 if (last_expect
->with_value
)
1284 memcpy(last_expect
->with_value
, token
+ 1, tokenptr
- token
- 1);
1289 * WITH-VALUE is a literal value...
1292 last_expect
->with_value
= strdup(token
);
1297 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
1302 else if (!strcasecmp(token
, "DISPLAY"))
1305 * Display attributes...
1308 if (num_displayed
>= (int)(sizeof(displayed
) / sizeof(displayed
[0])))
1310 print_fatal_error("Too many DISPLAY's on line %d", linenum
);
1314 if (!get_token(fp
, token
, sizeof(token
), &linenum
))
1316 print_fatal_error("Missing DISPLAY name on line %d.", linenum
);
1320 displayed
[num_displayed
] = strdup(token
);
1325 print_fatal_error("Unexpected token %s seen on line %d.", token
,
1332 * Submit the IPP request...
1335 request
->request
.op
.version
[0] = version
/ 10;
1336 request
->request
.op
.version
[1] = version
% 10;
1337 request
->request
.op
.operation_id
= op
;
1338 request
->request
.op
.request_id
= request_id
;
1343 puts("<key>Name</key>");
1344 print_xml_string("string", name
);
1345 puts("<key>Operation</key>");
1346 print_xml_string("string", ippOpString(op
));
1347 puts("<key>RequestAttributes</key>");
1349 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1350 print_attr(attrptr
);
1357 printf(" %s:\n", ippOpString(op
));
1359 for (attrptr
= request
->attrs
; attrptr
; attrptr
= attrptr
->next
)
1360 print_attr(attrptr
);
1363 printf(" %-69.69s [", name
);
1367 if (transfer
== _CUPS_TRANSFER_CHUNKED
||
1368 (transfer
== _CUPS_TRANSFER_AUTO
&& filename
[0]))
1371 * Send request using chunking...
1374 http_status_t status
= cupsSendRequest(http
, request
, resource
, 0);
1376 if (status
== HTTP_CONTINUE
&& filename
[0])
1378 int fd
; /* File to send */
1379 char buffer
[8192]; /* Copy buffer */
1380 ssize_t bytes
; /* Bytes read/written */
1382 if ((fd
= open(filename
, O_RDONLY
| O_BINARY
)) >= 0)
1384 while ((bytes
= read(fd
, buffer
, sizeof(buffer
))) > 0)
1385 if ((status
= cupsWriteRequestData(http
, buffer
,
1386 bytes
)) != HTTP_CONTINUE
)
1391 snprintf(buffer
, sizeof(buffer
), "%s: %s", filename
, strerror(errno
));
1392 _cupsSetError(IPP_INTERNAL_ERROR
, buffer
, 0);
1394 status
= HTTP_ERROR
;
1400 if (status
== HTTP_CONTINUE
)
1401 response
= cupsGetResponse(http
, resource
);
1405 else if (filename
[0])
1406 response
= cupsDoFileRequest(http
, request
, resource
, filename
);
1408 response
= cupsDoRequest(http
, request
, resource
);
1416 if (http
->version
!= HTTP_1_1
)
1419 if (response
->request
.status
.version
[0] != (version
/ 10) ||
1420 response
->request
.status
.version
[1] != (version
% 10) ||
1421 response
->request
.status
.request_id
!= request_id
)
1424 if ((attrptr
= ippFindAttribute(response
, "job-id",
1425 IPP_TAG_INTEGER
)) != NULL
)
1427 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1428 set_variable(vars
, "job-id", temp
);
1431 if ((attrptr
= ippFindAttribute(response
, "job-uri",
1432 IPP_TAG_URI
)) != NULL
)
1433 set_variable(vars
, "job-uri", attrptr
->values
[0].string
.text
);
1435 if ((attrptr
= ippFindAttribute(response
, "notify-subscription-id",
1436 IPP_TAG_INTEGER
)) != NULL
)
1438 snprintf(temp
, sizeof(temp
), "%d", attrptr
->values
[0].integer
);
1439 set_variable(vars
, "notify-subscription-id", temp
);
1442 attrptr
= response
->attrs
;
1443 if (!attrptr
|| !attrptr
->name
||
1444 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
1445 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1446 attrptr
->num_values
!= 1 ||
1447 strcmp(attrptr
->name
, "attributes-charset"))
1452 attrptr
= attrptr
->next
;
1453 if (!attrptr
|| !attrptr
->name
||
1454 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
1455 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1456 attrptr
->num_values
!= 1 ||
1457 strcmp(attrptr
->name
, "attributes-natural-language"))
1461 if ((attrptr
= ippFindAttribute(response
, "status-message",
1462 IPP_TAG_ZERO
)) != NULL
&&
1463 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1464 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1465 attrptr
->num_values
!= 1 ||
1466 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1467 strlen(attrptr
->values
[0].string
.text
) > 255)))
1470 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
1471 IPP_TAG_ZERO
)) != NULL
&&
1472 (attrptr
->value_tag
!= IPP_TAG_TEXT
||
1473 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1474 attrptr
->num_values
!= 1 ||
1475 (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1476 strlen(attrptr
->values
[0].string
.text
) > 1023)))
1479 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
1481 attrptr
= attrptr
->next
)
1483 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
1489 if (!validate_attr(attrptr
, 0))
1496 for (i
= 0; i
< num_statuses
; i
++)
1498 if (statuses
[i
].if_defined
&&
1499 !get_variable(vars
, statuses
[i
].if_defined
))
1502 if (statuses
[i
].if_undefined
&&
1503 get_variable(vars
, statuses
[i
].if_undefined
))
1506 if (response
->request
.status
.status_code
== statuses
[i
].status
)
1510 if (i
== num_statuses
&& num_statuses
> 0)
1514 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
1516 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
1519 if (expect
->if_undefined
&& get_variable(vars
, expect
->if_undefined
))
1522 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
1524 if ((found
&& expect
->not_expect
) ||
1525 (!found
&& !(expect
->not_expect
|| expect
->optional
)) ||
1526 (found
&& !expect_matches(expect
, found
->value_tag
)) ||
1527 (found
&& expect
->in_group
&&
1528 found
->group_tag
!= expect
->in_group
))
1535 !with_value(expect
->with_value
, expect
->with_regex
, found
))
1541 if (found
&& expect
->count
> 0 && found
->num_values
!= expect
->count
)
1547 if (found
&& expect
->same_count_as
)
1549 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
1552 if (!attrptr
|| attrptr
->num_values
!= found
->num_values
)
1564 puts("<key>Successful</key>");
1565 puts(pass
? "<true />" : "<false />");
1566 puts("<key>StatusCode</key>");
1567 print_xml_string("string", ippErrorString(cupsLastError()));
1568 puts("<key>ResponseAttributes</key>");
1570 for (attrptr
= response
? response
->attrs
: NULL
;
1572 attrptr
= attrptr
->next
)
1573 print_attr(attrptr
);
1578 puts(pass
? "PASS]" : "FAIL]");
1580 if (Verbosity
&& response
)
1582 printf(" RECEIVED: %lu bytes in response\n",
1583 (unsigned long)ippLength(response
));
1584 printf(" status-code = %x (%s)\n", cupsLastError(),
1585 ippErrorString(cupsLastError()));
1587 for (attrptr
= response
->attrs
;
1589 attrptr
= attrptr
->next
)
1591 print_attr(attrptr
);
1596 if (pass
&& !XML
&& !Verbosity
&& num_displayed
> 0)
1598 for (attrptr
= response
->attrs
;
1600 attrptr
= attrptr
->next
)
1602 for (i
= 0; i
< num_displayed
; i
++)
1603 if (!strcmp(displayed
[i
], attrptr
->name
))
1605 print_attr(attrptr
);
1613 puts("<key>Errors</key>");
1617 if (http
->version
!= HTTP_1_1
)
1618 print_test_error("Bad HTTP version (%d.%d)", http
->version
/ 100,
1619 http
->version
% 100);
1622 print_test_error("IPP request failed with status %s (%s)",
1623 ippErrorString(cupsLastError()),
1624 cupsLastErrorString());
1627 if (response
->request
.status
.version
[0] != (version
/ 10) ||
1628 response
->request
.status
.version
[1] != (version
% 10))
1629 print_test_error("Bad version %d.%d in response - expected %d.%d "
1630 "(RFC 2911 section 3.1.8).",
1631 response
->request
.status
.version
[0],
1632 response
->request
.status
.version
[1],
1633 version
/ 10, version
% 10);
1635 if (response
->request
.status
.request_id
!= request_id
)
1636 print_test_error("Bad request ID %d in response - expected %d "
1637 "(RFC 2911 section 3.1.1)",
1638 response
->request
.status
.request_id
, request_id
);
1640 attrptr
= response
->attrs
;
1642 print_test_error("Missing first attribute \"attributes-charset "
1643 "(charset)\" in group operation-attributes-tag "
1644 "(RFC 2911 section 3.1.4).");
1647 if (!attrptr
->name
||
1648 attrptr
->value_tag
!= IPP_TAG_CHARSET
||
1649 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1650 attrptr
->num_values
!= 1 ||
1651 strcmp(attrptr
->name
, "attributes-charset"))
1652 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
1653 "expected \"attributes-charset (charset)\" in "
1654 "group operation-attributes-tag (RFC 2911 section "
1656 attrptr
->name
? attrptr
->name
: "(null)",
1657 attrptr
->num_values
> 1 ? "1setOf " : "",
1658 ippTagString(attrptr
->value_tag
),
1659 ippTagString(attrptr
->group_tag
));
1661 attrptr
= attrptr
->next
;
1663 print_test_error("Missing second attribute \"attributes-natural-"
1664 "language (naturalLanguage)\" in group "
1665 "operation-attributes-tag (RFC 2911 section "
1667 else if (!attrptr
->name
||
1668 attrptr
->value_tag
!= IPP_TAG_LANGUAGE
||
1669 attrptr
->group_tag
!= IPP_TAG_OPERATION
||
1670 attrptr
->num_values
!= 1 ||
1671 strcmp(attrptr
->name
, "attributes-natural-language"))
1672 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
1673 "expected \"attributes-natural-language "
1674 "(naturalLanguage)\" in group "
1675 "operation-attributes-tag (RFC 2911 section "
1677 attrptr
->name
? attrptr
->name
: "(null)",
1678 attrptr
->num_values
> 1 ? "1setOf " : "",
1679 ippTagString(attrptr
->value_tag
),
1680 ippTagString(attrptr
->group_tag
));
1683 if ((attrptr
= ippFindAttribute(response
, "status-message",
1684 IPP_TAG_ZERO
)) != NULL
)
1686 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
1687 print_test_error("status-message (text(255)) has wrong value tag "
1688 "%s (RFC 2911 section 3.1.6.2).",
1689 ippTagString(attrptr
->value_tag
));
1690 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
1691 print_test_error("status-message (text(255)) has wrong group tag "
1692 "%s (RFC 2911 section 3.1.6.2).",
1693 ippTagString(attrptr
->group_tag
));
1694 if (attrptr
->num_values
!= 1)
1695 print_test_error("status-message (text(255)) has %d values "
1696 "(RFC 2911 section 3.1.6.2).",
1697 attrptr
->num_values
);
1698 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1699 strlen(attrptr
->values
[0].string
.text
) > 255)
1700 print_test_error("status-message (text(255)) has bad length %d"
1701 " (RFC 2911 section 3.1.6.2).",
1702 (int)strlen(attrptr
->values
[0].string
.text
));
1705 if ((attrptr
= ippFindAttribute(response
, "detailed-status-message",
1706 IPP_TAG_ZERO
)) != NULL
)
1708 if (attrptr
->value_tag
!= IPP_TAG_TEXT
)
1709 print_test_error("detailed-status-message (text(MAX)) has wrong "
1710 "value tag %s (RFC 2911 section 3.1.6.3).",
1711 ippTagString(attrptr
->value_tag
));
1712 if (attrptr
->group_tag
!= IPP_TAG_OPERATION
)
1713 print_test_error("detailed-status-message (text(MAX)) has wrong "
1714 "group tag %s (RFC 2911 section 3.1.6.3).",
1715 ippTagString(attrptr
->group_tag
));
1716 if (attrptr
->num_values
!= 1)
1717 print_test_error("detailed-status-message (text(MAX)) has %d values"
1718 " (RFC 2911 section 3.1.6.3).",
1719 attrptr
->num_values
);
1720 if (attrptr
->value_tag
== IPP_TAG_TEXT
&&
1721 strlen(attrptr
->values
[0].string
.text
) > 1023)
1722 print_test_error("detailed-status-message (text(MAX)) has bad "
1723 "length %d (RFC 2911 section 3.1.6.3).",
1724 (int)strlen(attrptr
->values
[0].string
.text
));
1727 for (attrptr
= response
->attrs
, group
= attrptr
->group_tag
;
1729 attrptr
= attrptr
->next
)
1731 if (attrptr
->group_tag
< group
&& attrptr
->group_tag
!= IPP_TAG_ZERO
)
1732 print_test_error("Attribute groups out of order (%s < %s)",
1733 ippTagString(attrptr
->group_tag
),
1734 ippTagString(group
));
1736 validate_attr(attrptr
, 1);
1739 for (i
= 0; i
< num_statuses
; i
++)
1741 if (statuses
[i
].if_defined
&&
1742 !get_variable(vars
, statuses
[i
].if_defined
))
1745 if (statuses
[i
].if_undefined
&&
1746 get_variable(vars
, statuses
[i
].if_undefined
))
1749 if (response
->request
.status
.status_code
== statuses
[i
].status
)
1753 if (i
== num_statuses
&& num_statuses
> 0)
1755 print_test_error("Bad status-code (%s)",
1756 ippErrorString(cupsLastError()));
1757 print_test_error("status-message=\"%s\"", cupsLastErrorString());
1760 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
1762 if (expect
->if_defined
&& !get_variable(vars
, expect
->if_defined
))
1765 if (expect
->if_undefined
&& get_variable(vars
, expect
->if_undefined
))
1768 found
= ippFindAttribute(response
, expect
->name
, IPP_TAG_ZERO
);
1770 if (found
&& expect
->not_expect
)
1771 print_test_error("NOT EXPECTED: %s", expect
->name
);
1772 else if (!found
&& !(expect
->not_expect
|| expect
->optional
))
1773 print_test_error("EXPECTED: %s", expect
->name
);
1776 if (!expect_matches(expect
, found
->value_tag
))
1777 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
1778 expect
->name
, expect
->of_type
,
1779 ippTagString(found
->value_tag
));
1781 if (expect
->in_group
&& found
->group_tag
!= expect
->in_group
)
1782 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
1783 expect
->name
, ippTagString(expect
->in_group
),
1784 ippTagString(found
->group_tag
));
1786 if (!with_value(expect
->with_value
, expect
->with_regex
, found
))
1788 if (expect
->with_regex
)
1789 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
1790 expect
->name
, expect
->with_value
);
1792 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
1793 expect
->name
, expect
->with_value
);
1796 if (expect
->count
> 0 && found
->num_values
!= expect
->count
)
1798 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect
->name
,
1799 expect
->count
, found
->num_values
);
1802 if (expect
->same_count_as
)
1804 attrptr
= ippFindAttribute(response
, expect
->same_count_as
,
1808 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
1809 "(not returned)", expect
->name
,
1810 found
->num_values
, expect
->same_count_as
);
1811 else if (attrptr
->num_values
!= found
->num_values
)
1812 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
1813 "(%d values)", expect
->name
, found
->num_values
,
1814 expect
->same_count_as
, attrptr
->num_values
);
1827 ippDelete(response
);
1830 for (i
= 0; i
< num_statuses
; i
++)
1832 if (statuses
[i
].if_defined
)
1833 free(statuses
[i
].if_defined
);
1834 if (statuses
[i
].if_undefined
)
1835 free(statuses
[i
].if_undefined
);
1839 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
1842 if (expect
->of_type
)
1843 free(expect
->of_type
);
1844 if (expect
->same_count_as
)
1845 free(expect
->same_count_as
);
1846 if (expect
->if_defined
)
1847 free(expect
->if_defined
);
1848 if (expect
->if_undefined
)
1849 free(expect
->if_undefined
);
1850 if (expect
->with_value
)
1851 free(expect
->with_value
);
1855 for (i
= 0; i
< num_displayed
; i
++)
1869 * If we get here there was a fatal test error...
1879 ippDelete(response
);
1881 for (i
= 0; i
< num_statuses
; i
++)
1883 if (statuses
[i
].if_defined
)
1884 free(statuses
[i
].if_defined
);
1885 if (statuses
[i
].if_undefined
)
1886 free(statuses
[i
].if_undefined
);
1889 for (i
= num_expects
, expect
= expects
; i
> 0; i
--, expect
++)
1892 if (expect
->of_type
)
1893 free(expect
->of_type
);
1894 if (expect
->same_count_as
)
1895 free(expect
->same_count_as
);
1896 if (expect
->if_defined
)
1897 free(expect
->if_defined
);
1898 if (expect
->if_undefined
)
1899 free(expect
->if_undefined
);
1900 if (expect
->with_value
)
1901 free(expect
->with_value
);
1904 for (i
= 0; i
< num_displayed
; i
++)
1912 * 'expand_variables()' - Expand variables in a string.
1916 expand_variables(_cups_vars_t
*vars
, /* I - Variables */
1917 char *dst
, /* I - Destination string buffer */
1918 const char *src
, /* I - Source string */
1919 size_t dstsize
) /* I - Size of destination buffer */
1921 char *dstptr
, /* Pointer into destination */
1922 *dstend
, /* End of destination */
1923 temp
[256], /* Temporary string */
1924 *tempptr
; /* Pointer into temporary string */
1925 const char *value
; /* Value to substitute */
1929 dstend
= dst
+ dstsize
- 1;
1931 while (*src
&& dstptr
< dstend
)
1936 * Substitute a string/number...
1939 if (!strncmp(src
, "$$", 2))
1944 else if (!strncmp(src
, "$ENV[", 5))
1946 strlcpy(temp
, src
+ 5, sizeof(temp
));
1948 for (tempptr
= temp
; *tempptr
; tempptr
++)
1949 if (*tempptr
== ']')
1955 value
= getenv(temp
);
1956 src
+= tempptr
- temp
+ 5;
1960 strlcpy(temp
, src
+ 1, sizeof(temp
));
1962 for (tempptr
= temp
; *tempptr
; tempptr
++)
1963 if (!isalnum(*tempptr
& 255) && *tempptr
!= '-' && *tempptr
!= '_')
1969 if (!strcmp(temp
, "uri"))
1971 else if (!strcmp(temp
, "filename"))
1972 value
= vars
->filename
;
1973 else if (!strcmp(temp
, "scheme") || !strcmp(temp
, "method"))
1974 value
= vars
->scheme
;
1975 else if (!strcmp(temp
, "username"))
1976 value
= vars
->userpass
;
1977 else if (!strcmp(temp
, "hostname"))
1978 value
= vars
->hostname
;
1979 else if (!strcmp(temp
, "port"))
1981 snprintf(temp
, sizeof(temp
), "%d", vars
->port
);
1984 else if (!strcmp(temp
, "resource"))
1985 value
= vars
->resource
;
1986 else if (!strcmp(temp
, "user"))
1989 value
= get_variable(vars
, temp
);
1991 src
+= tempptr
- temp
+ 1;
2001 strlcpy(dstptr
, value
, dstend
- dstptr
+ 1);
2002 dstptr
+= strlen(dstptr
);
2014 * 'expect_matches()' - Return true if the tag matches the specification.
2017 static int /* O - 1 if matches, 0 otherwise */
2019 _cups_expect_t
*expect
, /* I - Expected attribute */
2020 ipp_tag_t value_tag
) /* I - Value tag for attribute */
2022 int match
; /* Match? */
2023 char *of_type
, /* Type name to match */
2024 *next
, /* Next name to match */
2025 sep
; /* Separator character */
2029 * If we don't expect a particular type, return immediately...
2032 if (!expect
->of_type
)
2036 * Parse the "of_type" value since the string can contain multiple attribute
2037 * types separated by "," or "|"...
2040 for (of_type
= expect
->of_type
, match
= 0; !match
&& of_type
; of_type
= next
)
2043 * Find the next separator, and set it (temporarily) to nul if present.
2046 for (next
= of_type
; *next
&& *next
!= '|' && *next
!= ','; next
++);
2048 if ((sep
= *next
) != '\0')
2052 * Support some meta-types to make it easier to write the test file.
2055 if (!strcmp(of_type
, "text"))
2056 match
= value_tag
== IPP_TAG_TEXTLANG
|| value_tag
== IPP_TAG_TEXT
;
2057 else if (!strcmp(of_type
, "name"))
2058 match
= value_tag
== IPP_TAG_NAMELANG
|| value_tag
== IPP_TAG_NAME
;
2059 else if (!strcmp(of_type
, "collection"))
2060 match
= value_tag
== IPP_TAG_BEGIN_COLLECTION
;
2062 match
= value_tag
== ippTagValue(of_type
);
2065 * Restore the separator if we have one...
2077 * 'get_collection()' - Get a collection value from the current test file.
2080 static ipp_t
* /* O - Collection value */
2081 get_collection(_cups_vars_t
*vars
, /* I - Variables */
2082 FILE *fp
, /* I - File to read from */
2083 int *linenum
) /* IO - Line number */
2085 char token
[1024], /* Token from file */
2086 temp
[1024], /* Temporary string */
2087 attr
[128]; /* Attribute name */
2088 ipp_tag_t value
; /* Current value type */
2089 ipp_t
*col
= ippNew(); /* Collection value */
2090 ipp_attribute_t
*lastcol
= NULL
; /* Last collection attribute */
2093 while (get_token(fp
, token
, sizeof(token
), linenum
) != NULL
)
2095 if (!strcmp(token
, "}"))
2097 else if (!strcmp(token
, "{") && lastcol
)
2100 * Another collection value
2103 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2104 /* Collection value */
2108 ipp_attribute_t
*tempcol
; /* Pointer to new buffer */
2112 * Reallocate memory...
2115 if ((tempcol
= realloc(lastcol
, sizeof(ipp_attribute_t
) +
2116 (lastcol
->num_values
+ 1) *
2117 sizeof(ipp_value_t
))) == NULL
)
2119 print_fatal_error("Unable to allocate memory on line %d.", *linenum
);
2123 if (tempcol
!= lastcol
)
2126 * Reset pointers in the list...
2130 col
->prev
->next
= tempcol
;
2132 col
->attrs
= tempcol
;
2134 lastcol
= col
->current
= col
->last
= tempcol
;
2137 lastcol
->values
[lastcol
->num_values
].collection
= subcol
;
2138 lastcol
->num_values
++;
2143 else if (!strcasecmp(token
, "MEMBER"))
2151 if (!get_token(fp
, token
, sizeof(token
), linenum
))
2153 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum
);
2157 if ((value
= ippTagValue(token
)) < 0)
2159 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token
,
2164 if (!get_token(fp
, attr
, sizeof(attr
), linenum
))
2166 print_fatal_error("Missing MEMBER name on line %d.", *linenum
);
2170 if (!get_token(fp
, temp
, sizeof(temp
), linenum
))
2172 print_fatal_error("Missing MEMBER value on line %d.", *linenum
);
2176 expand_variables(vars
, token
, temp
, sizeof(token
));
2180 case IPP_TAG_BOOLEAN
:
2181 if (!strcasecmp(token
, "true"))
2182 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, 1);
2184 ippAddBoolean(col
, IPP_TAG_ZERO
, attr
, atoi(token
));
2187 case IPP_TAG_INTEGER
:
2189 ippAddInteger(col
, IPP_TAG_ZERO
, value
, attr
, atoi(token
));
2192 case IPP_TAG_RESOLUTION
:
2194 int xres
, /* X resolution */
2195 yres
; /* Y resolution */
2196 char units
[6]; /* Units */
2198 if (sscanf(token
, "%dx%d%5s", &xres
, &yres
, units
) != 3 ||
2199 (strcasecmp(units
, "dpi") && strcasecmp(units
, "dpc") &&
2200 strcasecmp(units
, "other")))
2202 print_fatal_error("Bad resolution value \"%s\" on line %d.",
2207 if (!strcasecmp(units
, "dpi"))
2208 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2210 else if (!strcasecmp(units
, "dpc"))
2211 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2214 ippAddResolution(col
, IPP_TAG_ZERO
, attr
, xres
, yres
,
2219 case IPP_TAG_RANGE
:
2221 int lowers
[4], /* Lower value */
2222 uppers
[4], /* Upper values */
2223 num_vals
; /* Number of values */
2226 num_vals
= sscanf(token
, "%d-%d,%d-%d,%d-%d,%d-%d",
2227 lowers
+ 0, uppers
+ 0,
2228 lowers
+ 1, uppers
+ 1,
2229 lowers
+ 2, uppers
+ 2,
2230 lowers
+ 3, uppers
+ 3);
2232 if ((num_vals
& 1) || num_vals
== 0)
2234 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
2239 ippAddRanges(col
, IPP_TAG_ZERO
, attr
, num_vals
/ 2, lowers
,
2244 case IPP_TAG_BEGIN_COLLECTION
:
2245 if (!strcmp(token
, "{"))
2247 ipp_t
*subcol
= get_collection(vars
, fp
, linenum
);
2248 /* Collection value */
2251 lastcol
= ippAddCollection(col
, IPP_TAG_ZERO
, attr
, subcol
);
2257 print_fatal_error("Bad collection value on line %d.", *linenum
);
2263 if (!strchr(token
, ','))
2264 ippAddString(col
, IPP_TAG_ZERO
, value
, attr
, NULL
, token
);
2268 * Multiple string values...
2271 int num_values
; /* Number of values */
2272 char *values
[100], /* Values */
2273 *ptr
; /* Pointer to next value */
2279 for (ptr
= strchr(token
, ','); ptr
; ptr
= strchr(ptr
, ','))
2282 values
[num_values
] = ptr
;
2286 ippAddStrings(col
, IPP_TAG_ZERO
, value
, attr
, num_values
,
2287 NULL
, (const char **)values
);
2297 * If we get here there was a parse error; free memory and return.
2309 * 'get_filename()' - Get a filename based on the current test file.
2312 static char * /* O - Filename */
2313 get_filename(const char *testfile
, /* I - Current test file */
2314 char *dst
, /* I - Destination filename */
2315 const char *src
, /* I - Source filename */
2316 size_t dstsize
) /* I - Size of destination buffer */
2318 char *dstptr
; /* Pointer into destination */
2319 _cups_globals_t
*cg
= _cupsGlobals();
2323 if (*src
== '<' && src
[strlen(src
) - 1] == '>')
2326 * Map <filename> to CUPS_DATADIR/ipptest/filename...
2329 snprintf(dst
, dstsize
, "%s/ipptest/%s", cg
->cups_datadir
, src
+ 1);
2330 dstptr
= dst
+ strlen(dst
) - 1;
2334 else if (*src
== '/' || !strchr(testfile
, '/'))
2337 * Use the path as-is...
2340 strlcpy(dst
, src
, dstsize
);
2345 * Make path relative to testfile...
2348 strlcpy(dst
, testfile
, dstsize
);
2349 if ((dstptr
= strrchr(dst
, '/')) != NULL
)
2352 dstptr
= dst
; /* Should never happen */
2354 strlcpy(dstptr
, src
, dstsize
- (dstptr
- dst
));
2362 * 'get_token()' - Get a token from a file.
2365 static char * /* O - Token from file or NULL on EOF */
2366 get_token(FILE *fp
, /* I - File to read from */
2367 char *buf
, /* I - Buffer to read into */
2368 int buflen
, /* I - Length of buffer */
2369 int *linenum
) /* IO - Current line number */
2371 int ch
, /* Character from file */
2372 quote
; /* Quoting character */
2373 char *bufptr
, /* Pointer into buffer */
2374 *bufend
; /* End of buffer */
2380 * Skip whitespace...
2383 while (isspace(ch
= getc(fp
)))
2395 else if (ch
== '\'' || ch
== '\"')
2398 * Quoted text or regular expression...
2403 bufend
= buf
+ buflen
- 1;
2405 while ((ch
= getc(fp
)) != EOF
)
2410 * Escape next character...
2413 if (bufptr
< bufend
)
2416 if ((ch
= getc(fp
)) != EOF
&& bufptr
< bufend
)
2419 else if (ch
== quote
)
2421 else if (bufptr
< bufend
)
2435 while ((ch
= getc(fp
)) != EOF
)
2444 * Whitespace delimited text...
2450 bufend
= buf
+ buflen
- 1;
2452 while ((ch
= getc(fp
)) != EOF
)
2453 if (isspace(ch
) || ch
== '#')
2455 else if (bufptr
< bufend
)
2460 else if (ch
== '\n')
2472 * 'get_variable()' - Get the value of a variable.
2475 static char * /* O - Value or NULL */
2476 get_variable(_cups_vars_t
*vars
, /* I - Variables */
2477 const char *name
) /* I - Variable name */
2479 _cups_var_t key
, /* Search key */
2480 *match
; /* Matching variable, if any */
2483 key
.name
= (char *)name
;
2484 match
= cupsArrayFind(vars
->vars
, &key
);
2486 return (match
? match
->value
: NULL
);
2491 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
2495 static char * /* O - ISO 8601 date/time string */
2496 iso_date(ipp_uchar_t
*date
) /* I - IPP (RFC 1903) date/time value */
2498 unsigned year
= (date
[0] << 8) + date
[1];
2500 static char buffer
[255]; /* String buffer */
2503 if (date
[9] == 0 && date
[10] == 0)
2504 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02uZ",
2505 year
, date
[2], date
[3], date
[4], date
[5], date
[6]);
2507 snprintf(buffer
, sizeof(buffer
), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
2508 year
, date
[2], date
[3], date
[4], date
[5], date
[6],
2509 date
[8], date
[9], date
[10]);
2516 * 'print_attr()' - Print an attribute on the screen.
2520 print_attr(ipp_attribute_t
*attr
) /* I - Attribute to print */
2522 int i
; /* Looping var */
2523 ipp_attribute_t
*colattr
; /* Collection attribute */
2530 printf("<key>%s</key>\n<true />\n", ippTagString(attr
->group_tag
));
2534 print_xml_string("key", attr
->name
);
2535 if (attr
->num_values
> 1)
2542 puts(" -- separator --");
2546 printf(" %s (%s%s) = ", attr
->name
,
2547 attr
->num_values
> 1 ? "1setOf " : "",
2548 ippTagString(attr
->value_tag
));
2551 switch (attr
->value_tag
)
2553 case IPP_TAG_INTEGER
:
2555 for (i
= 0; i
< attr
->num_values
; i
++)
2557 printf("<integer>%d</integer>\n", attr
->values
[i
].integer
);
2559 printf("%d ", attr
->values
[i
].integer
);
2562 case IPP_TAG_BOOLEAN
:
2563 for (i
= 0; i
< attr
->num_values
; i
++)
2565 puts(attr
->values
[i
].boolean
? "<true />" : "<false />");
2566 else if (attr
->values
[i
].boolean
)
2567 fputs("true ", stdout
);
2569 fputs("false ", stdout
);
2572 case IPP_TAG_RANGE
:
2573 for (i
= 0; i
< attr
->num_values
; i
++)
2575 printf("<dict><key>lower</key><integer>%d</integer>"
2576 "<key>upper</key><integer>%d</integer></dict>\n",
2577 attr
->values
[i
].range
.lower
, attr
->values
[i
].range
.upper
);
2579 printf("%d-%d ", attr
->values
[i
].range
.lower
,
2580 attr
->values
[i
].range
.upper
);
2583 case IPP_TAG_RESOLUTION
:
2584 for (i
= 0; i
< attr
->num_values
; i
++)
2586 printf("<dict><key>xres</key><integer>%d</integer>"
2587 "<key>yres</key><integer>%d</integer>"
2588 "<key>units</key><string>%s</string></dict>\n",
2589 attr
->values
[i
].resolution
.xres
,
2590 attr
->values
[i
].resolution
.yres
,
2591 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2594 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
2595 attr
->values
[i
].resolution
.yres
,
2596 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2601 for (i
= 0; i
< attr
->num_values
; i
++)
2603 printf("<date>%s</date>\n", iso_date(attr
->values
[i
].date
));
2605 printf("%s ", iso_date(attr
->values
[i
].date
));
2608 case IPP_TAG_STRING
:
2611 case IPP_TAG_KEYWORD
:
2612 case IPP_TAG_CHARSET
:
2614 case IPP_TAG_MIMETYPE
:
2615 case IPP_TAG_LANGUAGE
:
2616 for (i
= 0; i
< attr
->num_values
; i
++)
2618 print_xml_string("string", attr
->values
[i
].string
.text
);
2620 printf("\"%s\" ", attr
->values
[i
].string
.text
);
2623 case IPP_TAG_TEXTLANG
:
2624 case IPP_TAG_NAMELANG
:
2625 for (i
= 0; i
< attr
->num_values
; i
++)
2628 fputs("<dict><key>language</key><string>", stdout
);
2629 print_xml_string(NULL
, attr
->values
[i
].string
.charset
);
2630 fputs("</string><key>string</key><string>", stdout
);
2631 print_xml_string(NULL
, attr
->values
[i
].string
.text
);
2632 puts("</string></dict>");
2635 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
2636 attr
->values
[i
].string
.charset
);
2639 case IPP_TAG_BEGIN_COLLECTION
:
2640 for (i
= 0; i
< attr
->num_values
; i
++)
2645 for (colattr
= attr
->values
[i
].collection
->attrs
;
2647 colattr
= colattr
->next
)
2648 print_attr(colattr
);
2656 print_col(attr
->values
[i
].collection
);
2663 printf("<string><<%s>></string>\n",
2664 ippTagString(attr
->value_tag
));
2666 fputs(ippTagString(attr
->value_tag
), stdout
);
2672 if (attr
->num_values
> 1)
2681 * 'print_col()' - Print a collection attribute on the screen.
2685 print_col(ipp_t
*col
) /* I - Collection attribute to print */
2687 int i
; /* Looping var */
2688 ipp_attribute_t
*attr
; /* Current attribute in collection */
2691 fputs("{ ", stdout
);
2692 for (attr
= col
->attrs
; attr
; attr
= attr
->next
)
2694 printf("%s (%s%s) = ", attr
->name
, attr
->num_values
> 1 ? "1setOf " : "",
2695 ippTagString(attr
->value_tag
));
2697 switch (attr
->value_tag
)
2699 case IPP_TAG_INTEGER
:
2701 for (i
= 0; i
< attr
->num_values
; i
++)
2702 printf("%d ", attr
->values
[i
].integer
);
2705 case IPP_TAG_BOOLEAN
:
2706 for (i
= 0; i
< attr
->num_values
; i
++)
2707 if (attr
->values
[i
].boolean
)
2713 case IPP_TAG_NOVALUE
:
2717 case IPP_TAG_RANGE
:
2718 for (i
= 0; i
< attr
->num_values
; i
++)
2719 printf("%d-%d ", attr
->values
[i
].range
.lower
,
2720 attr
->values
[i
].range
.upper
);
2723 case IPP_TAG_RESOLUTION
:
2724 for (i
= 0; i
< attr
->num_values
; i
++)
2725 printf("%dx%d%s ", attr
->values
[i
].resolution
.xres
,
2726 attr
->values
[i
].resolution
.yres
,
2727 attr
->values
[i
].resolution
.units
== IPP_RES_PER_INCH
?
2731 case IPP_TAG_STRING
:
2734 case IPP_TAG_KEYWORD
:
2735 case IPP_TAG_CHARSET
:
2737 case IPP_TAG_MIMETYPE
:
2738 case IPP_TAG_LANGUAGE
:
2739 for (i
= 0; i
< attr
->num_values
; i
++)
2740 printf("\"%s\" ", attr
->values
[i
].string
.text
);
2743 case IPP_TAG_TEXTLANG
:
2744 case IPP_TAG_NAMELANG
:
2745 for (i
= 0; i
< attr
->num_values
; i
++)
2746 printf("\"%s\",%s ", attr
->values
[i
].string
.text
,
2747 attr
->values
[i
].string
.charset
);
2750 case IPP_TAG_BEGIN_COLLECTION
:
2751 for (i
= 0; i
< attr
->num_values
; i
++)
2753 print_col(attr
->values
[i
].collection
);
2759 break; /* anti-compiler-warning-code */
2768 * 'print_fatal_error()' - Print a fatal error message.
2772 print_fatal_error(const char *s
, /* I - Printf-style format string */
2773 ...) /* I - Additional arguments as needed */
2775 char buffer
[10240]; /* Format buffer */
2776 va_list ap
; /* Pointer to arguments */
2780 * Format the error message...
2784 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
2794 print_xml_trailer(0, buffer
);
2797 _cupsLangPrintf(stderr
, "ipptest: %s\n", buffer
);
2802 * 'print_test_error()' - Print a test error message.
2806 print_test_error(const char *s
, /* I - Printf-style format string */
2807 ...) /* I - Additional arguments as needed */
2809 char buffer
[10240]; /* Format buffer */
2810 va_list ap
; /* Pointer to arguments */
2814 * Format the error message...
2818 vsnprintf(buffer
, sizeof(buffer
), s
, ap
);
2826 print_xml_string("string", buffer
);
2828 printf(" %s\n", buffer
);
2833 * 'print_xml_header()' - Print a standard XML plist header.
2837 print_xml_header(void)
2841 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
2842 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
2843 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
2844 puts("<plist version=\"1.0\">");
2846 puts("<key>Transfer</key>");
2847 printf("<string>%s</string>\n",
2848 Transfer
== _CUPS_TRANSFER_AUTO
? "auto" :
2849 Transfer
== _CUPS_TRANSFER_CHUNKED
? "chunked" : "length");
2850 puts("<key>Tests</key>");
2859 * 'print_xml_string()' - Print an XML string with escaping.
2863 print_xml_string(const char *element
, /* I - Element name or NULL */
2864 const char *s
) /* I - String to print */
2867 printf("<%s>", element
);
2872 fputs("&", stdout
);
2874 fputs("<", stdout
);
2876 fputs(">", stdout
);
2884 printf("</%s>\n", element
);
2889 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
2893 print_xml_trailer(int success
, /* I - 1 on success, 0 on failure */
2894 const char *message
) /* I - Error message or NULL */
2899 puts("<key>Successful</key>");
2900 puts(success
? "<true />" : "<false />");
2903 puts("<key>ErrorMessage</key>");
2904 print_xml_string("string", message
);
2915 * 'set_variable()' - Set a variable value.
2919 set_variable(_cups_vars_t
*vars
, /* I - Variables */
2920 const char *name
, /* I - Variable name */
2921 const char *value
) /* I - Value string */
2923 _cups_var_t key
, /* Search key */
2924 *var
; /* New variable */
2927 key
.name
= (char *)name
;
2928 if ((var
= cupsArrayFind(vars
->vars
, &key
)) != NULL
)
2931 var
->value
= strdup(value
);
2933 else if ((var
= malloc(sizeof(_cups_var_t
))) == NULL
)
2935 print_fatal_error("Unable to allocate memory for variable \"%s\".", name
);
2940 var
->name
= strdup(name
);
2941 var
->value
= strdup(value
);
2943 cupsArrayAdd(vars
->vars
, var
);
2949 * 'usage()' - Show program usage.
2955 _cupsLangPuts(stderr
,
2956 _("Usage: ipptest [options] URI filename.test [ ... "
2957 "filenameN.test ]\n"
2961 "-E Test with encryption.\n"
2962 "-V version Set default IPP version.\n"
2963 "-X Produce XML instead of plain text.\n"
2964 "-c Send requests using chunking (default)\n"
2965 "-d name=value Define variable.\n"
2966 "-f filename Set default test file.\n"
2967 "-i seconds Repeat the last test file with the given "
2969 "-l Send requests using content-length\n"
2970 "-v Show all attributes sent and received.\n"));
2977 * 'validate_attr()' - Determine whether an attribute is valid.
2980 static int /* O - 1 if valid, 0 otherwise */
2981 validate_attr(ipp_attribute_t
*attr
, /* I - Attribute to validate */
2982 int print
) /* I - 1 = report issues to stdout */
2984 int i
; /* Looping var */
2985 char scheme
[64], /* Scheme from URI */
2986 userpass
[256], /* Username/password from URI */
2987 hostname
[256], /* Hostname from URI */
2988 resource
[1024]; /* Resource from URI */
2989 int port
, /* Port number from URI */
2990 uri_status
, /* URI separation status */
2991 valid
= 1; /* Is the attribute valid? */
2992 const char *ptr
; /* Pointer into string */
2993 ipp_attribute_t
*colattr
; /* Collection attribute */
2994 regex_t re
; /* Regular expression */
2995 ipp_uchar_t
*date
; /* Current date value */
3006 * Validate the attribute name.
3009 for (ptr
= attr
->name
; *ptr
; ptr
++)
3010 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' && *ptr
!= '_')
3013 if (*ptr
|| ptr
== attr
->name
)
3018 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
3019 "2911 section 4.1.3).", attr
->name
);
3022 if ((ptr
- attr
->name
) > 255)
3027 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
3028 "section 4.1.3).", attr
->name
);
3031 switch (attr
->value_tag
)
3033 case IPP_TAG_INTEGER
:
3036 case IPP_TAG_BOOLEAN
:
3037 for (i
= 0; i
< attr
->num_values
; i
++)
3039 if (attr
->values
[i
].boolean
!= 0 &&
3040 attr
->values
[i
].boolean
!= 1)
3045 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
3046 "4.1.10).", attr
->name
, attr
->values
[i
].boolean
);
3054 for (i
= 0; i
< attr
->num_values
; i
++)
3056 if (attr
->values
[i
].integer
< 1)
3061 print_test_error("\"%s\": Bad enum value %d - out of range "
3062 "(RFC 2911 section 4.1.4).", attr
->name
,
3063 attr
->values
[i
].integer
);
3070 case IPP_TAG_STRING
:
3071 for (i
= 0; i
< attr
->num_values
; i
++)
3073 if (attr
->values
[i
].unknown
.length
> 1023)
3078 print_test_error("\"%s\": Bad octetString value - bad length %d "
3079 "(RFC 2911 section 4.1.10).", attr
->name
,
3080 attr
->values
[i
].unknown
.length
);
3088 for (i
= 0; i
< attr
->num_values
; i
++)
3090 date
= attr
->values
[i
].date
;
3092 if (date
[2] < 1 || date
[2] > 12)
3097 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
3098 "section 4.1.13).", attr
->name
, date
[2]);
3103 if (date
[3] < 1 || date
[3] > 31)
3108 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
3109 "section 4.1.13).", attr
->name
, date
[3]);
3119 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
3120 "section 4.1.13).", attr
->name
, date
[4]);
3130 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
3131 "section 4.1.13).", attr
->name
, date
[5]);
3141 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
3142 "section 4.1.13).", attr
->name
, date
[6]);
3152 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
3153 "section 4.1.13).", attr
->name
, date
[7]);
3158 if (date
[8] != '-' && date
[8] != '+')
3163 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
3164 "section 4.1.13).", attr
->name
, date
[8]);
3174 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
3175 "section 4.1.13).", attr
->name
, date
[9]);
3185 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
3186 "section 4.1.13).", attr
->name
, date
[10]);
3193 case IPP_TAG_RESOLUTION
:
3194 for (i
= 0; i
< attr
->num_values
; i
++)
3196 if (attr
->values
[i
].resolution
.xres
<= 0)
3201 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
3202 "feed resolution must be positive (RFC 2911 "
3203 "section 4.1.13).", attr
->name
,
3204 attr
->values
[i
].resolution
.xres
,
3205 attr
->values
[i
].resolution
.yres
,
3206 attr
->values
[i
].resolution
.units
==
3207 IPP_RES_PER_INCH
? "dpi" :
3208 attr
->values
[i
].resolution
.units
==
3209 IPP_RES_PER_CM
? "dpc" : "unknown");
3214 if (attr
->values
[i
].resolution
.yres
<= 0)
3219 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
3220 "resolution must be positive (RFC 2911 section "
3221 "4.1.13).", attr
->name
,
3222 attr
->values
[i
].resolution
.xres
,
3223 attr
->values
[i
].resolution
.yres
,
3224 attr
->values
[i
].resolution
.units
==
3225 IPP_RES_PER_INCH
? "dpi" :
3226 attr
->values
[i
].resolution
.units
==
3227 IPP_RES_PER_CM
? "dpc" : "unknown");
3232 if (attr
->values
[i
].resolution
.units
!= IPP_RES_PER_INCH
&&
3233 attr
->values
[i
].resolution
.units
!= IPP_RES_PER_CM
)
3238 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
3239 "units value (RFC 2911 section 4.1.13).",
3240 attr
->name
, attr
->values
[i
].resolution
.xres
,
3241 attr
->values
[i
].resolution
.yres
,
3242 attr
->values
[i
].resolution
.units
==
3243 IPP_RES_PER_INCH
? "dpi" :
3244 attr
->values
[i
].resolution
.units
==
3245 IPP_RES_PER_CM
? "dpc" : "unknown");
3252 case IPP_TAG_RANGE
:
3253 for (i
= 0; i
< attr
->num_values
; i
++)
3255 if (attr
->values
[i
].range
.lower
> attr
->values
[i
].range
.upper
)
3260 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
3261 "greater than upper (RFC 2911 section 4.1.13).",
3262 attr
->name
, attr
->values
[i
].range
.lower
,
3263 attr
->values
[i
].range
.upper
);
3270 case IPP_TAG_BEGIN_COLLECTION
:
3271 for (i
= 0; i
< attr
->num_values
; i
++)
3273 for (colattr
= attr
->values
[i
].collection
->attrs
;
3275 colattr
= colattr
->next
)
3277 if (!validate_attr(colattr
, 0))
3284 if (colattr
&& print
)
3286 print_test_error("\"%s\": Bad collection value.", attr
->name
);
3290 validate_attr(colattr
, print
);
3291 colattr
= colattr
->next
;
3298 case IPP_TAG_TEXTLANG
:
3299 for (i
= 0; i
< attr
->num_values
; i
++)
3301 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
3303 if ((*ptr
& 0xe0) == 0xc0)
3306 if ((*ptr
& 0xc0) != 0x80)
3309 else if ((*ptr
& 0xf0) == 0xe0)
3312 if ((*ptr
& 0xc0) != 0x80)
3315 if ((*ptr
& 0xc0) != 0x80)
3318 else if ((*ptr
& 0xf8) == 0xf0)
3321 if ((*ptr
& 0xc0) != 0x80)
3324 if ((*ptr
& 0xc0) != 0x80)
3327 if ((*ptr
& 0xc0) != 0x80)
3330 else if (*ptr
& 0x80)
3339 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
3340 "sequence (RFC 2911 section 4.1.1).", attr
->name
,
3341 attr
->values
[i
].string
.text
);
3346 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
3351 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
3352 "(RFC 2911 section 4.1.1).", attr
->name
,
3353 attr
->values
[i
].string
.text
,
3354 (int)strlen(attr
->values
[i
].string
.text
));
3362 case IPP_TAG_NAMELANG
:
3363 for (i
= 0; i
< attr
->num_values
; i
++)
3365 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
3367 if ((*ptr
& 0xe0) == 0xc0)
3370 if ((*ptr
& 0xc0) != 0x80)
3373 else if ((*ptr
& 0xf0) == 0xe0)
3376 if ((*ptr
& 0xc0) != 0x80)
3379 if ((*ptr
& 0xc0) != 0x80)
3382 else if ((*ptr
& 0xf8) == 0xf0)
3385 if ((*ptr
& 0xc0) != 0x80)
3388 if ((*ptr
& 0xc0) != 0x80)
3391 if ((*ptr
& 0xc0) != 0x80)
3394 else if (*ptr
& 0x80)
3403 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
3404 "sequence (RFC 2911 section 4.1.2).", attr
->name
,
3405 attr
->values
[i
].string
.text
);
3410 if ((ptr
- attr
->values
[i
].string
.text
) > 1023)
3415 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
3416 "(RFC 2911 section 4.1.2).", attr
->name
,
3417 attr
->values
[i
].string
.text
,
3418 (int)strlen(attr
->values
[i
].string
.text
));
3425 case IPP_TAG_KEYWORD
:
3426 for (i
= 0; i
< attr
->num_values
; i
++)
3428 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
3429 if (!isalnum(*ptr
& 255) && *ptr
!= '-' && *ptr
!= '.' &&
3433 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
3438 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
3439 "character (RFC 2911 section 4.1.3).",
3440 attr
->name
, attr
->values
[i
].string
.text
);
3445 if ((ptr
- attr
->values
[i
].string
.text
) > 255)
3450 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
3451 "length %d (RFC 2911 section 4.1.3).",
3452 attr
->name
, attr
->values
[i
].string
.text
,
3453 (int)strlen(attr
->values
[i
].string
.text
));
3461 for (i
= 0; i
< attr
->num_values
; i
++)
3463 uri_status
= httpSeparateURI(HTTP_URI_CODING_ALL
,
3464 attr
->values
[i
].string
.text
,
3465 scheme
, sizeof(scheme
),
3466 userpass
, sizeof(userpass
),
3467 hostname
, sizeof(hostname
),
3468 &port
, resource
, sizeof(resource
));
3470 if (uri_status
< HTTP_URI_OK
)
3475 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
3476 "(RFC 2911 section 4.1.5).", attr
->name
,
3477 attr
->values
[i
].string
.text
,
3478 URIStatusStrings
[uri_status
-
3479 HTTP_URI_OVERFLOW
]);
3484 if (strlen(attr
->values
[i
].string
.text
) > 1023)
3489 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
3490 "(RFC 2911 section 4.1.5).", attr
->name
,
3491 attr
->values
[i
].string
.text
,
3492 (int)strlen(attr
->values
[i
].string
.text
));
3499 case IPP_TAG_URISCHEME
:
3500 for (i
= 0; i
< attr
->num_values
; i
++)
3502 ptr
= attr
->values
[i
].string
.text
;
3503 if (islower(*ptr
& 255))
3505 for (ptr
++; *ptr
; ptr
++)
3506 if (!islower(*ptr
& 255) && !isdigit(*ptr
& 255) &&
3507 *ptr
!= '+' && *ptr
!= '-' && *ptr
!= '.')
3511 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
3516 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
3517 "characters (RFC 2911 section 4.1.6).",
3518 attr
->name
, attr
->values
[i
].string
.text
);
3523 if ((ptr
- attr
->values
[i
].string
.text
) > 63)
3528 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
3529 "length %d (RFC 2911 section 4.1.6).",
3530 attr
->name
, attr
->values
[i
].string
.text
,
3531 (int)strlen(attr
->values
[i
].string
.text
));
3538 case IPP_TAG_CHARSET
:
3539 for (i
= 0; i
< attr
->num_values
; i
++)
3541 for (ptr
= attr
->values
[i
].string
.text
; *ptr
; ptr
++)
3542 if (!isprint(*ptr
& 255) || isupper(*ptr
& 255) ||
3543 isspace(*ptr
& 255))
3546 if (*ptr
|| ptr
== attr
->values
[i
].string
.text
)
3551 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
3552 "characters (RFC 2911 section 4.1.7).",
3553 attr
->name
, attr
->values
[i
].string
.text
);
3558 if ((ptr
- attr
->values
[i
].string
.text
) > 40)
3563 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
3564 "length %d (RFC 2911 section 4.1.7).",
3565 attr
->name
, attr
->values
[i
].string
.text
,
3566 (int)strlen(attr
->values
[i
].string
.text
));
3573 case IPP_TAG_LANGUAGE
:
3575 * The following regular expression is derived from the ABNF for
3576 * language tags in RFC 4646. All I can say is that this is the
3577 * easiest way to check the values...
3580 if ((i
= regcomp(&re
,
3582 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
3584 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
3585 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
3586 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
3587 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
3588 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
3590 "x(-[a-z0-9]{1,8})+" /* privateuse */
3592 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
3594 REG_NOSUB
| REG_EXTENDED
)) != 0)
3596 char temp
[256]; /* Temporary error string */
3598 regerror(i
, &re
, temp
, sizeof(temp
));
3599 print_fatal_error("Unable to compile naturalLanguage regular "
3600 "expression: %s.", temp
);
3603 for (i
= 0; i
< attr
->num_values
; i
++)
3605 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
3610 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
3611 "characters (RFC 2911 section 4.1.8).",
3612 attr
->name
, attr
->values
[i
].string
.text
);
3617 if (strlen(attr
->values
[i
].string
.text
) > 63)
3622 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
3623 "length %d (RFC 2911 section 4.1.8).",
3624 attr
->name
, attr
->values
[i
].string
.text
,
3625 (int)strlen(attr
->values
[i
].string
.text
));
3634 case IPP_TAG_MIMETYPE
:
3636 * The following regular expression is derived from the ABNF for
3637 * language tags in RFC 2045 and 4288. All I can say is that this is
3638 * the easiest way to check the values...
3641 if ((i
= regcomp(&re
,
3643 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
3645 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
3646 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
3647 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
3650 REG_NOSUB
| REG_EXTENDED
)) != 0)
3652 char temp
[256]; /* Temporary error string */
3654 regerror(i
, &re
, temp
, sizeof(temp
));
3655 print_fatal_error("Unable to compile mimeMediaType regular "
3656 "expression: %s.", temp
);
3659 for (i
= 0; i
< attr
->num_values
; i
++)
3661 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
3666 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
3667 "characters (RFC 2911 section 4.1.9).",
3668 attr
->name
, attr
->values
[i
].string
.text
);
3673 if (strlen(attr
->values
[i
].string
.text
) > 255)
3678 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
3679 "length %d (RFC 2911 section 4.1.9).",
3680 attr
->name
, attr
->values
[i
].string
.text
,
3681 (int)strlen(attr
->values
[i
].string
.text
));
3697 * 'with_value()' - Test a WITH-VALUE predicate.
3700 static int /* O - 1 on match, 0 on non-match */
3701 with_value(char *value
, /* I - Value string */
3702 int regex
, /* I - Value is a regular expression */
3703 ipp_attribute_t
*attr
) /* I - Attribute to compare */
3705 int i
; /* Looping var */
3706 char *valptr
; /* Pointer into value */
3710 * NULL matches everything.
3713 if (!value
|| !*value
)
3717 * Compare the value string to the attribute value.
3720 switch (attr
->value_tag
)
3722 case IPP_TAG_INTEGER
:
3724 for (i
= 0; i
< attr
->num_values
; i
++)
3726 char op
, /* Comparison operator */
3727 *nextptr
; /* Next pointer */
3728 int intvalue
; /* Integer value */
3732 if (!strncmp(valptr
, "no-value,", 9))
3735 while (isspace(*valptr
& 255) || isdigit(*valptr
& 255) ||
3736 *valptr
== '-' || *valptr
== ',' || *valptr
== '<' ||
3737 *valptr
== '=' || *valptr
== '>')
3740 while (*valptr
&& !isdigit(*valptr
& 255) && *valptr
!= '-')
3742 if (*valptr
== '<' || *valptr
== '>' || *valptr
== '=')
3750 intvalue
= strtol(valptr
, &nextptr
, 0);
3751 if (nextptr
== valptr
)
3758 if (attr
->values
[i
].integer
== intvalue
)
3762 if (attr
->values
[i
].integer
< intvalue
)
3766 if (attr
->values
[i
].integer
> intvalue
)
3774 case IPP_TAG_BOOLEAN
:
3775 for (i
= 0; i
< attr
->num_values
; i
++)
3777 if (!strcmp(value
, "true") == attr
->values
[i
].boolean
)
3782 case IPP_TAG_NOVALUE
:
3783 return (!strcmp(value
, "no-value") || !strncmp(value
, "no-value,", 9));
3785 case IPP_TAG_CHARSET
:
3786 case IPP_TAG_KEYWORD
:
3787 case IPP_TAG_LANGUAGE
:
3788 case IPP_TAG_MIMETYPE
:
3790 case IPP_TAG_NAMELANG
:
3792 case IPP_TAG_TEXTLANG
:
3794 case IPP_TAG_URISCHEME
:
3798 * Value is an extended, case-sensitive POSIX regular expression...
3801 regex_t re
; /* Regular expression */
3803 if ((i
= regcomp(&re
, value
, REG_EXTENDED
| REG_NOSUB
)) != 0)
3805 char temp
[256]; /* Temporary string */
3807 regerror(i
, &re
, temp
, sizeof(temp
));
3809 print_fatal_error("Unable to compile WITH-VALUE regular expression "
3810 "\"%s\" - %s", value
, temp
);
3815 * See if ALL of the values match the given regular expression.
3818 for (i
= 0; i
< attr
->num_values
; i
++)
3820 if (regexec(&re
, attr
->values
[i
].string
.text
, 0, NULL
, 0))
3826 return (i
== attr
->num_values
);
3831 * Value is a literal string, see if at least one value matches the
3835 for (i
= 0; i
< attr
->num_values
; i
++)
3837 if (!strcmp(value
, attr
->values
[i
].string
.text
))
3852 * End of "$Id: ipptest.c 9000 2010-02-24 06:29:38Z mike $".