blob: 856ba1043cd4437b678f3aece9106cf877a0ce09 [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 *
7 * Copyright (c) 2020 CESNET, z.s.p.o.
8 *
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"
30#include "tests/config.h"
31
32/**
33 * TESTS OVERVIEW
34 *
35 * To include utest's environment, just include "utests.h" in the test's source
36 * code. In case it is the main source code for a cmocka test group (there is a
37 * main() function), define _UTEST_MAIN_ before including this header.
38 *
39 * TESTS VARIABLES
40 *
41 * Checking macros use internal storage to store various variables necessary
42 * during the checking. It is possible to access these variables using the
43 * following macros:
44 *
45 * UTEST_LYCTX - libyang context
46 * UTEST_IN - input handler
47 * UTEST_OUT - output handler
48 *
49 * All these variables are cleaned with test's teardown.
50 *
51 * TESTS SETUP
52 *
53 * CMocka's CMUnitTest list definition macros (cmoka_unit_test*()) are replaced
54 * by UTEST macro with possibility to specify own setup and teardown functions:
55 *
56 * UTEST(test_func) - only implicit setup and teardown functions are used
57 * UTEST(test_func, setup) - implicit teardown but own setup
58 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
59 *
60 * Note that the tests environment always provide (and need) internal setup and
61 * teardown functions. In case the test-specific setup or teardown are used, they
62 * are supposed to include UTEST_SETUP at the setup beginning and UTEST_TEARDOWN
63 * at the teardown end.
64 *
65 * Libyang context is part of the prepared environment. To add a schema into the
66 * context (despite it is in the test-specific setup or in test function itself),
67 * use UTEST_ADD_MODULE macro.
68 *
69 * LOGGING
70 *
71 * There are 2 macros to check content of the log from the previously called
72 * libyang function. CHECK_LOG macro test only the last error message and path
73 * stored directly via logging callback. CHECK_LOG_CTX gets error message and
74 * path from the libyang context (in case the function does not store the error
75 * information into the libyang context, the message cannot be checked this way).
76 * libyang is set to store multiple error information, so multiple couples of
77 * error message and path can be provided to be checked (the first couple
78 * corresponds to the latest error). The macro also cleanups the errors list, so
79 * it is fine to check that there is no error after succeeding successful
80 * function call.
81 */
82
83/**
84 * @brief Test's context to provide common storage for various variables.
85 */
86struct utest_context {
87 struct ly_ctx *ctx; /**< libyang context */
88
89 char *err_msg; /**< Directly logged error message */
90 char *err_path; /**< Directly logged error path */
91
92 struct ly_in *in; /**< Input handler */
93 struct ly_out *out; /**< Outpu handler */
94};
95
96/**
97 * @brief Shortcut to access utest_context.
98 */
99#define _UC ((struct utest_context *)*state)
100
101/**
102 * @brief libyang context provider.
103 */
104#define UTEST_LYCTX (_UC->ctx)
105
106/**
107 * @brief Context's input handler provider
108 */
109#define UTEST_IN (_UC->in)
110
111/**
112 * @brief Context's input handler provider
113 */
114#define UTEST_OUT (_UC->out)
115
116/**
117 * @brief Parse (and validate) data from the input handler as a YANG data tree.
118 *
119 * @param[in] INPUT The input data in the specified @p format to parse (and validate)
120 * @param[in] INPUT_FORMAT Format of the input data to be parsed. Can be 0 to try to detect format from the input handler.
121 * @param[in] PARSE_OPTIONS Options for parser, see @ref dataparseroptions.
122 * @param[in] VALIDATE_OPTIONS Options for the validation phase, see @ref datavalidationoptions.
123 * @param[in] OUT_STATUS expected return status
124 * @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.
125 * The returned data are expected to be freed using LYD_TREE_DESTROY().
126 */
127#define CHECK_PARSE_LYD_PARAM(INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, OUT_STATUS, OUT_NODE) \
128 assert_int_equal(OUT_STATUS, lyd_parse_data_mem(_UC->ctx, INPUT, INPUT_FORMAT, PARSE_OPTIONS, VALIDATE_OPTIONS, &OUT_NODE)); \
129 if (OUT_STATUS == LY_SUCCESS) { \
130 assert_non_null(OUT_NODE); \
131 } else { \
132 assert_null(OUT_NODE); \
133 }
134
135/**
136 * @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.
137 * @param[in] NODE pointer to lyd_node
138 * @param[in] TEXT expected output string in json or xml format.
139 * @param[in] FORMAT format of input text. LYD_JSON, LYD_XML
140 * @param[in] PARAM options [Data printer flags](@ref dataprinterflags).
141 */
142#define CHECK_LYD_STRING_PARAM(NODE, TEXT, FORMAT, PARAM) \
143 { \
144 char *test; \
145 lyd_print_mem(&test, NODE, FORMAT, PARAM); \
146 assert_string_equal(test, TEXT); \
147 free(test); \
148 }
149
150/**
151 * @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;
152 * @param[in] NODE_1 pointer to lyd_node
153 * @param[in] NODE_2 pointer to lyd_node
154 */
155#define CHECK_LYD(NODE_1, NODE_2) \
156 { \
157 char *test_1; \
158 char *test_2; \
159 lyd_print_mem(&test_1, NODE_1, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
160 lyd_print_mem(&test_2, NODE_2, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK); \
161 assert_string_equal(test_1, test_2); \
162 free(test_1); \
163 free(test_2); \
164 }
165
166/*
167 * SUPPORT MACROS
168 */
169
170/**
171 * @brief Internal macro witch assert that two given string are equal or are both null.
172 *
173 * @param[in] STRING string to check
174 * @param[in] TEXT string to compare
175 */
176#define CHECK_STRING(STRING, TEXT)\
177 if (TEXT == NULL) { \
178 assert_null(STRING); \
179 } else { \
180 assert_non_null(STRING); \
181 assert_string_equal(STRING, TEXT); \
182 }
183
184/**
185 * @brief Internal macro witch assert that pointer is null when flag is 0.
186 *
187 * @param[in] POINTER pointer to check
188 * @param[in] FLAG 0 -> pointer is NULL, 1 -> pointer is not null
189 */
190#define CHECK_POINTER(POINTER, FLAG) \
191 assert_true(FLAG == 0 ? POINTER == NULL : POINTER != NULL)
192
193/**
194 * @brief Internal macro check size of [sized array](@ref sizedarrays)'s
195 *
196 * @param[in] ARRAY pointer to [sized array](@ref sizedarrays)
197 * @param[in] SIZE expected [sized array](@ref sizedarrays) size of array
198 */
199#define CHECK_ARRAY(ARRAY, SIZE) \
200 assert_true((SIZE == 0) ? \
201 (ARRAY == NULL) : \
202 (ARRAY != NULL && SIZE == LY_ARRAY_COUNT(ARRAY)));
203
204/*
205 * LIBYANG NODE CHECKING
206 */
207
208/**
209 * @brief assert that lysp_action_inout structure members are correct
210 * @param[in] NODE pointer to lysp_action_inout variable
211 * @param[in] DATA 0 -> check if pointer to data is NULL, 1 -> check if pointer to data is not null
212 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list
213 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
214 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of must restriction list
215 * @param[in] NODETYPE node type. LYS_INPUT or LYS_OUTPUT
216 * @param[in] PARENT 0 -> check if node is root, 1 -> check if node is not root
217 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of typedefs list
218 */
219#define CHECK_LYSP_ACTION_INOUT(NODE, DATA, EXTS, GROUPINGS, MUSTS, NODETYPE, PARENT, TYPEDEFS) \
220 assert_non_null(NODE); \
221 CHECK_POINTER((NODE)->data, DATA); \
222 CHECK_ARRAY((NODE)->exts, EXTS); \
223 CHECK_ARRAY((NODE)->groupings, GROUPINGS); \
224 CHECK_ARRAY((NODE)->musts, MUSTS); \
225 assert_int_equal((NODE)->nodetype, NODETYPE); \
226 CHECK_POINTER((NODE)->parent, PARENT); \
227 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS);
228
229/**
230 * @brief assert that lysp_action structure members are correct
231 * @param[in] NODE pointer to lysp_action variable
232 * @param[in] DSC expected description
233 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extension list
234 * @param[in] FLAGS expected [schema node flags](@ref snodeflags)
235 * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list
236 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of if-feature expressions list
237 * @param[in] INPUT_* ::LYSP_ACTION_INOUT_CHECK
238 * @param[in] NAME expected name
239 * @param[in] NODETYPE node type. LYS_RPC or LYS_ACTION
240 * @param[in] OUTPUT_* ::LYSP_ACTION_INOUT_CHECK
241 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
242 * @param[in] REF expected reference
243 * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of list of typedefs
244 */
245#define CHECK_LYSP_ACTION(NODE, DSC, EXTS, FLAGS, GROUPINGS, IFFEATURES, \
246 INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, INPUT_MUSTS, \
247 INPUT_PARENT, INPUT_TYPEDEFS, \
248 NAME, NODETYPE, \
249 OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, OUTPUT_MUSTS, \
250 OUTPUT_PARENT, OUTPUT_TYPEDEFS, \
251 PARENT, REF, TYPEDEFS) \
252 assert_non_null(NODE); \
253 CHECK_STRING((NODE)->dsc, DSC); \
254 CHECK_ARRAY((NODE)->exts, EXTS); \
255 assert_int_equal((NODE)->flags, FLAGS); \
256 CHECK_ARRAY((NODE)->groupings, GROUPINGS); \
257 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
258 CHECK_LYSP_ACTION_INOUT(&((NODE)->input), INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, \
259 INPUT_MUSTS, LYS_INPUT, INPUT_PARENT, INPUT_TYPEDEFS); \
260 assert_string_equal((NODE)->name, NAME); \
261 assert_int_equal((NODE)->nodetype, NODETYPE); \
262 CHECK_LYSP_ACTION_INOUT(&((NODE)->output), OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, \
263 OUTPUT_MUSTS, LYS_OUTPUT, OUTPUT_PARENT, OUTPUT_TYPEDEFS); \
264 CHECK_POINTER((NODE)->parent, PARENT); \
265 CHECK_STRING((NODE)->ref, REF); \
266 CHECK_ARRAY((NODE)->typedefs, TYPEDEFS) \
267
268/**
269 * @brief assert that lysp_when structure members are correct
270 * @param[in] NODE pointer to lysp_when variable
271 * @param[in] COND expected string specifid condition
272 * @param[in] DSC expected string description statement
273 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
274 * @param[in] REF expected string reference
275 */
276#define CHECK_LYSP_WHEN(NODE, COND, DSC, EXTS, REF) \
277 assert_non_null(NODE); \
278 assert_string_equal((NODE)->cond, COND); \
279 CHECK_STRING((NODE)->dsc, DSC); \
280 CHECK_ARRAY((NODE)->exts, EXTS); \
281 if (REF == NULL) { \
282 assert_null((NODE)->ref); \
283 } else { \
284 assert_non_null((NODE)->ref); \
285 assert_string_equal((NODE)->ref, REF); \
286 }
287
288/**
289 * @brief assert that lysp_restr structure members are correct
290 * @param[in] NODE pointer to lysp_restr variable
291 * @param[in] ARG_STR expected string. The restriction expression/value
292 * @param[in] DSC expected descrition
293 * @param[in] EAPPTAG expected string reprezenting error-app-tag value
294 * @param[in] EMSG expected string reprezenting error message
295 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array
296 * @param[in] REF expected reference
297 */
298
299#define CHECK_LYSP_RESTR(NODE, ARG_STR, DSC, EAPPTAG, EMSG, EXTS, REF) \
300 assert_non_null(NODE); \
301 assert_non_null((NODE)->arg.mod); \
302 assert_string_equal((NODE)->arg.str, ARG_STR); \
303 CHECK_STRING((NODE)->dsc, DSC); \
304 CHECK_STRING((NODE)->eapptag, EAPPTAG); \
305 CHECK_STRING((NODE)->emsg, EMSG); \
306 CHECK_ARRAY((NODE)->exts, EXTS); \
307 CHECK_STRING((NODE)->ref, REF);
308
309/**
310 * @brief assert that lysp_import structure members are correct
311 * @param[in] NODE pointer to lysp_import variable
312 * @param[in] DSC expected description or NULL
313 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extensions
314 * @param[in] NAME expected name of imported module
315 * @param[in] PREFIX expected prefix for the data from the imported schema
316 * @param[in] REF expected reference
317 * @prame[in] REV expected reprezenting date in format "11-10-2020"
318 */
319#define CHECK_LYSP_IMPORT(NODE, DSC, EXTS, NAME, PREFIX, REF, REV) \
320 assert_non_null(NODE); \
321 CHECK_STRING((NODE)->dsc, DSC); \
322 CHECK_ARRAY((NODE)->exts, EXTS); \
323 /*assert_non_null((NODE)->module); // ?? it is mandatory but in some test it doesnt work */ \
324 assert_string_equal((NODE)->name, NAME); \
325 assert_string_equal((NODE)->prefix, PREFIX); \
326 CHECK_STRING((NODE)->ref, REF); \
327 CHECK_STRING((NODE)->rev, REV); \
328
329/**
330 * @brief assert that lysp_ext structure members are correct
331 * @param[in] NODE pointer to lysp_ext_instance variable
332 * @param[in] ARGUMENT expected argument name
333 * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists
334 * @param[in] DSC expected string reprezent description
335 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances
336 * @param[in] FLAGS expected LYS_STATUS_* and LYS_YINELEM_* values (@ref snodeflags)
337 * @param[in] NAME expected name
338 * @param[in] REF expected ref
339 */
340#define CHECK_LYSP_EXT(NODE, ARGUMENT, COMPILED, DSC, EXTS, FLAGS, NAME, REF) \
341 assert_non_null(NODE); \
342 CHECK_STRING((NODE)->argument, ARGUMENT); \
343 CHECK_POINTER((NODE)->compiled, COMPILED); \
344 CHECK_STRING((NODE)->dsc, DSC); \
345 CHECK_ARRAY((NODE)->exts, EXTS); \
346 assert_int_equal((NODE)->flags, FLAGS); \
347 assert_string_equal((NODE)->name, NAME); \
348 CHECK_STRING((NODE)->ref, REF);
349
350/**
351 * @brief assert that lysp_ext_instance structure members are correct
352 * @param[in] NODE pointer to lysp_ext_instance variable
353 * @param[in] ARGUMENT expected optional value of the extension's argument
354 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
355 * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists
356 * @param[in] INSUBSTMS expected value identifying placement of the extension instance
357 * @param[in] INSUBSTMS_INDEX expected indentifi index
358 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
359 * @param[in] PARENT_TYPE expected parent type ::LYEXT_PARENT. not relevat if PARENT == 0
360 * @param[in] YIN expected flag for YIN source format, can be set to LYS_YIN
361 */
362#define CHECK_LYSP_EXT_INSTANCE(NODE, ARGUMENT, CHILD, COMPILED, INSUBSTMT, INSUBSTMT_INDEX, NAME, HAS_PARENT, PARENT_TYPE, YIN) \
363 assert_non_null(NODE); \
364 CHECK_STRING((NODE)->argument, ARGUMENT); \
365 CHECK_POINTER((NODE)->child, CHILD); \
366 CHECK_POINTER((NODE)->compiled, COMPILED); \
367 /*assert_int_equal((NODE)->flags, LYS_INTERNAL);*/ \
368 assert_int_equal((NODE)->insubstmt, INSUBSTMT); \
369 assert_int_equal((NODE)->insubstmt_index, INSUBSTMT_INDEX); \
370 assert_string_equal((NODE)->name, NAME); \
371 if (HAS_PARENT) { \
372 assert_non_null((NODE)->parent); \
373 assert_int_equal((NODE)->parent_type, PARENT_TYPE); \
374 } else { \
375 assert_null((NODE)->parent); \
376 } \
377 assert_int_equal((NODE)->yin, YIN);
378
379/**
380 * @brief assert that lysp_stmt structure members are correct
381 * @param[in] NODE pointer to lysp_stmt variable
382 * @param[in] ARG expected statemet argumet
383 * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children
384 * @param[in] FLAGS expected statement flags, can be set to LYS_YIN_ATTR
385 * @param[in] KW expected numeric respresentation of the stmt value
386 * @param[in] NEXT 0 -> pointer is NULL, 1 -> pointer is not null
387 * @param[in] STMS expected identifier of the statement
388 */
389#define CHECK_LYSP_STMT(NODE, ARG, CHILD, FLAGS, KW, NEXT, STMT) \
390 assert_non_null(NODE); \
391 CHECK_STRING((NODE)->arg, ARG); \
392 CHECK_POINTER((NODE)->child, CHILD); \
393 assert_int_equal((NODE)->flags, FLAGS); \
394 assert_int_equal((NODE)->kw, KW); \
395 CHECK_POINTER((NODE)->next, NEXT); \
396 assert_string_equal((NODE)->stmt, STMT); \
397
398/**
399 * @brief assert that lysp_type_enum structure members are correct
400 * @param[in] NODE pointer to lysp_type_enum variable
401 * @param[in] DSC expected description
402 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
403 * @param[in] FLAGS only LYS_STATUS_ and LYS_SET_VALUE values are allowed
404 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
405 * @param[in] NAME expected name
406 * @param[in] REF expected reference statement
407 * @param[in] VALUE expected enum's value or bit's position
408 */
409#define CHECK_LYSP_TYPE_ENUM(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, REF, VALUE) \
410 assert_non_null(NODE); \
411 CHECK_STRING((NODE)->dsc, DSC); \
412 CHECK_ARRAY((NODE)->exts, EXTS); \
413 assert_int_equal((NODE)->flags, FLAGS); \
414 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
415 CHECK_STRING((NODE)->name, NAME); \
416 CHECK_STRING((NODE)->ref, REF); \
417 assert_int_equal(VALUE, (NODE)->value);
418
419/**
420 * @brief assert that lysp_node structure members are correct
421 * @param[in] NODE pointer to lysp_node variable
422 * @param[in] DSC expected description statement
423 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
424 * @param[in] FLAGS [schema node flags](@ref snodeflags)
425 * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances
426 * @param[in] NAME expected name
427 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
428 * @param[in] NODETYPE node type LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, LYS_LEAFLIST,
429 * LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, LYS_ACTION, LYS_NOTIF,
430 * LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, LYS_AUGMENT
431 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
432 * @param[in] REF expected reference statement
433 * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null
434 */
435#define CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, NODETYPE, PARENT, REF, WHEN) \
436 assert_non_null(NODE); \
437 CHECK_STRING((NODE)->dsc, DSC); \
438 CHECK_ARRAY((NODE)->exts, EXTS); \
439 assert_int_equal((NODE)->flags, FLAGS); \
440 CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \
441 CHECK_STRING((NODE)->name, NAME); \
442 CHECK_POINTER((NODE)->next, NEXT); \
443 assert_int_equal((NODE)->nodetype, NODETYPE); \
444 CHECK_POINTER((NODE)->parent, PARENT); \
445 CHECK_STRING((NODE)->ref, REF); \
446 CHECK_POINTER((NODE)->when, WHEN);
447
448/**
449 * @brief assert that lysc_notif structure members are correct
450 * @param[in] NODE pointer to lysp_notif variable
451 * @param[in] DATA 0 pointer is null, 1 pointer is not null
452 * @param[in] DSC expected description
453 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
454 * @param[in] FLAGS [schema node flags](@ref snodeflags)
455 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
456 * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction
457 * @param[in] NAME expected name
458 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
459 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
460 * @param[in] REF expected reference
461 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
462 */
463#define CHECK_LYSC_NOTIF(NODE, DATA, DSC, EXTS, FLAGS, MODULE, MUSTS, NAME, PARENT, PRIV, REF, WHEN) \
464 assert_non_null(NODE); \
465 CHECK_POINTER((NODE)->data, DATA); \
466 CHECK_STRING((NODE)->dsc, DSC); \
467 CHECK_ARRAY((NODE)->exts, EXTS); \
468 assert_int_equal((NODE)->flags, FLAGS); \
469 CHECK_POINTER((NODE)->module, MODULE); \
470 CHECK_ARRAY((NODE)->musts, MUSTS); \
471 assert_string_equal((NODE)->name, NAME); \
472 assert_int_equal((NODE)->nodetype, LYS_NOTIF); \
473 CHECK_POINTER((NODE)->parent, PARENT); \
474 CHECK_POINTER((NODE)->priv, PRIV); \
475 CHECK_STRING((NODE)->ref, REF); \
476 CHECK_ARRAY((NODE)->when, WHEN);
477
478/**
479 * @brief assert that lysc_action_inout structure members are correct
480 * @param[in] NODE pointer to lysp_notif variable
481 * @param[in] DATA 0 pointer is null, 1 pointer is not null
482 * @param[in] MUST expected [sized array](@ref sizedarrays) size of list of must restrictions
483 * @param[in] NODETYPE LYS_INPUT or LYS_OUTPUT
484 */
485#define CHECK_LYSC_ACTION_INOUT(NODE, DATA, MUST, NODETYPE) \
486 assert_non_null(NODE); \
487 CHECK_POINTER((NODE)->data, DATA); \
488 CHECK_ARRAY((NODE)->musts, MUST); \
489 assert_int_equal((NODE)->nodetype, NODETYPE);
490
491/**
492 * @brief assert that lysc_action structure members are correct
493 * @param[in] NODE pointer to lysp_action variable
494 * @param[in] DSC string description statement
495 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
496 * @param[in] FLAGS [schema node flags](@ref snodeflags)
497 * @param[in] INPUT_DATA 0 pointer is null, 1 pointer is not null
498 * @param[in] INPUT_MUST expected [sized array](@ref sizedarrays) size of input list of must restrictions
499 * @param[in] INPUT_EXTS expected [sized array](@ref sizedarrays) size of the input extension instances of input
500 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
501 * @param[in] NAME expected name
502 * @param[in] NODETYPE LYS_RPC, LYS_ACTION
503 * @param[in] OUTPUT_DATA 0 pointer is null, 1 pointer is not null
504 * @param[in] OUTPUT_MUST expected [sized array](@ref sizedarrays) size of output list of must restrictions
505 * @param[in] OUTPUT_EXTS expected [sized array](@ref sizedarrays) size of the output extension instances of input
506 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
507 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
508 * @param[in] REF expected reference
509 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
510 */
511#define CHECK_LYSC_ACTION(NODE, DSC, EXTS, FLAGS, INPUT_DATA, INPUT_MUST, INPUT_EXTS, MODULE, NAME, NODETYPE, \
512 OUTPUT_DATA, OUTPUT_MUST, OUTPUT_EXTS, PARENT, PRIV, REF, WHEN) \
513 assert_non_null(NODE); \
514 CHECK_STRING((NODE)->dsc, DSC); \
515 CHECK_ARRAY((NODE)->exts, EXTS); \
516 assert_int_equal((NODE)->flags, FLAGS); \
517 CHECK_LYSC_ACTION_INOUT(&(NODE)->input, INPUT_DATA, INPUT_MUST, LYS_INPUT); \
518 CHECK_ARRAY((NODE)->input_exts, INPUT_EXTS); \
519 CHECK_POINTER((NODE)->module, MODULE); \
520 assert_string_equal((NODE)->name, NAME); \
521 assert_int_equal((NODE)->nodetype, NODETYPE); \
522 CHECK_LYSC_ACTION_INOUT(&(NODE)->output, OUTPUT_DATA, OUTPUT_MUST, LYS_OUTPUT); \
523 CHECK_ARRAY((NODE)->output_exts, OUTPUT_EXTS); \
524 CHECK_POINTER((NODE)->parent, PARENT); \
525 CHECK_POINTER((NODE)->priv, PRIV); \
526 CHECK_STRING((NODE)->ref, REF); \
527 CHECK_ARRAY((NODE)->when, WHEN);
528
529/**
530 * @brief assert that lysc_node structure members are correct
531 * @param[in] NODE pointer to lysc_node variable
532 * @param[in] DSC expected description
533 * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances
534 * @param[in] FLAGS [schema node flags](@ref snodeflags)
535 * @param[in] MODULE 0 pointer is null, 1 pointer is not null
536 * @param[in] NAME expected name
537 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
538 * @param[in] NODETYPE type of the node LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF,
539 * LYS_LEAFLIST, LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC,
540 * LYS_ACTION, LYS_NOTIF, LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING,
541 * LYS_AUGMENT
542 * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root
543 * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null
544 * @param[in] REF expected reference
545 * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements
546 */
547#define CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, NODETYPE, PARENT, PRIV, REF, WHEN) \
548 assert_non_null(NODE); \
549 CHECK_STRING((NODE)->dsc, DSC); \
550 CHECK_ARRAY((NODE)->exts, EXTS); \
551 assert_int_equal((NODE)->flags, FLAGS); \
552 CHECK_POINTER((NODE)->module, MODULE); \
553 assert_string_equal((NODE)->name, NAME); \
554 CHECK_POINTER((NODE)->next, NEXT); \
555 assert_int_equal((NODE)->nodetype, NODETYPE); \
556 CHECK_POINTER((NODE)->parent, PARENT); \
557 assert_non_null((NODE)->prev); \
558 CHECK_POINTER((NODE)->priv, PRIV); \
559 CHECK_STRING((NODE)->ref, REF); \
560 CHECK_ARRAY((NODE)->when, WHEN);
561
562/**
563 * @brief assert that lyd_meta structure members are correct
564 * @param[in] NODE pointer to lyd_meta variable
565 * @param[in] ANNOTATION 0 pointer is null, 1 pointer is not null
566 * @param[in] NAME expected name
567 * @param[in] NEXT 0 pointer is null, 1 pointer is not null
568 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
569 * part of text reprezenting LY_DATA_TYPE.
570 * @param[in] ... ::CHECK_LYD_VALUE
571 */
572#define CHECK_LYD_META(NODE, ANNOTATION, NAME, NEXT, PARENT, TYPE_VAL, ...) \
573 assert_non_null(NODE); \
574 CHECK_POINTER((NODE)->annotation, ANNOTATION); \
575 assert_string_equal((NODE)->name, NAME); \
576 CHECK_POINTER((NODE)->next, NEXT); \
577 CHECK_POINTER((NODE)->parent, PARENT); \
578 CHECK_LYD_VALUE((NODE)->value, TYPE_VAL, ##__VA_ARGS__);
579
580/**
581 * @brief assert that lyd_node_term structure members are correct
582 * @param[in] NODE pointer to lyd_node_term variable
583 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
584 * @param[in] META 0 -> meta is not prezent, 1 -> meta is prezent
585 * @param[in] NEXT 0 -> next node is not prezent, 1 -> next node is prezent
586 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
587 * part of text reprezenting LY_DATA_TYPE.
588 * @param[in] ... ::CHECK_LYD_VALUE
589 */
590#define CHECK_LYD_NODE_TERM(NODE, FLAGS, META, NEXT, PARENT, SCHEMA, VALUE_TYPE, ...) \
591 assert_non_null(NODE); \
592 assert_int_equal((NODE)->flags, FLAGS); \
593 CHECK_POINTER((NODE)->meta, META); \
594 CHECK_POINTER((NODE)->next, NEXT); \
595 CHECK_POINTER((NODE)->parent, PARENT); \
596 assert_non_null((NODE)->prev); \
597 CHECK_POINTER((NODE)->schema, SCHEMA); \
598 CHECK_LYD_VALUE((NODE)->value, VALUE_TYPE, ##__VA_ARGS__);
599
600/**
601 * @brief assert that lyd_node_any structure members are correct
602 * @param[in] NODE pointer to lyd_node_term variable
603 * @param[in] FLAGS expected [data node flags](@ref dnodeflags)
604 * @param[in] META 0 meta isnt present , 1 meta is present
605 * @param[in] PARENT 0 it is root node , 1 node have parent
606 * @param[in] VALUE_TYPE value type ::lyd_node_any.value
607 */
608#define CHECK_LYD_NODE_ANY(NODE, FLAGS, META, PARENT, VALUE_TYPE) \
609 assert_non_null(NODE); \
610 assert_int_equal((NODE)->flags, FLAGS); \
611 CHECK_POINTER((NODE)->meta, META); \
612 CHECK_POINTER((NODE)->meta, PARENT); \
613 assert_non_null((NODE)->prev); \
614 assert_non_null((NODE)->schema); \
615 assert_int_equal((NODE)->value_type, VALUE_TYPE);
616
617/**
618 * @brief assert that lyd_node_opaq structure members are correct
619 * @param[in] NODE pointer to lyd_node_opaq variable
620 * @param[in] ATTR 0 if pointer is null ,1 if pointer is not null
621 * @param[in] CHILD 0 if pointer is null ,1 if pointer is not null
622 * @param[in] FORMAT LY_PREF_XML or LY_PREF_JSON
623 * @param[in] VAL_PREFS 0 if pointer is null ,1 if pointer is not null
624 * @param[in] NAME expected name
625 * @param[in] value expected orignal value
626 */
627#define CHECK_LYD_NODE_OPAQ(NODE, ATTR, CHILD, FORMAT, NAME, NEXT, PARENT, PREFIX, VAL_PREFS, VALUE) \
628 assert_non_null(NODE); \
629 CHECK_POINTER((NODE)->attr, ATTR); \
630 CHECK_POINTER((NODE)->child, CHILD); \
631 assert_ptr_equal((NODE)->ctx, UTEST_LYCTX); \
632 assert_int_equal((NODE)->flags, 0); \
633 assert_true((NODE)->format == FORMAT); \
634 assert_int_equal((NODE)->hash, 0); \
635 assert_string_equal((NODE)->name.name, NAME); \
636 assert_non_null((NODE)->prev); \
637 assert_null((NODE)->schema); \
638 CHECK_POINTER((NODE)->val_prefix_data, VAL_PREFS); \
639 assert_string_equal((NODE)->value, VALUE);
640
641/**
642 * @brief assert that lyd_value structure members are correct
643 * @param[in] NODE lyd_value
644 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
645 * part of text reprezenting LY_DATA_TYPE.
646 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
647 * by type of value. These parameters are passed to macro
648 * CHECK_LYD_VALUE_ ## TYPE_VAL.
649 */
650#define CHECK_LYD_VALUE(NODE, TYPE_VAL, ...) \
651 CHECK_LYD_VALUE_ ## TYPE_VAL (NODE, ##__VA_ARGS__);
652
653/*
654 * LYD VALUES CHECKING SPECIALIZATION
655 */
656
657/**
658 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type EMPTY
659 * Example CHECK_LYD_VALUE(node->value, EMPTY, "");
660 * @param[in] NODE lyd_value variable
661 * @param[in] CANNONICAL_VAL expected cannonical value
662 */
663#define CHECK_LYD_VALUE_EMPTY(NODE, CANNONICAL_VAL) \
664 assert_non_null((NODE).canonical); \
665 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
666 assert_non_null((NODE).realtype); \
667 assert_int_equal((NODE).realtype->basetype, LY_TYPE_EMPTY);
668
669/**
670 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UNION
671 * Example CHECK_LYD_VALUE(node->value, UNION, "12", INT8, "12", 12);
672 * @warning type of subvalue cannot be UNION. Example of calling
673 * @param[in] NODE lyd_value variable
674 * @param[in] CANNONICAL_VAL expected cannonical value
675 * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT
676 * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified
677 * by type of value. These parameters are passed to macro
678 * CHECK_LYD_VALUE_ ## TYPE_VAL.
679 */
680#define CHECK_LYD_VALUE_UNION(NODE, CANNONICAL_VAL, TYPE_VAL, ...) \
681 assert_non_null((NODE).canonical); \
682 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
683 assert_non_null((NODE).realtype); \
684 assert_int_equal(LY_TYPE_UNION, (NODE).realtype->basetype); \
685 assert_non_null((NODE).subvalue); \
686 assert_non_null((NODE).subvalue->prefix_data); \
687 CHECK_LYD_VALUE_ ## TYPE_VAL ((NODE).subvalue->value, ## __VA_ARGS__)
688
689/**
690 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BITS
691 * Example arr[] = {"a", "b"}; CHECK_LYD_VALUE(node->value, BITS, "a b", arr);
692 * @param[in] NODE lyd_value variable
693 * @param[in] CANNONICAL_VAL expected cannonical value
694 * @param[in] VALUE expected array of bits names
695 */
696#define CHECK_LYD_VALUE_BITS(NODE, CANNONICAL_VAL, VALUE) \
697 assert_non_null((NODE).canonical); \
698 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
699 assert_non_null((NODE).realtype); \
700 assert_int_equal(LY_TYPE_BITS, (NODE).realtype->basetype); \
701 { \
Michal Vasko79135ae2020-12-16 10:08:35 +0100702 LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200703 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).bits_items)); \
704 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
705 assert_string_equal(VALUE[it], (NODE).bits_items[it]->name); \
706 } \
707 }
708
709/**
710 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INST
711 * @param[in] NODE lyd_value variable
712 * @param[in] CANNONICAL_VAL expected cannonical value
713 * @param[in] VALUE expected array of enum ly_path_pred_type
714 * @brief Example enum arr[] = {0x0, 0x1}; CHECK_LYD_VALUE(node->value, INST, "test/d", arr);
715 */
716#define CHECK_LYD_VALUE_INST(NODE, CANNONICAL_VAL, VALUE) \
717 assert_non_null((NODE).canonical); \
718 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
719 assert_non_null((NODE).realtype); \
720 assert_int_equal(LY_TYPE_INST, (NODE).realtype->basetype); \
721 { \
Michal Vasko79135ae2020-12-16 10:08:35 +0100722 LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200723 assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).target)); \
724 for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \
725 assert_int_equal(VALUE[it], (NODE).target[it].pred_type); \
726 } \
727 }
728
729/**
730 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type ENUM.
731 * Example CHECK_LYD_VALUE(node->value, ENUM, "item_name", "item_name");
732 * @param[in] NODE lyd_value variable
733 * @param[in] CANNONICAL_VAL expected cannonical value
734 * @param[in] VALUE expected enum item name
735 */
736#define CHECK_LYD_VALUE_ENUM(NODE, CANNONICAL_VAL, VALUE) \
737 assert_non_null((NODE).canonical); \
738 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
739 assert_non_null((NODE).realtype); \
740 assert_int_equal(LY_TYPE_ENUM, (NODE).realtype->basetype); \
741 assert_string_equal(VALUE, (NODE).enum_item->name);
742
743/**
744 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT8
745 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
746 * @param[in] NODE lyd_value variable
747 * @param[in] CANNONICAL_VAL expected cannonical value
748 * @param[in] VALUE expected inteager value (-128 to 127).
749 */
750#define CHECK_LYD_VALUE_INT8(NODE, CANNONICAL_VAL, VALUE) \
751 assert_non_null((NODE).canonical); \
752 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
753 assert_non_null((NODE).realtype); \
754 assert_int_equal(LY_TYPE_INT8, (NODE).realtype->basetype); \
755 assert_int_equal(VALUE, (NODE).int8);
756
757/**
758 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT16
759 * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12);
760 * @param[in] NODE lyd_value variable
761 * @param[in] CANNONICAL_VAL expected cannonical value
762 * @param[in] VALUE expected inteager value.
763 */
764#define CHECK_LYD_VALUE_INT16(NODE, CANNONICAL_VAL, VALUE) \
765 assert_non_null((NODE).canonical); \
766 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
767 assert_non_null((NODE).realtype); \
768 assert_int_equal(LY_TYPE_INT16, (NODE).realtype->basetype); \
769 assert_int_equal(VALUE, (NODE).int16);
770
771/**
772 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT8.
773 * Example CHECK_LYD_VALUE(node->value, UINT8, "12", 12);
774 * @param[in] NODE lyd_value variable
775 * @param[in] CANNONICAL_VAL expected cannonical value
776 * @param[in] VALUE expected inteager (0 to 255).
777 */
778#define CHECK_LYD_VALUE_UINT8(NODE, CANNONICAL_VAL, VALUE) \
779 assert_non_null((NODE).canonical); \
780 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
781 assert_non_null((NODE).realtype); \
782 assert_int_equal(LY_TYPE_UINT8, (NODE).realtype->basetype); \
783 assert_int_equal(VALUE, (NODE).uint8);
784
785/**
786 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type STRING.
787 * Example CHECK_LYD_VALUE(node->value, STRING, "text");
788 * @param[in] NODE lyd_value variable
789 * @param[in] CANNONICAL_VAL expected cannonical value
790 */
791#define CHECK_LYD_VALUE_STRING(NODE, CANNONICAL_VAL) \
792 assert_non_null((NODE).canonical); \
793 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
794 assert_non_null((NODE).realtype); \
795 assert_int_equal(LY_TYPE_STRING, (NODE).realtype->basetype);
796
797/**
798 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type LEAFREF
799 * @brief Example CHECK_LYD_VALUE(node->value, LEAFREF, "");
800 * @param[in] NODE lyd_value variable
801 * @param[in] CANNONICAL_VAL expected cannonical value
802 */
803#define CHECK_LYD_VALUE_LEAFREF(NODE, CANNONICAL_VAL) \
804 assert_non_null((NODE).canonical); \
805 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
806 assert_non_null((NODE).realtype); \
807 assert_int_equal(LY_TYPE_LEAFREF, (NODE).realtype->basetype); \
808 assert_non_null((NODE).ptr)
809
810/**
811 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type DEC64
812 * Example CHECK_LYD_VALUE(node->value, DEC64, "125", 125);
813 * @param[in] NODE lyd_value variable
814 * @param[in] CANNONICAL_VAL expected cannonical value
815 * @param[in] VALUE expected value 64bit inteager
816*/
817#define CHECK_LYD_VALUE_DEC64(NODE, CANNONICAL_VAL, VALUE) \
818 assert_non_null((NODE).canonical); \
819 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
820 assert_non_null((NODE).realtype); \
821 assert_int_equal(LY_TYPE_DEC64, (NODE).realtype->basetype); \
822 assert_int_equal(VALUE, (NODE).dec64);
823
824/**
825 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BINARY.
826 * Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8=");
827 * @param[in] NODE lyd_value variable
828 * @param[in] CANNONICAL_VAL expected cannonical value
829*/
830#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL) \
831 assert_non_null((NODE).canonical); \
832 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
833 assert_non_null((NODE).realtype); \
834 assert_int_equal(LY_TYPE_BINARY, (NODE).realtype->basetype);
835
836/**
837 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BOOL.
838 * Example CHECK_LYD_VALUE(node->value, BOOL, "true", 1);
839 * @param[in] NODE lyd_value variable
840 * @param[in] CANNONICAL_VAL expected cannonical value
841 * @param[in] VALUE expected boolean value 0,1
842*/
843#define CHECK_LYD_VALUE_BOOL(NODE, CANNONICAL_VAL, VALUE) \
844 assert_non_null((NODE).canonical); \
845 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
846 assert_non_null((NODE).realtype); \
847 assert_int_equal(LY_TYPE_BOOL, (NODE).realtype->basetype); \
848 assert_int_equal(VALUE, (NODE).boolean);
849
850/**
851 * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type IDENT.
852 * Example CHECK_LYD_VALUE(node->value, IDENT, "types:gigabit-ethernet", "gigabit-ethernet");
853 * @param[in] NODE lyd_value variable
854 * @param[in] CANNONICAL_VAL expected cannonical value
855 * @param[in] VALUE expected ident name
856*/
857#define CHECK_LYD_VALUE_IDENT(NODE, CANNONICAL_VAL, VALUE) \
858 assert_non_null((NODE).canonical); \
859 assert_string_equal((NODE).canonical, CANNONICAL_VAL); \
860 assert_non_null((NODE).realtype); \
861 assert_int_equal(LY_TYPE_IDENT, (NODE).realtype->basetype); \
862 assert_string_equal(VALUE, (NODE).ident->name);
863
864/**
865 * @brief Add module (from a string) into the used libyang context.
866 * @param[in] DATA String storing the schema module representation.
867 * @param[in] FORMAT Schema format of the @p DATA
868 * @param[in] FEATURES Array of module's features to enable
869 * @param[out] MOD Optional parameter as a pointer to variable to store the resulting module.
870 */
871#define UTEST_ADD_MODULE(DATA, FORMAT, FEATURES, MOD) \
872 assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \
873 assert_int_equal(LY_SUCCESS, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, MOD)); \
874 ly_in_free(_UC->in, 0); \
875 _UC->in = NULL
876
877/**
878 * @brief Internal macro to compare error info record with the expected error message and path.
879 * If NULL is provided as MSG, no error info record (NULL) is expected.
880 * @param[in] ERR Error information record from libyang context.
881 * @param[in] MSG Expected error message.
882 * @param[in] PATH Expected error path.
883 *
884 */
885#define _CHECK_LOG_CTX(ERR, MSG, PATH) \
886 if (!MSG) { \
887 assert_null(ERR); \
888 } else { \
889 assert_non_null(ERR); \
890 CHECK_STRING((ERR)->msg, MSG); \
891 CHECK_STRING((ERR)->path, PATH); \
892 }
893
894/**`
895 * @brief Internal macro to check the last libyang's context error.
896 */
897#define _CHECK_LOG_CTX1(MSG, PATH) \
898 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG, PATH)
899
900/**
901 * @brief Internal macro to check the last two libyang's context error.
902 */
903#define _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2) \
904 _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG1, PATH1); \
905 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev, MSG2, PATH2)
906
907/**
908 * @brief Internal macro to check the last three libyang's context error.
909 */
910#define _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3) \
911 _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2); \
912 _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev, MSG3, PATH3)
913
914/**
915 * @brief Internal helper macro to select _CHECK_LOG_CTX* macro according to the provided parameters.
916 */
917#define _GET_CHECK_LOG_MACRO(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
918
919/**
920 * @brief Check expected error(s) in libyang context.
921 *
922 * Macro has variadic parameters expected to be provided in pairs of error message and error path starting
923 * from the latest error. Current limit is checking at most 3 last errors. After checking, macro cleans up
924 * all the errors from the libyang context.
925 *
926 * @param[in] MSG Expected error message.
927 * @param[in] PATH Expected error path.
928 */
929#define CHECK_LOG_CTX(...) \
930 _GET_CHECK_LOG_MACRO(__VA_ARGS__, _CHECK_LOG_CTX3, _INVAL, _CHECK_LOG_CTX2, _INVAL, _CHECK_LOG_CTX1)(__VA_ARGS__); \
931 ly_err_clean(_UC->ctx, NULL)
932
933/**
934 * @brief Clean up the logging callback's storage.
935 */
936#define UTEST_LOG_CLEAN \
937 free(_UC->err_msg); \
938 free(_UC->err_path); \
939 _UC->err_msg = NULL; \
940 _UC->err_path = NULL;
941
942/**
943 * @brief Check expected error directly logged via logging callback.
944 * Useful mainly for messages logged by functions without access to libyang context.
945 * @param[in] MSG Expected error message.
946 * @param[in] PATH Expected error path.
947 */
948#define CHECK_LOG(MSG, PATH) \
949 CHECK_STRING(_UC->err_msg, MSG); \
950 CHECK_STRING(_UC->err_path, PATH); \
951 UTEST_LOG_CLEAN
952
953#ifdef _UTEST_MAIN_
954/*
955 * Functions inlined into each C source file including this header with _UTEST_MAIN_ defined
956 */
957
958/**
959 * @brief Global variable holding the tests context to simplify access to it.
960 */
961struct utest_context *current_utest_context;
962
963/* set to 0 to printing error messages to stderr instead of checking them in code */
964#define ENABLE_LOGGER_CHECKING 1
965
966/**
967 * @brief Logging callback for libyang.
968 */
969static void
970_utest_logger(LY_LOG_LEVEL level, const char *msg, const char *path)
971{
972 (void) level; /* unused */
973
974 if (ENABLE_LOGGER_CHECKING == 0) {
975 printf("ERROR:\n\tMESSAGE: %s\n\tPATH: %s\n\tLEVEL: %i\n", msg, path, level);
976 } else {
977 free(current_utest_context->err_msg);
978 current_utest_context->err_msg = msg ? strdup(msg) : NULL;
979 free(current_utest_context->err_path);
980 current_utest_context->err_path = path ? strdup(path) : NULL;
981 }
982}
983
984/**
985 * @brief Generic utest's setup
986 */
987static int
988utest_setup(void **state)
989{
990 /* setup the logger */
991 ly_set_log_clb(_utest_logger, 1);
992 ly_log_options(LY_LOLOG | LY_LOSTORE);
993
994 current_utest_context = calloc(1, sizeof *current_utest_context);
995 assert_non_null(current_utest_context);
996 *state = current_utest_context;
997
998 /* libyang context */
999 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &current_utest_context->ctx));
1000
1001 return 0;
1002}
1003
1004/**
1005 * @brief macro to include generic utest's setup into the test-specific setup.
1006 *
1007 * Place at the beginning of the test-specific setup
1008 */
1009#define UTEST_SETUP \
1010 assert_int_equal(0, utest_setup(state))
1011
1012/**
1013 * @brief Generic utest's teardown
1014 */
1015static int
1016utest_teardown(void **state)
1017{
1018 *state = NULL;
1019
1020 /* libyang context */
1021 ly_ctx_destroy(current_utest_context->ctx, NULL);
1022
1023 /* utest context */
1024 ly_in_free(current_utest_context->in, 0);
1025 free(current_utest_context->err_msg);
1026 free(current_utest_context->err_path);
1027 free(current_utest_context);
1028 current_utest_context = NULL;
1029
1030 return 0;
1031}
1032
1033/**
1034 * @brief macro to include generic utest's teardown into the test-specific teardown.
1035 *
1036 * Place at the end of the test-specific teardown
1037 */
1038#define UTEST_TEARDOWN \
1039 assert_int_equal(0, utest_teardown(state))
1040
1041/**
1042 * @brief Internal macro for utest setup with test-specific setup and teardown
1043 */
1044#define _UTEST_SETUP_TEARDOWN(FUNC, SETUP, TEARDOWN) \
1045 cmocka_unit_test_setup_teardown(FUNC, SETUP, TEARDOWN)
1046
1047/**
1048 * @brief Internal macro for utest setup with test-specific setup and generic teardown
1049 */
1050#define _UTEST_SETUP(FUNC, SETUP) \
1051 cmocka_unit_test_setup_teardown(FUNC, SETUP, utest_teardown)
1052
1053/**
1054 * @brief Internal macro for utest setup with generic setup and teardown
1055 */
1056#define _UTEST(FUNC) \
1057 cmocka_unit_test_setup_teardown(FUNC, utest_setup, utest_teardown)
1058
1059/**
1060 * @brief Internal helper macro to select _UTEST* macro according to the provided parameters.
1061 */
1062#define _GET_UTEST_MACRO(_1, _2, _3, NAME, ...) NAME
1063
1064/**
1065 * @brief Macro to specify test function using utest environment. Macro has variadic parameters
1066 * to provide test-specific setup/teardown functions:
1067 *
1068 * UTEST(test_func) - only implicit setup and teardown functions are used
1069 * UTEST(test_func, setup) - implicit teardown but own setup
1070 * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific
1071 */
1072#define UTEST(...) \
1073 _GET_UTEST_MACRO(__VA_ARGS__, _UTEST_SETUP_TEARDOWN, _UTEST_SETUP, _UTEST)(__VA_ARGS__)
1074
1075#else /* _UTEST_MAIN_ */
1076
1077extern struct utest_context *current_utest_context;
1078
1079#endif /* _UTEST_MAIN_ */
1080
1081#endif /* _UTESTS_H_ */