68b51403305c9e24857f08e13abce891c40f9d25
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 **/
27 #include "common.h"
28 #include "testing.h"
29 #include "utils_cmds.h"
31 static void error_cb (void *ud, cmd_status_t status,
32 const char *format, va_list ap)
33 {
34 if (status == CMD_OK)
35 return;
37 printf ("ERROR[%d]: ", status);
38 vprintf (format, ap);
39 printf ("\n");
40 fflush (stdout);
41 } /* void error_cb */
43 struct {
44 char *input;
45 cmd_status_t expected_status;
46 cmd_type_t expected_type;
47 } parse_data[] = {
48 /* Valid FLUSH commands. */
49 {
50 "FLUSH",
51 CMD_OK,
52 CMD_FLUSH,
53 },
54 {
55 "FLUSH identifier=myhost/magic/MAGIC",
56 CMD_OK,
57 CMD_FLUSH,
58 },
59 {
60 "FLUSH timeout=123 plugin=\"A\"",
61 CMD_OK,
62 CMD_FLUSH,
63 },
64 /* Invalid FLUSH commands. */
65 {
66 /* Missing 'identifier' key. */
67 "FLUSH myhost/magic/MAGIC",
68 CMD_PARSE_ERROR,
69 CMD_UNKNOWN,
70 },
71 {
72 /* Invalid timeout. */
73 "FLUSH timeout=A",
74 CMD_PARSE_ERROR,
75 CMD_UNKNOWN,
76 },
77 {
78 /* Invalid identifier. */
79 "FLUSH identifier=invalid",
80 CMD_PARSE_ERROR,
81 CMD_UNKNOWN,
82 },
83 {
84 /* Invalid option. */
85 "FLUSH invalid=option",
86 CMD_PARSE_ERROR,
87 CMD_UNKNOWN,
88 },
90 /* Valid GETVAL commands. */
91 {
92 "GETVAL myhost/magic/MAGIC",
93 CMD_OK,
94 CMD_GETVAL,
95 },
97 /* Invalid GETVAL commands. */
98 {
99 "GETVAL",
100 CMD_PARSE_ERROR,
101 CMD_UNKNOWN,
102 },
103 {
104 "GETVAL invalid",
105 CMD_PARSE_ERROR,
106 CMD_UNKNOWN,
107 },
109 /* Valid LISTVAL commands. */
110 {
111 "LISTVAL",
112 CMD_OK,
113 CMD_LISTVAL,
114 },
116 /* Invalid LISTVAL commands. */
117 {
118 "LISTVAL invalid",
119 CMD_PARSE_ERROR,
120 CMD_UNKNOWN,
121 },
123 /* Valid PUTVAL commands. */
124 {
125 "PUTVAL myhost/magic/MAGIC N:42",
126 CMD_OK,
127 CMD_PUTVAL,
128 },
129 {
130 "PUTVAL myhost/magic/MAGIC 1234:42",
131 CMD_OK,
132 CMD_PUTVAL,
133 },
134 {
135 "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
136 CMD_OK,
137 CMD_PUTVAL,
138 },
139 {
140 "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
141 CMD_OK,
142 CMD_PUTVAL,
143 },
144 {
145 "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
146 CMD_OK,
147 CMD_PUTVAL,
148 },
150 /* Invalid PUTVAL commands. */
151 {
152 "PUTVAL",
153 CMD_PARSE_ERROR,
154 CMD_UNKNOWN,
155 },
156 {
157 "PUTVAL invalid N:42",
158 CMD_PARSE_ERROR,
159 CMD_UNKNOWN,
160 },
161 {
162 "PUTVAL myhost/magic/MAGIC A:42",
163 CMD_PARSE_ERROR,
164 CMD_UNKNOWN,
165 },
166 {
167 "PUTVAL myhost/magic/MAGIC 1234:A",
168 CMD_PARSE_ERROR,
169 CMD_UNKNOWN,
170 },
171 {
172 "PUTVAL myhost/magic/MAGIC",
173 CMD_PARSE_ERROR,
174 CMD_UNKNOWN,
175 },
176 {
177 "PUTVAL 1234:A",
178 CMD_PARSE_ERROR,
179 CMD_UNKNOWN,
180 },
181 {
182 "PUTVAL myhost/magic/UNKNOWN 1234:42",
183 CMD_PARSE_ERROR,
184 CMD_UNKNOWN,
185 },
186 /*
187 * As of collectd 5.x, PUTVAL accepts invalid options.
188 {
189 "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
190 CMD_PARSE_ERROR,
191 CMD_UNKNOWN,
192 },
193 */
195 /* Invalid commands. */
196 {
197 "INVALID",
198 CMD_UNKNOWN_COMMAND,
199 CMD_UNKNOWN,
200 },
201 {
202 "INVALID interval=2",
203 CMD_UNKNOWN_COMMAND,
204 CMD_UNKNOWN,
205 },
206 };
208 DEF_TEST(parse)
209 {
210 cmd_error_handler_t err = { error_cb, NULL };
211 int test_result = 0;
212 size_t i;
214 for (i = 0; i < STATIC_ARRAY_SIZE (parse_data); i++) {
215 char *input = strdup (parse_data[i].input);
217 char description[1024];
218 cmd_status_t status;
219 cmd_t cmd;
221 _Bool result;
223 memset (&cmd, 0, sizeof (cmd));
225 status = cmd_parse (input, &cmd, &err);
226 snprintf (description, sizeof (description),
227 "cmd_parse (\"%s\") = %d (type=%d [%s]); want %d (type=%d [%s])",
228 parse_data[i].input, status,
229 cmd.type, CMD_TO_STRING (cmd.type),
230 parse_data[i].expected_status,
231 parse_data[i].expected_type,
232 CMD_TO_STRING (parse_data[i].expected_type));
233 result = (status == parse_data[i].expected_status)
234 && (cmd.type == parse_data[i].expected_type);
235 LOG (result, description);
237 /* Run all tests before failing. */
238 if (! result)
239 test_result = -1;
241 cmd_destroy (&cmd);
242 free (input);
243 }
245 return (test_result);
246 }
248 int main (int argc, char **argv)
249 {
250 RUN_TEST(parse);
251 END_TEST;
252 }