Clang-format a few files to make CI happy
[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 // 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     /* Invalid PUTVAL commands. */
209     {
210         "PUTVAL magic/MAGIC N:42",
211         NULL,
212         CMD_PARSE_ERROR,
213         CMD_UNKNOWN,
214     },
215     {
216         "PUTVAL",
217         NULL,
218         CMD_PARSE_ERROR,
219         CMD_UNKNOWN,
220     },
221     {
222         "PUTVAL invalid N:42",
223         NULL,
224         CMD_PARSE_ERROR,
225         CMD_UNKNOWN,
226     },
227     {
228         "PUTVAL myhost/magic/MAGIC A:42",
229         NULL,
230         CMD_PARSE_ERROR,
231         CMD_UNKNOWN,
232     },
233     {
234         "PUTVAL myhost/magic/MAGIC 1234:A",
235         NULL,
236         CMD_PARSE_ERROR,
237         CMD_UNKNOWN,
238     },
239     {
240         "PUTVAL myhost/magic/MAGIC",
241         NULL,
242         CMD_PARSE_ERROR,
243         CMD_UNKNOWN,
244     },
245     {
246         "PUTVAL 1234:A",
247         NULL,
248         CMD_PARSE_ERROR,
249         CMD_UNKNOWN,
250     },
251     {
252         "PUTVAL myhost/magic/UNKNOWN 1234:42",
253         NULL,
254         CMD_PARSE_ERROR,
255         CMD_UNKNOWN,
256     },
257     /*
258      * As of collectd 5.x, PUTVAL accepts invalid options.
259     {
260             "PUTVAL myhost/magic/MAGIC invalid=2 1234:42",
261             NULL,
262             CMD_PARSE_ERROR,
263             CMD_UNKNOWN,
264     },
265     */
266
267     /* Invalid commands. */
268     {
269         "INVALID",
270         NULL,
271         CMD_UNKNOWN_COMMAND,
272         CMD_UNKNOWN,
273     },
274     {
275         "INVALID interval=2",
276         NULL,
277         CMD_UNKNOWN_COMMAND,
278         CMD_UNKNOWN,
279     },
280 };
281
282 DEF_TEST(parse) {
283   cmd_error_handler_t err = {error_cb, NULL};
284   int test_result = 0;
285
286   for (size_t i = 0; i < STATIC_ARRAY_SIZE(parse_data); i++) {
287     char *input = strdup(parse_data[i].input);
288
289     char description[1024];
290     cmd_status_t status;
291     cmd_t cmd;
292
293     bool result;
294
295     memset(&cmd, 0, sizeof(cmd));
296
297     status = cmd_parse(input, &cmd, parse_data[i].opts, &err);
298     ssnprintf(description, sizeof(description),
299               "cmd_parse (\"%s\", opts=%p) = "
300               "%d (type=%d [%s]); want %d "
301               "(type=%d [%s])",
302               parse_data[i].input, parse_data[i].opts, status, cmd.type,
303               CMD_TO_STRING(cmd.type), parse_data[i].expected_status,
304               parse_data[i].expected_type,
305               CMD_TO_STRING(parse_data[i].expected_type));
306     result = (status == parse_data[i].expected_status) &&
307              (cmd.type == parse_data[i].expected_type);
308     LOG(result, description);
309
310     /* Run all tests before failing. */
311     if (!result)
312       test_result = -1;
313
314     cmd_destroy(&cmd);
315     free(input);
316   }
317
318   return test_result;
319 }
320
321 int main(int argc, char **argv) {
322   RUN_TEST(parse);
323   END_TEST;
324 }