Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1 | /** |
| 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ša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 7 | * Copyright (c) 2021 CESNET, z.s.p.o. |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 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 Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 21 | #include <setjmp.h> |
| 22 | #include <stdarg.h> |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | #include <cmocka.h> |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 26 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "libyang.h" |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 30 | #include "plugins_types.h" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 31 | #include "tests/config.h" |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 32 | #include "tree_schema_internal.h" |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 33 | |
| 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 | */ |
| 88 | struct 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ša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 211 | * @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 | |
| 244 | /* @brief check range |
| 245 | * @param[in] NODE pointer to lysc_range value |
| 246 | * @param[in] DSC expected descriptin (string) |
| 247 | * @param[in] EAPPTAG expected string reprezenting error-app-tag value |
| 248 | * @param[in] EMSG expected string reprezenting error message |
| 249 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list |
| 250 | * @param[in] PARTS expected [sized array](@ref sizedarrays) number of rang limitations |
| 251 | * @param[in] REF expected reference |
| 252 | */ |
| 253 | #define CHECK_LYSC_RANGE(NODE, DSC, EAPPTAG, EMSG, EXTS, PARTS, REF) \ |
| 254 | assert_non_null(NODE); \ |
| 255 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 256 | CHECK_STRING((NODE)->eapptag, EAPPTAG); \ |
| 257 | CHECK_STRING((NODE)->emsg, EMSG); \ |
| 258 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 259 | CHECK_ARRAY((NODE)->parts, PARTS); \ |
| 260 | CHECK_STRING((NODE)->ref, REF) |
| 261 | |
| 262 | /* @brief check pattern |
| 263 | * @param[in] NODE pointer to lysc_pattern value |
| 264 | * @param[in] DSC expected descriptin (string) |
| 265 | * @param[in] EAPPTAG expected string reprezenting error-app-tag value |
| 266 | * @param[in] EMSG expected string reprezenting error message |
| 267 | * @param[in] EEXPR expected string reprezenting original, not compiled, regular expression |
| 268 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list |
| 269 | * @param[in] INVERTED if regular expression is inverted. |
| 270 | * @param[in] REF expected reference |
| 271 | */ |
| 272 | #define CHECK_LYSC_PATTERN(NODE, DSC, EAPPTAG, EMSG, EXPR, EXTS, INVERTED, REF) \ |
| 273 | assert_non_null(NODE); \ |
| 274 | assert_non_null((NODE)->code); \ |
| 275 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 276 | CHECK_STRING((NODE)->eapptag, EAPPTAG); \ |
| 277 | CHECK_STRING((NODE)->emsg, EMSG); \ |
| 278 | CHECK_STRING((NODE)->expr, EXPR); \ |
| 279 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 280 | assert_int_equal((NODE)->inverted, INVERTED); \ |
| 281 | CHECK_STRING((NODE)->ref, REF) |
| 282 | |
| 283 | /** |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 284 | * @brief assert that lysp_action_inout structure members are correct |
| 285 | * @param[in] NODE pointer to lysp_action_inout variable |
| 286 | * @param[in] DATA 0 -> check if pointer to data is NULL, 1 -> check if pointer to data is not null |
| 287 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extens list |
| 288 | * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list |
| 289 | * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of must restriction list |
| 290 | * @param[in] NODETYPE node type. LYS_INPUT or LYS_OUTPUT |
| 291 | * @param[in] PARENT 0 -> check if node is root, 1 -> check if node is not root |
| 292 | * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of typedefs list |
| 293 | */ |
| 294 | #define CHECK_LYSP_ACTION_INOUT(NODE, DATA, EXTS, GROUPINGS, MUSTS, NODETYPE, PARENT, TYPEDEFS) \ |
| 295 | assert_non_null(NODE); \ |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 296 | CHECK_POINTER((NODE)->child, DATA); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 297 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 298 | CHECK_POINTER((NODE)->groupings, GROUPINGS); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 299 | CHECK_ARRAY((NODE)->musts, MUSTS); \ |
| 300 | assert_int_equal((NODE)->nodetype, NODETYPE); \ |
| 301 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 302 | CHECK_ARRAY((NODE)->typedefs, TYPEDEFS); |
| 303 | |
| 304 | /** |
| 305 | * @brief assert that lysp_action structure members are correct |
| 306 | * @param[in] NODE pointer to lysp_action variable |
| 307 | * @param[in] DSC expected description |
| 308 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of extension list |
| 309 | * @param[in] FLAGS expected [schema node flags](@ref snodeflags) |
| 310 | * @param[in] GROUPINGS expected [sized array](@ref sizedarrays) size of grouping list |
| 311 | * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of if-feature expressions list |
| 312 | * @param[in] INPUT_* ::LYSP_ACTION_INOUT_CHECK |
| 313 | * @param[in] NAME expected name |
| 314 | * @param[in] NODETYPE node type. LYS_RPC or LYS_ACTION |
| 315 | * @param[in] OUTPUT_* ::LYSP_ACTION_INOUT_CHECK |
| 316 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 317 | * @param[in] REF expected reference |
| 318 | * @param[in] TYPEDEFS expected [sized array](@ref sizedarrays) size of list of typedefs |
| 319 | */ |
| 320 | #define CHECK_LYSP_ACTION(NODE, DSC, EXTS, FLAGS, GROUPINGS, IFFEATURES, \ |
| 321 | INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, INPUT_MUSTS, \ |
| 322 | INPUT_PARENT, INPUT_TYPEDEFS, \ |
| 323 | NAME, NODETYPE, \ |
| 324 | OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, OUTPUT_MUSTS, \ |
| 325 | OUTPUT_PARENT, OUTPUT_TYPEDEFS, \ |
| 326 | PARENT, REF, TYPEDEFS) \ |
| 327 | assert_non_null(NODE); \ |
| 328 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 329 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 330 | assert_int_equal((NODE)->flags, FLAGS); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 331 | CHECK_POINTER((NODE)->groupings, GROUPINGS); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 332 | CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \ |
| 333 | CHECK_LYSP_ACTION_INOUT(&((NODE)->input), INPUT_DATA, INPUT_EXTS, INPUT_GROUPINGS, \ |
| 334 | INPUT_MUSTS, LYS_INPUT, INPUT_PARENT, INPUT_TYPEDEFS); \ |
| 335 | assert_string_equal((NODE)->name, NAME); \ |
| 336 | assert_int_equal((NODE)->nodetype, NODETYPE); \ |
| 337 | CHECK_LYSP_ACTION_INOUT(&((NODE)->output), OUTPUT_DATA, OUTPUT_EXTS, OUTPUT_GROUPINGS, \ |
| 338 | OUTPUT_MUSTS, LYS_OUTPUT, OUTPUT_PARENT, OUTPUT_TYPEDEFS); \ |
| 339 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 340 | CHECK_STRING((NODE)->ref, REF); \ |
| 341 | CHECK_ARRAY((NODE)->typedefs, TYPEDEFS) \ |
| 342 | |
| 343 | /** |
| 344 | * @brief assert that lysp_when structure members are correct |
| 345 | * @param[in] NODE pointer to lysp_when variable |
| 346 | * @param[in] COND expected string specifid condition |
| 347 | * @param[in] DSC expected string description statement |
| 348 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array |
| 349 | * @param[in] REF expected string reference |
| 350 | */ |
| 351 | #define CHECK_LYSP_WHEN(NODE, COND, DSC, EXTS, REF) \ |
| 352 | assert_non_null(NODE); \ |
| 353 | assert_string_equal((NODE)->cond, COND); \ |
| 354 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 355 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 356 | if (REF == NULL) { \ |
| 357 | assert_null((NODE)->ref); \ |
| 358 | } else { \ |
| 359 | assert_non_null((NODE)->ref); \ |
| 360 | assert_string_equal((NODE)->ref, REF); \ |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @brief assert that lysp_restr structure members are correct |
| 365 | * @param[in] NODE pointer to lysp_restr variable |
| 366 | * @param[in] ARG_STR expected string. The restriction expression/value |
| 367 | * @param[in] DSC expected descrition |
| 368 | * @param[in] EAPPTAG expected string reprezenting error-app-tag value |
| 369 | * @param[in] EMSG expected string reprezenting error message |
| 370 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension array |
| 371 | * @param[in] REF expected reference |
| 372 | */ |
| 373 | |
| 374 | #define CHECK_LYSP_RESTR(NODE, ARG_STR, DSC, EAPPTAG, EMSG, EXTS, REF) \ |
| 375 | assert_non_null(NODE); \ |
| 376 | assert_non_null((NODE)->arg.mod); \ |
| 377 | assert_string_equal((NODE)->arg.str, ARG_STR); \ |
| 378 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 379 | CHECK_STRING((NODE)->eapptag, EAPPTAG); \ |
| 380 | CHECK_STRING((NODE)->emsg, EMSG); \ |
| 381 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 382 | CHECK_STRING((NODE)->ref, REF); |
| 383 | |
| 384 | /** |
| 385 | * @brief assert that lysp_import structure members are correct |
| 386 | * @param[in] NODE pointer to lysp_import variable |
| 387 | * @param[in] DSC expected description or NULL |
| 388 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extensions |
| 389 | * @param[in] NAME expected name of imported module |
| 390 | * @param[in] PREFIX expected prefix for the data from the imported schema |
| 391 | * @param[in] REF expected reference |
| 392 | * @prame[in] REV expected reprezenting date in format "11-10-2020" |
| 393 | */ |
| 394 | #define CHECK_LYSP_IMPORT(NODE, DSC, EXTS, NAME, PREFIX, REF, REV) \ |
| 395 | assert_non_null(NODE); \ |
| 396 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 397 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 398 | /*assert_non_null((NODE)->module); // ?? it is mandatory but in some test it doesnt work */ \ |
| 399 | assert_string_equal((NODE)->name, NAME); \ |
| 400 | assert_string_equal((NODE)->prefix, PREFIX); \ |
| 401 | CHECK_STRING((NODE)->ref, REF); \ |
| 402 | CHECK_STRING((NODE)->rev, REV); \ |
| 403 | |
| 404 | /** |
| 405 | * @brief assert that lysp_ext structure members are correct |
| 406 | * @param[in] NODE pointer to lysp_ext_instance variable |
| 407 | * @param[in] ARGUMENT expected argument name |
| 408 | * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists |
| 409 | * @param[in] DSC expected string reprezent description |
| 410 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances |
| 411 | * @param[in] FLAGS expected LYS_STATUS_* and LYS_YINELEM_* values (@ref snodeflags) |
| 412 | * @param[in] NAME expected name |
| 413 | * @param[in] REF expected ref |
| 414 | */ |
| 415 | #define CHECK_LYSP_EXT(NODE, ARGUMENT, COMPILED, DSC, EXTS, FLAGS, NAME, REF) \ |
| 416 | assert_non_null(NODE); \ |
| 417 | CHECK_STRING((NODE)->argument, ARGUMENT); \ |
| 418 | CHECK_POINTER((NODE)->compiled, COMPILED); \ |
| 419 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 420 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 421 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 422 | assert_string_equal((NODE)->name, NAME); \ |
| 423 | CHECK_STRING((NODE)->ref, REF); |
| 424 | |
| 425 | /** |
| 426 | * @brief assert that lysp_ext_instance structure members are correct |
| 427 | * @param[in] NODE pointer to lysp_ext_instance variable |
| 428 | * @param[in] ARGUMENT expected optional value of the extension's argument |
| 429 | * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children |
| 430 | * @param[in] COMPILED 0 -> compiled data dosnt exists, 1 -> compiled data exists |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 431 | * @param[in] PARENT_STMT expected value identifying placement of the extension instance |
| 432 | * @param[in] PARENT_STMT_INDEX expected indentifi index |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 433 | * @param[in] FORMAT expected format |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 434 | */ |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 435 | #define CHECK_LYSP_EXT_INSTANCE(NODE, ARGUMENT, CHILD, COMPILED, PARENT_STMT, PARENT_STMT_INDEX, NAME, FORMAT) \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 436 | assert_non_null(NODE); \ |
| 437 | CHECK_STRING((NODE)->argument, ARGUMENT); \ |
| 438 | CHECK_POINTER((NODE)->child, CHILD); \ |
| 439 | CHECK_POINTER((NODE)->compiled, COMPILED); \ |
| 440 | /*assert_int_equal((NODE)->flags, LYS_INTERNAL);*/ \ |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 441 | assert_int_equal((NODE)->parent_stmt, PARENT_STMT); \ |
| 442 | assert_int_equal((NODE)->parent_stmt_index, PARENT_STMT_INDEX); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 443 | assert_string_equal((NODE)->name, NAME); \ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 444 | assert_int_equal((NODE)->format, FORMAT); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 445 | |
| 446 | /** |
| 447 | * @brief assert that lysp_stmt structure members are correct |
| 448 | * @param[in] NODE pointer to lysp_stmt variable |
| 449 | * @param[in] ARG expected statemet argumet |
| 450 | * @param[in] CHILD 0 -> node doesnt have child, 1 -> node have children |
| 451 | * @param[in] FLAGS expected statement flags, can be set to LYS_YIN_ATTR |
| 452 | * @param[in] KW expected numeric respresentation of the stmt value |
| 453 | * @param[in] NEXT 0 -> pointer is NULL, 1 -> pointer is not null |
| 454 | * @param[in] STMS expected identifier of the statement |
| 455 | */ |
| 456 | #define CHECK_LYSP_STMT(NODE, ARG, CHILD, FLAGS, KW, NEXT, STMT) \ |
| 457 | assert_non_null(NODE); \ |
| 458 | CHECK_STRING((NODE)->arg, ARG); \ |
| 459 | CHECK_POINTER((NODE)->child, CHILD); \ |
| 460 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 461 | assert_int_equal((NODE)->kw, KW); \ |
| 462 | CHECK_POINTER((NODE)->next, NEXT); \ |
| 463 | assert_string_equal((NODE)->stmt, STMT); \ |
| 464 | |
| 465 | /** |
| 466 | * @brief assert that lysp_type_enum structure members are correct |
| 467 | * @param[in] NODE pointer to lysp_type_enum variable |
| 468 | * @param[in] DSC expected description |
| 469 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 470 | * @param[in] FLAGS only LYS_STATUS_ and LYS_SET_VALUE values are allowed |
| 471 | * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 472 | * @param[in] NAME expected name |
| 473 | * @param[in] REF expected reference statement |
| 474 | * @param[in] VALUE expected enum's value or bit's position |
| 475 | */ |
| 476 | #define CHECK_LYSP_TYPE_ENUM(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, REF, VALUE) \ |
| 477 | assert_non_null(NODE); \ |
| 478 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 479 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 480 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 481 | CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \ |
| 482 | CHECK_STRING((NODE)->name, NAME); \ |
| 483 | CHECK_STRING((NODE)->ref, REF); \ |
| 484 | assert_int_equal(VALUE, (NODE)->value); |
| 485 | |
| 486 | /** |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 487 | * @brief assert that lysp_type_enum structure members are correct |
| 488 | * @param[in] NODE pointer to lysp_type variable |
| 489 | * @param[in] BASES expected [sized array](@ref sizedarrays) size of list of indentifiers |
| 490 | * @param[in] BITS expected [sized array](@ref sizedarrays) size of list of bits |
| 491 | * @param[in] COMPILED 0 -> pointer to compiled type is null, 1 -> pointer to compilet type is valid |
| 492 | * @param[in] ENUMS expected [sized array](@ref sizedarrays) size of list of enums-stmts |
| 493 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of extension instances |
| 494 | * @param[in] FLAGS expected flags |
| 495 | * @param[in] FRACTION_DIGITS expected number of fraction digits decimal64 |
| 496 | * @param[in] LENGTH expected 0 -> there isnt any restriction on length, 1 -> type is restricted on length (string, binary) |
| 497 | * @param[in] NAME expected name of type |
| 498 | * @param[in] PATH 0 -> no pointer to parsed path, 1 -> pointer to parsed path is valid |
| 499 | * @param[in] PATTERNS expected [sized array](@ref sizedarrays) size of list of patterns for string |
| 500 | * @param[in] PMOD expected submodule where type is defined 0 -> pointer is null, 1 -> pointer is not null |
| 501 | * @param[in] RANGE expected [sized array](@ref sizedarrays) size of list of range restriction |
| 502 | * @param[in] REQUIRE_INSTANCE expected require instance flag |
| 503 | * @param[in] TYPES expected [sized array](@ref sizedarrays) size of list of sub-types |
| 504 | */ |
| 505 | #define CHECK_LYSP_TYPE(NODE, BASES, BITS, COMPILED, ENUMS, EXTS, FLAGS, FRACTIONS_DIGITS, \ |
| 506 | LENGTH, NAME, PATH, PATTERNS, PMOD, RANGE, REQUIRE_INSTANCE, TYPES) \ |
| 507 | assert_non_null(NODE);\ |
| 508 | CHECK_ARRAY((NODE)->bases, BASES); \ |
| 509 | CHECK_ARRAY((NODE)->bits, BITS); \ |
| 510 | CHECK_POINTER((NODE)->compiled, COMPILED); \ |
| 511 | CHECK_ARRAY((NODE)->enums, ENUMS); \ |
| 512 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 513 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 514 | assert_int_equal((NODE)->fraction_digits, FRACTIONS_DIGITS); \ |
| 515 | CHECK_POINTER((NODE)->length, LENGTH); \ |
| 516 | CHECK_STRING((NODE)->name, NAME); \ |
| 517 | CHECK_POINTER((NODE)->path, PATH); \ |
| 518 | CHECK_ARRAY((NODE)->patterns, PATTERNS); \ |
| 519 | CHECK_POINTER((NODE)->pmod, PMOD); \ |
| 520 | CHECK_POINTER((NODE)->range, RANGE); \ |
| 521 | assert_int_equal((NODE)->require_instance, REQUIRE_INSTANCE); \ |
| 522 | CHECK_ARRAY((NODE)->types , TYPES) |
| 523 | |
| 524 | /** |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 525 | * @brief assert that lysp_node structure members are correct |
| 526 | * @param[in] NODE pointer to lysp_node variable |
| 527 | * @param[in] DSC expected description statement |
| 528 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 529 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 530 | * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 531 | * @param[in] NAME expected name |
| 532 | * @param[in] NEXT 0 pointer is null, 1 pointer is not null |
| 533 | * @param[in] NODETYPE node type LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, LYS_LEAFLIST, |
| 534 | * LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, LYS_ACTION, LYS_NOTIF, |
| 535 | * LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, LYS_AUGMENT |
| 536 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 537 | * @param[in] REF expected reference statement |
| 538 | * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null |
| 539 | */ |
| 540 | #define CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, NODETYPE, PARENT, REF, WHEN) \ |
| 541 | assert_non_null(NODE); \ |
| 542 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 543 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 544 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 545 | CHECK_ARRAY((NODE)->iffeatures, IFFEATURES); \ |
| 546 | CHECK_STRING((NODE)->name, NAME); \ |
| 547 | CHECK_POINTER((NODE)->next, NEXT); \ |
| 548 | assert_int_equal((NODE)->nodetype, NODETYPE); \ |
| 549 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 550 | CHECK_STRING((NODE)->ref, REF); \ |
Radek Krejci | 9a3823e | 2021-01-27 20:26:46 +0100 | [diff] [blame] | 551 | CHECK_POINTER(lysp_node_when((struct lysp_node *)NODE), WHEN); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 552 | |
| 553 | /** |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 554 | * @brief assert that lysp_node structure members are correct |
| 555 | * @param[in] NODE pointer to lysp_node variable |
| 556 | * @param[in] DSC expected description statement |
| 557 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 558 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 559 | * @param[in] IFFEATURES expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 560 | * @param[in] NAME expected name |
| 561 | * @param[in] NEXT 0 pointer is null, 1 pointer is not null |
| 562 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 563 | * @param[in] REF expected reference statement |
| 564 | * @param[in] WHEN 0-> pointer is null, 1 -> pointer is not null |
| 565 | * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction |
| 566 | * @param[in] UNITS expected string reprezenting units |
| 567 | * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value |
| 568 | */ |
| 569 | #define CHECK_LYSP_NODE_LEAF(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, \ |
| 570 | PARENT, REF, WHEN, MUSTS, UNITS, DFLT) \ |
| 571 | CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, LYS_LEAF, PARENT, REF, WHEN); \ |
| 572 | CHECK_ARRAY((NODE)->musts, MUSTS); \ |
| 573 | CHECK_STRING((NODE)->units, UNITS); \ |
| 574 | CHECK_STRING((NODE)->dflt.str, DFLT); |
| 575 | |
| 576 | /** |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 577 | * @brief assert that lysc_notif structure members are correct |
| 578 | * @param[in] NODE pointer to lysp_notif variable |
| 579 | * @param[in] DATA 0 pointer is null, 1 pointer is not null |
| 580 | * @param[in] DSC expected description |
| 581 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 582 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 583 | * @param[in] MODULE 0 pointer is null, 1 pointer is not null |
| 584 | * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction |
| 585 | * @param[in] NAME expected name |
| 586 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 587 | * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null |
| 588 | * @param[in] REF expected reference |
| 589 | * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements |
| 590 | */ |
| 591 | #define CHECK_LYSC_NOTIF(NODE, DATA, DSC, EXTS, FLAGS, MODULE, MUSTS, NAME, PARENT, PRIV, REF, WHEN) \ |
| 592 | assert_non_null(NODE); \ |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 593 | CHECK_POINTER((NODE)->child, DATA); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 594 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 595 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 596 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 597 | CHECK_POINTER((NODE)->module, MODULE); \ |
| 598 | CHECK_ARRAY((NODE)->musts, MUSTS); \ |
| 599 | assert_string_equal((NODE)->name, NAME); \ |
| 600 | assert_int_equal((NODE)->nodetype, LYS_NOTIF); \ |
| 601 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 602 | CHECK_POINTER((NODE)->priv, PRIV); \ |
| 603 | CHECK_STRING((NODE)->ref, REF); \ |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 604 | CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 605 | |
| 606 | /** |
| 607 | * @brief assert that lysc_action_inout structure members are correct |
| 608 | * @param[in] NODE pointer to lysp_notif variable |
| 609 | * @param[in] DATA 0 pointer is null, 1 pointer is not null |
| 610 | * @param[in] MUST expected [sized array](@ref sizedarrays) size of list of must restrictions |
| 611 | * @param[in] NODETYPE LYS_INPUT or LYS_OUTPUT |
| 612 | */ |
| 613 | #define CHECK_LYSC_ACTION_INOUT(NODE, DATA, MUST, NODETYPE) \ |
| 614 | assert_non_null(NODE); \ |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 615 | CHECK_POINTER((NODE)->child, DATA); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 616 | CHECK_ARRAY((NODE)->musts, MUST); \ |
| 617 | assert_int_equal((NODE)->nodetype, NODETYPE); |
| 618 | |
| 619 | /** |
| 620 | * @brief assert that lysc_action structure members are correct |
| 621 | * @param[in] NODE pointer to lysp_action variable |
| 622 | * @param[in] DSC string description statement |
| 623 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 624 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 625 | * @param[in] INPUT_DATA 0 pointer is null, 1 pointer is not null |
| 626 | * @param[in] INPUT_MUST expected [sized array](@ref sizedarrays) size of input list of must restrictions |
| 627 | * @param[in] INPUT_EXTS expected [sized array](@ref sizedarrays) size of the input extension instances of input |
| 628 | * @param[in] MODULE 0 pointer is null, 1 pointer is not null |
| 629 | * @param[in] NAME expected name |
| 630 | * @param[in] NODETYPE LYS_RPC, LYS_ACTION |
| 631 | * @param[in] OUTPUT_DATA 0 pointer is null, 1 pointer is not null |
| 632 | * @param[in] OUTPUT_MUST expected [sized array](@ref sizedarrays) size of output list of must restrictions |
| 633 | * @param[in] OUTPUT_EXTS expected [sized array](@ref sizedarrays) size of the output extension instances of input |
| 634 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 635 | * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null |
| 636 | * @param[in] REF expected reference |
| 637 | * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements |
| 638 | */ |
| 639 | #define CHECK_LYSC_ACTION(NODE, DSC, EXTS, FLAGS, INPUT_DATA, INPUT_MUST, INPUT_EXTS, MODULE, NAME, NODETYPE, \ |
| 640 | OUTPUT_DATA, OUTPUT_MUST, OUTPUT_EXTS, PARENT, PRIV, REF, WHEN) \ |
| 641 | assert_non_null(NODE); \ |
| 642 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 643 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 644 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 645 | CHECK_LYSC_ACTION_INOUT(&(NODE)->input, INPUT_DATA, INPUT_MUST, LYS_INPUT); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 646 | CHECK_ARRAY((NODE)->input.exts, INPUT_EXTS); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 647 | CHECK_POINTER((NODE)->module, MODULE); \ |
| 648 | assert_string_equal((NODE)->name, NAME); \ |
| 649 | assert_int_equal((NODE)->nodetype, NODETYPE); \ |
| 650 | CHECK_LYSC_ACTION_INOUT(&(NODE)->output, OUTPUT_DATA, OUTPUT_MUST, LYS_OUTPUT); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 651 | CHECK_ARRAY((NODE)->output.exts, OUTPUT_EXTS); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 652 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 653 | CHECK_POINTER((NODE)->priv, PRIV); \ |
| 654 | CHECK_STRING((NODE)->ref, REF); \ |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 655 | CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 656 | |
| 657 | /** |
| 658 | * @brief assert that lysc_node structure members are correct |
| 659 | * @param[in] NODE pointer to lysc_node variable |
| 660 | * @param[in] DSC expected description |
| 661 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 662 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 663 | * @param[in] MODULE 0 pointer is null, 1 pointer is not null |
| 664 | * @param[in] NAME expected name |
| 665 | * @param[in] NEXT 0 pointer is null, 1 pointer is not null |
| 666 | * @param[in] NODETYPE type of the node LYS_UNKNOWN, LYS_CONTAINER, LYS_CHOICE, LYS_LEAF, |
| 667 | * LYS_LEAFLIST, LYS_LIST, LYS_ANYXML, LYS_ANYDATA, LYS_CASE, LYS_RPC, |
| 668 | * LYS_ACTION, LYS_NOTIF, LYS_USES, LYS_INPUT, LYS_OUTPUT, LYS_GROUPING, |
| 669 | * LYS_AUGMENT |
| 670 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 671 | * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null |
| 672 | * @param[in] REF expected reference |
| 673 | * @param[in] WHEN expected [sized array](@ref sizedarrays) size of list of pointers to when statements |
| 674 | */ |
| 675 | #define CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, NODETYPE, PARENT, PRIV, REF, WHEN) \ |
| 676 | assert_non_null(NODE); \ |
| 677 | CHECK_STRING((NODE)->dsc, DSC); \ |
| 678 | CHECK_ARRAY((NODE)->exts, EXTS); \ |
| 679 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 680 | CHECK_POINTER((NODE)->module, MODULE); \ |
| 681 | assert_string_equal((NODE)->name, NAME); \ |
| 682 | CHECK_POINTER((NODE)->next, NEXT); \ |
| 683 | assert_int_equal((NODE)->nodetype, NODETYPE); \ |
| 684 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 685 | assert_non_null((NODE)->prev); \ |
| 686 | CHECK_POINTER((NODE)->priv, PRIV); \ |
| 687 | CHECK_STRING((NODE)->ref, REF); \ |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 688 | CHECK_ARRAY(lysc_node_when((const struct lysc_node *)NODE), WHEN); |
| 689 | |
| 690 | /** |
| 691 | * @brief assert that lysc_node_leaf structure members are correct |
| 692 | * @param[in] NODE pointer to lysc_node variable |
| 693 | * @param[in] DSC expected description |
| 694 | * @param[in] EXTS expected [sized array](@ref sizedarrays) size of list of the extension instances |
| 695 | * @param[in] FLAGS [schema node flags](@ref snodeflags) |
| 696 | * @param[in] MODULE 0 pointer is null, 1 pointer is not null |
| 697 | * @param[in] NAME expected name |
| 698 | * @param[in] NEXT 0 pointer is null, 1 pointer is not null |
| 699 | * @param[in] PARENT 0-> check if node is root, 1-> check if node is not root |
| 700 | * @param[in] PRIV 0-> pointer is null, 1-> pointer is not null |
| 701 | * @param[in] REF expected reference |
| 702 | * @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 |
| 703 | * @param[in] MUSTS expected [sized array](@ref sizedarrays) size of list of must restriction |
| 704 | * @param[in] UNITS expected string reprezenting units |
| 705 | * @param[in] DFLT 0-> node dosn't have default value. 1 -> node have default value |
| 706 | */ |
| 707 | #define CHECK_LYSC_NODE_LEAF(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, \ |
| 708 | PARENT, PRIV, REF, WHEN, MUSTS, UNITS, DFLT) \ |
| 709 | CHECK_LYSC_NODE(NODE, DSC, EXTS, FLAGS, MODULE, NAME, NEXT, LYS_LEAF, PARENT, PRIV, REF, WHEN); \ |
| 710 | CHECK_ARRAY((NODE)->musts, MUSTS); \ |
| 711 | assert_non_null((NODE)->type); \ |
| 712 | CHECK_STRING((NODE)->units, UNITS); \ |
| 713 | CHECK_POINTER((NODE)->dflt, DFLT); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * @brief assert that lyd_meta structure members are correct |
| 717 | * @param[in] NODE pointer to lyd_meta variable |
| 718 | * @param[in] ANNOTATION 0 pointer is null, 1 pointer is not null |
| 719 | * @param[in] NAME expected name |
| 720 | * @param[in] NEXT 0 pointer is null, 1 pointer is not null |
| 721 | * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT |
| 722 | * part of text reprezenting LY_DATA_TYPE. |
| 723 | * @param[in] ... ::CHECK_LYD_VALUE |
| 724 | */ |
| 725 | #define CHECK_LYD_META(NODE, ANNOTATION, NAME, NEXT, PARENT, TYPE_VAL, ...) \ |
| 726 | assert_non_null(NODE); \ |
| 727 | CHECK_POINTER((NODE)->annotation, ANNOTATION); \ |
| 728 | assert_string_equal((NODE)->name, NAME); \ |
| 729 | CHECK_POINTER((NODE)->next, NEXT); \ |
| 730 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 731 | CHECK_LYD_VALUE((NODE)->value, TYPE_VAL, ##__VA_ARGS__); |
| 732 | |
| 733 | /** |
| 734 | * @brief assert that lyd_node_term structure members are correct |
| 735 | * @param[in] NODE pointer to lyd_node_term variable |
| 736 | * @param[in] FLAGS expected [data node flags](@ref dnodeflags) |
| 737 | * @param[in] META 0 -> meta is not prezent, 1 -> meta is prezent |
| 738 | * @param[in] NEXT 0 -> next node is not prezent, 1 -> next node is prezent |
| 739 | * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT |
| 740 | * part of text reprezenting LY_DATA_TYPE. |
| 741 | * @param[in] ... ::CHECK_LYD_VALUE |
| 742 | */ |
| 743 | #define CHECK_LYD_NODE_TERM(NODE, FLAGS, META, NEXT, PARENT, SCHEMA, VALUE_TYPE, ...) \ |
| 744 | assert_non_null(NODE); \ |
| 745 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 746 | CHECK_POINTER((NODE)->meta, META); \ |
| 747 | CHECK_POINTER((NODE)->next, NEXT); \ |
| 748 | CHECK_POINTER((NODE)->parent, PARENT); \ |
| 749 | assert_non_null((NODE)->prev); \ |
| 750 | CHECK_POINTER((NODE)->schema, SCHEMA); \ |
| 751 | CHECK_LYD_VALUE((NODE)->value, VALUE_TYPE, ##__VA_ARGS__); |
| 752 | |
| 753 | /** |
| 754 | * @brief assert that lyd_node_any structure members are correct |
| 755 | * @param[in] NODE pointer to lyd_node_term variable |
| 756 | * @param[in] FLAGS expected [data node flags](@ref dnodeflags) |
| 757 | * @param[in] META 0 meta isnt present , 1 meta is present |
| 758 | * @param[in] PARENT 0 it is root node , 1 node have parent |
| 759 | * @param[in] VALUE_TYPE value type ::lyd_node_any.value |
| 760 | */ |
| 761 | #define CHECK_LYD_NODE_ANY(NODE, FLAGS, META, PARENT, VALUE_TYPE) \ |
| 762 | assert_non_null(NODE); \ |
| 763 | assert_int_equal((NODE)->flags, FLAGS); \ |
| 764 | CHECK_POINTER((NODE)->meta, META); \ |
| 765 | CHECK_POINTER((NODE)->meta, PARENT); \ |
| 766 | assert_non_null((NODE)->prev); \ |
| 767 | assert_non_null((NODE)->schema); \ |
| 768 | assert_int_equal((NODE)->value_type, VALUE_TYPE); |
| 769 | |
| 770 | /** |
| 771 | * @brief assert that lyd_node_opaq structure members are correct |
| 772 | * @param[in] NODE pointer to lyd_node_opaq variable |
| 773 | * @param[in] ATTR 0 if pointer is null ,1 if pointer is not null |
| 774 | * @param[in] CHILD 0 if pointer is null ,1 if pointer is not null |
| 775 | * @param[in] FORMAT LY_PREF_XML or LY_PREF_JSON |
| 776 | * @param[in] VAL_PREFS 0 if pointer is null ,1 if pointer is not null |
| 777 | * @param[in] NAME expected name |
| 778 | * @param[in] value expected orignal value |
| 779 | */ |
| 780 | #define CHECK_LYD_NODE_OPAQ(NODE, ATTR, CHILD, FORMAT, NAME, NEXT, PARENT, PREFIX, VAL_PREFS, VALUE) \ |
| 781 | assert_non_null(NODE); \ |
| 782 | CHECK_POINTER((NODE)->attr, ATTR); \ |
| 783 | CHECK_POINTER((NODE)->child, CHILD); \ |
| 784 | assert_ptr_equal((NODE)->ctx, UTEST_LYCTX); \ |
| 785 | assert_int_equal((NODE)->flags, 0); \ |
| 786 | assert_true((NODE)->format == FORMAT); \ |
| 787 | assert_int_equal((NODE)->hash, 0); \ |
| 788 | assert_string_equal((NODE)->name.name, NAME); \ |
| 789 | assert_non_null((NODE)->prev); \ |
| 790 | assert_null((NODE)->schema); \ |
| 791 | CHECK_POINTER((NODE)->val_prefix_data, VAL_PREFS); \ |
| 792 | assert_string_equal((NODE)->value, VALUE); |
| 793 | |
| 794 | /** |
| 795 | * @brief assert that lyd_value structure members are correct |
| 796 | * @param[in] NODE lyd_value |
| 797 | * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT |
| 798 | * part of text reprezenting LY_DATA_TYPE. |
| 799 | * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified |
| 800 | * by type of value. These parameters are passed to macro |
| 801 | * CHECK_LYD_VALUE_ ## TYPE_VAL. |
| 802 | */ |
| 803 | #define CHECK_LYD_VALUE(NODE, TYPE_VAL, ...) \ |
| 804 | CHECK_LYD_VALUE_ ## TYPE_VAL (NODE, ##__VA_ARGS__); |
| 805 | |
| 806 | /* |
| 807 | * LYD VALUES CHECKING SPECIALIZATION |
| 808 | */ |
| 809 | |
| 810 | /** |
| 811 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type EMPTY |
| 812 | * Example CHECK_LYD_VALUE(node->value, EMPTY, ""); |
| 813 | * @param[in] NODE lyd_value variable |
| 814 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 815 | */ |
| 816 | #define CHECK_LYD_VALUE_EMPTY(NODE, CANNONICAL_VAL) \ |
| 817 | assert_non_null((NODE).canonical); \ |
| 818 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 819 | assert_non_null((NODE).realtype); \ |
| 820 | assert_int_equal((NODE).realtype->basetype, LY_TYPE_EMPTY); |
| 821 | |
| 822 | /** |
| 823 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UNION |
| 824 | * Example CHECK_LYD_VALUE(node->value, UNION, "12", INT8, "12", 12); |
| 825 | * @warning type of subvalue cannot be UNION. Example of calling |
| 826 | * @param[in] NODE lyd_value variable |
| 827 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 828 | * @param[in] TYPE_VAL value type. EMPTY, UNION, BITS, INST, ENUM, INT8, INT16, UINT8, STRING, LEAFREF, DEC64, BINARY, BOOL, IDENT |
| 829 | * @param[in] ... Unspecified parameters. Type and numbers of parameters are specified |
| 830 | * by type of value. These parameters are passed to macro |
| 831 | * CHECK_LYD_VALUE_ ## TYPE_VAL. |
| 832 | */ |
| 833 | #define CHECK_LYD_VALUE_UNION(NODE, CANNONICAL_VAL, TYPE_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(LY_TYPE_UNION, (NODE).realtype->basetype); \ |
| 838 | assert_non_null((NODE).subvalue); \ |
| 839 | assert_non_null((NODE).subvalue->prefix_data); \ |
| 840 | CHECK_LYD_VALUE_ ## TYPE_VAL ((NODE).subvalue->value, ## __VA_ARGS__) |
| 841 | |
| 842 | /** |
| 843 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BITS |
| 844 | * Example arr[] = {"a", "b"}; CHECK_LYD_VALUE(node->value, BITS, "a b", arr); |
| 845 | * @param[in] NODE lyd_value variable |
| 846 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 847 | * @param[in] VALUE expected array of bits names |
| 848 | */ |
| 849 | #define CHECK_LYD_VALUE_BITS(NODE, CANNONICAL_VAL, VALUE) \ |
| 850 | assert_non_null((NODE).canonical); \ |
| 851 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 852 | assert_non_null((NODE).realtype); \ |
| 853 | assert_int_equal(LY_TYPE_BITS, (NODE).realtype->basetype); \ |
| 854 | { \ |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 855 | LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 856 | assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).bits_items)); \ |
| 857 | for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \ |
| 858 | assert_string_equal(VALUE[it], (NODE).bits_items[it]->name); \ |
| 859 | } \ |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INST |
| 864 | * @param[in] NODE lyd_value variable |
| 865 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 866 | * @param[in] VALUE expected array of enum ly_path_pred_type |
| 867 | * @brief Example enum arr[] = {0x0, 0x1}; CHECK_LYD_VALUE(node->value, INST, "test/d", arr); |
| 868 | */ |
| 869 | #define CHECK_LYD_VALUE_INST(NODE, CANNONICAL_VAL, VALUE) \ |
| 870 | assert_non_null((NODE).canonical); \ |
| 871 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 872 | assert_non_null((NODE).realtype); \ |
| 873 | assert_int_equal(LY_TYPE_INST, (NODE).realtype->basetype); \ |
| 874 | { \ |
Michal Vasko | 79135ae | 2020-12-16 10:08:35 +0100 | [diff] [blame] | 875 | LY_ARRAY_COUNT_TYPE arr_size = sizeof(VALUE) / sizeof(VALUE[0]); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 876 | assert_int_equal(arr_size, LY_ARRAY_COUNT((NODE).target)); \ |
| 877 | for (LY_ARRAY_COUNT_TYPE it = 0; it < arr_size; it++) { \ |
| 878 | assert_int_equal(VALUE[it], (NODE).target[it].pred_type); \ |
| 879 | } \ |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type ENUM. |
| 884 | * Example CHECK_LYD_VALUE(node->value, ENUM, "item_name", "item_name"); |
| 885 | * @param[in] NODE lyd_value variable |
| 886 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 887 | * @param[in] VALUE expected enum item name |
| 888 | */ |
| 889 | #define CHECK_LYD_VALUE_ENUM(NODE, CANNONICAL_VAL, VALUE) \ |
| 890 | assert_non_null((NODE).canonical); \ |
| 891 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 892 | assert_non_null((NODE).realtype); \ |
| 893 | assert_int_equal(LY_TYPE_ENUM, (NODE).realtype->basetype); \ |
| 894 | assert_string_equal(VALUE, (NODE).enum_item->name); |
| 895 | |
| 896 | /** |
| 897 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT8 |
| 898 | * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12); |
| 899 | * @param[in] NODE lyd_value variable |
| 900 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 901 | * @param[in] VALUE expected inteager value (-128 to 127). |
| 902 | */ |
| 903 | #define CHECK_LYD_VALUE_INT8(NODE, CANNONICAL_VAL, VALUE) \ |
| 904 | assert_non_null((NODE).canonical); \ |
| 905 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 906 | assert_non_null((NODE).realtype); \ |
| 907 | assert_int_equal(LY_TYPE_INT8, (NODE).realtype->basetype); \ |
| 908 | assert_int_equal(VALUE, (NODE).int8); |
| 909 | |
| 910 | /** |
| 911 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type INT16 |
| 912 | * Example CHECK_LYD_VALUE(node->value, INT8, "12", 12); |
| 913 | * @param[in] NODE lyd_value variable |
| 914 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 915 | * @param[in] VALUE expected inteager value. |
| 916 | */ |
| 917 | #define CHECK_LYD_VALUE_INT16(NODE, CANNONICAL_VAL, VALUE) \ |
| 918 | assert_non_null((NODE).canonical); \ |
| 919 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 920 | assert_non_null((NODE).realtype); \ |
| 921 | assert_int_equal(LY_TYPE_INT16, (NODE).realtype->basetype); \ |
| 922 | assert_int_equal(VALUE, (NODE).int16); |
| 923 | |
| 924 | /** |
| 925 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type UINT8. |
| 926 | * Example CHECK_LYD_VALUE(node->value, UINT8, "12", 12); |
| 927 | * @param[in] NODE lyd_value variable |
| 928 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 929 | * @param[in] VALUE expected inteager (0 to 255). |
| 930 | */ |
| 931 | #define CHECK_LYD_VALUE_UINT8(NODE, CANNONICAL_VAL, VALUE) \ |
| 932 | assert_non_null((NODE).canonical); \ |
| 933 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 934 | assert_non_null((NODE).realtype); \ |
| 935 | assert_int_equal(LY_TYPE_UINT8, (NODE).realtype->basetype); \ |
| 936 | assert_int_equal(VALUE, (NODE).uint8); |
| 937 | |
| 938 | /** |
| 939 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type STRING. |
| 940 | * Example CHECK_LYD_VALUE(node->value, STRING, "text"); |
| 941 | * @param[in] NODE lyd_value variable |
| 942 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 943 | */ |
| 944 | #define CHECK_LYD_VALUE_STRING(NODE, CANNONICAL_VAL) \ |
| 945 | assert_non_null((NODE).canonical); \ |
| 946 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 947 | assert_non_null((NODE).realtype); \ |
| 948 | assert_int_equal(LY_TYPE_STRING, (NODE).realtype->basetype); |
| 949 | |
| 950 | /** |
| 951 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type LEAFREF |
| 952 | * @brief Example CHECK_LYD_VALUE(node->value, LEAFREF, ""); |
| 953 | * @param[in] NODE lyd_value variable |
| 954 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 955 | */ |
| 956 | #define CHECK_LYD_VALUE_LEAFREF(NODE, CANNONICAL_VAL) \ |
| 957 | assert_non_null((NODE).canonical); \ |
| 958 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 959 | assert_non_null((NODE).realtype); \ |
| 960 | assert_int_equal(LY_TYPE_LEAFREF, (NODE).realtype->basetype); \ |
| 961 | assert_non_null((NODE).ptr) |
| 962 | |
| 963 | /** |
| 964 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type DEC64 |
| 965 | * Example CHECK_LYD_VALUE(node->value, DEC64, "125", 125); |
| 966 | * @param[in] NODE lyd_value variable |
| 967 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 968 | * @param[in] VALUE expected value 64bit inteager |
| 969 | */ |
| 970 | #define CHECK_LYD_VALUE_DEC64(NODE, CANNONICAL_VAL, VALUE) \ |
| 971 | assert_non_null((NODE).canonical); \ |
| 972 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 973 | assert_non_null((NODE).realtype); \ |
| 974 | assert_int_equal(LY_TYPE_DEC64, (NODE).realtype->basetype); \ |
| 975 | assert_int_equal(VALUE, (NODE).dec64); |
| 976 | |
| 977 | /** |
| 978 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BINARY. |
| 979 | * Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8="); |
| 980 | * @param[in] NODE lyd_value variable |
| 981 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 982 | */ |
| 983 | #define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL) \ |
| 984 | assert_non_null((NODE).canonical); \ |
| 985 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 986 | assert_non_null((NODE).realtype); \ |
| 987 | assert_int_equal(LY_TYPE_BINARY, (NODE).realtype->basetype); |
| 988 | |
| 989 | /** |
| 990 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type BOOL. |
| 991 | * Example CHECK_LYD_VALUE(node->value, BOOL, "true", 1); |
| 992 | * @param[in] NODE lyd_value variable |
| 993 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 994 | * @param[in] VALUE expected boolean value 0,1 |
| 995 | */ |
| 996 | #define CHECK_LYD_VALUE_BOOL(NODE, CANNONICAL_VAL, VALUE) \ |
| 997 | assert_non_null((NODE).canonical); \ |
| 998 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 999 | assert_non_null((NODE).realtype); \ |
| 1000 | assert_int_equal(LY_TYPE_BOOL, (NODE).realtype->basetype); \ |
| 1001 | assert_int_equal(VALUE, (NODE).boolean); |
| 1002 | |
| 1003 | /** |
| 1004 | * @brief Internal macro. Assert that lyd_value structure members are correct. Lyd value is type IDENT. |
| 1005 | * Example CHECK_LYD_VALUE(node->value, IDENT, "types:gigabit-ethernet", "gigabit-ethernet"); |
| 1006 | * @param[in] NODE lyd_value variable |
| 1007 | * @param[in] CANNONICAL_VAL expected cannonical value |
| 1008 | * @param[in] VALUE expected ident name |
| 1009 | */ |
| 1010 | #define CHECK_LYD_VALUE_IDENT(NODE, CANNONICAL_VAL, VALUE) \ |
| 1011 | assert_non_null((NODE).canonical); \ |
| 1012 | assert_string_equal((NODE).canonical, CANNONICAL_VAL); \ |
| 1013 | assert_non_null((NODE).realtype); \ |
| 1014 | assert_int_equal(LY_TYPE_IDENT, (NODE).realtype->basetype); \ |
| 1015 | assert_string_equal(VALUE, (NODE).ident->name); |
| 1016 | |
| 1017 | /** |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 1018 | * @brief Macro testing parser when parsing incorrect module; |
| 1019 | * @param[in] DATA String storing the schema module representation. |
| 1020 | * @param[in] FORMAT Schema format of the @p DATA |
| 1021 | * @param[in] FEATURES Array of module's features to enable |
| 1022 | * @param[in] RET_VAL ly_in_new_memory return error value |
| 1023 | */ |
| 1024 | #define UTEST_INVALID_MODULE(DATA, FORMAT, FEATURES, RET_VAL) \ |
| 1025 | { \ |
| 1026 | const struct lys_module *mod; \ |
| 1027 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \ |
| 1028 | assert_int_equal(RET_VAL, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, &mod)); \ |
| 1029 | assert_null(mod); \ |
| 1030 | } \ |
| 1031 | ly_in_free(_UC->in, 0); \ |
| 1032 | _UC->in = NULL; \ |
| 1033 | |
| 1034 | /** |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1035 | * @brief Add module (from a string) into the used libyang context. |
| 1036 | * @param[in] DATA String storing the schema module representation. |
| 1037 | * @param[in] FORMAT Schema format of the @p DATA |
| 1038 | * @param[in] FEATURES Array of module's features to enable |
| 1039 | * @param[out] MOD Optional parameter as a pointer to variable to store the resulting module. |
| 1040 | */ |
| 1041 | #define UTEST_ADD_MODULE(DATA, FORMAT, FEATURES, MOD) \ |
| 1042 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(DATA, &_UC->in)); \ |
| 1043 | assert_int_equal(LY_SUCCESS, lys_parse(_UC->ctx, _UC->in, FORMAT, FEATURES, MOD)); \ |
| 1044 | ly_in_free(_UC->in, 0); \ |
| 1045 | _UC->in = NULL |
| 1046 | |
| 1047 | /** |
| 1048 | * @brief Internal macro to compare error info record with the expected error message and path. |
| 1049 | * If NULL is provided as MSG, no error info record (NULL) is expected. |
| 1050 | * @param[in] ERR Error information record from libyang context. |
| 1051 | * @param[in] MSG Expected error message. |
| 1052 | * @param[in] PATH Expected error path. |
| 1053 | * |
| 1054 | */ |
| 1055 | #define _CHECK_LOG_CTX(ERR, MSG, PATH) \ |
| 1056 | if (!MSG) { \ |
| 1057 | assert_null(ERR); \ |
| 1058 | } else { \ |
| 1059 | assert_non_null(ERR); \ |
| 1060 | CHECK_STRING((ERR)->msg, MSG); \ |
| 1061 | CHECK_STRING((ERR)->path, PATH); \ |
| 1062 | } |
| 1063 | |
| 1064 | /**` |
| 1065 | * @brief Internal macro to check the last libyang's context error. |
| 1066 | */ |
| 1067 | #define _CHECK_LOG_CTX1(MSG, PATH) \ |
| 1068 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG, PATH) |
| 1069 | |
| 1070 | /** |
| 1071 | * @brief Internal macro to check the last two libyang's context error. |
| 1072 | */ |
| 1073 | #define _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2) \ |
| 1074 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx), MSG1, PATH1); \ |
| 1075 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev, MSG2, PATH2) |
| 1076 | |
| 1077 | /** |
| 1078 | * @brief Internal macro to check the last three libyang's context error. |
| 1079 | */ |
| 1080 | #define _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3) \ |
| 1081 | _CHECK_LOG_CTX2(MSG1, PATH1, MSG2, PATH2); \ |
| 1082 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev, MSG3, PATH3) |
| 1083 | |
| 1084 | /** |
Radek Krejci | fe6ec26 | 2021-01-20 10:28:28 +0100 | [diff] [blame] | 1085 | * @brief Internal macro to check the last three libyang's context error. |
| 1086 | */ |
| 1087 | #define _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4) \ |
| 1088 | _CHECK_LOG_CTX3(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3); \ |
| 1089 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev, MSG4, PATH4) |
| 1090 | |
| 1091 | /** |
| 1092 | * @brief Internal macro to check the last three libyang's context error. |
| 1093 | */ |
| 1094 | #define _CHECK_LOG_CTX5(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4, MSG5, PATH5) \ |
| 1095 | _CHECK_LOG_CTX4(MSG1, PATH1, MSG2, PATH2, MSG3, PATH3, MSG4, PATH4); \ |
| 1096 | _CHECK_LOG_CTX(ly_err_last(_UC->ctx)->prev->prev->prev->prev, MSG5, PATH5) |
| 1097 | |
| 1098 | /** |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1099 | * @brief Internal helper macro to select _CHECK_LOG_CTX* macro according to the provided parameters. |
| 1100 | */ |
Radek Krejci | fe6ec26 | 2021-01-20 10:28:28 +0100 | [diff] [blame] | 1101 | #define _GET_CHECK_LOG_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1102 | |
| 1103 | /** |
| 1104 | * @brief Check expected error(s) in libyang context. |
| 1105 | * |
| 1106 | * Macro has variadic parameters expected to be provided in pairs of error message and error path starting |
| 1107 | * from the latest error. Current limit is checking at most 3 last errors. After checking, macro cleans up |
| 1108 | * all the errors from the libyang context. |
| 1109 | * |
| 1110 | * @param[in] MSG Expected error message. |
| 1111 | * @param[in] PATH Expected error path. |
| 1112 | */ |
| 1113 | #define CHECK_LOG_CTX(...) \ |
Radek Krejci | fe6ec26 | 2021-01-20 10:28:28 +0100 | [diff] [blame] | 1114 | _GET_CHECK_LOG_MACRO(__VA_ARGS__, _CHECK_LOG_CTX5, _INVAL, _CHECK_LOG_CTX4, _INVAL, \ |
| 1115 | _CHECK_LOG_CTX3, _INVAL, _CHECK_LOG_CTX2, _INVAL, _CHECK_LOG_CTX1)(__VA_ARGS__); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1116 | ly_err_clean(_UC->ctx, NULL) |
| 1117 | |
| 1118 | /** |
| 1119 | * @brief Clean up the logging callback's storage. |
| 1120 | */ |
| 1121 | #define UTEST_LOG_CLEAN \ |
| 1122 | free(_UC->err_msg); \ |
| 1123 | free(_UC->err_path); \ |
| 1124 | _UC->err_msg = NULL; \ |
| 1125 | _UC->err_path = NULL; |
| 1126 | |
| 1127 | /** |
| 1128 | * @brief Check expected error directly logged via logging callback. |
| 1129 | * Useful mainly for messages logged by functions without access to libyang context. |
| 1130 | * @param[in] MSG Expected error message. |
| 1131 | * @param[in] PATH Expected error path. |
| 1132 | */ |
| 1133 | #define CHECK_LOG(MSG, PATH) \ |
| 1134 | CHECK_STRING(_UC->err_msg, MSG); \ |
| 1135 | CHECK_STRING(_UC->err_path, PATH); \ |
| 1136 | UTEST_LOG_CLEAN |
| 1137 | |
| 1138 | #ifdef _UTEST_MAIN_ |
| 1139 | /* |
| 1140 | * Functions inlined into each C source file including this header with _UTEST_MAIN_ defined |
| 1141 | */ |
| 1142 | |
| 1143 | /** |
| 1144 | * @brief Global variable holding the tests context to simplify access to it. |
| 1145 | */ |
| 1146 | struct utest_context *current_utest_context; |
| 1147 | |
| 1148 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 1149 | #define ENABLE_LOGGER_CHECKING 1 |
| 1150 | |
| 1151 | /** |
| 1152 | * @brief Logging callback for libyang. |
| 1153 | */ |
| 1154 | static void |
| 1155 | _utest_logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 1156 | { |
| 1157 | (void) level; /* unused */ |
| 1158 | |
| 1159 | if (ENABLE_LOGGER_CHECKING == 0) { |
Radek Iša | a9ff2b8 | 2021-01-13 21:44:13 +0100 | [diff] [blame] | 1160 | printf("\tERROR:\n\t\tMESSAGE: %s\n\t\tPATH: %s\n\t\tLEVEL: %i\n", msg, path, level); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1161 | } else { |
| 1162 | free(current_utest_context->err_msg); |
| 1163 | current_utest_context->err_msg = msg ? strdup(msg) : NULL; |
| 1164 | free(current_utest_context->err_path); |
| 1165 | current_utest_context->err_path = path ? strdup(path) : NULL; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * @brief Generic utest's setup |
| 1171 | */ |
| 1172 | static int |
| 1173 | utest_setup(void **state) |
| 1174 | { |
| 1175 | /* setup the logger */ |
| 1176 | ly_set_log_clb(_utest_logger, 1); |
| 1177 | ly_log_options(LY_LOLOG | LY_LOSTORE); |
| 1178 | |
| 1179 | current_utest_context = calloc(1, sizeof *current_utest_context); |
| 1180 | assert_non_null(current_utest_context); |
| 1181 | *state = current_utest_context; |
| 1182 | |
| 1183 | /* libyang context */ |
| 1184 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, ¤t_utest_context->ctx)); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * @brief macro to include generic utest's setup into the test-specific setup. |
| 1191 | * |
| 1192 | * Place at the beginning of the test-specific setup |
| 1193 | */ |
| 1194 | #define UTEST_SETUP \ |
| 1195 | assert_int_equal(0, utest_setup(state)) |
| 1196 | |
| 1197 | /** |
| 1198 | * @brief Generic utest's teardown |
| 1199 | */ |
| 1200 | static int |
| 1201 | utest_teardown(void **state) |
| 1202 | { |
| 1203 | *state = NULL; |
| 1204 | |
| 1205 | /* libyang context */ |
| 1206 | ly_ctx_destroy(current_utest_context->ctx, NULL); |
| 1207 | |
| 1208 | /* utest context */ |
| 1209 | ly_in_free(current_utest_context->in, 0); |
| 1210 | free(current_utest_context->err_msg); |
| 1211 | free(current_utest_context->err_path); |
| 1212 | free(current_utest_context); |
| 1213 | current_utest_context = NULL; |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * @brief macro to include generic utest's teardown into the test-specific teardown. |
| 1220 | * |
| 1221 | * Place at the end of the test-specific teardown |
| 1222 | */ |
| 1223 | #define UTEST_TEARDOWN \ |
| 1224 | assert_int_equal(0, utest_teardown(state)) |
| 1225 | |
| 1226 | /** |
| 1227 | * @brief Internal macro for utest setup with test-specific setup and teardown |
| 1228 | */ |
| 1229 | #define _UTEST_SETUP_TEARDOWN(FUNC, SETUP, TEARDOWN) \ |
| 1230 | cmocka_unit_test_setup_teardown(FUNC, SETUP, TEARDOWN) |
| 1231 | |
| 1232 | /** |
| 1233 | * @brief Internal macro for utest setup with test-specific setup and generic teardown |
| 1234 | */ |
| 1235 | #define _UTEST_SETUP(FUNC, SETUP) \ |
| 1236 | cmocka_unit_test_setup_teardown(FUNC, SETUP, utest_teardown) |
| 1237 | |
| 1238 | /** |
| 1239 | * @brief Internal macro for utest setup with generic setup and teardown |
| 1240 | */ |
| 1241 | #define _UTEST(FUNC) \ |
| 1242 | cmocka_unit_test_setup_teardown(FUNC, utest_setup, utest_teardown) |
| 1243 | |
| 1244 | /** |
| 1245 | * @brief Internal helper macro to select _UTEST* macro according to the provided parameters. |
| 1246 | */ |
| 1247 | #define _GET_UTEST_MACRO(_1, _2, _3, NAME, ...) NAME |
| 1248 | |
| 1249 | /** |
| 1250 | * @brief Macro to specify test function using utest environment. Macro has variadic parameters |
| 1251 | * to provide test-specific setup/teardown functions: |
| 1252 | * |
| 1253 | * UTEST(test_func) - only implicit setup and teardown functions are used |
| 1254 | * UTEST(test_func, setup) - implicit teardown but own setup |
| 1255 | * UTEST(test_func, setup, teardown) - both setup and teardown are test-specific |
| 1256 | */ |
| 1257 | #define UTEST(...) \ |
| 1258 | _GET_UTEST_MACRO(__VA_ARGS__, _UTEST_SETUP_TEARDOWN, _UTEST_SETUP, _UTEST)(__VA_ARGS__) |
| 1259 | |
| 1260 | #else /* _UTEST_MAIN_ */ |
| 1261 | |
| 1262 | extern struct utest_context *current_utest_context; |
| 1263 | |
| 1264 | #endif /* _UTEST_MAIN_ */ |
| 1265 | |
| 1266 | #endif /* _UTESTS_H_ */ |