Run all changed files 68 8.c/1 *.h through clang-format agin..
[collectd.git] / src / utils / cmds / cmds_test.c
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 #include "testing.h"
28 #include "utils/cmds/cmds.h"
29 #include "utils/common/common.h"
30
31 static void error_cb(void *ud, cmd_status_t status, const char *format,
32                      va_list ap) {
33   if (status == CMD_OK)
34     return;
35
36   printf("ERROR[%d]: ", status);
37   vprintf(format, ap);
38   printf("\n");
39   fflush(stdout);
40 } /* void error_cb */
41
42 static cmd_options_t default_host_opts = {
43     /* identifier_default_host = */ "dummy-host",
44 };
45
46 static struct {
47   char *input;
48   cmd_options_t *opts;
49   cmd_status_t expected_status;
50   cmd_type_t expected_type;
51 } parse_data[] = {
52     /* Valid FLUSH commands. */
53     {
54         "FLUSH",
55         NULL,
56         CMD_OK,
57         CMD_FLUSH,
58     },
59     {
60         "FLUSH identifier=myhost/magic/MAGIC",
61         NULL,
62         CMD_OK,
63         CMD_FLUSH,
64     },
65     {
66         "FLUSH identifier=magic/MAGIC",
67         &default_host_opts,
68         CMD_OK,
69         CMD_FLUSH,
70     },
71     {
72         "FLUSH timeout=123 plugin=\"A\"",
73         NULL,
74         CMD_OK,
75         CMD_FLUSH,
76     },
77     /* Invalid FLUSH commands. */
78     {
79         /* Missing hostname; no default. */
80         "FLUSH identifier=magic/MAGIC",
81         NULL,
82         CMD_PARSE_ERROR,
83         CMD_UNKNOWN,
84     },
85     {
86         /* Missing 'identifier' key. */
87         "FLUSH myhost/magic/MAGIC",
88         NULL,
89         CMD_PARSE_ERROR,
90         CMD_UNKNOWN,
91     },
92     {
93         /* Invalid timeout. */
94         "FLUSH timeout=A",
95         NULL,
96         CMD_PARSE_ERROR,
97         CMD_UNKNOWN,
98     },
99     {
100         /* Invalid identifier. */
101         "FLUSH identifier=invalid",
102         NULL,
103         CMD_PARSE_ERROR,
104         CMD_UNKNOWN,
105     },
106     {
107         /* Invalid option. */
108         "FLUSH invalid=option",
109         NULL,
110         CMD_PARSE_ERROR,
111         CMD_UNKNOWN,
112     },
113
114     /* Valid GETVAL commands. */
115     {
116         "GETVAL myhost/magic/MAGIC",
117         NULL,
118         CMD_OK,
119         CMD_GETVAL,
120     },
121     {
122         "GETVAL magic/MAGIC",
123         &default_host_opts,
124         CMD_OK,
125         CMD_GETVAL,
126     },
127
128     /* Invalid GETVAL commands. */
129     {
130         "GETVAL magic/MAGIC",
131         NULL,
132         CMD_PARSE_ERROR,
133         CMD_UNKNOWN,
134     },
135     {
136         "GETVAL",
137         NULL,
138         CMD_PARSE_ERROR,
139         CMD_UNKNOWN,
140     },
141     {
142         "GETVAL invalid",
143         NULL,
144         CMD_PARSE_ERROR,
145         CMD_UNKNOWN,
146     },
147
148     /* Valid LISTVAL commands. */
149     {
150         "LISTVAL",
151         NULL,
152         CMD_OK,
153         CMD_LISTVAL,
154     },
155
156     /* Invalid LISTVAL commands. */
157     {
158         "LISTVAL invalid",
159         NULL,
160         CMD_PARSE_ERROR,
161         CMD_UNKNOWN,
162     },
163
164     /* Valid PUTVAL commands. */
165     {
166         "PUTVAL magic/MAGIC N:42",
167         &default_host_opts,
168         CMD_OK,
169         CMD_PUTVAL,
170     },
171     {
172         "PUTVAL myhost/magic/MAGIC N:42",
173         NULL,
174         CMD_OK,
175         CMD_PUTVAL,
176     },
177     {
178         "PUTVAL myhost/magic/MAGIC 1234:42",
179         NULL,
180         CMD_OK,
181         CMD_PUTVAL,
182     },
183     {
184         "PUTVAL myhost/magic/MAGIC 1234:42 2345:23",
185         NULL,
186         CMD_OK,
187         CMD_PUTVAL,
188     },
189     {
190         "PUTVAL myhost/magic/MAGIC interval=2 1234:42",
191         NULL,
192         CMD_OK,
193         CMD_PUTVAL,
194     },
195     {
196         "PUTVAL myhost/magic/MAGIC interval=2 1234:42 interval=5 2345:23",
197         NULL,
198         CMD_OK,
199         CMD_PUTVAL,
200     },
201
202     /* Invalid PUTVAL commands. */
203     {
204         "PUTVAL magic/MAGIC N:42",
205         NULL,
206         CMD_PARSE_ERROR,
207         CMD_UNKNOWN,
208     },
209     {
210         "PUTVAL",
211         NULL,
212         CMD_PARSE_ERROR,
213         CMD_UNKNOWN,
214     },
215     {
216         "PUTVAL invalid N:42",
217         NULL,
218         CMD_PARSE_ERROR,
219         CMD_UNKNOWN,
220     },
221     {
222         "PUTVAL myhost/magic/MAGIC A:42",
223         NULL,
224         CMD_PARSE_ERROR,
225         CMD_UNKNOWN,
226     },
227     {
228         "PUTVAL myhost/magic/MAGIC 1234:A",
229         NULL,
230         CMD_PARSE_ERROR,
231         CMD_UNKNOWN,
232     },
233     {
234         "PUTVAL myhost/magic/MAGIC",
235         NULL,
236         CMD_PARSE_ERROR,
237         CMD_UNKNOWN,
238     },
239     {
240         "PUTVAL 1234:A",
241         NULL,
242         CMD_PARSE_ERROR,
243         CMD_UNKNOWN,
244     },
245     {
246         "PUTVAL myhost/magic/UNKNOWN 1234:42",
247         NULL,
248         CMD_PARSE_ERROR,
249         CMD_UNKNOWN,
250     },
251     /*
252      * As of collectd 5.x, PUTVAL accepts invalid options.
253     {
254             "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
255             NULL,
256             CMD_PARSE_ERROR,
257             CMD_UNKNOWN,
258     },
259     */
260
261     /* Invalid commands. */
262     {
263         "INVALID",
264         NULL,
265         CMD_UNKNOWN_COMMAND,
266         CMD_UNKNOWN,
267     },
268     {
269         "INVALID interval=2",
270         NULL,
271         CMD_UNKNOWN_COMMAND,
272         CMD_UNKNOWN,
273     },
274 };
275
276 DEF_TEST(parse) {
277   cmd_error_handler_t err = {error_cb, NULL};
278   int test_result = 0;
279
280   for (size_t i = 0; i < STATIC_ARRAY_SIZE(parse_data); i++) {
281     char *input = strdup(parse_data[i].input);
282
283     char description[1024];
284     cmd_status_t status;
285     cmd_t cmd;
286
287     bool result;
288
289     memset(&cmd, 0, sizeof(cmd));
290
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 "
295               "(type=%d [%s])",
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);
303
304     /* Run all tests before failing. */
305     if (!result)
306       test_result = -1;
307
308     cmd_destroy(&cmd);
309     free(input);
310   }
311
312   return test_result;
313 }
314
315 int main(int argc, char **argv) {
316   RUN_TEST(parse);
317   END_TEST;
318 }