libamxd  6.4.1
Data Model Manager
test_amxd_object.c File Reference
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <cmocka.h>
#include <amxc/amxc.h>
#include <amxp/amxp_signal.h>
#include <amxp/amxp_slot.h>
#include <amxd/amxd_common.h>
#include <amxd/amxd_dm.h>
#include <amxd/amxd_object.h>
#include "test_amxd_object.h"
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

void test_amxd_object_new_singleton (UNUSED void **state)
 
void test_amxd_object_new_template (UNUSED void **state)
 
void test_amxd_object_new_mib (UNUSED void **state)
 
void test_amxd_object_new_invalid_type (UNUSED void **state)
 
void test_amxd_object_new_invalid_name (UNUSED void **state)
 
void test_amxd_object_new_instance (UNUSED void **state)
 
void test_amxd_object_new_instance_invalid_name_index (UNUSED void **state)
 
void test_amxd_object_new_instance_of_singelton (UNUSED void **state)
 
void test_amxd_object_new_instance_with_children (UNUSED void **state)
 
void test_amxd_object_new_delete_invalid_args (UNUSED void **state)
 
void test_amxd_object_get_name (UNUSED void **state)
 
void test_amxd_object_get_name_of_indexed_instance (UNUSED void **state)
 
void test_amxd_object_attributes (UNUSED void **state)
 

Variables

static amxd_dm_t dm
 

Function Documentation

◆ test_amxd_object_attributes()

void test_amxd_object_attributes ( UNUSED void **  state)

Definition at line 343 of file test_amxd_object.c.

343  {
344  amxd_object_t* object = NULL;
345 
346  assert_int_equal(amxd_object_new(&object, amxd_object_template, "test_object"), 0);
347  assert_false(amxd_object_is_attr_set(object, amxd_oattr_read_only));
348  assert_false(amxd_object_is_attr_set(object, amxd_oattr_persistent));
349  assert_false(amxd_object_is_attr_set(object, amxd_oattr_private));
350  assert_false(amxd_object_is_attr_set(object, amxd_oattr_locked));
351  assert_false(amxd_object_is_attr_set(object, 999));
352 
353  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_read_only, true), 0);
354  assert_true(amxd_object_is_attr_set(object, amxd_oattr_read_only));
355  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_read_only, false), 0);
356  assert_false(amxd_object_is_attr_set(object, amxd_oattr_read_only));
357 
358  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_persistent, true), 0);
359  assert_true(amxd_object_is_attr_set(object, amxd_oattr_persistent));
360  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_persistent, false), 0);
361  assert_false(amxd_object_is_attr_set(object, amxd_oattr_persistent));
362 
363  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_private, true), 0);
364  assert_true(amxd_object_is_attr_set(object, amxd_oattr_private));
365  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_private, false), 0);
366  assert_false(amxd_object_is_attr_set(object, amxd_oattr_private));
367 
368  assert_int_not_equal(amxd_object_set_attr(object, 999, false), 0);
369  assert_false(amxd_object_is_attr_set(object, 999));
370 
371  assert_int_equal(amxd_object_set_attrs(object,
375  true), 0);
376  assert_true(amxd_object_is_attr_set(object, amxd_oattr_read_only));
377  assert_true(amxd_object_is_attr_set(object, amxd_oattr_persistent));
378  assert_true(amxd_object_is_attr_set(object, amxd_oattr_private));
379 
380  assert_int_equal(amxd_object_set_attrs(object,
384  false), 0);
385  assert_false(amxd_object_is_attr_set(object, amxd_oattr_read_only));
386  assert_false(amxd_object_is_attr_set(object, amxd_oattr_persistent));
387  assert_false(amxd_object_is_attr_set(object, amxd_oattr_private));
388 
389  assert_int_equal(amxd_object_set_attr(object, amxd_oattr_locked, true), 0);
390  assert_true(amxd_object_is_attr_set(object, amxd_oattr_locked));
391  assert_int_not_equal(amxd_object_set_attr(object, amxd_oattr_locked, false), 0);
392  assert_true(amxd_object_is_attr_set(object, amxd_oattr_locked));
393 
394  assert_int_not_equal(amxd_object_set_attrs(object,
397  SET_BIT((amxd_oattr_max + 1)),
398  true), 0);
399 
400  amxd_object_delete(&object);
401 
402  assert_int_not_equal(amxd_object_set_attrs(NULL,
406  false), 0);
407 
408  assert_false(amxd_object_is_attr_set(NULL, amxd_oattr_locked));
410 }
#define SET_BIT(x)
Definition: amxd_common.h:65
@ amxd_oattr_read_only
Definition: amxd_types.h:199
@ amxd_oattr_locked
Definition: amxd_types.h:206
@ amxd_oattr_max
Definition: amxd_types.h:211
@ amxd_oattr_persistent
Definition: amxd_types.h:200
@ amxd_oattr_private
Definition: amxd_types.h:202
@ amxd_object_template
Definition: amxd_types.h:183
amxd_status_t amxd_object_set_attr(amxd_object_t *const object, const amxd_oattr_id_t attr, const bool enable)
Sets or unsets an object attribute.
Definition: amxd_object.c:269
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.
bool amxd_object_is_attr_set(const amxd_object_t *const object, const amxd_oattr_id_t attr)
Checks if an attribute is set.
Definition: amxd_object.c:348
amxd_status_t amxd_object_set_attrs(amxd_object_t *const object, const uint32_t bitmask, bool enable)
Sets or unsets object attributes using a bitmap.
Definition: amxd_object.c:301

