libamxrt  0.4.2
Ambiorix Run Time Library
test_amxrt_config.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_config.h"
61 
62 int test_config_setup(UNUSED void** state) {
63  amxrt_new();
64  return 0;
65 }
66 
67 int test_config_teardown(UNUSED void** state) {
68  amxrt_delete();
69  return 0;
70 }
71 
72 void test_config_init(UNUSED void** state) {
73  amxc_var_t* config = amxrt_get_config();
74  const char* inc_dirs[] = { ".", "${prefix}${cfg-dir}/${name}", "${prefix}${cfg-dir}/modules" };
75  const char* lib_dirs[] = { ".",
76  "${prefix}${plugin-dir}/${name}",
77  "${prefix}${plugin-dir}/modules",
78  "${prefix}/usr/local/lib/amx/${name}",
79  "${prefix}/usr/local/lib/amx/modules" };
80  const char* mib_dirs[] = { "${prefix}${cfg-dir}/${name}/mibs" };
81  char* argv[] = { "amxrt" };
82  int index = 0;
83 
84  assert_int_equal(amxrt_config_init(sizeof(argv) / sizeof(argv[0]), argv, &index, NULL), 0);
85  assert_int_equal(index, 1);
86 
87  amxc_var_dump(config, STDOUT_FILENO);
88 
89  assert_true(GET_BOOL(config, AMXRT_COPT_AUTO_DETECT));
90  assert_true(GET_BOOL(config, AMXRT_COPT_AUTO_CONNECT));
91  assert_false(GET_BOOL(config, AMXRT_COPT_DAEMON));
92  assert_int_equal(GET_INT32(config, AMXRT_COPT_PRIORITY), 0);
93  assert_true(GET_BOOL(config, AMXRT_COPT_PID_FILE));
94  assert_string_equal(GET_CHAR(config, AMXRT_COPT_PREFIX_PATH), "");
95  assert_string_equal(GET_CHAR(config, AMXRT_COPT_PLUGIN_DIR), AMXRT_CVAL_PLUGIN_DIR);
96  assert_string_equal(GET_CHAR(config, AMXRT_COPT_CFG_DIR), AMXRT_CVAL_CFG_DIR);
97  assert_false(GET_BOOL(config, AMXRT_COPT_EVENT));
98  assert_false(GET_BOOL(config, AMXRT_COPT_DUMP_CONFIG));
99  assert_string_equal(GET_CHAR(config, AMXRT_COPT_BACKENDS_DIR), AMXRT_CVAL_BACKEND_DIR);
100  assert_string_equal(GET_CHAR(config, AMXRT_COPT_STORAGE_TYPE), AMXRT_CVAL_STORAGE_TYPE);
101  assert_false(GET_BOOL(config, AMXRT_COPT_LOG));
102  assert_string_equal(GET_CHAR(config, AMXRT_COPT_RW_DATA_PATH), "${prefix}" RWDATAPATH);
103  assert_string_equal(GET_CHAR(config, AMXRT_COPT_STORAGE_DIR), "${rw_data_path}/${name}/");
104 
105  index = 0;
106  amxc_var_for_each(var, GET_ARG(config, AMXRT_COPT_INCDIRS)) {
107  assert_string_equal(GET_CHAR(var, NULL), inc_dirs[index]);
108  index++;
109  }
110 
111  index = 0;
112  amxc_var_for_each(var, GET_ARG(config, AMXRT_COPT_LIBDIRS)) {
113  assert_string_equal(GET_CHAR(var, NULL), lib_dirs[index]);
114  index++;
115  }
116 
117  index = 0;
118  amxc_var_for_each(var, GET_ARG(config, AMXRT_COPT_MIBDIRS)) {
119  assert_string_equal(GET_CHAR(var, NULL), mib_dirs[index]);
120  index++;
121  }
122 }
123 
124 void test_config_can_use_env_vars(UNUSED void** state) {
125  amxc_var_t* config = amxrt_get_config();
126 
127  setenv("AMXRT_PREFIX_PATH", "/tmp/test/", 1);
128  setenv("AMXB_BACKENDS", "/tmp/test1.so;/tmp/test2.so", 1);
129  setenv("TEST_VAR", "some value", 1);
130 
131  char* argv[] = { "amxrt.bin" };
132  int index = 0;
133 
134  assert_int_equal(amxrt_config_init(sizeof(argv) / sizeof(argv[0]), argv, &index, NULL), 0);
135  assert_int_equal(index, 1);
136  assert_string_equal(GET_CHAR(config, AMXRT_COPT_PREFIX_PATH), "/tmp/test/");
137 
138  amxrt_config_read_env_var("TEST_VAR", "test", AMXC_VAR_ID_CSTRING);
139  assert_string_equal(GET_CHAR(config, "test"), "some value");
140 
141 }
142 
143 void test_invalid_options(UNUSED void** state) {
144  char* argv[] = { "amxrt.bin", "-B" };
145  int index = 0;
146 
147  assert_int_not_equal(amxrt_config_init(sizeof(argv) / sizeof(argv[0]), argv, &index, NULL), 0);
148  assert_int_equal(index, sizeof(argv) / sizeof(argv[0]));
149 }
#define AMXRT_COPT_RW_DATA_PATH
Definition: amxrt.h:94
#define AMXRT_COPT_PLUGIN_DIR
Definition: amxrt.h:87
#define AMXRT_COPT_PID_FILE
Definition: amxrt.h:84
#define AMXRT_COPT_LIBDIRS
Definition: amxrt.h:79
#define AMXRT_COPT_CFG_DIR
Definition: amxrt.h:88
#define AMXRT_COPT_MIBDIRS
Definition: amxrt.h:80
#define AMXRT_COPT_DUMP_CONFIG
Definition: amxrt.h:91
#define AMXRT_COPT_PRIORITY
Definition: amxrt.h:83
#define AMXRT_COPT_PREFIX_PATH
Definition: amxrt.h:86
#define AMXRT_COPT_STORAGE_DIR
Definition: amxrt.h:95
#define AMXRT_COPT_STORAGE_TYPE
Definition: amxrt.h:96
#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_BACKENDS_DIR
Definition: amxrt.h:93
#define AMXRT_COPT_DAEMON
Definition: amxrt.h:82
#define AMXRT_COPT_INCDIRS
Definition: amxrt.h:78
#define AMXRT_CVAL_CFG_DIR
Definition: amxrt_priv.h:65
#define AMXRT_CVAL_BACKEND_DIR
Definition: amxrt_priv.h:66
#define AMXRT_CVAL_STORAGE_TYPE
Definition: amxrt_priv.h:67
#define AMXRT_CVAL_PLUGIN_DIR
Definition: amxrt_priv.h:64
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 amxrt_config_read_env_var(const char *var_name, const char *config_name, int32_t var_type)
Helper function to read an environment variable and add it's value to the runtime configuration.
Definition: amxrt_config.c:264
int amxrt_config_init(int argc, char *argv[], int *index, amxrt_arg_fn_t fn)
Initializes the default runtime configuration.
Definition: amxrt_config.c:217
void test_invalid_options(UNUSED void **state)
int test_config_teardown(UNUSED void **state)
int test_config_setup(UNUSED void **state)
void test_config_init(UNUSED void **state)
void test_config_can_use_env_vars(UNUSED void **state)
config
Definition: test.odl:54