libamxo  4.3.4
Object Definition Language (ODL) parsing
test_resolvers.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 <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <inttypes.h>
65 #include <limits.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <cmocka.h>
69 
70 #include <amxc/amxc.h>
71 #include <amxp/amxp_signal.h>
72 #include <amxd/amxd_dm.h>
73 #include <amxd/amxd_object.h>
74 #include <amxd/amxd_parameter.h>
75 #include <amxo/amxo.h>
76 #include <amxo/amxo_hooks.h>
77 
78 #include "test_resolvers.h"
79 
80 #include <amxc/amxc_macros.h>
81 static const char* priv_data = "PRIVATE";
82 
83 static void test_resolver_get(amxo_parser_t* parser,
84  void* priv) {
85  amxc_var_t* config = amxo_parser_claim_config(parser, "test-data");
86  assert_ptr_not_equal(config, NULL);
87  amxc_var_set(int32_t, config, 666);
88  assert_ptr_equal(priv, &priv_data);
89 }
90 
92  UNUSED const char* fn_name,
94  UNUSED const char* data,
95  void* priv) {
96  amxc_var_t* config = amxo_parser_get_config(parser, "test-data");
97  assert_ptr_not_equal(config, NULL);
98  assert_int_equal(amxc_var_constcast(int32_t, config), 666);
99  assert_ptr_equal(priv, &priv_data);
100 
101  return NULL;
102 }
103 
104 static void test_resolver_clean(amxo_parser_t* parser,
105  void* priv) {
106  amxc_var_t* config = amxo_parser_get_config(parser, "test-data");
107  assert_ptr_not_equal(config, NULL);
108  assert_int_equal(amxc_var_constcast(int32_t, config), 666);
109  assert_ptr_equal(priv, &priv_data);
110 }
111 
114  .resolve = test_resolver_resolve,
115  .clean = test_resolver_clean,
116  .priv = &priv_data
117 };
118 
121  .resolve = NULL,
122  .clean = test_resolver_clean,
123  .priv = &priv_data
124 };
125 
126 void test_register_resolver(UNUSED void** state) {
127  amxd_dm_t dm;
128  amxo_parser_t parser;
129  const char* odls[] = {
130  "%define { object Test { void Func(); } }",
131  "%define { object Test { void Func()<!test:data!>; } }",
132  NULL
133  };
134 
135  amxd_dm_init(&dm);
136 
137  assert_int_not_equal(amxo_register_resolver("ftab", &test_resolver), 0);
138  assert_int_not_equal(amxo_register_resolver("auto", &test_resolver), 0);
139  assert_int_not_equal(amxo_register_resolver("import", &test_resolver), 0);
140  assert_int_not_equal(amxo_register_resolver("import2", NULL), 0);
141  assert_int_not_equal(amxo_register_resolver("My.Resolver", &test_resolver), 0);
142  assert_int_not_equal(amxo_register_resolver("test", &invalid_resolver), 0);
143  assert_int_equal(amxo_register_resolver("test", &test_resolver), 0);
144 
145  amxo_parser_init(&parser);
146 
147  for(int i = 0; odls[i] != NULL; i++) {
148  assert_int_equal(amxo_parser_parse_string(&parser, odls[i], amxd_dm_get_root(&dm)), 0);
149  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
150  amxd_dm_clean(&dm);
151  }
152 
153  assert_int_equal(amxo_unregister_resolver("test"), 0);
154  assert_int_not_equal(amxo_unregister_resolver("test"), 0);
155 
156  amxo_parser_clean(&parser);
157  amxd_dm_clean(&dm);
158 }
159 
161  amxd_dm_t dm;
162  amxo_parser_t parser;
163  const char* odls[] = {
164  "%define { object Test { void Func()<!!>; } }",
165  "%define { object Test { void Func()<!:data!>; } }",
166  "%define { object Test { void Func()<! :data!>; } }",
167  "%define { object Test { void Func()<!invalid-resolver:data!>; } }",
168  NULL
169  };
170 
171  amxd_dm_init(&dm);
172  amxo_parser_init(&parser);
173 
174  for(int i = 0; odls[i] != NULL; i++) {
175  assert_int_not_equal(amxo_parser_parse_string(&parser, odls[i], amxd_dm_get_root(&dm)), 0);
176  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_invalid_name);
177  amxd_dm_clean(&dm);
178  }
179 
180  amxo_parser_clean(&parser);
181  amxd_dm_clean(&dm);
182 }
183 
185  amxd_dm_t dm;
186  amxo_parser_t parser;
187  amxc_var_t order;
188  const char* odls[] = {
189  "%define { object Test { void Func(); } }",
190  "%define { object Test { void Func()<!test:data!>; } }",
191  "%config { resolver = 'test'; } %define { object Test { void Func()<!${resolver}:data!>; } }",
192  NULL
193  };
194 
195  assert_int_equal(amxo_register_resolver("test", &test_resolver), 0);
196 
197  amxd_dm_init(&dm);
198  amxo_parser_init(&parser);
199  amxc_var_init(&order);
200  amxc_var_set_type(&order, AMXC_VAR_ID_LIST);
201  amxo_parser_set_config(&parser, "auto-resolver-order", &order);
202  amxc_var_clean(&order);
203 
204  for(int i = 0; odls[i] != NULL; i++) {
205  assert_int_equal(amxo_parser_parse_string(&parser, odls[i], amxd_dm_get_root(&dm)), 0);
206  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
207  amxd_dm_clean(&dm);
208  }
209 
210  assert_int_equal(amxo_unregister_resolver("test"), 0);
211 
212  amxo_parser_clean(&parser);
213  amxd_dm_clean(&dm);
214 }
Ambiorix ODL parser header file.
void(* amxo_fn_ptr_t)(void)
Definition: amxo_types.h:71
enum _amxo_fn_type amxo_fn_type_t
config
amxc_var_t * amxo_parser_get_config(amxo_parser_t *parser, const char *path)
Gets a configuration option.
int amxo_parser_set_config(amxo_parser_t *parser, const char *path, amxc_var_t *value)
Sets a configuration option.
amxc_var_t * amxo_parser_claim_config(amxo_parser_t *parser, const char *path)
Gets or creates 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.
int amxo_unregister_resolver(const char *name)
Unregisters a function resolver.
int amxo_register_resolver(const char *name, amxo_resolver_t *resolver)
Registers a function resolver.
The ODL parser structure.
Definition: amxo_types.h:245
amxo_res_get_default_t get
Definition: amxo_types.h:102
#define UNUSED
Definition: test_issue_48.c:84
static const char * priv_data
static amxo_resolver_t invalid_resolver
void test_register_resolver(UNUSED void **state)
void test_auto_resolver_order_no_any(UNUSED void **state)
static amxo_resolver_t test_resolver
void test_invalid_resolver_names(UNUSED void **state)
static amxo_fn_ptr_t test_resolver_resolve(amxo_parser_t *parser, UNUSED const char *fn_name, UNUSED amxo_fn_type_t type, UNUSED const char *data, void *priv)
static void test_resolver_clean(amxo_parser_t *parser, void *priv)
static void test_resolver_get(amxo_parser_t *parser, void *priv)