◆ test_amxd_object_get_name()

void test_amxd_object_get_name ( UNUSED void **  state)

Definition at line 295 of file test_amxd_object.c.

295  {
296  amxd_object_t* parent = NULL;
297  amxd_object_t* child1 = NULL;
298  amxd_object_t* child2 = NULL;
299  amxd_object_t* instance_child2 = NULL;
300  amxd_object_t* instance = NULL;
301 
302  assert_int_equal(amxd_dm_init(&dm), 0);
303 
304  assert_int_equal(amxd_object_new(&parent, amxd_object_singleton, "parent"), 0);
305  assert_int_equal(amxd_object_new(&child1, amxd_object_template, "child"), 0);
306  assert_int_equal(amxd_object_new(&child2, amxd_object_template, "sub-child"), 0);
307  assert_int_equal(amxd_dm_add_root_object(&dm, parent), 0);
308  assert_int_equal(amxd_object_add_object(parent, child1), 0);
309  assert_int_equal(amxd_object_add_object(child1, child2), 0);
310  assert_int_equal(amxd_object_new_instance(&instance, child1, "sub-child", 0, NULL), 0);
311 
312  assert_string_equal(amxd_object_get_name(parent, AMXD_OBJECT_NAMED), "parent");
313  assert_string_equal(amxd_object_get_name(child1, AMXD_OBJECT_NAMED), "child");
314  assert_string_equal(amxd_object_get_name(child2, AMXD_OBJECT_NAMED), "sub-child");
315  assert_string_equal(amxd_object_get_name(instance, AMXD_OBJECT_NAMED), "sub-child");
316  assert_string_equal(amxd_object_get_name(instance, AMXD_OBJECT_INDEXED), "1");
317  assert_ptr_equal(amxd_object_get_name(amxd_dm_get_root(&dm), AMXD_OBJECT_NAMED), NULL);
318 
319  instance_child2 = amxd_object_findf(instance, "sub-child");
320  assert_ptr_not_equal(instance_child2, NULL);
321  assert_ptr_not_equal(instance_child2, child2);
322  assert_string_equal(amxd_object_get_name(instance_child2, AMXD_OBJECT_NAMED), "sub-child");
323 
324  assert_ptr_equal(amxd_object_get_name(NULL, 0), NULL);
325 
326  amxd_dm_clean(&dm);
327 }
#define AMXD_OBJECT_INDEXED
Name and path format flag - use index for instance objects.
Definition: amxd_object.h:176
#define AMXD_OBJECT_NAMED
Name and path format flag - default behavior, use name for instance objects.
Definition: amxd_object.h:164
@ 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_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_object_t * amxd_object_findf(amxd_object_t *object, const char *rel_path,...) __attribute__((format(printf
Find an object in the data model tree, starting from an object.
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_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.
const char * amxd_object_get_name(const amxd_object_t *const object, const uint32_t flags)
Get the name of the object (or index as a string for instance objects)
Definition: amxd_object.c:239
static amxd_dm_t dm

◆ test_amxd_object_get_name_of_indexed_instance()

void test_amxd_object_get_name_of_indexed_instance ( UNUSED void **  state)

Definition at line 329 of file test_amxd_object.c.

329  {
330  amxd_object_t* parent = NULL;
331  amxd_object_t* instance = NULL;
332 
333  assert_int_equal(amxd_dm_init(&dm), 0);
334  assert_int_equal(amxd_object_new(&parent, amxd_object_template, "parent"), 0);
335  assert_int_equal(amxd_dm_add_root_object(&dm, parent), 0);
336  assert_int_equal(amxd_object_new_instance(&instance, parent, NULL, 5, NULL), 0);
337 
338  assert_string_equal(amxd_object_get_name(instance, AMXD_OBJECT_NAMED), "5");
339 
340  amxd_dm_clean(&dm);
341 }

◆ test_amxd_object_new_delete_invalid_args()

void test_amxd_object_new_delete_invalid_args ( UNUSED void **  state)

Definition at line 278 of file test_amxd_object.c.

278  {
279  amxd_object_t* template = NULL;
280  amxd_object_t* object = NULL;
281  assert_int_equal(amxd_object_new(&template, amxd_object_template, "test_object"), 0);
282  assert_ptr_not_equal(template, NULL);
283 
284  assert_int_not_equal(amxd_object_new_instance(&object, NULL, "instance1", 0, NULL), 0);
285  assert_ptr_equal(object, NULL);
286  assert_int_not_equal(amxd_object_new_instance(NULL, template, "instance1", 0, NULL), 0);
287 
288  amxd_object_delete(&template);
289  amxd_object_delete(&template);
290  amxd_object_delete(NULL);
291 
292  assert_int_not_equal(amxd_object_new(NULL, amxd_object_template, "test_object"), 0);
293 }

◆ test_amxd_object_new_instance()

void test_amxd_object_new_instance ( UNUSED void **  state)

Definition at line 177 of file test_amxd_object.c.

177  {
178  amxd_object_t* template = NULL;
179  amxd_object_t* object = NULL;
180  assert_int_equal(amxd_object_new(&template, amxd_object_template, "test_object"), 0);
181  assert_ptr_not_equal(template, NULL);
182 
183  assert_int_equal(amxd_object_new_instance(&object, template, "test_instance1", 0, NULL), 0);
184  assert_ptr_not_equal(object, NULL);
185  assert_string_equal(object->name, "test_instance1");
186  assert_int_equal(object->type, amxd_object_instance);
187  assert_false(object->attr.read_only);
188  assert_false(object->attr.priv);
189  assert_false(object->attr.persistent);
190  assert_false(object->attr.locked);
191  assert_int_equal(object->index, 1);
192  assert_true(amxc_llist_is_empty(&object->objects));
193  assert_true(amxc_llist_is_empty(&object->instances));
194  assert_true(amxc_llist_is_empty(&object->functions));
195  assert_true(amxc_llist_is_empty(&object->derived_objects));
196  assert_ptr_equal(object->derived_from.llist, NULL);
197  assert_ptr_equal(object->it.llist, &template->instances);
198  amxd_object_delete(&template);
199 
200  amxd_object_delete(&template);
201 }
@ amxd_object_instance
Definition: amxd_types.h:186
uint32_t read_only
Definition: amxd_types.h:221
uint32_t persistent
Definition: amxd_types.h:223
amxc_llist_t derived_objects
Definition: amxd_types.h:248
amxc_llist_it_t derived_from
Definition: amxd_types.h:249
amxd_object_type_t type
Definition: amxd_types.h:236
amxd_object_attr_t attr
Definition: amxd_types.h:237
amxc_llist_t functions
Definition: amxd_types.h:245
uint32_t index
Definition: amxd_types.h:240
amxc_llist_t objects
Definition: amxd_types.h:243
char * name
Definition: amxd_types.h:238
amxc_llist_it_t it
Definition: amxd_types.h:230
amxc_llist_t instances
Definition: amxd_types.h:244

◆ test_amxd_object_new_instance_invalid_name_index()

void test_amxd_object_new_instance_invalid_name_index ( UNUSED void **  state)

Definition at line 203 of file test_amxd_object.c.

203  {
204  amxd_object_t* template = NULL;
205  amxd_object_t* object = NULL;
206  assert_int_equal(amxd_object_new(&template, amxd_object_template, "test_object"), 0);
207  assert_ptr_not_equal(template, NULL);
208 
209  assert_int_equal(amxd_object_new_instance(&object, template, "test-instance1", 0, NULL), 0);
210  assert_int_equal(object->index, 1);
211  assert_int_not_equal(amxd_object_new_instance(&object, template, "test-instance2", 1, NULL), 0);
212  assert_int_not_equal(amxd_object_new_instance(&object, template, "test-instance1", 0, NULL), 0);
213  assert_int_equal(amxd_object_new_instance(&object, template, NULL, 0, NULL), 0);
214  assert_int_equal(object->index, 2);
215  assert_int_equal(amxd_object_new_instance(&object, template, NULL, 5, NULL), 0);
216  assert_int_equal(object->index, 5);
217  assert_int_equal(amxd_object_new_instance(&object, template, "", 0, NULL), 0);
218  assert_int_equal(object->index, 6);
219 
220  assert_int_not_equal(amxd_object_new_instance(&object, template, "1", 0, NULL), 0);
221  assert_ptr_equal(object, NULL);
222  assert_int_not_equal(amxd_object_new_instance(&object, template, "Test#Instance", 0, NULL), 0);
223  assert_ptr_equal(object, NULL);
224  assert_int_not_equal(amxd_object_new_instance(&object, template, "Test.Instance", 0, NULL), 0);
225  assert_ptr_equal(object, NULL);
226  assert_int_not_equal(amxd_object_new_instance(&object, template, "Test/Instance", 0, NULL), 0);
227  assert_ptr_equal(object, NULL);
228  assert_int_not_equal(amxd_object_new_instance(&object, template, "1Q", 0, NULL), 0);
229  assert_ptr_equal(object, NULL);
230 
231  amxd_object_delete(&template);
232 }

◆ test_amxd_object_new_instance_of_singelton()

void test_amxd_object_new_instance_of_singelton ( UNUSED void **  state)

Definition at line 234 of file test_amxd_object.c.

234  {
235  amxd_object_t* singelton = NULL;
236  amxd_object_t* object = NULL;
237  assert_int_equal(amxd_object_new(&singelton, amxd_object_singleton, "test_object"), 0);
238  assert_ptr_not_equal(singelton, NULL);
239 
240  assert_int_not_equal(amxd_object_new_instance(&object, singelton, "instance1", 0, NULL), 0);
241  assert_ptr_equal(object, NULL);
242 
243  amxd_object_delete(&singelton);
244 }

◆ test_amxd_object_new_instance_with_children()

void test_amxd_object_new_instance_with_children ( UNUSED void **  state)

Definition at line 246 of file test_amxd_object.c.

246  {
247  amxd_object_t* template = NULL;
248  amxd_object_t* child_object = NULL;
249  amxd_object_t* object = NULL;
250 
251  assert_int_equal(amxd_dm_init(&dm), 0);
252 
253  assert_int_equal(amxd_object_new(&template, amxd_object_template, "test_object"), 0);
254  assert_ptr_not_equal(template, NULL);
255  assert_int_equal(amxd_dm_add_root_object(&dm, template), 0);
256 
257  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "sub-object-1"), 0);
258  assert_ptr_not_equal(object, NULL);
259  assert_int_equal(amxd_object_add_object(template, object), 0);
260  assert_int_equal(amxd_object_new(&child_object, amxd_object_singleton, "sub-object-2"), 0);
261  assert_ptr_not_equal(object, NULL);
262  assert_int_equal(amxd_object_add_object(template, child_object), 0);
263  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "sub-object-3"), 0);
264  assert_ptr_not_equal(object, NULL);
265  assert_int_equal(amxd_object_add_object(template, object), 0);
266  assert_int_equal(amxc_llist_size(&template->objects), 3);
267 
268  assert_int_equal(amxd_object_new_instance(&object, template, "instance-1", 0, NULL), 0);
269  assert_ptr_not_equal(object, NULL);
270  assert_int_equal(amxc_llist_size(&object->objects), 3);
271  assert_int_equal(amxc_llist_size(&child_object->derived_objects), 1);
272 
273  amxd_object_delete(&child_object);
274 
275  amxd_dm_clean(&dm);
276 }

