blob: 6824db4e6a5dbab5e6ec18994d83c684d0c309e7 [file] [log] [blame]
Radek Iša56ca9e42020-09-08 18:42:00 +02001/**
2 * @file utests.h
3 * @author Radek Iša <isa@cesnet.cz>
4 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskob4750962022-10-06 15:33:35 +02005 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * @brief this file contains macros for simplification test writing
7 *
Michal Vaskob4750962022-10-06 15:33:35 +02008 * Copyright (c) 2021 - 2022 CESNET, z.s.p.o.
Radek Iša56ca9e42020-09-08 18:42:00 +02009 *
10 * This source code is licensed under BSD 3-Clause License (the "License").
11 * You may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * https://opensource.org/licenses/BSD-3-Clause
15 */
16
17#ifndef _UTESTS_H_
18#define _UTESTS_H_
19
20#define _POSIX_C_SOURCE 200809L /* strdup */
21
Radek Krejcib4ac5a92020-11-23 17:54:33 +010022#include <setjmp.h>
23#include <stdarg.h>
24#include <stddef.h>
Antonio Prcelafe128282022-08-24 11:36:34 +020025#include <stdint.h>
Michal Vasko86421632021-05-04 13:11:25 +020026#include <stdlib.h>
Radek Krejcib4ac5a92020-11-23 17:54:33 +010027
28#include <cmocka.h>
Radek Iša56ca9e42020-09-08 18:42:00 +020029
30#include <string.h>
31
32#include "libyang.h"
Michal Vaskob4750962022-10-06 15:33:35 +020033#include "plugins_exts/metadata.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010034#include "plugins_internal.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010035#include "plugins_types.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020036#include "tests_config.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010037#include "tree_schema_internal.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020038
39/**
40 * TESTS OVERVIEW
41 *
42 * To include utest's environment, just include "utests.h" in the test's source
43 * code. In case it is the main source code for a cmocka test group (there is a
44 * main() function), define _UTEST_MAIN_ before including this header.
45 *
46 * TESTS VARIABLES
47 *
48 * Checking macros use internal storage to store various variables necessary
49 * during the checking. It is possible to access these variables using the
50 * following macros:
51 *
52 * UTEST_LYCTX - libyang context
53 * UTEST_IN - input handler
54 * UTEST_OUT - output handler
55 *
56 * All these variables are cleaned with test's teardown.
57 *
58 * TESTS SETUP
59 *
60 * CMocka's CMUnitTest list definition macros (cmoka_unit_test*()) are replaced
61 * by UTEST macro with possibility to specify own setup and teardown functions:
62 *
63 * UTEST(test_func) - only implicit setup and teardown functions are used
64 * UTEST(test_func, setup) - implicit teardown but own setup
65 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
66 *
67 * Note that the tests environment always provide (and need) internal setup and
68 * teardown functions. In case the test-specific setup or teardown are used, they
69 * are supposed to include UTEST_SETUP at the setup beginning and UTEST_TEARDOWN
70 * at the teardown end.
71 *
72 * Libyang context is part of the prepared environment. To add a schema into the
73 * context (despite it is in the test-specific setup or in test function itself),
74 * use UTEST_ADD_MODULE macro.
75 *
76 * LOGGING
77 *
78 * There are 2 macros to check content of the log from the previously called
79 * libyang function. CHECK_LOG macro test only the last error message and path
80 * stored directly via logging callback. CHECK_LOG_CTX gets error message and
81 * path from the libyang context (in case the function does not store the error
82 * information into the libyang context, the message cannot be checked this way).
83 * libyang is set to store multiple error information, so multiple couples of
84 * error message and path can be provided to be checked (the first couple
85 * corresponds to the latest error). The macro also cleanups the errors list, so
86 * it is fine to check that there is no error after succeeding successful
87 * function call.
88 */
89
90/**
91 * @brief Test's context to provide common storage for various variables.
92 */
93struct utest_context {
94 struct ly_ctx *ctx; /**< libyang context */
95
Radek Iša56ca9e42020-09-08 18:42:00 +020096 struct ly_in *in; /**< Input handler */
97 struct ly_out *out; /**< Outpu handler */
Michal Vasko86421632021-05-04 13:11:25 +020098
99 char *orig_tz; /**< Original "TZ" environment variable value */
Radek Iša56ca9e42020-09-08 18:42:00 +0200100};
101
102/**
103 * @brief Shortcut to access utest_context.
104 */
105#define _UC ((struct utest_context *)*state)
106
107/**
108 * @brief libyang context provider.
109 */
110#define UTEST_LYCTX (_UC->ctx)
111
112/**
113 * @brief Context's input handler provider
114 */
115#define UTEST_IN (_UC->in)
116
117/**
118 * @brief Context's input handler provider
119 */
120#define UTEST_OUT (_UC->out)
121
122/**
123 * @brief Parse (and validate) data from the input handler as a YANG data tree.
124 *
125 * @param[in] INPUT The input data in the specified @p format to parse (and validate)
126 * @param[in] INPUT_FORMAT Format of the input data to be parsed. Can be 0 to try to detect format from the input handler.
127 * @param[in] PARSE_OPTIONS Options for parser, see @ref dataparseroptions.
128 * @param[in] VALIDATE_OPTIONS Options for the validation phase, see @ref datavalidationoptions.
Michal Vaskoddd76592022-01-17 13:34:48 +0100129 * @param[in] RET expected return status
130 * @param[out] OUT_NODE Resulting data tree built from the input data. Note that NULL can be a valid result as a
131 * representation of an empty YANG data tree.
Radek Iša56ca9e42020-09-08 18:42:00 +0200132 */
Michal Vaskoddd76592022-01-17 13:34:48 +0100133#define CHECK_PARSE_LYD_PARAM(INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, RET, OUT_NODE) \
134 { \
135 LY_ERR _r = lyd_parse_data_mem(_UC->ctx, INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, &OUT_NODE); \
136 if (_r != RET) { \
137 if (_r) { \
138 fail_msg("%s != 0x%d; MSG: %s", #RET, _r, ly_err_last(_UC->ctx)->msg); \
139 } else { \
140 fail_msg("%s != 0x%d", #RET, _r); \
141 } \
142 } \
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 }
144
145/**
146 * @brief Check if lyd_node and his subnodes have correct values. Print lyd_node and his sunodes int o string in json or xml format.
147 * @param[in] NODE pointer to lyd_node
148 * @param[in] TEXT expected output string in json or xml format.
149 * @param[in] FORMAT format of input text. LYD_JSON, LYD_XML
150 * @param[in] PARAM options [Data printer flags](@ref dataprinterflags).
151 */
152#define CHECK_LYD_STRING_PARAM(NODE, TEXT, FORMAT, PARAM) \
153 { \
Michal Vaskoddd76592022-01-17 13:34:48 +0100154 char *str; \
155 LY_ERR _r = lyd_print_mem(&str, NODE, FORMAT, PARAM); \
156 if (_r) { \
157 fail_msg("Print err 0x%d; MSG: %s", _r, ly_err_last(_UC->ctx)->msg); \
158 } \
159 assert_string_equal(str, TEXT); \
160 free(str); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200161 }
162
163/**
164 * @brief Compare two lyd_node structure. Macro print lyd_node structure into string and then compare string. Print function use these two parameters. LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK;
165 * @param[in] NODE_1 pointer to lyd_node
166 * @param[in] NODE_2 pointer to lyd_node
167 */
168#define CHECK_LYD(NODE_1, NODE_2) \
169 { \
Michal Vaskoddd76592022-01-17 13:34:48 +0100170 char *str1; \
171 char *str2; \
172 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str1, NODE_1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK)); \
173 assert_int_equal(LY_SUCCESS, lyd_print_mem(&str2, NODE_2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK)); \
174 assert_non_null(str1); \
175 assert_non_null(str2); \
176 assert_string_equal(str1, str2); \
177 free(str1); \
178 free(str2); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 }
180
181/*
182 * SUPPORT MACROS
183 */
184
185/**
186 * @brief Internal macro witch assert that two given string are equal or are both null.
187 *
188 * @param[in] STRING string to check
189 * @param[in] TEXT string to compare
190 */
191#define CHECK_STRING(STRING, TEXT)\
192 if (TEXT == NULL) { \
193 assert_null(STRING); \
194 } else { \
195 assert_non_null(STRING); \
196 assert_string_equal(STRING, TEXT); \
197 }
198
199/**
200 * @brief Internal macro witch assert that pointer is null when flag is 0.
201 *
202 * @param[in] POINTER pointer to check
203 * @param[in] FLAG 0 -> pointer is NULL, 1 -> pointer is not null
204 */
205#define CHECK_POINTER(POINTER, FLAG) \
206 assert_true(FLAG == 0 ? POINTER == NULL : POINTER != NULL)
207
208/**
209 * @brief Internal macro check size of [sized array](@ref sizedarrays)'s
210 *
211 * @param[in] ARRAY pointer to [sized array](@ref sizedarrays)
212 * @param[in] SIZE expected [sized array](@ref sizedarrays) size of array
213 */
214#define CHECK_ARRAY(ARRAY, SIZE) \
215 assert_true((SIZE == 0) ? \
216 (ARRAY == NULL) : \
217 (ARRAY != NULL && SIZE == LY_ARRAY_COUNT(ARRAY)));
218
219/*
220 * LIBYANG NODE CHECKING
221 */
222
223/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100224 * @brief check compileted type
Michal Vaskoddd76592022-01-17 13:34:48 +0100225 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100226 * @param[in] NODE pointer to lysc_type value
227 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
228 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
229 */
230#define CHECK_LYSC_TYPE(NODE, TYPE, EXTS) \
231 assert_non_null(NODE); \
232 assert_int_equal((NODE)->basetype, TYPE); \
233 CHECK_ARRAY((NODE)->exts, EXTS); \
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200234 assert_ptr_equal((NODE)->plugin, lyplg_type_plugin_find("", NULL, ly_data_type2str[TYPE]))
Radek Išaa9ff2b82021-01-13 21:44:13 +0100235
Michal Vaskoddd76592022-01-17 13:34:48 +0100236/**
237 * @brief check compileted numeric type
238 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100239 * @param[in] NODE pointer to lysc_type_num value
240 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
241 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
242 * @warning only integer types INT, UINT, NUM
243 */
244#define CHECK_LYSC_TYPE_NUM(NODE, TYPE, EXTS, RANGE) \
245 CHECK_LYSC_TYPE(NODE, TYPE, EXTS);\
246 CHECK_POINTER((NODE)->range, RANGE)
247
Michal Vaskoddd76592022-01-17 13:34:48 +0100248/**
249 * @brief check compiled string type
250 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100251 * @param[in] NODE pointer to lysc_type_num value
252 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
253 * @param[in] LENGTH 0 -> node dosnt have length limitation, 1 -> node have length limitation
254 * @param[in] PATTERNS expected number of patterns [sized array](@ref sizedarrays)
255 * @warning only integer types INT, UINT, NUM
256 */
257#define CHECK_LYSC_TYPE_STR(NODE, EXTS, LENGTH, PATTERNS) \
258 CHECK_LYSC_TYPE(NODE, LY_TYPE_STRING, EXTS); \
259 CHECK_POINTER((NODE)->length, LENGTH); \
260 CHECK_ARRAY((NODE)->patterns, PATTERNS)
261
Michal Vaskoddd76592022-01-17 13:34:48 +0100262/**
263 * @brief check compiled bits type
264 *
Radek Išaded31052021-03-10 13:22:53 +0100265 * @param[in] NODE pointer to lysc_type_num value
266 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
267 * @param[in] BITS expected number of bits
268 * @warning only integer types INT, UINT, NUM
269 */
270#define CHECK_LYSC_TYPE_BITS(NODE, EXTS, BITS) \
271 CHECK_LYSC_TYPE(NODE, LY_TYPE_BITS, EXTS); \
272 CHECK_ARRAY((NODE)->bits, BITS)
273
Radek Išaded31052021-03-10 13:22:53 +0100274#define CHECK_LYSC_TYPE_BITENUM_ITEM(NODE, POSITION, DSC, EXTS, FLAGS, NAME, REF)\
275 assert_non_null(NODE); \
276 assert_int_equal((NODE)->position, POSITION); \
277 CHECK_STRING((NODE)->dsc, DSC); \
278 CHECK_ARRAY((NODE)->exts, EXTS); \
279 assert_int_equal((NODE)->flags, FLAGS); \
280 CHECK_STRING((NODE)->name, NAME); \
281 CHECK_STRING((NODE)->ref, REF) \
282
Michal Vaskoddd76592022-01-17 13:34:48 +0100283/**
284 * @brief check range
285 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100286 * @param[in] NODE pointer to lysc_range value
287 * @param[in] DSC expected descriptin (string)
288 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
289 * @param[in] EMSG expected string reprezenting error message
290 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
291 * @param[in] PARTS expected [sized array](@ref sizedarrays) number of rang limitations
292 * @param[in] REF expected reference
293 */
294#define CHECK_LYSC_RANGE(NODE, DSC, EAPPTAG, EMSG, EXTS, PARTS, REF) \
295 assert_non_null(NODE); \
296 CHECK_STRING((NODE)->dsc, DSC); \
297 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
298 CHECK_STRING((NODE)->emsg, EMSG); \
299 CHECK_ARRAY((NODE)->exts, EXTS); \
300 CHECK_ARRAY((NODE)->parts, PARTS); \
301 CHECK_STRING((NODE)->ref, REF)
302
Michal Vaskoddd76592022-01-17 13:34:48 +0100303/**
304 * @brief check pattern
305 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100306 * @param[in] NODE pointer to lysc_pattern value
307 * @param[in] DSC expected descriptin (string)
308 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
309 * @param[in] EMSG expected string reprezenting error message
310 * @param[in] EEXPR expected string reprezenting original, not compiled, regular expression
311 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
312 * @param[in] INVERTED if regular expression is inverted.
313 * @param[in] REF expected reference
314 */
315#define CHECK_LYSC_PATTERN(NODE, DSC, EAPPTAG, EMSG, EXPR, EXTS, INVERTED, REF) \
316 assert_non_null(NODE); \
317 assert_non_null((NODE)->code); \
318 CHECK_STRING((NODE)->dsc, DSC); \
319 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
320 CHECK_STRING((NODE)->emsg, EMSG); \
321 CHECK_STRING((NODE)->expr, EXPR); \
322 CHECK_ARRAY((NODE)->exts, EXTS); \
323 assert_int_equal((NODE)->inverted, INVERTED); \
324 CHECK_STRING((NODE)->ref, REF)
325
326/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200327 * @brief assert that lysp_action_inout structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100328 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200329 * @param[in] NODE pointer to lysp_action_inout variable
330 * @param[in] DATA 0 -> check if pointer to data is NULL, 1 -> check if pointer to data is not null
331 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
332 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
333 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of must restriction list
334 * @param[in] NODETYPE node type. LYS_INPUT or LYS_OUTPUT
335 * @param[in] PARENT 0 -> check if node is root, 1 -> check if node is not root
336 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of typedefs list
337 */
338#define CHECK_LYSP_ACTION_INOUT(NODE, DATA, EXTS, GROUPINGS, MUSTS, NODETYPE, PARENT, TYPEDEFS) \
339 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100340 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200341 CHECK_ARRAY((NODE)->exts, EXTS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100342 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200343 CHECK_ARRAY((NODE)->musts, MUSTS); \
344 assert_int_equal((NODE)->nodetype, NODETYPE); \
345 CHECK_POINTER((NODE)->parent, PARENT); \
346 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS);
347
348/**
349 * @brief assert that lysp_action structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100350 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200351 * @param[in] NODE pointer to lysp_action variable
352 * @param[in] DSC expected description
353 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extension list
354 * @param[in] FLAGS expected [schema node flags](@ref snodeflags)
355 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
356 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of if-feature expressions list
357 * @param[in] INPUT_* ::LYSP_ACTION_INOUT_CHECK
358 * @param[in] NAME expected name
359 * @param[in] NODETYPE node type. LYS_RPC or LYS_ACTION
360 * @param[in] OUTPUT_* ::LYSP_ACTION_INOUT_CHECK
361 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
362 * @param[in] REF expected reference
363 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of list of typedefs
364 */
365#define CHECK_LYSP_ACTION(NODE, DSC, EXTS, FLAGS, GROUPINGS, IFFEATURES, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100366 INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, INPUT_MUSTS, \
367 INPUT_PARENT, INPUT_TYPEDEFS, \
368 NAME, NODETYPE, \
369 OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, OUTPUT_MUSTS, \
370 OUTPUT_PARENT, OUTPUT_TYPEDEFS, \
371 PARENT, REF, TYPEDEFS) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200372 assert_non_null(NODE); \
373 CHECK_STRING((NODE)->dsc, DSC); \
374 CHECK_ARRAY((NODE)->exts, EXTS); \
375 assert_int_equal((NODE)->flags, FLAGS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100376 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200377 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
378 CHECK_LYSP_ACTION_INOUT(&((NODE)->input), INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, \
379 INPUT_MUSTS, LYS_INPUT, INPUT_PARENT, INPUT_TYPEDEFS); \
380 assert_string_equal((NODE)->name, NAME); \
381 assert_int_equal((NODE)->nodetype, NODETYPE); \
382 CHECK_LYSP_ACTION_INOUT(&((NODE)->output), OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, \
383 OUTPUT_MUSTS, LYS_OUTPUT, OUTPUT_PARENT, OUTPUT_TYPEDEFS); \
384 CHECK_POINTER((NODE)->parent, PARENT); \
385 CHECK_STRING((NODE)->ref, REF); \
386 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS) \
387
388/**
389 * @brief assert that lysp_when structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100390 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200391 * @param[in] NODE pointer to lysp_when variable
392 * @param[in] COND expected string specifid condition
393 * @param[in] DSC expected string description statement
394 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
395 * @param[in] REF expected string reference
396 */
397#define CHECK_LYSP_WHEN(NODE, COND, DSC, EXTS, REF) \
398 assert_non_null(NODE); \
399 assert_string_equal((NODE)->cond, COND); \
400 CHECK_STRING((NODE)->dsc, DSC); \
401 CHECK_ARRAY((NODE)->exts, EXTS); \
402 if (REF == NULL) { \
403 assert_null((NODE)->ref); \
404 } else { \
405 assert_non_null((NODE)->ref); \
406 assert_string_equal((NODE)->ref, REF); \
407 }
408
409/**
410 * @brief assert that lysp_restr structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100411 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200412 * @param[in] NODE pointer to lysp_restr variable
413 * @param[in] ARG_STR expected string. The restriction expression/value
414 * @param[in] DSC expected descrition
415 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
416 * @param[in] EMSG expected string reprezenting error message
417 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
418 * @param[in] REF expected reference
419 */
420
421#define CHECK_LYSP_RESTR(NODE, ARG_STR, DSC, EAPPTAG, EMSG, EXTS, REF) \
422 assert_non_null(NODE); \
423 assert_non_null((NODE)->arg.mod); \
424 assert_string_equal((NODE)->arg.str, ARG_STR); \
425 CHECK_STRING((NODE)->dsc, DSC); \
426 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
427 CHECK_STRING((NODE)->emsg, EMSG); \
428 CHECK_ARRAY((NODE)->exts, EXTS); \
429 CHECK_STRING((NODE)->ref, REF);
430
431/**
432 * @brief assert that lysp_import structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100433 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200434 * @param[in] NODE pointer to lysp_import variable
435 * @param[in] DSC expected description or NULL
436 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extensions
437 * @param[in] NAME expected name of imported module
438 * @param[in] PREFIX expected prefix for the data from the imported schema
439 * @param[in] REF expected reference
Michal Vasko2bf4af42023-01-04 12:08:38 +0100440 * @param[in] REV expected reprezenting date in format "11-10-2020"
Radek Iša56ca9e42020-09-08 18:42:00 +0200441 */
442#define CHECK_LYSP_IMPORT(NODE, DSC, EXTS, NAME, PREFIX, REF, REV) \
443 assert_non_null(NODE); \
444 CHECK_STRING((NODE)->dsc, DSC); \
445 CHECK_ARRAY((NODE)->exts, EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200446 assert_string_equal((NODE)->name, NAME); \
447 assert_string_equal((NODE)->prefix, PREFIX); \
448 CHECK_STRING((NODE)->ref, REF); \
449 CHECK_STRING((NODE)->rev, REV); \
450
451/**
452 * @brief assert that lysp_ext structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100453 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200454 * @param[in] NODE pointer to lysp_ext_instance variable
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100455 * @param[in] ARGNAME expected argument name
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists
457 * @param[in] DSC expected string reprezent description
458 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
459 * @param[in] FLAGS expected LYS_STATUS_* and LYS_YINELEM_* values (@ref snodeflags)
460 * @param[in] NAME expected name
461 * @param[in] REF expected ref
462 */
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100463#define CHECK_LYSP_EXT(NODE, ARGNAME, COMPILED, DSC, EXTS, FLAGS, NAME, REF) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 assert_non_null(NODE); \
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100465 CHECK_STRING((NODE)->argname, ARGNAME); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 CHECK_POINTER((NODE)->compiled, COMPILED); \
467 CHECK_STRING((NODE)->dsc, DSC); \
468 CHECK_ARRAY((NODE)->exts, EXTS); \
469 assert_int_equal((NODE)->flags, FLAGS); \
470 assert_string_equal((NODE)->name, NAME); \
471 CHECK_STRING((NODE)->ref, REF);
472
473/**
474 * @brief assert that lysp_ext_instance structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100475 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200476 * @param[in] NODE pointer to lysp_ext_instance variable
477 * @param[in] ARGUMENT expected optional value of the extension's argument
478 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
Radek Krejciab430862021-03-02 20:13:40 +0100479 * @param[in] PARENT_STMT expected value identifying placement of the extension instance
480 * @param[in] PARENT_STMT_INDEX expected indentifi index
Michal Vaskofc2cd072021-02-24 13:17:17 +0100481 * @param[in] FORMAT expected format
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 */
Radek Krejci85ac8312021-03-03 20:21:33 +0100483#define CHECK_LYSP_EXT_INSTANCE(NODE, ARGUMENT, CHILD, PARENT_STMT, PARENT_STMT_INDEX, NAME, FORMAT) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 assert_non_null(NODE); \
485 CHECK_STRING((NODE)->argument, ARGUMENT); \
486 CHECK_POINTER((NODE)->child, CHILD); \
Radek Krejciab430862021-03-02 20:13:40 +0100487 assert_int_equal((NODE)->parent_stmt, PARENT_STMT); \
488 assert_int_equal((NODE)->parent_stmt_index, PARENT_STMT_INDEX); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 assert_string_equal((NODE)->name, NAME); \
Michal Vaskofc2cd072021-02-24 13:17:17 +0100490 assert_int_equal((NODE)->format, FORMAT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200491
492/**
493 * @brief assert that lysp_stmt structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100494 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200495 * @param[in] NODE pointer to lysp_stmt variable
496 * @param[in] ARG expected statemet argumet
497 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
498 * @param[in] FLAGS expected statement flags, can be set to LYS_YIN_ATTR
499 * @param[in] KW expected numeric respresentation of the stmt value
500 * @param[in] NEXT 0 -> pointer is NULL, 1 -> pointer is not null
501 * @param[in] STMS expected identifier of the statement
502 */
503#define CHECK_LYSP_STMT(NODE, ARG, CHILD, FLAGS, KW, NEXT, STMT) \
504 assert_non_null(NODE); \
505 CHECK_STRING((NODE)->arg, ARG); \
506 CHECK_POINTER((NODE)->child, CHILD); \
507 assert_int_equal((NODE)->flags, FLAGS); \
508 assert_int_equal((NODE)->kw, KW); \
509 CHECK_POINTER((NODE)->next, NEXT); \
510 assert_string_equal((NODE)->stmt, STMT); \
511
512/**
513 * @brief assert that lysp_type_enum structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100514 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200515 * @param[in] NODE pointer to lysp_type_enum variable
516 * @param[in] DSC expected description
517 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
518 * @param[in] FLAGS only LYS_STATUS_ and LYS_SET_VALUE values are allowed
519 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
520 * @param[in] NAME expected name
521 * @param[in] REF expected reference statement
522 * @param[in] VALUE expected enum's value or bit's position
523 */
524#define CHECK_LYSP_TYPE_ENUM(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, REF, VALUE) \
525 assert_non_null(NODE); \
526 CHECK_STRING((NODE)->dsc, DSC); \
527 CHECK_ARRAY((NODE)->exts, EXTS); \
528 assert_int_equal((NODE)->flags, FLAGS); \
529 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
530 CHECK_STRING((NODE)->name, NAME); \
531 CHECK_STRING((NODE)->ref, REF); \
532 assert_int_equal(VALUE, (NODE)->value);
533
534/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100535 * @brief assert that lysp_type_enum structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100536 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100537 * @param[in] NODE pointer to lysp_type variable
538 * @param[in] BASES expected [sized array](@ref sizedarrays) size of list of indentifiers
539 * @param[in] BITS expected [sized array](@ref sizedarrays) size of list of bits
540 * @param[in] COMPILED 0 -> pointer to compiled type is null, 1 -> pointer to compilet type is valid
541 * @param[in] ENUMS expected [sized array](@ref sizedarrays) size of list of enums-stmts
542 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
543 * @param[in] FLAGS expected flags
544 * @param[in] FRACTION_DIGITS expected number of fraction digits decimal64
545 * @param[in] LENGTH expected 0 -> there isnt any restriction on length, 1 -> type is restricted on length (string, binary)
546 * @param[in] NAME expected name of type
547 * @param[in] PATH 0 -> no pointer to parsed path, 1 -> pointer to parsed path is valid
548 * @param[in] PATTERNS expected [sized array](@ref sizedarrays) size of list of patterns for string
549 * @param[in] PMOD expected submodule where type is defined 0 -> pointer is null, 1 -> pointer is not null
550 * @param[in] RANGE expected [sized array](@ref sizedarrays) size of list of range restriction
551 * @param[in] REQUIRE_INSTANCE expected require instance flag
552 * @param[in] TYPES expected [sized array](@ref sizedarrays) size of list of sub-types
553 */
554#define CHECK_LYSP_TYPE(NODE, BASES, BITS, COMPILED, ENUMS, EXTS, FLAGS, FRACTIONS_DIGITS, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100555 LENGTH, NAME, PATH, PATTERNS, PMOD, RANGE, REQUIRE_INSTANCE, TYPES) \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100556 assert_non_null(NODE);\
557 CHECK_ARRAY((NODE)->bases, BASES); \
558 CHECK_ARRAY((NODE)->bits, BITS); \
559 CHECK_POINTER((NODE)->compiled, COMPILED); \
560 CHECK_ARRAY((NODE)->enums, ENUMS); \
561 CHECK_ARRAY((NODE)->exts, EXTS); \
562 assert_int_equal((NODE)->flags, FLAGS); \
563 assert_int_equal((NODE)->fraction_digits, FRACTIONS_DIGITS); \
564 CHECK_POINTER((NODE)->length, LENGTH); \
565 CHECK_STRING((NODE)->name, NAME); \
566 CHECK_POINTER((NODE)->path, PATH); \
567 CHECK_ARRAY((NODE)->patterns, PATTERNS); \
568 CHECK_POINTER((NODE)->pmod, PMOD); \
569 CHECK_POINTER((NODE)->range, RANGE); \
570 assert_int_equal((NODE)->require_instance, REQUIRE_INSTANCE); \
571 CHECK_ARRAY((NODE)->types , TYPES)
572
573/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200574 * @brief assert that lysp_node structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100575 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200576 * @param[in] NODE pointer to lysp_node variable
577 * @param[in] DSC expected description statement
578 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
579 * @param[in] FLAGS [schema node flags](@ref snodeflags)
580 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
581 * @param[in] NAME expected name
582 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
583 * @param[in] NODETYPE node type LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, LYS_LEAFLIST,
584 * LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, LYS_ACTION, LYS_NOTIF,
585 * LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, LYS_AUGMENT
586 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
587 * @param[in] REF expected reference statement
588 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
589 */
590#define CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, NODETYPE, PARENT, REF, WHEN) \
591 assert_non_null(NODE); \
592 CHECK_STRING((NODE)->dsc, DSC); \
593 CHECK_ARRAY((NODE)->exts, EXTS); \
594 assert_int_equal((NODE)->flags, FLAGS); \
595 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
596 CHECK_STRING((NODE)->name, NAME); \
597 CHECK_POINTER((NODE)->next, NEXT); \
598 assert_int_equal((NODE)->nodetype, NODETYPE); \
599 CHECK_POINTER((NODE)->parent, PARENT); \
600 CHECK_STRING((NODE)->ref, REF); \
Radek Krejci9a3823e2021-01-27 20:26:46 +0100601 CHECK_POINTER(lysp_node_when((struct lysp_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200602
603/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100604 * @brief assert that lysp_node structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100605 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100606 * @param[in] NODE pointer to lysp_node variable
607 * @param[in] DSC expected description statement
608 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
609 * @param[in] FLAGS [schema node flags](@ref snodeflags)
610 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
611 * @param[in] NAME expected name
612 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
613 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
614 * @param[in] REF expected reference statement
615 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
616 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
617 * @param[in] UNITS expected string reprezenting units
618 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
619 */
620#define CHECK_LYSP_NODE_LEAF(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100621 PARENT, REF, WHEN, MUSTS, UNITS, DFLT) \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100622 CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, LYS_LEAF, PARENT, REF, WHEN); \
623 CHECK_ARRAY((NODE)->musts, MUSTS); \
624 CHECK_STRING((NODE)->units, UNITS); \
625 CHECK_STRING((NODE)->dflt.str, DFLT);
626
627/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200628 * @brief assert that lysc_notif structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100629 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200630 * @param[in] NODE pointer to lysp_notif variable
631 * @param[in] DATA 0 pointer is null, 1 pointer is not null
632 * @param[in] DSC expected description
633 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
634 * @param[in] FLAGS [schema node flags](@ref snodeflags)
635 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
636 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
637 * @param[in] NAME expected name
638 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
639 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
640 * @param[in] REF expected reference
641 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
642 */
643#define CHECK_LYSC_NOTIF(NODE, DATA, DSC, EXTS, FLAGS, MODULE, MUSTS, NAME, PARENT, PRIV, REF, WHEN) \
644 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100645 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200646 CHECK_STRING((NODE)->dsc, DSC); \
647 CHECK_ARRAY((NODE)->exts, EXTS); \
648 assert_int_equal((NODE)->flags, FLAGS); \
649 CHECK_POINTER((NODE)->module, MODULE); \
650 CHECK_ARRAY((NODE)->musts, MUSTS); \
651 assert_string_equal((NODE)->name, NAME); \
652 assert_int_equal((NODE)->nodetype, LYS_NOTIF); \
653 CHECK_POINTER((NODE)->parent, PARENT); \
654 CHECK_POINTER((NODE)->priv, PRIV); \
655 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100656 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200657
658/**
659 * @brief assert that lysc_action_inout structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100660 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200661 * @param[in] NODE pointer to lysp_notif variable
662 * @param[in] DATA 0 pointer is null, 1 pointer is not null
663 * @param[in] MUST expected [sized array](@ref sizedarrays) size of list of must restrictions
664 * @param[in] NODETYPE LYS_INPUT or LYS_OUTPUT
665 */
666#define CHECK_LYSC_ACTION_INOUT(NODE, DATA, MUST, NODETYPE) \
667 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100668 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200669 CHECK_ARRAY((NODE)->musts, MUST); \
670 assert_int_equal((NODE)->nodetype, NODETYPE);
671
672/**
673 * @brief assert that lysc_action structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100674 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200675 * @param[in] NODE pointer to lysp_action variable
676 * @param[in] DSC string description statement
677 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
678 * @param[in] FLAGS [schema node flags](@ref snodeflags)
679 * @param[in] INPUT_DATA 0 pointer is null, 1 pointer is not null
680 * @param[in] INPUT_MUST expected [sized array](@ref sizedarrays) size of input list of must restrictions
681 * @param[in] INPUT_EXTS expected [sized array](@ref sizedarrays) size of the input extension instances of input
682 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
683 * @param[in] NAME expected name
684 * @param[in] NODETYPE LYS_RPC, LYS_ACTION
685 * @param[in] OUTPUT_DATA 0 pointer is null, 1 pointer is not null
686 * @param[in] OUTPUT_MUST expected [sized array](@ref sizedarrays) size of output list of must restrictions
687 * @param[in] OUTPUT_EXTS expected [sized array](@ref sizedarrays) size of the output extension instances of input
688 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
689 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
690 * @param[in] REF expected reference
691 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
692 */
693#define CHECK_LYSC_ACTION(NODE, DSC, EXTS, FLAGS, INPUT_DATA, INPUT_MUST, INPUT_EXTS, MODULE, NAME, NODETYPE, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100694 OUTPUT_DATA, OUTPUT_MUST, OUTPUT_EXTS, PARENT, PRIV, REF, WHEN) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200695 assert_non_null(NODE); \
696 CHECK_STRING((NODE)->dsc, DSC); \
697 CHECK_ARRAY((NODE)->exts, EXTS); \
698 assert_int_equal((NODE)->flags, FLAGS); \
699 CHECK_LYSC_ACTION_INOUT(&(NODE)->input, INPUT_DATA, INPUT_MUST, LYS_INPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100700 CHECK_ARRAY((NODE)->input.exts, INPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200701 CHECK_POINTER((NODE)->module, MODULE); \
702 assert_string_equal((NODE)->name, NAME); \
703 assert_int_equal((NODE)->nodetype, NODETYPE); \
704 CHECK_LYSC_ACTION_INOUT(&(NODE)->output, OUTPUT_DATA, OUTPUT_MUST, LYS_OUTPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100705 CHECK_ARRAY((NODE)->output.exts, OUTPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200706 CHECK_POINTER((NODE)->parent, PARENT); \
707 CHECK_POINTER((NODE)->priv, PRIV); \
708 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100709 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200710
711/**
712 * @brief assert that lysc_node structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100713 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200714 * @param[in] NODE pointer to lysc_node variable
715 * @param[in] DSC expected description
716 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
717 * @param[in] FLAGS [schema node flags](@ref snodeflags)
718 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
719 * @param[in] NAME expected name
720 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
721 * @param[in] NODETYPE type of the node LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF,
722 * LYS_LEAFLIST, LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC,
723 * LYS_ACTION, LYS_NOTIF, LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING,
724 * LYS_AUGMENT
725 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
726 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
727 * @param[in] REF expected reference
728 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
729 */
730#define CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, NODETYPE, PARENT, PRIV, REF, WHEN) \
731 assert_non_null(NODE); \
732 CHECK_STRING((NODE)->dsc, DSC); \
733 CHECK_ARRAY((NODE)->exts, EXTS); \
734 assert_int_equal((NODE)->flags, FLAGS); \
735 CHECK_POINTER((NODE)->module, MODULE); \
736 assert_string_equal((NODE)->name, NAME); \
737 CHECK_POINTER((NODE)->next, NEXT); \
738 assert_int_equal((NODE)->nodetype, NODETYPE); \
739 CHECK_POINTER((NODE)->parent, PARENT); \
740 assert_non_null((NODE)->prev); \
741 CHECK_POINTER((NODE)->priv, PRIV); \
742 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100743 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
744
745/**
746 * @brief assert that lysc_node_leaf structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100747 *
Radek Išaa9ff2b82021-01-13 21:44:13 +0100748 * @param[in] NODE pointer to lysc_node variable
749 * @param[in] DSC expected description
750 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
751 * @param[in] FLAGS [schema node flags](@ref snodeflags)
752 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
753 * @param[in] NAME expected name
754 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
755 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
756 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
757 * @param[in] REF expected reference
Radek Išaa76902f2021-03-29 08:52:51 +0200758 * @param[in] ACTIONS 1 if is set pointer to structure lysc_node_action other 0
759 * @param[in] CHILD 1 if is set pointer to child other 0
760 * @param[in] MAX possible maximum elements in list
761 * @param[in] MIN possible minimum elements in list
762 * @param[in] MUSTS [sized array](@ref sizedarrays) number of must node elements in array
763 * @param[in] NOTIFS 1 if is set pointer to any notifs node
764 * @param[in] UNIQUES [sized array](@ref sizedarrays) number of unique nodes element in array
765 * @param[in] WHEN [sized array](@ref sizedarrays) size of when node array
766 */
767#define CHECK_LYSC_NODE_LIST(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100768 PARENT, PRIV, REF, ACTIONS, CHILD, MAX, MIN, MUSTS, NOTIFS, UNIQUES, WHEN) \
Radek Išaa76902f2021-03-29 08:52:51 +0200769 CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LIST, PARENT, PRIV, REF, WHEN); \
770 CHECK_POINTER((NODE)->actions, ACTIONS); \
771 CHECK_POINTER((NODE)->child, CHILD); \
772 assert_int_equal((NODE)->max, MAX); \
773 assert_int_equal((NODE)->min, MIN); \
774 CHECK_ARRAY((NODE)->musts, MUSTS); \
775 CHECK_POINTER((NODE)->notifs, NOTIFS); \
776 CHECK_ARRAY((NODE)->uniques, UNIQUES); \
777 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN)
778
779/**
780 * @brief assert that lysc_node_leaf structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100781 *
Radek Išaa76902f2021-03-29 08:52:51 +0200782 * @param[in] NODE pointer to lysc_node variable
783 * @param[in] DSC expected description
784 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
785 * @param[in] FLAGS [schema node flags](@ref snodeflags)
786 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
787 * @param[in] NAME expected name
788 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
789 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
790 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
791 * @param[in] REF expected reference
792 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
Radek Išaa9ff2b82021-01-13 21:44:13 +0100793 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
794 * @param[in] UNITS expected string reprezenting units
795 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
796 */
797#define CHECK_LYSC_NODE_LEAF(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \
Michal Vasko2bf4af42023-01-04 12:08:38 +0100798 PARENT, PRIV, REF, WHEN, MUSTS, UNITS, DFLT) \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100799 CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LEAF, PARENT, PRIV, REF, WHEN); \
800 CHECK_ARRAY((NODE)->musts, MUSTS); \
801 assert_non_null((NODE)->type); \
802 CHECK_STRING((NODE)->units, UNITS); \
803 CHECK_POINTER((NODE)->dflt, DFLT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200804
805/**
806 * @brief assert that lyd_meta structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100807 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200808 * @param[in] NODE pointer to lyd_meta variable
809 * @param[in] ANNOTATION 0 pointer is null, 1 pointer is not null
810 * @param[in] NAME expected name
811 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
812 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
813 * part of text reprezenting LY_DATA_TYPE.
814 * @param[in] ... ::CHECK_LYD_VALUE
815 */
816#define CHECK_LYD_META(NODE, ANNOTATION, NAME, NEXT, PARENT, TYPE_VAL, ...) \
817 assert_non_null(NODE); \
818 CHECK_POINTER((NODE)->annotation, ANNOTATION); \
819 assert_string_equal((NODE)->name, NAME); \
820 CHECK_POINTER((NODE)->next, NEXT); \
821 CHECK_POINTER((NODE)->parent, PARENT); \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200822 CHECK_LYD_VALUE((NODE)->value, TYPE_VAL, __VA_ARGS__);
Radek Iša56ca9e42020-09-08 18:42:00 +0200823
824/**
825 * @brief assert that lyd_node_term structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100826 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200827 * @param[in] NODE pointer to lyd_node_term variable
828 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
829 * @param[in] META 0 -> meta is not prezent, 1 -> meta is prezent
830 * @param[in] NEXT 0 -> next node is not prezent, 1 -> next node is prezent
831 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
832 * part of text reprezenting LY_DATA_TYPE.
833 * @param[in] ... ::CHECK_LYD_VALUE
834 */
835#define CHECK_LYD_NODE_TERM(NODE, FLAGS, META, NEXT, PARENT, SCHEMA, VALUE_TYPE, ...) \
836 assert_non_null(NODE); \
837 assert_int_equal((NODE)->flags, FLAGS); \
838 CHECK_POINTER((NODE)->meta, META); \
839 CHECK_POINTER((NODE)->next, NEXT); \
840 CHECK_POINTER((NODE)->parent, PARENT); \
841 assert_non_null((NODE)->prev); \
842 CHECK_POINTER((NODE)->schema, SCHEMA); \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200843 CHECK_LYD_VALUE((NODE)->value, VALUE_TYPE, __VA_ARGS__);
Radek Iša56ca9e42020-09-08 18:42:00 +0200844
845/**
846 * @brief assert that lyd_node_any structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100847 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200848 * @param[in] NODE pointer to lyd_node_term variable
849 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
850 * @param[in] META 0 meta isnt present , 1 meta is present
851 * @param[in] PARENT 0 it is root node , 1 node have parent
852 * @param[in] VALUE_TYPE value type ::lyd_node_any.value
853 */
854#define CHECK_LYD_NODE_ANY(NODE, FLAGS, META, PARENT, VALUE_TYPE) \
855 assert_non_null(NODE); \
856 assert_int_equal((NODE)->flags, FLAGS); \
857 CHECK_POINTER((NODE)->meta, META); \
858 CHECK_POINTER((NODE)->meta, PARENT); \
859 assert_non_null((NODE)->prev); \
860 assert_non_null((NODE)->schema); \
861 assert_int_equal((NODE)->value_type, VALUE_TYPE);
862
863/**
864 * @brief assert that lyd_node_opaq structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100865 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200866 * @param[in] NODE pointer to lyd_node_opaq variable
867 * @param[in] ATTR 0 if pointer is null ,1 if pointer is not null
868 * @param[in] CHILD 0 if pointer is null ,1 if pointer is not null
869 * @param[in] FORMAT LY_PREF_XML or LY_PREF_JSON
870 * @param[in] VAL_PREFS 0 if pointer is null ,1 if pointer is not null
871 * @param[in] NAME expected name
872 * @param[in] value expected orignal value
873 */
874#define CHECK_LYD_NODE_OPAQ(NODE, ATTR, CHILD, FORMAT, NAME, NEXT, PARENT, PREFIX, VAL_PREFS, VALUE) \
875 assert_non_null(NODE); \
876 CHECK_POINTER((NODE)->attr, ATTR); \
877 CHECK_POINTER((NODE)->child, CHILD); \
878 assert_ptr_equal((NODE)->ctx, UTEST_LYCTX); \
879 assert_int_equal((NODE)->flags, 0); \
880 assert_true((NODE)->format == FORMAT); \
881 assert_int_equal((NODE)->hash, 0); \
882 assert_string_equal((NODE)->name.name, NAME); \
883 assert_non_null((NODE)->prev); \
884 assert_null((NODE)->schema); \
885 CHECK_POINTER((NODE)->val_prefix_data, VAL_PREFS); \
886 assert_string_equal((NODE)->value, VALUE);
887
888/**
Radek Išaa76902f2021-03-29 08:52:51 +0200889 * @brief assert that lyd_node_opaq structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100890 *
Radek Išaa76902f2021-03-29 08:52:51 +0200891 * @param[in] NODE pointer to lyd_node_opaq variable
892 * @param[in] CHILD 1 if node has children other 0
893 * @param[in] HILD_HT 1 if node has children hash table other 0
894 * @param[in] META 1 if node has metadata other 0
895 * @param[in] FLAGS expected flag
896 * @param[in] NEXT 1 if next node is present other 0
897 * @param[in] PARENT 1 if node has parent other 0
898 * @param[in] PRIV 1 if node has private data other 0
899 * @param[in] SCHEMA 1 if node has schema other 0
900*/
901#define CHECK_LYD_NODE_INNER(NODE, CHILD, CHILD_HT, META, FLAGS, NEXT, PARENT, PRIV, SCHEMA) \
902 assert_non_null(NODE); \
903 CHECK_POINTER((NODE)->child, CHILD); \
904 CHECK_POINTER((NODE)->children_ht, CHILD_HT); \
905 CHECK_POINTER((NODE)->meta, META); \
906 assert_int_equal((NODE)->flags, FLAGS); \
907 CHECK_POINTER((NODE)->parent, PARENT); \
908 assert_non_null((NODE)->prev); \
909 CHECK_POINTER((NODE)->next, NEXT); \
910 CHECK_POINTER((NODE)->priv, PRIV); \
911 CHECK_POINTER((NODE)->schema, SCHEMA)
912
913/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200914 * @brief assert that lyd_value structure members are correct
Michal Vaskoddd76592022-01-17 13:34:48 +0100915 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200916 * @param[in] NODE lyd_value
917 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
918 * part of text reprezenting LY_DATA_TYPE.
919 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
920 * by type of value. These parameters are passed to macro
921 * CHECK_LYD_VALUE_ ## TYPE_VAL.
922 */
923#define CHECK_LYD_VALUE(NODE, TYPE_VAL, ...) \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200924 CHECK_LYD_VALUE_ ## TYPE_VAL (NODE, __VA_ARGS__);
Radek Iša56ca9e42020-09-08 18:42:00 +0200925
926/*
927 * LYD VALUES CHECKING SPECIALIZATION
928 */
929
930/**
931 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type EMPTY
932 * Example CHECK_LYD_VALUE(node->value, EMPTY, "");
Michal Vaskoddd76592022-01-17 13:34:48 +0100933 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200934 * @param[in] NODE lyd_value variable
935 * @param[in] CANNONICAL_VAL expected cannonical value
936 */
937#define CHECK_LYD_VALUE_EMPTY(NODE, CANNONICAL_VAL) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200938 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200939 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200940 assert_non_null((NODE).realtype); \
941 assert_int_equal((NODE).realtype->basetype, LY_TYPE_EMPTY);
942
943/**
944 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UNION
945 * Example CHECK_LYD_VALUE(node->value, UNION, "12", INT8, "12", 12);
946 * @warning type of subvalue cannot be UNION. Example of calling
Michal Vaskoddd76592022-01-17 13:34:48 +0100947 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200948 * @param[in] NODE lyd_value variable
949 * @param[in] CANNONICAL_VAL expected cannonical value
950 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
951 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
952 * by type of value. These parameters are passed to macro
953 * CHECK_LYD_VALUE_ ## TYPE_VAL.
954 */
955#define CHECK_LYD_VALUE_UNION(NODE, CANNONICAL_VAL, TYPE_VAL, ...) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200956 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200957 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200958 assert_non_null((NODE).realtype); \
959 assert_int_equal(LY_TYPE_UNION, (NODE).realtype->basetype); \
960 assert_non_null((NODE).subvalue); \
961 assert_non_null((NODE).subvalue->prefix_data); \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200962 CHECK_LYD_VALUE_ ## TYPE_VAL ((NODE).subvalue->value, __VA_ARGS__)
963
964/**
965 * @brief Internal macro. Get 1st variadic argument.
966 */
967#define _GETARG1(ARG1, ...) ARG1
Radek Iša56ca9e42020-09-08 18:42:00 +0200968
969/**
970 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BITS
971 * Example arr[] = {"a", "b"}; CHECK_LYD_VALUE(node->value, BITS, "a b", arr);
Michal Vaskoddd76592022-01-17 13:34:48 +0100972 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200973 * @param[in] NODE lyd_value variable
974 * @param[in] CANNONICAL_VAL expected cannonical value
975 * @param[in] VALUE expected array of bits names
976 */
Michal Vasko151ae6c2021-09-23 08:23:51 +0200977#define CHECK_LYD_VALUE_BITS(NODE, ...) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200978 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200979 assert_string_equal((NODE)._canonical, _GETARG1(__VA_ARGS__, DUMMY)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200980 assert_non_null((NODE).realtype); \
981 assert_int_equal(LY_TYPE_BITS, (NODE).realtype->basetype); \
982 { \
Radek Išaded31052021-03-10 13:22:53 +0100983 const char *arr[] = { __VA_ARGS__ }; \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200984 LY_ARRAY_COUNT_TYPE arr_size = (sizeof(arr) / sizeof(arr[0])) - 1; \
Michal Vaskoaa0ee622021-05-11 09:29:25 +0200985 struct lyd_value_bits *_val; \
986 LYD_VALUE_GET(&(NODE), _val); \
987 assert_int_equal(arr_size, LY_ARRAY_COUNT(_val->items)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200988 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
Michal Vasko151ae6c2021-09-23 08:23:51 +0200989 assert_string_equal(arr[it + 1], _val->items[it]->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200990 } \
991 }
992
993/**
994 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INST
Michal Vaskoddd76592022-01-17 13:34:48 +0100995 *
Radek Iša56ca9e42020-09-08 18:42:00 +0200996 * @param[in] NODE lyd_value variable
997 * @param[in] CANNONICAL_VAL expected cannonical value
998 * @param[in] VALUE expected array of enum ly_path_pred_type
999 * @brief Example enum arr[] = {0x0, 0x1}; CHECK_LYD_VALUE(node->value, INST, "test/d", arr);
1000 */
1001#define CHECK_LYD_VALUE_INST(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001002 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001003 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001004 assert_non_null((NODE).realtype); \
1005 assert_int_equal(LY_TYPE_INST, (NODE).realtype->basetype); \
1006 { \
Michal Vasko79135ae2020-12-16 10:08:35 +01001007 LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001008 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).target)); \
1009 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
Michal Vasko90189962023-02-28 12:10:34 +01001010 if ((NODE).target[it].predicates) { \
1011 assert_int_equal(VALUE[it], (NODE).target[it].predicates[0].type); \
1012 } \
Radek Iša56ca9e42020-09-08 18:42:00 +02001013 } \
1014 }
1015
1016/**
1017 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type ENUM.
1018 * Example CHECK_LYD_VALUE(node->value, ENUM, "item_name", "item_name");
Michal Vaskoddd76592022-01-17 13:34:48 +01001019 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001020 * @param[in] NODE lyd_value variable
1021 * @param[in] CANNONICAL_VAL expected cannonical value
1022 * @param[in] VALUE expected enum item name
1023 */
1024#define CHECK_LYD_VALUE_ENUM(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001025 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001026 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001027 assert_non_null((NODE).realtype); \
1028 assert_int_equal(LY_TYPE_ENUM, (NODE).realtype->basetype); \
1029 assert_string_equal(VALUE, (NODE).enum_item->name);
1030
1031/**
1032 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT8
1033 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
Michal Vaskoddd76592022-01-17 13:34:48 +01001034 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001035 * @param[in] NODE lyd_value variable
1036 * @param[in] CANNONICAL_VAL expected cannonical value
1037 * @param[in] VALUE expected inteager value (-128 to 127).
1038 */
1039#define CHECK_LYD_VALUE_INT8(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001040 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001041 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001042 assert_non_null((NODE).realtype); \
1043 assert_int_equal(LY_TYPE_INT8, (NODE).realtype->basetype); \
1044 assert_int_equal(VALUE, (NODE).int8);
1045
1046/**
1047 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT16
1048 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
Michal Vaskoddd76592022-01-17 13:34:48 +01001049 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001050 * @param[in] NODE lyd_value variable
1051 * @param[in] CANNONICAL_VAL expected cannonical value
1052 * @param[in] VALUE expected inteager value.
1053 */
1054#define CHECK_LYD_VALUE_INT16(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001055 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001056 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001057 assert_non_null((NODE).realtype); \
1058 assert_int_equal(LY_TYPE_INT16, (NODE).realtype->basetype); \
1059 assert_int_equal(VALUE, (NODE).int16);
1060
1061/**
1062 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT8.
1063 * Example CHECK_LYD_VALUE(node->value, UINT8, "12", 12);
Michal Vaskoddd76592022-01-17 13:34:48 +01001064 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001065 * @param[in] NODE lyd_value variable
1066 * @param[in] CANNONICAL_VAL expected cannonical value
1067 * @param[in] VALUE expected inteager (0 to 255).
1068 */
1069#define CHECK_LYD_VALUE_UINT8(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001070 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001071 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001072 assert_non_null((NODE).realtype); \
1073 assert_int_equal(LY_TYPE_UINT8, (NODE).realtype->basetype); \
1074 assert_int_equal(VALUE, (NODE).uint8);
1075
1076/**
Radek Išaa76902f2021-03-29 08:52:51 +02001077 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT32.
1078 * Example CHECK_LYD_VALUE(node->value, UINT32, "12", 12);
Michal Vaskoddd76592022-01-17 13:34:48 +01001079 *
Radek Išaa76902f2021-03-29 08:52:51 +02001080 * @param[in] NODE lyd_value variable
1081 * @param[in] CANNONICAL_VAL expected cannonical value
1082 * @param[in] VALUE expected inteager (0 to MAX_UINT32).
1083 */
1084#define CHECK_LYD_VALUE_UINT32(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001085 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001086 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Išaa76902f2021-03-29 08:52:51 +02001087 assert_non_null((NODE).realtype); \
1088 assert_int_equal(LY_TYPE_UINT32, (NODE).realtype->basetype); \
1089 assert_int_equal(VALUE, (NODE).uint32);
1090
1091/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001092 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type STRING.
1093 * Example CHECK_LYD_VALUE(node->value, STRING, "text");
Michal Vaskoddd76592022-01-17 13:34:48 +01001094 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001095 * @param[in] NODE lyd_value variable
1096 * @param[in] CANNONICAL_VAL expected cannonical value
1097 */
1098#define CHECK_LYD_VALUE_STRING(NODE, CANNONICAL_VAL) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001099 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001100 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001101 assert_non_null((NODE).realtype); \
1102 assert_int_equal(LY_TYPE_STRING, (NODE).realtype->basetype);
1103
1104/**
1105 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type LEAFREF
Michal Vaskoddd76592022-01-17 13:34:48 +01001106 * Example CHECK_LYD_VALUE(node->value, LEAFREF, "");
1107 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001108 * @param[in] NODE lyd_value variable
1109 * @param[in] CANNONICAL_VAL expected cannonical value
1110 */
1111#define CHECK_LYD_VALUE_LEAFREF(NODE, CANNONICAL_VAL) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001112 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001113 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001114 assert_non_null((NODE).realtype); \
1115 assert_int_equal(LY_TYPE_LEAFREF, (NODE).realtype->basetype); \
1116 assert_non_null((NODE).ptr)
1117
1118/**
1119 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type DEC64
1120 * Example CHECK_LYD_VALUE(node->value, DEC64, "125", 125);
Michal Vaskoddd76592022-01-17 13:34:48 +01001121 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001122 * @param[in] NODE lyd_value variable
1123 * @param[in] CANNONICAL_VAL expected cannonical value
1124 * @param[in] VALUE expected value 64bit inteager
1125*/
1126#define CHECK_LYD_VALUE_DEC64(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001127 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001128 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001129 assert_non_null((NODE).realtype); \
1130 assert_int_equal(LY_TYPE_DEC64, (NODE).realtype->basetype); \
1131 assert_int_equal(VALUE, (NODE).dec64);
1132
1133/**
1134 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BINARY.
1135 * Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8=");
Michal Vaskoddd76592022-01-17 13:34:48 +01001136 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001137 * @param[in] NODE lyd_value variable
1138 * @param[in] CANNONICAL_VAL expected cannonical value
Michal Vasko495f4502021-04-27 14:48:05 +02001139 * @param[in] VALUE expected value data
1140 * @param[in] SIZE expected value data size
Radek Iša56ca9e42020-09-08 18:42:00 +02001141*/
Michal Vasko495f4502021-04-27 14:48:05 +02001142#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL, VALUE, SIZE) \
Michal Vaskoaa0ee622021-05-11 09:29:25 +02001143 { \
1144 struct lyd_value_binary *_val; \
1145 LYD_VALUE_GET(&(NODE), _val); \
1146 assert_int_equal(_val->size, SIZE); \
1147 assert_int_equal(0, memcmp(_val->data, VALUE, SIZE)); \
1148 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
1149 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
1150 assert_non_null((NODE).realtype); \
1151 assert_int_equal(LY_TYPE_BINARY, (NODE).realtype->basetype); \
1152 }
Radek Iša56ca9e42020-09-08 18:42:00 +02001153
1154/**
1155 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BOOL.
1156 * Example CHECK_LYD_VALUE(node->value, BOOL, "true", 1);
Michal Vaskoddd76592022-01-17 13:34:48 +01001157 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001158 * @param[in] NODE lyd_value variable
1159 * @param[in] CANNONICAL_VAL expected cannonical value
1160 * @param[in] VALUE expected boolean value 0,1
1161*/
1162#define CHECK_LYD_VALUE_BOOL(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001163 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001164 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001165 assert_non_null((NODE).realtype); \
1166 assert_int_equal(LY_TYPE_BOOL, (NODE).realtype->basetype); \
1167 assert_int_equal(VALUE, (NODE).boolean);
1168
1169/**
1170 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type IDENT.
1171 * Example CHECK_LYD_VALUE(node->value, IDENT, "types:gigabit-ethernet", "gigabit-ethernet");
Michal Vaskoddd76592022-01-17 13:34:48 +01001172 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001173 * @param[in] NODE lyd_value variable
1174 * @param[in] CANNONICAL_VAL expected cannonical value
1175 * @param[in] VALUE expected ident name
1176*/
1177#define CHECK_LYD_VALUE_IDENT(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001178 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001179 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001180 assert_non_null((NODE).realtype); \
1181 assert_int_equal(LY_TYPE_IDENT, (NODE).realtype->basetype); \
1182 assert_string_equal(VALUE, (NODE).ident->name);
1183
1184/**
Radek Išaa9ff2b82021-01-13 21:44:13 +01001185 * @brief Macro testing parser when parsing incorrect module;
Michal Vaskoddd76592022-01-17 13:34:48 +01001186 *
Radek Išaa9ff2b82021-01-13 21:44:13 +01001187 * @param[in] DATA String storing the schema module representation.
1188 * @param[in] FORMAT Schema format of the @p DATA
1189 * @param[in] FEATURES Array of module's features to enable
1190 * @param[in] RET_VAL ly_in_new_memory return error value
1191 */
1192#define UTEST_INVALID_MODULE(DATA, FORMAT, FEATURES, RET_VAL) \
1193 { \
Michal Vasko4de7d072021-07-09 09:13:18 +02001194 struct lys_module *mod; \
Radek Išaa9ff2b82021-01-13 21:44:13 +01001195 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
1196 assert_int_equal(RET_VAL, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, &mod)); \
1197 assert_null(mod); \
1198 } \
1199 ly_in_free(_UC->in, 0); \
1200 _UC->in = NULL; \
1201
1202/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001203 * @brief Add module (from a string) into the used libyang context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001204 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001205 * @param[in] DATA String storing the schema module representation.
1206 * @param[in] FORMAT Schema format of the @p DATA
1207 * @param[in] FEATURES Array of module's features to enable
1208 * @param[out] MOD Optional parameter as a pointer to variable to store the resulting module.
1209 */
1210#define UTEST_ADD_MODULE(DATA, FORMAT, FEATURES, MOD) \
1211 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
Michal Vasko193dacd2022-10-13 08:43:05 +02001212 { \
1213 LY_ERR __r = lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, MOD); \
1214 if (__r != LY_SUCCESS) { \
1215 print_message("[ MSG ] Module parsing failed:\n"); \
Michal Vasko7a266772024-01-23 11:02:38 +01001216 for (const struct ly_err_item *e = ly_err_first(_UC->ctx); e; e = e->next) { \
1217 print_message("[ MSG ] \t%s Schema path %s\n", e->msg, e->schema_path); \
Michal Vasko193dacd2022-10-13 08:43:05 +02001218 } \
1219 fail(); \
1220 } \
1221 } \
Radek Iša56ca9e42020-09-08 18:42:00 +02001222 ly_in_free(_UC->in, 0); \
1223 _UC->in = NULL
1224
1225/**
Michal Vasko236cbac2023-02-10 15:45:37 +01001226 * @brief Check expected last error message.
1227 *
1228 * @param[in] MSG Expected error message.
1229 */
1230#define CHECK_LOG_LASTMSG(MSG) \
Michal Vasko7a266772024-01-23 11:02:38 +01001231 CHECK_STRING(ly_last_logmsg(), MSG)
Michal Vasko236cbac2023-02-10 15:45:37 +01001232
1233/**
Michal Vasko62af3692023-02-09 14:00:09 +01001234 * @brief Check expected last error in libyang context, which is then cleared. Can be called repeatedly to check
1235 * several errors. If NULL is provided as MSG, no error info record (NULL) is expected.
Michal Vaskoddd76592022-01-17 13:34:48 +01001236 *
Radek Iša56ca9e42020-09-08 18:42:00 +02001237 * @param[in] MSG Expected error message.
Michal Vasko7a266772024-01-23 11:02:38 +01001238 * @param[in] PATH Expected error data/schema path.
1239 * @param[in] LINE Expected error line.
Radek Iša56ca9e42020-09-08 18:42:00 +02001240 */
Michal Vasko7a266772024-01-23 11:02:38 +01001241#define CHECK_LOG_CTX(MSG, PATH, LINE) \
Michal Vasko62af3692023-02-09 14:00:09 +01001242 { \
Michal Vasko7a266772024-01-23 11:02:38 +01001243 struct ly_err_item *_e = (struct ly_err_item *)ly_err_last(_UC->ctx); \
Michal Vasko62af3692023-02-09 14:00:09 +01001244 if (!MSG) { \
1245 assert_null(_e); \
1246 } else { \
1247 assert_non_null(_e); \
1248 CHECK_STRING(_e->msg, MSG); \
Michal Vasko7a266772024-01-23 11:02:38 +01001249 if (_e->data_path) { \
1250 CHECK_STRING(_e->data_path, PATH); \
1251 } else { \
1252 CHECK_STRING(_e->schema_path, PATH); \
1253 } \
1254 assert_int_equal(_e->line, LINE); \
Michal Vasko62af3692023-02-09 14:00:09 +01001255 } \
1256 ly_err_clean(_UC->ctx, _e); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001257 }
1258
Radek Iša56ca9e42020-09-08 18:42:00 +02001259/**
Michal Vaskoe9391c72021-10-05 10:04:56 +02001260 * @brief Check expected error in libyang context including error-app-tag.
1261 *
1262 * @param[in] MSG Expected error message.
Michal Vasko7a266772024-01-23 11:02:38 +01001263 * @param[in] PATH Expected error data/schema path.
1264 * @param[in] LINE Expected error line.
Michal Vaskoe9391c72021-10-05 10:04:56 +02001265 * @param[in] APPTAG Expected error-app-tag.
1266 */
Michal Vasko7a266772024-01-23 11:02:38 +01001267#define CHECK_LOG_CTX_APPTAG(MSG, PATH, LINE, APPTAG) \
Michal Vasko62af3692023-02-09 14:00:09 +01001268 { \
Michal Vasko7a266772024-01-23 11:02:38 +01001269 struct ly_err_item *_e = (struct ly_err_item *)ly_err_last(_UC->ctx); \
Michal Vasko62af3692023-02-09 14:00:09 +01001270 if (!MSG) { \
1271 assert_null(_e); \
1272 } else { \
1273 assert_non_null(_e); \
1274 CHECK_STRING(_e->msg, MSG); \
Michal Vasko7a266772024-01-23 11:02:38 +01001275 if (_e->data_path) { \
1276 CHECK_STRING(_e->data_path, PATH); \
1277 } else { \
1278 CHECK_STRING(_e->schema_path, PATH); \
1279 } \
1280 assert_int_equal(_e->line, LINE); \
Michal Vasko62af3692023-02-09 14:00:09 +01001281 CHECK_STRING(_e->apptag, APPTAG); \
1282 } \
1283 ly_err_clean(_UC->ctx, _e); \
1284 }
1285
1286/**
1287 * @brief Clear all errors stored in the libyang context.
1288 */
1289#define UTEST_LOG_CTX_CLEAN \
Michal Vaskoe9391c72021-10-05 10:04:56 +02001290 ly_err_clean(_UC->ctx, NULL)
1291
Radek Iša56ca9e42020-09-08 18:42:00 +02001292#ifdef _UTEST_MAIN_
1293/*
1294 * Functions inlined into each C source file including this header with _UTEST_MAIN_ defined
1295 */
1296
1297/**
1298 * @brief Global variable holding the tests context to simplify access to it.
1299 */
1300struct utest_context *current_utest_context;
1301
Radek Iša56ca9e42020-09-08 18:42:00 +02001302/**
1303 * @brief Generic utest's setup
1304 */
1305static int
1306utest_setup(void **state)
1307{
Michal Vasko86421632021-05-04 13:11:25 +02001308 char *cur_tz;
1309
Radek Iša56ca9e42020-09-08 18:42:00 +02001310 /* setup the logger */
Michal Vasko7a266772024-01-23 11:02:38 +01001311 ly_log_options(/*LY_LOLOG |*/ LY_LOSTORE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001312
1313 current_utest_context = calloc(1, sizeof *current_utest_context);
1314 assert_non_null(current_utest_context);
1315 *state = current_utest_context;
1316
1317 /* libyang context */
1318 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &current_utest_context->ctx));
1319
Michal Vasko86421632021-05-04 13:11:25 +02001320 /* backup timezone, if any */
1321 cur_tz = getenv("TZ");
1322 if (cur_tz) {
1323 current_utest_context->orig_tz = strdup(cur_tz);
1324 }
1325
1326 /* set CET */
1327 setenv("TZ", "CET+02:00", 1);
1328
Radek Iša56ca9e42020-09-08 18:42:00 +02001329 return 0;
1330}
1331
1332/**
1333 * @brief macro to include generic utest's setup into the test-specific setup.
1334 *
1335 * Place at the beginning of the test-specific setup
1336 */
1337#define UTEST_SETUP \
1338 assert_int_equal(0, utest_setup(state))
1339
1340/**
1341 * @brief Generic utest's teardown
1342 */
1343static int
1344utest_teardown(void **state)
1345{
1346 *state = NULL;
1347
Michal Vasko62af3692023-02-09 14:00:09 +01001348 /* libyang context, no leftover messages */
1349 assert_null(ly_err_last(current_utest_context->ctx));
Radek Krejci90ed21e2021-04-12 14:47:46 +02001350 ly_ctx_destroy(current_utest_context->ctx);
Radek Iša56ca9e42020-09-08 18:42:00 +02001351
Michal Vasko86421632021-05-04 13:11:25 +02001352 if (current_utest_context->orig_tz) {
1353 /* restore TZ */
1354 setenv("TZ", current_utest_context->orig_tz, 1);
1355 }
1356
Radek Iša56ca9e42020-09-08 18:42:00 +02001357 /* utest context */
1358 ly_in_free(current_utest_context->in, 0);
Michal Vasko86421632021-05-04 13:11:25 +02001359 free(current_utest_context->orig_tz);
Radek Iša56ca9e42020-09-08 18:42:00 +02001360 free(current_utest_context);
1361 current_utest_context = NULL;
1362
1363 return 0;
1364}
1365
1366/**
1367 * @brief macro to include generic utest's teardown into the test-specific teardown.
1368 *
1369 * Place at the end of the test-specific teardown
1370 */
1371#define UTEST_TEARDOWN \
1372 assert_int_equal(0, utest_teardown(state))
1373
1374/**
1375 * @brief Internal macro for utest setup with test-specific setup and teardown
1376 */
1377#define _UTEST_SETUP_TEARDOWN(FUNC, SETUP, TEARDOWN) \
1378 cmocka_unit_test_setup_teardown(FUNC, SETUP, TEARDOWN)
1379
1380/**
1381 * @brief Internal macro for utest setup with test-specific setup and generic teardown
1382 */
1383#define _UTEST_SETUP(FUNC, SETUP) \
1384 cmocka_unit_test_setup_teardown(FUNC, SETUP, utest_teardown)
1385
1386/**
1387 * @brief Internal macro for utest setup with generic setup and teardown
1388 */
1389#define _UTEST(FUNC) \
1390 cmocka_unit_test_setup_teardown(FUNC, utest_setup, utest_teardown)
1391
1392/**
1393 * @brief Internal helper macro to select _UTEST* macro according to the provided parameters.
1394 */
1395#define _GET_UTEST_MACRO(_1, _2, _3, NAME, ...) NAME
1396
1397/**
1398 * @brief Macro to specify test function using utest environment. Macro has variadic parameters
1399 * to provide test-specific setup/teardown functions:
1400 *
1401 * UTEST(test_func) - only implicit setup and teardown functions are used
1402 * UTEST(test_func, setup) - implicit teardown but own setup
1403 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
1404 */
1405#define UTEST(...) \
Michal Vasko151ae6c2021-09-23 08:23:51 +02001406 _GET_UTEST_MACRO(__VA_ARGS__, _UTEST_SETUP_TEARDOWN, _UTEST_SETUP, _UTEST, DUMMY)(__VA_ARGS__)
Radek Iša56ca9e42020-09-08 18:42:00 +02001407
1408#else /* _UTEST_MAIN_ */
1409
1410extern struct utest_context *current_utest_context;
1411
1412#endif /* _UTEST_MAIN_ */
1413
1414#endif /* _UTESTS_H_ */