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>
29 * Explicit order is required or _FILE_OFFSET_BITS will have definition mismatches on Solaris
30 * See Github Issue #3193 for details
32 #include "utils/common/common.h"
34 #include "utils/cmds/cmds.h"
37 static void error_cb(void *ud, cmd_status_t status, const char *format,
42 printf("ERROR[%d]: ", status);
48 static cmd_options_t default_host_opts = {
49 /* identifier_default_host = */ "dummy-host",
55 cmd_status_t expected_status;
56 cmd_type_t expected_type;
58 /* Valid FLUSH commands. */
60 "FLUSH", NULL, CMD_OK, CMD_FLUSH,
63 "FLUSH identifier=myhost/magic/MAGIC", NULL, CMD_OK, CMD_FLUSH,
66 "FLUSH identifier=magic/MAGIC", &default_host_opts, CMD_OK, CMD_FLUSH,
69 "FLUSH timeout=123 plugin=\"A\"", NULL, CMD_OK, CMD_FLUSH,
71 /* Invalid FLUSH commands. */
73 /* Missing hostname; no default. */
74 "FLUSH identifier=magic/MAGIC", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
77 /* Missing 'identifier' key. */
78 "FLUSH myhost/magic/MAGIC", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
81 /* Invalid timeout. */
82 "FLUSH timeout=A", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
85 /* Invalid identifier. */
86 "FLUSH identifier=invalid", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
90 "FLUSH invalid=option", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
93 /* Valid GETVAL commands. */
95 "GETVAL myhost/magic/MAGIC", NULL, CMD_OK, CMD_GETVAL,
98 "GETVAL magic/MAGIC", &default_host_opts, CMD_OK, CMD_GETVAL,
101 /* Invalid GETVAL commands. */
103 "GETVAL magic/MAGIC", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
106 "GETVAL", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
109 "GETVAL invalid", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
112 /* Valid LISTVAL commands. */
114 "LISTVAL", NULL, CMD_OK, CMD_LISTVAL,
117 /* Invalid LISTVAL commands. */
119 "LISTVAL invalid", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
122 /* Valid PUTVAL commands. */
124 "PUTVAL magic/MAGIC N:42", &default_host_opts, CMD_OK, CMD_PUTVAL,
127 "PUTVAL myhost/magic/MAGIC N:42", NULL, CMD_OK, CMD_PUTVAL,
130 "PUTVAL myhost/magic/MAGIC 1234:42", NULL, CMD_OK, CMD_PUTVAL,
133 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23", NULL, CMD_OK, CMD_PUTVAL,
136 "PUTVAL myhost/magic/MAGIC interval=2 1234:42", NULL, CMD_OK,
140 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23", NULL,
144 /* Invalid PUTVAL commands. */
146 "PUTVAL magic/MAGIC N:42", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
149 "PUTVAL", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
152 "PUTVAL invalid N:42", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
155 "PUTVAL myhost/magic/MAGIC A:42", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
158 "PUTVAL myhost/magic/MAGIC 1234:A", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
161 "PUTVAL myhost/magic/MAGIC", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
164 "PUTVAL 1234:A", NULL, CMD_PARSE_ERROR, CMD_UNKNOWN,
167 "PUTVAL myhost/magic/UNKNOWN 1234:42", NULL, CMD_PARSE_ERROR,
171 * As of collectd 5.x, PUTVAL accepts invalid options.
173 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
180 /* Invalid commands. */
182 "INVALID", NULL, CMD_UNKNOWN_COMMAND, CMD_UNKNOWN,
185 "INVALID interval=2", NULL, CMD_UNKNOWN_COMMAND, CMD_UNKNOWN,
190 cmd_error_handler_t err = {error_cb, NULL};
193 for (size_t i = 0; i < STATIC_ARRAY_SIZE(parse_data); i++) {
194 char *input = strdup(parse_data[i].input);
196 char description[1024];
202 memset(&cmd, 0, sizeof(cmd));
204 status = cmd_parse(input, &cmd, parse_data[i].opts, &err);
205 ssnprintf(description, sizeof(description), "cmd_parse (\"%s\", opts=%p) = "
206 "%d (type=%d [%s]); want %d "
208 parse_data[i].input, parse_data[i].opts, status, cmd.type,
209 CMD_TO_STRING(cmd.type), parse_data[i].expected_status,
210 parse_data[i].expected_type,
211 CMD_TO_STRING(parse_data[i].expected_type));
212 result = (status == parse_data[i].expected_status) &&
213 (cmd.type == parse_data[i].expected_type);
214 LOG(result, description);
216 /* Run all tests before failing. */
227 int main(int argc, char **argv) {