blob: 852ce08a7900e33a800f2aec6dee1b67f86c5f14 [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>
24
25#include <cmocka.h>
Radek Iša56ca9e42020-09-08 18:42:00 +020026
27#include <string.h>
28
29#include "libyang.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010030#include "plugins_types.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020031#include "tests/config.h"
Radek Išaa9ff2b82021-01-13 21:44:13 +010032#include "tree_schema_internal.h"
Radek Iša56ca9e42020-09-08 18:42:00 +020033
34/**
35 * TESTS OVERVIEW
36 *
37 * To include utest's environment, just include "utests.h" in the test's source
38 * code. In case it is the main source code for a cmocka test group (there is a
39 * main() function), define _UTEST_MAIN_ before including this header.
40 *
41 * TESTS VARIABLES
42 *
43 * Checking macros use internal storage to store various variables necessary
44 * during the checking. It is possible to access these variables using the
45 * following macros:
46 *
47 * UTEST_LYCTX - libyang context
48 * UTEST_IN - input handler
49 * UTEST_OUT - output handler
50 *
51 * All these variables are cleaned with test's teardown.
52 *
53 * TESTS SETUP
54 *
55 * CMocka's CMUnitTest list definition macros (cmoka_unit_test*()) are replaced
56 * by UTEST macro with possibility to specify own setup and teardown functions:
57 *
58 * UTEST(test_func) - only implicit setup and teardown functions are used
59 * UTEST(test_func, setup) - implicit teardown but own setup
60 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
61 *
62 * Note that the tests environment always provide (and need) internal setup and
63 * teardown functions. In case the test-specific setup or teardown are used, they
64 * are supposed to include UTEST_SETUP at the setup beginning and UTEST_TEARDOWN
65 * at the teardown end.
66 *
67 * Libyang context is part of the prepared environment. To add a schema into the
68 * context (despite it is in the test-specific setup or in test function itself),
69 * use UTEST_ADD_MODULE macro.
70 *
71 * LOGGING
72 *
73 * There are 2 macros to check content of the log from the previously called
74 * libyang function. CHECK_LOG macro test only the last error message and path
75 * stored directly via logging callback. CHECK_LOG_CTX gets error message and
76 * path from the libyang context (in case the function does not store the error
77 * information into the libyang context, the message cannot be checked this way).
78 * libyang is set to store multiple error information, so multiple couples of
79 * error message and path can be provided to be checked (the first couple
80 * corresponds to the latest error). The macro also cleanups the errors list, so
81 * it is fine to check that there is no error after succeeding successful
82 * function call.
83 */
84
85/**
86 * @brief Test's context to provide common storage for various variables.
87 */
88struct utest_context {
89 struct ly_ctx *ctx; /**< libyang context */
90
91 char *err_msg; /**< Directly logged error message */
92 char *err_path; /**< Directly logged error path */
93
94 struct ly_in *in; /**< Input handler */
95 struct ly_out *out; /**< Outpu handler */
96};
97
98/**
99 * @brief Shortcut to access utest_context.
100 */
101#define _UC ((struct utest_context *)*state)
102
103/**
104 * @brief libyang context provider.
105 */
106#define UTEST_LYCTX (_UC->ctx)
107
108/**
109 * @brief Context's input handler provider
110 */
111#define UTEST_IN (_UC->in)
112
113/**
114 * @brief Context's input handler provider
115 */
116#define UTEST_OUT (_UC->out)
117
118/**
119 * @brief Parse (and validate) data from the input handler as a YANG data tree.
120 *
121 * @param[in] INPUT The input data in the specified @p format to parse (and validate)
122 * @param[in] INPUT_FORMAT Format of the input data to be parsed. Can be 0 to try to detect format from the input handler.
123 * @param[in] PARSE_OPTIONS Options for parser, see @ref dataparseroptions.
124 * @param[in] VALIDATE_OPTIONS Options for the validation phase, see @ref datavalidationoptions.
125 * @param[in] OUT_STATUS expected return status
126 * @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.
127 * The returned data are expected to be freed using LYD_TREE_DESTROY().
128 */
129#define CHECK_PARSE_LYD_PARAM(INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, OUT_STATUS, OUT_NODE) \
130 assert_int_equal(OUT_STATUS, lyd_parse_data_mem(_UC->ctx, INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, &OUT_NODE)); \
131 if (OUT_STATUS == LY_SUCCESS) { \
132 assert_non_null(OUT_NODE); \
133 } else { \
134 assert_null(OUT_NODE); \
135 }
136
137/**
138 * @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.
139 * @param[in] NODE pointer to lyd_node
140 * @param[in] TEXT expected output string in json or xml format.
141 * @param[in] FORMAT format of input text. LYD_JSON, LYD_XML
142 * @param[in] PARAM options [Data printer flags](@ref dataprinterflags).
143 */
144#define CHECK_LYD_STRING_PARAM(NODE, TEXT, FORMAT, PARAM) \
145 { \
146 char *test; \
147 lyd_print_mem(&test, NODE, FORMAT, PARAM); \
148 assert_string_equal(test, TEXT); \
149 free(test); \
150 }
151
152/**
153 * @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;
154 * @param[in] NODE_1 pointer to lyd_node
155 * @param[in] NODE_2 pointer to lyd_node
156 */
157#define CHECK_LYD(NODE_1, NODE_2) \
158 { \
159 char *test_1; \
160 char *test_2; \
161 lyd_print_mem(&test_1, NODE_1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
162 lyd_print_mem(&test_2, NODE_2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
163 assert_string_equal(test_1, test_2); \
164 free(test_1); \
165 free(test_2); \
166 }
167
168/*
169 * SUPPORT MACROS
170 */
171
172/**
173 * @brief Internal macro witch assert that two given string are equal or are both null.
174 *
175 * @param[in] STRING string to check
176 * @param[in] TEXT string to compare
177 */
178#define CHECK_STRING(STRING, TEXT)\
179 if (TEXT == NULL) { \
180 assert_null(STRING); \
181 } else { \
182 assert_non_null(STRING); \
183 assert_string_equal(STRING, TEXT); \
184 }
185
186/**
187 * @brief Internal macro witch assert that pointer is null when flag is 0.
188 *
189 * @param[in] POINTER pointer to check
190 * @param[in] FLAG 0 -> pointer is NULL, 1 -> pointer is not null
191 */
192#define CHECK_POINTER(POINTER, FLAG) \
193 assert_true(FLAG == 0 ? POINTER == NULL : POINTER != NULL)
194
195/**
196 * @brief Internal macro check size of [sized array](@ref sizedarrays)'s
197 *
198 * @param[in] ARRAY pointer to [sized array](@ref sizedarrays)
199 * @param[in] SIZE expected [sized array](@ref sizedarrays) size of array
200 */
201#define CHECK_ARRAY(ARRAY, SIZE) \
202 assert_true((SIZE == 0) ? \
203 (ARRAY == NULL) : \
204 (ARRAY != NULL && SIZE == LY_ARRAY_COUNT(ARRAY)));
205
206/*
207 * LIBYANG NODE CHECKING
208 */
209
210/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100211 * @brief check compileted type
212 * @param[in] NODE pointer to lysc_type value
213 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
214 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
215 */
216#define CHECK_LYSC_TYPE(NODE, TYPE, EXTS) \
217 assert_non_null(NODE); \
218 assert_int_equal((NODE)->basetype, TYPE); \
219 CHECK_ARRAY((NODE)->exts, EXTS); \
220 assert_ptr_equal((NODE)->plugin, &(ly_builtin_type_plugins[TYPE]))
221
222/* @brief check compileted numeric type
223 * @param[in] NODE pointer to lysc_type_num value
224 * @param[in] TYPE expected type [LY_DATA_TYPE](@ref LY_DATA_TYPE)
225 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
226 * @warning only integer types INT, UINT, NUM
227 */
228#define CHECK_LYSC_TYPE_NUM(NODE, TYPE, EXTS, RANGE) \
229 CHECK_LYSC_TYPE(NODE, TYPE, EXTS);\
230 CHECK_POINTER((NODE)->range, RANGE)
231
232/* @brief check compiled string type
233 * @param[in] NODE pointer to lysc_type_num value
234 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
235 * @param[in] LENGTH 0 -> node dosnt have length limitation, 1 -> node have length limitation
236 * @param[in] PATTERNS expected number of patterns [sized array](@ref sizedarrays)
237 * @warning only integer types INT, UINT, NUM
238 */
239#define CHECK_LYSC_TYPE_STR(NODE, EXTS, LENGTH, PATTERNS) \
240 CHECK_LYSC_TYPE(NODE, LY_TYPE_STRING, EXTS); \
241 CHECK_POINTER((NODE)->length, LENGTH); \
242 CHECK_ARRAY((NODE)->patterns, PATTERNS)
243
Radek Išaded31052021-03-10 13:22:53 +0100244/* @brief check compiled bits type
245 * @param[in] NODE pointer to lysc_type_num value
246 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
247 * @param[in] BITS expected number of bits
248 * @warning only integer types INT, UINT, NUM
249 */
250#define CHECK_LYSC_TYPE_BITS(NODE, EXTS, BITS) \
251 CHECK_LYSC_TYPE(NODE, LY_TYPE_BITS, EXTS); \
252 CHECK_ARRAY((NODE)->bits, BITS)
253
Radek Išaded31052021-03-10 13:22:53 +0100254#define CHECK_LYSC_TYPE_BITENUM_ITEM(NODE, POSITION, DSC, EXTS, FLAGS, NAME, REF)\
255 assert_non_null(NODE); \
256 assert_int_equal((NODE)->position, POSITION); \
257 CHECK_STRING((NODE)->dsc, DSC); \
258 CHECK_ARRAY((NODE)->exts, EXTS); \
259 assert_int_equal((NODE)->flags, FLAGS); \
260 CHECK_STRING((NODE)->name, NAME); \
261 CHECK_STRING((NODE)->ref, REF) \
262
Radek Išaa9ff2b82021-01-13 21:44:13 +0100263/* @brief check range
264 * @param[in] NODE pointer to lysc_range value
265 * @param[in] DSC expected descriptin (string)
266 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
267 * @param[in] EMSG expected string reprezenting error message
268 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
269 * @param[in] PARTS expected [sized array](@ref sizedarrays) number of rang limitations
270 * @param[in] REF expected reference
271 */
272#define CHECK_LYSC_RANGE(NODE, DSC, EAPPTAG, EMSG, EXTS, PARTS, REF) \
273 assert_non_null(NODE); \
274 CHECK_STRING((NODE)->dsc, DSC); \
275 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
276 CHECK_STRING((NODE)->emsg, EMSG); \
277 CHECK_ARRAY((NODE)->exts, EXTS); \
278 CHECK_ARRAY((NODE)->parts, PARTS); \
279 CHECK_STRING((NODE)->ref, REF)
280
281/* @brief check pattern
282 * @param[in] NODE pointer to lysc_pattern value
283 * @param[in] DSC expected descriptin (string)
284 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
285 * @param[in] EMSG expected string reprezenting error message
286 * @param[in] EEXPR expected string reprezenting original, not compiled, regular expression
287 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
288 * @param[in] INVERTED if regular expression is inverted.
289 * @param[in] REF expected reference
290 */
291#define CHECK_LYSC_PATTERN(NODE, DSC, EAPPTAG, EMSG, EXPR, EXTS, INVERTED, REF) \
292 assert_non_null(NODE); \
293 assert_non_null((NODE)->code); \
294 CHECK_STRING((NODE)->dsc, DSC); \
295 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
296 CHECK_STRING((NODE)->emsg, EMSG); \
297 CHECK_STRING((NODE)->expr, EXPR); \
298 CHECK_ARRAY((NODE)->exts, EXTS); \
299 assert_int_equal((NODE)->inverted, INVERTED); \
300 CHECK_STRING((NODE)->ref, REF)
301
302/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 * @brief assert that lysp_action_inout structure members are correct
304 * @param[in] NODE pointer to lysp_action_inout variable
305 * @param[in] DATA 0 -> check if pointer to data is NULL, 1 -> check if pointer to data is not null
306 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
307 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
308 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of must restriction list
309 * @param[in] NODETYPE node type. LYS_INPUT or LYS_OUTPUT
310 * @param[in] PARENT 0 -> check if node is root, 1 -> check if node is not root
311 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of typedefs list
312 */
313#define CHECK_LYSP_ACTION_INOUT(NODE, DATA, EXTS, GROUPINGS, MUSTS, NODETYPE, PARENT, TYPEDEFS) \
314 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100315 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 CHECK_ARRAY((NODE)->exts, EXTS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100317 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 CHECK_ARRAY((NODE)->musts, MUSTS); \
319 assert_int_equal((NODE)->nodetype, NODETYPE); \
320 CHECK_POINTER((NODE)->parent, PARENT); \
321 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS);
322
323/**
324 * @brief assert that lysp_action structure members are correct
325 * @param[in] NODE pointer to lysp_action variable
326 * @param[in] DSC expected description
327 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extension list
328 * @param[in] FLAGS expected [schema node flags](@ref snodeflags)
329 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
330 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of if-feature expressions list
331 * @param[in] INPUT_* ::LYSP_ACTION_INOUT_CHECK
332 * @param[in] NAME expected name
333 * @param[in] NODETYPE node type. LYS_RPC or LYS_ACTION
334 * @param[in] OUTPUT_* ::LYSP_ACTION_INOUT_CHECK
335 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
336 * @param[in] REF expected reference
337 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of list of typedefs
338 */
339#define CHECK_LYSP_ACTION(NODE, DSC, EXTS, FLAGS, GROUPINGS, IFFEATURES, \
340 INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, INPUT_MUSTS, \
341 INPUT_PARENT, INPUT_TYPEDEFS, \
342 NAME, NODETYPE, \
343 OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, OUTPUT_MUSTS, \
344 OUTPUT_PARENT, OUTPUT_TYPEDEFS, \
345 PARENT, REF, TYPEDEFS) \
346 assert_non_null(NODE); \
347 CHECK_STRING((NODE)->dsc, DSC); \
348 CHECK_ARRAY((NODE)->exts, EXTS); \
349 assert_int_equal((NODE)->flags, FLAGS); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100350 CHECK_POINTER((NODE)->groupings, GROUPINGS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200351 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
352 CHECK_LYSP_ACTION_INOUT(&((NODE)->input), INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, \
353 INPUT_MUSTS, LYS_INPUT, INPUT_PARENT, INPUT_TYPEDEFS); \
354 assert_string_equal((NODE)->name, NAME); \
355 assert_int_equal((NODE)->nodetype, NODETYPE); \
356 CHECK_LYSP_ACTION_INOUT(&((NODE)->output), OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, \
357 OUTPUT_MUSTS, LYS_OUTPUT, OUTPUT_PARENT, OUTPUT_TYPEDEFS); \
358 CHECK_POINTER((NODE)->parent, PARENT); \
359 CHECK_STRING((NODE)->ref, REF); \
360 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS) \
361
362/**
363 * @brief assert that lysp_when structure members are correct
364 * @param[in] NODE pointer to lysp_when variable
365 * @param[in] COND expected string specifid condition
366 * @param[in] DSC expected string description statement
367 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
368 * @param[in] REF expected string reference
369 */
370#define CHECK_LYSP_WHEN(NODE, COND, DSC, EXTS, REF) \
371 assert_non_null(NODE); \
372 assert_string_equal((NODE)->cond, COND); \
373 CHECK_STRING((NODE)->dsc, DSC); \
374 CHECK_ARRAY((NODE)->exts, EXTS); \
375 if (REF == NULL) { \
376 assert_null((NODE)->ref); \
377 } else { \
378 assert_non_null((NODE)->ref); \
379 assert_string_equal((NODE)->ref, REF); \
380 }
381
382/**
383 * @brief assert that lysp_restr structure members are correct
384 * @param[in] NODE pointer to lysp_restr variable
385 * @param[in] ARG_STR expected string. The restriction expression/value
386 * @param[in] DSC expected descrition
387 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
388 * @param[in] EMSG expected string reprezenting error message
389 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
390 * @param[in] REF expected reference
391 */
392
393#define CHECK_LYSP_RESTR(NODE, ARG_STR, DSC, EAPPTAG, EMSG, EXTS, REF) \
394 assert_non_null(NODE); \
395 assert_non_null((NODE)->arg.mod); \
396 assert_string_equal((NODE)->arg.str, ARG_STR); \
397 CHECK_STRING((NODE)->dsc, DSC); \
398 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
399 CHECK_STRING((NODE)->emsg, EMSG); \
400 CHECK_ARRAY((NODE)->exts, EXTS); \
401 CHECK_STRING((NODE)->ref, REF);
402
403/**
404 * @brief assert that lysp_import structure members are correct
405 * @param[in] NODE pointer to lysp_import variable
406 * @param[in] DSC expected description or NULL
407 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extensions
408 * @param[in] NAME expected name of imported module
409 * @param[in] PREFIX expected prefix for the data from the imported schema
410 * @param[in] REF expected reference
411 * @prame[in] REV expected reprezenting date in format "11-10-2020"
412 */
413#define CHECK_LYSP_IMPORT(NODE, DSC, EXTS, NAME, PREFIX, REF, REV) \
414 assert_non_null(NODE); \
415 CHECK_STRING((NODE)->dsc, DSC); \
416 CHECK_ARRAY((NODE)->exts, EXTS); \
417 /*assert_non_null((NODE)->module); // ?? it is mandatory but in some test it doesnt work */ \
418 assert_string_equal((NODE)->name, NAME); \
419 assert_string_equal((NODE)->prefix, PREFIX); \
420 CHECK_STRING((NODE)->ref, REF); \
421 CHECK_STRING((NODE)->rev, REV); \
422
423/**
424 * @brief assert that lysp_ext structure members are correct
425 * @param[in] NODE pointer to lysp_ext_instance variable
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100426 * @param[in] ARGNAME expected argument name
Radek Iša56ca9e42020-09-08 18:42:00 +0200427 * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists
428 * @param[in] DSC expected string reprezent description
429 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
430 * @param[in] FLAGS expected LYS_STATUS_* and LYS_YINELEM_* values (@ref snodeflags)
431 * @param[in] NAME expected name
432 * @param[in] REF expected ref
433 */
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100434#define CHECK_LYSP_EXT(NODE, ARGNAME, COMPILED, DSC, EXTS, FLAGS, NAME, REF) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200435 assert_non_null(NODE); \
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100436 CHECK_STRING((NODE)->argname, ARGNAME); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200437 CHECK_POINTER((NODE)->compiled, COMPILED); \
438 CHECK_STRING((NODE)->dsc, DSC); \
439 CHECK_ARRAY((NODE)->exts, EXTS); \
440 assert_int_equal((NODE)->flags, FLAGS); \
441 assert_string_equal((NODE)->name, NAME); \
442 CHECK_STRING((NODE)->ref, REF);
443
444/**
445 * @brief assert that lysp_ext_instance structure members are correct
446 * @param[in] NODE pointer to lysp_ext_instance variable
447 * @param[in] ARGUMENT expected optional value of the extension's argument
448 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
Radek Krejciab430862021-03-02 20:13:40 +0100449 * @param[in] PARENT_STMT expected value identifying placement of the extension instance
450 * @param[in] PARENT_STMT_INDEX expected indentifi index
Michal Vaskofc2cd072021-02-24 13:17:17 +0100451 * @param[in] FORMAT expected format
Radek Iša56ca9e42020-09-08 18:42:00 +0200452 */
Radek Krejci85ac8312021-03-03 20:21:33 +0100453#define CHECK_LYSP_EXT_INSTANCE(NODE, ARGUMENT, CHILD, PARENT_STMT, PARENT_STMT_INDEX, NAME, FORMAT) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200454 assert_non_null(NODE); \
455 CHECK_STRING((NODE)->argument, ARGUMENT); \
456 CHECK_POINTER((NODE)->child, CHILD); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200457 /*assert_int_equal((NODE)->flags, LYS_INTERNAL);*/ \
Radek Krejciab430862021-03-02 20:13:40 +0100458 assert_int_equal((NODE)->parent_stmt, PARENT_STMT); \
459 assert_int_equal((NODE)->parent_stmt_index, PARENT_STMT_INDEX); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200460 assert_string_equal((NODE)->name, NAME); \
Michal Vaskofc2cd072021-02-24 13:17:17 +0100461 assert_int_equal((NODE)->format, FORMAT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200462
463/**
464 * @brief assert that lysp_stmt structure members are correct
465 * @param[in] NODE pointer to lysp_stmt variable
466 * @param[in] ARG expected statemet argumet
467 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
468 * @param[in] FLAGS expected statement flags, can be set to LYS_YIN_ATTR
469 * @param[in] KW expected numeric respresentation of the stmt value
470 * @param[in] NEXT 0 -> pointer is NULL, 1 -> pointer is not null
471 * @param[in] STMS expected identifier of the statement
472 */
473#define CHECK_LYSP_STMT(NODE, ARG, CHILD, FLAGS, KW, NEXT, STMT) \
474 assert_non_null(NODE); \
475 CHECK_STRING((NODE)->arg, ARG); \
476 CHECK_POINTER((NODE)->child, CHILD); \
477 assert_int_equal((NODE)->flags, FLAGS); \
478 assert_int_equal((NODE)->kw, KW); \
479 CHECK_POINTER((NODE)->next, NEXT); \
480 assert_string_equal((NODE)->stmt, STMT); \
481
482/**
483 * @brief assert that lysp_type_enum structure members are correct
484 * @param[in] NODE pointer to lysp_type_enum variable
485 * @param[in] DSC expected description
486 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
487 * @param[in] FLAGS only LYS_STATUS_ and LYS_SET_VALUE values are allowed
488 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
489 * @param[in] NAME expected name
490 * @param[in] REF expected reference statement
491 * @param[in] VALUE expected enum's value or bit's position
492 */
493#define CHECK_LYSP_TYPE_ENUM(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, REF, VALUE) \
494 assert_non_null(NODE); \
495 CHECK_STRING((NODE)->dsc, DSC); \
496 CHECK_ARRAY((NODE)->exts, EXTS); \
497 assert_int_equal((NODE)->flags, FLAGS); \
498 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
499 CHECK_STRING((NODE)->name, NAME); \
500 CHECK_STRING((NODE)->ref, REF); \
501 assert_int_equal(VALUE, (NODE)->value);
502
503/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100504 * @brief assert that lysp_type_enum structure members are correct
505 * @param[in] NODE pointer to lysp_type variable
506 * @param[in] BASES expected [sized array](@ref sizedarrays) size of list of indentifiers
507 * @param[in] BITS expected [sized array](@ref sizedarrays) size of list of bits
508 * @param[in] COMPILED 0 -> pointer to compiled type is null, 1 -> pointer to compilet type is valid
509 * @param[in] ENUMS expected [sized array](@ref sizedarrays) size of list of enums-stmts
510 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
511 * @param[in] FLAGS expected flags
512 * @param[in] FRACTION_DIGITS expected number of fraction digits decimal64
513 * @param[in] LENGTH expected 0 -> there isnt any restriction on length, 1 -> type is restricted on length (string, binary)
514 * @param[in] NAME expected name of type
515 * @param[in] PATH 0 -> no pointer to parsed path, 1 -> pointer to parsed path is valid
516 * @param[in] PATTERNS expected [sized array](@ref sizedarrays) size of list of patterns for string
517 * @param[in] PMOD expected submodule where type is defined 0 -> pointer is null, 1 -> pointer is not null
518 * @param[in] RANGE expected [sized array](@ref sizedarrays) size of list of range restriction
519 * @param[in] REQUIRE_INSTANCE expected require instance flag
520 * @param[in] TYPES expected [sized array](@ref sizedarrays) size of list of sub-types
521 */
522#define CHECK_LYSP_TYPE(NODE, BASES, BITS, COMPILED, ENUMS, EXTS, FLAGS, FRACTIONS_DIGITS, \
523 LENGTH, NAME, PATH, PATTERNS, PMOD, RANGE, REQUIRE_INSTANCE, TYPES) \
524 assert_non_null(NODE);\
525 CHECK_ARRAY((NODE)->bases, BASES); \
526 CHECK_ARRAY((NODE)->bits, BITS); \
527 CHECK_POINTER((NODE)->compiled, COMPILED); \
528 CHECK_ARRAY((NODE)->enums, ENUMS); \
529 CHECK_ARRAY((NODE)->exts, EXTS); \
530 assert_int_equal((NODE)->flags, FLAGS); \
531 assert_int_equal((NODE)->fraction_digits, FRACTIONS_DIGITS); \
532 CHECK_POINTER((NODE)->length, LENGTH); \
533 CHECK_STRING((NODE)->name, NAME); \
534 CHECK_POINTER((NODE)->path, PATH); \
535 CHECK_ARRAY((NODE)->patterns, PATTERNS); \
536 CHECK_POINTER((NODE)->pmod, PMOD); \
537 CHECK_POINTER((NODE)->range, RANGE); \
538 assert_int_equal((NODE)->require_instance, REQUIRE_INSTANCE); \
539 CHECK_ARRAY((NODE)->types , TYPES)
540
541/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200542 * @brief assert that lysp_node structure members are correct
543 * @param[in] NODE pointer to lysp_node variable
544 * @param[in] DSC expected description statement
545 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
546 * @param[in] FLAGS [schema node flags](@ref snodeflags)
547 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
548 * @param[in] NAME expected name
549 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
550 * @param[in] NODETYPE node type LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, LYS_LEAFLIST,
551 * LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, LYS_ACTION, LYS_NOTIF,
552 * LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, LYS_AUGMENT
553 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
554 * @param[in] REF expected reference statement
555 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
556 */
557#define CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, NODETYPE, PARENT, REF, WHEN) \
558 assert_non_null(NODE); \
559 CHECK_STRING((NODE)->dsc, DSC); \
560 CHECK_ARRAY((NODE)->exts, EXTS); \
561 assert_int_equal((NODE)->flags, FLAGS); \
562 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
563 CHECK_STRING((NODE)->name, NAME); \
564 CHECK_POINTER((NODE)->next, NEXT); \
565 assert_int_equal((NODE)->nodetype, NODETYPE); \
566 CHECK_POINTER((NODE)->parent, PARENT); \
567 CHECK_STRING((NODE)->ref, REF); \
Radek Krejci9a3823e2021-01-27 20:26:46 +0100568 CHECK_POINTER(lysp_node_when((struct lysp_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200569
570/**
Radek Išaa9ff2b82021-01-13 21:44:13 +0100571 * @brief assert that lysp_node structure members are correct
572 * @param[in] NODE pointer to lysp_node variable
573 * @param[in] DSC expected description statement
574 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
575 * @param[in] FLAGS [schema node flags](@ref snodeflags)
576 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
577 * @param[in] NAME expected name
578 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
579 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
580 * @param[in] REF expected reference statement
581 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
582 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
583 * @param[in] UNITS expected string reprezenting units
584 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
585 */
586#define CHECK_LYSP_NODE_LEAF(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, \
587 PARENT, REF, WHEN, MUSTS, UNITS, DFLT) \
588 CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, LYS_LEAF, PARENT, REF, WHEN); \
589 CHECK_ARRAY((NODE)->musts, MUSTS); \
590 CHECK_STRING((NODE)->units, UNITS); \
591 CHECK_STRING((NODE)->dflt.str, DFLT);
592
593/**
Radek Iša56ca9e42020-09-08 18:42:00 +0200594 * @brief assert that lysc_notif structure members are correct
595 * @param[in] NODE pointer to lysp_notif variable
596 * @param[in] DATA 0 pointer is null, 1 pointer is not null
597 * @param[in] DSC expected description
598 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
599 * @param[in] FLAGS [schema node flags](@ref snodeflags)
600 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
601 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
602 * @param[in] NAME expected name
603 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
604 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
605 * @param[in] REF expected reference
606 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
607 */
608#define CHECK_LYSC_NOTIF(NODE, DATA, DSC, EXTS, FLAGS, MODULE, MUSTS, NAME, PARENT, PRIV, REF, WHEN) \
609 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100610 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200611 CHECK_STRING((NODE)->dsc, DSC); \
612 CHECK_ARRAY((NODE)->exts, EXTS); \
613 assert_int_equal((NODE)->flags, FLAGS); \
614 CHECK_POINTER((NODE)->module, MODULE); \
615 CHECK_ARRAY((NODE)->musts, MUSTS); \
616 assert_string_equal((NODE)->name, NAME); \
617 assert_int_equal((NODE)->nodetype, LYS_NOTIF); \
618 CHECK_POINTER((NODE)->parent, PARENT); \
619 CHECK_POINTER((NODE)->priv, PRIV); \
620 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100621 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200622
623/**
624 * @brief assert that lysc_action_inout structure members are correct
625 * @param[in] NODE pointer to lysp_notif variable
626 * @param[in] DATA 0 pointer is null, 1 pointer is not null
627 * @param[in] MUST expected [sized array](@ref sizedarrays) size of list of must restrictions
628 * @param[in] NODETYPE LYS_INPUT or LYS_OUTPUT
629 */
630#define CHECK_LYSC_ACTION_INOUT(NODE, DATA, MUST, NODETYPE) \
631 assert_non_null(NODE); \
Radek Krejci01180ac2021-01-27 08:48:22 +0100632 CHECK_POINTER((NODE)->child, DATA); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200633 CHECK_ARRAY((NODE)->musts, MUST); \
634 assert_int_equal((NODE)->nodetype, NODETYPE);
635
636/**
637 * @brief assert that lysc_action structure members are correct
638 * @param[in] NODE pointer to lysp_action variable
639 * @param[in] DSC string description statement
640 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
641 * @param[in] FLAGS [schema node flags](@ref snodeflags)
642 * @param[in] INPUT_DATA 0 pointer is null, 1 pointer is not null
643 * @param[in] INPUT_MUST expected [sized array](@ref sizedarrays) size of input list of must restrictions
644 * @param[in] INPUT_EXTS expected [sized array](@ref sizedarrays) size of the input extension instances of input
645 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
646 * @param[in] NAME expected name
647 * @param[in] NODETYPE LYS_RPC, LYS_ACTION
648 * @param[in] OUTPUT_DATA 0 pointer is null, 1 pointer is not null
649 * @param[in] OUTPUT_MUST expected [sized array](@ref sizedarrays) size of output list of must restrictions
650 * @param[in] OUTPUT_EXTS expected [sized array](@ref sizedarrays) size of the output extension instances of input
651 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
652 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
653 * @param[in] REF expected reference
654 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
655 */
656#define CHECK_LYSC_ACTION(NODE, DSC, EXTS, FLAGS, INPUT_DATA, INPUT_MUST, INPUT_EXTS, MODULE, NAME, NODETYPE, \
657 OUTPUT_DATA, OUTPUT_MUST, OUTPUT_EXTS, PARENT, PRIV, REF, WHEN) \
658 assert_non_null(NODE); \
659 CHECK_STRING((NODE)->dsc, DSC); \
660 CHECK_ARRAY((NODE)->exts, EXTS); \
661 assert_int_equal((NODE)->flags, FLAGS); \
662 CHECK_LYSC_ACTION_INOUT(&(NODE)->input, INPUT_DATA, INPUT_MUST, LYS_INPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100663 CHECK_ARRAY((NODE)->input.exts, INPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200664 CHECK_POINTER((NODE)->module, MODULE); \
665 assert_string_equal((NODE)->name, NAME); \
666 assert_int_equal((NODE)->nodetype, NODETYPE); \
667 CHECK_LYSC_ACTION_INOUT(&(NODE)->output, OUTPUT_DATA, OUTPUT_MUST, LYS_OUTPUT); \
Radek Krejci2a9fc652021-01-22 17:44:34 +0100668 CHECK_ARRAY((NODE)->output.exts, OUTPUT_EXTS); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200669 CHECK_POINTER((NODE)->parent, PARENT); \
670 CHECK_POINTER((NODE)->priv, PRIV); \
671 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100672 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200673
674/**
675 * @brief assert that lysc_node structure members are correct
676 * @param[in] NODE pointer to lysc_node variable
677 * @param[in] DSC expected description
678 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
679 * @param[in] FLAGS [schema node flags](@ref snodeflags)
680 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
681 * @param[in] NAME expected name
682 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
683 * @param[in] NODETYPE type of the node LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF,
684 * LYS_LEAFLIST, LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC,
685 * LYS_ACTION, LYS_NOTIF, LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING,
686 * LYS_AUGMENT
687 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
688 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
689 * @param[in] REF expected reference
690 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
691 */
692#define CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, NODETYPE, PARENT, PRIV, REF, WHEN) \
693 assert_non_null(NODE); \
694 CHECK_STRING((NODE)->dsc, DSC); \
695 CHECK_ARRAY((NODE)->exts, EXTS); \
696 assert_int_equal((NODE)->flags, FLAGS); \
697 CHECK_POINTER((NODE)->module, MODULE); \
698 assert_string_equal((NODE)->name, NAME); \
699 CHECK_POINTER((NODE)->next, NEXT); \
700 assert_int_equal((NODE)->nodetype, NODETYPE); \
701 CHECK_POINTER((NODE)->parent, PARENT); \
702 assert_non_null((NODE)->prev); \
703 CHECK_POINTER((NODE)->priv, PRIV); \
704 CHECK_STRING((NODE)->ref, REF); \
Radek Išaa9ff2b82021-01-13 21:44:13 +0100705 CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN);
706
707/**
708 * @brief assert that lysc_node_leaf structure members are correct
709 * @param[in] NODE pointer to lysc_node variable
710 * @param[in] DSC expected description
711 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
712 * @param[in] FLAGS [schema node flags](@ref snodeflags)
713 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
714 * @param[in] NAME expected name
715 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
716 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
717 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
718 * @param[in] REF expected reference
719 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
720 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
721 * @param[in] UNITS expected string reprezenting units
722 * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value
723 */
724#define CHECK_LYSC_NODE_LEAF(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \
725 PARENT, PRIV, REF, WHEN, MUSTS, UNITS, DFLT) \
726 CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LEAF, PARENT, PRIV, REF, WHEN); \
727 CHECK_ARRAY((NODE)->musts, MUSTS); \
728 assert_non_null((NODE)->type); \
729 CHECK_STRING((NODE)->units, UNITS); \
730 CHECK_POINTER((NODE)->dflt, DFLT);
Radek Iša56ca9e42020-09-08 18:42:00 +0200731
732/**
733 * @brief assert that lyd_meta structure members are correct
734 * @param[in] NODE pointer to lyd_meta variable
735 * @param[in] ANNOTATION 0 pointer is null, 1 pointer is not null
736 * @param[in] NAME expected name
737 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
738 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
739 * part of text reprezenting LY_DATA_TYPE.
740 * @param[in] ... ::CHECK_LYD_VALUE
741 */
742#define CHECK_LYD_META(NODE, ANNOTATION, NAME, NEXT, PARENT, TYPE_VAL, ...) \
743 assert_non_null(NODE); \
744 CHECK_POINTER((NODE)->annotation, ANNOTATION); \
745 assert_string_equal((NODE)->name, NAME); \
746 CHECK_POINTER((NODE)->next, NEXT); \
747 CHECK_POINTER((NODE)->parent, PARENT); \
748 CHECK_LYD_VALUE((NODE)->value, TYPE_VAL, ##__VA_ARGS__);
749
750/**
751 * @brief assert that lyd_node_term structure members are correct
752 * @param[in] NODE pointer to lyd_node_term variable
753 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
754 * @param[in] META 0 -> meta is not prezent, 1 -> meta is prezent
755 * @param[in] NEXT 0 -> next node is not prezent, 1 -> next node is prezent
756 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
757 * part of text reprezenting LY_DATA_TYPE.
758 * @param[in] ... ::CHECK_LYD_VALUE
759 */
760#define CHECK_LYD_NODE_TERM(NODE, FLAGS, META, NEXT, PARENT, SCHEMA, VALUE_TYPE, ...) \
761 assert_non_null(NODE); \
762 assert_int_equal((NODE)->flags, FLAGS); \
763 CHECK_POINTER((NODE)->meta, META); \
764 CHECK_POINTER((NODE)->next, NEXT); \
765 CHECK_POINTER((NODE)->parent, PARENT); \
766 assert_non_null((NODE)->prev); \
767 CHECK_POINTER((NODE)->schema, SCHEMA); \
768 CHECK_LYD_VALUE((NODE)->value, VALUE_TYPE, ##__VA_ARGS__);
769
770/**
771 * @brief assert that lyd_node_any structure members are correct
772 * @param[in] NODE pointer to lyd_node_term variable
773 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
774 * @param[in] META 0 meta isnt present , 1 meta is present
775 * @param[in] PARENT 0 it is root node , 1 node have parent
776 * @param[in] VALUE_TYPE value type ::lyd_node_any.value
777 */
778#define CHECK_LYD_NODE_ANY(NODE, FLAGS, META, PARENT, VALUE_TYPE) \
779 assert_non_null(NODE); \
780 assert_int_equal((NODE)->flags, FLAGS); \
781 CHECK_POINTER((NODE)->meta, META); \
782 CHECK_POINTER((NODE)->meta, PARENT); \
783 assert_non_null((NODE)->prev); \
784 assert_non_null((NODE)->schema); \
785 assert_int_equal((NODE)->value_type, VALUE_TYPE);
786
787/**
788 * @brief assert that lyd_node_opaq structure members are correct
789 * @param[in] NODE pointer to lyd_node_opaq variable
790 * @param[in] ATTR 0 if pointer is null ,1 if pointer is not null
791 * @param[in] CHILD 0 if pointer is null ,1 if pointer is not null
792 * @param[in] FORMAT LY_PREF_XML or LY_PREF_JSON
793 * @param[in] VAL_PREFS 0 if pointer is null ,1 if pointer is not null
794 * @param[in] NAME expected name
795 * @param[in] value expected orignal value
796 */
797#define CHECK_LYD_NODE_OPAQ(NODE, ATTR, CHILD, FORMAT, NAME, NEXT, PARENT, PREFIX, VAL_PREFS, VALUE) \
798 assert_non_null(NODE); \
799 CHECK_POINTER((NODE)->attr, ATTR); \
800 CHECK_POINTER((NODE)->child, CHILD); \
801 assert_ptr_equal((NODE)->ctx, UTEST_LYCTX); \
802 assert_int_equal((NODE)->flags, 0); \
803 assert_true((NODE)->format == FORMAT); \
804 assert_int_equal((NODE)->hash, 0); \
805 assert_string_equal((NODE)->name.name, NAME); \
806 assert_non_null((NODE)->prev); \
807 assert_null((NODE)->schema); \
808 CHECK_POINTER((NODE)->val_prefix_data, VAL_PREFS); \
809 assert_string_equal((NODE)->value, VALUE);
810
811/**
812 * @brief assert that lyd_value structure members are correct
813 * @param[in] NODE lyd_value
814 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
815 * part of text reprezenting LY_DATA_TYPE.
816 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
817 * by type of value. These parameters are passed to macro
818 * CHECK_LYD_VALUE_ ## TYPE_VAL.
819 */
820#define CHECK_LYD_VALUE(NODE, TYPE_VAL, ...) \
821 CHECK_LYD_VALUE_ ## TYPE_VAL (NODE, ##__VA_ARGS__);
822
823/*
824 * LYD VALUES CHECKING SPECIALIZATION
825 */
826
827/**
828 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type EMPTY
829 * Example CHECK_LYD_VALUE(node->value, EMPTY, "");
830 * @param[in] NODE lyd_value variable
831 * @param[in] CANNONICAL_VAL expected cannonical value
832 */
833#define CHECK_LYD_VALUE_EMPTY(NODE, CANNONICAL_VAL) \
834 assert_non_null((NODE).canonical); \
835 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
836 assert_non_null((NODE).realtype); \
837 assert_int_equal((NODE).realtype->basetype, LY_TYPE_EMPTY);
838
839/**
840 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UNION
841 * Example CHECK_LYD_VALUE(node->value, UNION, "12", INT8, "12", 12);
842 * @warning type of subvalue cannot be UNION. Example of calling
843 * @param[in] NODE lyd_value variable
844 * @param[in] CANNONICAL_VAL expected cannonical value
845 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
846 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
847 * by type of value. These parameters are passed to macro
848 * CHECK_LYD_VALUE_ ## TYPE_VAL.
849 */
850#define CHECK_LYD_VALUE_UNION(NODE, CANNONICAL_VAL, TYPE_VAL, ...) \
851 assert_non_null((NODE).canonical); \
852 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
853 assert_non_null((NODE).realtype); \
854 assert_int_equal(LY_TYPE_UNION, (NODE).realtype->basetype); \
855 assert_non_null((NODE).subvalue); \
856 assert_non_null((NODE).subvalue->prefix_data); \
857 CHECK_LYD_VALUE_ ## TYPE_VAL ((NODE).subvalue->value, ## __VA_ARGS__)
858
859/**
860 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BITS
861 * Example arr[] = {"a", "b"}; CHECK_LYD_VALUE(node->value, BITS, "a b", arr);
862 * @param[in] NODE lyd_value variable
863 * @param[in] CANNONICAL_VAL expected cannonical value
864 * @param[in] VALUE expected array of bits names
865 */
Radek Išaded31052021-03-10 13:22:53 +0100866#define CHECK_LYD_VALUE_BITS(NODE, CANNONICAL_VAL, ...) \
Radek Iša56ca9e42020-09-08 18:42:00 +0200867 assert_non_null((NODE).canonical); \
868 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
869 assert_non_null((NODE).realtype); \
870 assert_int_equal(LY_TYPE_BITS, (NODE).realtype->basetype); \
871 { \
Radek Išaded31052021-03-10 13:22:53 +0100872 const char *arr[] = { __VA_ARGS__ }; \
873 LY_ARRAY_COUNT_TYPE arr_size = sizeof(arr) / sizeof(arr[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200874 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).bits_items)); \
875 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
Radek Išaded31052021-03-10 13:22:53 +0100876 assert_string_equal(arr[it], (NODE).bits_items[it]->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200877 } \
878 }
879
880/**
881 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INST
882 * @param[in] NODE lyd_value variable
883 * @param[in] CANNONICAL_VAL expected cannonical value
884 * @param[in] VALUE expected array of enum ly_path_pred_type
885 * @brief Example enum arr[] = {0x0, 0x1}; CHECK_LYD_VALUE(node->value, INST, "test/d", arr);
886 */
887#define CHECK_LYD_VALUE_INST(NODE, CANNONICAL_VAL, VALUE) \
888 assert_non_null((NODE).canonical); \
889 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
890 assert_non_null((NODE).realtype); \
891 assert_int_equal(LY_TYPE_INST, (NODE).realtype->basetype); \
892 { \
Michal Vasko79135ae2020-12-16 10:08:35 +0100893 LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200894 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).target)); \
895 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
896 assert_int_equal(VALUE[it], (NODE).target[it].pred_type); \
897 } \
898 }
899
900/**
901 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type ENUM.
902 * Example CHECK_LYD_VALUE(node->value, ENUM, "item_name", "item_name");
903 * @param[in] NODE lyd_value variable
904 * @param[in] CANNONICAL_VAL expected cannonical value
905 * @param[in] VALUE expected enum item name
906 */
907#define CHECK_LYD_VALUE_ENUM(NODE, CANNONICAL_VAL, VALUE) \
908 assert_non_null((NODE).canonical); \
909 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
910 assert_non_null((NODE).realtype); \
911 assert_int_equal(LY_TYPE_ENUM, (NODE).realtype->basetype); \
912 assert_string_equal(VALUE, (NODE).enum_item->name);
913
914/**
915 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT8
916 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
917 * @param[in] NODE lyd_value variable
918 * @param[in] CANNONICAL_VAL expected cannonical value
919 * @param[in] VALUE expected inteager value (-128 to 127).
920 */
921#define CHECK_LYD_VALUE_INT8(NODE, CANNONICAL_VAL, VALUE) \
922 assert_non_null((NODE).canonical); \
923 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
924 assert_non_null((NODE).realtype); \
925 assert_int_equal(LY_TYPE_INT8, (NODE).realtype->basetype); \
926 assert_int_equal(VALUE, (NODE).int8);
927
928/**
929 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT16
930 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
931 * @param[in] NODE lyd_value variable
932 * @param[in] CANNONICAL_VAL expected cannonical value
933 * @param[in] VALUE expected inteager value.
934 */
935#define CHECK_LYD_VALUE_INT16(NODE, CANNONICAL_VAL, VALUE) \
936 assert_non_null((NODE).canonical); \
937 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
938 assert_non_null((NODE).realtype); \
939 assert_int_equal(LY_TYPE_INT16, (NODE).realtype->basetype); \
940 assert_int_equal(VALUE, (NODE).int16);
941
942/**
943 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT8.
944 * Example CHECK_LYD_VALUE(node->value, UINT8, "12", 12);
945 * @param[in] NODE lyd_value variable
946 * @param[in] CANNONICAL_VAL expected cannonical value
947 * @param[in] VALUE expected inteager (0 to 255).
948 */
949#define CHECK_LYD_VALUE_UINT8(NODE, CANNONICAL_VAL, VALUE) \
950 assert_non_null((NODE).canonical); \
951 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
952 assert_non_null((NODE).realtype); \
953 assert_int_equal(LY_TYPE_UINT8, (NODE).realtype->basetype); \
954 assert_int_equal(VALUE, (NODE).uint8);
955
956/**
957 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type STRING.
958 * Example CHECK_LYD_VALUE(node->value, STRING, "text");
959 * @param[in] NODE lyd_value variable
960 * @param[in] CANNONICAL_VAL expected cannonical value
961 */
962#define CHECK_LYD_VALUE_STRING(NODE, CANNONICAL_VAL) \
963 assert_non_null((NODE).canonical); \
964 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
965 assert_non_null((NODE).realtype); \
966 assert_int_equal(LY_TYPE_STRING, (NODE).realtype->basetype);
967
968/**
969 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type LEAFREF
970 * @brief Example CHECK_LYD_VALUE(node->value, LEAFREF, "");
971 * @param[in] NODE lyd_value variable
972 * @param[in] CANNONICAL_VAL expected cannonical value
973 */
974#define CHECK_LYD_VALUE_LEAFREF(NODE, CANNONICAL_VAL) \
975 assert_non_null((NODE).canonical); \
976 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
977 assert_non_null((NODE).realtype); \
978 assert_int_equal(LY_TYPE_LEAFREF, (NODE).realtype->basetype); \
979 assert_non_null((NODE).ptr)
980
981/**
982 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type DEC64
983 * Example CHECK_LYD_VALUE(node->value, DEC64, "125", 125);
984 * @param[in] NODE lyd_value variable
985 * @param[in] CANNONICAL_VAL expected cannonical value
986 * @param[in] VALUE expected value 64bit inteager
987*/
988#define CHECK_LYD_VALUE_DEC64(NODE, CANNONICAL_VAL, VALUE) \
989 assert_non_null((NODE).canonical); \
990 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
991 assert_non_null((NODE).realtype); \
992 assert_int_equal(LY_TYPE_DEC64, (NODE).realtype->basetype); \
993 assert_int_equal(VALUE, (NODE).dec64);
994
995/**
996 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BINARY.
997 * Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8=");
998 * @param[in] NODE lyd_value variable
999 * @param[in] CANNONICAL_VAL expected cannonical value
1000*/
1001#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL) \
1002 assert_non_null((NODE).canonical); \
1003 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
1004 assert_non_null((NODE).realtype); \
1005 assert_int_equal(LY_TYPE_BINARY, (NODE).realtype->basetype);
1006
1007/**
1008 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BOOL.
1009 * Example CHECK_LYD_VALUE(node->value, BOOL, "true", 1);
1010 * @param[in] NODE lyd_value variable
1011 * @param[in] CANNONICAL_VAL expected cannonical value
1012 * @param[in] VALUE expected boolean value 0,1
1013*/
1014#define CHECK_LYD_VALUE_BOOL(NODE, CANNONICAL_VAL, VALUE) \
1015 assert_non_null((NODE).canonical); \
1016 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
1017 assert_non_null((NODE).realtype); \
1018 assert_int_equal(LY_TYPE_BOOL, (NODE).realtype->basetype); \
1019 assert_int_equal(VALUE, (NODE).boolean);
1020
1021/**
1022 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type IDENT.
1023 * Example CHECK_LYD_VALUE(node->value, IDENT, "types:gigabit-ethernet", "gigabit-ethernet");
1024 * @param[in] NODE lyd_value variable
1025 * @param[in] CANNONICAL_VAL expected cannonical value
1026 * @param[in] VALUE expected ident name
1027*/
1028#define CHECK_LYD_VALUE_IDENT(NODE, CANNONICAL_VAL, VALUE) \
1029 assert_non_null((NODE).canonical); \
1030 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
1031 assert_non_null((NODE).realtype); \
1032 assert_int_equal(LY_TYPE_IDENT, (NODE).realtype->basetype); \
1033 assert_string_equal(VALUE, (NODE).ident->name);
1034
1035/**
Radek Išaa9ff2b82021-01-13 21:44:13 +01001036 * @brief Macro testing parser when parsing incorrect module;
1037 * @param[in] DATA String storing the schema module representation.
1038 * @param[in] FORMAT Schema format of the @p DATA
1039 * @param[in] FEATURES Array of module's features to enable
1040 * @param[in] RET_VAL ly_in_new_memory return error value
1041 */
1042#define UTEST_INVALID_MODULE(DATA, FORMAT, FEATURES, RET_VAL) \
1043 { \
1044 const struct lys_module *mod; \
1045 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
1046 assert_int_equal(RET_VAL, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, &mod)); \
1047 assert_null(mod); \
1048 } \
1049 ly_in_free(_UC->in, 0); \
1050 _UC->in = NULL; \
1051
1052/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001053 * @brief Add module (from a string) into the used libyang context.
1054 * @param[in] DATA String storing the schema module representation.
1055 * @param[in] FORMAT Schema format of the @p DATA
1056 * @param[in] FEATURES Array of module's features to enable
1057 * @param[out] MOD Optional parameter as a pointer to variable to store the resulting module.
1058 */
1059#define UTEST_ADD_MODULE(DATA, FORMAT, FEATURES, MOD) \
1060 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
1061 assert_int_equal(LY_SUCCESS, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, MOD)); \
1062 ly_in_free(_UC->in, 0); \
1063 _UC->in = NULL
1064
1065/**
1066 * @brief Internal macro to compare error info record with the expected error message and path.
1067 * If NULL is provided as MSG, no error info record (NULL) is expected.
1068 * @param[in] ERR Error information record from libyang context.
1069 * @param[in] MSG Expected error message.
1070 * @param[in] PATH Expected error path.
1071 *
1072 */
1073#define _CHECK_LOG_CTX(ERR, MSG, PATH) \
1074 if (!MSG) { \
1075 assert_null(ERR); \
1076 } else { \
1077 assert_non_null(ERR); \
1078 CHECK_STRING((ERR)->msg, MSG); \
1079 CHECK_STRING((ERR)->path, PATH); \
1080 }
1081
1082/**`
1083 * @brief Internal macro to check the last libyang's context error.
1084 */
1085#define _CHECK_LOG_CTX1(MSG, PATH) \
1086 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG, PATH)
1087
1088/**
1089 * @brief Internal macro to check the last two libyang's context error.
1090 */
1091#define _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2) \
1092 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG1, PATH1); \
1093 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev, MSG2, PATH2)
1094
1095/**
1096 * @brief Internal macro to check the last three libyang's context error.
1097 */
1098#define _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3) \
1099 _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2); \
1100 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev, MSG3, PATH3)
1101
1102/**
Radek Krejcife6ec262021-01-20 10:28:28 +01001103 * @brief Internal macro to check the last three libyang's context error.
1104 */
1105#define _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4) \
1106 _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3); \
1107 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev, MSG4, PATH4)
1108
1109/**
1110 * @brief Internal macro to check the last three libyang's context error.
1111 */
1112#define _CHECK_LOG_CTX5(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4, MSG5, PATH5) \
1113 _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4); \
1114 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev->prev, MSG5, PATH5)
1115
1116/**
Radek Iša56ca9e42020-09-08 18:42:00 +02001117 * @brief Internal helper macro to select _CHECK_LOG_CTX* macro according to the provided parameters.
1118 */
Radek Krejcife6ec262021-01-20 10:28:28 +01001119#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 +02001120
1121/**
1122 * @brief Check expected error(s) in libyang context.
1123 *
1124 * Macro has variadic parameters expected to be provided in pairs of error message and error path starting
1125 * from the latest error. Current limit is checking at most 3 last errors. After checking, macro cleans up
1126 * all the errors from the libyang context.
1127 *
1128 * @param[in] MSG Expected error message.
1129 * @param[in] PATH Expected error path.
1130 */
1131#define CHECK_LOG_CTX(...) \
Radek Krejcife6ec262021-01-20 10:28:28 +01001132 _GET_CHECK_LOG_MACRO(__VA_ARGS__, _CHECK_LOG_CTX5, _INVAL, _CHECK_LOG_CTX4, _INVAL, \
1133 _CHECK_LOG_CTX3, _INVAL, _CHECK_LOG_CTX2, _INVAL, _CHECK_LOG_CTX1)(__VA_ARGS__); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001134 ly_err_clean(_UC->ctx, NULL)
1135
1136/**
1137 * @brief Clean up the logging callback's storage.
1138 */
1139#define UTEST_LOG_CLEAN \
1140 free(_UC->err_msg); \
1141 free(_UC->err_path); \
1142 _UC->err_msg = NULL; \
1143 _UC->err_path = NULL;
1144
1145/**
1146 * @brief Check expected error directly logged via logging callback.
1147 * Useful mainly for messages logged by functions without access to libyang context.
1148 * @param[in] MSG Expected error message.
1149 * @param[in] PATH Expected error path.
1150 */
1151#define CHECK_LOG(MSG, PATH) \
1152 CHECK_STRING(_UC->err_msg, MSG); \
1153 CHECK_STRING(_UC->err_path, PATH); \
1154 UTEST_LOG_CLEAN
1155
1156#ifdef _UTEST_MAIN_
1157/*
1158 * Functions inlined into each C source file including this header with _UTEST_MAIN_ defined
1159 */
1160
1161/**
1162 * @brief Global variable holding the tests context to simplify access to it.
1163 */
1164struct utest_context *current_utest_context;
1165
1166/* set to 0 to printing error messages to stderr instead of checking them in code */
1167#define ENABLE_LOGGER_CHECKING 1
1168
1169/**
1170 * @brief Logging callback for libyang.
1171 */
1172static void
1173_utest_logger(LY_LOG_LEVEL level, const char *msg, const char *path)
1174{
1175 (void) level; /* unused */
1176
1177 if (ENABLE_LOGGER_CHECKING == 0) {
Radek Išaa9ff2b82021-01-13 21:44:13 +01001178 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 +02001179 } else {
1180 free(current_utest_context->err_msg);
1181 current_utest_context->err_msg = msg ? strdup(msg) : NULL;
1182 free(current_utest_context->err_path);
1183 current_utest_context->err_path = path ? strdup(path) : NULL;
1184 }
1185}
1186
1187/**
1188 * @brief Generic utest's setup
1189 */
1190static int
1191utest_setup(void **state)
1192{
1193 /* setup the logger */
1194 ly_set_log_clb(_utest_logger, 1);
1195 ly_log_options(LY_LOLOG | LY_LOSTORE);
1196
1197 current_utest_context = calloc(1, sizeof *current_utest_context);
1198 assert_non_null(current_utest_context);
1199 *state = current_utest_context;
1200
1201 /* libyang context */
1202 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &current_utest_context->ctx));
1203
1204 return 0;
1205}
1206
1207/**
1208 * @brief macro to include generic utest's setup into the test-specific setup.
1209 *
1210 * Place at the beginning of the test-specific setup
1211 */
1212#define UTEST_SETUP \
1213 assert_int_equal(0, utest_setup(state))
1214
1215/**
1216 * @brief Generic utest's teardown
1217 */
1218static int
1219utest_teardown(void **state)
1220{
1221 *state = NULL;
1222
1223 /* libyang context */
1224 ly_ctx_destroy(current_utest_context->ctx, NULL);
1225
1226 /* utest context */
1227 ly_in_free(current_utest_context->in, 0);
1228 free(current_utest_context->err_msg);
1229 free(current_utest_context->err_path);
1230 free(current_utest_context);
1231 current_utest_context = NULL;
1232
1233 return 0;
1234}
1235
1236/**
1237 * @brief macro to include generic utest's teardown into the test-specific teardown.
1238 *
1239 * Place at the end of the test-specific teardown
1240 */
1241#define UTEST_TEARDOWN \
1242 assert_int_equal(0, utest_teardown(state))
1243
1244/**
1245 * @brief Internal macro for utest setup with test-specific setup and teardown
1246 */
1247#define _UTEST_SETUP_TEARDOWN(FUNC, SETUP, TEARDOWN) \
1248 cmocka_unit_test_setup_teardown(FUNC, SETUP, TEARDOWN)
1249
1250/**
1251 * @brief Internal macro for utest setup with test-specific setup and generic teardown
1252 */
1253#define _UTEST_SETUP(FUNC, SETUP) \
1254 cmocka_unit_test_setup_teardown(FUNC, SETUP, utest_teardown)
1255
1256/**
1257 * @brief Internal macro for utest setup with generic setup and teardown
1258 */
1259#define _UTEST(FUNC) \
1260 cmocka_unit_test_setup_teardown(FUNC, utest_setup, utest_teardown)
1261
1262/**
1263 * @brief Internal helper macro to select _UTEST* macro according to the provided parameters.
1264 */
1265#define _GET_UTEST_MACRO(_1, _2, _3, NAME, ...) NAME
1266
1267/**
1268 * @brief Macro to specify test function using utest environment. Macro has variadic parameters
1269 * to provide test-specific setup/teardown functions:
1270 *
1271 * UTEST(test_func) - only implicit setup and teardown functions are used
1272 * UTEST(test_func, setup) - implicit teardown but own setup
1273 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
1274 */
1275#define UTEST(...) \
1276 _GET_UTEST_MACRO(__VA_ARGS__, _UTEST_SETUP_TEARDOWN, _UTEST_SETUP, _UTEST)(__VA_ARGS__)
1277
1278#else /* _UTEST_MAIN_ */
1279
1280extern struct utest_context *current_utest_context;
1281
1282#endif /* _UTEST_MAIN_ */
1283
1284#endif /* _UTESTS_H_ */