libamxd  6.4.1
Data Model Manager
test_amxd_dm.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_dm.h>
69 #include <amxd/amxd_object.h>
70 
71 #include "test_amxd_dm.h"
72 
73 #include <amxc/amxc_macros.h>
74 static amxd_dm_t dm;
75 
76 static const char* expected_signal = NULL;
77 
78 static void test_amxd_root_signals(const char* const sig_name,
79  const amxc_var_t* const data,
80  UNUSED void* const priv) {
81  amxc_var_t* object_path = NULL;
82  assert_string_equal(sig_name, expected_signal);
83  object_path = amxc_var_get_key(data, "object", AMXC_VAR_FLAG_DEFAULT);
84  assert_string_equal(amxc_var_constcast(cstring_t, object_path), "test_object");
85 
86 }
87 
88 void test_amxd_dm_init_clean(UNUSED void** state) {
89  assert_int_equal(amxd_dm_init(&dm), 0);
90  amxd_dm_clean(&dm);
91 
92  assert_int_not_equal(amxd_dm_init(NULL), 0);
93  amxd_dm_clean(NULL);
94 }
95 
96 void test_amxd_dm_new_delete(UNUSED void** state) {
97  amxd_dm_t* dm = NULL;
98 
99  assert_int_equal(amxd_dm_new(&dm), 0);
100  assert_int_not_equal(amxd_dm_new(&dm), 0);
101  amxd_dm_delete(&dm);
102  assert_ptr_equal(dm, NULL);
103  amxd_dm_delete(&dm);
104 
105  assert_int_not_equal(amxd_dm_new(NULL), 0);
106  amxd_dm_delete(NULL);
107 }
108 
109 void test_amxd_dm_get_root(UNUSED void** state) {
110  assert_int_equal(amxd_dm_init(&dm), 0);
111 
112  assert_ptr_equal(amxd_dm_get_root(&dm), &dm.object);
113  assert_ptr_equal(amxd_dm_get_root(NULL), NULL);
114 
115  amxd_dm_clean(&dm);
116 }
117 
118 void test_amxd_dm_add_root_object(UNUSED void** state) {
119  amxd_object_t* object = NULL;
120 
121  assert_int_equal(amxd_dm_init(&dm), 0);
122 
123  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "test_object"), 0);
124  assert_ptr_not_equal(object, NULL);
125 
126  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
127  assert_int_equal(amxc_llist_size(&dm.object.objects), 1);
128  assert_ptr_equal(object->it.llist, &dm.object.objects);
129 
130  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "test_object"), 0);
131  assert_int_not_equal(amxd_dm_add_root_object(&dm, object), 0);
132  amxd_object_delete(&object);
133 
134  assert_int_not_equal(amxd_dm_add_root_object(&dm, NULL), 0);
135  assert_int_not_equal(amxd_dm_add_root_object(NULL, NULL), 0);
136 
137  amxd_dm_clean(&dm);
138 }
139 
140 void test_amxd_dm_remove_root_object(UNUSED void** state) {
141  amxd_object_t* object = NULL;
142 
143  assert_int_equal(amxd_dm_init(&dm), 0);
144 
145  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "test_object"), 0);
146  assert_ptr_not_equal(object, NULL);
147 
148  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
149  assert_int_equal(amxc_llist_size(&dm.object.objects), 1);
150  assert_ptr_equal(object->it.llist, &dm.object.objects);
151 
152  object = NULL;
153  assert_int_equal(amxd_dm_remove_root_object(&dm, "test_object"), 0);
154  assert_int_equal(amxc_llist_size(&dm.object.objects), 0);
155  assert_int_not_equal(amxd_dm_remove_root_object(&dm, ""), 0);
156  assert_int_not_equal(amxd_dm_remove_root_object(&dm, NULL), 0);
157  assert_int_not_equal(amxd_dm_remove_root_object(NULL, NULL), 0);
158 
159  amxd_dm_clean(&dm);
160 }
161 
162 void test_amxd_dm_root_object_signals(UNUSED void** state) {
163  amxd_object_t* object = NULL;
164 
165  assert_int_equal(amxd_dm_init(&dm), 0);
166 
167  amxp_slot_connect(&dm.sigmngr, "dm:root-added", NULL, test_amxd_root_signals, NULL);
168  amxp_slot_connect(&dm.sigmngr, "dm:root-removed", NULL, test_amxd_root_signals, NULL);
169 
170  expected_signal = "dm:root-added";
171  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "test_object"), 0);
172  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
173 
174  assert_int_equal(amxp_signal_read(), 0);
175 
176  expected_signal = "dm:root-removed";
177  assert_int_equal(amxd_dm_remove_root_object(&dm, "test_object"), 0);
178 
179  amxd_dm_clean(&dm);
180 }
181 
182 void test_amxd_dm_error_string(UNUSED void** state) {
183  assert_string_equal(amxd_status_string(amxd_status_ok), "ok");
184  assert_string_equal(amxd_status_string(amxd_status_invalid_action), "invalid action");
185  assert_string_equal(amxd_status_string(amxd_status_last + 1), "unknown error");
186  assert_string_equal(amxd_status_string(amxd_status_last), "last");
187 }
const char * amxd_status_string(const amxd_status_t status)
Definition: amxd_common.c:134
Ambiorix Data Model API header file.
Ambiorix Data Model API header file.
@ amxd_status_last
Definition: amxd_types.h:106
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_status_invalid_action
Definition: amxd_types.h:89
@ amxd_object_singleton
Definition: amxd_types.h:181
amxd_object_t * amxd_dm_get_root(amxd_dm_t *const dm)
Fetches the root object of the data model.
Definition: amxd_dm.c:456
amxd_status_t amxd_dm_new(amxd_dm_t **dm)
Instantiate a new data model.
Definition: amxd_dm.c:306
amxd_status_t amxd_dm_remove_root_object(amxd_dm_t *const dm, const char *name)
Removes an object from the root of the data model.
Definition: amxd_dm.c:433
void amxd_dm_delete(amxd_dm_t **dm)
Deletes a data model structure.
Definition: amxd_dm.c:321
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_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 amxd_object_delete(amxd_object_t **object)
Invokes the destroy handler(s) of the object.
amxd_object_t object
Definition: amxd_types.h:260
amxp_signal_mngr_t sigmngr
Definition: amxd_types.h:261
amxc_llist_t objects
Definition: amxd_types.h:243
amxc_llist_it_t it
Definition: amxd_types.h:230
static const char * expected_signal
Definition: test_amxd_dm.c:76
void test_amxd_dm_root_object_signals(UNUSED void **state)
Definition: test_amxd_dm.c:162
void test_amxd_dm_new_delete(UNUSED void **state)
Definition: test_amxd_dm.c:96
static amxd_dm_t dm
Definition: test_amxd_dm.c:74
static void test_amxd_root_signals(const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
Definition: test_amxd_dm.c:78
void test_amxd_dm_add_root_object(UNUSED void **state)
Definition: test_amxd_dm.c:118
void test_amxd_dm_error_string(UNUSED void **state)
Definition: test_amxd_dm.c:182
void test_amxd_dm_init_clean(UNUSED void **state)
Definition: test_amxd_dm.c:88
void test_amxd_dm_get_root(UNUSED void **state)
Definition: test_amxd_dm.c:109
void test_amxd_dm_remove_root_object(UNUSED void **state)
Definition: test_amxd_dm.c:140