◆ test_amxd_object_new_invalid_name()

void test_amxd_object_new_invalid_name ( UNUSED void **  state)

Definition at line 163 of file test_amxd_object.c.

163  {
164  amxd_object_t* object = NULL;
165  assert_int_not_equal(amxd_object_new(&object, amxd_object_template, "1_test_object"), 0);
166  assert_ptr_equal(object, NULL);
167  assert_int_not_equal(amxd_object_new(&object, amxd_object_template, "test&object"), 0);
168  assert_ptr_equal(object, NULL);
169  assert_int_not_equal(amxd_object_new(&object, amxd_object_template, "-test_object"), 0);
170  assert_ptr_equal(object, NULL);
171  assert_int_not_equal(amxd_object_new(&object, amxd_object_template, ""), 0);
172  assert_ptr_equal(object, NULL);
173  assert_int_not_equal(amxd_object_new(&object, amxd_object_template, NULL), 0);
174  assert_ptr_equal(object, NULL);
175 }

◆ test_amxd_object_new_invalid_type()

void test_amxd_object_new_invalid_type ( UNUSED void **  state)

Definition at line 153 of file test_amxd_object.c.

153  {
154  amxd_object_t* object = NULL;
155  assert_int_not_equal(amxd_object_new(&object, amxd_object_root, "test_object"), 0);
156  assert_ptr_equal(object, NULL);
157  assert_int_not_equal(amxd_object_new(&object, amxd_object_instance, "test_object"), 0);
158  assert_ptr_equal(object, NULL);
159  assert_int_not_equal(amxd_object_new(&object, amxd_object_invalid, "test_object"), 0);
160  assert_ptr_equal(object, NULL);
161 }
@ amxd_object_root
Definition: amxd_types.h:178
@ amxd_object_invalid
Definition: amxd_types.h:190

