libamxo  4.3.4
Object Definition Language (ODL) parsing
test_hooks.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 #include <amxo/amxo_hooks.h>
78 
79 #include "test_hooks.h"
80 
81 #include <amxc/amxc_macros.h>
82 static amxc_aqueue_t expected_order;
83 
84 static void test_hook_comment(UNUSED amxo_parser_t* parser, const char* comment) {
85  printf("%s\n", comment);
86 }
87 
88 static void test_hook_start(UNUSED amxo_parser_t* parser) {
89  const char* check = amxc_aqueue_remove(&expected_order);
90  assert_ptr_not_equal(check, NULL);
91  printf("%s\n", check);
92  assert_string_equal(check, "start");
93 }
94 
95 static void test_hook_end(UNUSED amxo_parser_t* parser) {
96  const char* check = amxc_aqueue_remove(&expected_order);
97  assert_ptr_not_equal(check, NULL);
98  printf("%s\n", check);
99  assert_string_equal(check, "end");
100 }
101 
103  UNUSED const char* file) {
104  const char* check = amxc_aqueue_remove(&expected_order);
105  assert_ptr_not_equal(check, NULL);
106  printf("%s\n", check);
107  assert_string_equal(check, "include start");
108 }
109 
111  UNUSED const char* file) {
112  const char* check = amxc_aqueue_remove(&expected_order);
113  assert_ptr_not_equal(check, NULL);
114  printf("%s\n", check);
115  assert_string_equal(check, "include end");
116 }
117 
119  UNUSED int section_id) {
120  const char* check = amxc_aqueue_remove(&expected_order);
121  assert_ptr_not_equal(check, NULL);
122  printf("%s\n", check);
123  assert_string_equal(check, "section start");
124 }
125 
127  UNUSED int section_id) {
128  const char* check = amxc_aqueue_remove(&expected_order);
129  assert_ptr_not_equal(check, NULL);
130  printf("%s\n", check);
131  assert_string_equal(check, "section end");
132 }
133 
135  UNUSED const char* option,
136  UNUSED amxc_var_t* value) {
137  const char* check = amxc_aqueue_remove(&expected_order);
138  assert_ptr_not_equal(check, NULL);
139  printf("%s\n", check);
140  assert_string_equal(check, "set config");
141 }
142 
144  UNUSED amxd_object_t* parent,
145  UNUSED const char* name,
146  UNUSED int64_t attr_bitmask,
147  UNUSED amxd_object_type_t type) {
148  const char* check = amxc_aqueue_remove(&expected_order);
149  assert_ptr_not_equal(check, NULL);
150  printf("%s\n", check);
151  assert_string_equal(check, "create object");
152 }
153 
155  UNUSED amxd_object_t* parent,
156  UNUSED uint32_t index,
157  UNUSED const char* name) {
158  const char* check = amxc_aqueue_remove(&expected_order);
159  assert_ptr_not_equal(check, NULL);
160  printf("%s\n", check);
161  assert_string_equal(check, "instance add");
162 }
163 
165  UNUSED amxd_object_t* parent,
166  UNUSED const char* path) {
167  const char* check = amxc_aqueue_remove(&expected_order);
168  assert_ptr_not_equal(check, NULL);
169  printf("%s\n", check);
170  assert_string_equal(check, "select object");
171 }
172 
174  UNUSED amxd_object_t* object) {
175  const char* check = amxc_aqueue_remove(&expected_order);
176  assert_ptr_not_equal(check, NULL);
177  printf("%s\n", check);
178  assert_string_equal(check, "end object");
179 }
180 
182  UNUSED amxd_object_t* object,
183  UNUSED const char* name,
184  UNUSED int64_t attr_bitmask,
185  UNUSED uint32_t type) {
186 }
187 
189  UNUSED amxd_object_t* object,
190  UNUSED amxd_param_t* param,
191  UNUSED amxc_var_t* value) {
192 }
193 
195  UNUSED amxd_object_t* object,
196  UNUSED amxd_param_t* param) {
197 }
198 
200  UNUSED amxd_object_t* object,
201  UNUSED const char* name,
202  UNUSED int64_t attr_bitmask,
203  UNUSED uint32_t type) {
204 }
205 
207  UNUSED amxd_object_t* object,
208  UNUSED amxd_function_t* function) {
209 }
210 
212  UNUSED amxd_object_t* object,
213  UNUSED amxd_function_t* func,
214  UNUSED const char* name,
215  UNUSED int64_t attr_bitmask,
216  UNUSED uint32_t type,
217  UNUSED amxc_var_t* def_value) {
218 }
219 
221  UNUSED amxd_object_t* parent,
222  const char* name) {
223  assert_string_equal(name, "NumberOfChildren");
224 }
225 
227  UNUSED amxd_object_t* object,
228  UNUSED const char* mib) {
229  const char* check = amxc_aqueue_remove(&expected_order);
230  assert_ptr_not_equal(check, NULL);
231  printf("%s\n", check);
232  assert_string_equal(check, "add mib");
233 }
234 
237  .end = test_hook_end,
238  .comment = test_hook_comment,
239  .start_include = test_hook_start_include,
240  .end_include = test_hook_end_include,
241  .set_config = test_hook_set_config,
242  .start_section = test_hook_start_section,
243  .end_section = test_hook_end_section,
244  .create_object = test_hook_create_object,
245  .add_instance = test_hook_add_instance,
246  .select_object = test_hook_select_object,
247  .end_object = test_hook_end_object,
248  .add_param = test_hook_add_param,
249  .set_param = test_hook_set_param,
250  .end_param = test_hook_end_param,
251  .add_func = test_hook_add_func,
252  .end_func = test_hook_end_func,
253  .add_func_arg = test_hook_add_func_arg,
254  .set_counter = test_hook_set_counter,
255  .add_mib = test_hook_add_mib
256 };
257 
259 
260 void test_hooks_are_called(UNUSED void** state) {
261  amxd_dm_t dm;
262  amxo_parser_t parser;
263 
264  amxc_aqueue_init(&expected_order);
265  amxd_dm_init(&dm);
266  amxo_parser_init(&parser);
267 
268  amxc_aqueue_add(&expected_order, "start");
269  amxc_aqueue_add(&expected_order, "section start");
270  amxc_aqueue_add(&expected_order, "set config");
271  amxc_aqueue_add(&expected_order, "set config");
272  amxc_aqueue_add(&expected_order, "set config");
273  amxc_aqueue_add(&expected_order, "section end");
274  amxc_aqueue_add(&expected_order, "include start");
275  amxc_aqueue_add(&expected_order, "section start");
276  amxc_aqueue_add(&expected_order, "section end");
277  amxc_aqueue_add(&expected_order, "include end");
278  amxc_aqueue_add(&expected_order, "section start");
279  amxc_aqueue_add(&expected_order, "create object"); // mib test_mib
280  amxc_aqueue_add(&expected_order, "end object");
281  amxc_aqueue_add(&expected_order, "create object"); // TestObjectRoot
282  amxc_aqueue_add(&expected_order, "create object"); // TestObjectSingelton
283  amxc_aqueue_add(&expected_order, "end object");
284  amxc_aqueue_add(&expected_order, "create object"); // TestObjSingletonAttr
285  amxc_aqueue_add(&expected_order, "end object");
286  amxc_aqueue_add(&expected_order, "create object"); // TestObjectTemplate
287  amxc_aqueue_add(&expected_order, "end object");
288  amxc_aqueue_add(&expected_order, "create object"); // TestObjTemplateAttr
289  amxc_aqueue_add(&expected_order, "end object");
290  amxc_aqueue_add(&expected_order, "create object"); // TestObjectParamTypes
291  amxc_aqueue_add(&expected_order, "end object");
292  amxc_aqueue_add(&expected_order, "create object"); // TestObjectFunctions
293  amxc_aqueue_add(&expected_order, "end object");
294  amxc_aqueue_add(&expected_order, "create object"); // TestSingeltonParamAttr
295  amxc_aqueue_add(&expected_order, "end object");
296  amxc_aqueue_add(&expected_order, "create object"); // TestTemplateParamAttr
297  amxc_aqueue_add(&expected_order, "end object");
298  amxc_aqueue_add(&expected_order, "create object"); // TestSingletonFuncAttr
299  amxc_aqueue_add(&expected_order, "end object");
300  amxc_aqueue_add(&expected_order, "create object"); // TestTemplateFuncAttr
301  amxc_aqueue_add(&expected_order, "end object");
302  amxc_aqueue_add(&expected_order, "create object"); // TestObjectTemplateWithChild
303  amxc_aqueue_add(&expected_order, "create object"); // TemplateChildObject
304  amxc_aqueue_add(&expected_order, "end object");
305  amxc_aqueue_add(&expected_order, "end object"); // TestObjectTemplateWithChild
306  amxc_aqueue_add(&expected_order, "create object"); // TestChangeAttr
307  amxc_aqueue_add(&expected_order, "end object");
308  amxc_aqueue_add(&expected_order, "end object"); // TestObjectRoot
309  amxc_aqueue_add(&expected_order, "section end");
310  amxc_aqueue_add(&expected_order, "section start");
311  amxc_aqueue_add(&expected_order, "select object");
312  amxc_aqueue_add(&expected_order, "instance add");
313  amxc_aqueue_add(&expected_order, "select object");
314  amxc_aqueue_add(&expected_order, "end object");
315  amxc_aqueue_add(&expected_order, "end object");
316  amxc_aqueue_add(&expected_order, "end object");
317  amxc_aqueue_add(&expected_order, "select object");
318  amxc_aqueue_add(&expected_order, "instance add");
319  amxc_aqueue_add(&expected_order, "end object");
320  amxc_aqueue_add(&expected_order, "instance add");
321  amxc_aqueue_add(&expected_order, "add mib");
322  amxc_aqueue_add(&expected_order, "end object");
323  amxc_aqueue_add(&expected_order, "end object");
324  amxc_aqueue_add(&expected_order, "select object");
325  amxc_aqueue_add(&expected_order, "end object");
326  amxc_aqueue_add(&expected_order, "section end");
327  amxc_aqueue_add(&expected_order, "end");
328 
330  assert_int_equal(amxo_parser_parse_file(&parser, "test_valid.odl", amxd_dm_get_root(&dm)), 0);
331  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
332  assert_int_equal(amxc_aqueue_size(&expected_order), 0);
333 
334  amxd_dm_clean(&dm);
335 
337  amxc_aqueue_add(&expected_order, "start");
338  assert_int_equal(amxo_parser_parse_file(&parser, "test_valid.odl", amxd_dm_get_root(&dm)), 0);
339  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
340  assert_int_equal(amxc_aqueue_size(&expected_order), 1);
341 
342  amxc_aqueue_clean(&expected_order, NULL);
343  amxo_parser_clean(&parser);
344  amxd_dm_clean(&dm);
345 }
346 
347 void test_hooks_empty_hooks(UNUSED void** state) {
348  amxd_dm_t dm;
349  amxo_parser_t parser;
350 
351  amxd_dm_init(&dm);
352  amxo_parser_init(&parser);
353 
355  assert_int_equal(amxo_parser_parse_file(&parser, "test_valid.odl", amxd_dm_get_root(&dm)), 0);
356  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
357 
358  amxo_parser_clean(&parser);
359  amxd_dm_clean(&dm);
360 }
361 
363  amxd_dm_t dm;
364  amxo_parser_t parser;
365  amxo_parser_t parser2;
366 
367  amxd_dm_init(&dm);
368  amxo_parser_init(&parser);
369  amxo_parser_init(&parser2);
370 
371  assert_int_equal(amxo_parser_set_hooks(&parser, &test_hooks), 0);
372  assert_int_equal(amxo_parser_set_hooks(&parser, &test_empty_hooks), 0);
373  assert_int_equal(amxo_parser_unset_hooks(&parser, &test_hooks), 0);
374  assert_int_equal(amxo_parser_unset_hooks(&parser, &test_empty_hooks), 0);
375 
376  assert_int_equal(amxo_parser_parse_file(&parser, "test_valid.odl", amxd_dm_get_root(&dm)), 0);
377  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
378 
379  assert_int_not_equal(amxo_parser_set_hooks(NULL, &test_hooks), 0);
380  assert_int_not_equal(amxo_parser_set_hooks(&parser, NULL), 0);
381  assert_int_not_equal(amxo_parser_unset_hooks(NULL, &test_hooks), 0);
382  assert_int_not_equal(amxo_parser_unset_hooks(&parser, NULL), 0);
383 
384  assert_int_equal(amxo_parser_set_hooks(&parser, &test_hooks), 0);
385  assert_int_not_equal(amxo_parser_set_hooks(&parser, &test_hooks), 0);
386  assert_int_not_equal(amxo_parser_unset_hooks(&parser2, &test_hooks), 0);
387 
388  amxo_parser_clean(&parser);
389  amxo_parser_clean(&parser2);
390  amxd_dm_clean(&dm);
391 }
Ambiorix ODL parser header file.
int amxo_parser_set_hooks(amxo_parser_t *parser, amxo_hooks_t *hooks)
int amxo_parser_unset_hooks(amxo_parser_t *parser, amxo_hooks_t *hooks)
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.
amxo_start_end_t start
Definition: amxo_types.h:184
The ODL parser structure.
Definition: amxo_types.h:245
static amxo_hooks_t test_empty_hooks
Definition: test_hooks.c:258
static void test_hook_create_object(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *parent, UNUSED const char *name, UNUSED int64_t attr_bitmask, UNUSED amxd_object_type_t type)
Definition: test_hooks.c:143
void test_hooks_are_called(UNUSED void **state)
Definition: test_hooks.c:260
static void test_hook_add_func_arg(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED amxd_function_t *func, UNUSED const char *name, UNUSED int64_t attr_bitmask, UNUSED uint32_t type, UNUSED amxc_var_t *def_value)
Definition: test_hooks.c:211
static void test_hook_set_config(UNUSED amxo_parser_t *parser, UNUSED const char *option, UNUSED amxc_var_t *value)
Definition: test_hooks.c:134
static void test_hook_start(UNUSED amxo_parser_t *parser)
Definition: test_hooks.c:88
static void test_hook_end(UNUSED amxo_parser_t *parser)
Definition: test_hooks.c:95
void test_hooks_add_remove_hooks(UNUSED void **state)
Definition: test_hooks.c:362
static void test_hook_end_object(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object)
Definition: test_hooks.c:173
static amxo_hooks_t test_hooks
Definition: test_hooks.c:235
static void test_hook_add_instance(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *parent, UNUSED uint32_t index, UNUSED const char *name)
Definition: test_hooks.c:154
static void test_hook_comment(UNUSED amxo_parser_t *parser, const char *comment)
Definition: test_hooks.c:84
static void test_hook_add_mib(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED const char *mib)
Definition: test_hooks.c:226
static void test_hook_add_func(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED const char *name, UNUSED int64_t attr_bitmask, UNUSED uint32_t type)
Definition: test_hooks.c:199
void test_hooks_empty_hooks(UNUSED void **state)
Definition: test_hooks.c:347
static void test_hook_start_include(UNUSED amxo_parser_t *parser, UNUSED const char *file)
Definition: test_hooks.c:102
static void test_hook_select_object(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *parent, UNUSED const char *path)
Definition: test_hooks.c:164
static void test_hook_set_counter(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *parent, const char *name)
Definition: test_hooks.c:220
static void test_hook_end_param(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED amxd_param_t *param)
Definition: test_hooks.c:194
static void test_hook_add_param(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED const char *name, UNUSED int64_t attr_bitmask, UNUSED uint32_t type)
Definition: test_hooks.c:181
static void test_hook_end_section(UNUSED amxo_parser_t *parser, UNUSED int section_id)
Definition: test_hooks.c:126
static void test_hook_end_func(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED amxd_function_t *function)
Definition: test_hooks.c:206
static void test_hook_start_section(UNUSED amxo_parser_t *parser, UNUSED int section_id)
Definition: test_hooks.c:118
static void test_hook_set_param(UNUSED amxo_parser_t *parser, UNUSED amxd_object_t *object, UNUSED amxd_param_t *param, UNUSED amxc_var_t *value)
Definition: test_hooks.c:188
static amxc_aqueue_t expected_order
Definition: test_hooks.c:82
static void test_hook_end_include(UNUSED amxo_parser_t *parser, UNUSED const char *file)
Definition: test_hooks.c:110
#define UNUSED
Definition: test_issue_48.c:84