libamxo  4.3.4
Object Definition Language (ODL) parsing
test_include.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 <sys/time.h>
55 #include <sys/resource.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 
59 #include <string.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <stdarg.h>
63 #include <stddef.h>
64 #include <setjmp.h>
65 #include <inttypes.h>
66 #include <limits.h>
67 #include <unistd.h>
68 #include <fcntl.h>
69 #include <cmocka.h>
70 
71 #include <amxc/amxc.h>
72 #include <amxp/amxp_signal.h>
73 #include <amxd/amxd_dm.h>
74 #include <amxd/amxd_object.h>
75 #include <amxd/amxd_parameter.h>
76 #include <amxo/amxo.h>
77 
78 #include "test_include.h"
79 
80 #include <amxc/amxc_macros.h>
81 void test_can_include_empty_file(UNUSED void** state) {
82  amxd_dm_t dm;
83  amxo_parser_t parser;
84  const char* odl = "include \"empty.odl\";";
85 
86  amxd_dm_init(&dm);
87  amxo_parser_init(&parser);
88 
89  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
90  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
91 
92  amxo_parser_clean(&parser);
93  amxd_dm_clean(&dm);
94 }
95 
97  amxd_dm_t dm;
98  amxo_parser_t parser;
99  const char* odl =
100  "include \"empty.odl\";" \
101  "%config { }" \
102  "include \"empty.odl\";" \
103  "%define { }" \
104  "include \"empty.odl\";" \
105  "%populate { }" \
106  "include \"empty.odl\";";
107 
108  amxd_dm_init(&dm);
109  amxo_parser_init(&parser);
110 
111  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
112  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
113 
114  amxo_parser_clean(&parser);
115  amxd_dm_clean(&dm);
116 }
117 
119  amxd_dm_t dm;
120  amxo_parser_t parser;
121  const char* odl = "include \"does_not_exists.odl\";";
122 
123  amxd_dm_init(&dm);
124  amxo_parser_init(&parser);
125 
126  assert_int_not_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
127  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_file_not_found);
128 
129  amxo_parser_clean(&parser);
130  amxd_dm_clean(&dm);
131 }
132 
134  amxd_dm_t dm;
135  amxo_parser_t parser;
136  const char* odl = "#include \"does_not_exists.odl\";";
137 
138  amxd_dm_init(&dm);
139  amxo_parser_init(&parser);
140 
141  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
142  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
143 
144  amxo_parser_clean(&parser);
145  amxd_dm_clean(&dm);
146 }
147 
149  amxd_dm_t dm;
150  amxo_parser_t parser;
151  const char* odl = "include \"test1.odl\";";
152 
153  amxd_dm_init(&dm);
154  amxo_parser_init(&parser);
155 
156  assert_int_not_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
157  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_unknown_error);
158 
159  amxo_parser_clean(&parser);
160  amxd_dm_clean(&dm);
161 }
162 
164  amxd_dm_t dm;
165  amxo_parser_t parser;
166  const char* odl = "include \"./recursive_dir/\";";
167 
168  amxd_dm_init(&dm);
169  amxo_parser_init(&parser);
170 
171  assert_int_not_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
172  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_unknown_error);
173 
174  amxo_parser_clean(&parser);
175  amxd_dm_clean(&dm);
176 }
177 
178 void test_include_absolute_path(UNUSED void** state) {
179  amxd_dm_t dm;
180  amxo_parser_t parser;
181  char* abs_path = realpath("empty.odl", NULL);
182  char odl[8192];
183 
184  amxd_dm_init(&dm);
185  amxo_parser_init(&parser);
186 
187  sprintf(odl, "include \"%s\";", abs_path);
188  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
189  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
190 
191  sprintf(odl, "include \"/tmp/fake.odl\";");
192  assert_int_not_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
193  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_file_not_found);
194 
195  free(abs_path);
196  amxo_parser_clean(&parser);
197  amxd_dm_clean(&dm);
198 }
199 
200 void test_post_include(UNUSED void** state) {
201  amxd_dm_t dm;
202  amxo_parser_t parser;
203  char* abs_path = realpath("empty.odl", NULL);
204  char odl[8192];
205 
206  amxd_dm_init(&dm);
207  amxo_parser_init(&parser);
208 
209  sprintf(odl, "&include \"%s\";", abs_path);
210  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
211  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
212 
213  assert_true(dm.sigmngr.enabled);
214 
215  sprintf(odl, "&include \"/tmp/fake.odl\";");
216  assert_int_not_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
217  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_file_not_found);
218 
219  assert_int_equal(amxo_parser_invoke_entry_points(&parser, &dm, 0), 0);
220 
221  assert_true(dm.sigmngr.enabled);
222 
223  free(abs_path);
224  amxo_parser_clean(&parser);
225  amxd_dm_clean(&dm);
226 }
227 
228 void test_can_include_directory(UNUSED void** state) {
229  amxd_dm_t dm;
230  amxo_parser_t parser;
231  const char* odl = "include \"main.odl\";";
232 
233  amxd_dm_init(&dm);
234  amxo_parser_init(&parser);
235 
236  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
237  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
238 
239  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.default"));
240  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.1"));
241  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.2"));
242  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.3"));
243 
244  amxo_parser_clean(&parser);
245  amxd_dm_clean(&dm);
246 }
247 
248 void test_empty_directory(UNUSED void** state) {
249  amxd_dm_t dm;
250  amxo_parser_t parser;
251  const char* odl = "include \"./empty_dir/\";";
252 
253  amxd_dm_init(&dm);
254  amxo_parser_init(&parser);
255 
256  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
257 
258  amxo_parser_clean(&parser);
259  amxd_dm_clean(&dm);
260 }
261 
263  amxd_dm_t dm;
264  amxo_parser_t parser;
265  const char* odl = "include \"./empty_dir/\":\"./main.odl\";";
266 
267  amxd_dm_init(&dm);
268  amxo_parser_init(&parser);
269 
270  assert_int_equal(amxo_parser_parse_string(&parser, odl, amxd_dm_get_root(&dm)), 0);
271  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
272  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.default"));
273  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.1"));
274  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.2"));
275  assert_non_null(amxd_dm_findf(&dm, "MainObject.InstanceObject.3"));
276 
277  amxo_parser_clean(&parser);
278  amxd_dm_clean(&dm);
279 }
280 
282  amxd_dm_t dm;
283  amxo_parser_t parser;
284  const char* odl = "./composite_config/config_main.odl";
285 
286  amxd_dm_init(&dm);
287  amxo_parser_init(&parser);
288 
289  assert_int_equal(amxo_parser_parse_file(&parser, odl, amxd_dm_get_root(&dm)), 0);
290 
291  assert_non_null(GETP_ARG(&parser.config, "ubus.watch-ubus-events"));
292  assert_non_null(GETP_ARG(&parser.config, "ubus.register-on-start-event"));
293 
294  amxo_parser_clean(&parser);
295  amxd_dm_clean(&dm);
296 }
297 
298 void test_global_nested_include(UNUSED void** state) {
299  amxd_dm_t dm;
300  amxo_parser_t parser;
301  const char* odl = "./composite_config/config_main.odl";
302  amxc_var_t* requires = NULL;
303  const amxc_llist_t* rl = NULL;
304  int index = 0;
305  const char* expected[] = { "Test1", "Test2", "Test3" };
306 
307  amxd_dm_init(&dm);
308  amxo_parser_init(&parser);
309 
310  assert_int_equal(amxo_parser_parse_file(&parser, odl, amxd_dm_get_root(&dm)), 0);
311 
312  requires = GETP_ARG(&parser.config, "requires");
313  assert_non_null(requires);
314  assert_int_equal(amxc_var_type_of(requires), AMXC_VAR_ID_LIST);
315 
316  rl = amxc_var_constcast(amxc_llist_t, requires);
317  assert_int_equal(amxc_llist_size(rl), 3);
318 
319  amxc_var_for_each(r, requires) {
320  assert_string_equal(expected[index], GET_CHAR(r, NULL));
321  index++;
322  }
323 
324  amxo_parser_clean(&parser);
325  amxd_dm_clean(&dm);
326 }
327 
Ambiorix ODL parser header file.
include test_include odl
Definition: test_valid.odl:66
int amxo_parser_parse_string(amxo_parser_t *parser, const char *text, amxd_object_t *object)
Parses a string containing a valid ODL part.
void amxo_parser_clean(amxo_parser_t *parser)
Cleans up the odl parser instance.
int amxo_parser_parse_file(amxo_parser_t *parser, const char *file_path, amxd_object_t *object)
Parses an odl file.
static amxd_status_t amxo_parser_get_status(amxo_parser_t *parser)
Get the status of the odl parser.
Definition: amxo.h:414
int amxo_parser_init(amxo_parser_t *parser)
Initializes a new odl parser instance.
int amxo_parser_invoke_entry_points(amxo_parser_t *parser, amxd_dm_t *dm, int reason)
Invokes all registered entry points.
The ODL parser structure.
Definition: amxo_types.h:245
amxc_var_t config
Definition: amxo_types.h:250
void test_empty_directory(UNUSED void **state)
Definition: test_include.c:248
void test_none_existing_include_file(UNUSED void **state)
Definition: test_include.c:118
void test_post_include(UNUSED void **state)
Definition: test_include.c:200
void test_composite_config_options_are_extended(UNUSED void **state)
Definition: test_include.c:281
void test_include_absolute_path(UNUSED void **state)
Definition: test_include.c:178
void test_global_nested_include(UNUSED void **state)
Definition: test_include.c:298
void test_can_include_directory(UNUSED void **state)
Definition: test_include.c:228
void test_recursive_include_detection(UNUSED void **state)
Definition: test_include.c:148
void test_can_include_empty_file(UNUSED void **state)
Definition: test_include.c:81
void test_none_existing_optional_include_file(UNUSED void **state)
Definition: test_include.c:133
void test_conditional_include_with_empty_directory_takes_second(UNUSED void **state)
Definition: test_include.c:262
void test_recursive_dir_include_detection(UNUSED void **state)
Definition: test_include.c:163
void test_can_include_between_sections(UNUSED void **state)
Definition: test_include.c:96
#define UNUSED
Definition: test_issue_48.c:84