◆ test_amxd_object_new_mib()

void test_amxd_object_new_mib ( UNUSED void **  state)

Definition at line 131 of file test_amxd_object.c.

131  {
132  amxd_object_t* object = NULL;
133 
134  assert_int_equal(amxd_object_new(&object, amxd_object_mib, "test-object"), 0);
135  assert_ptr_not_equal(object, NULL);
136  assert_string_equal(object->name, "test-object");
137  assert_int_equal(object->type, amxd_object_mib);
138  assert_false(object->attr.read_only);
139  assert_false(object->attr.priv);
140  assert_false(object->attr.persistent);
141  assert_false(object->attr.locked);
142  assert_int_equal(object->index, 0);
143  assert_true(amxc_llist_is_empty(&object->objects));
144  assert_true(amxc_llist_is_empty(&object->instances));
145  assert_true(amxc_llist_is_empty(&object->functions));
146  assert_true(amxc_llist_is_empty(&object->derived_objects));
147  assert_ptr_equal(object->derived_from.llist, NULL);
148  assert_ptr_equal(object->it.llist, NULL);
149  amxd_object_delete(&object);
150  assert_ptr_equal(object, NULL);
151 }
@ amxd_object_mib
Definition: amxd_types.h:188

◆ test_amxd_object_new_singleton()

