libamxrt  0.4.2
Ambiorix Run Time Library
test_amxrt_runtime.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 #include <string.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63 
64 #include "test_amxrt_runtime.h"
65 
66 typedef struct dummy_bus {
67  int data_pipe[2];
68  amxb_bus_ctx_t ctx;
70 
72 
73 int __wrap_amxb_be_load_multiple(amxc_var_t* const bes);
74 int __wrap_amxb_connect(amxb_bus_ctx_t** ctx, const char* uri);
75 int __wrap_amxb_listen(amxb_bus_ctx_t** ctx, const char* uri);
76 int __wrap_amxb_get_fd(amxb_bus_ctx_t* ctx);
77 void __wrap_amxb_free(amxb_bus_ctx_t** ctx);
78 int __wrap_amxb_register(amxb_bus_ctx_t* ctx, amxd_dm_t* dm);
79 int __wrap_amxb_read(amxb_bus_ctx_t* ctx);
80 int __wrap_amxb_wait_for_object(const char* object);
81 int __wrap_daemon(int nochdir, int noclose);
82 int __wrap_setpriority(int which, id_t who, int value);
83 
84 int __wrap_amxb_be_load_multiple(UNUSED amxc_var_t* const bes) {
85  return (int) mock();
86 }
87 
88 int __wrap_amxb_connect(amxb_bus_ctx_t** ctx, UNUSED const char* uri) {
89  *ctx = &dummy_ctx.ctx;
90  return (int) mock();
91 }
92 
93 int __wrap_amxb_listen(amxb_bus_ctx_t** ctx, UNUSED const char* uri) {
94  *ctx = &dummy_ctx.ctx;
95  return (int) mock();
96 }
97 
98 int __wrap_amxb_get_fd(UNUSED amxb_bus_ctx_t* ctx) {
99  return dummy_ctx.data_pipe[0];
100 }
101 
102 void __wrap_amxb_free(amxb_bus_ctx_t** ctx) {
103  *ctx = NULL;
104 }
105 
106 int __wrap_amxb_register(UNUSED amxb_bus_ctx_t* ctx, UNUSED amxd_dm_t* dm) {
107  return (int) mock();
108 }
109 
110 int __wrap_amxb_read(UNUSED amxb_bus_ctx_t* ctx) {
111  char buf[10];
112  read(dummy_ctx.data_pipe[0], buf, 1);
113  amxrt_el_stop();
114  return 0;
115 }
116 
117 int __wrap_amxb_wait_for_object(UNUSED const char* object) {
118  return (int) mock();
119 }
120 
121 int __wrap_daemon(UNUSED int nochdir, UNUSED int noclose) {
122  return (int) mock();
123 }
124 
125 int __wrap_setpriority(UNUSED int which, UNUSED id_t who, UNUSED int value) {
126  return (int) mock();
127 }
128 
129 int test_runtime_setup(UNUSED void** state) {
130  amxd_dm_t* dm = NULL;
131  amxrt_new();
132  dm = amxrt_get_dm();
133  memset(&dummy_ctx.ctx, 0, sizeof(amxb_bus_ctx_t));
134  amxp_sigmngr_init(&dummy_ctx.ctx.sigmngr);
135  amxc_htable_init(&dummy_ctx.ctx.subscriptions, 5);
136  amxd_dm_clean(dm);
137  assert_int_equal(pipe(dummy_ctx.data_pipe), 0);
138  return 0;
139 }
140 
141 int test_runtime_teardown(UNUSED void** state) {
142  close(dummy_ctx.data_pipe[0]);
143  close(dummy_ctx.data_pipe[1]);
144  amxp_sigmngr_clean(&dummy_ctx.ctx.sigmngr);
145  amxc_htable_clean(&dummy_ctx.ctx.subscriptions, NULL);
146  amxrt_delete();
147  return 0;
148 }
149 
150 void test_runtime_start_prints_help_and_exits(UNUSED void** state) {
151  char* argv[] = {"amxrt", "-h"};
152 
153  optind = 1;
154  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
155 }
156 
157 void test_runtime_parses_default_odl(UNUSED void** state) {
158  char* argv1[] = {"amxrt", "-B dymmy", "-u", "dummy://127.0.0.1:1970"};
159  char* argv2[] = {"test-plugin", "-B fake", "-u", "dummy://127.0.0.1:1970"};
160 
161  amxd_dm_t* dm = amxrt_get_dm();
162  amxc_var_t* config = amxrt_get_config();
163  amxc_var_t* prefix = GET_ARG(config, "prefix");
164 
165  char* current_wd = getcwd(NULL, 0);
166 
167  will_return_always(__wrap_amxb_be_load_multiple, 0);
168  will_return_always(__wrap_setpriority, 0);
169  will_return_always(__wrap_amxb_connect, 0);
170  will_return_always(__wrap_amxb_register, 0);
171 
172  amxc_var_set(cstring_t, prefix, current_wd);
173 
174  amxd_dm_init(dm);
175  optind = 1;
176  write(dummy_ctx.data_pipe[1], "s", 1);
177  assert_int_equal(amxrt(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), 0);
178  amxd_dm_clean(dm);
179 
180  amxd_dm_init(dm);
181  optind = 1;
182  write(dummy_ctx.data_pipe[1], "s", 1);
183  amxc_var_set(cstring_t, prefix, current_wd);
184  assert_int_equal(amxrt(sizeof(argv2) / sizeof(argv2[0]), argv2, NULL), 0);
185  amxd_dm_clean(dm);
186 
187  amxc_var_set(cstring_t, prefix, "");
188 
189  free(current_wd);
190 }
191 
192 void test_runtime_parses_odls(UNUSED void** state) {
193  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970",
194  "test.odl", "etc/amx/amxrt/amxrt.odl", "etc/amx/test-plugin/test-plugin.odl"};
195 
196  amxd_dm_t* dm = amxrt_get_dm();
197 
198  will_return(__wrap_amxb_be_load_multiple, 0);
199  will_return_always(__wrap_amxb_connect, 0);
200  will_return_always(__wrap_setpriority, 0);
201  will_return_always(__wrap_amxb_register, 0);
202 
203  amxd_dm_init(dm);
204  optind = 1;
205  write(dummy_ctx.data_pipe[1], "s", 1);
206  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
207 
208  amxd_dm_clean(dm);
209 }
210 
212  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970",
213  "test.odl", "-E", "etc/amx/amxrt/amxrt.odl", "etc/amx/test-plugin/test-plugin.odl"};
214 
215  amxd_dm_t* dm = amxrt_get_dm();
216  amxc_var_t* config = amxrt_get_config();
217 
218  will_return(__wrap_amxb_be_load_multiple, 0);
219  will_return_always(__wrap_amxb_connect, 0);
220  will_return_always(__wrap_setpriority, 0);
221  will_return_always(__wrap_amxb_register, 0);
222 
223  amxd_dm_init(dm);
224  optind = 1;
225  write(dummy_ctx.data_pipe[1], "s", 1);
226  amxc_var_add_key(bool, config, AMXRT_COPT_HANDLE_EVENTS, true);
227  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
228  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_HANDLE_EVENTS), false);
229 
230  amxd_dm_clean(dm);
231 }
232 
233 void test_runtime_succeeds_when_no_odl(UNUSED void** state) {
234  char* argv1[] = {"amxrt", "-B a", "-u", "dummy://127.0.0.1:1970"};
235 
236  amxd_dm_t* dm = amxrt_get_dm();
237 
238  will_return(__wrap_amxb_be_load_multiple, 0);
239  will_return_always(__wrap_amxb_connect, 0);
240  will_return_always(__wrap_setpriority, 0);
241  will_return_always(__wrap_amxb_register, 0);
242 
243  amxd_dm_init(dm);
244  optind = 1;
245  write(dummy_ctx.data_pipe[1], "s", 1);
246  assert_int_equal(amxrt(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), 0);
247 
248  amxd_dm_clean(dm);
249 }
250 
251 void test_runtime_fails_when_invalid_odl(UNUSED void** state) {
252  char* argv1[] = {"amxrt", "-B a", "-u", "dummy://127.0.0.1:1970", "invalid.odl"};
253 
254  amxd_dm_t* dm = amxrt_get_dm();
255 
256  amxd_dm_init(dm);
257  optind = 1;
258  write(dummy_ctx.data_pipe[1], "s", 1);
259  assert_int_not_equal(amxrt(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), 0);
260 
261  amxd_dm_clean(dm);
262 }
263 
264 void test_runtime_connects_to_uri(UNUSED void** state) {
265  char* argv1[] = {"amxrt", "-B a", "-u", "dummy://127.0.0.1:1970", "test.odl"};
266 
267  amxd_dm_t* dm = amxrt_get_dm();
268 
269  amxd_dm_init(dm);
270  optind = 1;
271  write(dummy_ctx.data_pipe[1], "s", 1);
272  will_return(__wrap_amxb_be_load_multiple, 0);
273  will_return_always(__wrap_amxb_connect, 0);
274  will_return(__wrap_setpriority, 0);
275  will_return_always(__wrap_amxb_register, 0);
276 
277  assert_int_equal(amxrt(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), 0);
278 
279  amxd_dm_clean(dm);
280 }
281 
282 void test_runtime_cannot_load_backends(UNUSED void** state) {
283  char* argv[] = {"amxrt", "-B b", "test.odl"};
284 
285  amxd_dm_t* dm = amxrt_get_dm();
286 
287  amxd_dm_init(dm);
288  optind = 1;
289  write(dummy_ctx.data_pipe[1], "s", 1);
290  will_return(__wrap_amxb_be_load_multiple, -1);
291  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
292 
293  amxd_dm_clean(dm);
294 }
295 
296 void test_runtime_cannot_connect_bus_uri(UNUSED void** state) {
297  char* argv[] = {"amxrt", "-B b", "test.odl", "-u", "dummy://127.0.0.1:1970"};
298 
299  amxd_dm_t* dm = amxrt_get_dm();
300 
301  amxd_dm_init(dm);
302  optind = 1;
303  write(dummy_ctx.data_pipe[1], "s", 1);
304  will_return(__wrap_amxb_be_load_multiple, 0);
305  will_return_always(__wrap_amxb_connect, -1);
306  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
307 
308  amxd_dm_clean(dm);
309 }
310 
311 void test_runtime_creates_pid_file(UNUSED void** state) {
312  char* argv1[] = {"dummy_name", "-B a", "test.odl",
313  "-u", "dummy://127.0.0.1:1970"};
314  struct stat sb;
315  FILE* fp;
316  char* line = NULL;
317  size_t len = 0;
318  ssize_t read = 0;
319 
320  amxd_dm_t* dm = amxrt_get_dm();
321  amxc_var_t* config = amxrt_get_config();
322  amxc_var_t* name = GET_ARG(config, "name");
323  amxc_var_delete(&name);
324 
325  optind = 1;
326  system("sudo rm -rf /var/run/dummy_name");
327  system("sudo mkdir -p /var/run/dummy_name");
328  system("sudo chown $USER /var/run/dummy_name");
329  system("sudo chmod 777 /var/run/dummy_name");
330  system("ls -la /var/run/dummy_name");
331  system("echo $USER");
332 
333  will_return(__wrap_amxb_be_load_multiple, 0);
334  will_return(__wrap_setpriority, 0);
335  will_return_always(__wrap_amxb_connect, 0);
336 
337  amxd_dm_init(dm);
338  assert_int_equal(amxrt_init(sizeof(argv1) / sizeof(argv1[0]), argv1, NULL), 0);
339  assert_true(stat("/var/run/dummy_name.pid", &sb) == 0 ||
340  stat("/var/run/dummy_name/dummy_name.pid", &sb) == 0);
341 
342  if(stat("/var/run/dummy_name.pid", &sb) == 0) {
343  fp = fopen("/var/run/dummy_name.pid", "r");
344  } else {
345  fp = fopen("/var/run/dummy_name/dummy_name.pid", "r");
346  }
347  read = getline(&line, &len, fp);
348  assert_true(read > 0);
349  assert_int_equal(atol(line), getpid());
350  fclose(fp);
351  free(line);
352 
353  system("sudo rm -rf /var/run/dummy_name");
354  amxrt_stop();
355 
356  amxd_dm_clean(dm);
357 }
358 
359 void test_runtime_daemon_failed(UNUSED void** state) {
360  char* argv[] = {"amxrt", "-B b", "test.odl", "-D", "-u", "dummy://127.0.0.1:1970"};
361 
362  amxc_var_t* config = amxrt_get_config();
363  amxd_dm_t* dm = amxrt_get_dm();
364 
365  amxd_dm_init(dm);
366  optind = 1;
367  write(dummy_ctx.data_pipe[1], "s", 1);
368  will_return(__wrap_amxb_be_load_multiple, 0);
369  will_return(__wrap_daemon, -1);
370  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
371 
372  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_DAEMON), false);
373  amxd_dm_clean(dm);
374 }
375 
376 void test_runtime_set_priority_failed(UNUSED void** state) {
377  char* argv[] = {"amxrt", "-B b", "-D", "-u", "dummy://127.0.0.1:1970", "test.odl"};
378 
379  amxc_var_t* config = amxrt_get_config();
380  amxd_dm_t* dm = amxrt_get_dm();
381 
382  amxd_dm_init(dm);
383  optind = 1;
384  will_return(__wrap_amxb_be_load_multiple, 0);
385  will_return(__wrap_daemon, 0);
386  will_return(__wrap_setpriority, -1);
387  will_return_always(__wrap_amxb_connect, 0);
388  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
389 
390  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_DAEMON), false);
391  amxd_dm_clean(dm);
392 }
393 
394 void test_runtime_parse_args_odl(UNUSED void** state) {
395  char* argv[] = {"amxrt", "-B b", "test.odl", "-O", "%define {object TestObject2 {string Text = \"Hallo World\";}}",
396  "-u", "dummy://127.0.0.1:1970"};
397 
398  amxc_var_t* config = amxrt_get_config();
399  amxd_dm_t* dm = amxrt_get_dm();
400  amxc_var_t* odl_string = NULL;
401 
402  amxd_dm_init(dm);
403  optind = 1;
404  write(dummy_ctx.data_pipe[1], "s", 1);
405  will_return_always(__wrap_amxb_connect, 0);
406  will_return(__wrap_amxb_be_load_multiple, 0);
407  will_return(__wrap_setpriority, 0);
408  will_return_always(__wrap_amxb_register, 0);
409  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
410 
411  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
412  amxc_var_delete(&odl_string);
413  amxd_dm_clean(dm);
414 }
415 
416 void test_runtime_parse_args_wrong_odl(UNUSED void** state) {
417  char* argv[] = {"amxrt", "-B b", "test.odl", "-O", "%define {object TestObject2 {string Text = \"Hallo World\";}"}; //invalid odl string
418  amxc_var_t* config = amxrt_get_config();
419  amxd_dm_t* dm = amxrt_get_dm();
420  amxc_var_t* odl_string = NULL;
421 
422  amxd_dm_init(dm);
423  optind = 1;
424  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
425 
426  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
427  amxc_var_delete(&odl_string);
428  amxd_dm_clean(dm);
429 }
430 
431 void test_runtime_no_pidfile(UNUSED void** state) {
432  char* argv[] = {"amxrt", "-B b", "test.odl", "-N", "-u", "dummy://127.0.0.1:1970"};
433 
434  amxd_dm_t* dm = amxrt_get_dm();
435 
436  amxd_dm_init(dm);
437  optind = 1;
438  write(dummy_ctx.data_pipe[1], "s", 1);
439  will_return_always(__wrap_amxb_connect, 0);
440  will_return(__wrap_amxb_be_load_multiple, 0);
441  will_return(__wrap_setpriority, 0);
442  will_return_always(__wrap_amxb_register, 0);
443  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
444 
445  amxd_dm_clean(dm);
446 }
447 
448 void test_runtime_parses_post_includes(UNUSED void** state) {
449  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "post_include.odl" };
450 
451  amxd_dm_t* dm = amxrt_get_dm();
452 
453  amxd_dm_init(dm);
454  optind = 1;
455  write(dummy_ctx.data_pipe[1], "s", 1);
456  will_return(__wrap_amxb_be_load_multiple, 0);
457  will_return_always(__wrap_amxb_connect, 0);
458  will_return(__wrap_setpriority, 0);
459  will_return_always(__wrap_amxb_register, 0);
460  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
461 
462  amxd_dm_clean(dm);
463 }
464 
465 void test_runtime_opens_syslog(UNUSED void** state) {
466  char* argv[] = {"amxrt", "-l", "test.odl", "-B b", "-u", "dummy://127.0.0.1:1970" };
467 
468  amxd_dm_t* dm = amxrt_get_dm();
469  amxc_var_t* config = amxrt_get_config();
470 
471  amxd_dm_init(dm);
472  optind = 1;
473  write(dummy_ctx.data_pipe[1], "s", 1);
474  will_return(__wrap_amxb_be_load_multiple, 0);
475  will_return_always(__wrap_amxb_connect, 0);
476  will_return(__wrap_setpriority, 0);
477  will_return_always(__wrap_amxb_register, 0);
478  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
479  assert_true(GET_BOOL(config, AMXRT_COPT_LOG));
480  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_LOG), false);
481 
482  amxd_dm_clean(dm);
483 }
484 
485 void test_runtime_dumps_config(UNUSED void** state) {
486  char* argv[] = {"amxrt", "-d", "test.odl", "-B b", "-u", "dummy://127.0.0.1:1970" };
487 
488  amxd_dm_t* dm = amxrt_get_dm();
489  amxc_var_t* config = amxrt_get_config();
490 
491  amxd_dm_init(dm);
492  optind = 1;
493  write(dummy_ctx.data_pipe[1], "s", 1);
494  will_return(__wrap_amxb_be_load_multiple, 0);
495  will_return_always(__wrap_amxb_connect, 0);
496  will_return(__wrap_setpriority, 0);
497  will_return_always(__wrap_amxb_register, 0);
498  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
499  assert_true(GET_BOOL(config, AMXRT_COPT_DUMP_CONFIG));
500  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_DUMP_CONFIG), false);
501 
502  amxd_dm_clean(dm);
503 }
504 
505 void test_runtime_enables_system_signals_list(UNUSED void** state) {
506  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-O", "%config { system-signals = [ 17 ]; }", "./test.odl" };
507 
508  amxd_dm_t* dm = amxrt_get_dm();
509  amxc_var_t* config = amxrt_get_config();
510  amxc_var_t* odl_string = NULL;
511 
512  amxd_dm_init(dm);
513  optind = 1;
514  write(dummy_ctx.data_pipe[1], "s", 1);
515  will_return(__wrap_amxb_be_load_multiple, 0);
516  will_return_always(__wrap_amxb_connect, 0);
517  will_return(__wrap_setpriority, 0);
518  will_return_always(__wrap_amxb_register, 0);
519  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
520  assert_true(amxp_syssig_is_enabled(17));
521  amxp_syssig_enable(17, false);
522 
523  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
524  amxc_var_delete(&odl_string);
525  amxd_dm_clean(dm);
526 }
527 
528 void test_runtime_enables_system_signal(UNUSED void** state) {
529  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-O", "%config { system-signals = 17; }", "./test.odl" };
530 
531  amxd_dm_t* dm = amxrt_get_dm();
532  amxc_var_t* config = amxrt_get_config();
533  amxc_var_t* odl_string = NULL;
534 
535  amxd_dm_init(dm);
536  optind = 1;
537  write(dummy_ctx.data_pipe[1], "s", 1);
538  will_return(__wrap_amxb_be_load_multiple, 0);
539  will_return_always(__wrap_amxb_connect, 0);
540  will_return(__wrap_setpriority, 0);
541  will_return_always(__wrap_amxb_register, 0);
542  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
543  assert_true(amxp_syssig_is_enabled(17));
544  amxp_syssig_enable(17, false);
545 
546  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
547  amxc_var_delete(&odl_string);
548  amxd_dm_clean(dm);
549 }
550 
551 void test_runtime_forced_options_are_kept(UNUSED void** state) {
552  char* argv[] = {"amxrt", "-B b", "-u",
553  "dummy://127.0.0.1:1970",
554  "-F", "my-option=123",
555  "-O", "%config { my-option = 666; }", "./test.odl" };
556 
557  amxd_dm_t* dm = amxrt_get_dm();
558  amxc_var_t* config = amxrt_get_config();
559  amxc_var_t* odl_string = NULL;
560 
561  amxd_dm_init(dm);
562  optind = 1;
563  write(dummy_ctx.data_pipe[1], "s", 1);
564  will_return(__wrap_amxb_be_load_multiple, 0);
565  will_return_always(__wrap_amxb_connect, 0);
566  will_return(__wrap_setpriority, 0);
567  will_return_always(__wrap_amxb_register, 0);
568 
569  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
570 
571  assert_int_equal(GET_INT32(config, "my-option"), 123);
572  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
573  amxc_var_delete(&odl_string);
574 
575  amxd_dm_clean(dm);
576 }
577 
578 void test_runtime_options_can_be_overwritten(UNUSED void** state) {
579  char* argv[] = {"amxrt", "-B b", "-u",
580  "dummy://127.0.0.1:1970",
581  "-o", "my-option=123",
582  "-O", "%config { my-option = 666; }", "./test.odl" };
583 
584  amxd_dm_t* dm = amxrt_get_dm();
585  amxc_var_t* config = amxrt_get_config();
586  amxc_var_t* odl_string = NULL;
587 
588  amxd_dm_init(dm);
589  optind = 1;
590  write(dummy_ctx.data_pipe[1], "s", 1);
591  will_return(__wrap_amxb_be_load_multiple, 0);
592  will_return_always(__wrap_amxb_connect, 0);
593  will_return(__wrap_setpriority, 0);
594  will_return_always(__wrap_amxb_register, 0);
595  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
596 
597  assert_int_equal(GET_INT32(config, "my-option"), 999);
598  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
599  amxc_var_delete(&odl_string);
600 
601  amxd_dm_clean(dm);
602 }
603 
604 void test_runtime_can_pass_option_path(UNUSED void** state) {
605  char* argv[] = {"amxrt", "-B b", "-u",
606  "dummy://127.0.0.1:1970",
607  "-o", "my-table.my-key.0=123",
608  "-o", "my-table.my-key.1=true",
609  "./test.odl" };
610 
611  amxd_dm_t* dm = amxrt_get_dm();
612  amxc_var_t* config = amxrt_get_config();
613  amxc_var_t* cfg = NULL;
614 
615  amxd_dm_init(dm);
616  optind = 1;
617  write(dummy_ctx.data_pipe[1], "s", 1);
618  will_return(__wrap_amxb_be_load_multiple, 0);
619  will_return_always(__wrap_amxb_connect, 0);
620  will_return(__wrap_setpriority, 0);
621  will_return_always(__wrap_amxb_register, 0);
622  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
623 
624  cfg = GETP_ARG(config, "my-table.my-key.0");
625  assert_int_equal(amxc_var_dyncast(int32_t, cfg), 123);
626  cfg = GETP_ARG(config, "my-table.my-key.1");
627  assert_true(GET_BOOL(cfg, NULL));
628 
629  amxd_dm_clean(dm);
630 }
631 
633  char* argv[] = {"amxrt", "-B b", "-u",
634  "dummy://127.0.0.1:1970",
635  "-F", "my-table.odl-key=xyz",
636  "-O", "%config{ my-table = { odl-key=\"abc\" }; }",
637  "./test.odl" };
638 
639  amxd_dm_t* dm = amxrt_get_dm();
640  amxc_var_t* config = amxrt_get_config();
641  amxc_var_t* cfg = NULL;
642  amxc_var_t* odl_string = NULL;
643 
644  amxd_dm_init(dm);
645  optind = 1;
646  write(dummy_ctx.data_pipe[1], "s", 1);
647  will_return(__wrap_amxb_be_load_multiple, 0);
648  will_return_always(__wrap_amxb_connect, 0);
649  will_return(__wrap_setpriority, 0);
650  will_return_always(__wrap_amxb_register, 0);
651  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
652 
653  cfg = GETP_ARG(config, "my-table.odl-key");
654  assert_string_equal(GET_CHAR(cfg, NULL), "xyz");
655  odl_string = GET_ARG(config, AMXRT_COPT_ODL);
656  amxc_var_delete(&odl_string);
657 
658  amxd_dm_clean(dm);
659 }
660 
661 void test_runtime_can_pass_json_value_option(UNUSED void** state) {
662  char* argv[] = {"amxrt", "-B b", "-u",
663  "dummy://127.0.0.1:1970",
664  "-o", "my-table.my-key.2={\"key1\":true, \"key2\":[1,2,3], \"key3\":\"abc\"}",
665  "./test.odl" };
666 
667  amxd_dm_t* dm = amxrt_get_dm();
668  amxc_var_t* config = amxrt_get_config();
669  amxc_var_t* cfg = NULL;
670 
671  amxd_dm_init(dm);
672  optind = 1;
673  write(dummy_ctx.data_pipe[1], "s", 1);
674  will_return(__wrap_amxb_be_load_multiple, 0);
675  will_return_always(__wrap_amxb_connect, 0);
676  will_return(__wrap_setpriority, 0);
677  will_return_always(__wrap_amxb_register, 0);
678  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
679 
680  cfg = GETP_ARG(config, "my-table.my-key.2.key1");
681  assert_true(amxc_var_constcast(bool, cfg));
682  cfg = GETP_ARG(config, "my-table.my-key.2.key2.0");
683  assert_int_equal(amxc_var_dyncast(uint32_t, cfg), 1);
684  cfg = GETP_ARG(config, "my-table.my-key.2.key2.1");
685  assert_int_equal(amxc_var_dyncast(uint32_t, cfg), 2);
686  cfg = GETP_ARG(config, "my-table.my-key.2.key2.2");
687  assert_int_equal(amxc_var_dyncast(uint32_t, cfg), 3);
688  cfg = GETP_ARG(config, "my-table.my-key.2.key3");
689  assert_string_equal(amxc_var_constcast(cstring_t, cfg), "abc");
690 
691  amxd_dm_clean(dm);
692 }
693 
695  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "test.odl" };
696 
697  amxd_dm_t* dm = amxrt_get_dm();
698 
699  amxd_dm_init(dm);
700  write(dummy_ctx.data_pipe[1], "s", 1);
701  optind = 1;
702  will_return(__wrap_amxb_be_load_multiple, 0);
703  will_return_always(__wrap_amxb_connect, 0);
704  will_return(__wrap_setpriority, 0);
705  will_return_always(__wrap_amxb_register, -1);
706  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
707 
708  amxd_dm_clean(dm);
709 }
710 
711 void test_runtime_can_wait_for_objects(UNUSED void** state) {
712  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-R", "NetDev.", "test.odl" };
713 
714  amxd_dm_t* dm = amxrt_get_dm();
715 
716  amxd_dm_init(dm);
717  optind = 1;
718  will_return(__wrap_amxb_be_load_multiple, 0);
719  will_return_always(__wrap_amxb_connect, 0);
720  will_return(__wrap_setpriority, 0);
721  will_return(__wrap_amxb_wait_for_object, 0);
722  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
723 
724  will_return_always(__wrap_amxb_register, 0);
725  amxp_sigmngr_trigger_signal(NULL, "wait:done", NULL);
726 
727  amxd_dm_clean(dm);
728 }
729 
730 void test_runtime_wait_for_objects_can_fail(UNUSED void** state) {
731  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-R", "NetDev.", "test.odl" };
732 
733  amxd_dm_t* dm = amxrt_get_dm();
734 
735  amxd_dm_init(dm);
736  optind = 1;
737  will_return(__wrap_amxb_be_load_multiple, 0);
738  will_return_always(__wrap_amxb_connect, 0);
739  will_return(__wrap_setpriority, 0);
740  will_return(__wrap_amxb_wait_for_object, -1);
741 
742  assert_int_not_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
743 
744  will_return_always(__wrap_amxb_register, 0);
745  amxp_sigmngr_trigger_signal(NULL, "wait:done", NULL);
746 
747  amxd_dm_clean(dm);
748 }
749 
751  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-R", "NetDev.", "test.odl" };
752 
753  amxd_dm_t* dm = amxrt_get_dm();
754  amxc_var_t* config = amxrt_get_config();
755 
756  amxd_dm_init(dm);
757  optind = 1;
758  will_return(__wrap_amxb_be_load_multiple, 0);
759  will_return_always(__wrap_amxb_connect, 0);
760  will_return(__wrap_setpriority, 0);
761  will_return_always(__wrap_amxb_wait_for_object, 0);
762  amxc_var_add_key(bool, config, AMXRT_COPT_SUSPEND, true);
763  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
764 
765  will_return_always(__wrap_amxb_register, 0);
766  amxp_sigmngr_trigger_signal(NULL, "wait:done", NULL);
767 
768  amxc_var_set(bool, GET_ARG(config, AMXRT_COPT_SUSPEND), false);
769 
770  amxd_dm_clean(dm);
771 }
772 
774  char* argv[] = {"amxrt", "-B b", "-u", "dummy://127.0.0.1:1970", "-R", "NetDev.", "test.odl" };
775 
776  amxd_dm_t* dm = amxrt_get_dm();
777 
778  amxd_dm_init(dm);
779  optind = 1;
780  will_return(__wrap_amxb_be_load_multiple, 0);
781  will_return_always(__wrap_amxb_connect, 0);
782  will_return(__wrap_setpriority, 0);
783  will_return_always(__wrap_amxb_register, -1);
784  will_return(__wrap_amxb_wait_for_object, 0);
785 
786  assert_int_equal(amxrt(sizeof(argv) / sizeof(argv[0]), argv, NULL), 0);
787  amxp_sigmngr_trigger_signal(NULL, "wait:done", NULL);
788 
789  amxd_dm_clean(dm);
790 }
791 
792 void test_runtime_can_create_listen_sockets(UNUSED void** state) {
793  amxc_var_t* config = amxrt_get_config();
794  amxc_var_t* listen = GET_ARG(config, AMXRT_COPT_LISTEN);
795 
796  amxc_var_add(cstring_t, listen, "usp:/var/run/usp-ep.sock");
797  will_return(__wrap_amxb_be_load_multiple, 0);
798  will_return_always(__wrap_amxb_connect, 0);
799  will_return_always(__wrap_amxb_listen, 0);
800  will_return(__wrap_setpriority, 0);
801  will_return_always(__wrap_amxb_register, 0);
802  assert_int_equal(amxrt_connect(), 0);
803  assert_int_equal(amxrt_register_or_wait(), 0);
804 }
805 
806 void test_runtime_connect_fails_if_no_backends(UNUSED void** state) {
807  amxc_var_t* config = amxrt_get_config();
808  amxc_var_t* backends = GET_ARG(config, AMXRT_COPT_BACKENDS);
809 
810  amxc_var_set_type(backends, AMXC_VAR_ID_CSTRING);
811  assert_int_not_equal(amxrt_connect(), 0);
812  amxc_var_set_type(backends, AMXC_VAR_ID_LIST);
813  assert_int_not_equal(amxrt_connect(), 0);
814 }
#define AMXRT_COPT_BACKENDS
Definition: amxrt.h:75
#define AMXRT_COPT_LISTEN
Definition: amxrt.h:89
#define AMXRT_COPT_ODL
Definition: amxrt.h:81
#define AMXRT_COPT_SUSPEND
Definition: amxrt.h:101
#define AMXRT_COPT_DUMP_CONFIG
Definition: amxrt.h:91
#define AMXRT_COPT_HANDLE_EVENTS
Definition: amxrt.h:100
#define AMXRT_COPT_LOG
Definition: amxrt.h:98
#define AMXRT_COPT_DAEMON
Definition: amxrt.h:82
int amxrt_el_stop(void)
Stops the event loop.
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301
int amxrt_connect(void)
Connects to all bus sockets.
Definition: amxrt.c:488
int amxrt_init(int argc, char *argv[], amxrt_arg_fn_t fn)
Initializes the ambiorix runtime.
Definition: amxrt.c:348
int amxrt(int argc, char *argv[], amxrt_arg_fn_t fn)
This function can create full ambiorix application (data model provider or client).
Definition: amxrt.c:334
void amxrt_delete(void)
Clean-up ambiorix runtime.
Definition: amxrt.c:378
amxd_dm_t * amxrt_get_dm(void)
Gets the runtime data model storage.
Definition: amxrt.c:309
void amxrt_new(void)
Create the ambiorix runtime.
Definition: amxrt.c:313
void amxrt_stop(void)
Stops the runtime.
Definition: amxrt.c:440
int amxrt_register_or_wait(void)
Register the data model or wait for required data model objects.
Definition: amxrt.c:389
amxb_bus_ctx_t ctx
config
Definition: test.odl:54
void test_runtime_can_wait_for_objects(UNUSED void **state)
void test_runtime_parses_default_odl(UNUSED void **state)
void __wrap_amxb_free(amxb_bus_ctx_t **ctx)
void test_runtime_parse_args_wrong_odl(UNUSED void **state)
void test_runtime_parses_odls_and_handle_events(UNUSED void **state)
int __wrap_amxb_be_load_multiple(amxc_var_t *const bes)
void test_runtime_forced_options_are_kept(UNUSED void **state)
int test_runtime_setup(UNUSED void **state)
void test_runtime_parse_args_odl(UNUSED void **state)
void test_runtime_fails_when_invalid_odl(UNUSED void **state)
void test_runtime_connect_fails_if_no_backends(UNUSED void **state)
int __wrap_amxb_read(amxb_bus_ctx_t *ctx)
void test_runtime_creates_pid_file(UNUSED void **state)
void test_runtime_start_prints_help_and_exits(UNUSED void **state)
struct dummy_bus dummy_bus_t
void test_runtime_cannot_connect_bus_uri(UNUSED void **state)
void test_runtime_stops_el_when_registering_after_wait_fails(UNUSED void **state)
void test_runtime_cannot_load_backends(UNUSED void **state)
int __wrap_amxb_register(amxb_bus_ctx_t *ctx, amxd_dm_t *dm)
void test_runtime_parses_post_includes(UNUSED void **state)
void test_runtime_dumps_config(UNUSED void **state)
int __wrap_daemon(int nochdir, int noclose)
void test_runtime_fails_when_registering_dm_fails(UNUSED void **state)
int __wrap_amxb_get_fd(amxb_bus_ctx_t *ctx)
int __wrap_amxb_connect(amxb_bus_ctx_t **ctx, const char *uri)
void test_runtime_enables_system_signals_list(UNUSED void **state)
void test_runtime_opens_syslog(UNUSED void **state)
void test_runtime_options_can_be_overwritten(UNUSED void **state)
void test_runtime_set_priority_failed(UNUSED void **state)
void test_runtime_parses_odls(UNUSED void **state)
void test_runtime_can_suspend_events_while_waiting(UNUSED void **state)
void test_runtime_can_pass_json_value_option(UNUSED void **state)
void test_runtime_forced_options_with_path_are_kept(UNUSED void **state)
int __wrap_amxb_wait_for_object(const char *object)
void test_runtime_succeeds_when_no_odl(UNUSED void **state)
void test_runtime_daemon_failed(UNUSED void **state)
void test_runtime_can_pass_option_path(UNUSED void **state)
void test_runtime_wait_for_objects_can_fail(UNUSED void **state)
void test_runtime_can_create_listen_sockets(UNUSED void **state)
int __wrap_amxb_listen(amxb_bus_ctx_t **ctx, const char *uri)
static dummy_bus_t dummy_ctx
void test_runtime_enables_system_signal(UNUSED void **state)
int test_runtime_teardown(UNUSED void **state)
void test_runtime_connects_to_uri(UNUSED void **state)
void test_runtime_no_pidfile(UNUSED void **state)
int __wrap_setpriority(int which, id_t who, int value)