1 /*
2 * Option unit test program for CUPS.
3 *
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright 2008-2016 by Apple Inc.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include "cups-private.h"
15
16
17 /*
18 * 'main()' - Test option processing functions.
19 */
20
21 int /* O - Exit status */
main(int argc,char * argv[])22 main(int argc, /* I - Number of command-line arguments */
23 char *argv[]) /* I - Command-line arguments */
24 {
25 int status = 0, /* Exit status */
26 num_options; /* Number of options */
27 cups_option_t *options; /* Options */
28 const char *value; /* Value of an option */
29 ipp_t *request; /* IPP request */
30 ipp_attribute_t *attr; /* IPP attribute */
31 int count; /* Number of attributes */
32
33
34 if (argc == 1)
35 {
36 /*
37 * cupsParseOptions()
38 */
39
40 fputs("cupsParseOptions: ", stdout);
41
42 num_options = cupsParseOptions("foo=1234 "
43 "bar=\"One Fish\",\"Two Fish\",\"Red Fish\","
44 "\"Blue Fish\" "
45 "baz={param1=1 param2=2} "
46 "foobar=FOO\\ BAR "
47 "barfoo=barfoo "
48 "barfoo=\"\'BAR FOO\'\" "
49 "auth-info=user,pass\\\\,word\\\\\\\\", 0, &options);
50
51 if (num_options != 6)
52 {
53 printf("FAIL (num_options=%d, expected 6)\n", num_options);
54 status ++;
55 }
56 else if ((value = cupsGetOption("foo", num_options, options)) == NULL ||
57 strcmp(value, "1234"))
58 {
59 printf("FAIL (foo=\"%s\", expected \"1234\")\n", value);
60 status ++;
61 }
62 else if ((value = cupsGetOption("bar", num_options, options)) == NULL ||
63 strcmp(value, "One Fish,Two Fish,Red Fish,Blue Fish"))
64 {
65 printf("FAIL (bar=\"%s\", expected \"One Fish,Two Fish,Red Fish,Blue "
66 "Fish\")\n", value);
67 status ++;
68 }
69 else if ((value = cupsGetOption("baz", num_options, options)) == NULL ||
70 strcmp(value, "{param1=1 param2=2}"))
71 {
72 printf("FAIL (baz=\"%s\", expected \"{param1=1 param2=2}\")\n", value);
73 status ++;
74 }
75 else if ((value = cupsGetOption("foobar", num_options, options)) == NULL ||
76 strcmp(value, "FOO BAR"))
77 {
78 printf("FAIL (foobar=\"%s\", expected \"FOO BAR\")\n", value);
79 status ++;
80 }
81 else if ((value = cupsGetOption("barfoo", num_options, options)) == NULL ||
82 strcmp(value, "\'BAR FOO\'"))
83 {
84 printf("FAIL (barfoo=\"%s\", expected \"\'BAR FOO\'\")\n", value);
85 status ++;
86 }
87 else if ((value = cupsGetOption("auth-info", num_options, options)) == NULL ||
88 strcmp(value, "user,pass\\,word\\\\"))
89 {
90 printf("FAIL (auth-info=\"%s\", expected \"user,pass\\,word\\\\\")\n", value);
91 status ++;
92 }
93 else
94 puts("PASS");
95
96 fputs("cupsEncodeOptions2: ", stdout);
97 request = ippNew();
98 ippSetOperation(request, IPP_OP_PRINT_JOB);
99
100 cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
101 for (count = 0, attr = ippFirstAttribute(request); attr; attr = ippNextAttribute(request), count ++);
102 if (count != 6)
103 {
104 printf("FAIL (%d attributes, expected 6)\n", count);
105 status ++;
106 }
107 else if ((attr = ippFindAttribute(request, "foo", IPP_TAG_ZERO)) == NULL)
108 {
109 puts("FAIL (Unable to find attribute \"foo\")");
110 status ++;
111 }
112 else if (ippGetValueTag(attr) != IPP_TAG_NAME)
113 {
114 printf("FAIL (\"foo\" of type %s, expected name)\n", ippTagString(ippGetValueTag(attr)));
115 status ++;
116 }
117 else if (ippGetCount(attr) != 1)
118 {
119 printf("FAIL (\"foo\" has %d values, expected 1)\n", (int)ippGetCount(attr));
120 status ++;
121 }
122 else if (strcmp(ippGetString(attr, 0, NULL), "1234"))
123 {
124 printf("FAIL (\"foo\" has value %s, expected 1234)\n", ippGetString(attr, 0, NULL));
125 status ++;
126 }
127 else if ((attr = ippFindAttribute(request, "auth-info", IPP_TAG_ZERO)) == NULL)
128 {
129 puts("FAIL (Unable to find attribute \"auth-info\")");
130 status ++;
131 }
132 else if (ippGetValueTag(attr) != IPP_TAG_TEXT)
133 {
134 printf("FAIL (\"auth-info\" of type %s, expected text)\n", ippTagString(ippGetValueTag(attr)));
135 status ++;
136 }
137 else if (ippGetCount(attr) != 2)
138 {
139 printf("FAIL (\"auth-info\" has %d values, expected 2)\n", (int)ippGetCount(attr));
140 status ++;
141 }
142 else if (strcmp(ippGetString(attr, 0, NULL), "user"))
143 {
144 printf("FAIL (\"auth-info\"[0] has value \"%s\", expected \"user\")\n", ippGetString(attr, 0, NULL));
145 status ++;
146 }
147 else if (strcmp(ippGetString(attr, 1, NULL), "pass,word\\"))
148 {
149 printf("FAIL (\"auth-info\"[1] has value \"%s\", expected \"pass,word\\\")\n", ippGetString(attr, 1, NULL));
150 status ++;
151 }
152 else
153 puts("PASS");
154 }
155 else
156 {
157 int i; /* Looping var */
158 cups_option_t *option; /* Current option */
159
160
161 num_options = cupsParseOptions(argv[1], 0, &options);
162
163 for (i = 0, option = options; i < num_options; i ++, option ++)
164 printf("options[%d].name=\"%s\", value=\"%s\"\n", i, option->name,
165 option->value);
166 }
167
168 exit (status);
169 }
170