blob: 50ec188c1539e3718706d4993f67da6a341d57b6 [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>
5 * @brief this file contains macros for simplification test writing
6 *
Radek Išaa9ff2b82021-01-13 21:44:13 +01007 * Copyright (c) 2021 CESNET, z.s.p.o.
Radek Iša56ca9e42020-09-08 18:42:00 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#ifndef _UTESTS_H_
17#define _UTESTS_H_
18
19#define _POSIX_C_SOURCE 200809L /* strdup */
20
Radek Krejcib4ac5a92020-11-23 17:54:33 +010021#include <setjmp.h>
22#include <stdarg.h>
23#include <stddef.h>
Michal Vasko86421632021-05-04 13:11:25 +020024#include <stdlib.h>
Radek Krejcib4ac5a92020-11-23 17:54:33 +010025
26#include <cmocka.h>
Radek Iša56ca9e42020-09-08 18:42:00 +020027
28#include <string.h>
29
30#include "libyang.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010031#include "plugins_internal.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010032#include "plugins_types.h"
Radek Krejcief5f7672021-04-01 17:04:12 +020033#include "tests_config.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010034#include "tree_schema_internal.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020035
36/**
37 * TESTS OVERVIEW
38 *
39 * To include utest's environment, just include "utests.h" in the test's source
40 * code. In case it is the main source code for a cmocka test group (there is a
41 * main() function), define _UTEST_MAIN_ before including this header.
42 *
43 * TESTS VARIABLES
44 *
45 * Checking macros use internal storage to store various variables necessary
46 * during the checking. It is possible to access these variables using the
47 * following macros:
48 *
49 * UTEST_LYCTX - libyang context
50 * UTEST_IN - input handler
51 * UTEST_OUT - output handler
52 *
53 * All these variables are cleaned with test's teardown.
54 *
55 * TESTS SETUP
56 *
57 * CMocka's CMUnitTest list definition macros (cmoka_unit_test*()) are replaced
58 * by UTEST macro with possibility to specify own setup and teardown functions:
59 *
60 * UTEST(test_func) - only implicit setup and teardown functions are used
61 * UTEST(test_func, setup) - implicit teardown but own setup
62 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
63 *
64 * Note that the tests environment always provide (and need) internal setup and
65 * teardown functions. In case the test-specific setup or teardown are used, they
66 * are supposed to include UTEST_SETUP at the setup beginning and UTEST_TEARDOWN
67 * at the teardown end.
68 *
69 * Libyang context is part of the prepared environment. To add a schema into the
70 * context (despite it is in the test-specific setup or in test function itself),
71 * use UTEST_ADD_MODULE macro.
72 *
73 * LOGGING
74 *
75 * There are 2 macros to check content of the log from the previously called
76 * libyang function. CHECK_LOG macro test only the last error message and path
77 * stored directly via logging callback. CHECK_LOG_CTX gets error message and
78 * path from the libyang context (in case the function does not store the error
79 * information into the libyang context, the message cannot be checked this way).
80 * libyang is set to store multiple error information, so multiple couples of
81 * error message and path can be provided to be checked (the first couple
82 * corresponds to the latest error). The macro also cleanups the errors list, so
83 * it is fine to check that there is no error after succeeding successful
84 * function call.
85 */
86
87/**
88 * @brief Test's context to provide common storage for various variables.
89 */
90struct utest_context {
91 struct ly_ctx *ctx; /**< libyang context */
92
93 char *err_msg; /**< Directly logged error message */
94 char *err_path; /**< Directly logged error path */
95
96 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.
129 * @param[in] OUT_STATUS 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 representation of an empty YANG data tree.
131 * The returned data are expected to be freed using LYD_TREE_DESTROY().
132 */
133#define CHECK_PARSE_LYD_PARAM(INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, OUT_STATUS, OUT_NODE) \
134 assert_int_equal(OUT_STATUS, lyd_parse_data_mem(_UC->ctx, INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, &OUT_NODE)); \
135 if (OUT_STATUS == LY_SUCCESS) { \
136 assert_non_null(OUT_NODE); \
137 } else { \
138 assert_null(OUT_NODE); \
139 }
140
141/**
142 * @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.
143 * @param[in] NODE pointer to lyd_node
144 * @param[in] TEXT expected output string in json or xml format.
145 * @param[in] FORMAT format of input text. LYD_JSON, LYD_XML
146 * @param[in] PARAM options [Data printer flags](@ref dataprinterflags).
147 */
148#define CHECK_LYD_STRING_PARAM(NODE, TEXT, FORMAT, PARAM) \
149 { \
150 char *test; \
151 lyd_print_mem(&test, NODE, FORMAT, PARAM); \
152 assert_string_equal(test, TEXT); \
153 free(test); \
154 }
155
156/**
157 * @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;
158 * @param[in] NODE_1 pointer to lyd_node
159 * @param[in] NODE_2 pointer to lyd_node
160 */
161#define CHECK_LYD(NODE_1, NODE_2) \
162 { \
163 char *test_1; \
164 char *test_2; \
165 lyd_print_mem(&test_1, NODE_1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
166 lyd_print_mem(&test_2, NODE_2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
167 assert_string_equal(test_1, test_2); \
168 free(test_1); \
169 free(test_2); \
170 }
171
172/*
173 * SUPPORT MACROS
174 */
175
176/**
177 * @brief Internal macro witch assert that two given string are equal or are both null.
178 *
179 * @param[in] STRING string to check
180 * @param[in] TEXT string to compare
181 */
182#define CHECK_STRING(STRING, TEXT)\
183 if (TEXT == NULL) { \
184 assert_null(STRING); \
185 } else { \
186 assert_non_null(STRING); \
187 assert_string_equal(STRING, TEXT); \
188 }
189
190/**
191 * @brief Internal macro witch assert that pointer is null when flag is 0.
192 *
193 * @param[in] POINTER pointer to check
194 * @param[in] FLAG 0 -> pointer is NULL, 1 -> pointer is not null
195 */
196#define CHECK_POINTER(POINTER, FLAG) \
197 assert_true(FLAG == 0 ? POINTER == NULL : POINTER != NULL)
198
199/**
200 * @brief Internal macro check size of [sized array](@ref sizedarrays)'s
201 *
202 * @param[in] ARRAY pointer to [sized array](@ref sizedarrays)
203 * @param[in] SIZE expected [sized array](@ref sizedarrays) size of array
204 */
205#define CHECK_ARRAY(ARRAY, SIZE) \
206 assert_true((SIZE == 0) ? \
207 (ARRAY == NULL) : \
208 (ARRAY != NULL && SIZE == LY_ARRAY_COUNT(ARRAY)));
209
210/*
211 * LIBYANG NODE CHECKING
212 */
213
214/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100215 * @brief check compileted type
216 * @param[in] NODE pointer to lysc_type value
217 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
218 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
219 */
220#define CHECK_LYSC_TYPE(NODE, TYPE, EXTS) \
221 assert_non_null(NODE); \
222 assert_int_equal((NODE)->basetype, TYPE); \
223 CHECK_ARRAY((NODE)->exts, EXTS); \
Radek Krejci3e6632f2021-03-22 22:08:21 +0100224 assert_ptr_equal((NODE)->plugin, lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[TYPE]))
Radek Išaa9ff2b82021-01-13 21:44:13 +0100225
226/* @brief check compileted numeric type
227 * @param[in] NODE pointer to lysc_type_num value
228 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
229 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
230 * @warning only integer types INT, UINT, NUM
231 */
232#define CHECK_LYSC_TYPE_NUM(NODE, TYPE, EXTS, RANGE) \
233 CHECK_LYSC_TYPE(NODE, TYPE, EXTS);\
234 CHECK_POINTER((NODE)->range, RANGE)
235
236/* @brief check compiled string type
237 * @param[in] NODE pointer to lysc_type_num value
238 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
239 * @param[in] LENGTH 0 -> node dosnt have length limitation, 1 -> node have length limitation
240 * @param[in] PATTERNS expected number of patterns [sized array](@ref sizedarrays)
241 * @warning only integer types INT, UINT, NUM
242 */
243#define CHECK_LYSC_TYPE_STR(NODE, EXTS, LENGTH, PATTERNS) \
244 CHECK_LYSC_TYPE(NODE, LY_TYPE_STRING, EXTS); \
245 CHECK_POINTER((NODE)->length, LENGTH); \
246 CHECK_ARRAY((NODE)->patterns, PATTERNS)
247
Radek Išaded31052021-03-10 13:22:53 +0100248/* @brief check compiled bits type
249 * @param[in] NODE pointer to lysc_type_num value
250 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
251 * @param[in] BITS expected number of bits
252 * @warning only integer types INT, UINT, NUM
253 */
254#define CHECK_LYSC_TYPE_BITS(NODE, EXTS, BITS) \
255 CHECK_LYSC_TYPE(NODE, LY_TYPE_BITS, EXTS); \
256 CHECK_ARRAY((NODE)->bits, BITS)
257
Radek Išaded31052021-03-10 13:22:53 +0100258#define CHECK_LYSC_TYPE_BITENUM_ITEM(NODE, POSITION, DSC, EXTS, FLAGS, NAME, REF)\
259 assert_non_null(NODE); \
260 assert_int_equal((NODE)->position, POSITION); \
261 CHECK_STRING((NODE)->dsc, DSC); \
262 CHECK_ARRAY((NODE)->exts, EXTS); \
263 assert_int_equal((NODE)->flags, FLAGS); \
264 CHECK_STRING((NODE)->name, NAME); \
265 CHECK_STRING((NODE)->ref, REF) \
266
Radek Išaa9ff2b82021-01-13 21:44:13 +0100267/* @brief check range
268 * @param[in] NODE pointer to lysc_range value
269 * @param[in] DSC expected descriptin (string)
270 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
271 * @param[in] EMSG expected string reprezenting error message
272 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
273 * @param[in] PARTS expected [sized array](@ref sizedarrays) number of rang limitations
274 * @param[in] REF expected reference
275 */
276#define CHECK_LYSC_RANGE(NODE, DSC, EAPPTAG, EMSG, EXTS, PARTS, REF) \
277 assert_non_null(NODE); \
278 CHECK_STRING((NODE)->dsc, DSC); \
279 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
280 CHECK_STRING((NODE)->emsg, EMSG); \
281 CHECK_ARRAY((NODE)->exts, EXTS); \
282 CHECK_ARRAY((NODE)->parts, PARTS); \
283 CHECK_STRING((NODE)->ref, REF)
284
285/* @brief check pattern
286 * @param[in] NODE pointer to lysc_pattern 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] EEXPR expected string reprezenting original, not compiled, regular expression
291 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
292 * @param[in] INVERTED if regular expression is inverted.
293 * @param[in] REF expected reference
294 */
295#define CHECK_LYSC_PATTERN(NODE, DSC, EAPPTAG, EMSG, EXPR, EXTS, INVERTED, REF) \
296 assert_non_null(NODE); \
297 assert_non_null((NODE)->code); \
298 CHECK_STRING((NODE)->dsc, DSC); \
299 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
300 CHECK_STRING((NODE)->emsg, EMSG); \
301 CHECK_STRING((NODE)->expr, EXPR); \
302 CHECK_ARRAY((NODE)->exts, EXTS); \
303 assert_int_equal((NODE)->inverted, INVERTED); \
304 CHECK_STRING((NODE)->ref, REF)
305
306/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200307 * @brief assert that lysp_action_inout structure members are correct
308 * @param[in] NODE pointer to lysp_action_inout variable
309 * @param[in] DATA 0 -> check if pointer to data is NULL, 1 -> check if pointer to data is not null
310 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
311 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
312 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of must restriction list
313 * @param[in] NODETYPE node type. LYS_INPUT or LYS_OUTPUT
314 * @param[in] PARENT 0 -> check if node is root, 1 -> check if node is not root
315 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of typedefs list
316 */
317#define CHECK_LYSP_ACTION_INOUT(NODE, DATA, EXTS, GROUPINGS, MUSTS, NODETYPE, PARENT, TYPEDEFS) \
318 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100319 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 CHECK_ARRAY((NODE)->exts, EXTS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100321 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200322 CHECK_ARRAY((NODE)->musts, MUSTS); \
323 assert_int_equal((NODE)->nodetype, NODETYPE); \
324 CHECK_POINTER((NODE)->parent, PARENT); \
325 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS);
326
327/**
328 * @brief assert that lysp_action structure members are correct
329 * @param[in] NODE pointer to lysp_action variable
330 * @param[in] DSC expected description
331 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extension list
332 * @param[in] FLAGS expected [schema node flags](@ref snodeflags)
333 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
334 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of if-feature expressions list
335 * @param[in] INPUT_* ::LYSP_ACTION_INOUT_CHECK
336 * @param[in] NAME expected name
337 * @param[in] NODETYPE node type. LYS_RPC or LYS_ACTION
338 * @param[in] OUTPUT_* ::LYSP_ACTION_INOUT_CHECK
339 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
340 * @param[in] REF expected reference
341 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of list of typedefs
342 */
343#define CHECK_LYSP_ACTION(NODE, DSC, EXTS, FLAGS, GROUPINGS, IFFEATURES, \
344 INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, INPUT_MUSTS, \
345 INPUT_PARENT, INPUT_TYPEDEFS, \
346 NAME, NODETYPE, \
347 OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, OUTPUT_MUSTS, \
348 OUTPUT_PARENT, OUTPUT_TYPEDEFS, \
349 PARENT, REF, TYPEDEFS) \
350 assert_non_null(NODE); \
351 CHECK_STRING((NODE)->dsc, DSC); \
352 CHECK_ARRAY((NODE)->exts, EXTS); \
353 assert_int_equal((NODE)->flags, FLAGS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100354 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200355 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
356 CHECK_LYSP_ACTION_INOUT(&((NODE)->input), INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, \
357 INPUT_MUSTS, LYS_INPUT, INPUT_PARENT, INPUT_TYPEDEFS); \
358 assert_string_equal((NODE)->name, NAME); \
359 assert_int_equal((NODE)->nodetype, NODETYPE); \
360 CHECK_LYSP_ACTION_INOUT(&((NODE)->output), OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, \
361 OUTPUT_MUSTS, LYS_OUTPUT, OUTPUT_PARENT, OUTPUT_TYPEDEFS); \
362 CHECK_POINTER((NODE)->parent, PARENT); \
363 CHECK_STRING((NODE)->ref, REF); \
364 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS) \
365
366/**
367 * @brief assert that lysp_when structure members are correct
368 * @param[in] NODE pointer to lysp_when variable
369 * @param[in] COND expected string specifid condition
370 * @param[in] DSC expected string description statement
371 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
372 * @param[in] REF expected string reference
373 */
374#define CHECK_LYSP_WHEN(NODE, COND, DSC, EXTS, REF) \
375 assert_non_null(NODE); \
376 assert_string_equal((NODE)->cond, COND); \
377 CHECK_STRING((NODE)->dsc, DSC); \
378 CHECK_ARRAY((NODE)->exts, EXTS); \
379 if (REF == NULL) { \
380 assert_null((NODE)->ref); \
381 } else { \
382 assert_non_null((NODE)->ref); \
383 assert_string_equal((NODE)->ref, REF); \
384 }
385
386/**
387 * @brief assert that lysp_restr structure members are correct
388 * @param[in] NODE pointer to lysp_restr variable
389 * @param[in] ARG_STR expected string. The restriction expression/value
390 * @param[in] DSC expected descrition
391 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
392 * @param[in] EMSG expected string reprezenting error message
393 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
394 * @param[in] REF expected reference
395 */
396
397#define CHECK_LYSP_RESTR(NODE, ARG_STR, DSC, EAPPTAG, EMSG, EXTS, REF) \
398 assert_non_null(NODE); \
399 assert_non_null((NODE)->arg.mod); \
400 assert_string_equal((NODE)->arg.str, ARG_STR); \
401 CHECK_STRING((NODE)->dsc, DSC); \
402 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
403 CHECK_STRING((NODE)->emsg, EMSG); \
404 CHECK_ARRAY((NODE)->exts, EXTS); \
405 CHECK_STRING((NODE)->ref, REF);
406
407/**
408 * @brief assert that lysp_import structure members are correct
409 * @param[in] NODE pointer to lysp_import variable
410 * @param[in] DSC expected description or NULL
411 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extensions
412 * @param[in] NAME expected name of imported module
413 * @param[in] PREFIX expected prefix for the data from the imported schema
414 * @param[in] REF expected reference
415 * @prame[in] REV expected reprezenting date in format "11-10-2020"
416 */
417#define CHECK_LYSP_IMPORT(NODE, DSC, EXTS, NAME, PREFIX, REF, REV) \
418 assert_non_null(NODE); \
419 CHECK_STRING((NODE)->dsc, DSC); \
420 CHECK_ARRAY((NODE)->exts, EXTS); \
421 /*assert_non_null((NODE)->module); // ?? it is mandatory but in some test it doesnt work */ \
422 assert_string_equal((NODE)->name, NAME); \
423 assert_string_equal((NODE)->prefix, PREFIX); \
424 CHECK_STRING((NODE)->ref, REF); \
425 CHECK_STRING((NODE)->rev, REV); \
426
427/**
428 * @brief assert that lysp_ext structure members are correct
429 * @param[in] NODE pointer to lysp_ext_instance variable
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100430 * @param[in] ARGNAME expected argument name
Radek Iša56ca9e42020-09-08 18:42:00 +0200431 * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists
432 * @param[in] DSC expected string reprezent description
433 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
434 * @param[in] FLAGS expected LYS_STATUS_* and LYS_YINELEM_* values (@ref snodeflags)
435 * @param[in] NAME expected name
436 * @param[in] REF expected ref
437 */
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100438#define CHECK_LYSP_EXT(NODE, ARGNAME, COMPILED, DSC, EXTS, FLAGS, NAME, REF) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200439 assert_non_null(NODE); \
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100440 CHECK_STRING((NODE)->argname, ARGNAME); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200441 CHECK_POINTER((NODE)->compiled, COMPILED); \
442 CHECK_STRING((NODE)->dsc, DSC); \
443 CHECK_ARRAY((NODE)->exts, EXTS); \
444 assert_int_equal((NODE)->flags, FLAGS); \
445 assert_string_equal((NODE)->name, NAME); \
446 CHECK_STRING((NODE)->ref, REF);
447
448/**
449 * @brief assert that lysp_ext_instance structure members are correct
450 * @param[in] NODE pointer to lysp_ext_instance variable
451 * @param[in] ARGUMENT expected optional value of the extension's argument
452 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
Radek Krejciab430862021-03-02 20:13:40 +0100453 * @param[in] PARENT_STMT expected value identifying placement of the extension instance
454 * @param[in] PARENT_STMT_INDEX expected indentifi index
Michal Vaskofc2cd072021-02-24 13:17:17 +0100455 * @param[in] FORMAT expected format
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 */
Radek Krejci85ac8312021-03-03 20:21:33 +0100457#define CHECK_LYSP_EXT_INSTANCE(NODE, ARGUMENT, CHILD, PARENT_STMT, PARENT_STMT_INDEX, NAME, FORMAT) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200458 assert_non_null(NODE); \
459 CHECK_STRING((NODE)->argument, ARGUMENT); \
460 CHECK_POINTER((NODE)->child, CHILD); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200461 /*assert_int_equal((NODE)->flags, LYS_INTERNAL);*/ \
Radek Krejciab430862021-03-02 20:13:40 +0100462 assert_int_equal((NODE)->parent_stmt, PARENT_STMT); \
463 assert_int_equal((NODE)->parent_stmt_index, PARENT_STMT_INDEX); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200464 assert_string_equal((NODE)->name, NAME); \
Michal Vaskofc2cd072021-02-24 13:17:17 +0100465 assert_int_equal((NODE)->format, FORMAT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200466
467/**
468 * @brief assert that lysp_stmt structure members are correct
469 * @param[in] NODE pointer to lysp_stmt variable
470 * @param[in] ARG expected statemet argumet
471 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
472 * @param[in] FLAGS expected statement flags, can be set to LYS_YIN_ATTR
473 * @param[in] KW expected numeric respresentation of the stmt value
474 * @param[in] NEXT 0 -> pointer is NULL, 1 -> pointer is not null
475 * @param[in] STMS expected identifier of the statement
476 */
477#define CHECK_LYSP_STMT(NODE, ARG, CHILD, FLAGS, KW, NEXT, STMT) \
478 assert_non_null(NODE); \
479 CHECK_STRING((NODE)->arg, ARG); \
480 CHECK_POINTER((NODE)->child, CHILD); \
481 assert_int_equal((NODE)->flags, FLAGS); \
482 assert_int_equal((NODE)->kw, KW); \
483 CHECK_POINTER((NODE)->next, NEXT); \
484 assert_string_equal((NODE)->stmt, STMT); \
485
486/**
487 * @brief assert that lysp_type_enum structure members are correct
488 * @param[in] NODE pointer to lysp_type_enum variable
489 * @param[in] DSC expected description
490 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
491 * @param[in] FLAGS only LYS_STATUS_ and LYS_SET_VALUE values are allowed
492 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
493 * @param[in] NAME expected name
494 * @param[in] REF expected reference statement
495 * @param[in] VALUE expected enum's value or bit's position
496 */
497#define CHECK_LYSP_TYPE_ENUM(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, REF, VALUE) \
498 assert_non_null(NODE); \
499 CHECK_STRING((NODE)->dsc, DSC); \
500 CHECK_ARRAY((NODE)->exts, EXTS); \
501 assert_int_equal((NODE)->flags, FLAGS); \
502 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
503 CHECK_STRING((NODE)->name, NAME); \
504 CHECK_STRING((NODE)->ref, REF); \
505 assert_int_equal(VALUE, (NODE)->value);
506
507/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100508 * @brief assert that lysp_type_enum structure members are correct
509 * @param[in] NODE pointer to lysp_type variable
510 * @param[in] BASES expected [sized array](@ref sizedarrays) size of list of indentifiers
511 * @param[in] BITS expected [sized array](@ref sizedarrays) size of list of bits
512 * @param[in] COMPILED 0 -> pointer to compiled type is null, 1 -> pointer to compilet type is valid
513 * @param[in] ENUMS expected [sized array](@ref sizedarrays) size of list of enums-stmts
514 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
515 * @param[in] FLAGS expected flags
516 * @param[in] FRACTION_DIGITS expected number of fraction digits decimal64
517 * @param[in] LENGTH expected 0 -> there isnt any restriction on length, 1 -> type is restricted on length (string, binary)
518 * @param[in] NAME expected name of type
519 * @param[in] PATH 0 -> no pointer to parsed path, 1 -> pointer to parsed path is valid
520 * @param[in] PATTERNS expected [sized array](@ref sizedarrays) size of list of patterns for string
521 * @param[in] PMOD expected submodule where type is defined 0 -> pointer is null, 1 -> pointer is not null
522 * @param[in] RANGE expected [sized array](@ref sizedarrays) size of list of range restriction
523 * @param[in] REQUIRE_INSTANCE expected require instance flag
524 * @param[in] TYPES expected [sized array](@ref sizedarrays) size of list of sub-types
525 */
526#define CHECK_LYSP_TYPE(NODE, BASES, BITS, COMPILED, ENUMS, EXTS, FLAGS, FRACTIONS_DIGITS, \
527 LENGTH, NAME, PATH, PATTERNS, PMOD, RANGE, REQUIRE_INSTANCE, TYPES) \
528 assert_non_null(NODE);\
529 CHECK_ARRAY((NODE)->bases, BASES); \
530 CHECK_ARRAY((NODE)->bits, BITS); \
531 CHECK_POINTER((NODE)->compiled, COMPILED); \
532 CHECK_ARRAY((NODE)->enums, ENUMS); \
533 CHECK_ARRAY((NODE)->exts, EXTS); \
534 assert_int_equal((NODE)->flags, FLAGS); \
535 assert_int_equal((NODE)->fraction_digits, FRACTIONS_DIGITS); \
536 CHECK_POINTER((NODE)->length, LENGTH); \
537 CHECK_STRING((NODE)->name, NAME); \
538 CHECK_POINTER((NODE)->path, PATH); \
539 CHECK_ARRAY((NODE)->patterns, PATTERNS); \
540 CHECK_POINTER((NODE)->pmod, PMOD); \
541 CHECK_POINTER((NODE)->range, RANGE); \
542 assert_int_equal((NODE)->require_instance, REQUIRE_INSTANCE); \
543 CHECK_ARRAY((NODE)->types , TYPES)
544
545/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200546 * @brief assert that lysp_node structure members are correct
547 * @param[in] NODE pointer to lysp_node variable
548 * @param[in] DSC expected description statement
549 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
550 * @param[in] FLAGS [schema node flags](@ref snodeflags)
551 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
552 * @param[in] NAME expected name
553 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
554 * @param[in] NODETYPE node type LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, LYS_LEAFLIST,
555 * LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, LYS_ACTION, LYS_NOTIF,
556 * LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, LYS_AUGMENT
557 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
558 * @param[in] REF expected reference statement
559 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
560 */
561#define CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, NODETYPE, PARENT, REF, WHEN) \
562 assert_non_null(NODE); \
563 CHECK_STRING((NODE)->dsc, DSC); \
564 CHECK_ARRAY((NODE)->exts, EXTS); \
565 assert_int_equal((NODE)->flags, FLAGS); \
566 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
567 CHECK_STRING((NODE)->name, NAME); \
568 CHECK_POINTER((NODE)->next, NEXT); \
569 assert_int_equal((NODE)->nodetype, NODETYPE); \
570 CHECK_POINTER((NODE)->parent, PARENT); \
571 CHECK_STRING((NODE)->ref, REF); \
Radek Krejci9a3823e2021-01-27 20:26:46 +0100572 CHECK_POINTER(lysp_node_when((struct lysp_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200573
574/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100575 * @brief assert that lysp_node structure members are correct
576 * @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] PARENT 0-> check if node is root, 1-> check if node is not root
584 * @param[in] REF expected reference statement
585 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
586 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
587 * @param[in] UNITS expected string reprezenting units
588 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
589 */
590#define CHECK_LYSP_NODE_LEAF(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, \
591 PARENT, REF, WHEN, MUSTS, UNITS, DFLT) \
592 CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, LYS_LEAF, PARENT, REF, WHEN); \
593 CHECK_ARRAY((NODE)->musts, MUSTS); \
594 CHECK_STRING((NODE)->units, UNITS); \
595 CHECK_STRING((NODE)->dflt.str, DFLT);
596
597/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200598 * @brief assert that lysc_notif structure members are correct
599 * @param[in] NODE pointer to lysp_notif variable
600 * @param[in] DATA 0 pointer is null, 1 pointer is not null
601 * @param[in] DSC expected description
602 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
603 * @param[in] FLAGS [schema node flags](@ref snodeflags)
604 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
605 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
606 * @param[in] NAME expected name
607 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
608 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
609 * @param[in] REF expected reference
610 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
611 */
612#define CHECK_LYSC_NOTIF(NODE, DATA, DSC, EXTS, FLAGS, MODULE, MUSTS, NAME, PARENT, PRIV, REF, WHEN) \
613 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100614 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200615 CHECK_STRING((NODE)->dsc, DSC); \
616 CHECK_ARRAY((NODE)->exts, EXTS); \
617 assert_int_equal((NODE)->flags, FLAGS); \
618 CHECK_POINTER((NODE)->module, MODULE); \
619 CHECK_ARRAY((NODE)->musts, MUSTS); \
620 assert_string_equal((NODE)->name, NAME); \
621 assert_int_equal((NODE)->nodetype, LYS_NOTIF); \
622 CHECK_POINTER((NODE)->parent, PARENT); \
623 CHECK_POINTER((NODE)->priv, PRIV); \
624 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100625 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200626
627/**
628 * @brief assert that lysc_action_inout structure members are correct
629 * @param[in] NODE pointer to lysp_notif variable
630 * @param[in] DATA 0 pointer is null, 1 pointer is not null
631 * @param[in] MUST expected [sized array](@ref sizedarrays) size of list of must restrictions
632 * @param[in] NODETYPE LYS_INPUT or LYS_OUTPUT
633 */
634#define CHECK_LYSC_ACTION_INOUT(NODE, DATA, MUST, NODETYPE) \
635 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100636 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200637 CHECK_ARRAY((NODE)->musts, MUST); \
638 assert_int_equal((NODE)->nodetype, NODETYPE);
639
640/**
641 * @brief assert that lysc_action structure members are correct
642 * @param[in] NODE pointer to lysp_action variable
643 * @param[in] DSC string description statement
644 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
645 * @param[in] FLAGS [schema node flags](@ref snodeflags)
646 * @param[in] INPUT_DATA 0 pointer is null, 1 pointer is not null
647 * @param[in] INPUT_MUST expected [sized array](@ref sizedarrays) size of input list of must restrictions
648 * @param[in] INPUT_EXTS expected [sized array](@ref sizedarrays) size of the input extension instances of input
649 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
650 * @param[in] NAME expected name
651 * @param[in] NODETYPE LYS_RPC, LYS_ACTION
652 * @param[in] OUTPUT_DATA 0 pointer is null, 1 pointer is not null
653 * @param[in] OUTPUT_MUST expected [sized array](@ref sizedarrays) size of output list of must restrictions
654 * @param[in] OUTPUT_EXTS expected [sized array](@ref sizedarrays) size of the output extension instances of input
655 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
656 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
657 * @param[in] REF expected reference
658 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
659 */
660#define CHECK_LYSC_ACTION(NODE, DSC, EXTS, FLAGS, INPUT_DATA, INPUT_MUST, INPUT_EXTS, MODULE, NAME, NODETYPE, \
661 OUTPUT_DATA, OUTPUT_MUST, OUTPUT_EXTS, PARENT, PRIV, REF, WHEN) \
662 assert_non_null(NODE); \
663 CHECK_STRING((NODE)->dsc, DSC); \
664 CHECK_ARRAY((NODE)->exts, EXTS); \
665 assert_int_equal((NODE)->flags, FLAGS); \
666 CHECK_LYSC_ACTION_INOUT(&(NODE)->input, INPUT_DATA, INPUT_MUST, LYS_INPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100667 CHECK_ARRAY((NODE)->input.exts, INPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200668 CHECK_POINTER((NODE)->module, MODULE); \
669 assert_string_equal((NODE)->name, NAME); \
670 assert_int_equal((NODE)->nodetype, NODETYPE); \
671 CHECK_LYSC_ACTION_INOUT(&(NODE)->output, OUTPUT_DATA, OUTPUT_MUST, LYS_OUTPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100672 CHECK_ARRAY((NODE)->output.exts, OUTPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200673 CHECK_POINTER((NODE)->parent, PARENT); \
674 CHECK_POINTER((NODE)->priv, PRIV); \
675 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100676 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200677
678/**
679 * @brief assert that lysc_node structure members are correct
680 * @param[in] NODE pointer to lysc_node variable
681 * @param[in] DSC expected description
682 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
683 * @param[in] FLAGS [schema node flags](@ref snodeflags)
684 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
685 * @param[in] NAME expected name
686 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
687 * @param[in] NODETYPE type of the node LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF,
688 * LYS_LEAFLIST, LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC,
689 * LYS_ACTION, LYS_NOTIF, LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING,
690 * LYS_AUGMENT
691 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
692 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
693 * @param[in] REF expected reference
694 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
695 */
696#define CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, NODETYPE, PARENT, PRIV, REF, WHEN) \
697 assert_non_null(NODE); \
698 CHECK_STRING((NODE)->dsc, DSC); \
699 CHECK_ARRAY((NODE)->exts, EXTS); \
700 assert_int_equal((NODE)->flags, FLAGS); \
701 CHECK_POINTER((NODE)->module, MODULE); \
702 assert_string_equal((NODE)->name, NAME); \
703 CHECK_POINTER((NODE)->next, NEXT); \
704 assert_int_equal((NODE)->nodetype, NODETYPE); \
705 CHECK_POINTER((NODE)->parent, PARENT); \
706 assert_non_null((NODE)->prev); \
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);
710
711/**
712 * @brief assert that lysc_node_leaf structure members are correct
713 * @param[in] NODE pointer to lysc_node variable
714 * @param[in] DSC expected description
715 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
716 * @param[in] FLAGS [schema node flags](@ref snodeflags)
717 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
718 * @param[in] NAME expected name
719 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
720 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
721 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
722 * @param[in] REF expected reference
Radek Išaa76902f2021-03-29 08:52:51 +0200723 * @param[in] ACTIONS 1 if is set pointer to structure lysc_node_action other 0
724 * @param[in] CHILD 1 if is set pointer to child other 0
725 * @param[in] MAX possible maximum elements in list
726 * @param[in] MIN possible minimum elements in list
727 * @param[in] MUSTS [sized array](@ref sizedarrays) number of must node elements in array
728 * @param[in] NOTIFS 1 if is set pointer to any notifs node
729 * @param[in] UNIQUES [sized array](@ref sizedarrays) number of unique nodes element in array
730 * @param[in] WHEN [sized array](@ref sizedarrays) size of when node array
731 */
732#define CHECK_LYSC_NODE_LIST(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \
733 PARENT, PRIV, REF, ACTIONS, CHILD, MAX, MIN, MUSTS, NOTIFS, UNIQUES, WHEN) \
734 CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LIST, PARENT, PRIV, REF, WHEN); \
735 CHECK_POINTER((NODE)->actions, ACTIONS); \
736 CHECK_POINTER((NODE)->child, CHILD); \
737 assert_int_equal((NODE)->max, MAX); \
738 assert_int_equal((NODE)->min, MIN); \
739 CHECK_ARRAY((NODE)->musts, MUSTS); \
740 CHECK_POINTER((NODE)->notifs, NOTIFS); \
741 CHECK_ARRAY((NODE)->uniques, UNIQUES); \
742 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN)
743
744/**
745 * @brief assert that lysc_node_leaf structure members are correct
746 * @param[in] NODE pointer to lysc_node variable
747 * @param[in] DSC expected description
748 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
749 * @param[in] FLAGS [schema node flags](@ref snodeflags)
750 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
751 * @param[in] NAME expected name
752 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
753 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
754 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
755 * @param[in] REF expected reference
756 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
Radek Išaa9ff2b82021-01-13 21:44:13 +0100757 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
758 * @param[in] UNITS expected string reprezenting units
759 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
760 */
761#define CHECK_LYSC_NODE_LEAF(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \
762 PARENT, PRIV, REF, WHEN, MUSTS, UNITS, DFLT) \
763 CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LEAF, PARENT, PRIV, REF, WHEN); \
764 CHECK_ARRAY((NODE)->musts, MUSTS); \
765 assert_non_null((NODE)->type); \
766 CHECK_STRING((NODE)->units, UNITS); \
767 CHECK_POINTER((NODE)->dflt, DFLT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200768
769/**
770 * @brief assert that lyd_meta structure members are correct
771 * @param[in] NODE pointer to lyd_meta variable
772 * @param[in] ANNOTATION 0 pointer is null, 1 pointer is not null
773 * @param[in] NAME expected name
774 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
775 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
776 * part of text reprezenting LY_DATA_TYPE.
777 * @param[in] ... ::CHECK_LYD_VALUE
778 */
779#define CHECK_LYD_META(NODE, ANNOTATION, NAME, NEXT, PARENT, TYPE_VAL, ...) \
780 assert_non_null(NODE); \
781 CHECK_POINTER((NODE)->annotation, ANNOTATION); \
782 assert_string_equal((NODE)->name, NAME); \
783 CHECK_POINTER((NODE)->next, NEXT); \
784 CHECK_POINTER((NODE)->parent, PARENT); \
785 CHECK_LYD_VALUE((NODE)->value, TYPE_VAL, ##__VA_ARGS__);
786
787/**
788 * @brief assert that lyd_node_term structure members are correct
789 * @param[in] NODE pointer to lyd_node_term variable
790 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
791 * @param[in] META 0 -> meta is not prezent, 1 -> meta is prezent
792 * @param[in] NEXT 0 -> next node is not prezent, 1 -> next node is prezent
793 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
794 * part of text reprezenting LY_DATA_TYPE.
795 * @param[in] ... ::CHECK_LYD_VALUE
796 */
797#define CHECK_LYD_NODE_TERM(NODE, FLAGS, META, NEXT, PARENT, SCHEMA, VALUE_TYPE, ...) \
798 assert_non_null(NODE); \
799 assert_int_equal((NODE)->flags, FLAGS); \
800 CHECK_POINTER((NODE)->meta, META); \
801 CHECK_POINTER((NODE)->next, NEXT); \
802 CHECK_POINTER((NODE)->parent, PARENT); \
803 assert_non_null((NODE)->prev); \
804 CHECK_POINTER((NODE)->schema, SCHEMA); \
805 CHECK_LYD_VALUE((NODE)->value, VALUE_TYPE, ##__VA_ARGS__);
806
807/**
808 * @brief assert that lyd_node_any structure members are correct
809 * @param[in] NODE pointer to lyd_node_term variable
810 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
811 * @param[in] META 0 meta isnt present , 1 meta is present
812 * @param[in] PARENT 0 it is root node , 1 node have parent
813 * @param[in] VALUE_TYPE value type ::lyd_node_any.value
814 */
815#define CHECK_LYD_NODE_ANY(NODE, FLAGS, META, PARENT, VALUE_TYPE) \
816 assert_non_null(NODE); \
817 assert_int_equal((NODE)->flags, FLAGS); \
818 CHECK_POINTER((NODE)->meta, META); \
819 CHECK_POINTER((NODE)->meta, PARENT); \
820 assert_non_null((NODE)->prev); \
821 assert_non_null((NODE)->schema); \
822 assert_int_equal((NODE)->value_type, VALUE_TYPE);
823
824/**
825 * @brief assert that lyd_node_opaq structure members are correct
826 * @param[in] NODE pointer to lyd_node_opaq variable
827 * @param[in] ATTR 0 if pointer is null ,1 if pointer is not null
828 * @param[in] CHILD 0 if pointer is null ,1 if pointer is not null
829 * @param[in] FORMAT LY_PREF_XML or LY_PREF_JSON
830 * @param[in] VAL_PREFS 0 if pointer is null ,1 if pointer is not null
831 * @param[in] NAME expected name
832 * @param[in] value expected orignal value
833 */
834#define CHECK_LYD_NODE_OPAQ(NODE, ATTR, CHILD, FORMAT, NAME, NEXT, PARENT, PREFIX, VAL_PREFS, VALUE) \
835 assert_non_null(NODE); \
836 CHECK_POINTER((NODE)->attr, ATTR); \
837 CHECK_POINTER((NODE)->child, CHILD); \
838 assert_ptr_equal((NODE)->ctx, UTEST_LYCTX); \
839 assert_int_equal((NODE)->flags, 0); \
840 assert_true((NODE)->format == FORMAT); \
841 assert_int_equal((NODE)->hash, 0); \
842 assert_string_equal((NODE)->name.name, NAME); \
843 assert_non_null((NODE)->prev); \
844 assert_null((NODE)->schema); \
845 CHECK_POINTER((NODE)->val_prefix_data, VAL_PREFS); \
846 assert_string_equal((NODE)->value, VALUE);
847
848/**
Radek Išaa76902f2021-03-29 08:52:51 +0200849 * @brief assert that lyd_node_opaq structure members are correct
850 * @param[in] NODE pointer to lyd_node_opaq variable
851 * @param[in] CHILD 1 if node has children other 0
852 * @param[in] HILD_HT 1 if node has children hash table other 0
853 * @param[in] META 1 if node has metadata other 0
854 * @param[in] FLAGS expected flag
855 * @param[in] NEXT 1 if next node is present other 0
856 * @param[in] PARENT 1 if node has parent other 0
857 * @param[in] PRIV 1 if node has private data other 0
858 * @param[in] SCHEMA 1 if node has schema other 0
859*/
860#define CHECK_LYD_NODE_INNER(NODE, CHILD, CHILD_HT, META, FLAGS, NEXT, PARENT, PRIV, SCHEMA) \
861 assert_non_null(NODE); \
862 CHECK_POINTER((NODE)->child, CHILD); \
863 CHECK_POINTER((NODE)->children_ht, CHILD_HT); \
864 CHECK_POINTER((NODE)->meta, META); \
865 assert_int_equal((NODE)->flags, FLAGS); \
866 CHECK_POINTER((NODE)->parent, PARENT); \
867 assert_non_null((NODE)->prev); \
868 CHECK_POINTER((NODE)->next, NEXT); \
869 CHECK_POINTER((NODE)->priv, PRIV); \
870 CHECK_POINTER((NODE)->schema, SCHEMA)
871
872/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200873 * @brief assert that lyd_value structure members are correct
874 * @param[in] NODE lyd_value
875 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
876 * part of text reprezenting LY_DATA_TYPE.
877 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
878 * by type of value. These parameters are passed to macro
879 * CHECK_LYD_VALUE_ ## TYPE_VAL.
880 */
881#define CHECK_LYD_VALUE(NODE, TYPE_VAL, ...) \
882 CHECK_LYD_VALUE_ ## TYPE_VAL (NODE, ##__VA_ARGS__);
883
884/*
885 * LYD VALUES CHECKING SPECIALIZATION
886 */
887
888/**
889 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type EMPTY
890 * Example CHECK_LYD_VALUE(node->value, EMPTY, "");
891 * @param[in] NODE lyd_value variable
892 * @param[in] CANNONICAL_VAL expected cannonical value
893 */
894#define CHECK_LYD_VALUE_EMPTY(NODE, CANNONICAL_VAL) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200895 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200896 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200897 assert_non_null((NODE).realtype); \
898 assert_int_equal((NODE).realtype->basetype, LY_TYPE_EMPTY);
899
900/**
901 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UNION
902 * Example CHECK_LYD_VALUE(node->value, UNION, "12", INT8, "12", 12);
903 * @warning type of subvalue cannot be UNION. Example of calling
904 * @param[in] NODE lyd_value variable
905 * @param[in] CANNONICAL_VAL expected cannonical value
906 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
907 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
908 * by type of value. These parameters are passed to macro
909 * CHECK_LYD_VALUE_ ## TYPE_VAL.
910 */
911#define CHECK_LYD_VALUE_UNION(NODE, CANNONICAL_VAL, TYPE_VAL, ...) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200912 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200913 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200914 assert_non_null((NODE).realtype); \
915 assert_int_equal(LY_TYPE_UNION, (NODE).realtype->basetype); \
916 assert_non_null((NODE).subvalue); \
917 assert_non_null((NODE).subvalue->prefix_data); \
918 CHECK_LYD_VALUE_ ## TYPE_VAL ((NODE).subvalue->value, ## __VA_ARGS__)
919
920/**
921 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BITS
922 * Example arr[] = {"a", "b"}; CHECK_LYD_VALUE(node->value, BITS, "a b", arr);
923 * @param[in] NODE lyd_value variable
924 * @param[in] CANNONICAL_VAL expected cannonical value
925 * @param[in] VALUE expected array of bits names
926 */
Radek Išaded31052021-03-10 13:22:53 +0100927#define CHECK_LYD_VALUE_BITS(NODE, CANNONICAL_VAL, ...) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200928 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200929 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200930 assert_non_null((NODE).realtype); \
931 assert_int_equal(LY_TYPE_BITS, (NODE).realtype->basetype); \
932 { \
Radek Išaded31052021-03-10 13:22:53 +0100933 const char *arr[] = { __VA_ARGS__ }; \
934 LY_ARRAY_COUNT_TYPE arr_size = sizeof(arr) / sizeof(arr[0]); \
Michal Vaskoaa0ee622021-05-11 09:29:25 +0200935 struct lyd_value_bits *_val; \
936 LYD_VALUE_GET(&(NODE), _val); \
937 assert_int_equal(arr_size, LY_ARRAY_COUNT(_val->items)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200938 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
Michal Vaskoaa0ee622021-05-11 09:29:25 +0200939 assert_string_equal(arr[it], _val->items[it]->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200940 } \
941 }
942
943/**
944 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INST
945 * @param[in] NODE lyd_value variable
946 * @param[in] CANNONICAL_VAL expected cannonical value
947 * @param[in] VALUE expected array of enum ly_path_pred_type
948 * @brief Example enum arr[] = {0x0, 0x1}; CHECK_LYD_VALUE(node->value, INST, "test/d", arr);
949 */
950#define CHECK_LYD_VALUE_INST(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200951 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200952 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200953 assert_non_null((NODE).realtype); \
954 assert_int_equal(LY_TYPE_INST, (NODE).realtype->basetype); \
955 { \
Michal Vasko79135ae2020-12-16 10:08:35 +0100956 LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200957 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).target)); \
958 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
959 assert_int_equal(VALUE[it], (NODE).target[it].pred_type); \
960 } \
961 }
962
963/**
964 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type ENUM.
965 * Example CHECK_LYD_VALUE(node->value, ENUM, "item_name", "item_name");
966 * @param[in] NODE lyd_value variable
967 * @param[in] CANNONICAL_VAL expected cannonical value
968 * @param[in] VALUE expected enum item name
969 */
970#define CHECK_LYD_VALUE_ENUM(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200971 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200972 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200973 assert_non_null((NODE).realtype); \
974 assert_int_equal(LY_TYPE_ENUM, (NODE).realtype->basetype); \
975 assert_string_equal(VALUE, (NODE).enum_item->name);
976
977/**
978 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT8
979 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
980 * @param[in] NODE lyd_value variable
981 * @param[in] CANNONICAL_VAL expected cannonical value
982 * @param[in] VALUE expected inteager value (-128 to 127).
983 */
984#define CHECK_LYD_VALUE_INT8(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200985 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +0200986 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200987 assert_non_null((NODE).realtype); \
988 assert_int_equal(LY_TYPE_INT8, (NODE).realtype->basetype); \
989 assert_int_equal(VALUE, (NODE).int8);
990
991/**
992 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT16
993 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
994 * @param[in] NODE lyd_value variable
995 * @param[in] CANNONICAL_VAL expected cannonical value
996 * @param[in] VALUE expected inteager value.
997 */
998#define CHECK_LYD_VALUE_INT16(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200999 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001000 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001001 assert_non_null((NODE).realtype); \
1002 assert_int_equal(LY_TYPE_INT16, (NODE).realtype->basetype); \
1003 assert_int_equal(VALUE, (NODE).int16);
1004
1005/**
1006 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT8.
1007 * Example CHECK_LYD_VALUE(node->value, UINT8, "12", 12);
1008 * @param[in] NODE lyd_value variable
1009 * @param[in] CANNONICAL_VAL expected cannonical value
1010 * @param[in] VALUE expected inteager (0 to 255).
1011 */
1012#define CHECK_LYD_VALUE_UINT8(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001013 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001014 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001015 assert_non_null((NODE).realtype); \
1016 assert_int_equal(LY_TYPE_UINT8, (NODE).realtype->basetype); \
1017 assert_int_equal(VALUE, (NODE).uint8);
1018
1019/**
Radek Išaa76902f2021-03-29 08:52:51 +02001020 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT32.
1021 * Example CHECK_LYD_VALUE(node->value, UINT32, "12", 12);
1022 * @param[in] NODE lyd_value variable
1023 * @param[in] CANNONICAL_VAL expected cannonical value
1024 * @param[in] VALUE expected inteager (0 to MAX_UINT32).
1025 */
1026#define CHECK_LYD_VALUE_UINT32(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001027 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001028 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Išaa76902f2021-03-29 08:52:51 +02001029 assert_non_null((NODE).realtype); \
1030 assert_int_equal(LY_TYPE_UINT32, (NODE).realtype->basetype); \
1031 assert_int_equal(VALUE, (NODE).uint32);
1032
1033/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001034 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type STRING.
1035 * Example CHECK_LYD_VALUE(node->value, STRING, "text");
1036 * @param[in] NODE lyd_value variable
1037 * @param[in] CANNONICAL_VAL expected cannonical value
1038 */
1039#define CHECK_LYD_VALUE_STRING(NODE, CANNONICAL_VAL) \
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_STRING, (NODE).realtype->basetype);
1044
1045/**
1046 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type LEAFREF
1047 * @brief Example CHECK_LYD_VALUE(node->value, LEAFREF, "");
1048 * @param[in] NODE lyd_value variable
1049 * @param[in] CANNONICAL_VAL expected cannonical value
1050 */
1051#define CHECK_LYD_VALUE_LEAFREF(NODE, CANNONICAL_VAL) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001052 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001053 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001054 assert_non_null((NODE).realtype); \
1055 assert_int_equal(LY_TYPE_LEAFREF, (NODE).realtype->basetype); \
1056 assert_non_null((NODE).ptr)
1057
1058/**
1059 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type DEC64
1060 * Example CHECK_LYD_VALUE(node->value, DEC64, "125", 125);
1061 * @param[in] NODE lyd_value variable
1062 * @param[in] CANNONICAL_VAL expected cannonical value
1063 * @param[in] VALUE expected value 64bit inteager
1064*/
1065#define CHECK_LYD_VALUE_DEC64(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001066 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001067 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001068 assert_non_null((NODE).realtype); \
1069 assert_int_equal(LY_TYPE_DEC64, (NODE).realtype->basetype); \
1070 assert_int_equal(VALUE, (NODE).dec64);
1071
1072/**
1073 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BINARY.
1074 * Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8=");
1075 * @param[in] NODE lyd_value variable
1076 * @param[in] CANNONICAL_VAL expected cannonical value
Michal Vasko495f4502021-04-27 14:48:05 +02001077 * @param[in] VALUE expected value data
1078 * @param[in] SIZE expected value data size
Radek Iša56ca9e42020-09-08 18:42:00 +02001079*/
Michal Vasko495f4502021-04-27 14:48:05 +02001080#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL, VALUE, SIZE) \
Michal Vaskoaa0ee622021-05-11 09:29:25 +02001081 { \
1082 struct lyd_value_binary *_val; \
1083 LYD_VALUE_GET(&(NODE), _val); \
1084 assert_int_equal(_val->size, SIZE); \
1085 assert_int_equal(0, memcmp(_val->data, VALUE, SIZE)); \
1086 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
1087 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
1088 assert_non_null((NODE).realtype); \
1089 assert_int_equal(LY_TYPE_BINARY, (NODE).realtype->basetype); \
1090 }
Radek Iša56ca9e42020-09-08 18:42:00 +02001091
1092/**
1093 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BOOL.
1094 * Example CHECK_LYD_VALUE(node->value, BOOL, "true", 1);
1095 * @param[in] NODE lyd_value variable
1096 * @param[in] CANNONICAL_VAL expected cannonical value
1097 * @param[in] VALUE expected boolean value 0,1
1098*/
1099#define CHECK_LYD_VALUE_BOOL(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001100 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001101 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001102 assert_non_null((NODE).realtype); \
1103 assert_int_equal(LY_TYPE_BOOL, (NODE).realtype->basetype); \
1104 assert_int_equal(VALUE, (NODE).boolean);
1105
1106/**
1107 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type IDENT.
1108 * Example CHECK_LYD_VALUE(node->value, IDENT, "types:gigabit-ethernet", "gigabit-ethernet");
1109 * @param[in] NODE lyd_value variable
1110 * @param[in] CANNONICAL_VAL expected cannonical value
1111 * @param[in] VALUE expected ident name
1112*/
1113#define CHECK_LYD_VALUE_IDENT(NODE, CANNONICAL_VAL, VALUE) \
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001114 assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
Radek Krejci995784f2021-04-26 08:02:13 +02001115 assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001116 assert_non_null((NODE).realtype); \
1117 assert_int_equal(LY_TYPE_IDENT, (NODE).realtype->basetype); \
1118 assert_string_equal(VALUE, (NODE).ident->name);
1119
1120/**
Radek Išaa9ff2b82021-01-13 21:44:13 +01001121 * @brief Macro testing parser when parsing incorrect module;
1122 * @param[in] DATA String storing the schema module representation.
1123 * @param[in] FORMAT Schema format of the @p DATA
1124 * @param[in] FEATURES Array of module's features to enable
1125 * @param[in] RET_VAL ly_in_new_memory return error value
1126 */
1127#define UTEST_INVALID_MODULE(DATA, FORMAT, FEATURES, RET_VAL) \
1128 { \
Michal Vasko4de7d072021-07-09 09:13:18 +02001129 struct lys_module *mod; \
Radek Išaa9ff2b82021-01-13 21:44:13 +01001130 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
1131 assert_int_equal(RET_VAL, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, &mod)); \
1132 assert_null(mod); \
1133 } \
1134 ly_in_free(_UC->in, 0); \
1135 _UC->in = NULL; \
1136
1137/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001138 * @brief Add module (from a string) into the used libyang context.
1139 * @param[in] DATA String storing the schema module representation.
1140 * @param[in] FORMAT Schema format of the @p DATA
1141 * @param[in] FEATURES Array of module's features to enable
1142 * @param[out] MOD Optional parameter as a pointer to variable to store the resulting module.
1143 */
1144#define UTEST_ADD_MODULE(DATA, FORMAT, FEATURES, MOD) \
1145 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
1146 assert_int_equal(LY_SUCCESS, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, MOD)); \
1147 ly_in_free(_UC->in, 0); \
1148 _UC->in = NULL
1149
1150/**
1151 * @brief Internal macro to compare error info record with the expected error message and path.
1152 * If NULL is provided as MSG, no error info record (NULL) is expected.
1153 * @param[in] ERR Error information record from libyang context.
1154 * @param[in] MSG Expected error message.
1155 * @param[in] PATH Expected error path.
1156 *
1157 */
1158#define _CHECK_LOG_CTX(ERR, MSG, PATH) \
1159 if (!MSG) { \
1160 assert_null(ERR); \
1161 } else { \
1162 assert_non_null(ERR); \
1163 CHECK_STRING((ERR)->msg, MSG); \
1164 CHECK_STRING((ERR)->path, PATH); \
1165 }
1166
1167/**`
1168 * @brief Internal macro to check the last libyang's context error.
1169 */
1170#define _CHECK_LOG_CTX1(MSG, PATH) \
1171 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG, PATH)
1172
1173/**
1174 * @brief Internal macro to check the last two libyang's context error.
1175 */
1176#define _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2) \
1177 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG1, PATH1); \
1178 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev, MSG2, PATH2)
1179
1180/**
1181 * @brief Internal macro to check the last three libyang's context error.
1182 */
1183#define _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3) \
1184 _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2); \
1185 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev, MSG3, PATH3)
1186
1187/**
Radek Krejcife6ec262021-01-20 10:28:28 +01001188 * @brief Internal macro to check the last three libyang's context error.
1189 */
1190#define _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4) \
1191 _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3); \
1192 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev, MSG4, PATH4)
1193
1194/**
1195 * @brief Internal macro to check the last three libyang's context error.
1196 */
1197#define _CHECK_LOG_CTX5(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4, MSG5, PATH5) \
1198 _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4); \
1199 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev->prev, MSG5, PATH5)
1200
1201/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001202 * @brief Internal helper macro to select _CHECK_LOG_CTX* macro according to the provided parameters.
1203 */
Radek Krejcife6ec262021-01-20 10:28:28 +01001204#define _GET_CHECK_LOG_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
Radek Iša56ca9e42020-09-08 18:42:00 +02001205
1206/**
1207 * @brief Check expected error(s) in libyang context.
1208 *
1209 * Macro has variadic parameters expected to be provided in pairs of error message and error path starting
1210 * from the latest error. Current limit is checking at most 3 last errors. After checking, macro cleans up
1211 * all the errors from the libyang context.
1212 *
1213 * @param[in] MSG Expected error message.
1214 * @param[in] PATH Expected error path.
1215 */
1216#define CHECK_LOG_CTX(...) \
Radek Krejcife6ec262021-01-20 10:28:28 +01001217 _GET_CHECK_LOG_MACRO(__VA_ARGS__, _CHECK_LOG_CTX5, _INVAL, _CHECK_LOG_CTX4, _INVAL, \
1218 _CHECK_LOG_CTX3, _INVAL, _CHECK_LOG_CTX2, _INVAL, _CHECK_LOG_CTX1)(__VA_ARGS__); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001219 ly_err_clean(_UC->ctx, NULL)
1220
1221/**
1222 * @brief Clean up the logging callback's storage.
1223 */
1224#define UTEST_LOG_CLEAN \
1225 free(_UC->err_msg); \
1226 free(_UC->err_path); \
1227 _UC->err_msg = NULL; \
1228 _UC->err_path = NULL;
1229
1230/**
1231 * @brief Check expected error directly logged via logging callback.
1232 * Useful mainly for messages logged by functions without access to libyang context.
1233 * @param[in] MSG Expected error message.
1234 * @param[in] PATH Expected error path.
1235 */
1236#define CHECK_LOG(MSG, PATH) \
1237 CHECK_STRING(_UC->err_msg, MSG); \
1238 CHECK_STRING(_UC->err_path, PATH); \
1239 UTEST_LOG_CLEAN
1240
1241#ifdef _UTEST_MAIN_
1242/*
1243 * Functions inlined into each C source file including this header with _UTEST_MAIN_ defined
1244 */
1245
1246/**
1247 * @brief Global variable holding the tests context to simplify access to it.
1248 */
1249struct utest_context *current_utest_context;
1250
1251/* set to 0 to printing error messages to stderr instead of checking them in code */
1252#define ENABLE_LOGGER_CHECKING 1
1253
1254/**
1255 * @brief Logging callback for libyang.
1256 */
1257static void
1258_utest_logger(LY_LOG_LEVEL level, const char *msg, const char *path)
1259{
1260 (void) level; /* unused */
1261
1262 if (ENABLE_LOGGER_CHECKING == 0) {
Radek Išaa9ff2b82021-01-13 21:44:13 +01001263 printf("\tERROR:\n\t\tMESSAGE: %s\n\t\tPATH: %s\n\t\tLEVEL: %i\n", msg, path, level);
Radek Iša56ca9e42020-09-08 18:42:00 +02001264 } else {
1265 free(current_utest_context->err_msg);
1266 current_utest_context->err_msg = msg ? strdup(msg) : NULL;
1267 free(current_utest_context->err_path);
1268 current_utest_context->err_path = path ? strdup(path) : NULL;
1269 }
1270}
1271
1272/**
1273 * @brief Generic utest's setup
1274 */
1275static int
1276utest_setup(void **state)
1277{
Michal Vasko86421632021-05-04 13:11:25 +02001278 char *cur_tz;
1279
Radek Iša56ca9e42020-09-08 18:42:00 +02001280 /* setup the logger */
1281 ly_set_log_clb(_utest_logger, 1);
1282 ly_log_options(LY_LOLOG | LY_LOSTORE);
1283
1284 current_utest_context = calloc(1, sizeof *current_utest_context);
1285 assert_non_null(current_utest_context);
1286 *state = current_utest_context;
1287
1288 /* libyang context */
1289 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &current_utest_context->ctx));
1290
Radek Krejci968d7552021-03-26 20:33:51 +01001291 /* clean all errors from the setup - usually warnings regarding the plugins directories */
1292 UTEST_LOG_CLEAN;
1293
Michal Vasko86421632021-05-04 13:11:25 +02001294 /* backup timezone, if any */
1295 cur_tz = getenv("TZ");
1296 if (cur_tz) {
1297 current_utest_context->orig_tz = strdup(cur_tz);
1298 }
1299
1300 /* set CET */
1301 setenv("TZ", "CET+02:00", 1);
1302
Radek Iša56ca9e42020-09-08 18:42:00 +02001303 return 0;
1304}
1305
1306/**
1307 * @brief macro to include generic utest's setup into the test-specific setup.
1308 *
1309 * Place at the beginning of the test-specific setup
1310 */
1311#define UTEST_SETUP \
1312 assert_int_equal(0, utest_setup(state))
1313
1314/**
1315 * @brief Generic utest's teardown
1316 */
1317static int
1318utest_teardown(void **state)
1319{
1320 *state = NULL;
1321
1322 /* libyang context */
Radek Krejci90ed21e2021-04-12 14:47:46 +02001323 ly_ctx_destroy(current_utest_context->ctx);
Radek Iša56ca9e42020-09-08 18:42:00 +02001324
Michal Vasko86421632021-05-04 13:11:25 +02001325 if (current_utest_context->orig_tz) {
1326 /* restore TZ */
1327 setenv("TZ", current_utest_context->orig_tz, 1);
1328 }
1329
Radek Iša56ca9e42020-09-08 18:42:00 +02001330 /* utest context */
1331 ly_in_free(current_utest_context->in, 0);
1332 free(current_utest_context->err_msg);
1333 free(current_utest_context->err_path);
Michal Vasko86421632021-05-04 13:11:25 +02001334 free(current_utest_context->orig_tz);
Radek Iša56ca9e42020-09-08 18:42:00 +02001335 free(current_utest_context);
1336 current_utest_context = NULL;
1337
1338 return 0;
1339}
1340
1341/**
1342 * @brief macro to include generic utest's teardown into the test-specific teardown.
1343 *
1344 * Place at the end of the test-specific teardown
1345 */
1346#define UTEST_TEARDOWN \
1347 assert_int_equal(0, utest_teardown(state))
1348
1349/**
1350 * @brief Internal macro for utest setup with test-specific setup and teardown
1351 */
1352#define _UTEST_SETUP_TEARDOWN(FUNC, SETUP, TEARDOWN) \
1353 cmocka_unit_test_setup_teardown(FUNC, SETUP, TEARDOWN)
1354
1355/**
1356 * @brief Internal macro for utest setup with test-specific setup and generic teardown
1357 */
1358#define _UTEST_SETUP(FUNC, SETUP) \
1359 cmocka_unit_test_setup_teardown(FUNC, SETUP, utest_teardown)
1360
1361/**
1362 * @brief Internal macro for utest setup with generic setup and teardown
1363 */
1364#define _UTEST(FUNC) \
1365 cmocka_unit_test_setup_teardown(FUNC, utest_setup, utest_teardown)
1366
1367/**
1368 * @brief Internal helper macro to select _UTEST* macro according to the provided parameters.
1369 */
1370#define _GET_UTEST_MACRO(_1, _2, _3, NAME, ...) NAME
1371
1372/**
1373 * @brief Macro to specify test function using utest environment. Macro has variadic parameters
1374 * to provide test-specific setup/teardown functions:
1375 *
1376 * UTEST(test_func) - only implicit setup and teardown functions are used
1377 * UTEST(test_func, setup) - implicit teardown but own setup
1378 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
1379 */
1380#define UTEST(...) \
1381 _GET_UTEST_MACRO(__VA_ARGS__, _UTEST_SETUP_TEARDOWN, _UTEST_SETUP, _UTEST)(__VA_ARGS__)
1382
1383#else /* _UTEST_MAIN_ */
1384
1385extern struct utest_context *current_utest_context;
1386
1387#endif /* _UTEST_MAIN_ */
1388
1389#endif /* _UTESTS_H_ */