libamxd  6.4.1
Data Model Manager
test_amxd_function_arg.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 
64 #include <amxc/amxc.h>
65 #include <amxp/amxp_signal.h>
66 #include <amxp/amxp_slot.h>
67 
68 #include <amxd/amxd_common.h>
69 #include <amxd/amxd_dm.h>
70 #include <amxd/amxd_object.h>
71 #include <amxd/amxd_function.h>
72 
73 #include "test_amxd_function.h"
74 
75 #include <amxc/amxc_macros.h>
77  amxd_function_t* func,
78  amxc_var_t* args,
79  amxc_var_t* ret) {
80  assert_ptr_not_equal(object, NULL);
81  assert_ptr_not_equal(func, NULL);
82  assert_ptr_not_equal(args, NULL);
83  assert_ptr_not_equal(ret, NULL);
84  assert_int_equal(amxc_var_type_of(args), AMXC_VAR_ID_HTABLE);
85 
86  return 0;
87 }
88 
89 void test_amxd_function_new_del_arg(UNUSED void** state) {
90  amxd_function_t* function = NULL;
91  amxc_var_t def_val;
92  amxc_var_init(&def_val);
93  amxc_var_set(uint32_t, &def_val, 10);
94 
95  assert_int_equal(amxd_function_new(&function, "test_func", AMXC_VAR_ID_ANY, test_func), 0);
96  assert_int_equal(amxd_function_new_arg(function, "arg1", AMXC_VAR_ID_UINT32, NULL), 0);
97  assert_int_equal(amxd_function_new_arg(function, "arg2", AMXC_VAR_ID_UINT32, &def_val), 0);
98 
99  assert_int_not_equal(amxd_function_new_arg(function, "", AMXC_VAR_ID_CSTRING, NULL), 0);
100  assert_int_not_equal(amxd_function_new_arg(function, NULL, AMXC_VAR_ID_CSTRING, NULL), 0);
101  assert_int_equal(amxd_function_new_arg(function, "123", AMXC_VAR_ID_CSTRING, NULL), 0);
102  assert_int_not_equal(amxd_function_new_arg(NULL, "arg3", AMXC_VAR_ID_CSTRING, NULL), 0);
103  assert_int_not_equal(amxd_function_new_arg(function, "arg2", AMXC_VAR_ID_CSTRING, NULL), 0);
104  assert_int_equal(amxc_llist_size(&function->args), 3);
105 
106  amxd_function_del_arg(function, "arg1");
107  amxd_function_del_arg(NULL, "arg1");
108  amxd_function_del_arg(function, "");
109  amxd_function_del_arg(function, NULL);
110  amxd_function_del_arg(function, "blabla");
111  amxd_function_del_arg(function, "arg1");
112  assert_int_equal(amxc_llist_size(&function->args), 2);
113 
114  amxc_var_clean(&def_val);
115  amxd_function_delete(&function);
116 }
117 
118 void test_amxd_function_arg_attributes(UNUSED void** state) {
119  amxd_function_t* function = NULL;
120 
121  assert_int_equal(amxd_function_new(&function, "templ_func", AMXC_VAR_ID_CSTRING, test_func), 0);
122  assert_int_equal(amxd_function_new_arg(function, "arg1", AMXC_VAR_ID_UINT32, NULL), 0);
123 
124  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_in));
125  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_out));
126  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_mandatory));
127  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_strict));
128 
129  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_in, true), 0);
130  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_in));
131  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_in, false), 0);
132  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_in));
133 
134  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_out, true), 0);
135  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_out));
136  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_out, false), 0);
137  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_out));
138 
139  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_mandatory, true), 0);
140  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_mandatory));
141  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_mandatory, false), 0);
142  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_mandatory));
143 
144  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_strict, true), 0);
145  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_strict));
146  assert_int_equal(amxd_function_arg_set_attr(function, "arg1", amxd_aattr_strict, false), 0);
147  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_strict));
148 
149  assert_int_equal(amxd_function_arg_set_attrs(function, "arg1", SET_BIT(amxd_aattr_in) |
152  SET_BIT(amxd_aattr_strict), true), 0);
153  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_in));
154  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_out));
155  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_mandatory));
156  assert_true(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_strict));
157 
158  assert_int_equal(amxd_function_arg_set_attrs(function, "arg1", SET_BIT(amxd_aattr_in) |
161  SET_BIT(amxd_aattr_strict), false), 0);
162  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_in));
163  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_out));
164  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_mandatory));
165  assert_false(amxd_function_arg_is_attr_set(function, "arg1", amxd_aattr_strict));
166 
167  amxd_function_arg_set_attr(NULL, "arg1", amxd_aattr_strict, true);
168  amxd_function_arg_set_attr(function, NULL, amxd_aattr_strict, true);
169  assert_false(amxd_function_arg_is_attr_set(NULL, "arg1", amxd_aattr_mandatory));
170  assert_false(amxd_function_arg_is_attr_set(function, NULL, amxd_aattr_mandatory));
171 
172  amxd_function_delete(&function);
173 }
#define SET_BIT(x)
Definition: amxd_common.h:65
Ambiorix Data Model API header file.
Ambiorix Data Model RPC methods API header file.
Ambiorix Data Model API header file.
enum _amxd_status amxd_status_t
@ amxd_aattr_strict
Definition: amxd_types.h:286
@ amxd_aattr_in
Definition: amxd_types.h:283
@ amxd_aattr_mandatory
Definition: amxd_types.h:285
@ amxd_aattr_out
Definition: amxd_types.h:284
void amxd_function_delete(amxd_function_t **func)
Data model RPC method destructor function.
amxd_status_t amxd_function_arg_set_attr(amxd_function_t *const func, const char *name, const amxd_aattr_id_t attr, const bool enable)
Sets or unsets a method argument attribute.
bool amxd_function_arg_is_attr_set(const amxd_function_t *const func, const char *name, const amxd_aattr_id_t attr)
Checks if a method argument attribute is set.
amxd_status_t amxd_function_new(amxd_function_t **func, const char *name, const uint32_t ret_type, amxd_object_fn_t impl)
Data model RPC method constructor function.
void amxd_function_del_arg(amxd_function_t *func, const char *name)
Removes an argument definition from a RPC method definition.
amxd_status_t amxd_function_new_arg(amxd_function_t *func, const char *name, const uint32_t type, amxc_var_t *default_value)
Adds an argument definition to a RPC method definition.
amxd_status_t amxd_function_arg_set_attrs(amxd_function_t *func, const char *name, const uint32_t bitmask, bool enable)
Sets or unsets method argument attributes using a bitmap.
RPC method structure.
Definition: amxd_types.h:341
void test_amxd_function_new_del_arg(UNUSED void **state)
void test_amxd_function_arg_attributes(UNUSED void **state)
static amxd_status_t test_func(amxd_object_t *object, amxd_function_t *func, amxc_var_t *args, amxc_var_t *ret)