libamxc  1.10.3
C Generic Data Containers
test_amxc_variant_dump.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 <stdio.h>
57 #include <stdarg.h>
58 #include <stddef.h>
59 #include <ctype.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <setjmp.h>
63 #include <cmocka.h>
64 #include <syslog.h>
65 #include <sys/mman.h>
66 #include <sys/stat.h>
67 #include <fcntl.h>
68 #include <malloc.h>
69 
70 
71 #include <amxc/amxc_variant_type.h>
72 
73 #include "test_amxc_variant_dump.h"
74 
75 #include <amxc/amxc_macros.h>
77 {
78  .init = NULL,
79  .del = NULL,
80  .copy = NULL,
81  .convert_from = NULL,
82  .convert_to = NULL,
83  .compare = NULL,
84  .name = "my_dummy_type_t"
85 };
86 
87 typedef struct {
88  int fd;
89  char* buffer;
90 } teststate_t;
91 
94  teststate_t* teststate = calloc(1, sizeof(teststate_t));
95  assert_non_null(teststate);
96 
97  teststate->fd = shm_open("/test_amxc_variant_dump", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
98  assert_int_not_equal(teststate->fd, -1);
99  assert_int_not_equal(-1, ftruncate(teststate->fd, 1024));
100  teststate->buffer = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, teststate->fd, 0);
101  assert_non_null(teststate->buffer);
102 
103  *state = teststate;
104  return 0;
105 }
106 
109  assert_non_null(state);
110  shm_unlink("/test_amxc_variant_dump");
111  free(*state);
112  *state = NULL;
113  return 0;
114 }
115 
116 void test_amxc_variant_dump(UNUSED void** state) {
117  amxc_var_t myvar;
118  amxc_var_t* subvar = NULL;
119 
122 
123  amxc_var_init(&myvar);
125 
126  subvar = amxc_var_add_new_key(&myvar, "KEY1");
128  amxc_var_add_new(subvar);
129  amxc_var_add_new(subvar);
130  subvar = amxc_var_add_new_key(&myvar, "KEY2");
132  amxc_var_add_new_key(subvar, "Foo");
133  amxc_var_add_new_key(subvar, "Bar");
134  subvar = amxc_var_add_key(cstring_t, &myvar, "KEY3", "Hello world");
135  subvar = amxc_var_add_key(uint64_t, &myvar, "KEY4", 666);
136  subvar = amxc_var_add_key(fd_t, &myvar, "KEY5", STDOUT_FILENO);
137  subvar = amxc_var_add_new_key(&myvar, "KEY6");
138  amxc_var_set_type(subvar, dummy_type_id);
139 
140  amxc_var_dump(&myvar, STDOUT_FILENO);
141 
143 
144  amxc_var_clean(&myvar);
145 }
146 
147 void test_amxc_variant_dump2(UNUSED void** state) {
148  amxc_var_t myvar;
149  amxc_var_t* subvar = NULL;
150  FILE* fp = NULL;
151  static char msgbuf[512];
152 
153  memset(msgbuf, 0, sizeof(msgbuf));
154  fp = fmemopen(msgbuf, 512, "w");
155  assert_non_null(fp);
156 
159 
160  amxc_var_init(&myvar);
162 
163  subvar = amxc_var_add_new_key(&myvar, "KEY1");
165  amxc_var_add_new(subvar);
166  amxc_var_add_new(subvar);
167  subvar = amxc_var_add_new_key(&myvar, "KEY2");
169  amxc_var_add_new_key(subvar, "Foo");
170  amxc_var_add_new_key(subvar, "Bar");
171  subvar = amxc_var_add_key(cstring_t, &myvar, "KEY3", "Hello world");
172  subvar = amxc_var_add_key(uint64_t, &myvar, "KEY4", 666);
173  subvar = amxc_var_add_key(fd_t, &myvar, "KEY5", STDOUT_FILENO);
174  subvar = amxc_var_add_new_key(&myvar, "KEY6");
175  amxc_var_set_type(subvar, dummy_type_id);
176 
177  amxc_var_dump_stream(&myvar, fp);
178  fclose(fp);
179  assert_non_null(strstr(msgbuf, "KEY1"));
180  assert_non_null(strstr(msgbuf, "KEY2"));
181  assert_non_null(strstr(msgbuf, "Foo"));
182  assert_non_null(strstr(msgbuf, "Bar"));
183  assert_non_null(strstr(msgbuf, "KEY3"));
184  assert_non_null(strstr(msgbuf, "Hello world"));
185  assert_non_null(strstr(msgbuf, "KEY4"));
186  assert_non_null(strstr(msgbuf, "666"));
187  assert_non_null(strstr(msgbuf, "KEY5"));
188  assert_non_null(strstr(msgbuf, "KEY6"));
189  assert_non_null(strstr(msgbuf, "my_dummy_type_t"));
190 
192 
193  amxc_var_clean(&myvar);
194 }
195 
196 void test_amxc_variant_log(UNUSED void** state) {
197  amxc_var_t myvar;
198  amxc_var_t* subvar = NULL;
199 
200  openlog("test", LOG_PID | LOG_CONS, LOG_DAEMON);
201 
204 
205  amxc_var_init(&myvar);
207 
208  subvar = amxc_var_add_new_key(&myvar, "KEY1");
210  amxc_var_add_new(subvar);
211  amxc_var_add_new(subvar);
212  subvar = amxc_var_add_new_key(&myvar, "KEY2");
214  amxc_var_add_new_key(subvar, "Foo");
215  amxc_var_add_new_key(subvar, "Bar");
216  subvar = amxc_var_add_key(cstring_t, &myvar, "KEY3", "Hello world");
217  subvar = amxc_var_add_key(uint64_t, &myvar, "KEY4", 666);
218  subvar = amxc_var_add_key(fd_t, &myvar, "KEY5", STDOUT_FILENO);
219  subvar = amxc_var_add_new_key(&myvar, "KEY6");
220  amxc_var_set_type(subvar, dummy_type_id);
221 
222  amxc_var_log(&myvar);
223 
225 
226  amxc_var_clean(&myvar);
227  closelog();
228 }
229 
231  amxc_var_dump(NULL, STDOUT_FILENO);
232 }
233 
235  amxc_var_t myvar;
236  amxc_var_t* subvar = NULL;
237  FILE* fp = NULL;
238  static char msgbuf[512];
239 
240  memset(msgbuf, 0, sizeof(msgbuf));
241  fp = fmemopen(msgbuf, 512, "w");
242  assert_non_null(fp);
243 
246 
247  amxc_var_init(&myvar);
249 
250  subvar = amxc_var_add_new_key(&myvar, "KEY1");
252  amxc_var_add_new(subvar);
253  amxc_var_add_new(subvar);
254  subvar = amxc_var_add_new_key(&myvar, "KEY2");
256  amxc_var_add_new_key(subvar, "Foo");
257  amxc_var_add_new_key(subvar, "Bar");
258  subvar = amxc_var_add_key(cstring_t, &myvar, "KEY3", "Hello world");
259  subvar = amxc_var_add_key(uint64_t, &myvar, "KEY4", 666);
260  subvar = amxc_var_add_key(fd_t, &myvar, "KEY5", STDOUT_FILENO);
261  subvar = amxc_var_add_new_key(&myvar, "KEY6");
262  amxc_var_set_type(subvar, dummy_type_id);
263 
264  amxc_var_dump_stream(NULL, NULL);
265  amxc_var_dump_stream(&myvar, NULL);
266  amxc_var_dump_stream(NULL, fp);
267  fclose(fp);
268 
270 
271  amxc_var_clean(&myvar);
272 }
273 
274 void test_amxc_variant_log_null(UNUSED void** state) {
275  amxc_var_log(NULL);
276 }
277 
279  amxc_var_t myvar;
280  teststate_t* teststate = *state;
281  int status = -1;
282 
283  // GIVEN a variant containing a list
284  amxc_var_init(&myvar);
286  amxc_var_add(cstring_t, &myvar, "text1");
287  amxc_var_add(cstring_t, &myvar, "text2");
288  amxc_var_add(int32_t, &myvar, 3);
289  amxc_var_add(bool, &myvar, true);
290 
291  // WHEN serializing it
292  status = amxc_var_dump(&myvar, teststate->fd);
293 
294  // THEN in the serialization output, commas are inbetween the elements
295  // and not after the last element
296  assert_int_equal(0, status);
297  assert_string_equal(teststate->buffer,
298  "[\n"
299  " \"text1\",\n"
300  " \"text2\",\n"
301  " 3,\n"
302  " true\n"
303  "]\n");
304 
305  amxc_var_clean(&myvar);
306 }
307 
309  amxc_var_t myvar;
310  teststate_t* teststate = *state;
311  int status = -1;
312 
313  // GIVEN a variant containing a hashtable
314  amxc_var_init(&myvar);
316  amxc_var_add_key(cstring_t, &myvar, "key1", "text1");
317  amxc_var_add_key(cstring_t, &myvar, "key2", "text2");
318  amxc_var_add_key(int32_t, &myvar, "key3", 3);
319  amxc_var_add_key(bool, &myvar, "key4", true);
320 
321  // WHEN serializing it
322  status = amxc_var_dump(&myvar, teststate->fd);
323 
324  // THEN the element without trailing comma is the last in the in the serialization output
325  // (not the last in hashtable internal order).
326  assert_int_equal(0, status);
327  assert_string_equal(teststate->buffer,
328  "{\n"
329  " key1 = \"text1\",\n"
330  " key2 = \"text2\",\n"
331  " key3 = 3,\n"
332  " key4 = true\n"
333  "}\n");
334 
335  amxc_var_clean(&myvar);
336 }
#define UNUSED
Definition: amxc_macros.h:70
#define fd_t
Convenience macro.
Definition: amxc_variant.h:600
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
Ambiorix ring buffer API header file.
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
uint32_t amxc_var_register_type(amxc_var_type_t *const type)
Register a new variant type.
uint32_t amxc_var_get_type_id_from_name(const char *const name)
Get the type id.
int amxc_var_unregister_type(amxc_var_type_t *const type)
Unregisters an already registered variant type.
int amxc_var_dump(const amxc_var_t *const var, int fd)
Dumps the content of the variant in a human readable manner.
int amxc_var_log(const amxc_var_t *const var)
Logs the content of the variant in a human readable manner to syslog.
int amxc_var_dump_stream(const amxc_var_t *const var, FILE *stream)
Dumps the content of the variant in a human readable manner.
#define amxc_var_add_key(type, var, key, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:627
#define amxc_var_add(type, var, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:618
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
int amxc_var_init(amxc_var_t *const var)
Initializes a variant.
Definition: amxc_variant.c:223
amxc_var_t * amxc_var_add_new(amxc_var_t *const var)
Adds a new variant to a composite variant.
Definition: amxc_variant.c:551
void amxc_var_clean(amxc_var_t *const var)
Clean-up and reset variant.
Definition: amxc_variant.c:237
amxc_var_t * amxc_var_add_new_key(amxc_var_t *const var, const char *key)
Adds a new variant with a key to a composite variant.
Definition: amxc_variant.c:526
A variant type structure.
const char * name
amxc_var_new_fn_t init
The variant struct definition.
Definition: amxc_variant.h:861
void test_amxc_variant_dump2(UNUSED void **state)
int test_amxc_variant_dump_testsetup(void **state)
void test_amxc_variant_dump2_null(UNUSED void **state)
void test_amxc_variant_dump_list_comma_position(void **state)
void test_amxc_variant_log_null(UNUSED void **state)
static amxc_var_type_t my_dummy_type
void test_amxc_variant_dump_null(UNUSED void **state)
void test_amxc_variant_dump_htable_comma_position(void **state)
void test_amxc_variant_dump(UNUSED void **state)
void test_amxc_variant_log(UNUSED void **state)
int test_amxc_variant_dump_testteardown(void **state)