libamxo  4.3.4
Object Definition Language (ODL) parsing
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_config_section.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_config_section.h"
79 
80 #include <amxc/amxc_macros.h>
81 void test_parsing_array(UNUSED void** state) {
82  amxd_dm_t dm;
83  amxo_parser_t parser;
84  const char* odls[] = {
85  "%config { MyOption = [ 1, 2, 3 ]; }",
86  "%config { MyOption = [ \"1\", \"2\", \"3\" ]; }",
87  "%config { MyOption = [ word1, word2, word3 ]; }",
88  "%config { MyOption = [ true, false, true ]; }",
89  "%config { MyOption = [ ]; }",
90  NULL
91  };
92 
93  amxd_dm_init(&dm);
94  amxo_parser_init(&parser);
95 
96  for(int i = 0; odls[i] != NULL; i++) {
97  amxc_var_t* option = NULL;
98  const amxc_llist_t* list = NULL;
99  assert_int_equal(amxo_parser_parse_string(&parser, odls[i], amxd_dm_get_root(&dm)), 0);
100  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
101  option = amxo_parser_get_config(&parser, "MyOption");
102  assert_ptr_not_equal(option, NULL);
103  assert_int_equal(amxc_var_type_of(option), AMXC_VAR_ID_LIST);
104  list = amxc_var_constcast(amxc_llist_t, option);
105  assert_ptr_not_equal(list, NULL);
106  switch(i) {
107  case 0:
108  assert_int_equal(amxc_llist_size(list), 3);
109  amxc_llist_for_each(it, list) {
110  amxc_var_t* item = amxc_var_from_llist_it(it);
111  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_INT64);
112  }
113  break;
114  case 1:
115  case 2:
116  assert_int_equal(amxc_llist_size(list), 3);
117  amxc_llist_for_each(it, list) {
118  amxc_var_t* item = amxc_var_from_llist_it(it);
119  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_CSTRING);
120  }
121  break;
122  case 3:
123  assert_int_equal(amxc_llist_size(list), 3);
124  amxc_llist_for_each(it, list) {
125  amxc_var_t* item = amxc_var_from_llist_it(it);
126  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_BOOL);
127  }
128  break;
129  case 4:
130  assert_true(amxc_llist_is_empty(list));
131  break;
132  }
133  }
134 
135  amxo_parser_clean(&parser);
136  amxd_dm_clean(&dm);
137 }
138 
140  amxd_dm_t dm;
141  amxo_parser_t parser;
142  const char* odls[] = {
143  "%config { MyOption = { Key1 = 1, Key2 = 2, Key3 = 3 }; }",
144  "%config { MyOption = { Key1 = \"1\", Key2 = \"2\", Key3 = \"3\" }; }",
145  "%config { MyOption = { Key1 = word1, Key2 = word2, Key3 = word3 }; }",
146  "%config { MyOption = { Key1 = true, Key2 = false, Key3 = true }; }",
147  "%config { MyOption = { }; }",
148  "%config { MyOption = { \"'Test.Key1.'\" = 1, \"'Test.Key2.'\" = 2, \"'Test.Key3.'\" = 3 }; }",
149  NULL
150  };
151 
152  amxd_dm_init(&dm);
153  amxo_parser_init(&parser);
154 
155  for(int i = 0; odls[i] != NULL; i++) {
156  amxc_var_t* option = NULL;
157  const amxc_htable_t* table = NULL;
158  printf("Parsing string %s\n", odls[i]);
159  assert_int_equal(amxo_parser_parse_string(&parser, odls[i], amxd_dm_get_root(&dm)), 0);
160  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
161  option = amxo_parser_get_config(&parser, "MyOption");
162  assert_ptr_not_equal(option, NULL);
163  amxc_var_dump(option, STDOUT_FILENO);
164  assert_int_equal(amxc_var_type_of(option), AMXC_VAR_ID_HTABLE);
165  table = amxc_var_constcast(amxc_htable_t, option);
166  assert_ptr_not_equal(table, NULL);
167  switch(i) {
168  case 0:
169  case 5:
170  assert_int_equal(amxc_htable_size(table), 3);
171  amxc_htable_for_each(it, table) {
172  amxc_var_t* item = amxc_var_from_htable_it(it);
173  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_INT64);
174  }
175  break;
176  case 1:
177  case 2:
178  assert_int_equal(amxc_htable_size(table), 3);
179  amxc_htable_for_each(it, table) {
180  amxc_var_t* item = amxc_var_from_htable_it(it);
181  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_CSTRING);
182  }
183  break;
184  case 3:
185  assert_int_equal(amxc_htable_size(table), 3);
186  amxc_htable_for_each(it, table) {
187  amxc_var_t* item = amxc_var_from_htable_it(it);
188  assert_int_equal(amxc_var_type_of(item), AMXC_VAR_ID_BOOL);
189  }
190  break;
191  case 4:
192  assert_true(amxc_htable_is_empty(table));
193  break;
194  }
195  }
196 
197  amxo_parser_clean(&parser);
198  amxd_dm_clean(&dm);
199 }
200 
202  amxd_dm_t dm;
203  amxo_parser_t parser;
204  amxc_var_t* setting = NULL;
205  const char* odls[] = {
206  "%config { Key1 = 1; Key2 = 2; Key3 = 3; } include \"global_config.odl\";",
207  NULL
208  };
209 
210  amxd_dm_init(&dm);
211  amxo_parser_init(&parser);
212 
213  assert_int_equal(amxo_parser_parse_string(&parser, odls[0], amxd_dm_get_root(&dm)), 0);
214  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
215  amxc_var_dump(&parser.config, STDOUT_FILENO);
216 
217  setting = amxo_parser_get_config(&parser, "local_setting");
218  assert_ptr_equal(setting, NULL);
219  assert_true(amxc_var_is_null(setting));
220  amxc_var_delete(&setting);
221 
222  setting = amxo_parser_get_config(&parser, "Key1");
223  assert_ptr_not_equal(setting, NULL);
224  assert_false(amxc_var_is_null(setting));
225  amxc_var_dump(setting, STDOUT_FILENO);
226  assert_int_equal(amxc_var_dyncast(uint32_t, setting), 1);
227 
228  setting = amxo_parser_get_config(&parser, "global_setting");
229  assert_ptr_not_equal(setting, NULL);
230  assert_false(amxc_var_is_null(setting));
231  assert_string_equal(amxc_var_constcast(cstring_t, setting), "global test");
232 
233  amxo_parser_clean(&parser);
234  amxd_dm_clean(&dm);
235 }
236 
238  amxd_dm_t dm;
239  amxo_parser_t parser;
240  amxc_var_t* setting = NULL;
241  const char* odls[] = {
242  "%config { MyOption.SubOption.Test = \"Hallo\"; MyOption.SubOption.Value = 1; }",
243  NULL
244  };
245 
246  amxd_dm_init(&dm);
247  amxo_parser_init(&parser);
248 
249  assert_int_equal(amxo_parser_parse_string(&parser, odls[0], amxd_dm_get_root(&dm)), 0);
250  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
251  amxc_var_dump(&parser.config, STDOUT_FILENO);
252 
253  setting = amxo_parser_get_config(&parser, "MyOption");
254  assert_non_null(setting);
255  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_HTABLE);
256 
257  setting = amxo_parser_get_config(&parser, "MyOption.SubOption");
258  assert_non_null(setting);
259  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_HTABLE);
260 
261  setting = amxo_parser_get_config(&parser, "MyOption.SubOption.Test");
262  assert_non_null(setting);
263  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_CSTRING);
264 
265  setting = amxo_parser_get_config(&parser, "MyOption.SubOption.Value");
266  assert_non_null(setting);
267  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_INT64);
268 
269  amxo_parser_clean(&parser);
270  amxd_dm_clean(&dm);
271 }
272 
274  amxd_dm_t dm;
275  amxo_parser_t parser;
276  amxc_var_t* setting = NULL;
277  const char* odls[] = {
278  "%config { MyOption = { SubOption.Test = \"Hallo\", SubOption.Value = 1 }; }",
279  NULL
280  };
281 
282  amxd_dm_init(&dm);
283  amxo_parser_init(&parser);
284 
285  assert_int_equal(amxo_parser_parse_string(&parser, odls[0], amxd_dm_get_root(&dm)), 0);
286  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
287  amxc_var_dump(&parser.config, STDOUT_FILENO);
288 
289  setting = amxo_parser_get_config(&parser, "MyOption");
290  assert_non_null(setting);
291  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_HTABLE);
292 
293  setting = amxo_parser_get_config(&parser, "MyOption.SubOption");
294  assert_non_null(setting);
295  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_HTABLE);
296 
297  setting = amxo_parser_get_config(&parser, "MyOption.SubOption.Test");
298  assert_non_null(setting);
299  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_CSTRING);
300 
301  setting = amxo_parser_get_config(&parser, "MyOption.SubOption.Value");
302  assert_non_null(setting);
303  assert_int_equal(amxc_var_type_of(setting), AMXC_VAR_ID_INT64);
304 
305  amxo_parser_clean(&parser);
306  amxd_dm_clean(&dm);
307 }
Ambiorix ODL parser header file.
amxc_var_t * amxo_parser_get_config(amxo_parser_t *parser, const char *path)
Gets a configuration option.
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.
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.
The ODL parser structure.
Definition: amxo_types.h:245
amxc_var_t config
Definition: amxo_types.h:250
void test_can_set_configuration_using_path(UNUSED void **state)
void test_parsing_key_value_pairs(UNUSED void **state)
void test_can_set_configuration_data_using_path(UNUSED void **state)
void test_global_setting_are_made_available_in_main_odl(UNUSED void **state)
void test_parsing_array(UNUSED void **state)
#define UNUSED
Definition: test_issue_48.c:84