libamxd  6.4.1
Data Model Manager
test_amxd_object_expression.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 
55 #include <stdlib.h>
56 #include <stdarg.h>
57 #include <stddef.h>
58 #include <setjmp.h>
59 #include <fcntl.h>
60 #include <unistd.h>
61 #include <signal.h>
62 #include <cmocka.h>
63 #include <unistd.h>
64 #include <signal.h>
65 #include <sys/signalfd.h>
66 #include <string.h>
67 
68 #include <amxc/amxc.h>
69 #include <amxp/amxp_signal.h>
70 #include <amxp/amxp_slot.h>
71 #include <amxp/amxp_expression.h>
72 
73 #include <amxd/amxd_common.h>
74 #include <amxd/amxd_dm.h>
75 #include <amxd/amxd_object.h>
77 
79 
80 #include <amxc/amxc_macros.h>
81 static amxd_dm_t dm;
82 
83 static void test_build_dm(void) {
84  amxd_object_t* object = NULL;
85  amxd_object_t* instance = NULL;
86  amxd_object_t* child_object = NULL;
87  amxd_param_t* param = NULL;
88  amxc_var_t data;
89  amxc_var_t* str = NULL;
90  amxc_var_t* nmbr = NULL;
91 
92  amxc_var_init(&data);
93  amxc_var_set_type(&data, AMXC_VAR_ID_HTABLE);
94  str = amxc_var_add_key(cstring_t, &data, "Param1", "TestData");
95  amxc_var_add_key(bool, &data, "Param2", true);
96  nmbr = amxc_var_add_key(uint32_t, &data, "Param3", 666);
97 
98  assert_int_equal(amxd_dm_init(&dm), 0);
99 
100  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "MyObject"), 0);
101  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
102 
103  assert_int_equal(amxd_object_new(&child_object, amxd_object_singleton, "ChildObject1"), 0);
104  assert_int_equal(amxd_object_add_object(object, child_object), 0);
105 
106  assert_int_equal(amxd_param_new(&param, "Param1", AMXC_VAR_ID_CSTRING), 0);
107  assert_int_equal(amxd_object_add_param(child_object, param), 0);
108  assert_int_equal(amxd_param_new(&param, "Param2", AMXC_VAR_ID_BOOL), 0);
109  assert_int_equal(amxd_object_add_param(child_object, param), 0);
110  assert_int_equal(amxd_param_new(&param, "Param3", AMXC_VAR_ID_UINT32), 0);
111  assert_int_equal(amxd_object_add_param(child_object, param), 0);
112  assert_int_equal(amxd_object_set_params(child_object, &data), 0);
113 
114  assert_int_equal(amxd_object_new(&child_object, amxd_object_template, "ChildObject2"), 0);
115  assert_int_equal(amxd_object_add_object(object, child_object), 0);
116 
117  assert_int_equal(amxd_param_new(&param, "Param1", AMXC_VAR_ID_CSTRING), 0);
118  assert_int_equal(amxd_object_add_param(child_object, param), 0);
119  assert_int_equal(amxd_param_new(&param, "Param2", AMXC_VAR_ID_BOOL), 0);
120  assert_int_equal(amxd_object_add_param(child_object, param), 0);
121  assert_int_equal(amxd_param_new(&param, "Param3", AMXC_VAR_ID_UINT32), 0);
122  assert_int_equal(amxd_object_add_param(child_object, param), 0);
123 
124  assert_int_equal(amxd_object_new_instance(&instance, child_object, NULL, 0, NULL), 0);
125  assert_int_equal(amxd_object_set_params(instance, &data), 0);
126 
127  assert_int_equal(amxd_object_new_instance(&instance, child_object, NULL, 0, NULL), 0);
128  amxc_var_set(cstring_t, str, "Hello");
129  amxc_var_set(uint32_t, nmbr, 1234);
130  assert_int_equal(amxd_object_set_params(instance, &data), 0);
131 
132  assert_int_equal(amxd_object_new_instance(&instance, child_object, NULL, 0, NULL), 0);
133  amxc_var_set(cstring_t, str, "World");
134  amxc_var_set(uint32_t, nmbr, 2020);
135  assert_int_equal(amxd_object_set_params(instance, &data), 0);
136 
137  assert_int_equal(amxd_object_new_instance(&instance, child_object, NULL, 0, NULL), 0);
138  amxc_var_set(cstring_t, str, "Hello");
139  amxc_var_set(uint32_t, nmbr, 2020);
140  assert_int_equal(amxd_object_set_params(instance, &data), 0);
141 
142  amxc_var_clean(&data);
143 }
144 
145 int test_object_expression_setup(UNUSED void** state) {
146  test_build_dm();
147  return 0;
148 }
149 
150 int test_object_expression_teardown(UNUSED void** state) {
151  amxd_dm_clean(&dm);
152 
153  return 0;
154 }
155 
156 void test_can_match_object_with_expression(UNUSED void** state) {
157  amxd_object_t* object = NULL;
158  amxp_expr_t expression;
159 
160  assert_int_equal(amxp_expr_init(&expression, "Param1==\"TestData\" && Param3 == 666"), 0);
161  object = amxd_dm_findf(&dm, "MyObject.ChildObject1");
162  assert_true(amxd_object_matches_expr(object, &expression));
163 
164  object = amxd_dm_findf(&dm, "MyObject.ChildObject2.1");
165  assert_true(amxd_object_matches_expr(object, &expression));
166 
167  object = amxd_dm_findf(&dm, "MyObject.ChildObject2.2");
168  assert_false(amxd_object_matches_expr(object, &expression));
169 
170  object = amxd_dm_findf(&dm, "MyObject.ChildObject2.3");
171  assert_false(amxd_object_matches_expr(object, &expression));
172 
173  amxp_expr_clean(&expression);
174 }
175 
177  amxd_object_t* object = NULL;
178  amxp_expr_t expression;
179 
180  object = amxd_dm_findf(&dm, "MyObject");
181 
182  assert_int_equal(amxp_expr_init(&expression, "ChildObject1.Param1==\"TestData\" && ChildObject1.Param3 == 666"), 0);
183  assert_true(amxd_object_matches_expr(object, &expression));
184  amxp_expr_clean(&expression);
185 
186  assert_int_equal(amxp_expr_init(&expression, "ChildObject2.1.Param9==\"TestData\" && ChildObject2.1.Param3 == 666"), 0);
187  assert_false(amxd_object_matches_expr(object, &expression));
188  amxp_expr_clean(&expression);
189 
190  assert_int_equal(amxp_expr_init(&expression, "ChildObject2.1.Param1==\"TestData\" && ChildObject2.1.Param3 == 666"), 0);
191  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
192  assert_false(amxd_object_matches_expr(object, &expression));
193  amxp_expr_clean(&expression);
194 }
195 
196 void test_expression_has_matching_instances(UNUSED void** state) {
197  amxd_object_t* object = NULL;
198  amxp_expr_t expression;
199 
200  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
201 
202  assert_int_equal(amxp_expr_init(&expression, "Param1==\"Hello\" && Param3 > 1000"), 0);
203  assert_true(amxd_object_has_matching_instances(object, &expression));
204  amxp_expr_clean(&expression);
205 }
206 
207 void test_api_does_input_arg_validation(UNUSED void** state) {
208  amxd_object_t* object = NULL;
209  amxp_expr_t expression;
210  amxp_expr_t* expr = NULL;
211  amxc_var_t data;
212 
213  amxc_var_init(&data);
214 
215  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
216  assert_int_equal(amxp_expr_init(&expression, "Param1==\"Hello\" && Param3 > 1000"), 0);
217 
218  assert_false(amxd_object_matches_expr(object, NULL));
219  assert_false(amxd_object_matches_expr(NULL, &expression));
220 
221  assert_false(amxd_object_has_matching_instances(object, NULL));
222  assert_false(amxd_object_has_matching_instances(NULL, &expression));
223  object = amxd_dm_findf(&dm, "MyObject.ChildObject1");
224  assert_false(amxd_object_has_matching_instances(object, &expression));
225 
226  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
227  assert_int_not_equal(amxd_object_new_key_expr(object, NULL, NULL), 0);
228  assert_int_not_equal(amxd_object_new_key_expr(object, NULL, &data), 0);
229  assert_int_not_equal(amxd_object_new_key_expr(object, &expr, &data), 0);
230  assert_ptr_equal(expr, NULL);
231 
232  amxc_var_set_type(&data, AMXC_VAR_ID_HTABLE);
233  assert_int_not_equal(amxd_object_new_key_expr(NULL, &expr, &data), 0);
234  assert_ptr_equal(expr, NULL);
235  object = amxd_dm_findf(&dm, "MyObject.ChildObject1");
236  assert_int_not_equal(amxd_object_new_key_expr(object, &expr, &data), 0);
237  assert_ptr_equal(expr, NULL);
238 
239  amxp_expr_clean(&expression);
240  amxc_var_clean(&data);
241 }
242 
244  amxd_object_t* object = NULL;
245  amxd_object_t* instance = NULL;
246  amxp_expr_t expression;
247 
248  assert_int_equal(amxp_expr_init(&expression, "Param1==\"Hello\""), 0);
249  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
250 
251  instance = amxd_object_find_instance(object, &expression);
252  assert_ptr_not_equal(instance, NULL);
253  assert_int_equal(amxd_object_get_index(instance), 2);
254 
255  instance = amxd_object_find_next_instance(instance, &expression);
256  assert_ptr_not_equal(instance, NULL);
257  assert_int_equal(amxd_object_get_index(instance), 4);
258 
259  instance = amxd_object_find_next_instance(instance, &expression);
260  assert_ptr_equal(instance, NULL);
261 
262  instance = amxd_object_find_next_instance(instance, &expression);
263  assert_ptr_equal(instance, NULL);
264 
265  amxp_expr_clean(&expression);
266 }
267 
268 void test_can_start_path_with_dot_in_expr(UNUSED void** state) {
269  amxd_object_t* object = NULL;
270  amxp_expr_t expression;
271 
272  assert_int_equal(amxp_expr_init(&expression, ".ChildObject2.3.Param3==2020"), 0);
273  object = amxd_dm_findf(&dm, "MyObject");
274  assert_true(amxd_object_matches_expr(object, &expression));
275  amxp_expr_clean(&expression);
276 
277  assert_int_equal(amxp_expr_init(&expression, ".Param3==2020"), 0);
278  assert_false(amxd_object_matches_expr(object, &expression));
279 
280  object = amxd_dm_findf(&dm, "MyObject.ChildObject2.3");
281  assert_true(amxd_object_matches_expr(object, &expression));
282 
283  object = amxd_dm_findf(&dm, "MyObject.ChildObject2");
284  assert_ptr_not_equal(amxd_object_find_instance(object, &expression), NULL);
285 
286  amxp_expr_clean(&expression);
287 }
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
Ambiorix Data Model API header file.
bool amxd_object_matches_expr(amxd_object_t *const object, amxp_expr_t *expr)
amxd_status_t amxd_object_new_key_expr(amxd_object_t *const templ, amxp_expr_t **expr, const amxc_var_t *const data)
bool amxd_object_has_matching_instances(const amxd_object_t *const templ, amxp_expr_t *expr)
amxd_object_t * amxd_object_find_next_instance(const amxd_object_t *const instance, amxp_expr_t *expr)
amxd_object_t * amxd_object_find_instance(const amxd_object_t *const templ, amxp_expr_t *expr)
amxd_status_t amxd_param_new(amxd_param_t **param, const char *name, const uint32_t type)
@ amxd_object_template
Definition: amxd_types.h:183
@ amxd_object_singleton
Definition: amxd_types.h:181
amxd_status_t amxd_dm_add_root_object(amxd_dm_t *const dm, amxd_object_t *const object)
Adds an object to the root of the data model.
Definition: amxd_dm.c:418
amxd_status_t amxd_dm_init(amxd_dm_t *dm)
Initializes a data model structure.
Definition: amxd_dm.c:334
void amxd_dm_clean(amxd_dm_t *dm)
Cleans a data model structure.
Definition: amxd_dm.c:365
amxd_status_t amxd_object_add_object(amxd_object_t *const parent, amxd_object_t *const child)
Adds an object in the data model tree.
Definition: amxd_object.c:207
amxd_status_t amxd_object_set_params(amxd_object_t *const object, amxc_var_t *const values)
Sets multiple parameter values in a data model object.
amxd_status_t amxd_object_add_param(amxd_object_t *const object, amxd_param_t *const param)
Adds a parameter definition to an object.
uint32_t amxd_object_get_index(const amxd_object_t *const object)
Get the index of an instance object.
Definition: amxd_object.c:265
amxd_status_t amxd_object_new_instance(amxd_object_t **object, amxd_object_t *templ, const char *name, uint32_t index, amxc_var_t *values)
Data model object constructor function.
amxd_status_t amxd_object_new(amxd_object_t **object, const amxd_object_type_t type, const char *name)
Data model object constructor function.
Definition: amxd_object.c:185
void test_expression_fields_are_retrieved_in_hierarchy(UNUSED void **state)
static amxd_dm_t dm
void test_expression_has_matching_instances(UNUSED void **state)
void test_can_iterator_over_all_matching_instances(UNUSED void **state)
static void test_build_dm(void)
int test_object_expression_setup(UNUSED void **state)
void test_can_start_path_with_dot_in_expr(UNUSED void **state)
int test_object_expression_teardown(UNUSED void **state)
void test_can_match_object_with_expression(UNUSED void **state)
void test_api_does_input_arg_validation(UNUSED void **state)