void test_amxd_object_new_singleton ( UNUSED void **  state)

Definition at line 77 of file test_amxd_object.c.

77  {
78  amxd_object_t* object = NULL;
79 
80  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "test_object"), 0);
81  assert_ptr_not_equal(object, NULL);
82  assert_string_equal(object->name, "test_object");
83  assert_int_equal(object->type, amxd_object_singleton);
84  assert_false(object->attr.read_only);
85  assert_false(object->attr.priv);
86  assert_false(object->attr.persistent);
87  assert_false(object->attr.locked);
88  assert_int_equal(object->index, 0);
89  assert_true(amxc_llist_is_empty(&object->objects));
90  assert_true(amxc_llist_is_empty(&object->instances));
91  assert_int_equal(amxc_llist_size(&object->functions), 0);
92  assert_true(amxc_llist_is_empty(&object->derived_objects));
93  assert_ptr_not_equal(object->derived_from.llist, NULL);
94  assert_ptr_equal(object->it.llist, NULL);
95  amxd_object_delete(&object);
96  assert_ptr_equal(object, NULL);
97 
98  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "_test-object2"), 0);
99  assert_ptr_not_equal(object, NULL);
100  amxd_object_delete(&object);
101  assert_ptr_equal(object, NULL);
102 }

◆ test_amxd_object_new_template()

void test_amxd_object_new_template ( UNUSED void **  state)

Definition at line 104 of file test_amxd_object.c.

104  {
105  amxd_object_t* object = NULL;
106 
107  assert_int_equal(amxd_object_new(&object, amxd_object_template, "test_object"), 0);
108  assert_ptr_not_equal(object, NULL);
109  assert_string_equal(object->name, "test_object");
110  assert_int_equal(object->type, amxd_object_template);
111  assert_false(object->attr.read_only);
112  assert_false(object->attr.priv);
113  assert_false(object->attr.persistent);
114  assert_false(object->attr.locked);
115  assert_int_equal(object->index, 0);
116  assert_true(amxc_llist_is_empty(&object->objects));
117  assert_true(amxc_llist_is_empty(&object->instances));
118  assert_int_equal(amxc_llist_size(&object->functions), 0);
119  assert_true(amxc_llist_is_empty(&object->derived_objects));
120  assert_ptr_not_equal(object->derived_from.llist, NULL);
121  assert_ptr_equal(object->it.llist, NULL);
122  amxd_object_delete(&object);
123  assert_ptr_equal(object, NULL);
124 
125  assert_int_equal(amxd_object_new(&object, amxd_object_template, "_test-object2"), 0);
126  assert_ptr_not_equal(object, NULL);
127  amxd_object_delete(&object);
128  assert_ptr_equal(object, NULL);
129 }

Variable Documentation

◆ dm

amxd_dm_t dm
static

Definition at line 75 of file test_amxd_object.c.