Debian Codegraph @

src/utils/cmds/cmds_test.c

raw
1 /**
2 * collectd - src/tests/utils_cmds_test.c
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Sebastian 'tokkee' Harl <sh at tokkee.org>
25 **/
26
27 // clang-format off
28 /*
29 * Explicit order is required or _FILE_OFFSET_BITS will have definition mismatches on Solaris
30 * See Github Issue #3193 for details
31 */
32 #include "utils/common/common.h"
33 #include "testing.h"
34 #include "utils/cmds/cmds.h"
35 // clang-format on
36
37 static void error_cb(void *ud, cmd_status_t status, const char *format,
38 va_list ap) {
39 if (status == CMD_OK)
40 return;
41
42 printf("ERROR[%d]: ", status);
43 vprintf(format, ap);
44 printf("\n");
45 fflush(stdout);
46 } /* void error_cb */
47
48 static cmd_options_t default_host_opts = {
49 /* identifier_default_host = */ "dummy-host",
50 };
51
52 static struct {
53 char *input;
54 cmd_options_t *opts;
55 cmd_status_t expected_status;
56 cmd_type_t expected_type;
57 } parse_data[] = {
58 /* Valid FLUSH commands. */
59 {
60 "FLUSH",
61 NULL,
62 CMD_OK,
63 CMD_FLUSH,
64 },
65 {
66 "FLUSH identifier=myhost/magic/MAGIC",
67 NULL,
68 CMD_OK,
69 CMD_FLUSH,
70 },
71 {
72 "FLUSH identifier=magic/MAGIC",
73 &default_host_opts,
74 CMD_OK,
75 CMD_FLUSH,
76 },
77 {
78 "FLUSH timeout=123 plugin=\"A\"",
79 NULL,
80 CMD_OK,
81 CMD_FLUSH,
82 },
83 /* Invalid FLUSH commands. */
84 {
85 /* Missing hostname; no default. */
86 "FLUSH identifier=magic/MAGIC",
87 NULL,
88 CMD_PARSE_ERROR,
89 CMD_UNKNOWN,
90 },
91 {
92 /* Missing 'identifier' key. */
93 "FLUSH myhost/magic/MAGIC",
94 NULL,
95 CMD_PARSE_ERROR,
96 CMD_UNKNOWN,
97 },
98 {
99 /* Invalid timeout. */
100 "FLUSH timeout=A",
101 NULL,
102 CMD_PARSE_ERROR,
103 CMD_UNKNOWN,
104 },
105 {
106 /* Invalid identifier. */
107 "FLUSH identifier=invalid",
108 NULL,
109 CMD_PARSE_ERROR,
110 CMD_UNKNOWN,
111 },
112 {
113 /* Invalid option. */
114 "FLUSH invalid=option",
115 NULL,
116 CMD_PARSE_ERROR,
117 CMD_UNKNOWN,
118 },
119
120 /* Valid GETVAL commands. */
121 {
122 "GETVAL myhost/magic/MAGIC",
123 NULL,
124 CMD_OK,
125 CMD_GETVAL,
126 },
127 {
128 "GETVAL magic/MAGIC",
129 &default_host_opts,
130 CMD_OK,
131 CMD_GETVAL,
132 },
133
134 /* Invalid GETVAL commands. */
135 {
136 "GETVAL magic/MAGIC",
137 NULL,
138 CMD_PARSE_ERROR,
139 CMD_UNKNOWN,
140 },
141 {
142 "GETVAL",
143 NULL,
144 CMD_PARSE_ERROR,
145 CMD_UNKNOWN,
146 },
147 {
148 "GETVAL invalid",
149 NULL,
150 CMD_PARSE_ERROR,
151 CMD_UNKNOWN,
152 },
153
154 /* Valid LISTVAL commands. */
155 {
156 "LISTVAL",
157 NULL,
158 CMD_OK,
159 CMD_LISTVAL,
160 },
161
162 /* Invalid LISTVAL commands. */
163 {
164 "LISTVAL invalid",
165 NULL,
166 CMD_PARSE_ERROR,
167 CMD_UNKNOWN,
168 },
169
170 /* Valid PUTVAL commands. */
171 {
172 "PUTVAL magic/MAGIC N:42",
173 &default_host_opts,
174 CMD_OK,
175 CMD_PUTVAL,
176 },
177 {
178 "PUTVAL myhost/magic/MAGIC N:42",
179 NULL,
180 CMD_OK,
181 CMD_PUTVAL,
182 },
183 {
184 "PUTVAL myhost/magic/MAGIC 1234:42",
185 NULL,
186 CMD_OK,
187 CMD_PUTVAL,
188 },
189 {
190 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
191 NULL,
192 CMD_OK,
193 CMD_PUTVAL,
194 },
195 {
196 "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
197 NULL,
198 CMD_OK,
199 CMD_PUTVAL,
200 },
201 {
202 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
203 NULL,
204 CMD_OK,
205 CMD_PUTVAL,
206 },
207 {
208 "PUTVAL myhost/magic/MAGIC meta:KEY=\"string_value\" 1234:42",
209 NULL,
210 CMD_OK,
211 CMD_PUTVAL,
212 },
213 {
214 "PUTVAL myhost/magic/MAGIC meta:KEY='string_value' 1234:42",
215 NULL,
216 CMD_OK,
217 CMD_PUTVAL,
218 },
219
220 /* Invalid PUTVAL commands. */
221 {
222 "PUTVAL magic/MAGIC N:42",
223 NULL,
224 CMD_PARSE_ERROR,
225 CMD_UNKNOWN,
226 },
227 {
228 "PUTVAL",
229 NULL,
230 CMD_PARSE_ERROR,
231 CMD_UNKNOWN,
232 },
233 {
234 "PUTVAL invalid N:42",
235 NULL,
236 CMD_PARSE_ERROR,
237 CMD_UNKNOWN,
238 },
239 {
240 "PUTVAL myhost/magic/MAGIC A:42",
241 NULL,
242 CMD_PARSE_ERROR,
243 CMD_UNKNOWN,
244 },
245 {
246 "PUTVAL myhost/magic/MAGIC 1234:A",
247 NULL,
248 CMD_PARSE_ERROR,
249 CMD_UNKNOWN,
250 },
251 {
252 "PUTVAL myhost/magic/MAGIC",
253 NULL,
254 CMD_PARSE_ERROR,
255 CMD_UNKNOWN,
256 },
257 {
258 "PUTVAL 1234:A",
259 NULL,
260 CMD_PARSE_ERROR,
261 CMD_UNKNOWN,
262 },
263 {
264 "PUTVAL myhost/magic/UNKNOWN 1234:42",
265 NULL,
266 CMD_PARSE_ERROR,
267 CMD_UNKNOWN,
268 },
269 /*
270 * As of collectd 5.x, PUTVAL accepts invalid options.
271 {
272 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
273 NULL,
274 CMD_PARSE_ERROR,
275 CMD_UNKNOWN,
276 },
277 */
278
279 /* Invalid commands. */
280 {
281 "INVALID",
282 NULL,
283 CMD_UNKNOWN_COMMAND,
284 CMD_UNKNOWN,
285 },
286 {
287 "INVALID interval=2",
288 NULL,
289 CMD_UNKNOWN_COMMAND,
290 CMD_UNKNOWN,
291 },
292 };
293
294 DEF_TEST(parse) {
295 cmd_error_handler_t err = {error_cb, NULL};
296 int test_result = 0;
297
298 for (size_t i = 0; i < STATIC_ARRAY_SIZE(parse_data); i++) {
299 char *input = strdup(parse_data[i].input);
300
301 char description[1024];
302 cmd_status_t status;
303 cmd_t cmd;
304
305 bool result;
306
307 memset(&cmd, 0, sizeof(cmd));
308
309 status = cmd_parse(input, &cmd, parse_data[i].opts, &err);
310 ssnprintf(description, sizeof(description),
311 "cmd_parse (\"%s\", opts=%p) = "
312 "%d (type=%d [%s]); want %d "
313 "(type=%d [%s])",
314 parse_data[i].input, parse_data[i].opts, status, cmd.type,
315 CMD_TO_STRING(cmd.type), parse_data[i].expected_status,
316 parse_data[i].expected_type,
317 CMD_TO_STRING(parse_data[i].expected_type));
318 result = (status == parse_data[i].expected_status) &&
319 (cmd.type == parse_data[i].expected_type);
320 LOG(result, description);
321
322 /* Run all tests before failing. */
323 if (!result)
324 test_result = -1;
325
326 cmd_destroy(&cmd);
327 free(input);
328 }
329
330 return test_result;
331 }
332
333 int main(int argc, char **argv) {
334 RUN_TEST(parse);
335 END_TEST;
336 }