libamxrt  0.4.2
Ambiorix Run Time Library
test_amxrt_args.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <setjmp.h>
57 #include <stdarg.h>
58 #include <cmocka.h>
59 
60 #include "test_amxrt_args.h"
61 
62 int test_args_setup(UNUSED void** state) {
63  amxrt_new();
64  return 0;
65 }
66 
67 int test_args_teardown(UNUSED void** state) {
68  amxrt_delete();
69  return 0;
70 }
71 
72 void test_can_print_help(UNUSED void** state) {
73  amxc_var_t* config = amxrt_get_config();
74  char* argv[] = { "amxrt", "-h" };
75 
76  amxc_var_add_key(cstring_t, config, "name", "amxrt");
77  optind = 1;
78  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), -1);
79 }
80 
81 void test_can_print_extended_help(UNUSED void** state) {
82  char* argv[] = { "amxrt", "-H" };
83 
84  optind = 1;
85  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), -1);
86 }
87 
88 void test_can_add_include_dir(UNUSED void** state) {
89  char* argv[] = { "amxrt", "-I", "/tmp/include/", "-I", "/etc/odl/" };
90  amxc_var_t* config = amxrt_get_config();
91  amxc_var_t* option = GET_ARG(config, AMXRT_COPT_INCDIRS);
92  const char* include_dirs[] = {
93  ".", "/tmp/include/", "/etc/odl/"
94  };
95  int index = 0;
96 
97  assert_non_null(option);
98 
99  optind = 1;
100  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
101 
102  amxc_var_for_each(var, option) {
103  assert_string_equal(GET_CHAR(var, NULL), include_dirs[index]);
104  index++;
105  }
106 }
107 
108 void test_can_add_import_dir(UNUSED void** state) {
109  char* argv[] = { "amxrt", "-L", "/tmp/include/", "-L", "/etc/odl/" };
110  amxc_var_t* config = amxrt_get_config();
111  amxc_var_t* option = GET_ARG(config, AMXRT_COPT_LIBDIRS);
112  const char* include_dirs[] = {
113  ".", "/tmp/include/", "/etc/odl/"
114  };
115  int index = 0;
116 
117  assert_non_null(option);
118 
119  optind = 1;
120  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
121 
122  amxc_var_for_each(var, option) {
123  assert_string_equal(GET_CHAR(var, NULL), include_dirs[index]);
124  index++;
125  }
126 }
127 
128 void test_can_add_backend(UNUSED void** state) {
129  char* argv[] = { "amxrt", "-B", "/usr/bin/mods/amx/mod_amxb_test.so", "-B", "/usr/bin/mods/amx/mod_amxb_dummy.so" };
130  amxc_var_t* config = amxrt_get_config();
131  amxc_var_t* option = GET_ARG(config, AMXRT_COPT_BACKENDS);
132  const char* include_dirs[] = {
133  "/usr/bin/mods/amx/mod_amxb_test.so", "/usr/bin/mods/amx/mod_amxb_dummy.so"
134  };
135  int index = 0;
136 
137  assert_non_null(option);
138 
139  optind = 1;
140  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
141 
142  amxc_var_for_each(var, option) {
143  assert_string_equal(GET_CHAR(var, NULL), include_dirs[index]);
144  index++;
145  }
146 }
147 
148 void test_can_add_uri(UNUSED void** state) {
149  char* argv[] = { "amxrt", "-u", "dummy:/var/run/dummy.sock", "-u", "dummy:/var/run/ubus.sock" };
150  amxc_var_t* config = amxrt_get_config();
151  amxc_var_t* option = GET_ARG(config, AMXRT_COPT_URIS);
152  const char* include_dirs[] = {
153  "dummy:/var/run/dummy.sock", "dummy:/var/run/ubus.sock"
154  };
155  int index = 0;
156 
157  assert_non_null(option);
158 
159  optind = 1;
160  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
161 
162  amxc_var_for_each(var, option) {
163  assert_string_equal(GET_CHAR(var, NULL), include_dirs[index]);
164  index++;
165  }
166 }
167 
168 void test_can_accept_option(UNUSED void** state) {
169  char* argv[] = {"amxrt", "-o option=new_option", "-o", "text=hallo"};
170  amxc_var_t* config = amxrt_get_config();
171 
172  assert_null(GET_ARG(config, "option"));
173  assert_null(GET_ARG(config, "text"));
174 
175  optind = 1;
176  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
177 
178  assert_non_null(GET_ARG(config, "option"));
179  assert_non_null(GET_ARG(config, "text"));
180 
181  assert_string_equal(GET_CHAR(config, "option"), "new_option");
182  assert_string_equal(GET_CHAR(config, "text"), "hallo");
183 }
184 
185 void test_can_overwrite_option(UNUSED void** state) {
186  char* argv[] = {"amxrt", "-o cfg-dir=my_cfg_dir", "-o", "daemon=no"};
187  amxc_var_t* config = amxrt_get_config();
188 
189  assert_non_null(GET_ARG(config, AMXRT_COPT_CFG_DIR));
190  assert_non_null(GET_ARG(config, AMXRT_COPT_DAEMON));
191 
192  optind = 1;
193  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
194  assert_string_equal(GET_CHAR(config, AMXRT_COPT_CFG_DIR), "my_cfg_dir");
195  assert_string_equal(GET_CHAR(config, AMXRT_COPT_DAEMON), "no");
196 }
197 
198 void test_ignores_invalid_options(UNUSED void** state) {
199  char* argv1[] = {"amxrt", "-o"};
200  char* argv2[] = {"amxrt", "-o option2"};
201  amxc_var_t* config = amxrt_get_config();
202 
203  optind = 1;
204  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), -1);
205 
206  optind = 1;
207  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv2) / sizeof(argv2[0]), argv2, NULL), sizeof(argv2) / sizeof(argv2[0]));
208  assert_null(GET_CHAR(config, "option2"));
209 }
210 
211 void test_can_disable_auto_detect(UNUSED void** state) {
212  char* argv[] = {"amxrt", "-A"};
213  amxc_var_t* config = amxrt_get_config();
214 
215  assert_non_null(GET_ARG(config, AMXRT_COPT_AUTO_DETECT));
216  assert_true(GET_BOOL(config, AMXRT_COPT_AUTO_DETECT));
217 
218  optind = 1;
219  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
220  assert_false(GET_BOOL(config, AMXRT_COPT_AUTO_DETECT));
221 }
222 
223 void test_can_disable_auto_connect(UNUSED void** state) {
224  char* argv[] = {"amxrt", "-C"};
225  amxc_var_t* config = amxrt_get_config();
226 
227  assert_non_null(GET_ARG(config, AMXRT_COPT_AUTO_CONNECT));
228  assert_true(GET_BOOL(config, AMXRT_COPT_AUTO_CONNECT));
229 
230  optind = 1;
231  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
232  assert_false(GET_BOOL(config, AMXRT_COPT_AUTO_CONNECT));
233 }
234 
235 void test_can_enable_daemonize(UNUSED void** state) {
236  char* argv[] = {"amxrt", "-D"};
237  amxc_var_t* config = amxrt_get_config();
238 
239  assert_non_null(GET_ARG(config, AMXRT_COPT_DAEMON));
240  assert_false(GET_BOOL(config, AMXRT_COPT_DAEMON));
241 
242  optind = 1;
243  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
244  assert_true(GET_BOOL(config, AMXRT_COPT_DAEMON));
245 }
246 
247 void test_can_enable_eventing(UNUSED void** state) {
248  char* argv[] = {"amxrt", "-E"};
249  amxc_var_t* config = amxrt_get_config();
250 
251  assert_non_null(GET_ARG(config, AMXRT_COPT_EVENT));
252  assert_false(GET_BOOL(config, AMXRT_COPT_EVENT));
253 
254  optind = 1;
255  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
256  assert_true(GET_BOOL(config, AMXRT_COPT_EVENT));
257 }
258 
259 void test_can_enable_dump_config(UNUSED void** state) {
260  char* argv[] = {"amxrt", "-d"};
261  amxc_var_t* config = amxrt_get_config();
262 
263  assert_non_null(GET_ARG(config, AMXRT_COPT_DUMP_CONFIG));
264  assert_false(GET_BOOL(config, AMXRT_COPT_DUMP_CONFIG));
265 
266  optind = 1;
267  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
268  assert_true(GET_BOOL(config, AMXRT_COPT_DUMP_CONFIG));
269 }
270 
271 void test_can_set_nice_level(UNUSED void** state) {
272  char* argv[] = {"amxrt", "-p", "-5"};
273  amxc_var_t* config = amxrt_get_config();
274 
275  assert_non_null(GET_ARG(config, AMXRT_COPT_PRIORITY));
276  assert_int_equal(GET_INT32(config, AMXRT_COPT_PRIORITY), 0);
277 
278  optind = 1;
279  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
280  assert_int_equal(GET_INT32(config, AMXRT_COPT_PRIORITY), -5);
281 }
282 
283 void test_can_disable_pid_file_creation(UNUSED void** state) {
284  char* argv[] = {"amxrt", "-N"};
285  amxc_var_t* config = amxrt_get_config();
286 
287  assert_non_null(GET_ARG(config, AMXRT_COPT_PID_FILE));
288  assert_true(GET_BOOL(config, AMXRT_COPT_PID_FILE));
289 
290  optind = 1;
291  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
292  assert_false(GET_BOOL(config, AMXRT_COPT_PID_FILE));
293 }
294 
295 void test_can_pass_odl_string(UNUSED void** state) {
296  char* argv[] = {"amxrt", "-O", "%%config { test = 123; }"};
297  amxc_var_t* config = amxrt_get_config();
298 
299  assert_null(GET_ARG(config, AMXRT_COPT_ODL));
300 
301  optind = 1;
302  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
303  assert_non_null(GET_ARG(config, AMXRT_COPT_ODL));
304  assert_string_equal(GET_CHAR(config, AMXRT_COPT_ODL), "%%config { test = 123; }");
305 }
306 
307 void test_can_enable_syslog(UNUSED void** state) {
308  char* argv[] = {"amxrt", "-l"};
309  amxc_var_t* config = amxrt_get_config();
310 
311  assert_non_null(GET_ARG(config, AMXRT_COPT_LOG));
312  assert_false(GET_BOOL(config, AMXRT_COPT_LOG));
313 
314  optind = 1;
315  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
316  assert_true(GET_BOOL(config, AMXRT_COPT_LOG));
317 }
318 
319 void test_can_add_required_objects(UNUSED void** state) {
320  char* argv[] = { "amxrt", "-R", "NetModel.", "-R", "NetDev." };
321  amxc_var_t* config = amxrt_get_config();
322  amxc_var_t* option = GET_ARG(config, AMXRT_COPT_REQUIRES);
323  const char* include_dirs[] = {
324  "NetModel.", "NetDev."
325  };
326  int index = 0;
327 
328  assert_non_null(option);
329 
330  optind = 1;
331  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv) / sizeof(argv[0]), argv, NULL), sizeof(argv) / sizeof(argv[0]));
332 
333  amxc_var_for_each(var, option) {
334  assert_string_equal(GET_CHAR(var, NULL), include_dirs[index]);
335  index++;
336  }
337 
338 }
339 
340 void test_can_add_forced_option(UNUSED void** state) {
341 
342 }
343 
344 static int test_args(UNUSED amxc_var_t* config,
345  int arg_id,
346  UNUSED const char* value) {
347  int rv = 0;
348  if(arg_id != 'z') {
349  rv = -2;
350  }
351  return rv;
352 }
353 
354 void test_can_add_custom_options(UNUSED void** state) {
355  char* argv1[] = {"amxrt", "-z"};
356 
357  amxrt_cmd_line_add_option(0, 'z', "zulu", no_argument, "testing", NULL);
358 
359  optind = 1;
360  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv1) / sizeof(argv1[0]), argv1, test_args), sizeof(argv1) / sizeof(argv1[0]));
361 }
362 
363 
364 void test_cmd_line_parse_fails_after_reset(UNUSED void** state) {
365  char* argv1[] = {"amxrt", "-z"};
366  char* argv2[] = {"amxrt", "-z", "-t"};
367 
369  amxrt_cmd_line_add_option(0, 'z', "zulu", no_argument, "testing", NULL);
370 
371  optind = 1;
372  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), -1);
373 
374  optind = 1;
375  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv1) / sizeof(argv1[0]), argv1, test_args), sizeof(argv1) / sizeof(argv1[0]));
376 
377  optind = 1;
378  assert_int_equal(amxrt_cmd_line_parse(sizeof(argv2) / sizeof(argv2[0]), argv2, test_args), -1);
379 }
380 
381 void test_can_set_usage_doc(UNUSED void** state) {
382  amxrt_cmd_line_set_usage_doc("Test usage");
386 }
#define AMXRT_COPT_BACKENDS
Definition: amxrt.h:75
#define AMXRT_COPT_URIS
Definition: amxrt.h:73
#define AMXRT_COPT_PID_FILE
Definition: amxrt.h:84
#define AMXRT_COPT_ODL
Definition: amxrt.h:81
#define AMXRT_COPT_LIBDIRS
Definition: amxrt.h:79
#define AMXRT_COPT_CFG_DIR
Definition: amxrt.h:88
#define AMXRT_COPT_DUMP_CONFIG
Definition: amxrt.h:91
#define AMXRT_COPT_PRIORITY
Definition: amxrt.h:83
#define AMXRT_COPT_REQUIRES
Definition: amxrt.h:99
#define AMXRT_COPT_EVENT
Definition: amxrt.h:90
#define AMXRT_COPT_AUTO_DETECT
Definition: amxrt.h:76
#define AMXRT_COPT_LOG
Definition: amxrt.h:98
#define AMXRT_COPT_AUTO_CONNECT
Definition: amxrt.h:77
#define AMXRT_COPT_DAEMON
Definition: amxrt.h:82
#define AMXRT_COPT_INCDIRS
Definition: amxrt.h:78
int amxrt_cmd_line_add_option(int id, char short_option, const char *long_option, int has_args, const char *doc, const char *arg_doc)
Adds a command line option definition.
Definition: amxrt_args.c:206
int amxrt_cmd_line_parse(int argc, char *argv[], amxrt_arg_fn_t fn)
Starts parsing the command line options.
Definition: amxrt_args.c:236
void amxrt_cmd_line_set_usage_doc(const char *usage)
Set the overall usage documentation string.
Definition: amxrt_args.c:230
void amxrt_cmd_line_options_reset(void)
Removes all default options.
Definition: amxrt_args.c:200
void amxrt_print_usage(void)
Prints the usage information and all available options to stdout.
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301
void amxrt_delete(void)
Clean-up ambiorix runtime.
Definition: amxrt.c:378
void amxrt_new(void)
Create the ambiorix runtime.
Definition: amxrt.c:313
void test_can_accept_option(UNUSED void **state)
void test_can_add_import_dir(UNUSED void **state)
void test_can_set_nice_level(UNUSED void **state)
void test_can_enable_syslog(UNUSED void **state)
void test_can_add_forced_option(UNUSED void **state)
void test_can_pass_odl_string(UNUSED void **state)
void test_can_set_usage_doc(UNUSED void **state)
void test_can_add_required_objects(UNUSED void **state)
int test_args_teardown(UNUSED void **state)
void test_can_disable_auto_connect(UNUSED void **state)
void test_can_overwrite_option(UNUSED void **state)
void test_can_print_help(UNUSED void **state)
static int test_args(UNUSED amxc_var_t *config, int arg_id, UNUSED const char *value)
void test_can_add_uri(UNUSED void **state)
void test_cmd_line_parse_fails_after_reset(UNUSED void **state)
void test_can_disable_auto_detect(UNUSED void **state)
void test_can_enable_dump_config(UNUSED void **state)
void test_can_add_include_dir(UNUSED void **state)
void test_can_add_backend(UNUSED void **state)
void test_can_print_extended_help(UNUSED void **state)
void test_can_disable_pid_file_creation(UNUSED void **state)
void test_can_enable_daemonize(UNUSED void **state)
void test_ignores_invalid_options(UNUSED void **state)
int test_args_setup(UNUSED void **state)
void test_can_add_custom_options(UNUSED void **state)
void test_can_enable_eventing(UNUSED void **state)
config
Definition: test.odl:54