]> git.ipfire.org Git - thirdparty/cups.git/blob - test/ipptool.c
81c663d5a4cbea37bbbf81a94f7beedbf4245f69
[thirdparty/cups.git] / test / ipptool.c
1 /*
2 * "$Id$"
3 *
4 * ipptool command for CUPS.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
8 *
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * main() - Parse options and do tests.
20 * compare_vars() - Compare two variables.
21 * do_tests() - Do tests as specified in the test file.
22 * expand_variables() - Expand variables in a string.
23 * expect_matches() - Return true if the tag matches the specification.
24 * get_collection() - Get a collection value from the current test file.
25 * get_filename() - Get a filename based on the current test file.
26 * get_token() - Get a token from a file.
27 * get_variable() - Get the value of a variable.
28 * iso_date() - Return an ISO 8601 date/time string for the given IPP
29 * dateTime value.
30 * password_cb() - Password callback for authenticated tests.
31 * print_attr() - Print an attribute on the screen.
32 * print_col() - Print a collection attribute on the screen.
33 * print_csv() - Print a line of CSV text.
34 * print_fatal_error() - Print a fatal error message.
35 * print_line() - Print a line of formatted or CSV text.
36 * print_test_error() - Print a test error message.
37 * print_xml_header() - Print a standard XML plist header.
38 * print_xml_string() - Print an XML string with escaping.
39 * print_xml_trailer() - Print the XML trailer with success/fail value.
40 * set_variable() - Set a variable value.
41 * timeout_cb() - Handle HTTP timeouts.
42 * usage() - Show program usage.
43 * validate_attr() - Determine whether an attribute is valid.
44 * with_value() - Test a WITH-VALUE predicate.
45 */
46
47 /*
48 * Include necessary headers...
49 */
50
51 #include <cups/cups-private.h>
52 #include <cups/file-private.h>
53 #include <regex.h>
54
55 #ifndef O_BINARY
56 # define O_BINARY 0
57 #endif /* !O_BINARY */
58
59
60 /*
61 * Types...
62 */
63
64 typedef enum _cups_transfer_e /**** How to send request data ****/
65 {
66 _CUPS_TRANSFER_AUTO, /* Chunk for files, length for static */
67 _CUPS_TRANSFER_CHUNKED, /* Chunk always */
68 _CUPS_TRANSFER_LENGTH /* Length always */
69 } _cups_transfer_t;
70
71 typedef enum _cups_output_e /**** Output mode ****/
72 {
73 _CUPS_OUTPUT_QUIET, /* No output */
74 _CUPS_OUTPUT_TEST, /* Traditional CUPS test output */
75 _CUPS_OUTPUT_PLIST, /* XML plist test output */
76 _CUPS_OUTPUT_LIST, /* Tabular list output */
77 _CUPS_OUTPUT_CSV /* Comma-separated values output */
78 } _cups_output_t;
79
80 typedef struct _cups_expect_s /**** Expected attribute info ****/
81 {
82 int optional, /* Optional attribute? */
83 not_expect; /* Don't expect attribute? */
84 char *name, /* Attribute name */
85 *of_type, /* Type name */
86 *same_count_as, /* Parallel attribute name */
87 *if_defined, /* Only required if variable defined */
88 *if_not_defined, /* Only required if variable is not defined */
89 *with_value, /* Attribute must include this value */
90 *define_match, /* Variable to define on match */
91 *define_no_match, /* Variable to define on no-match */
92 *define_value; /* Variable to define with value */
93 int with_regex, /* WITH-VALUE is a regular expression */
94 count; /* Expected count if > 0 */
95 ipp_tag_t in_group; /* IN-GROUP value */
96 } _cups_expect_t;
97
98 typedef struct _cups_status_s /**** Status info ****/
99 {
100 ipp_status_t status; /* Expected status code */
101 char *if_defined, /* Only if variable is defined */
102 *if_not_defined; /* Only if variable is not defined */
103 } _cups_status_t;
104
105 typedef struct _cups_var_s /**** Variable ****/
106 {
107 char *name, /* Name of variable */
108 *value; /* Value of variable */
109 } _cups_var_t;
110
111 typedef struct _cups_vars_s /**** Set of variables ****/
112 {
113 const char *uri, /* URI for printer */
114 *filename; /* Filename */
115 char scheme[64], /* Scheme from URI */
116 userpass[256], /* Username/password from URI */
117 hostname[256], /* Hostname from URI */
118 resource[1024]; /* Resource path from URI */
119 int port; /* Port number from URI */
120 http_encryption_t encryption; /* Encryption for connection? */
121 double timeout; /* Timeout for connection */
122 int family; /* Address family */
123 cups_array_t *vars; /* Array of variables */
124 } _cups_vars_t;
125
126
127 /*
128 * Globals...
129 */
130
131 _cups_transfer_t Transfer = _CUPS_TRANSFER_AUTO;
132 /* How to transfer requests */
133 _cups_output_t Output = _CUPS_OUTPUT_LIST;
134 /* Output mode */
135 int IgnoreErrors = 0, /* Ignore errors? */
136 Verbosity = 0, /* Show all attributes? */
137 Version = 11, /* Default IPP version */
138 XMLHeader = 0; /* 1 if header is written */
139 char *Password = NULL; /* Password from URI */
140 const char * const URIStatusStrings[] = /* URI status strings */
141 {
142 "URI too large",
143 "Bad arguments to function",
144 "Bad resource in URI",
145 "Bad port number in URI",
146 "Bad hostname/address in URI",
147 "Bad username in URI",
148 "Bad scheme in URI",
149 "Bad/empty URI",
150 "OK",
151 "Missing scheme in URI",
152 "Unknown scheme in URI",
153 "Missing resource in URI"
154 };
155
156
157 /*
158 * Local functions...
159 */
160
161 static int compare_vars(_cups_var_t *a, _cups_var_t *b);
162 static int do_tests(_cups_vars_t *vars, const char *testfile);
163 static void expand_variables(_cups_vars_t *vars, char *dst, const char *src,
164 size_t dstsize)
165 #ifdef __GNUC__
166 __attribute((nonnull(1,2,3)))
167 #endif /* __GNUC__ */
168 ;
169 static int expect_matches(_cups_expect_t *expect, ipp_tag_t value_tag);
170 static ipp_t *get_collection(_cups_vars_t *vars, FILE *fp, int *linenum);
171 static char *get_filename(const char *testfile, char *dst, const char *src,
172 size_t dstsize);
173 static char *get_token(FILE *fp, char *buf, int buflen,
174 int *linenum);
175 static char *get_variable(_cups_vars_t *vars, const char *name);
176 static char *iso_date(ipp_uchar_t *date);
177 static const char *password_cb(const char *prompt);
178 static void print_attr(ipp_attribute_t *attr, ipp_tag_t *group);
179 static void print_col(ipp_t *col);
180 static void print_csv(ipp_attribute_t *attr, int num_displayed,
181 char **displayed, size_t *widths);
182 static void print_fatal_error(const char *s, ...)
183 #ifdef __GNUC__
184 __attribute__ ((__format__ (__printf__, 1, 2)))
185 #endif /* __GNUC__ */
186 ;
187 static void print_line(ipp_attribute_t *attr, int num_displayed,
188 char **displayed, size_t *widths);
189 static void print_test_error(const char *s, ...)
190 #ifdef __GNUC__
191 __attribute__ ((__format__ (__printf__, 1, 2)))
192 #endif /* __GNUC__ */
193 ;
194 static void print_xml_header(void);
195 static void print_xml_string(const char *element, const char *s);
196 static void print_xml_trailer(int success, const char *message);
197 static void set_variable(_cups_vars_t *vars, const char *name,
198 const char *value);
199 static int timeout_cb(http_t *http, void *user_data);
200 static void usage(void);
201 static int validate_attr(ipp_attribute_t *attr, int print);
202 static int with_value(char *value, int regex, ipp_attribute_t *attr,
203 int report);
204
205
206 /*
207 * 'main()' - Parse options and do tests.
208 */
209
210 int /* O - Exit status */
211 main(int argc, /* I - Number of command-line args */
212 char *argv[]) /* I - Command-line arguments */
213 {
214 int i; /* Looping var */
215 int status; /* Status of tests... */
216 char *opt, /* Current option */
217 name[1024], /* Name/value buffer */
218 *value, /* Pointer to value */
219 filename[1024], /* Real filename */
220 testname[1024]; /* Real test filename */
221 const char *testfile; /* Test file to use */
222 int interval, /* Test interval in microseconds */
223 repeat; /* Repeat count */
224 _cups_vars_t vars; /* Variables */
225 http_uri_status_t uri_status; /* URI separation status */
226 _cups_globals_t *cg = _cupsGlobals();
227 /* Global data */
228
229
230
231 /*
232 * Initialize the locale and variables...
233 */
234
235 _cupsSetLocale(argv);
236
237 memset(&vars, 0, sizeof(vars));
238 vars.family = AF_UNSPEC;
239 vars.vars = cupsArrayNew((cups_array_func_t)compare_vars, NULL);
240
241 /*
242 * We need at least:
243 *
244 * ipptool URI testfile
245 */
246
247 interval = 0;
248 repeat = 0;
249 status = 0;
250 testfile = NULL;
251
252 for (i = 1; i < argc; i ++)
253 {
254 if (argv[i][0] == '-')
255 {
256 for (opt = argv[i] + 1; *opt; opt ++)
257 {
258 switch (*opt)
259 {
260 case '4' : /* Connect using IPv4 only */
261 vars.family = AF_INET;
262 break;
263
264 #ifdef AF_INET6
265 case '6' : /* Connect using IPv6 only */
266 vars.family = AF_INET6;
267 break;
268 #endif /* AF_INET6 */
269
270 case 'C' : /* Enable HTTP chunking */
271 Transfer = _CUPS_TRANSFER_CHUNKED;
272 break;
273
274 case 'E' : /* Encrypt with TLS */
275 #ifdef HAVE_SSL
276 vars.encryption = HTTP_ENCRYPT_REQUIRED;
277 #else
278 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
279 argv[0]);
280 #endif /* HAVE_SSL */
281 break;
282
283 case 'I' : /* Ignore errors */
284 IgnoreErrors = 1;
285 break;
286
287 case 'L' : /* Disable HTTP chunking */
288 Transfer = _CUPS_TRANSFER_LENGTH;
289 break;
290
291 case 'S' : /* Encrypt with SSL */
292 #ifdef HAVE_SSL
293 vars.encryption = HTTP_ENCRYPT_ALWAYS;
294 #else
295 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
296 argv[0]);
297 #endif /* HAVE_SSL */
298 break;
299
300 case 'T' : /* Set timeout */
301 i ++;
302
303 if (i >= argc)
304 {
305 _cupsLangPuts(stderr,
306 _("ipptool: Missing timeout for \"-T\"."));
307 usage();
308 }
309
310 vars.timeout = _cupsStrScand(argv[i], NULL, localeconv());
311 break;
312
313 case 'V' : /* Set IPP version */
314 i ++;
315
316 if (i >= argc)
317 {
318 _cupsLangPuts(stderr,
319 _("ipptool: Missing version for \"-V\"."));
320 usage();
321 }
322
323 if (!strcmp(argv[i], "1.0"))
324 Version = 10;
325 else if (!strcmp(argv[i], "1.1"))
326 Version = 11;
327 else if (!strcmp(argv[i], "2.0"))
328 Version = 20;
329 else if (!strcmp(argv[i], "2.1"))
330 Version = 21;
331 else if (!strcmp(argv[i], "2.2"))
332 Version = 22;
333 else
334 {
335 _cupsLangPrintf(stderr,
336 _("ipptool: Bad version %s for \"-V\"."),
337 argv[i]);
338 usage();
339 }
340 break;
341
342 case 'X' : /* Produce XML output */
343 Output = _CUPS_OUTPUT_PLIST;
344
345 if (interval || repeat)
346 {
347 _cupsLangPuts(stderr, _("ipptool: \"-i\" and \"-n\" are "
348 "incompatible with -X\"."));
349 usage();
350 }
351 break;
352
353 case 'c' : /* CSV output */
354 Output = _CUPS_OUTPUT_CSV;
355 break;
356
357 case 'd' : /* Define a variable */
358 i ++;
359
360 if (i >= argc)
361 {
362 _cupsLangPuts(stderr,
363 _("ipptool: Missing name=value for \"-d\"."));
364 usage();
365 }
366
367 strlcpy(name, argv[i], sizeof(name));
368 if ((value = strchr(name, '=')) != NULL)
369 *value++ = '\0';
370 else
371 value = name + strlen(name);
372
373 set_variable(&vars, name, value);
374 break;
375
376 case 'f' : /* Set the default test filename */
377 i ++;
378
379 if (i >= argc)
380 {
381 _cupsLangPuts(stderr,
382 _("ipptool: Missing filename for \"-f\"."));
383 usage();
384 }
385
386 if (access(argv[i], 0) && argv[i][0] != '/')
387 {
388 snprintf(filename, sizeof(filename), "%s/ipptool/%s",
389 cg->cups_datadir, argv[i]);
390 if (access(argv[i], 0))
391 vars.filename = argv[i];
392 else
393 vars.filename = filename;
394 }
395 else
396 vars.filename = argv[i];
397 break;
398
399 case 'i' : /* Test every N seconds */
400 i ++;
401
402 if (i >= argc)
403 {
404 _cupsLangPuts(stderr,
405 _("ipptool: Missing seconds for \"-i\"."));
406 usage();
407 }
408 else
409 {
410 interval = (int)(_cupsStrScand(argv[i], NULL, localeconv()) *
411 1000000.0);
412 if (interval <= 0)
413 {
414 _cupsLangPuts(stderr,
415 _("ipptool: Invalid seconds for \"-i\"."));
416 usage();
417 }
418 }
419
420 if (Output == _CUPS_OUTPUT_PLIST && interval)
421 {
422 _cupsLangPuts(stderr, _("ipptool: \"-i\" is incompatible with "
423 "\"-X\"."));
424 usage();
425 }
426 break;
427
428 case 'l' : /* List as a table */
429 Output = _CUPS_OUTPUT_LIST;
430 break;
431
432 case 'n' : /* Repeat count */
433 i ++;
434
435 if (i >= argc)
436 {
437 _cupsLangPuts(stderr,
438 _("ipptool: Missing count for \"-n\"."));
439 usage();
440 }
441 else
442 repeat = atoi(argv[i]);
443
444 if (Output == _CUPS_OUTPUT_PLIST && repeat)
445 {
446 _cupsLangPuts(stderr, _("ipptool: \"-n\" is incompatible with "
447 "\"-X\"."));
448 usage();
449 }
450 break;
451
452 case 'q' : /* Be quiet */
453 Output = _CUPS_OUTPUT_QUIET;
454 break;
455
456 case 't' : /* CUPS test output */
457 Output = _CUPS_OUTPUT_TEST;
458 break;
459
460 case 'v' : /* Be verbose */
461 Verbosity ++;
462 break;
463
464 default :
465 _cupsLangPrintf(stderr, _("ipptool: Unknown option \"-%c\"."),
466 *opt);
467 usage();
468 break;
469 }
470 }
471 }
472 else if (!strncmp(argv[i], "ipp://", 6) || !strncmp(argv[i], "http://", 7)
473 #ifdef HAVE_SSL
474 || !strncmp(argv[i], "ipps://", 7)
475 || !strncmp(argv[i], "https://", 8)
476 #endif /* HAVE_SSL */
477 )
478 {
479 /*
480 * Set URI...
481 */
482
483 if (vars.uri)
484 {
485 _cupsLangPuts(stderr, _("ipptool: May only specify a single URI."));
486 usage();
487 }
488
489 #ifdef HAVE_SSL
490 if (!strncmp(argv[i], "ipps://", 7) || !strncmp(argv[i], "https://", 8))
491 vars.encryption = HTTP_ENCRYPT_ALWAYS;
492 #endif /* HAVE_SSL */
493
494 vars.uri = argv[i];
495 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL, vars.uri,
496 vars.scheme, sizeof(vars.scheme),
497 vars.userpass, sizeof(vars.userpass),
498 vars.hostname, sizeof(vars.hostname),
499 &(vars.port),
500 vars.resource, sizeof(vars.resource));
501
502 if (uri_status != HTTP_URI_OK)
503 {
504 _cupsLangPrintf(stderr, _("ipptool: Bad URI - %s."),
505 URIStatusStrings[uri_status - HTTP_URI_OVERFLOW]);
506 return (1);
507 }
508
509 if (vars.userpass[0])
510 {
511 if ((Password = strchr(vars.userpass, ':')) != NULL)
512 *Password++ = '\0';
513
514 cupsSetUser(vars.userpass);
515 cupsSetPasswordCB(password_cb);
516 set_variable(&vars, "uriuser", vars.userpass);
517 }
518 }
519 else
520 {
521 /*
522 * Run test...
523 */
524
525 if (!vars.uri)
526 {
527 _cupsLangPuts(stderr, _("ipptool: URI required before test file."));
528 usage();
529 }
530
531 if (access(argv[i], 0) && argv[i][0] != '/')
532 {
533 snprintf(testname, sizeof(testname), "%s/ipptool/%s", cg->cups_datadir,
534 argv[i]);
535 if (access(testname, 0))
536 testfile = argv[i];
537 else
538 testfile = testname;
539 }
540 else
541 testfile = argv[i];
542
543 if (!do_tests(&vars, testfile))
544 status = 1;
545 }
546 }
547
548 if (!vars.uri || !testfile)
549 usage();
550
551 /*
552 * Loop if the interval is set...
553 */
554
555 if (Output == _CUPS_OUTPUT_PLIST)
556 print_xml_trailer(!status, NULL);
557 else if (interval > 0 && repeat > 0)
558 {
559 while (repeat > 1)
560 {
561 usleep(interval);
562 do_tests(&vars, testfile);
563 repeat --;
564 }
565 }
566 else if (interval > 0)
567 {
568 for (;;)
569 {
570 usleep(interval);
571 do_tests(&vars, testfile);
572 }
573 }
574
575 /*
576 * Exit...
577 */
578
579 return (status);
580 }
581
582
583 /*
584 * 'compare_vars()' - Compare two variables.
585 */
586
587 static int /* O - Result of comparison */
588 compare_vars(_cups_var_t *a, /* I - First variable */
589 _cups_var_t *b) /* I - Second variable */
590 {
591 return (_cups_strcasecmp(a->name, b->name));
592 }
593
594
595 /*
596 * 'do_tests()' - Do tests as specified in the test file.
597 */
598
599 static int /* 1 = success, 0 = failure */
600 do_tests(_cups_vars_t *vars, /* I - Variables */
601 const char *testfile) /* I - Test file to use */
602 {
603 int i, /* Looping var */
604 linenum, /* Current line number */
605 pass, /* Did we pass the test? */
606 prev_pass = 1, /* Did we pass the previous test? */
607 request_id, /* Current request ID */
608 show_header = 1, /* Show the test header? */
609 ignore_errors, /* Ignore test failures? */
610 skip_previous = 0; /* Skip on previous test failure? */
611 http_t *http = NULL; /* HTTP connection to server */
612 FILE *fp = NULL; /* Test file */
613 char resource[512], /* Resource for request */
614 token[1024], /* Token from file */
615 *tokenptr, /* Pointer into token */
616 temp[1024]; /* Temporary string */
617 ipp_t *request = NULL; /* IPP request */
618 ipp_t *response = NULL; /* IPP response */
619 char attr[128]; /* Attribute name */
620 ipp_op_t op; /* Operation */
621 ipp_tag_t group; /* Current group */
622 ipp_tag_t value; /* Current value type */
623 ipp_attribute_t *attrptr, /* Attribute pointer */
624 *found, /* Found attribute */
625 *lastcol = NULL; /* Last collection attribute */
626 char name[1024]; /* Name of test */
627 char filename[1024]; /* Filename */
628 _cups_transfer_t transfer; /* To chunk or not to chunk */
629 int version, /* IPP version number to use */
630 skip_test; /* Skip this test? */
631 int num_statuses = 0; /* Number of valid status codes */
632 _cups_status_t statuses[100], /* Valid status codes */
633 *last_status; /* Last STATUS (for predicates) */
634 int num_expects = 0; /* Number of expected attributes */
635 _cups_expect_t expects[200], /* Expected attributes */
636 *expect, /* Current expected attribute */
637 *last_expect; /* Last EXPECT (for predicates) */
638 int num_displayed = 0; /* Number of displayed attributes */
639 char *displayed[200]; /* Displayed attributes */
640 size_t widths[200]; /* Width of columns */
641
642
643 /*
644 * Open the test file...
645 */
646
647 if ((fp = fopen(testfile, "r")) == NULL)
648 {
649 print_fatal_error("Unable to open test file %s - %s", testfile,
650 strerror(errno));
651 pass = 0;
652 goto test_exit;
653 }
654
655 /*
656 * Connect to the server...
657 */
658
659 if ((http = _httpCreate(vars->hostname, vars->port, NULL, vars->encryption,
660 vars->family)) == NULL)
661 {
662 print_fatal_error("Unable to connect to %s on port %d - %s", vars->hostname,
663 vars->port, strerror(errno));
664 pass = 0;
665 goto test_exit;
666 }
667
668 if (httpReconnect(http))
669 {
670 print_fatal_error("Unable to connect to %s on port %d - %s", vars->hostname,
671 vars->port, strerror(errno));
672 pass = 0;
673 goto test_exit;
674 }
675
676 if (vars->timeout > 0.0)
677 httpSetTimeout(http, vars->timeout, timeout_cb, NULL);
678
679 /*
680 * Loop on tests...
681 */
682
683 CUPS_SRAND(time(NULL));
684
685 pass = 1;
686 linenum = 1;
687 request_id = (CUPS_RAND() % 1000) * 137 + 1;
688
689 while (get_token(fp, token, sizeof(token), &linenum) != NULL)
690 {
691 /*
692 * Expect an open brace...
693 */
694
695 if (!strcmp(token, "DEFINE"))
696 {
697 /*
698 * DEFINE name value
699 */
700
701 if (get_token(fp, attr, sizeof(attr), &linenum) &&
702 get_token(fp, temp, sizeof(temp), &linenum))
703 {
704 expand_variables(vars, token, temp, sizeof(token));
705 set_variable(vars, attr, token);
706 }
707 else
708 {
709 print_fatal_error("Missing DEFINE name and/or value on line %d.",
710 linenum);
711 pass = 0;
712 goto test_exit;
713 }
714
715 continue;
716 }
717 else if (!strcmp(token, "DEFINE-DEFAULT"))
718 {
719 /*
720 * DEFINE-DEFAULT name value
721 */
722
723 if (get_token(fp, attr, sizeof(attr), &linenum) &&
724 get_token(fp, temp, sizeof(temp), &linenum))
725 {
726 expand_variables(vars, token, temp, sizeof(token));
727 if (!get_variable(vars, attr))
728 set_variable(vars, attr, token);
729 }
730 else
731 {
732 print_fatal_error("Missing DEFINE-DEFAULT name and/or value on line "
733 "%d.", linenum);
734 pass = 0;
735 goto test_exit;
736 }
737
738 continue;
739 }
740 else if (!strcmp(token, "IGNORE-ERRORS"))
741 {
742 /*
743 * IGNORE-ERRORS yes
744 * IGNORE-ERRORS no
745 */
746
747 if (get_token(fp, temp, sizeof(temp), &linenum) &&
748 (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no")))
749 {
750 IgnoreErrors = !_cups_strcasecmp(temp, "yes");
751 }
752 else
753 {
754 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum);
755 pass = 0;
756 goto test_exit;
757 }
758
759 continue;
760 }
761 else if (!strcmp(token, "INCLUDE"))
762 {
763 /*
764 * INCLUDE "filename"
765 * INCLUDE <filename>
766 */
767
768 if (get_token(fp, temp, sizeof(temp), &linenum))
769 {
770 /*
771 * Map the filename to and then run the tests...
772 */
773
774 if (!do_tests(vars, get_filename(testfile, filename, temp,
775 sizeof(filename))))
776 {
777 pass = 0;
778
779 if (!IgnoreErrors)
780 goto test_exit;
781 }
782 }
783 else
784 {
785 print_fatal_error("Missing INCLUDE filename on line %d.", linenum);
786 pass = 0;
787 goto test_exit;
788 }
789
790 show_header = 1;
791 continue;
792 }
793 else if (!strcmp(token, "INCLUDE-IF-DEFINED"))
794 {
795 /*
796 * INCLUDE-IF-DEFINED name "filename"
797 * INCLUDE-IF-DEFINED name <filename>
798 */
799
800 if (get_token(fp, attr, sizeof(attr), &linenum) &&
801 get_token(fp, temp, sizeof(temp), &linenum))
802 {
803 /*
804 * Map the filename to and then run the tests...
805 */
806
807 if (get_variable(vars, attr) &&
808 !do_tests(vars, get_filename(testfile, filename, temp,
809 sizeof(filename))))
810 {
811 pass = 0;
812
813 if (!IgnoreErrors)
814 goto test_exit;
815 }
816 }
817 else
818 {
819 print_fatal_error("Missing INCLUDE-IF-DEFINED name or filename on line "
820 "%d.", linenum);
821 pass = 0;
822 goto test_exit;
823 }
824
825 show_header = 1;
826 continue;
827 }
828 else if (!strcmp(token, "INCLUDE-IF-NOT-DEFINED"))
829 {
830 /*
831 * INCLUDE-IF-NOT-DEFINED name "filename"
832 * INCLUDE-IF-NOT-DEFINED name <filename>
833 */
834
835 if (get_token(fp, attr, sizeof(attr), &linenum) &&
836 get_token(fp, temp, sizeof(temp), &linenum))
837 {
838 /*
839 * Map the filename to and then run the tests...
840 */
841
842 if (!get_variable(vars, attr) &&
843 !do_tests(vars, get_filename(testfile, filename, temp,
844 sizeof(filename))))
845 {
846 pass = 0;
847
848 if (!IgnoreErrors)
849 goto test_exit;
850 }
851 }
852 else
853 {
854 print_fatal_error("Missing INCLUDE-IF-NOT-DEFINED name or filename on "
855 "line %d.", linenum);
856 pass = 0;
857 goto test_exit;
858 }
859
860 show_header = 1;
861 continue;
862 }
863 else if (!strcmp(token, "SKIP-IF-DEFINED"))
864 {
865 /*
866 * SKIP-IF-DEFINED variable
867 */
868
869 if (get_token(fp, temp, sizeof(temp), &linenum))
870 {
871 if (get_variable(vars, temp))
872 goto test_exit;
873 }
874 else
875 {
876 print_fatal_error("Missing SKIP-IF-DEFINED variable on line %d.",
877 linenum);
878 pass = 0;
879 goto test_exit;
880 }
881 }
882 else if (!strcmp(token, "SKIP-IF-NOT-DEFINED"))
883 {
884 /*
885 * SKIP-IF-NOT-DEFINED variable
886 */
887
888 if (get_token(fp, temp, sizeof(temp), &linenum))
889 {
890 if (!get_variable(vars, temp))
891 goto test_exit;
892 }
893 else
894 {
895 print_fatal_error("Missing SKIP-IF-NOT-DEFINED variable on line %d.",
896 linenum);
897 pass = 0;
898 goto test_exit;
899 }
900 }
901 else if (!strcmp(token, "TRANSFER"))
902 {
903 /*
904 * TRANSFER auto
905 * TRANSFER chunked
906 * TRANSFER length
907 */
908
909 if (get_token(fp, temp, sizeof(temp), &linenum))
910 {
911 if (!strcmp(temp, "auto"))
912 Transfer = _CUPS_TRANSFER_AUTO;
913 else if (!strcmp(temp, "chunked"))
914 Transfer = _CUPS_TRANSFER_CHUNKED;
915 else if (!strcmp(temp, "length"))
916 Transfer = _CUPS_TRANSFER_LENGTH;
917 else
918 {
919 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp,
920 linenum);
921 pass = 0;
922 goto test_exit;
923 }
924 }
925 else
926 {
927 print_fatal_error("Missing TRANSFER value on line %d.", linenum);
928 pass = 0;
929 goto test_exit;
930 }
931
932 continue;
933 }
934 else if (!strcmp(token, "VERSION"))
935 {
936 if (get_token(fp, temp, sizeof(temp), &linenum))
937 {
938 if (!strcmp(temp, "1.0"))
939 Version = 10;
940 else if (!strcmp(temp, "1.1"))
941 Version = 11;
942 else if (!strcmp(temp, "2.0"))
943 Version = 20;
944 else if (!strcmp(temp, "2.1"))
945 Version = 21;
946 else if (!strcmp(temp, "2.2"))
947 Version = 22;
948 else
949 {
950 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp, linenum);
951 pass = 0;
952 goto test_exit;
953 }
954 }
955 else
956 {
957 print_fatal_error("Missing VERSION number on line %d.", linenum);
958 pass = 0;
959 goto test_exit;
960 }
961
962 continue;
963 }
964 else if (strcmp(token, "{"))
965 {
966 print_fatal_error("Unexpected token %s seen on line %d.", token, linenum);
967 pass = 0;
968 goto test_exit;
969 }
970
971 /*
972 * Initialize things...
973 */
974
975 if (show_header)
976 {
977 if (Output == _CUPS_OUTPUT_PLIST)
978 print_xml_header();
979 else if (Output == _CUPS_OUTPUT_TEST)
980 printf("\"%s\":\n", testfile);
981
982 show_header = 0;
983 }
984
985 strlcpy(resource, vars->resource, sizeof(resource));
986
987 request_id ++;
988 request = ippNew();
989 op = (ipp_op_t)0;
990 group = IPP_TAG_ZERO;
991 ignore_errors = IgnoreErrors;
992 last_expect = NULL;
993 last_status = NULL;
994 filename[0] = '\0';
995 skip_test = 0;
996 version = Version;
997 transfer = Transfer;
998
999 strlcpy(name, testfile, sizeof(name));
1000 if (strrchr(name, '.') != NULL)
1001 *strrchr(name, '.') = '\0';
1002
1003 /*
1004 * Parse until we see a close brace...
1005 */
1006
1007 while (get_token(fp, token, sizeof(token), &linenum) != NULL)
1008 {
1009 if (_cups_strcasecmp(token, "COUNT") &&
1010 _cups_strcasecmp(token, "DEFINE-MATCH") &&
1011 _cups_strcasecmp(token, "DEFINE-NO-MATCH") &&
1012 _cups_strcasecmp(token, "DEFINE-VALUE") &&
1013 _cups_strcasecmp(token, "IF-DEFINED") &&
1014 _cups_strcasecmp(token, "IF-NOT-DEFINED") &&
1015 _cups_strcasecmp(token, "IN-GROUP") &&
1016 _cups_strcasecmp(token, "OF-TYPE") &&
1017 _cups_strcasecmp(token, "SAME-COUNT-AS") &&
1018 _cups_strcasecmp(token, "WITH-VALUE"))
1019 last_expect = NULL;
1020
1021 if (_cups_strcasecmp(token, "IF-DEFINED") &&
1022 _cups_strcasecmp(token, "IF-NOT-DEFINED"))
1023 last_status = NULL;
1024
1025 if (!strcmp(token, "}"))
1026 break;
1027 else if (!strcmp(token, "{") && lastcol)
1028 {
1029 /*
1030 * Another collection value
1031 */
1032
1033 ipp_t *col = get_collection(vars, fp, &linenum);
1034 /* Collection value */
1035
1036 if (col)
1037 {
1038 ipp_attribute_t *tempcol; /* Pointer to new buffer */
1039
1040
1041 /*
1042 * Reallocate memory...
1043 */
1044
1045 if ((tempcol = realloc(lastcol, sizeof(ipp_attribute_t) +
1046 (lastcol->num_values + 1) *
1047 sizeof(ipp_value_t))) == NULL)
1048 {
1049 print_fatal_error("Unable to allocate memory on line %d.", linenum);
1050 pass = 0;
1051 goto test_exit;
1052 }
1053
1054 if (tempcol != lastcol)
1055 {
1056 /*
1057 * Reset pointers in the list...
1058 */
1059
1060 if (request->prev)
1061 request->prev->next = tempcol;
1062 else
1063 request->attrs = tempcol;
1064
1065 lastcol = request->current = request->last = tempcol;
1066 }
1067
1068 lastcol->values[lastcol->num_values].collection = col;
1069 lastcol->num_values ++;
1070 }
1071 else
1072 {
1073 pass = 0;
1074 goto test_exit;
1075 }
1076 }
1077 else if (!strcmp(token, "DEFINE"))
1078 {
1079 /*
1080 * DEFINE name value
1081 */
1082
1083 if (get_token(fp, attr, sizeof(attr), &linenum) &&
1084 get_token(fp, temp, sizeof(temp), &linenum))
1085 {
1086 expand_variables(vars, token, temp, sizeof(token));
1087 set_variable(vars, attr, token);
1088 }
1089 else
1090 {
1091 print_fatal_error("Missing DEFINE name and/or value on line %d.",
1092 linenum);
1093 pass = 0;
1094 goto test_exit;
1095 }
1096 }
1097 else if (!strcmp(token, "IGNORE-ERRORS"))
1098 {
1099 /*
1100 * IGNORE-ERRORS yes
1101 * IGNORE-ERRORS no
1102 */
1103
1104 if (get_token(fp, temp, sizeof(temp), &linenum) &&
1105 (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no")))
1106 {
1107 ignore_errors = !_cups_strcasecmp(temp, "yes");
1108 }
1109 else
1110 {
1111 print_fatal_error("Missing IGNORE-ERRORS value on line %d.", linenum);
1112 pass = 0;
1113 goto test_exit;
1114 }
1115
1116 continue;
1117 }
1118 else if (!_cups_strcasecmp(token, "NAME"))
1119 {
1120 /*
1121 * Name of test...
1122 */
1123
1124 get_token(fp, name, sizeof(name), &linenum);
1125 }
1126 else if (!strcmp(token, "REQUEST-ID"))
1127 {
1128 /*
1129 * REQUEST-ID #
1130 * REQUEST-ID random
1131 */
1132
1133 if (get_token(fp, temp, sizeof(temp), &linenum))
1134 {
1135 if (isdigit(temp[0] & 255))
1136 request_id = atoi(temp);
1137 else if (!_cups_strcasecmp(temp, "random"))
1138 request_id = (CUPS_RAND() % 1000) * 137 + 1;
1139 else
1140 {
1141 print_fatal_error("Bad REQUEST-ID value \"%s\" on line %d.", temp,
1142 linenum);
1143 pass = 0;
1144 goto test_exit;
1145 }
1146 }
1147 else
1148 {
1149 print_fatal_error("Missing REQUEST-ID value on line %d.", linenum);
1150 pass = 0;
1151 goto test_exit;
1152 }
1153 }
1154 else if (!strcmp(token, "SKIP-IF-DEFINED"))
1155 {
1156 /*
1157 * SKIP-IF-DEFINED variable
1158 */
1159
1160 if (get_token(fp, temp, sizeof(temp), &linenum))
1161 {
1162 if (get_variable(vars, temp))
1163 skip_test = 1;
1164 }
1165 else
1166 {
1167 print_fatal_error("Missing SKIP-IF-DEFINED value on line %d.",
1168 linenum);
1169 pass = 0;
1170 goto test_exit;
1171 }
1172 }
1173 else if (!strcmp(token, "SKIP-IF-NOT-DEFINED"))
1174 {
1175 /*
1176 * SKIP-IF-NOT-DEFINED variable
1177 */
1178
1179 if (get_token(fp, temp, sizeof(temp), &linenum))
1180 {
1181 if (!get_variable(vars, temp))
1182 skip_test = 1;
1183 }
1184 else
1185 {
1186 print_fatal_error("Missing SKIP-IF-NOT-DEFINED value on line %d.",
1187 linenum);
1188 pass = 0;
1189 goto test_exit;
1190 }
1191 }
1192 else if (!strcmp(token, "SKIP-PREVIOUS-ERROR"))
1193 {
1194 /*
1195 * SKIP-PREVIOUS-ERROR yes
1196 * SKIP-PREVIOUS-ERROR no
1197 */
1198
1199 if (get_token(fp, temp, sizeof(temp), &linenum) &&
1200 (!_cups_strcasecmp(temp, "yes") || !_cups_strcasecmp(temp, "no")))
1201 {
1202 skip_previous = !_cups_strcasecmp(temp, "yes");
1203 }
1204 else
1205 {
1206 print_fatal_error("Missing SKIP-PREVIOUS-ERROR value on line %d.", linenum);
1207 pass = 0;
1208 goto test_exit;
1209 }
1210
1211 continue;
1212 }
1213 else if (!strcmp(token, "TRANSFER"))
1214 {
1215 /*
1216 * TRANSFER auto
1217 * TRANSFER chunked
1218 * TRANSFER length
1219 */
1220
1221 if (get_token(fp, temp, sizeof(temp), &linenum))
1222 {
1223 if (!strcmp(temp, "auto"))
1224 transfer = _CUPS_TRANSFER_AUTO;
1225 else if (!strcmp(temp, "chunked"))
1226 transfer = _CUPS_TRANSFER_CHUNKED;
1227 else if (!strcmp(temp, "length"))
1228 transfer = _CUPS_TRANSFER_LENGTH;
1229 else
1230 {
1231 print_fatal_error("Bad TRANSFER value \"%s\" on line %d.", temp,
1232 linenum);
1233 pass = 0;
1234 goto test_exit;
1235 }
1236 }
1237 else
1238 {
1239 print_fatal_error("Missing TRANSFER value on line %d.", linenum);
1240 pass = 0;
1241 goto test_exit;
1242 }
1243 }
1244 else if (!_cups_strcasecmp(token, "VERSION"))
1245 {
1246 if (get_token(fp, temp, sizeof(temp), &linenum))
1247 {
1248 if (!strcmp(temp, "0.0"))
1249 version = 0;
1250 else if (!strcmp(temp, "1.0"))
1251 version = 10;
1252 else if (!strcmp(temp, "1.1"))
1253 version = 11;
1254 else if (!strcmp(temp, "2.0"))
1255 version = 20;
1256 else if (!strcmp(temp, "2.1"))
1257 version = 21;
1258 else if (!strcmp(temp, "2.2"))
1259 version = 22;
1260 else
1261 {
1262 print_fatal_error("Bad VERSION \"%s\" on line %d.", temp, linenum);
1263 pass = 0;
1264 goto test_exit;
1265 }
1266 }
1267 else
1268 {
1269 print_fatal_error("Missing VERSION number on line %d.", linenum);
1270 pass = 0;
1271 goto test_exit;
1272 }
1273 }
1274 else if (!_cups_strcasecmp(token, "RESOURCE"))
1275 {
1276 /*
1277 * Resource name...
1278 */
1279
1280 if (!get_token(fp, resource, sizeof(resource), &linenum))
1281 {
1282 print_fatal_error("Missing RESOURCE path on line %d.", linenum);
1283 pass = 0;
1284 goto test_exit;
1285 }
1286 }
1287 else if (!_cups_strcasecmp(token, "OPERATION"))
1288 {
1289 /*
1290 * Operation...
1291 */
1292
1293 if (!get_token(fp, token, sizeof(token), &linenum))
1294 {
1295 print_fatal_error("Missing OPERATION code on line %d.", linenum);
1296 pass = 0;
1297 goto test_exit;
1298 }
1299
1300 if ((op = ippOpValue(token)) == (ipp_op_t)-1 &&
1301 (op = strtol(token, NULL, 0)) == 0)
1302 {
1303 print_fatal_error("Bad OPERATION code \"%s\" on line %d.", token,
1304 linenum);
1305 pass = 0;
1306 goto test_exit;
1307 }
1308 }
1309 else if (!_cups_strcasecmp(token, "GROUP"))
1310 {
1311 /*
1312 * Attribute group...
1313 */
1314
1315 if (!get_token(fp, token, sizeof(token), &linenum))
1316 {
1317 print_fatal_error("Missing GROUP tag on line %d.", linenum);
1318 pass = 0;
1319 goto test_exit;
1320 }
1321
1322 if ((value = ippTagValue(token)) < 0)
1323 {
1324 print_fatal_error("Bad GROUP tag \"%s\" on line %d.", token, linenum);
1325 pass = 0;
1326 goto test_exit;
1327 }
1328
1329 if (value == group)
1330 ippAddSeparator(request);
1331
1332 group = value;
1333 }
1334 else if (!_cups_strcasecmp(token, "DELAY"))
1335 {
1336 /*
1337 * Delay before operation...
1338 */
1339
1340 double delay;
1341
1342 if (!get_token(fp, token, sizeof(token), &linenum))
1343 {
1344 print_fatal_error("Missing DELAY value on line %d.", linenum);
1345 pass = 0;
1346 goto test_exit;
1347 }
1348
1349 if ((delay = _cupsStrScand(token, NULL, localeconv())) <= 0.0)
1350 {
1351 print_fatal_error("Bad DELAY value \"%s\" on line %d.", token,
1352 linenum);
1353 pass = 0;
1354 goto test_exit;
1355 }
1356 else
1357 {
1358 if (Output == _CUPS_OUTPUT_TEST)
1359 printf(" [%g second delay]\n", delay);
1360
1361 usleep((int)(1000000.0 * delay));
1362 }
1363 }
1364 else if (!_cups_strcasecmp(token, "ATTR"))
1365 {
1366 /*
1367 * Attribute...
1368 */
1369
1370 if (!get_token(fp, token, sizeof(token), &linenum))
1371 {
1372 print_fatal_error("Missing ATTR value tag on line %d.", linenum);
1373 pass = 0;
1374 goto test_exit;
1375 }
1376
1377 if ((value = ippTagValue(token)) == IPP_TAG_ZERO)
1378 {
1379 print_fatal_error("Bad ATTR value tag \"%s\" on line %d.", token,
1380 linenum);
1381 pass = 0;
1382 goto test_exit;
1383 }
1384
1385 if (!get_token(fp, attr, sizeof(attr), &linenum))
1386 {
1387 print_fatal_error("Missing ATTR name on line %d.", linenum);
1388 pass = 0;
1389 goto test_exit;
1390 }
1391
1392 if (!get_token(fp, temp, sizeof(temp), &linenum))
1393 {
1394 print_fatal_error("Missing ATTR value on line %d.", linenum);
1395 pass = 0;
1396 goto test_exit;
1397 }
1398
1399 expand_variables(vars, token, temp, sizeof(token));
1400
1401 switch (value)
1402 {
1403 case IPP_TAG_BOOLEAN :
1404 if (!_cups_strcasecmp(token, "true"))
1405 ippAddBoolean(request, group, attr, 1);
1406 else
1407 ippAddBoolean(request, group, attr, atoi(token));
1408 break;
1409
1410 case IPP_TAG_INTEGER :
1411 case IPP_TAG_ENUM :
1412 if (!strchr(token, ','))
1413 ippAddInteger(request, group, value, attr,
1414 strtol(token, &tokenptr, 0));
1415 else
1416 {
1417 int values[100], /* Values */
1418 num_values = 1; /* Number of values */
1419
1420 values[0] = strtol(token, &tokenptr, 10);
1421 while (tokenptr && *tokenptr &&
1422 num_values < (int)(sizeof(values) / sizeof(values[0])))
1423 {
1424 if (*tokenptr == ',')
1425 tokenptr ++;
1426 else if (!isdigit(*tokenptr & 255) && *tokenptr != '-')
1427 break;
1428
1429 values[num_values] = strtol(tokenptr, &tokenptr, 0);
1430 num_values ++;
1431 }
1432
1433 ippAddIntegers(request, group, value, attr, num_values, values);
1434 }
1435
1436 if (!tokenptr || *tokenptr)
1437 {
1438 print_fatal_error("Bad %s value \"%s\" on line %d.",
1439 ippTagString(value), token, linenum);
1440 pass = 0;
1441 goto test_exit;
1442 }
1443 break;
1444
1445 case IPP_TAG_RESOLUTION :
1446 {
1447 int xres, /* X resolution */
1448 yres; /* Y resolution */
1449 char *ptr; /* Pointer into value */
1450
1451 xres = yres = strtol(token, (char **)&ptr, 10);
1452 if (ptr > token && xres > 0)
1453 {
1454 if (*ptr == 'x')
1455 yres = strtol(ptr + 1, (char **)&ptr, 10);
1456 }
1457
1458 if (ptr <= token || xres <= 0 || yres <= 0 || !ptr ||
1459 (_cups_strcasecmp(ptr, "dpi") && _cups_strcasecmp(ptr, "dpc") &&
1460 _cups_strcasecmp(ptr, "other")))
1461 {
1462 print_fatal_error("Bad resolution value \"%s\" on line %d.",
1463 token, linenum);
1464 pass = 0;
1465 goto test_exit;
1466 }
1467
1468 if (!_cups_strcasecmp(ptr, "dpi"))
1469 ippAddResolution(request, group, attr, IPP_RES_PER_INCH,
1470 xres, yres);
1471 else if (!_cups_strcasecmp(ptr, "dpc"))
1472 ippAddResolution(request, group, attr, IPP_RES_PER_CM,
1473 xres, yres);
1474 else
1475 ippAddResolution(request, group, attr, (ipp_res_t)0,
1476 xres, yres);
1477 }
1478 break;
1479
1480 case IPP_TAG_RANGE :
1481 {
1482 int lowers[4], /* Lower value */
1483 uppers[4], /* Upper values */
1484 num_vals; /* Number of values */
1485
1486
1487 num_vals = sscanf(token, "%d-%d,%d-%d,%d-%d,%d-%d",
1488 lowers + 0, uppers + 0,
1489 lowers + 1, uppers + 1,
1490 lowers + 2, uppers + 2,
1491 lowers + 3, uppers + 3);
1492
1493 if ((num_vals & 1) || num_vals == 0)
1494 {
1495 print_fatal_error("Bad rangeOfInteger value \"%s\" on line "
1496 "%d.", token, linenum);
1497 pass = 0;
1498 goto test_exit;
1499 }
1500
1501 ippAddRanges(request, group, attr, num_vals / 2, lowers,
1502 uppers);
1503 }
1504 break;
1505
1506 case IPP_TAG_BEGIN_COLLECTION :
1507 if (!strcmp(token, "{"))
1508 {
1509 ipp_t *col = get_collection(vars, fp, &linenum);
1510 /* Collection value */
1511
1512 if (col)
1513 {
1514 lastcol = ippAddCollection(request, group, attr, col);
1515 ippDelete(col);
1516 }
1517 else
1518 {
1519 pass = 0;
1520 goto test_exit;
1521 }
1522 }
1523 else
1524 {
1525 print_fatal_error("Bad ATTR collection value on line %d.",
1526 linenum);
1527 pass = 0;
1528 goto test_exit;
1529 }
1530 break;
1531
1532 default :
1533 print_fatal_error("Unsupported ATTR value tag %s on line %d.",
1534 ippTagString(value), linenum);
1535 pass = 0;
1536 goto test_exit;
1537
1538 case IPP_TAG_TEXTLANG :
1539 case IPP_TAG_NAMELANG :
1540 case IPP_TAG_TEXT :
1541 case IPP_TAG_NAME :
1542 case IPP_TAG_KEYWORD :
1543 case IPP_TAG_URI :
1544 case IPP_TAG_URISCHEME :
1545 case IPP_TAG_CHARSET :
1546 case IPP_TAG_LANGUAGE :
1547 case IPP_TAG_MIMETYPE :
1548 if (!strchr(token, ','))
1549 ippAddString(request, group, value, attr, NULL, token);
1550 else
1551 {
1552 /*
1553 * Multiple string values...
1554 */
1555
1556 int num_values; /* Number of values */
1557 char *values[100], /* Values */
1558 *ptr; /* Pointer to next value */
1559
1560
1561 values[0] = token;
1562 num_values = 1;
1563
1564 for (ptr = strchr(token, ','); ptr; ptr = strchr(ptr, ','))
1565 {
1566 *ptr++ = '\0';
1567 values[num_values] = ptr;
1568 num_values ++;
1569 }
1570
1571 ippAddStrings(request, group, value, attr, num_values,
1572 NULL, (const char **)values);
1573 }
1574 break;
1575 }
1576 }
1577 else if (!_cups_strcasecmp(token, "FILE"))
1578 {
1579 /*
1580 * File...
1581 */
1582
1583 if (!get_token(fp, temp, sizeof(temp), &linenum))
1584 {
1585 print_fatal_error("Missing FILE filename on line %d.", linenum);
1586 pass = 0;
1587 goto test_exit;
1588 }
1589
1590 expand_variables(vars, token, temp, sizeof(token));
1591 get_filename(testfile, filename, token, sizeof(filename));
1592 }
1593 else if (!_cups_strcasecmp(token, "STATUS"))
1594 {
1595 /*
1596 * Status...
1597 */
1598
1599 if (num_statuses >= (int)(sizeof(statuses) / sizeof(statuses[0])))
1600 {
1601 print_fatal_error("Too many STATUS's on line %d.", linenum);
1602 pass = 0;
1603 goto test_exit;
1604 }
1605
1606 if (!get_token(fp, token, sizeof(token), &linenum))
1607 {
1608 print_fatal_error("Missing STATUS code on line %d.", linenum);
1609 pass = 0;
1610 goto test_exit;
1611 }
1612
1613 if ((statuses[num_statuses].status = ippErrorValue(token))
1614 == (ipp_status_t)-1 &&
1615 (statuses[num_statuses].status = strtol(token, NULL, 0)) == 0)
1616 {
1617 print_fatal_error("Bad STATUS code \"%s\" on line %d.", token,
1618 linenum);
1619 pass = 0;
1620 goto test_exit;
1621 }
1622
1623 last_status = statuses + num_statuses;
1624 num_statuses ++;
1625
1626 last_status->if_defined = NULL;
1627 last_status->if_not_defined = NULL;
1628 }
1629 else if (!_cups_strcasecmp(token, "EXPECT"))
1630 {
1631 /*
1632 * Expected attributes...
1633 */
1634
1635 if (num_expects >= (int)(sizeof(expects) / sizeof(expects[0])))
1636 {
1637 print_fatal_error("Too many EXPECT's on line %d.", linenum);
1638 pass = 0;
1639 goto test_exit;
1640 }
1641
1642 if (!get_token(fp, token, sizeof(token), &linenum))
1643 {
1644 print_fatal_error("Missing EXPECT name on line %d.", linenum);
1645 pass = 0;
1646 goto test_exit;
1647 }
1648
1649 last_expect = expects + num_expects;
1650 num_expects ++;
1651
1652 memset(last_expect, 0, sizeof(_cups_expect_t));
1653
1654 if (token[0] == '!')
1655 {
1656 last_expect->not_expect = 1;
1657 last_expect->name = strdup(token + 1);
1658 }
1659 else if (token[0] == '?')
1660 {
1661 last_expect->optional = 1;
1662 last_expect->name = strdup(token + 1);
1663 }
1664 else
1665 last_expect->name = strdup(token);
1666 }
1667 else if (!_cups_strcasecmp(token, "COUNT"))
1668 {
1669 if (!get_token(fp, token, sizeof(token), &linenum))
1670 {
1671 print_fatal_error("Missing COUNT number on line %d.", linenum);
1672 pass = 0;
1673 goto test_exit;
1674 }
1675
1676 if ((i = atoi(token)) <= 0)
1677 {
1678 print_fatal_error("Bad COUNT \"%s\" on line %d.", token, linenum);
1679 pass = 0;
1680 goto test_exit;
1681 }
1682
1683 if (last_expect)
1684 last_expect->count = i;
1685 else
1686 {
1687 print_fatal_error("COUNT without a preceding EXPECT on line %d.",
1688 linenum);
1689 pass = 0;
1690 goto test_exit;
1691 }
1692 }
1693 else if (!_cups_strcasecmp(token, "DEFINE-MATCH"))
1694 {
1695 if (!get_token(fp, token, sizeof(token), &linenum))
1696 {
1697 print_fatal_error("Missing DEFINE-MATCH variable on line %d.",
1698 linenum);
1699 pass = 0;
1700 goto test_exit;
1701 }
1702
1703 if (last_expect)
1704 last_expect->define_match = strdup(token);
1705 else
1706 {
1707 print_fatal_error("DEFINE-MATCH without a preceding EXPECT on line "
1708 "%d.", linenum);
1709 pass = 0;
1710 goto test_exit;
1711 }
1712 }
1713 else if (!_cups_strcasecmp(token, "DEFINE-NO-MATCH"))
1714 {
1715 if (!get_token(fp, token, sizeof(token), &linenum))
1716 {
1717 print_fatal_error("Missing DEFINE-NO-MATCH variable on line %d.",
1718 linenum);
1719 pass = 0;
1720 goto test_exit;
1721 }
1722
1723 if (last_expect)
1724 last_expect->define_no_match = strdup(token);
1725 else
1726 {
1727 print_fatal_error("DEFINE-NO-MATCH without a preceding EXPECT on "
1728 "line %d.", linenum);
1729 pass = 0;
1730 goto test_exit;
1731 }
1732 }
1733 else if (!_cups_strcasecmp(token, "DEFINE-VALUE"))
1734 {
1735 if (!get_token(fp, token, sizeof(token), &linenum))
1736 {
1737 print_fatal_error("Missing DEFINE-VALUE variable on line %d.",
1738 linenum);
1739 pass = 0;
1740 goto test_exit;
1741 }
1742
1743 if (last_expect)
1744 last_expect->define_value = strdup(token);
1745 else
1746 {
1747 print_fatal_error("DEFINE-VALUE without a preceding EXPECT on line "
1748 "%d.", linenum);
1749 pass = 0;
1750 goto test_exit;
1751 }
1752 }
1753 else if (!_cups_strcasecmp(token, "OF-TYPE"))
1754 {
1755 if (!get_token(fp, token, sizeof(token), &linenum))
1756 {
1757 print_fatal_error("Missing OF-TYPE value tag(s) on line %d.",
1758 linenum);
1759 pass = 0;
1760 goto test_exit;
1761 }
1762
1763 if (last_expect)
1764 last_expect->of_type = strdup(token);
1765 else
1766 {
1767 print_fatal_error("OF-TYPE without a preceding EXPECT on line %d.",
1768 linenum);
1769 pass = 0;
1770 goto test_exit;
1771 }
1772 }
1773 else if (!_cups_strcasecmp(token, "IN-GROUP"))
1774 {
1775 ipp_tag_t in_group; /* IN-GROUP value */
1776
1777
1778 if (!get_token(fp, token, sizeof(token), &linenum))
1779 {
1780 print_fatal_error("Missing IN-GROUP group tag on line %d.", linenum);
1781 pass = 0;
1782 goto test_exit;
1783 }
1784
1785 if ((in_group = ippTagValue(token)) == (ipp_tag_t)-1)
1786 {
1787 }
1788 else if (last_expect)
1789 last_expect->in_group = in_group;
1790 else
1791 {
1792 print_fatal_error("IN-GROUP without a preceding EXPECT on line %d.",
1793 linenum);
1794 pass = 0;
1795 goto test_exit;
1796 }
1797 }
1798 else if (!_cups_strcasecmp(token, "SAME-COUNT-AS"))
1799 {
1800 if (!get_token(fp, token, sizeof(token), &linenum))
1801 {
1802 print_fatal_error("Missing SAME-COUNT-AS name on line %d.", linenum);
1803 pass = 0;
1804 goto test_exit;
1805 }
1806
1807 if (last_expect)
1808 last_expect->same_count_as = strdup(token);
1809 else
1810 {
1811 print_fatal_error("SAME-COUNT-AS without a preceding EXPECT on line "
1812 "%d.", linenum);
1813 pass = 0;
1814 goto test_exit;
1815 }
1816 }
1817 else if (!_cups_strcasecmp(token, "IF-DEFINED"))
1818 {
1819 if (!get_token(fp, token, sizeof(token), &linenum))
1820 {
1821 print_fatal_error("Missing IF-DEFINED name on line %d.", linenum);
1822 pass = 0;
1823 goto test_exit;
1824 }
1825
1826 if (last_expect)
1827 last_expect->if_defined = strdup(token);
1828 else if (last_status)
1829 last_status->if_defined = strdup(token);
1830 else
1831 {
1832 print_fatal_error("IF-DEFINED without a preceding EXPECT or STATUS "
1833 "on line %d.", linenum);
1834 pass = 0;
1835 goto test_exit;
1836 }
1837 }
1838 else if (!_cups_strcasecmp(token, "IF-NOT-DEFINED"))
1839 {
1840 if (!get_token(fp, token, sizeof(token), &linenum))
1841 {
1842 print_fatal_error("Missing IF-NOT-DEFINED name on line %d.", linenum);
1843 pass = 0;
1844 goto test_exit;
1845 }
1846
1847 if (last_expect)
1848 last_expect->if_not_defined = strdup(token);
1849 else if (last_status)
1850 last_status->if_not_defined = strdup(token);
1851 else
1852 {
1853 print_fatal_error("IF-NOT-DEFINED without a preceding EXPECT or STATUS "
1854 "on line %d.", linenum);
1855 pass = 0;
1856 goto test_exit;
1857 }
1858 }
1859 else if (!_cups_strcasecmp(token, "WITH-VALUE"))
1860 {
1861 if (!get_token(fp, temp, sizeof(temp), &linenum))
1862 {
1863 print_fatal_error("Missing WITH-VALUE value on line %d.", linenum);
1864 pass = 0;
1865 goto test_exit;
1866 }
1867
1868 if (last_expect)
1869 {
1870 /*
1871 * Expand any variables in the value and then save it.
1872 */
1873
1874 expand_variables(vars, token, temp, sizeof(token));
1875
1876 tokenptr = token + strlen(token) - 1;
1877
1878 if (token[0] == '/' && tokenptr > token && *tokenptr == '/')
1879 {
1880 /*
1881 * WITH-VALUE is a POSIX extended regular expression.
1882 */
1883
1884 last_expect->with_value = calloc(1, tokenptr - token);
1885 last_expect->with_regex = 1;
1886
1887 if (last_expect->with_value)
1888 memcpy(last_expect->with_value, token + 1, tokenptr - token - 1);
1889 }
1890 else
1891 {
1892 /*
1893 * WITH-VALUE is a literal value...
1894 */
1895
1896 last_expect->with_value = strdup(token);
1897 }
1898 }
1899 else
1900 {
1901 print_fatal_error("WITH-VALUE without a preceding EXPECT on line %d.",
1902 linenum);
1903 pass = 0;
1904 goto test_exit;
1905 }
1906 }
1907 else if (!_cups_strcasecmp(token, "DISPLAY"))
1908 {
1909 /*
1910 * Display attributes...
1911 */
1912
1913 if (num_displayed >= (int)(sizeof(displayed) / sizeof(displayed[0])))
1914 {
1915 print_fatal_error("Too many DISPLAY's on line %d", linenum);
1916 pass = 0;
1917 goto test_exit;
1918 }
1919
1920 if (!get_token(fp, token, sizeof(token), &linenum))
1921 {
1922 print_fatal_error("Missing DISPLAY name on line %d.", linenum);
1923 pass = 0;
1924 goto test_exit;
1925 }
1926
1927 displayed[num_displayed] = strdup(token);
1928 num_displayed ++;
1929 }
1930 else
1931 {
1932 print_fatal_error("Unexpected token %s seen on line %d.", token,
1933 linenum);
1934 pass = 0;
1935 goto test_exit;
1936 }
1937 }
1938
1939 /*
1940 * Submit the IPP request...
1941 */
1942
1943 request->request.op.version[0] = version / 10;
1944 request->request.op.version[1] = version % 10;
1945 request->request.op.operation_id = op;
1946 request->request.op.request_id = request_id;
1947
1948 if (Output == _CUPS_OUTPUT_PLIST)
1949 {
1950 puts("<dict>");
1951 puts("<key>Name</key>");
1952 print_xml_string("string", name);
1953 puts("<key>Operation</key>");
1954 print_xml_string("string", ippOpString(op));
1955 puts("<key>RequestAttributes</key>");
1956 puts("<array>");
1957 if (request->attrs)
1958 {
1959 puts("<dict>");
1960 for (attrptr = request->attrs, group = attrptr->group_tag;
1961 attrptr;
1962 attrptr = attrptr->next)
1963 print_attr(attrptr, &group);
1964 puts("</dict>");
1965 }
1966 puts("</array>");
1967 }
1968 else if (Output == _CUPS_OUTPUT_TEST)
1969 {
1970 if (Verbosity)
1971 {
1972 printf(" %s:\n", ippOpString(op));
1973
1974 for (attrptr = request->attrs; attrptr; attrptr = attrptr->next)
1975 print_attr(attrptr, NULL);
1976 }
1977
1978 printf(" %-69.69s [", name);
1979 fflush(stdout);
1980 }
1981
1982 if ((skip_previous && !prev_pass) || skip_test)
1983 {
1984 ippDelete(request);
1985 request = NULL;
1986
1987 if (Output == _CUPS_OUTPUT_PLIST)
1988 {
1989 puts("<key>Successful</key>");
1990 puts("<true />");
1991 puts("<key>StatusCode</key>");
1992 print_xml_string("string", "skip");
1993 puts("<key>ResponseAttributes</key>");
1994 puts("<dict>");
1995 puts("</dict>");
1996 }
1997 else if (Output == _CUPS_OUTPUT_TEST)
1998 puts("SKIP]");
1999
2000 goto skip_error;
2001 }
2002
2003 if (transfer == _CUPS_TRANSFER_CHUNKED ||
2004 (transfer == _CUPS_TRANSFER_AUTO && filename[0]))
2005 {
2006 /*
2007 * Send request using chunking...
2008 */
2009
2010 http_status_t status = cupsSendRequest(http, request, resource, 0);
2011
2012 if (status == HTTP_CONTINUE && filename[0])
2013 {
2014 int fd; /* File to send */
2015 char buffer[8192]; /* Copy buffer */
2016 ssize_t bytes; /* Bytes read/written */
2017
2018 if ((fd = open(filename, O_RDONLY | O_BINARY)) >= 0)
2019 {
2020 while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
2021 if ((status = cupsWriteRequestData(http, buffer,
2022 bytes)) != HTTP_CONTINUE)
2023 break;
2024 }
2025 else
2026 {
2027 snprintf(buffer, sizeof(buffer), "%s: %s", filename, strerror(errno));
2028 _cupsSetError(IPP_INTERNAL_ERROR, buffer, 0);
2029
2030 status = HTTP_ERROR;
2031 }
2032 }
2033
2034 ippDelete(request);
2035
2036 if (status == HTTP_CONTINUE)
2037 response = cupsGetResponse(http, resource);
2038 else
2039 response = NULL;
2040 }
2041 else if (filename[0])
2042 response = cupsDoFileRequest(http, request, resource, filename);
2043 else
2044 response = cupsDoRequest(http, request, resource);
2045
2046 request = NULL;
2047 prev_pass = 1;
2048
2049 if (!response)
2050 prev_pass = pass = 0;
2051 else
2052 {
2053 if (http->version != HTTP_1_1)
2054 prev_pass = pass = 0;
2055
2056 if (response->request.status.request_id != request_id)
2057 prev_pass = pass = 0;
2058
2059 if (version &&
2060 (response->request.status.version[0] != (version / 10) ||
2061 response->request.status.version[1] != (version % 10)))
2062 prev_pass = pass = 0;
2063
2064 if ((attrptr = ippFindAttribute(response, "job-id",
2065 IPP_TAG_INTEGER)) != NULL)
2066 {
2067 snprintf(temp, sizeof(temp), "%d", attrptr->values[0].integer);
2068 set_variable(vars, "job-id", temp);
2069 }
2070
2071 if ((attrptr = ippFindAttribute(response, "job-uri",
2072 IPP_TAG_URI)) != NULL)
2073 set_variable(vars, "job-uri", attrptr->values[0].string.text);
2074
2075 if ((attrptr = ippFindAttribute(response, "notify-subscription-id",
2076 IPP_TAG_INTEGER)) != NULL)
2077 {
2078 snprintf(temp, sizeof(temp), "%d", attrptr->values[0].integer);
2079 set_variable(vars, "notify-subscription-id", temp);
2080 }
2081
2082 attrptr = response->attrs;
2083 if (!attrptr || !attrptr->name ||
2084 attrptr->value_tag != IPP_TAG_CHARSET ||
2085 attrptr->group_tag != IPP_TAG_OPERATION ||
2086 attrptr->num_values != 1 ||
2087 strcmp(attrptr->name, "attributes-charset"))
2088 prev_pass = pass = 0;
2089
2090 if (attrptr)
2091 {
2092 attrptr = attrptr->next;
2093 if (!attrptr || !attrptr->name ||
2094 attrptr->value_tag != IPP_TAG_LANGUAGE ||
2095 attrptr->group_tag != IPP_TAG_OPERATION ||
2096 attrptr->num_values != 1 ||
2097 strcmp(attrptr->name, "attributes-natural-language"))
2098 prev_pass = pass = 0;
2099 }
2100
2101 if ((attrptr = ippFindAttribute(response, "status-message",
2102 IPP_TAG_ZERO)) != NULL &&
2103 (attrptr->value_tag != IPP_TAG_TEXT ||
2104 attrptr->group_tag != IPP_TAG_OPERATION ||
2105 attrptr->num_values != 1 ||
2106 (attrptr->value_tag == IPP_TAG_TEXT &&
2107 strlen(attrptr->values[0].string.text) > 255)))
2108 prev_pass = pass = 0;
2109
2110 if ((attrptr = ippFindAttribute(response, "detailed-status-message",
2111 IPP_TAG_ZERO)) != NULL &&
2112 (attrptr->value_tag != IPP_TAG_TEXT ||
2113 attrptr->group_tag != IPP_TAG_OPERATION ||
2114 attrptr->num_values != 1 ||
2115 (attrptr->value_tag == IPP_TAG_TEXT &&
2116 strlen(attrptr->values[0].string.text) > 1023)))
2117 prev_pass = pass = 0;
2118
2119 for (attrptr = response->attrs, group = attrptr->group_tag;
2120 attrptr;
2121 attrptr = attrptr->next)
2122 {
2123 if (attrptr->group_tag < group && attrptr->group_tag != IPP_TAG_ZERO)
2124 {
2125 prev_pass = pass = 0;
2126 break;
2127 }
2128
2129 if (!validate_attr(attrptr, 0))
2130 {
2131 prev_pass = pass = 0;
2132 break;
2133 }
2134 }
2135
2136 for (i = 0; i < num_statuses; i ++)
2137 {
2138 if (statuses[i].if_defined &&
2139 !get_variable(vars, statuses[i].if_defined))
2140 continue;
2141
2142 if (statuses[i].if_not_defined &&
2143 get_variable(vars, statuses[i].if_not_defined))
2144 continue;
2145
2146 if (response->request.status.status_code == statuses[i].status)
2147 break;
2148 }
2149
2150 if (i == num_statuses && num_statuses > 0)
2151 prev_pass = pass = 0;
2152 else
2153 {
2154 for (i = num_expects, expect = expects; i > 0; i --, expect ++)
2155 {
2156 if (expect->if_defined && !get_variable(vars, expect->if_defined))
2157 continue;
2158
2159 if (expect->if_not_defined &&
2160 get_variable(vars, expect->if_not_defined))
2161 continue;
2162
2163 found = ippFindAttribute(response, expect->name, IPP_TAG_ZERO);
2164
2165 if ((found && expect->not_expect) ||
2166 (!found && !(expect->not_expect || expect->optional)) ||
2167 (found && !expect_matches(expect, found->value_tag)) ||
2168 (found && expect->in_group &&
2169 found->group_tag != expect->in_group))
2170 {
2171 if (expect->define_no_match)
2172 set_variable(vars, expect->define_no_match, "1");
2173 else if (!expect->define_match)
2174 prev_pass = pass = 0;
2175
2176 continue;
2177 }
2178
2179 if (found &&
2180 !with_value(expect->with_value, expect->with_regex, found, 0))
2181 {
2182 if (expect->define_no_match)
2183 set_variable(vars, expect->define_no_match, "1");
2184 else if (!expect->define_match)
2185 prev_pass = pass = 0;
2186
2187 continue;
2188 }
2189
2190 if (found && expect->count > 0 && found->num_values != expect->count)
2191 {
2192 if (expect->define_no_match)
2193 set_variable(vars, expect->define_no_match, "1");
2194 else if (!expect->define_match)
2195 prev_pass = pass = 0;
2196
2197 continue;
2198 }
2199
2200 if (found && expect->same_count_as)
2201 {
2202 attrptr = ippFindAttribute(response, expect->same_count_as,
2203 IPP_TAG_ZERO);
2204
2205 if (!attrptr || attrptr->num_values != found->num_values)
2206 {
2207 if (expect->define_no_match)
2208 set_variable(vars, expect->define_no_match, "1");
2209 else if (!expect->define_match)
2210 prev_pass = pass = 0;
2211
2212 continue;
2213 }
2214 }
2215
2216 if (found && expect->define_match)
2217 set_variable(vars, expect->define_match, "1");
2218
2219 if (found && expect->define_value)
2220 {
2221 _ippAttrString(found, token, sizeof(token));
2222 set_variable(vars, expect->define_value, token);
2223 }
2224 }
2225 }
2226 }
2227
2228 if (Output == _CUPS_OUTPUT_PLIST)
2229 {
2230 puts("<key>Successful</key>");
2231 puts(prev_pass ? "<true />" : "<false />");
2232 puts("<key>StatusCode</key>");
2233 print_xml_string("string", ippErrorString(cupsLastError()));
2234 puts("<key>ResponseAttributes</key>");
2235 puts("<array>");
2236 puts("<dict>");
2237 for (attrptr = response ? response->attrs : NULL,
2238 group = attrptr ? attrptr->group_tag : IPP_TAG_ZERO;
2239 attrptr;
2240 attrptr = attrptr->next)
2241 print_attr(attrptr, &group);
2242 puts("</dict>");
2243 puts("</array>");
2244 }
2245 else if (Output == _CUPS_OUTPUT_TEST)
2246 {
2247 puts(prev_pass ? "PASS]" : "FAIL]");
2248
2249 if (Verbosity && response)
2250 {
2251 printf(" RECEIVED: %lu bytes in response\n",
2252 (unsigned long)ippLength(response));
2253 printf(" status-code = %x (%s)\n", cupsLastError(),
2254 ippErrorString(cupsLastError()));
2255
2256 for (attrptr = response->attrs;
2257 attrptr != NULL;
2258 attrptr = attrptr->next)
2259 {
2260 print_attr(attrptr, NULL);
2261 }
2262 }
2263 }
2264 else if (!prev_pass)
2265 fprintf(stderr, "%s\n", cupsLastErrorString());
2266
2267 if (prev_pass && Output != _CUPS_OUTPUT_PLIST &&
2268 Output != _CUPS_OUTPUT_QUIET && !Verbosity && num_displayed > 0)
2269 {
2270 if (Output >= _CUPS_OUTPUT_LIST)
2271 {
2272 size_t width; /* Length of value */
2273
2274
2275 for (i = 0; i < num_displayed; i ++)
2276 {
2277 widths[i] = strlen(displayed[i]);
2278
2279 for (attrptr = ippFindAttribute(response, displayed[i], IPP_TAG_ZERO);
2280 attrptr;
2281 attrptr = ippFindNextAttribute(response, displayed[i],
2282 IPP_TAG_ZERO))
2283 {
2284 width = _ippAttrString(attrptr, NULL, 0);
2285 if (width > widths[i])
2286 widths[i] = width;
2287 }
2288 }
2289
2290 if (Output == _CUPS_OUTPUT_CSV)
2291 print_csv(NULL, num_displayed, displayed, widths);
2292 else
2293 print_line(NULL, num_displayed, displayed, widths);
2294
2295 attrptr = response->attrs;
2296
2297 while (attrptr)
2298 {
2299 while (attrptr && attrptr->group_tag <= IPP_TAG_OPERATION)
2300 attrptr = attrptr->next;
2301
2302 if (attrptr)
2303 {
2304 if (Output == _CUPS_OUTPUT_CSV)
2305 print_csv(attrptr, num_displayed, displayed, widths);
2306 else
2307 print_line(attrptr, num_displayed, displayed, widths);
2308
2309 while (attrptr && attrptr->group_tag > IPP_TAG_OPERATION)
2310 attrptr = attrptr->next;
2311 }
2312 }
2313 }
2314 else
2315 {
2316 for (attrptr = response->attrs;
2317 attrptr != NULL;
2318 attrptr = attrptr->next)
2319 {
2320 if (attrptr->name)
2321 {
2322 for (i = 0; i < num_displayed; i ++)
2323 {
2324 if (!strcmp(displayed[i], attrptr->name))
2325 {
2326 print_attr(attrptr, NULL);
2327 break;
2328 }
2329 }
2330 }
2331 }
2332 }
2333 }
2334 else if (!prev_pass)
2335 {
2336 if (Output == _CUPS_OUTPUT_PLIST)
2337 {
2338 puts("<key>Errors</key>");
2339 puts("<array>");
2340 }
2341
2342 if (http->version != HTTP_1_1)
2343 print_test_error("Bad HTTP version (%d.%d)", http->version / 100,
2344 http->version % 100);
2345
2346 if (!response)
2347 print_test_error("IPP request failed with status %s (%s)",
2348 ippErrorString(cupsLastError()),
2349 cupsLastErrorString());
2350 else
2351 {
2352 if (version &&
2353 (response->request.status.version[0] != (version / 10) ||
2354 response->request.status.version[1] != (version % 10)))
2355 print_test_error("Bad version %d.%d in response - expected %d.%d "
2356 "(RFC 2911 section 3.1.8).",
2357 response->request.status.version[0],
2358 response->request.status.version[1],
2359 version / 10, version % 10);
2360
2361 if (response->request.status.request_id != request_id)
2362 print_test_error("Bad request ID %d in response - expected %d "
2363 "(RFC 2911 section 3.1.1)",
2364 response->request.status.request_id, request_id);
2365
2366 attrptr = response->attrs;
2367 if (!attrptr)
2368 print_test_error("Missing first attribute \"attributes-charset "
2369 "(charset)\" in group operation-attributes-tag "
2370 "(RFC 2911 section 3.1.4).");
2371 else
2372 {
2373 if (!attrptr->name ||
2374 attrptr->value_tag != IPP_TAG_CHARSET ||
2375 attrptr->group_tag != IPP_TAG_OPERATION ||
2376 attrptr->num_values != 1 ||
2377 strcmp(attrptr->name, "attributes-charset"))
2378 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2379 "expected \"attributes-charset (charset)\" in "
2380 "group operation-attributes-tag (RFC 2911 section "
2381 "3.1.4).",
2382 attrptr->name ? attrptr->name : "(null)",
2383 attrptr->num_values > 1 ? "1setOf " : "",
2384 ippTagString(attrptr->value_tag),
2385 ippTagString(attrptr->group_tag));
2386
2387 attrptr = attrptr->next;
2388 if (!attrptr)
2389 print_test_error("Missing second attribute \"attributes-natural-"
2390 "language (naturalLanguage)\" in group "
2391 "operation-attributes-tag (RFC 2911 section "
2392 "3.1.4).");
2393 else if (!attrptr->name ||
2394 attrptr->value_tag != IPP_TAG_LANGUAGE ||
2395 attrptr->group_tag != IPP_TAG_OPERATION ||
2396 attrptr->num_values != 1 ||
2397 strcmp(attrptr->name, "attributes-natural-language"))
2398 print_test_error("Bad first attribute \"%s (%s%s)\" in group %s, "
2399 "expected \"attributes-natural-language "
2400 "(naturalLanguage)\" in group "
2401 "operation-attributes-tag (RFC 2911 section "
2402 "3.1.4).",
2403 attrptr->name ? attrptr->name : "(null)",
2404 attrptr->num_values > 1 ? "1setOf " : "",
2405 ippTagString(attrptr->value_tag),
2406 ippTagString(attrptr->group_tag));
2407 }
2408
2409 if ((attrptr = ippFindAttribute(response, "status-message",
2410 IPP_TAG_ZERO)) != NULL)
2411 {
2412 if (attrptr->value_tag != IPP_TAG_TEXT)
2413 print_test_error("status-message (text(255)) has wrong value tag "
2414 "%s (RFC 2911 section 3.1.6.2).",
2415 ippTagString(attrptr->value_tag));
2416 if (attrptr->group_tag != IPP_TAG_OPERATION)
2417 print_test_error("status-message (text(255)) has wrong group tag "
2418 "%s (RFC 2911 section 3.1.6.2).",
2419 ippTagString(attrptr->group_tag));
2420 if (attrptr->num_values != 1)
2421 print_test_error("status-message (text(255)) has %d values "
2422 "(RFC 2911 section 3.1.6.2).",
2423 attrptr->num_values);
2424 if (attrptr->value_tag == IPP_TAG_TEXT &&
2425 strlen(attrptr->values[0].string.text) > 255)
2426 print_test_error("status-message (text(255)) has bad length %d"
2427 " (RFC 2911 section 3.1.6.2).",
2428 (int)strlen(attrptr->values[0].string.text));
2429 }
2430
2431 if ((attrptr = ippFindAttribute(response, "detailed-status-message",
2432 IPP_TAG_ZERO)) != NULL)
2433 {
2434 if (attrptr->value_tag != IPP_TAG_TEXT)
2435 print_test_error("detailed-status-message (text(MAX)) has wrong "
2436 "value tag %s (RFC 2911 section 3.1.6.3).",
2437 ippTagString(attrptr->value_tag));
2438 if (attrptr->group_tag != IPP_TAG_OPERATION)
2439 print_test_error("detailed-status-message (text(MAX)) has wrong "
2440 "group tag %s (RFC 2911 section 3.1.6.3).",
2441 ippTagString(attrptr->group_tag));
2442 if (attrptr->num_values != 1)
2443 print_test_error("detailed-status-message (text(MAX)) has %d values"
2444 " (RFC 2911 section 3.1.6.3).",
2445 attrptr->num_values);
2446 if (attrptr->value_tag == IPP_TAG_TEXT &&
2447 strlen(attrptr->values[0].string.text) > 1023)
2448 print_test_error("detailed-status-message (text(MAX)) has bad "
2449 "length %d (RFC 2911 section 3.1.6.3).",
2450 (int)strlen(attrptr->values[0].string.text));
2451 }
2452
2453 for (attrptr = response->attrs, group = attrptr->group_tag;
2454 attrptr;
2455 attrptr = attrptr->next)
2456 {
2457 if (attrptr->group_tag < group && attrptr->group_tag != IPP_TAG_ZERO)
2458 print_test_error("Attribute groups out of order (%s < %s)",
2459 ippTagString(attrptr->group_tag),
2460 ippTagString(group));
2461
2462 validate_attr(attrptr, 1);
2463 }
2464
2465 for (i = 0; i < num_statuses; i ++)
2466 {
2467 if (statuses[i].if_defined &&
2468 !get_variable(vars, statuses[i].if_defined))
2469 continue;
2470
2471 if (statuses[i].if_not_defined &&
2472 get_variable(vars, statuses[i].if_not_defined))
2473 continue;
2474
2475 if (response->request.status.status_code == statuses[i].status)
2476 break;
2477 }
2478
2479 if (i == num_statuses && num_statuses > 0)
2480 {
2481 for (i = 0; i < num_statuses; i ++)
2482 {
2483 if (statuses[i].if_defined &&
2484 !get_variable(vars, statuses[i].if_defined))
2485 continue;
2486
2487 if (statuses[i].if_not_defined &&
2488 get_variable(vars, statuses[i].if_not_defined))
2489 continue;
2490
2491 print_test_error("EXPECTED: STATUS %s (got %s)",
2492 ippErrorString(statuses[i].status),
2493 ippErrorString(cupsLastError()));
2494 }
2495
2496 if ((attrptr = ippFindAttribute(response, "status-message",
2497 IPP_TAG_TEXT)) != NULL)
2498 print_test_error("status-message=\"%s\"",
2499 attrptr->values[0].string.text);
2500 }
2501
2502 for (i = num_expects, expect = expects; i > 0; i --, expect ++)
2503 {
2504 if (expect->define_match || expect->define_no_match)
2505 continue;
2506
2507 if (expect->if_defined && !get_variable(vars, expect->if_defined))
2508 continue;
2509
2510 if (expect->if_not_defined &&
2511 get_variable(vars, expect->if_not_defined))
2512 continue;
2513
2514 found = ippFindAttribute(response, expect->name, IPP_TAG_ZERO);
2515
2516 if (found && expect->not_expect)
2517 print_test_error("NOT EXPECTED: %s", expect->name);
2518 else if (!found && !(expect->not_expect || expect->optional))
2519 print_test_error("EXPECTED: %s", expect->name);
2520 else if (found)
2521 {
2522 if (!expect_matches(expect, found->value_tag))
2523 print_test_error("EXPECTED: %s OF-TYPE %s (got %s)",
2524 expect->name, expect->of_type,
2525 ippTagString(found->value_tag));
2526
2527 if (expect->in_group && found->group_tag != expect->in_group)
2528 print_test_error("EXPECTED: %s IN-GROUP %s (got %s).",
2529 expect->name, ippTagString(expect->in_group),
2530 ippTagString(found->group_tag));
2531
2532 if (!with_value(expect->with_value, expect->with_regex, found, 0))
2533 {
2534 if (expect->with_regex)
2535 print_test_error("EXPECTED: %s WITH-VALUE /%s/",
2536 expect->name, expect->with_value);
2537 else
2538 print_test_error("EXPECTED: %s WITH-VALUE \"%s\"",
2539 expect->name, expect->with_value);
2540
2541 with_value(expect->with_value, expect->with_regex, found, 1);
2542 }
2543
2544 if (expect->count > 0 && found->num_values != expect->count)
2545 {
2546 print_test_error("EXPECTED: %s COUNT %d (got %d)", expect->name,
2547 expect->count, found->num_values);
2548 }
2549
2550 if (expect->same_count_as)
2551 {
2552 attrptr = ippFindAttribute(response, expect->same_count_as,
2553 IPP_TAG_ZERO);
2554
2555 if (!attrptr)
2556 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2557 "(not returned)", expect->name,
2558 found->num_values, expect->same_count_as);
2559 else if (attrptr->num_values != found->num_values)
2560 print_test_error("EXPECTED: %s (%d values) SAME-COUNT-AS %s "
2561 "(%d values)", expect->name, found->num_values,
2562 expect->same_count_as, attrptr->num_values);
2563 }
2564 }
2565 }
2566 }
2567
2568 if (Output == _CUPS_OUTPUT_PLIST)
2569 puts("</array>");
2570 }
2571
2572 skip_error:
2573
2574 if (Output == _CUPS_OUTPUT_PLIST)
2575 puts("</dict>");
2576
2577 ippDelete(response);
2578 response = NULL;
2579
2580 for (i = 0; i < num_statuses; i ++)
2581 {
2582 if (statuses[i].if_defined)
2583 free(statuses[i].if_defined);
2584 if (statuses[i].if_not_defined)
2585 free(statuses[i].if_not_defined);
2586 }
2587 num_statuses = 0;
2588
2589 for (i = num_expects, expect = expects; i > 0; i --, expect ++)
2590 {
2591 free(expect->name);
2592 if (expect->of_type)
2593 free(expect->of_type);
2594 if (expect->same_count_as)
2595 free(expect->same_count_as);
2596 if (expect->if_defined)
2597 free(expect->if_defined);
2598 if (expect->if_not_defined)
2599 free(expect->if_not_defined);
2600 if (expect->with_value)
2601 free(expect->with_value);
2602 if (expect->define_match)
2603 free(expect->define_match);
2604 if (expect->define_no_match)
2605 free(expect->define_no_match);
2606 if (expect->define_value)
2607 free(expect->define_value);
2608 }
2609 num_expects = 0;
2610
2611 for (i = 0; i < num_displayed; i ++)
2612 free(displayed[i]);
2613 num_displayed = 0;
2614
2615 if (!ignore_errors && !prev_pass)
2616 break;
2617 }
2618
2619 test_exit:
2620
2621 if (fp)
2622 fclose(fp);
2623
2624 httpClose(http);
2625 ippDelete(request);
2626 ippDelete(response);
2627
2628 for (i = 0; i < num_statuses; i ++)
2629 {
2630 if (statuses[i].if_defined)
2631 free(statuses[i].if_defined);
2632 if (statuses[i].if_not_defined)
2633 free(statuses[i].if_not_defined);
2634 }
2635
2636 for (i = num_expects, expect = expects; i > 0; i --, expect ++)
2637 {
2638 free(expect->name);
2639 if (expect->of_type)
2640 free(expect->of_type);
2641 if (expect->same_count_as)
2642 free(expect->same_count_as);
2643 if (expect->if_defined)
2644 free(expect->if_defined);
2645 if (expect->if_not_defined)
2646 free(expect->if_not_defined);
2647 if (expect->with_value)
2648 free(expect->with_value);
2649 if (expect->define_match)
2650 free(expect->define_match);
2651 if (expect->define_no_match)
2652 free(expect->define_no_match);
2653 if (expect->define_value)
2654 free(expect->define_value);
2655 }
2656
2657 for (i = 0; i < num_displayed; i ++)
2658 free(displayed[i]);
2659
2660 return (pass);
2661 }
2662
2663
2664 /*
2665 * 'expand_variables()' - Expand variables in a string.
2666 */
2667
2668 static void
2669 expand_variables(_cups_vars_t *vars, /* I - Variables */
2670 char *dst, /* I - Destination string buffer */
2671 const char *src, /* I - Source string */
2672 size_t dstsize) /* I - Size of destination buffer */
2673 {
2674 char *dstptr, /* Pointer into destination */
2675 *dstend, /* End of destination */
2676 temp[256], /* Temporary string */
2677 *tempptr; /* Pointer into temporary string */
2678 const char *value; /* Value to substitute */
2679
2680
2681 dstptr = dst;
2682 dstend = dst + dstsize - 1;
2683
2684 while (*src && dstptr < dstend)
2685 {
2686 if (*src == '$')
2687 {
2688 /*
2689 * Substitute a string/number...
2690 */
2691
2692 if (!strncmp(src, "$$", 2))
2693 {
2694 value = "$";
2695 src += 2;
2696 }
2697 else if (!strncmp(src, "$ENV[", 5))
2698 {
2699 strlcpy(temp, src + 5, sizeof(temp));
2700
2701 for (tempptr = temp; *tempptr; tempptr ++)
2702 if (*tempptr == ']')
2703 break;
2704
2705 if (*tempptr)
2706 *tempptr++ = '\0';
2707
2708 value = getenv(temp);
2709 src += tempptr - temp + 5;
2710 }
2711 else if (vars)
2712 {
2713 strlcpy(temp, src + 1, sizeof(temp));
2714
2715 for (tempptr = temp; *tempptr; tempptr ++)
2716 if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
2717 break;
2718
2719 if (*tempptr)
2720 *tempptr = '\0';
2721
2722 if (!strcmp(temp, "uri"))
2723 value = vars->uri;
2724 else if (!strcmp(temp, "filename"))
2725 value = vars->filename;
2726 else if (!strcmp(temp, "scheme") || !strcmp(temp, "method"))
2727 value = vars->scheme;
2728 else if (!strcmp(temp, "username"))
2729 value = vars->userpass;
2730 else if (!strcmp(temp, "hostname"))
2731 value = vars->hostname;
2732 else if (!strcmp(temp, "port"))
2733 {
2734 snprintf(temp, sizeof(temp), "%d", vars->port);
2735 value = temp;
2736 }
2737 else if (!strcmp(temp, "resource"))
2738 value = vars->resource;
2739 else if (!strcmp(temp, "user"))
2740 value = cupsUser();
2741 else
2742 value = get_variable(vars, temp);
2743
2744 src += tempptr - temp + 1;
2745 }
2746 else
2747 {
2748 value = "$";
2749 src ++;
2750 }
2751
2752 if (value)
2753 {
2754 strlcpy(dstptr, value, dstend - dstptr + 1);
2755 dstptr += strlen(dstptr);
2756 }
2757 }
2758 else
2759 *dstptr++ = *src++;
2760 }
2761
2762 *dstptr = '\0';
2763 }
2764
2765
2766 /*
2767 * 'expect_matches()' - Return true if the tag matches the specification.
2768 */
2769
2770 static int /* O - 1 if matches, 0 otherwise */
2771 expect_matches(
2772 _cups_expect_t *expect, /* I - Expected attribute */
2773 ipp_tag_t value_tag) /* I - Value tag for attribute */
2774 {
2775 int match; /* Match? */
2776 char *of_type, /* Type name to match */
2777 *next, /* Next name to match */
2778 sep; /* Separator character */
2779
2780
2781 /*
2782 * If we don't expect a particular type, return immediately...
2783 */
2784
2785 if (!expect->of_type)
2786 return (1);
2787
2788 /*
2789 * Parse the "of_type" value since the string can contain multiple attribute
2790 * types separated by "," or "|"...
2791 */
2792
2793 for (of_type = expect->of_type, match = 0; !match && *of_type; of_type = next)
2794 {
2795 /*
2796 * Find the next separator, and set it (temporarily) to nul if present.
2797 */
2798
2799 for (next = of_type; *next && *next != '|' && *next != ','; next ++);
2800
2801 if ((sep = *next) != '\0')
2802 *next = '\0';
2803
2804 /*
2805 * Support some meta-types to make it easier to write the test file.
2806 */
2807
2808 if (!strcmp(of_type, "text"))
2809 match = value_tag == IPP_TAG_TEXTLANG || value_tag == IPP_TAG_TEXT;
2810 else if (!strcmp(of_type, "name"))
2811 match = value_tag == IPP_TAG_NAMELANG || value_tag == IPP_TAG_NAME;
2812 else if (!strcmp(of_type, "collection"))
2813 match = value_tag == IPP_TAG_BEGIN_COLLECTION;
2814 else
2815 match = value_tag == ippTagValue(of_type);
2816
2817 /*
2818 * Restore the separator if we have one...
2819 */
2820
2821 if (sep)
2822 *next++ = sep;
2823 }
2824
2825 return (match);
2826 }
2827
2828
2829 /*
2830 * 'get_collection()' - Get a collection value from the current test file.
2831 */
2832
2833 static ipp_t * /* O - Collection value */
2834 get_collection(_cups_vars_t *vars, /* I - Variables */
2835 FILE *fp, /* I - File to read from */
2836 int *linenum) /* IO - Line number */
2837 {
2838 char token[1024], /* Token from file */
2839 temp[1024], /* Temporary string */
2840 attr[128]; /* Attribute name */
2841 ipp_tag_t value; /* Current value type */
2842 ipp_t *col = ippNew(); /* Collection value */
2843 ipp_attribute_t *lastcol = NULL; /* Last collection attribute */
2844
2845
2846 while (get_token(fp, token, sizeof(token), linenum) != NULL)
2847 {
2848 if (!strcmp(token, "}"))
2849 break;
2850 else if (!strcmp(token, "{") && lastcol)
2851 {
2852 /*
2853 * Another collection value
2854 */
2855
2856 ipp_t *subcol = get_collection(vars, fp, linenum);
2857 /* Collection value */
2858
2859 if (subcol)
2860 {
2861 ipp_attribute_t *tempcol; /* Pointer to new buffer */
2862
2863
2864 /*
2865 * Reallocate memory...
2866 */
2867
2868 if ((tempcol = realloc(lastcol, sizeof(ipp_attribute_t) +
2869 (lastcol->num_values + 1) *
2870 sizeof(ipp_value_t))) == NULL)
2871 {
2872 print_fatal_error("Unable to allocate memory on line %d.", *linenum);
2873 goto col_error;
2874 }
2875
2876 if (tempcol != lastcol)
2877 {
2878 /*
2879 * Reset pointers in the list...
2880 */
2881
2882 if (col->prev)
2883 col->prev->next = tempcol;
2884 else
2885 col->attrs = tempcol;
2886
2887 lastcol = col->current = col->last = tempcol;
2888 }
2889
2890 lastcol->values[lastcol->num_values].collection = subcol;
2891 lastcol->num_values ++;
2892 }
2893 else
2894 goto col_error;
2895 }
2896 else if (!_cups_strcasecmp(token, "MEMBER"))
2897 {
2898 /*
2899 * Attribute...
2900 */
2901
2902 lastcol = NULL;
2903
2904 if (!get_token(fp, token, sizeof(token), linenum))
2905 {
2906 print_fatal_error("Missing MEMBER value tag on line %d.", *linenum);
2907 goto col_error;
2908 }
2909
2910 if ((value = ippTagValue(token)) == IPP_TAG_ZERO)
2911 {
2912 print_fatal_error("Bad MEMBER value tag \"%s\" on line %d.", token,
2913 *linenum);
2914 goto col_error;
2915 }
2916
2917 if (!get_token(fp, attr, sizeof(attr), linenum))
2918 {
2919 print_fatal_error("Missing MEMBER name on line %d.", *linenum);
2920 goto col_error;
2921 }
2922
2923 if (!get_token(fp, temp, sizeof(temp), linenum))
2924 {
2925 print_fatal_error("Missing MEMBER value on line %d.", *linenum);
2926 goto col_error;
2927 }
2928
2929 expand_variables(vars, token, temp, sizeof(token));
2930
2931 switch (value)
2932 {
2933 case IPP_TAG_BOOLEAN :
2934 if (!_cups_strcasecmp(token, "true"))
2935 ippAddBoolean(col, IPP_TAG_ZERO, attr, 1);
2936 else
2937 ippAddBoolean(col, IPP_TAG_ZERO, attr, atoi(token));
2938 break;
2939
2940 case IPP_TAG_INTEGER :
2941 case IPP_TAG_ENUM :
2942 ippAddInteger(col, IPP_TAG_ZERO, value, attr, atoi(token));
2943 break;
2944
2945 case IPP_TAG_RESOLUTION :
2946 {
2947 int xres, /* X resolution */
2948 yres; /* Y resolution */
2949 char units[6]; /* Units */
2950
2951 if (sscanf(token, "%dx%d%5s", &xres, &yres, units) != 3 ||
2952 (_cups_strcasecmp(units, "dpi") && _cups_strcasecmp(units, "dpc") &&
2953 _cups_strcasecmp(units, "other")))
2954 {
2955 print_fatal_error("Bad resolution value \"%s\" on line %d.",
2956 token, *linenum);
2957 goto col_error;
2958 }
2959
2960 if (!_cups_strcasecmp(units, "dpi"))
2961 ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
2962 IPP_RES_PER_INCH);
2963 else if (!_cups_strcasecmp(units, "dpc"))
2964 ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
2965 IPP_RES_PER_CM);
2966 else
2967 ippAddResolution(col, IPP_TAG_ZERO, attr, xres, yres,
2968 (ipp_res_t)0);
2969 }
2970 break;
2971
2972 case IPP_TAG_RANGE :
2973 {
2974 int lowers[4], /* Lower value */
2975 uppers[4], /* Upper values */
2976 num_vals; /* Number of values */
2977
2978
2979 num_vals = sscanf(token, "%d-%d,%d-%d,%d-%d,%d-%d",
2980 lowers + 0, uppers + 0,
2981 lowers + 1, uppers + 1,
2982 lowers + 2, uppers + 2,
2983 lowers + 3, uppers + 3);
2984
2985 if ((num_vals & 1) || num_vals == 0)
2986 {
2987 print_fatal_error("Bad rangeOfInteger value \"%s\" on line %d.",
2988 token, *linenum);
2989 goto col_error;
2990 }
2991
2992 ippAddRanges(col, IPP_TAG_ZERO, attr, num_vals / 2, lowers,
2993 uppers);
2994 }
2995 break;
2996
2997 case IPP_TAG_BEGIN_COLLECTION :
2998 if (!strcmp(token, "{"))
2999 {
3000 ipp_t *subcol = get_collection(vars, fp, linenum);
3001 /* Collection value */
3002
3003 if (subcol)
3004 {
3005 lastcol = ippAddCollection(col, IPP_TAG_ZERO, attr, subcol);
3006 ippDelete(subcol);
3007 }
3008 else
3009 goto col_error;
3010 }
3011 else
3012 {
3013 print_fatal_error("Bad collection value on line %d.", *linenum);
3014 goto col_error;
3015 }
3016 break;
3017
3018 default :
3019 if (!strchr(token, ','))
3020 ippAddString(col, IPP_TAG_ZERO, value, attr, NULL, token);
3021 else
3022 {
3023 /*
3024 * Multiple string values...
3025 */
3026
3027 int num_values; /* Number of values */
3028 char *values[100], /* Values */
3029 *ptr; /* Pointer to next value */
3030
3031
3032 values[0] = token;
3033 num_values = 1;
3034
3035 for (ptr = strchr(token, ','); ptr; ptr = strchr(ptr, ','))
3036 {
3037 *ptr++ = '\0';
3038 values[num_values] = ptr;
3039 num_values ++;
3040 }
3041
3042 ippAddStrings(col, IPP_TAG_ZERO, value, attr, num_values,
3043 NULL, (const char **)values);
3044 }
3045 break;
3046 }
3047 }
3048 }
3049
3050 return (col);
3051
3052 /*
3053 * If we get here there was a parse error; free memory and return.
3054 */
3055
3056 col_error:
3057
3058 ippDelete(col);
3059
3060 return (NULL);
3061 }
3062
3063
3064 /*
3065 * 'get_filename()' - Get a filename based on the current test file.
3066 */
3067
3068 static char * /* O - Filename */
3069 get_filename(const char *testfile, /* I - Current test file */
3070 char *dst, /* I - Destination filename */
3071 const char *src, /* I - Source filename */
3072 size_t dstsize) /* I - Size of destination buffer */
3073 {
3074 char *dstptr; /* Pointer into destination */
3075 _cups_globals_t *cg = _cupsGlobals();
3076 /* Global data */
3077
3078
3079 if (*src == '<' && src[strlen(src) - 1] == '>')
3080 {
3081 /*
3082 * Map <filename> to CUPS_DATADIR/ipptool/filename...
3083 */
3084
3085 snprintf(dst, dstsize, "%s/ipptool/%s", cg->cups_datadir, src + 1);
3086 dstptr = dst + strlen(dst) - 1;
3087 if (*dstptr == '>')
3088 *dstptr = '\0';
3089 }
3090 else if (*src == '/' || !strchr(testfile, '/'))
3091 {
3092 /*
3093 * Use the path as-is...
3094 */
3095
3096 strlcpy(dst, src, dstsize);
3097 }
3098 else
3099 {
3100 /*
3101 * Make path relative to testfile...
3102 */
3103
3104 strlcpy(dst, testfile, dstsize);
3105 if ((dstptr = strrchr(dst, '/')) != NULL)
3106 dstptr ++;
3107 else
3108 dstptr = dst; /* Should never happen */
3109
3110 strlcpy(dstptr, src, dstsize - (dstptr - dst));
3111 }
3112
3113 return (dst);
3114 }
3115
3116
3117 /*
3118 * 'get_token()' - Get a token from a file.
3119 */
3120
3121 static char * /* O - Token from file or NULL on EOF */
3122 get_token(FILE *fp, /* I - File to read from */
3123 char *buf, /* I - Buffer to read into */
3124 int buflen, /* I - Length of buffer */
3125 int *linenum) /* IO - Current line number */
3126 {
3127 int ch, /* Character from file */
3128 quote; /* Quoting character */
3129 char *bufptr, /* Pointer into buffer */
3130 *bufend; /* End of buffer */
3131
3132
3133 for (;;)
3134 {
3135 /*
3136 * Skip whitespace...
3137 */
3138
3139 while (isspace(ch = getc(fp)))
3140 {
3141 if (ch == '\n')
3142 (*linenum) ++;
3143 }
3144
3145 /*
3146 * Read a token...
3147 */
3148
3149 if (ch == EOF)
3150 return (NULL);
3151 else if (ch == '\'' || ch == '\"')
3152 {
3153 /*
3154 * Quoted text or regular expression...
3155 */
3156
3157 quote = ch;
3158 bufptr = buf;
3159 bufend = buf + buflen - 1;
3160
3161 while ((ch = getc(fp)) != EOF)
3162 {
3163 if (ch == '\\')
3164 {
3165 /*
3166 * Escape next character...
3167 */
3168
3169 if (bufptr < bufend)
3170 *bufptr++ = ch;
3171
3172 if ((ch = getc(fp)) != EOF && bufptr < bufend)
3173 *bufptr++ = ch;
3174 }
3175 else if (ch == quote)
3176 break;
3177 else if (bufptr < bufend)
3178 *bufptr++ = ch;
3179 }
3180
3181 *bufptr = '\0';
3182
3183 return (buf);
3184 }
3185 else if (ch == '#')
3186 {
3187 /*
3188 * Comment...
3189 */
3190
3191 while ((ch = getc(fp)) != EOF)
3192 if (ch == '\n')
3193 break;
3194
3195 (*linenum) ++;
3196 }
3197 else
3198 {
3199 /*
3200 * Whitespace delimited text...
3201 */
3202
3203 ungetc(ch, fp);
3204
3205 bufptr = buf;
3206 bufend = buf + buflen - 1;
3207
3208 while ((ch = getc(fp)) != EOF)
3209 if (isspace(ch) || ch == '#')
3210 break;
3211 else if (bufptr < bufend)
3212 *bufptr++ = ch;
3213
3214 if (ch == '#')
3215 ungetc(ch, fp);
3216 else if (ch == '\n')
3217 (*linenum) ++;
3218
3219 *bufptr = '\0';
3220
3221 return (buf);
3222 }
3223 }
3224 }
3225
3226
3227 /*
3228 * 'get_variable()' - Get the value of a variable.
3229 */
3230
3231 static char * /* O - Value or NULL */
3232 get_variable(_cups_vars_t *vars, /* I - Variables */
3233 const char *name) /* I - Variable name */
3234 {
3235 _cups_var_t key, /* Search key */
3236 *match; /* Matching variable, if any */
3237
3238
3239 key.name = (char *)name;
3240 match = cupsArrayFind(vars->vars, &key);
3241
3242 return (match ? match->value : NULL);
3243 }
3244
3245
3246 /*
3247 * 'iso_date()' - Return an ISO 8601 date/time string for the given IPP dateTime
3248 * value.
3249 */
3250
3251 static char * /* O - ISO 8601 date/time string */
3252 iso_date(ipp_uchar_t *date) /* I - IPP (RFC 1903) date/time value */
3253 {
3254 unsigned year = (date[0] << 8) + date[1];
3255 /* Year */
3256 static char buffer[255]; /* String buffer */
3257
3258
3259 if (date[9] == 0 && date[10] == 0)
3260 snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02uZ",
3261 year, date[2], date[3], date[4], date[5], date[6]);
3262 else
3263 snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u",
3264 year, date[2], date[3], date[4], date[5], date[6],
3265 date[8], date[9], date[10]);
3266
3267 return (buffer);
3268 }
3269
3270
3271 /*
3272 * 'password_cb()' - Password callback for authenticated tests.
3273 */
3274
3275 static const char * /* O - Password */
3276 password_cb(const char *prompt) /* I - Prompt (unused) */
3277 {
3278 (void)prompt;
3279
3280 return (Password);
3281 }
3282
3283
3284 /*
3285 * 'print_attr()' - Print an attribute on the screen.
3286 */
3287
3288 static void
3289 print_attr(ipp_attribute_t *attr, /* I - Attribute to print */
3290 ipp_tag_t *group) /* IO - Current group */
3291 {
3292 int i; /* Looping var */
3293 ipp_attribute_t *colattr; /* Collection attribute */
3294
3295
3296 if (Output == _CUPS_OUTPUT_PLIST)
3297 {
3298 if (!attr->name || (group && *group != attr->group_tag))
3299 {
3300 puts("</dict>");
3301 puts("<dict>");
3302
3303 if (group)
3304 *group = attr->group_tag;
3305 }
3306
3307 if (!attr->name)
3308 return;
3309
3310 print_xml_string("key", attr->name);
3311 if (attr->num_values > 1)
3312 puts("<array>");
3313 }
3314 else if (Output == _CUPS_OUTPUT_TEST)
3315 {
3316 if (!attr->name)
3317 {
3318 puts(" -- separator --");
3319 return;
3320 }
3321
3322 printf(" %s (%s%s) = ", attr->name,
3323 attr->num_values > 1 ? "1setOf " : "",
3324 ippTagString(attr->value_tag));
3325 }
3326
3327 switch (attr->value_tag)
3328 {
3329 case IPP_TAG_INTEGER :
3330 case IPP_TAG_ENUM :
3331 for (i = 0; i < attr->num_values; i ++)
3332 if (Output == _CUPS_OUTPUT_PLIST)
3333 printf("<integer>%d</integer>\n", attr->values[i].integer);
3334 else
3335 printf("%d ", attr->values[i].integer);
3336 break;
3337
3338 case IPP_TAG_BOOLEAN :
3339 for (i = 0; i < attr->num_values; i ++)
3340 if (Output == _CUPS_OUTPUT_PLIST)
3341 puts(attr->values[i].boolean ? "<true />" : "<false />");
3342 else if (attr->values[i].boolean)
3343 fputs("true ", stdout);
3344 else
3345 fputs("false ", stdout);
3346 break;
3347
3348 case IPP_TAG_RANGE :
3349 for (i = 0; i < attr->num_values; i ++)
3350 if (Output == _CUPS_OUTPUT_PLIST)
3351 printf("<dict><key>lower</key><integer>%d</integer>"
3352 "<key>upper</key><integer>%d</integer></dict>\n",
3353 attr->values[i].range.lower, attr->values[i].range.upper);
3354 else
3355 printf("%d-%d ", attr->values[i].range.lower,
3356 attr->values[i].range.upper);
3357 break;
3358
3359 case IPP_TAG_RESOLUTION :
3360 for (i = 0; i < attr->num_values; i ++)
3361 if (Output == _CUPS_OUTPUT_PLIST)
3362 printf("<dict><key>xres</key><integer>%d</integer>"
3363 "<key>yres</key><integer>%d</integer>"
3364 "<key>units</key><string>%s</string></dict>\n",
3365 attr->values[i].resolution.xres,
3366 attr->values[i].resolution.yres,
3367 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3368 "dpi" : "dpc");
3369 else
3370 printf("%dx%d%s ", attr->values[i].resolution.xres,
3371 attr->values[i].resolution.yres,
3372 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3373 "dpi" : "dpc");
3374 break;
3375
3376 case IPP_TAG_DATE :
3377 for (i = 0; i < attr->num_values; i ++)
3378 if (Output == _CUPS_OUTPUT_PLIST)
3379 printf("<date>%s</date>\n", iso_date(attr->values[i].date));
3380 else
3381 printf("%s ", iso_date(attr->values[i].date));
3382 break;
3383
3384 case IPP_TAG_STRING :
3385 case IPP_TAG_TEXT :
3386 case IPP_TAG_NAME :
3387 case IPP_TAG_KEYWORD :
3388 case IPP_TAG_CHARSET :
3389 case IPP_TAG_URI :
3390 case IPP_TAG_MIMETYPE :
3391 case IPP_TAG_LANGUAGE :
3392 for (i = 0; i < attr->num_values; i ++)
3393 if (Output == _CUPS_OUTPUT_PLIST)
3394 print_xml_string("string", attr->values[i].string.text);
3395 else
3396 printf("\"%s\" ", attr->values[i].string.text);
3397 break;
3398
3399 case IPP_TAG_TEXTLANG :
3400 case IPP_TAG_NAMELANG :
3401 for (i = 0; i < attr->num_values; i ++)
3402 if (Output == _CUPS_OUTPUT_PLIST)
3403 {
3404 fputs("<dict><key>language</key><string>", stdout);
3405 print_xml_string(NULL, attr->values[i].string.charset);
3406 fputs("</string><key>string</key><string>", stdout);
3407 print_xml_string(NULL, attr->values[i].string.text);
3408 puts("</string></dict>");
3409 }
3410 else
3411 printf("\"%s\"(%s) ", attr->values[i].string.text,
3412 attr->values[i].string.charset);
3413 break;
3414
3415 case IPP_TAG_BEGIN_COLLECTION :
3416 for (i = 0; i < attr->num_values; i ++)
3417 {
3418 if (Output == _CUPS_OUTPUT_PLIST)
3419 {
3420 puts("<dict>");
3421 for (colattr = attr->values[i].collection->attrs;
3422 colattr;
3423 colattr = colattr->next)
3424 print_attr(colattr, NULL);
3425 puts("</dict>");
3426 }
3427 else
3428 {
3429 if (i)
3430 putchar(' ');
3431
3432 print_col(attr->values[i].collection);
3433 }
3434 }
3435 break;
3436
3437 default :
3438 if (Output == _CUPS_OUTPUT_PLIST)
3439 printf("<string>&lt;&lt;%s&gt;&gt;</string>\n",
3440 ippTagString(attr->value_tag));
3441 else
3442 fputs(ippTagString(attr->value_tag), stdout);
3443 break;
3444 }
3445
3446 if (Output == _CUPS_OUTPUT_PLIST)
3447 {
3448 if (attr->num_values > 1)
3449 puts("</array>");
3450 }
3451 else
3452 putchar('\n');
3453 }
3454
3455
3456 /*
3457 * 'print_col()' - Print a collection attribute on the screen.
3458 */
3459
3460 static void
3461 print_col(ipp_t *col) /* I - Collection attribute to print */
3462 {
3463 int i; /* Looping var */
3464 ipp_attribute_t *attr; /* Current attribute in collection */
3465
3466
3467 fputs("{ ", stdout);
3468 for (attr = col->attrs; attr; attr = attr->next)
3469 {
3470 printf("%s (%s%s) = ", attr->name, attr->num_values > 1 ? "1setOf " : "",
3471 ippTagString(attr->value_tag));
3472
3473 switch (attr->value_tag)
3474 {
3475 case IPP_TAG_INTEGER :
3476 case IPP_TAG_ENUM :
3477 for (i = 0; i < attr->num_values; i ++)
3478 printf("%d ", attr->values[i].integer);
3479 break;
3480
3481 case IPP_TAG_BOOLEAN :
3482 for (i = 0; i < attr->num_values; i ++)
3483 if (attr->values[i].boolean)
3484 printf("true ");
3485 else
3486 printf("false ");
3487 break;
3488
3489 case IPP_TAG_NOVALUE :
3490 printf("novalue");
3491 break;
3492
3493 case IPP_TAG_RANGE :
3494 for (i = 0; i < attr->num_values; i ++)
3495 printf("%d-%d ", attr->values[i].range.lower,
3496 attr->values[i].range.upper);
3497 break;
3498
3499 case IPP_TAG_RESOLUTION :
3500 for (i = 0; i < attr->num_values; i ++)
3501 printf("%dx%d%s ", attr->values[i].resolution.xres,
3502 attr->values[i].resolution.yres,
3503 attr->values[i].resolution.units == IPP_RES_PER_INCH ?
3504 "dpi" : "dpc");
3505 break;
3506
3507 case IPP_TAG_STRING :
3508 case IPP_TAG_TEXT :
3509 case IPP_TAG_NAME :
3510 case IPP_TAG_KEYWORD :
3511 case IPP_TAG_CHARSET :
3512 case IPP_TAG_URI :
3513 case IPP_TAG_MIMETYPE :
3514 case IPP_TAG_LANGUAGE :
3515 for (i = 0; i < attr->num_values; i ++)
3516 printf("\"%s\" ", attr->values[i].string.text);
3517 break;
3518
3519 case IPP_TAG_TEXTLANG :
3520 case IPP_TAG_NAMELANG :
3521 for (i = 0; i < attr->num_values; i ++)
3522 printf("\"%s\",%s ", attr->values[i].string.text,
3523 attr->values[i].string.charset);
3524 break;
3525
3526 case IPP_TAG_BEGIN_COLLECTION :
3527 for (i = 0; i < attr->num_values; i ++)
3528 {
3529 print_col(attr->values[i].collection);
3530 putchar(' ');
3531 }
3532 break;
3533
3534 default :
3535 break; /* anti-compiler-warning-code */
3536 }
3537 }
3538
3539 putchar('}');
3540 }
3541
3542
3543 /*
3544 * 'print_csv()' - Print a line of CSV text.
3545 */
3546
3547 static void
3548 print_csv(
3549 ipp_attribute_t *attr, /* I - First attribute for line */
3550 int num_displayed, /* I - Number of attributes to display */
3551 char **displayed, /* I - Attributes to display */
3552 size_t *widths) /* I - Column widths */
3553 {
3554 int i; /* Looping var */
3555 size_t maxlength; /* Max length of all columns */
3556 char *buffer, /* String buffer */
3557 *bufptr; /* Pointer into buffer */
3558 ipp_attribute_t *current; /* Current attribute */
3559
3560
3561 /*
3562 * Get the maximum string length we have to show and allocate...
3563 */
3564
3565 for (i = 1, maxlength = widths[0]; i < num_displayed; i ++)
3566 if (widths[i] > maxlength)
3567 maxlength = widths[i];
3568
3569 maxlength += 2;
3570
3571 if ((buffer = malloc(maxlength)) == NULL)
3572 return;
3573
3574 /*
3575 * Loop through the attributes to display...
3576 */
3577
3578 if (attr)
3579 {
3580 for (i = 0; i < num_displayed; i ++)
3581 {
3582 if (i)
3583 putchar(',');
3584
3585 buffer[0] = '\0';
3586
3587 for (current = attr; current; current = current->next)
3588 {
3589 if (!current->name)
3590 break;
3591 else if (!strcmp(current->name, displayed[i]))
3592 {
3593 _ippAttrString(current, buffer, maxlength);
3594 break;
3595 }
3596 }
3597
3598 if (strchr(buffer, ',') != NULL || strchr(buffer, '\"') != NULL ||
3599 strchr(buffer, '\\') != NULL)
3600 {
3601 putchar('\"');
3602 for (bufptr = buffer; *bufptr; bufptr ++)
3603 {
3604 if (*bufptr == '\\' || *bufptr == '\"')
3605 putchar('\\');
3606 putchar(*bufptr);
3607 }
3608 putchar('\"');
3609 }
3610 else
3611 fputs(buffer, stdout);
3612 }
3613 putchar('\n');
3614 }
3615 else
3616 {
3617 for (i = 0; i < num_displayed; i ++)
3618 {
3619 if (i)
3620 putchar(',');
3621
3622 fputs(displayed[i], stdout);
3623 }
3624 putchar('\n');
3625 }
3626
3627 free(buffer);
3628 }
3629
3630
3631 /*
3632 * 'print_fatal_error()' - Print a fatal error message.
3633 */
3634
3635 static void
3636 print_fatal_error(const char *s, /* I - Printf-style format string */
3637 ...) /* I - Additional arguments as needed */
3638 {
3639 char buffer[10240]; /* Format buffer */
3640 va_list ap; /* Pointer to arguments */
3641
3642
3643 /*
3644 * Format the error message...
3645 */
3646
3647 va_start(ap, s);
3648 vsnprintf(buffer, sizeof(buffer), s, ap);
3649 va_end(ap);
3650
3651 /*
3652 * Then output it...
3653 */
3654
3655 if (Output == _CUPS_OUTPUT_PLIST)
3656 {
3657 print_xml_header();
3658 print_xml_trailer(0, buffer);
3659 }
3660 else
3661 _cupsLangPrintf(stderr, "ipptool: %s", buffer);
3662 }
3663
3664
3665 /*
3666 * 'print_line()' - Print a line of formatted or CSV text.
3667 */
3668
3669 static void
3670 print_line(
3671 ipp_attribute_t *attr, /* I - First attribute for line */
3672 int num_displayed, /* I - Number of attributes to display */
3673 char **displayed, /* I - Attributes to display */
3674 size_t *widths) /* I - Column widths */
3675 {
3676 int i; /* Looping var */
3677 size_t maxlength; /* Max length of all columns */
3678 char *buffer; /* String buffer */
3679 ipp_attribute_t *current; /* Current attribute */
3680
3681
3682 /*
3683 * Get the maximum string length we have to show and allocate...
3684 */
3685
3686 for (i = 1, maxlength = widths[0]; i < num_displayed; i ++)
3687 if (widths[i] > maxlength)
3688 maxlength = widths[i];
3689
3690 maxlength += 2;
3691
3692 if ((buffer = malloc(maxlength)) == NULL)
3693 return;
3694
3695 /*
3696 * Loop through the attributes to display...
3697 */
3698
3699 if (attr)
3700 {
3701 for (i = 0; i < num_displayed; i ++)
3702 {
3703 if (i)
3704 putchar(' ');
3705
3706 buffer[0] = '\0';
3707
3708 for (current = attr; current; current = current->next)
3709 {
3710 if (!current->name)
3711 break;
3712 else if (!strcmp(current->name, displayed[i]))
3713 {
3714 _ippAttrString(current, buffer, maxlength);
3715 break;
3716 }
3717 }
3718
3719 printf("%*s", (int)-widths[i], buffer);
3720 }
3721 putchar('\n');
3722 }
3723 else
3724 {
3725 for (i = 0; i < num_displayed; i ++)
3726 {
3727 if (i)
3728 putchar(' ');
3729
3730 printf("%*s", (int)-widths[i], displayed[i]);
3731 }
3732 putchar('\n');
3733
3734 for (i = 0; i < num_displayed; i ++)
3735 {
3736 if (i)
3737 putchar(' ');
3738
3739 memset(buffer, '-', widths[i]);
3740 buffer[widths[i]] = '\0';
3741 fputs(buffer, stdout);
3742 }
3743 putchar('\n');
3744 }
3745
3746 free(buffer);
3747 }
3748
3749
3750 /*
3751 * 'print_test_error()' - Print a test error message.
3752 */
3753
3754 static void
3755 print_test_error(const char *s, /* I - Printf-style format string */
3756 ...) /* I - Additional arguments as needed */
3757 {
3758 char buffer[10240]; /* Format buffer */
3759 va_list ap; /* Pointer to arguments */
3760
3761
3762 /*
3763 * Format the error message...
3764 */
3765
3766 va_start(ap, s);
3767 vsnprintf(buffer, sizeof(buffer), s, ap);
3768 va_end(ap);
3769
3770 /*
3771 * Then output it...
3772 */
3773
3774 if (Output == _CUPS_OUTPUT_PLIST)
3775 print_xml_string("string", buffer);
3776 else
3777 printf(" %s\n", buffer);
3778 }
3779
3780
3781 /*
3782 * 'print_xml_header()' - Print a standard XML plist header.
3783 */
3784
3785 static void
3786 print_xml_header(void)
3787 {
3788 if (!XMLHeader)
3789 {
3790 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
3791 puts("<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
3792 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
3793 puts("<plist version=\"1.0\">");
3794 puts("<dict>");
3795 puts("<key>Transfer</key>");
3796 printf("<string>%s</string>\n",
3797 Transfer == _CUPS_TRANSFER_AUTO ? "auto" :
3798 Transfer == _CUPS_TRANSFER_CHUNKED ? "chunked" : "length");
3799 puts("<key>Tests</key>");
3800 puts("<array>");
3801
3802 XMLHeader = 1;
3803 }
3804 }
3805
3806
3807 /*
3808 * 'print_xml_string()' - Print an XML string with escaping.
3809 */
3810
3811 static void
3812 print_xml_string(const char *element, /* I - Element name or NULL */
3813 const char *s) /* I - String to print */
3814 {
3815 if (element)
3816 printf("<%s>", element);
3817
3818 while (*s)
3819 {
3820 if (*s == '&')
3821 fputs("&amp;", stdout);
3822 else if (*s == '<')
3823 fputs("&lt;", stdout);
3824 else if (*s == '>')
3825 fputs("&gt;", stdout);
3826 else
3827 putchar(*s);
3828
3829 s ++;
3830 }
3831
3832 if (element)
3833 printf("</%s>\n", element);
3834 }
3835
3836
3837 /*
3838 * 'print_xml_trailer()' - Print the XML trailer with success/fail value.
3839 */
3840
3841 static void
3842 print_xml_trailer(int success, /* I - 1 on success, 0 on failure */
3843 const char *message) /* I - Error message or NULL */
3844 {
3845 if (XMLHeader)
3846 {
3847 puts("</array>");
3848 puts("<key>Successful</key>");
3849 puts(success ? "<true />" : "<false />");
3850 if (message)
3851 {
3852 puts("<key>ErrorMessage</key>");
3853 print_xml_string("string", message);
3854 }
3855 puts("</dict>");
3856 puts("</plist>");
3857
3858 XMLHeader = 0;
3859 }
3860 }
3861
3862
3863 /*
3864 * 'set_variable()' - Set a variable value.
3865 */
3866
3867 static void
3868 set_variable(_cups_vars_t *vars, /* I - Variables */
3869 const char *name, /* I - Variable name */
3870 const char *value) /* I - Value string */
3871 {
3872 _cups_var_t key, /* Search key */
3873 *var; /* New variable */
3874
3875
3876 key.name = (char *)name;
3877 if ((var = cupsArrayFind(vars->vars, &key)) != NULL)
3878 {
3879 free(var->value);
3880 var->value = strdup(value);
3881 }
3882 else if ((var = malloc(sizeof(_cups_var_t))) == NULL)
3883 {
3884 print_fatal_error("Unable to allocate memory for variable \"%s\".", name);
3885 exit(1);
3886 }
3887 else
3888 {
3889 var->name = strdup(name);
3890 var->value = strdup(value);
3891
3892 cupsArrayAdd(vars->vars, var);
3893 }
3894 }
3895
3896
3897 /*
3898 * 'timeout_cb()' - Handle HTTP timeouts.
3899 */
3900
3901 static int /* O - 1 to continue, 0 to cancel */
3902 timeout_cb(http_t *http, /* I - Connection to server (unused) */
3903 void *user_data) /* I - User data (unused) */
3904 {
3905 (void)http;
3906 (void)user_data;
3907
3908 return (0);
3909 }
3910
3911
3912 /*
3913 * 'usage()' - Show program usage.
3914 */
3915
3916 static void
3917 usage(void)
3918 {
3919 _cupsLangPuts(stderr, _("Usage: ipptool [options] URI filename [ ... "
3920 "filenameN ]"));
3921 _cupsLangPuts(stderr, _("Options:"));
3922 _cupsLangPuts(stderr, _(" -4 Connect using IPv4."));
3923 _cupsLangPuts(stderr, _(" -6 Connect using IPv6."));
3924 _cupsLangPuts(stderr, _(" -C Send requests using "
3925 "chunking (default)."));
3926 _cupsLangPuts(stderr, _(" -E Test with TLS "
3927 "encryption."));
3928 _cupsLangPuts(stderr, _(" -I Ignore errors."));
3929 _cupsLangPuts(stderr, _(" -L Send requests using "
3930 "content-length."));
3931 _cupsLangPuts(stderr, _(" -S Test with SSL "
3932 "encryption."));
3933 _cupsLangPuts(stderr, _(" -T Set the receive/send "
3934 "timeout in seconds."));
3935 _cupsLangPuts(stderr, _(" -V version Set default IPP "
3936 "version."));
3937 _cupsLangPuts(stderr, _(" -X Produce XML plist instead "
3938 "of plain text."));
3939 _cupsLangPuts(stderr, _(" -d name=value Set named variable to "
3940 "value."));
3941 _cupsLangPuts(stderr, _(" -f filename Set default request "
3942 "filename."));
3943 _cupsLangPuts(stderr, _(" -i seconds Repeat the last file with "
3944 "the given time interval."));
3945 _cupsLangPuts(stderr, _(" -n count Repeat the last file the "
3946 "given number of times."));
3947 _cupsLangPuts(stderr, _(" -q Be quiet - no output "
3948 "except errors."));
3949 _cupsLangPuts(stderr, _(" -t Produce a test report."));
3950 _cupsLangPuts(stderr, _(" -v Show all attributes sent "
3951 "and received."));
3952
3953 exit(1);
3954 }
3955
3956
3957 /*
3958 * 'validate_attr()' - Determine whether an attribute is valid.
3959 */
3960
3961 static int /* O - 1 if valid, 0 otherwise */
3962 validate_attr(ipp_attribute_t *attr, /* I - Attribute to validate */
3963 int print) /* I - 1 = report issues to stdout */
3964 {
3965 int i; /* Looping var */
3966 char scheme[64], /* Scheme from URI */
3967 userpass[256], /* Username/password from URI */
3968 hostname[256], /* Hostname from URI */
3969 resource[1024]; /* Resource from URI */
3970 int port, /* Port number from URI */
3971 uri_status, /* URI separation status */
3972 valid = 1; /* Is the attribute valid? */
3973 const char *ptr; /* Pointer into string */
3974 ipp_attribute_t *colattr; /* Collection attribute */
3975 regex_t re; /* Regular expression */
3976 ipp_uchar_t *date; /* Current date value */
3977
3978
3979 /*
3980 * Skip separators.
3981 */
3982
3983 if (!attr->name)
3984 return (1);
3985
3986 /*
3987 * Validate the attribute name.
3988 */
3989
3990 for (ptr = attr->name; *ptr; ptr ++)
3991 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' && *ptr != '_')
3992 break;
3993
3994 if (*ptr || ptr == attr->name)
3995 {
3996 valid = 0;
3997
3998 if (print)
3999 print_test_error("\"%s\": Bad attribute name - invalid character (RFC "
4000 "2911 section 4.1.3).", attr->name);
4001 }
4002
4003 if ((ptr - attr->name) > 255)
4004 {
4005 valid = 0;
4006
4007 if (print)
4008 print_test_error("\"%s\": Bad attribute name - bad length (RFC 2911 "
4009 "section 4.1.3).", attr->name);
4010 }
4011
4012 switch (attr->value_tag)
4013 {
4014 case IPP_TAG_INTEGER :
4015 break;
4016
4017 case IPP_TAG_BOOLEAN :
4018 for (i = 0; i < attr->num_values; i ++)
4019 {
4020 if (attr->values[i].boolean != 0 &&
4021 attr->values[i].boolean != 1)
4022 {
4023 valid = 0;
4024
4025 if (print)
4026 print_test_error("\"%s\": Bad boolen value %d (RFC 2911 section "
4027 "4.1.10).", attr->name, attr->values[i].boolean);
4028 else
4029 break;
4030 }
4031 }
4032 break;
4033
4034 case IPP_TAG_ENUM :
4035 for (i = 0; i < attr->num_values; i ++)
4036 {
4037 if (attr->values[i].integer < 1)
4038 {
4039 valid = 0;
4040
4041 if (print)
4042 print_test_error("\"%s\": Bad enum value %d - out of range "
4043 "(RFC 2911 section 4.1.4).", attr->name,
4044 attr->values[i].integer);
4045 else
4046 break;
4047 }
4048 }
4049 break;
4050
4051 case IPP_TAG_STRING :
4052 for (i = 0; i < attr->num_values; i ++)
4053 {
4054 if (attr->values[i].unknown.length > 1023)
4055 {
4056 valid = 0;
4057
4058 if (print)
4059 print_test_error("\"%s\": Bad octetString value - bad length %d "
4060 "(RFC 2911 section 4.1.10).", attr->name,
4061 attr->values[i].unknown.length);
4062 else
4063 break;
4064 }
4065 }
4066 break;
4067
4068 case IPP_TAG_DATE :
4069 for (i = 0; i < attr->num_values; i ++)
4070 {
4071 date = attr->values[i].date;
4072
4073 if (date[2] < 1 || date[2] > 12)
4074 {
4075 valid = 0;
4076
4077 if (print)
4078 print_test_error("\"%s\": Bad dateTime month %u (RFC 2911 "
4079 "section 4.1.13).", attr->name, date[2]);
4080 else
4081 break;
4082 }
4083
4084 if (date[3] < 1 || date[3] > 31)
4085 {
4086 valid = 0;
4087
4088 if (print)
4089 print_test_error("\"%s\": Bad dateTime day %u (RFC 2911 "
4090 "section 4.1.13).", attr->name, date[3]);
4091 else
4092 break;
4093 }
4094
4095 if (date[4] > 23)
4096 {
4097 valid = 0;
4098
4099 if (print)
4100 print_test_error("\"%s\": Bad dateTime hours %u (RFC 2911 "
4101 "section 4.1.13).", attr->name, date[4]);
4102 else
4103 break;
4104 }
4105
4106 if (date[5] > 59)
4107 {
4108 valid = 0;
4109
4110 if (print)
4111 print_test_error("\"%s\": Bad dateTime minutes %u (RFC 2911 "
4112 "section 4.1.13).", attr->name, date[5]);
4113 else
4114 break;
4115 }
4116
4117 if (date[6] > 60)
4118 {
4119 valid = 0;
4120
4121 if (print)
4122 print_test_error("\"%s\": Bad dateTime seconds %u (RFC 2911 "
4123 "section 4.1.13).", attr->name, date[6]);
4124 else
4125 break;
4126 }
4127
4128 if (date[7] > 9)
4129 {
4130 valid = 0;
4131
4132 if (print)
4133 print_test_error("\"%s\": Bad dateTime deciseconds %u (RFC 2911 "
4134 "section 4.1.13).", attr->name, date[7]);
4135 else
4136 break;
4137 }
4138
4139 if (date[8] != '-' && date[8] != '+')
4140 {
4141 valid = 0;
4142
4143 if (print)
4144 print_test_error("\"%s\": Bad dateTime UTC sign '%c' (RFC 2911 "
4145 "section 4.1.13).", attr->name, date[8]);
4146 else
4147 break;
4148 }
4149
4150 if (date[9] > 11)
4151 {
4152 valid = 0;
4153
4154 if (print)
4155 print_test_error("\"%s\": Bad dateTime UTC hours %u (RFC 2911 "
4156 "section 4.1.13).", attr->name, date[9]);
4157 else
4158 break;
4159 }
4160
4161 if (date[10] > 59)
4162 {
4163 valid = 0;
4164
4165 if (print)
4166 print_test_error("\"%s\": Bad dateTime UTC minutes %u (RFC 2911 "
4167 "section 4.1.13).", attr->name, date[10]);
4168 else
4169 break;
4170 }
4171 }
4172 break;
4173
4174 case IPP_TAG_RESOLUTION :
4175 for (i = 0; i < attr->num_values; i ++)
4176 {
4177 if (attr->values[i].resolution.xres <= 0)
4178 {
4179 valid = 0;
4180
4181 if (print)
4182 print_test_error("\"%s\": Bad resolution value %dx%d%s - cross "
4183 "feed resolution must be positive (RFC 2911 "
4184 "section 4.1.13).", attr->name,
4185 attr->values[i].resolution.xres,
4186 attr->values[i].resolution.yres,
4187 attr->values[i].resolution.units ==
4188 IPP_RES_PER_INCH ? "dpi" :
4189 attr->values[i].resolution.units ==
4190 IPP_RES_PER_CM ? "dpc" : "unknown");
4191 else
4192 break;
4193 }
4194
4195 if (attr->values[i].resolution.yres <= 0)
4196 {
4197 valid = 0;
4198
4199 if (print)
4200 print_test_error("\"%s\": Bad resolution value %dx%d%s - feed "
4201 "resolution must be positive (RFC 2911 section "
4202 "4.1.13).", attr->name,
4203 attr->values[i].resolution.xres,
4204 attr->values[i].resolution.yres,
4205 attr->values[i].resolution.units ==
4206 IPP_RES_PER_INCH ? "dpi" :
4207 attr->values[i].resolution.units ==
4208 IPP_RES_PER_CM ? "dpc" : "unknown");
4209 else
4210 break;
4211 }
4212
4213 if (attr->values[i].resolution.units != IPP_RES_PER_INCH &&
4214 attr->values[i].resolution.units != IPP_RES_PER_CM)
4215 {
4216 valid = 0;
4217
4218 if (print)
4219 print_test_error("\"%s\": Bad resolution value %dx%d%s - bad "
4220 "units value (RFC 2911 section 4.1.13).",
4221 attr->name, attr->values[i].resolution.xres,
4222 attr->values[i].resolution.yres,
4223 attr->values[i].resolution.units ==
4224 IPP_RES_PER_INCH ? "dpi" :
4225 attr->values[i].resolution.units ==
4226 IPP_RES_PER_CM ? "dpc" : "unknown");
4227 else
4228 break;
4229 }
4230 }
4231 break;
4232
4233 case IPP_TAG_RANGE :
4234 for (i = 0; i < attr->num_values; i ++)
4235 {
4236 if (attr->values[i].range.lower > attr->values[i].range.upper)
4237 {
4238 valid = 0;
4239
4240 if (print)
4241 print_test_error("\"%s\": Bad rangeOfInteger value %d-%d - lower "
4242 "greater than upper (RFC 2911 section 4.1.13).",
4243 attr->name, attr->values[i].range.lower,
4244 attr->values[i].range.upper);
4245 else
4246 break;
4247 }
4248 }
4249 break;
4250
4251 case IPP_TAG_BEGIN_COLLECTION :
4252 for (i = 0; i < attr->num_values; i ++)
4253 {
4254 for (colattr = attr->values[i].collection->attrs;
4255 colattr;
4256 colattr = colattr->next)
4257 {
4258 if (!validate_attr(colattr, 0))
4259 {
4260 valid = 0;
4261 break;
4262 }
4263 }
4264
4265 if (colattr && print)
4266 {
4267 print_test_error("\"%s\": Bad collection value.", attr->name);
4268
4269 while (colattr)
4270 {
4271 validate_attr(colattr, print);
4272 colattr = colattr->next;
4273 }
4274 }
4275 }
4276 break;
4277
4278 case IPP_TAG_TEXT :
4279 case IPP_TAG_TEXTLANG :
4280 for (i = 0; i < attr->num_values; i ++)
4281 {
4282 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4283 {
4284 if ((*ptr & 0xe0) == 0xc0)
4285 {
4286 ptr ++;
4287 if ((*ptr & 0xc0) != 0x80)
4288 break;
4289 }
4290 else if ((*ptr & 0xf0) == 0xe0)
4291 {
4292 ptr ++;
4293 if ((*ptr & 0xc0) != 0x80)
4294 break;
4295 ptr ++;
4296 if ((*ptr & 0xc0) != 0x80)
4297 break;
4298 }
4299 else if ((*ptr & 0xf8) == 0xf0)
4300 {
4301 ptr ++;
4302 if ((*ptr & 0xc0) != 0x80)
4303 break;
4304 ptr ++;
4305 if ((*ptr & 0xc0) != 0x80)
4306 break;
4307 ptr ++;
4308 if ((*ptr & 0xc0) != 0x80)
4309 break;
4310 }
4311 else if (*ptr & 0x80)
4312 break;
4313 }
4314
4315 if (*ptr)
4316 {
4317 valid = 0;
4318
4319 if (print)
4320 print_test_error("\"%s\": Bad text value \"%s\" - bad UTF-8 "
4321 "sequence (RFC 2911 section 4.1.1).", attr->name,
4322 attr->values[i].string.text);
4323 else
4324 break;
4325 }
4326
4327 if ((ptr - attr->values[i].string.text) > 1023)
4328 {
4329 valid = 0;
4330
4331 if (print)
4332 print_test_error("\"%s\": Bad text value \"%s\" - bad length %d "
4333 "(RFC 2911 section 4.1.1).", attr->name,
4334 attr->values[i].string.text,
4335 (int)strlen(attr->values[i].string.text));
4336 else
4337 break;
4338 }
4339 }
4340 break;
4341
4342 case IPP_TAG_NAME :
4343 case IPP_TAG_NAMELANG :
4344 for (i = 0; i < attr->num_values; i ++)
4345 {
4346 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4347 {
4348 if ((*ptr & 0xe0) == 0xc0)
4349 {
4350 ptr ++;
4351 if ((*ptr & 0xc0) != 0x80)
4352 break;
4353 }
4354 else if ((*ptr & 0xf0) == 0xe0)
4355 {
4356 ptr ++;
4357 if ((*ptr & 0xc0) != 0x80)
4358 break;
4359 ptr ++;
4360 if ((*ptr & 0xc0) != 0x80)
4361 break;
4362 }
4363 else if ((*ptr & 0xf8) == 0xf0)
4364 {
4365 ptr ++;
4366 if ((*ptr & 0xc0) != 0x80)
4367 break;
4368 ptr ++;
4369 if ((*ptr & 0xc0) != 0x80)
4370 break;
4371 ptr ++;
4372 if ((*ptr & 0xc0) != 0x80)
4373 break;
4374 }
4375 else if (*ptr & 0x80)
4376 break;
4377 }
4378
4379 if (*ptr)
4380 {
4381 valid = 0;
4382
4383 if (print)
4384 print_test_error("\"%s\": Bad name value \"%s\" - bad UTF-8 "
4385 "sequence (RFC 2911 section 4.1.2).", attr->name,
4386 attr->values[i].string.text);
4387 else
4388 break;
4389 }
4390
4391 if ((ptr - attr->values[i].string.text) > 1023)
4392 {
4393 valid = 0;
4394
4395 if (print)
4396 print_test_error("\"%s\": Bad name value \"%s\" - bad length %d "
4397 "(RFC 2911 section 4.1.2).", attr->name,
4398 attr->values[i].string.text,
4399 (int)strlen(attr->values[i].string.text));
4400 else
4401 break;
4402 }
4403 }
4404 break;
4405
4406 case IPP_TAG_KEYWORD :
4407 for (i = 0; i < attr->num_values; i ++)
4408 {
4409 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4410 if (!isalnum(*ptr & 255) && *ptr != '-' && *ptr != '.' &&
4411 *ptr != '_')
4412 break;
4413
4414 if (*ptr || ptr == attr->values[i].string.text)
4415 {
4416 valid = 0;
4417
4418 if (print)
4419 print_test_error("\"%s\": Bad keyword value \"%s\" - invalid "
4420 "character (RFC 2911 section 4.1.3).",
4421 attr->name, attr->values[i].string.text);
4422 else
4423 break;
4424 }
4425
4426 if ((ptr - attr->values[i].string.text) > 255)
4427 {
4428 valid = 0;
4429
4430 if (print)
4431 print_test_error("\"%s\": Bad keyword value \"%s\" - bad "
4432 "length %d (RFC 2911 section 4.1.3).",
4433 attr->name, attr->values[i].string.text,
4434 (int)strlen(attr->values[i].string.text));
4435 else
4436 break;
4437 }
4438 }
4439 break;
4440
4441 case IPP_TAG_URI :
4442 for (i = 0; i < attr->num_values; i ++)
4443 {
4444 uri_status = httpSeparateURI(HTTP_URI_CODING_ALL,
4445 attr->values[i].string.text,
4446 scheme, sizeof(scheme),
4447 userpass, sizeof(userpass),
4448 hostname, sizeof(hostname),
4449 &port, resource, sizeof(resource));
4450
4451 if (uri_status < HTTP_URI_OK)
4452 {
4453 valid = 0;
4454
4455 if (print)
4456 print_test_error("\"%s\": Bad URI value \"%s\" - %s "
4457 "(RFC 2911 section 4.1.5).", attr->name,
4458 attr->values[i].string.text,
4459 URIStatusStrings[uri_status -
4460 HTTP_URI_OVERFLOW]);
4461 else
4462 break;
4463 }
4464
4465 if (strlen(attr->values[i].string.text) > 1023)
4466 {
4467 valid = 0;
4468
4469 if (print)
4470 print_test_error("\"%s\": Bad URI value \"%s\" - bad length %d "
4471 "(RFC 2911 section 4.1.5).", attr->name,
4472 attr->values[i].string.text,
4473 (int)strlen(attr->values[i].string.text));
4474 else
4475 break;
4476 }
4477 }
4478 break;
4479
4480 case IPP_TAG_URISCHEME :
4481 for (i = 0; i < attr->num_values; i ++)
4482 {
4483 ptr = attr->values[i].string.text;
4484 if (islower(*ptr & 255))
4485 {
4486 for (ptr ++; *ptr; ptr ++)
4487 if (!islower(*ptr & 255) && !isdigit(*ptr & 255) &&
4488 *ptr != '+' && *ptr != '-' && *ptr != '.')
4489 break;
4490 }
4491
4492 if (*ptr || ptr == attr->values[i].string.text)
4493 {
4494 valid = 0;
4495
4496 if (print)
4497 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4498 "characters (RFC 2911 section 4.1.6).",
4499 attr->name, attr->values[i].string.text);
4500 else
4501 break;
4502 }
4503
4504 if ((ptr - attr->values[i].string.text) > 63)
4505 {
4506 valid = 0;
4507
4508 if (print)
4509 print_test_error("\"%s\": Bad uriScheme value \"%s\" - bad "
4510 "length %d (RFC 2911 section 4.1.6).",
4511 attr->name, attr->values[i].string.text,
4512 (int)strlen(attr->values[i].string.text));
4513 else
4514 break;
4515 }
4516 }
4517 break;
4518
4519 case IPP_TAG_CHARSET :
4520 for (i = 0; i < attr->num_values; i ++)
4521 {
4522 for (ptr = attr->values[i].string.text; *ptr; ptr ++)
4523 if (!isprint(*ptr & 255) || isupper(*ptr & 255) ||
4524 isspace(*ptr & 255))
4525 break;
4526
4527 if (*ptr || ptr == attr->values[i].string.text)
4528 {
4529 valid = 0;
4530
4531 if (print)
4532 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4533 "characters (RFC 2911 section 4.1.7).",
4534 attr->name, attr->values[i].string.text);
4535 else
4536 break;
4537 }
4538
4539 if ((ptr - attr->values[i].string.text) > 40)
4540 {
4541 valid = 0;
4542
4543 if (print)
4544 print_test_error("\"%s\": Bad charset value \"%s\" - bad "
4545 "length %d (RFC 2911 section 4.1.7).",
4546 attr->name, attr->values[i].string.text,
4547 (int)strlen(attr->values[i].string.text));
4548 else
4549 break;
4550 }
4551 }
4552 break;
4553
4554 case IPP_TAG_LANGUAGE :
4555 /*
4556 * The following regular expression is derived from the ABNF for
4557 * language tags in RFC 4646. All I can say is that this is the
4558 * easiest way to check the values...
4559 */
4560
4561 if ((i = regcomp(&re,
4562 "^("
4563 "(([a-z]{2,3}(-[a-z][a-z][a-z]){0,3})|[a-z]{4,8})"
4564 /* language */
4565 "(-[a-z][a-z][a-z][a-z]){0,1}" /* script */
4566 "(-([a-z][a-z]|[0-9][0-9][0-9])){0,1}" /* region */
4567 "(-([a-z]{5,8}|[0-9][0-9][0-9]))*" /* variant */
4568 "(-[a-wy-z](-[a-z0-9]{2,8})+)*" /* extension */
4569 "(-x(-[a-z0-9]{1,8})+)*" /* privateuse */
4570 "|"
4571 "x(-[a-z0-9]{1,8})+" /* privateuse */
4572 "|"
4573 "[a-z]{1,3}(-[a-z][0-9]{2,8}){1,2}" /* grandfathered */
4574 ")$",
4575 REG_NOSUB | REG_EXTENDED)) != 0)
4576 {
4577 char temp[256]; /* Temporary error string */
4578
4579 regerror(i, &re, temp, sizeof(temp));
4580 print_fatal_error("Unable to compile naturalLanguage regular "
4581 "expression: %s.", temp);
4582 }
4583
4584 for (i = 0; i < attr->num_values; i ++)
4585 {
4586 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
4587 {
4588 valid = 0;
4589
4590 if (print)
4591 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4592 "characters (RFC 2911 section 4.1.8).",
4593 attr->name, attr->values[i].string.text);
4594 else
4595 break;
4596 }
4597
4598 if (strlen(attr->values[i].string.text) > 63)
4599 {
4600 valid = 0;
4601
4602 if (print)
4603 print_test_error("\"%s\": Bad naturalLanguage value \"%s\" - bad "
4604 "length %d (RFC 2911 section 4.1.8).",
4605 attr->name, attr->values[i].string.text,
4606 (int)strlen(attr->values[i].string.text));
4607 else
4608 break;
4609 }
4610 }
4611
4612 regfree(&re);
4613 break;
4614
4615 case IPP_TAG_MIMETYPE :
4616 /*
4617 * The following regular expression is derived from the ABNF for
4618 * language tags in RFC 2045 and 4288. All I can say is that this is
4619 * the easiest way to check the values...
4620 */
4621
4622 if ((i = regcomp(&re,
4623 "^"
4624 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* type-name */
4625 "/"
4626 "[-a-zA-Z0-9!#$&.+^_]{1,127}" /* subtype-name */
4627 "(;[-a-zA-Z0-9!#$&.+^_]{1,127}=" /* parameter= */
4628 "([-a-zA-Z0-9!#$&.+^_]{1,127}|\"[^\"]*\"))*"
4629 /* value */
4630 "$",
4631 REG_NOSUB | REG_EXTENDED)) != 0)
4632 {
4633 char temp[256]; /* Temporary error string */
4634
4635 regerror(i, &re, temp, sizeof(temp));
4636 print_fatal_error("Unable to compile mimeMediaType regular "
4637 "expression: %s.", temp);
4638 }
4639
4640 for (i = 0; i < attr->num_values; i ++)
4641 {
4642 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
4643 {
4644 valid = 0;
4645
4646 if (print)
4647 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4648 "characters (RFC 2911 section 4.1.9).",
4649 attr->name, attr->values[i].string.text);
4650 else
4651 break;
4652 }
4653
4654 if (strlen(attr->values[i].string.text) > 255)
4655 {
4656 valid = 0;
4657
4658 if (print)
4659 print_test_error("\"%s\": Bad mimeMediaType value \"%s\" - bad "
4660 "length %d (RFC 2911 section 4.1.9).",
4661 attr->name, attr->values[i].string.text,
4662 (int)strlen(attr->values[i].string.text));
4663 else
4664 break;
4665 }
4666 }
4667 break;
4668
4669 default :
4670 break;
4671 }
4672
4673 return (valid);
4674 }
4675
4676
4677 /*
4678 * 'with_value()' - Test a WITH-VALUE predicate.
4679 */
4680
4681 static int /* O - 1 on match, 0 on non-match */
4682 with_value(char *value, /* I - Value string */
4683 int regex, /* I - Value is a regular expression */
4684 ipp_attribute_t *attr, /* I - Attribute to compare */
4685 int report) /* I - 1 = report failures */
4686 {
4687 int i; /* Looping var */
4688 char *valptr; /* Pointer into value */
4689
4690
4691 /*
4692 * NULL matches everything.
4693 */
4694
4695 if (!value || !*value)
4696 return (1);
4697
4698 /*
4699 * Compare the value string to the attribute value.
4700 */
4701
4702 switch (attr->value_tag)
4703 {
4704 case IPP_TAG_INTEGER :
4705 case IPP_TAG_ENUM :
4706 for (i = 0; i < attr->num_values; i ++)
4707 {
4708 char op, /* Comparison operator */
4709 *nextptr; /* Next pointer */
4710 int intvalue; /* Integer value */
4711
4712
4713 valptr = value;
4714 if (!strncmp(valptr, "no-value,", 9))
4715 valptr += 9;
4716
4717 while (isspace(*valptr & 255) || isdigit(*valptr & 255) ||
4718 *valptr == '-' || *valptr == ',' || *valptr == '<' ||
4719 *valptr == '=' || *valptr == '>')
4720 {
4721 op = '=';
4722 while (*valptr && !isdigit(*valptr & 255) && *valptr != '-')
4723 {
4724 if (*valptr == '<' || *valptr == '>' || *valptr == '=')
4725 op = *valptr;
4726 valptr ++;
4727 }
4728
4729 if (!*valptr)
4730 break;
4731
4732 intvalue = strtol(valptr, &nextptr, 0);
4733 if (nextptr == valptr)
4734 break;
4735 valptr = nextptr;
4736
4737 switch (op)
4738 {
4739 case '=' :
4740 if (attr->values[i].integer == intvalue)
4741 return (1);
4742 break;
4743 case '<' :
4744 if (attr->values[i].integer < intvalue)
4745 return (1);
4746 break;
4747 case '>' :
4748 if (attr->values[i].integer > intvalue)
4749 return (1);
4750 break;
4751 }
4752 }
4753 }
4754
4755 if (report)
4756 {
4757 for (i = 0; i < attr->num_values; i ++)
4758 print_test_error("GOT: %s=%d", attr->name, attr->values[i].integer);
4759 }
4760 break;
4761
4762 case IPP_TAG_RANGE :
4763 for (i = 0; i < attr->num_values; i ++)
4764 {
4765 char op, /* Comparison operator */
4766 *nextptr; /* Next pointer */
4767 int intvalue; /* Integer value */
4768
4769
4770 valptr = value;
4771 if (!strncmp(valptr, "no-value,", 9))
4772 valptr += 9;
4773
4774 while (isspace(*valptr & 255) || isdigit(*valptr & 255) ||
4775 *valptr == '-' || *valptr == ',' || *valptr == '<' ||
4776 *valptr == '=' || *valptr == '>')
4777 {
4778 op = '=';
4779 while (*valptr && !isdigit(*valptr & 255) && *valptr != '-')
4780 {
4781 if (*valptr == '<' || *valptr == '>' || *valptr == '=')
4782 op = *valptr;
4783 valptr ++;
4784 }
4785
4786 if (!*valptr)
4787 break;
4788
4789 intvalue = strtol(valptr, &nextptr, 0);
4790 if (nextptr == valptr)
4791 break;
4792 valptr = nextptr;
4793
4794 switch (op)
4795 {
4796 case '=' :
4797 if (attr->values[i].range.lower == intvalue ||
4798 attr->values[i].range.upper == intvalue)
4799 return (1);
4800 break;
4801 case '<' :
4802 if (attr->values[i].range.upper < intvalue)
4803 return (1);
4804 break;
4805 case '>' :
4806 if (attr->values[i].range.upper > intvalue)
4807 return (1);
4808 break;
4809 }
4810 }
4811 }
4812
4813 if (report)
4814 {
4815 for (i = 0; i < attr->num_values; i ++)
4816 print_test_error("GOT: %s=%d-%d", attr->name,
4817 attr->values[i].range.lower,
4818 attr->values[i].range.upper);
4819 }
4820 break;
4821
4822 case IPP_TAG_BOOLEAN :
4823 for (i = 0; i < attr->num_values; i ++)
4824 {
4825 if (!strcmp(value, "true") == attr->values[i].boolean)
4826 return (1);
4827 }
4828
4829 if (report)
4830 {
4831 for (i = 0; i < attr->num_values; i ++)
4832 print_test_error("GOT: %s=%s", attr->name,
4833 attr->values[i].boolean ? "true" : "false");
4834 }
4835 break;
4836
4837 case IPP_TAG_NOVALUE :
4838 return (!strcmp(value, "no-value") || !strncmp(value, "no-value,", 9));
4839
4840 case IPP_TAG_CHARSET :
4841 case IPP_TAG_KEYWORD :
4842 case IPP_TAG_LANGUAGE :
4843 case IPP_TAG_MIMETYPE :
4844 case IPP_TAG_NAME :
4845 case IPP_TAG_NAMELANG :
4846 case IPP_TAG_TEXT :
4847 case IPP_TAG_TEXTLANG :
4848 case IPP_TAG_URI :
4849 case IPP_TAG_URISCHEME :
4850 if (regex)
4851 {
4852 /*
4853 * Value is an extended, case-sensitive POSIX regular expression...
4854 */
4855
4856 regex_t re; /* Regular expression */
4857
4858 if ((i = regcomp(&re, value, REG_EXTENDED | REG_NOSUB)) != 0)
4859 {
4860 char temp[256]; /* Temporary string */
4861
4862 regerror(i, &re, temp, sizeof(temp));
4863
4864 print_fatal_error("Unable to compile WITH-VALUE regular expression "
4865 "\"%s\" - %s", value, temp);
4866 return (0);
4867 }
4868
4869 /*
4870 * See if ALL of the values match the given regular expression.
4871 */
4872
4873 for (i = 0; i < attr->num_values; i ++)
4874 {
4875 if (regexec(&re, attr->values[i].string.text, 0, NULL, 0))
4876 {
4877 if (report)
4878 print_test_error("GOT: %s=\"%s\"", attr->name,
4879 attr->values[i].string.text);
4880 else
4881 break;
4882 }
4883 }
4884
4885 regfree(&re);
4886
4887 return (i == attr->num_values);
4888 }
4889 else
4890 {
4891 /*
4892 * Value is a literal string, see if at least one value matches the
4893 * literal string...
4894 */
4895
4896 for (i = 0; i < attr->num_values; i ++)
4897 {
4898 if (!strcmp(value, attr->values[i].string.text))
4899 return (1);
4900 }
4901
4902 if (report)
4903 {
4904 for (i = 0; i < attr->num_values; i ++)
4905 print_test_error("GOT: %s=\"%s\"", attr->name,
4906 attr->values[i].string.text);
4907 }
4908 }
4909 break;
4910
4911 default :
4912 break;
4913 }
4914
4915 return (0);
4916 }
4917
4918
4919 /*
4920 * End of "$Id$".
4921 */