2 * collectd - src/tests/utils_cmds_test.c
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian 'tokkee' Harl <sh at tokkee.org>
28 #include "utils/cmds/cmds.h"
29 #include "utils/common/common.h"
31 static void error_cb(void *ud, cmd_status_t status, const char *format,
36 printf("ERROR[%d]: ", status);
42 static cmd_options_t default_host_opts = {
43 /* identifier_default_host = */ "dummy-host",
49 cmd_status_t expected_status;
50 cmd_type_t expected_type;
52 /* Valid FLUSH commands. */
60 "FLUSH identifier=myhost/magic/MAGIC",
66 "FLUSH identifier=magic/MAGIC",
72 "FLUSH timeout=123 plugin=\"A\"",
77 /* Invalid FLUSH commands. */
79 /* Missing hostname; no default. */
80 "FLUSH identifier=magic/MAGIC",
86 /* Missing 'identifier' key. */
87 "FLUSH myhost/magic/MAGIC",
93 /* Invalid timeout. */
100 /* Invalid identifier. */
101 "FLUSH identifier=invalid",
107 /* Invalid option. */
108 "FLUSH invalid=option",
114 /* Valid GETVAL commands. */
116 "GETVAL myhost/magic/MAGIC",
122 "GETVAL magic/MAGIC",
128 /* Invalid GETVAL commands. */
130 "GETVAL magic/MAGIC",
148 /* Valid LISTVAL commands. */
156 /* Invalid LISTVAL commands. */
164 /* Valid PUTVAL commands. */
166 "PUTVAL magic/MAGIC N:42",
172 "PUTVAL myhost/magic/MAGIC N:42",
178 "PUTVAL myhost/magic/MAGIC 1234:42",
184 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
190 "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
196 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
202 /* Invalid PUTVAL commands. */
204 "PUTVAL magic/MAGIC N:42",
216 "PUTVAL invalid N:42",
222 "PUTVAL myhost/magic/MAGIC A:42",
228 "PUTVAL myhost/magic/MAGIC 1234:A",
234 "PUTVAL myhost/magic/MAGIC",
246 "PUTVAL myhost/magic/UNKNOWN 1234:42",
252 * As of collectd 5.x, PUTVAL accepts invalid options.
254 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
261 /* Invalid commands. */
269 "INVALID interval=2",
277 cmd_error_handler_t err = {error_cb, NULL};
280 for (size_t i = 0; i < STATIC_ARRAY_SIZE(parse_data); i++) {
281 char *input = strdup(parse_data[i].input);
283 char description[1024];
289 memset(&cmd, 0, sizeof(cmd));
291 status = cmd_parse(input, &cmd, parse_data[i].opts, &err);
292 ssnprintf(description, sizeof(description),
293 "cmd_parse (\"%s\", opts=%p) = "
294 "%d (type=%d [%s]); want %d "
296 parse_data[i].input, parse_data[i].opts, status, cmd.type,
297 CMD_TO_STRING(cmd.type), parse_data[i].expected_status,
298 parse_data[i].expected_type,
299 CMD_TO_STRING(parse_data[i].expected_type));
300 result = (status == parse_data[i].expected_status) &&
301 (cmd.type == parse_data[i].expected_type);
302 LOG(result, description);
304 /* Run all tests before failing. */
315 int main(int argc, char **argv) {