/** * @file test_yin.c * @author David Sedlák * @author Michal Vasko * @brief unit tests for YIN parser and printer * * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. * * This source code is licensed under BSD 3-Clause License (the "License"). * You may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://opensource.org/licenses/BSD-3-Clause */ #define _UTEST_MAIN_ #include "utests.h" #include #include #include #include "in.h" #include "ly_common.h" #include "parser_internal.h" #include "schema_compile.h" #include "tree.h" #include "tree_edit.h" #include "tree_schema.h" #include "tree_schema_internal.h" #include "xml.h" #include "xpath.h" /* copied from parser_yin.c */ enum yin_argument { YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */ YIN_ARG_NAME, /**< argument name */ YIN_ARG_TARGET_NODE, /**< argument target-node */ YIN_ARG_MODULE, /**< argument module */ YIN_ARG_VALUE, /**< argument value */ YIN_ARG_TEXT, /**< argument text */ YIN_ARG_CONDITION, /**< argument condition */ YIN_ARG_URI, /**< argument uri */ YIN_ARG_DATE, /**< argument data */ YIN_ARG_TAG, /**< argument tag */ YIN_ARG_NONE /**< empty (special value) */ }; struct yin_subelement { enum ly_stmt type; /**< type of keyword */ void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */ uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */ }; struct import_meta { const char *prefix; /**< module prefix. */ struct lysp_import **imports; /**< imports to add to. */ }; struct yin_argument_meta { uint16_t *flags; /**< Argument flags */ const char **argument; /**< Argument value */ }; struct tree_node_meta { struct lysp_node *parent; /**< parent node */ struct lysp_node **nodes; /**< linked list of siblings */ }; struct include_meta { const char *name; /**< Module/submodule name. */ struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */ }; struct inout_meta { struct lysp_node *parent; /**< Parent node. */ struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */ }; struct minmax_dev_meta { uint32_t *lim; /**< min/max value to write to. */ uint16_t *flags; /**< min/max flags to write to. */ struct lysp_ext_instance **exts; /**< extension instances to add to. */ }; #define YIN_SUBELEM_MANDATORY 0x01 #define YIN_SUBELEM_UNIQUE 0x02 #define YIN_SUBELEM_FIRST 0x04 #define YIN_SUBELEM_VER2 0x08 #define YIN_SUBELEM_PARSED 0x80 /* prototypes of static functions */ enum yin_argument yin_match_argument_name(const char *name, size_t len); LY_ERR yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size, const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts); LY_ERR yin_validate_value(struct lysp_yin_ctx *ctx, enum yang_arg val_type); enum ly_stmt yin_match_keyword(struct lysp_yin_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, enum ly_stmt parrent); LY_ERR yin_parse_extension_instance(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt, LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts); LY_ERR yin_parse_element_generic(struct lysp_yin_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element); LY_ERR yin_parse_mod(struct lysp_yin_ctx *ctx, struct lysp_module *mod); LY_ERR yin_parse_submod(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod); /* wrapping element used for mocking has nothing to do with real module structure */ #define ELEMENT_WRAPPER_START "" #define ELEMENT_WRAPPER_END "" #define TEST_1_CHECK_LYSP_EXT_INSTANCE(NODE, INSUBSTMT)\ CHECK_LYSP_EXT_INSTANCE((NODE), NULL, 1, INSUBSTMT, 0, "myext:c-define", LY_VALUE_XML) struct lysp_yin_ctx *YCTX; struct lysf_ctx fctx; static int setup_ctx(void **state) { struct lysp_module *pmod; /* allocate parser context */ YCTX = calloc(1, sizeof(*YCTX)); YCTX->main_ctx = (struct lysp_ctx *)YCTX; YCTX->format = LYS_IN_YIN; ly_set_new(&YCTX->parsed_mods); /* allocate new parsed module */ pmod = calloc(1, sizeof *pmod); ly_set_add(YCTX->parsed_mods, pmod, 1, NULL); /* allocate new module */ pmod->mod = calloc(1, sizeof *pmod->mod); pmod->mod->ctx = UTEST_LYCTX; pmod->mod->parsed = pmod; return 0; } static int setup(void **state) { UTEST_SETUP; setup_ctx(state); fctx.ctx = UTEST_LYCTX; fctx.mod = PARSER_CUR_PMOD(YCTX)->mod; return 0; } static int teardown_ctx(void **UNUSED(state)) { lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0); lysp_yin_ctx_free(YCTX); YCTX = NULL; return 0; } static int teardown(void **state) { teardown_ctx(state); lysf_ctx_erase(&fctx); UTEST_TEARDOWN; return 0; } #define RESET_STATE \ ly_in_free(UTEST_IN, 0); \ UTEST_IN = NULL; \ teardown_ctx(state); \ setup_ctx(state) static void test_yin_match_keyword(void **state) { const char *prefix; size_t prefix_len; /* create mock yin namespace in xml context */ ly_in_new_memory("", &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); prefix = YCTX->xmlctx->prefix; prefix_len = YCTX->xmlctx->prefix_len; assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE); assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE); assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE); assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA); assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML); assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT); assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT); assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE); assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO); assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT); assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE); assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE); assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG); assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT); assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER); assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT); assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION); assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE); assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION); assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM); assert_int_equal(yin_match_keyword(YCTX, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_APP_TAG); assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE); assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION); assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE); assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS); assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING); assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY); assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE); assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT); assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE); assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT); assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY); assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF); assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST); assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH); assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST); assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY); assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS); assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS); assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER); assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE); assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST); assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE); assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION); assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY); assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION); assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT); assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH); assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN); assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION); assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX); assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE); assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE); assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE); assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE); assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE); assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION); assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE); assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC); assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS); assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE); assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE); assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF); assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE); assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS); assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES); assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE); assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN); assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION); assert_int_equal(yin_match_keyword(YCTX, "yin-element", strlen("yin-element"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YIN_ELEMENT); } static void test_yin_match_argument_name(void **UNUSED(state)) { assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN); assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION); assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE); assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE); assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME); assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG); assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT); assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI); assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE); } static void test_yin_parse_content(void **state) { LY_ERR ret = LY_SUCCESS; const char *data = "\n" " totally amazing extension\n" " \n" " \n" " desc\n" " ref\n" " \n" " \n" " wsefsdf\n" " \n" " \n" " when_ref\n" " when_desc\n" " \n" " \n" " \n" " error-msg\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""; struct lysp_ext_instance *exts = NULL; const char *value; /* test unique subelem */ const char *prefix_value; struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE}, {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}}; data = ELEMENT_WRAPPER_START "" "wsefsdf" "wsefsdf" ELEMENT_WRAPPER_END; ly_in_new_memory(data, &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); lyxml_ctx_next(YCTX->xmlctx); ret = yin_parse_content(YCTX, subelems2, 2, NULL, LY_STMT_STATUS, NULL, &exts); assert_int_equal(ret, LY_EVALID); CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", NULL, 1); lydict_remove(UTEST_LYCTX, prefix_value); lydict_remove(UTEST_LYCTX, value); RESET_STATE; /* test first subelem */ data = ELEMENT_WRAPPER_START "" "wsefsdf" "wsefsdf" ELEMENT_WRAPPER_END; struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE}, {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}}; ly_in_new_memory(data, &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); lyxml_ctx_next(YCTX->xmlctx); ret = yin_parse_content(YCTX, subelems3, 2, NULL, LY_STMT_STATUS, NULL, &exts); assert_int_equal(ret, LY_EVALID); CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", NULL, 1); lydict_remove(UTEST_LYCTX, prefix_value); RESET_STATE; /* test mandatory subelem */ data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END; struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}}; ly_in_new_memory(data, &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); lyxml_ctx_next(YCTX->xmlctx); ret = yin_parse_content(YCTX, subelems4, 1, NULL, LY_STMT_STATUS, NULL, &exts); assert_int_equal(ret, LY_EVALID); CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", NULL, 1); } static void test_validate_value(void **state) { const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END; /* create some XML context */ ly_in_new_memory(data, &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); YCTX->xmlctx->status = LYXML_ELEM_CONTENT; YCTX->xmlctx->dynamic = 0; YCTX->xmlctx->value = "#invalid"; YCTX->xmlctx->value_len = 8; assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID); CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", NULL, 1); YCTX->xmlctx->value = ""; YCTX->xmlctx->value_len = 0; assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS); YCTX->xmlctx->value = "pre:b"; YCTX->xmlctx->value_len = 5; assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID); CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", NULL, 1); assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS); YCTX->xmlctx->value = "pre:pre:b"; YCTX->xmlctx->value_len = 9; assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID); CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", NULL, 1); } static void test_valid_module(void **state) { struct lys_module *mod; char *printed; const char *links_yin = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n"; const char *statements_yin = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n"; UTEST_ADD_MODULE(links_yin, LYS_IN_YIN, NULL, NULL); UTEST_ADD_MODULE(statements_yin, LYS_IN_YIN, NULL, &mod); lys_print_mem(&printed, mod, LYS_OUT_YIN, 0); assert_string_equal(printed, statements_yin); free(printed); } static void test_print_module(void **state) { struct lys_module *mod; char *orig = malloc(8096); strcpy(orig, "module all {\n" " yang-version 1.1;\n" " namespace \"urn:all\";\n" " prefix all_mod;\n\n" " import ietf-yang-types {\n" " prefix yt;\n" " revision-date 2013-07-15;\n" " description\n" " \"YANG types\";\n" " reference\n" " \"RFC reference\";\n" " }\n\n" " feature feat1 {\n" " if-feature \"feat2\";\n" " status obsolete;\n" " }\n\n" " feature feat2;\n" " feature feat3;\n\n" " identity ident2 {\n" " base ident1;\n" " }\n\n" " identity ident1;\n\n" " typedef tdef1 {\n" " type tdef2 {\n" " length \"3..9 | 30..40\";\n" " pattern \"[ac]*\";\n" " }\n" " units \"none\";\n" " default \"aaa\";\n" " }\n\n" " typedef tdef2 {\n" " type string {\n" " length \"2..10 | 20..50\";\n" " pattern \"[ab]*\";\n" " }\n" " }\n\n" " grouping group1 {\n" " leaf leaf1 {\n" " type int8;\n" " }\n" " }\n\n" " container cont1 {\n" " leaf leaf2 {\n" " if-feature \"feat1\";\n" " type int16;\n" " status obsolete;\n" " }\n\n" " uses group1 {\n" " if-feature \"feat2\";\n" " refine \"leaf1\" {\n" " if-feature \"feat3\";\n" " must \"24 - 4 = number('20')\";\n" " default \"25\";\n" " config true;\n" " mandatory false;\n" " description\n" " \"dsc\";\n" " reference\n" " \"none\";\n" " }\n" " }\n\n" " leaf leaf3 {\n" " type int32;\n" " }\n\n" " leaf leaf4 {\n" " type int64 {\n" " range \"1000 .. 50000\" {\n" " error-message\n" " \"Special error message.\";\n" " error-app-tag \"special-tag\";\n" " }\n" " }\n" " }\n\n" " leaf leaf5 {\n" " type uint8;\n" " }\n\n" " leaf leaf6 {\n" " type uint16;\n" " }\n\n" " leaf leaf7 {\n" " type uint32;\n" " }\n\n" " leaf leaf8 {\n" " type uint64;\n" " }\n\n" " choice choic1 {\n" " default \"leaf9b\";\n" " leaf leaf9a {\n" " type decimal64 {\n" " fraction-digits 9;\n" " }\n" " }\n\n" " leaf leaf9b {\n" " type boolean;\n" " default \"false\";\n" " }\n" " }\n\n" " leaf leaf10 {\n" " type boolean;\n" " }\n\n"); strcpy(orig + strlen(orig), " leaf leaf11 {\n" " type enumeration {\n" " enum \"one\";\n" " enum \"two\";\n" " enum \"five\" {\n" " value 5;\n" " }\n" " }\n" " }\n\n" " leaf leaf12 {\n" " type bits {\n" " bit flag0 {\n" " position 0;\n" " }\n" " bit flag1;\n" " bit flag2 {\n" " position 2;\n" " }\n" " bit flag3 {\n" " position 3;\n" " }\n" " }\n" " default \"flag0 flag3\";\n" " }\n\n" " leaf leaf13 {\n" " type binary;\n" " }\n\n" " leaf leaf14 {\n" " type leafref {\n" " path \"/cont1/leaf17\";\n" " }\n" " }\n\n" " leaf leaf15 {\n" " type empty;\n" " }\n\n" " leaf leaf16 {\n" " type union {\n" " type instance-identifier {\n" " require-instance true;\n" " }\n" " type int8;\n" " }\n" " }\n\n" " list list1 {\n" " key \"leaf18\";\n" " unique \"leaf19\";\n" " min-elements 1;\n" " max-elements 20;\n" " leaf leaf18 {\n" " type string;\n" " }\n\n" " leaf leaf19 {\n" " type uint32;\n" " }\n\n" " anyxml axml1;\n" " anydata adata1;\n\n" " action act1 {\n" " input {\n" " leaf leaf24 {\n" " type string;\n" " }\n" " }\n\n" " output {\n" " leaf leaf25 {\n" " type string;\n" " }\n" " }\n" " }\n\n" " notification notif1 {\n" " leaf leaf26 {\n" " type string;\n" " }\n" " }\n" " }\n\n" " leaf-list llist1 {\n" " type tdef1;\n" " ordered-by user;\n" " }\n\n" " list list2 {\n" " key \"leaf27 leaf28\";\n" " leaf leaf27 {\n" " type uint8;\n" " }\n\n" " leaf leaf28 {\n" " type uint8;\n" " }\n" " }\n\n" " leaf leaf29 {\n" " type instance-identifier;\n" " }\n\n" " container must-deviations-container {\n" " presence \"Allows deviations on the leaf\";\n" " leaf leaf30 {\n" " type string;\n" " }\n" " }\n\n" " leaf leaf23 {\n" " type empty;\n" " }\n" " }\n\n" " augment \"/cont1\" {\n" " leaf leaf17 {\n" " type string;\n" " }\n" " }\n\n" " rpc rpc1 {\n" " input {\n" " leaf leaf20 {\n" " type tdef1;\n" " }\n" " }\n\n" " output {\n" " container cont2 {\n" " leaf leaf21 {\n" " type empty;\n" " }\n" " }\n" " }\n" " }\n\n" " container test-when {\n" " leaf when-check {\n" " type boolean;\n" " }\n\n" " leaf gated-data {\n" " when \"../when-check = 'true'\";\n" " type uint16;\n" " }\n" " }\n\n" " extension c-define {\n" " description\n" " \"Takes as an argument a name string.\n" " Makes the code generator use the given name\n" " in the #define.\";\n" " argument \"name\";\n" " }\n" "}\n"); char *ori_res = malloc(8096); strcpy(ori_res, "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " YANG types\n" " \n" " \n" " RFC reference\n" " \n" " \n" " \n" " \n" " \n" " Takes as an argument a name string.\n" "Makes the code generator use the given name\n" "in the #define.\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " dsc\n" " \n" " \n" " none\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " Special error message.\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n"); strcpy(ori_res + strlen(ori_res), " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "\n"); char *printed; struct ly_out *out; assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod); assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YIN, 0, 0)); assert_int_equal(strlen(ori_res), ly_out_printed(out)); assert_string_equal(printed, ori_res); ly_out_free(out, NULL, 1); free(orig); free(ori_res); } static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) { *module_data = user_data; *format = LYS_IN_YIN; *free_module_data = NULL; return LY_SUCCESS; } static void test_print_submodule(void **state) { struct lys_module *mod; const char *mod_yin = "\n" "\n" " \n" " \n" " \n" " \n" "\n"; char *submod_yin = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n\n" " \n" " YANG types\n" " \n" " \n" " RFC reference\n" " \n" "\n"; char *printed; struct ly_out *out; assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yin); UTEST_ADD_MODULE(mod_yin, LYS_IN_YIN, NULL, &mod); assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YIN, 0, 0)); assert_int_equal(strlen(submod_yin), ly_out_printed(out)); assert_string_equal(printed, submod_yin); ly_out_free(out, NULL, 1); } /* helper function to simplify unit test of each element using parse_content function */ LY_ERR test_element_helper(void **state, const char *data, void *dest, const char **text, struct lysp_ext_instance **exts) { const char *name, *prefix; size_t name_len, prefix_len; LY_ERR ret = LY_SUCCESS; struct yin_subelement subelems[71] = { {LY_STMT_ACTION, dest, 0}, {LY_STMT_ANYDATA, dest, 0}, {LY_STMT_ANYXML, dest, 0}, {LY_STMT_ARGUMENT, dest, 0}, {LY_STMT_AUGMENT, dest, 0}, {LY_STMT_BASE, dest, 0}, {LY_STMT_BELONGS_TO, dest, 0}, {LY_STMT_BIT, dest, 0}, {LY_STMT_CASE, dest, 0}, {LY_STMT_CHOICE, dest, 0}, {LY_STMT_CONFIG, dest, 0}, {LY_STMT_CONTACT, dest, 0}, {LY_STMT_CONTAINER, dest, 0}, {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_DESCRIPTION, dest, 0}, {LY_STMT_DEVIATE, dest, 0}, {LY_STMT_DEVIATION, dest, 0}, {LY_STMT_ENUM, dest, 0}, {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_ERROR_MESSAGE, dest, 0}, {LY_STMT_EXTENSION, dest, 0}, {LY_STMT_FEATURE, dest, 0}, {LY_STMT_FRACTION_DIGITS, dest, 0}, {LY_STMT_GROUPING, dest, 0}, {LY_STMT_IDENTITY, dest, 0}, {LY_STMT_IF_FEATURE, dest, 0}, {LY_STMT_IMPORT, dest, 0}, {LY_STMT_INCLUDE, dest, 0}, {LY_STMT_INPUT, dest, 0}, {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_LEAF, dest, 0}, {LY_STMT_LEAF_LIST, dest, 0}, {LY_STMT_LENGTH, dest, 0}, {LY_STMT_LIST, dest, 0}, {LY_STMT_MANDATORY, dest, 0}, {LY_STMT_MAX_ELEMENTS, dest, 0}, {LY_STMT_MIN_ELEMENTS, dest, 0}, {LY_STMT_MODIFIER, dest, 0}, {LY_STMT_MODULE, dest, 0}, {LY_STMT_MUST, dest, 0}, {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_NOTIFICATION, dest, 0}, {LY_STMT_ORDERED_BY, dest, 0}, {LY_STMT_ORGANIZATION, dest, 0}, {LY_STMT_OUTPUT, dest, 0}, {LY_STMT_PATH, dest, 0}, {LY_STMT_PATTERN, dest, 0}, {LY_STMT_POSITION, dest, 0}, {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_RANGE, dest, 0}, {LY_STMT_REFERENCE, dest, 0}, {LY_STMT_REFINE, dest, 0}, {LY_STMT_REQUIRE_INSTANCE, dest, 0}, {LY_STMT_REVISION, dest, 0}, {LY_STMT_REVISION_DATE, dest, 0}, {LY_STMT_RPC, dest, 0}, {LY_STMT_STATUS, dest, 0}, {LY_STMT_SUBMODULE, dest, 0}, {LY_STMT_TYPE, dest, 0}, {LY_STMT_TYPEDEF, dest, 0}, {LY_STMT_UNIQUE, dest, 0}, {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE}, {LY_STMT_USES, dest, 0}, {LY_STMT_VALUE, dest, 0}, {LY_STMT_WHEN, dest, 0}, {LY_STMT_YANG_VERSION, dest, 0}, {LY_STMT_YIN_ELEMENT, dest, 0}, {LY_STMT_EXTENSION_INSTANCE, dest, 0}, {LY_STMT_ARG_TEXT, dest, 0}, {LY_STMT_ARG_VALUE, dest, 0} }; YCTX->main_ctx = (struct lysp_ctx *)YCTX; ly_in_new_memory(data, &UTEST_IN); lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx); prefix = YCTX->xmlctx->prefix; prefix_len = YCTX->xmlctx->prefix_len; name = YCTX->xmlctx->name; name_len = YCTX->xmlctx->name_len; lyxml_ctx_next(YCTX->xmlctx); ret = yin_parse_content(YCTX, subelems, 71, NULL, yin_match_keyword(YCTX, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts); /* free parser and input */ lyxml_ctx_free(YCTX->xmlctx); YCTX->xmlctx = NULL; ly_in_free(UTEST_IN, 0); UTEST_IN = NULL; return ret; } #define EXT_SUBELEM "" static void test_enum_elem(void **state) { struct lysp_type type = {0}; const char *data; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " desc...\n" " ref...\n" " " EXT_SUBELEM "\n" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE; CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55); assert_string_equal(type.enums->iffeatures[0].str, "feature"); TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof type); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); assert_string_equal(type.enums->name, "enum-name"); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof type); } static void test_bit_elem(void **state) { struct lysp_type type = {0}; const char *data; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " desc...\n" " ref...\n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE; CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55); assert_string_equal(type.bits->iffeatures[0].str, "feature"); TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof type); data = ELEMENT_WRAPPER_START " " ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof type); } static void test_status_elem(void **state) { const char *data; uint16_t flags = 0; /* test invalid value */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. " "Valid values are \"current\", \"deprecated\" and \"obsolete\".", NULL, 1); } static void test_yin_element_elem(void **state) { const char *data; uint16_t flags = 0; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. " "Valid values are \"true\" and \"false\".", NULL, 1); } static void test_yangversion_elem(void **state) { const char *data; uint8_t version = 0; /* invalid value */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. " "Valid values are \"1\" and \"1.1\".", NULL, 1); } static void test_argument_elem(void **state) { const char *data; uint16_t flags = 0; const char *arg; struct yin_argument_meta arg_meta = {&flags, &arg}; /* min subelems */ data = ELEMENT_WRAPPER_START "" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(arg, "arg"); assert_true(flags == 0); lydict_remove(UTEST_LYCTX, arg); } static void test_belongsto_elem(void **state) { const char *data; struct lysp_submodule submod; lydict_insert(UTEST_LYCTX, "module-name", 0, &PARSER_CUR_PMOD(YCTX)->mod->name); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", NULL, 1); } static void test_config_elem(void **state) { const char *data; uint16_t flags = 0; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS); assert_true(flags & LYS_CONFIG_R); flags = 0; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. " "Valid values are \"true\" and \"false\".", NULL, 1); } static void test_default_elem(void **state) { const char *data; struct lysp_qname val = {0}; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute value of default element.", NULL, 1); } static void test_err_app_tag_elem(void **state) { const char *data; const char *val = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", NULL, 1); } static void test_err_msg_elem(void **state) { const char *data; const char *val = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", NULL, 1); } static void test_fracdigits_elem(void **state) { const char *data; struct lysp_type type = {0}; /* invalid values */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", NULL, 1); } static void test_iffeature_elem(void **state) { const char *data; const char **iffeatures = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", NULL, 1); LY_ARRAY_FREE(iffeatures); iffeatures = NULL; } static void test_length_elem(void **state) { const char *data; struct lysp_type type = {0}; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " err-msg\n" " \n" " desc\n" " ref\n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_RESTR(type.length, "length-str", "desc", "err-app-tag", "err-msg", 1, "ref"); assert_true(type.flags & LYS_SET_LENGTH); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); /* min subelems */ data = ELEMENT_WRAPPER_START "" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_RESTR(type.length, "length-str", NULL, NULL, NULL, 0, NULL); lysp_type_free(&fctx, &type); assert_true(type.flags & LYS_SET_LENGTH); memset(&type, 0, sizeof(type)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute value of length element.", NULL, 1); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); } static void test_modifier_elem(void **state) { const char *data; const char *pat; assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. " "Only valid value is \"invert-match\".", NULL, 1); lydict_remove(UTEST_LYCTX, pat); } static void test_namespace_elem(void **state) { const char *data; const char *ns; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", NULL, 1); } static void test_pattern_elem(void **state) { const char *data; struct lysp_type type = {0}; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " err-msg-value\n" " \n" " "pattern-desc"\n" " pattern-ref\n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); assert_true(type.flags & LYS_SET_PATTERN); CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"", "err-app-tag-value", "err-msg-value", 1, "pattern-ref"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); /* min subelems */ data = ELEMENT_WRAPPER_START " " ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); } static void test_value_position_elem(void **state) { const char *data; struct lysp_type_enum en = {0}; /* valid values */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS); CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55); memset(&en, 0, sizeof(en)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS); CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0); memset(&en, 0, sizeof(en)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS); CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0); memset(&en, 0, sizeof(en)); /* valid positions */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS); CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0); memset(&en, 0, sizeof(en)); /* invalid values */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", NULL, 1); /*invalid positions */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", NULL, 1); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", NULL, 1); } static void test_prefix_elem(void **state) { const char *data; const char *value = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS); assert_string_equal(value, "pref"); lydict_remove(UTEST_LYCTX, value); } static void test_range_elem(void **state) { const char *data; struct lysp_type type = {0}; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " err-msg\n" " \n" " desc\n" " ref\n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_RESTR(type.range, "range-str", "desc", "err-app-tag", "err-msg", 1, "ref"); assert_true(type.flags & LYS_SET_RANGE); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); CHECK_LYSP_RESTR(type.range, "range-str", NULL, NULL, NULL, 0, NULL); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); } static void test_reqinstance_elem(void **state) { const char *data; struct lysp_type type = {0}; data = ELEMENT_WRAPPER_START "" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); assert_int_equal(type.require_instance, 1); assert_true(type.flags & LYS_SET_REQINST); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); assert_int_equal(type.require_instance, 0); assert_true(type.flags & LYS_SET_REQINST); memset(&type, 0, sizeof(type)); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID); memset(&type, 0, sizeof(type)); CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. " "Valid values are \"true\" and \"false\".", NULL, 1); } static void test_revision_date_elem(void **state) { const char *data; char rev[LY_REV_SIZE]; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS); assert_string_equal(rev, "2000-01-01"); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", NULL, 1); } static void test_unique_elem(void **state) { const char *data; const char **values = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS); assert_string_equal(*values, "tag"); lydict_remove(UTEST_LYCTX, *values); LY_ARRAY_FREE(values); values = NULL; } static void test_units_elem(void **state) { const char *data; const char *values = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS); assert_string_equal(values, "name"); lydict_remove(UTEST_LYCTX, values); values = NULL; } static void test_yin_text_value_elem(void **state) { const char *data; const char *val; data = ELEMENT_WRAPPER_START "text" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS); assert_string_equal(val, "text"); lydict_remove(UTEST_LYCTX, val); data = " text "; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS); assert_string_equal(val, "text"); lydict_remove(UTEST_LYCTX, val); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS); assert_string_equal("", val); lydict_remove(UTEST_LYCTX, val); } static void test_type_elem(void **state) { const char *data; struct lysp_type type = {0}; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); assert_string_equal(type.name, "type-name"); assert_string_equal(*type.bases, "base-name"); assert_string_equal(type.bits->name, "bit"); assert_string_equal(type.enums->name, "enum"); assert_int_equal(type.fraction_digits, 2); CHECK_LYSP_RESTR(type.length, "length", NULL, NULL, NULL, 0, NULL); assert_string_equal(type.path->expr, "/path"); CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL, NULL, NULL, 0, NULL); CHECK_LYSP_RESTR(type.range, "range", NULL, NULL, NULL, 0, NULL); assert_int_equal(type.require_instance, 1); assert_string_equal(type.types->name, "sub-type-name"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_TYPE); assert_true(type.flags & LYS_SET_BASE); assert_true(type.flags & LYS_SET_BIT); assert_true(type.flags & LYS_SET_ENUM); assert_true(type.flags & LYS_SET_FRDIGITS); assert_true(type.flags & LYS_SET_LENGTH); assert_true(type.flags & LYS_SET_PATH); assert_true(type.flags & LYS_SET_PATTERN); assert_true(type.flags & LYS_SET_RANGE); assert_true(type.flags & LYS_SET_REQINST); assert_true(type.flags & LYS_SET_TYPE); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS); lysp_type_free(&fctx, &type); memset(&type, 0, sizeof(type)); } static void test_max_elems_elem(void **state) { const char *data; struct lysp_node_list list = {0}; struct lysp_refine refine = {0}; data = " "; assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS); assert_int_equal(refine.max, 10); assert_true(refine.flags & LYS_SET_MAX); data = " "; assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", NULL, 1); } static void test_min_elems_elem(void **state) { const char *data; struct lysp_node_leaflist llist = {0}; data = " "; assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Value \"-5\" of \"value\" attribute in \"min-elements\" element is out of bounds.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Value \"99999999999999999\" of \"value\" attribute in \"min-elements\" element is out of bounds.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", NULL, 1); data = " "; assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", NULL, 1); } static void test_ordby_elem(void **state) { const char *data; uint16_t flags = 0; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS); assert_true(flags & LYS_ORDBY_USER); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. " "Valid values are \"system\" and \"user\".", NULL, 1); } static void test_any_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings}; struct lysp_node_anydata *parsed = NULL; uint16_t flags; /* anyxml max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " desc\n" " \n" " \n" " \n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_anydata *)siblings; flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "any-name", 0, LYS_ANYXML, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML); lysp_node_free(&fctx, siblings); siblings = NULL; /* anydata max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " desc\n" " \n" " \n" " \n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_anydata *)siblings; flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "any-name", 0, LYS_ANYDATA, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA); lysp_node_free(&fctx, siblings); siblings = NULL; /* min subelems */ node_meta.parent = (void *)0x10; data = ELEMENT_WRAPPER_START " " ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_anydata *)siblings; assert_ptr_equal(parsed->parent, node_meta.parent); CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0, "any-name", 0, LYS_ANYDATA, 1, NULL, 0); lysp_node_free(&fctx, siblings); } static void test_leaf_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings}; struct lysp_node_leaf *parsed = NULL; uint16_t flags; /* max elements */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " desc\n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaf *)siblings; flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "leaf", 0, LYS_LEAF, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF); assert_string_equal(parsed->musts->arg.str, "must-cond"); assert_string_equal(parsed->type.name, "type"); assert_string_equal(parsed->units, "uni"); assert_string_equal(parsed->dflt.str, "def-val"); lysp_node_free(&fctx, siblings); siblings = NULL; /* min elements */ data = ELEMENT_WRAPPER_START " " ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaf *)siblings; assert_string_equal(parsed->name, "leaf"); assert_string_equal(parsed->type.name, "type"); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_leaf_list_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings}; struct lysp_node_leaflist *parsed = NULL; uint16_t flags; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaflist *)siblings; flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "llist", 0, LYS_LEAFLIST, 0, "ref", 1); CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL); assert_string_equal(parsed->dflts[0].str, "def-val0"); assert_string_equal(parsed->dflts[1].str, "def-val1"); assert_string_equal(parsed->iffeatures[0].str, "feature"); assert_int_equal(parsed->max, 5); assert_string_equal(parsed->type.name, "type"); assert_string_equal(parsed->units, "uni"); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST); lysp_node_free(&fctx, siblings); siblings = NULL; data = ELEMENT_WRAPPER_START "\n" " \n" " desc\n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaflist *)siblings; flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "llist", 0, LYS_LEAFLIST, 0, "ref", 1); CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); assert_int_equal(parsed->min, 5); assert_string_equal(parsed->type.name, "type"); assert_string_equal(parsed->units, "uni"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST); lysp_node_free(&fctx, siblings); siblings = NULL; data = ELEMENT_WRAPPER_START "\n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaflist *)siblings; flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX; CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1, "llist", 0, LYS_LEAFLIST, 0, "ref", 1); CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); assert_int_equal(parsed->min, 5); assert_int_equal(parsed->max, 15); assert_string_equal(parsed->type.name, "type"); assert_string_equal(parsed->units, "uni"); lysp_node_free(&fctx, siblings); siblings = NULL; data = ELEMENT_WRAPPER_START "\n" " \n" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_leaflist *)siblings; assert_string_equal(parsed->name, "llist"); assert_string_equal(parsed->type.name, "type"); lysp_node_free(&fctx, siblings); siblings = NULL; /* invalid combinations */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " " "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5.", NULL, 4); lysp_node_free(&fctx, siblings); siblings = NULL; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element.", NULL, 5); lysp_node_free(&fctx, siblings); siblings = NULL; data = ELEMENT_WRAPPER_START "" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", NULL, 1); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_presence_elem(void **state) { const char *data; const char *val; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS); assert_string_equal(val, "presence-val"); lydict_remove(UTEST_LYCTX, val); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", NULL, 1); } static void test_key_elem(void **state) { const char *data; const char *val; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS); assert_string_equal(val, "key-value"); lydict_remove(UTEST_LYCTX, val); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory attribute value of key element.", NULL, 1); } static void test_uses_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {NULL, &siblings}; struct lysp_node_uses *parsed = NULL; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " desc\n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_uses *)&siblings[0]; CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1, "uses-name", 0, LYS_USES, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "feature"); assert_string_equal(parsed->refines->nodeid, "target"); assert_string_equal(parsed->augments->nodeid, "target"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES); lysp_node_free(&fctx, siblings); siblings = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(siblings[0].name, "uses-name"); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_list_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {NULL, &siblings}; struct lysp_node_list *parsed = NULL; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " desc\n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_list *)&siblings[0]; assert_string_equal(parsed->child->name, "anyd"); assert_int_equal(parsed->child->nodetype, LYS_ANYDATA); assert_string_equal(parsed->child->next->name, "anyx"); assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML); assert_string_equal(parsed->child->next->next->name, "cont"); assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER); assert_string_equal(parsed->child->next->next->next->name, "choice"); assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE); assert_string_equal(parsed->child->next->next->next->next->name, "leaf"); assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(parsed->child->next->next->next->next->next->name, "llist"); assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list"); assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(parsed->child->next->next->next->next->next->next->next->next); uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "list-name", 0, LYS_LIST, 0, "ref", 1); CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL); CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL); assert_string_equal(parsed->groupings->name, "grp"); assert_string_equal(parsed->actions->name, "action"); assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING); assert_string_equal(parsed->notifs->name, "notf"); assert_string_equal(parsed->iffeatures[0].str, "iff"); assert_string_equal(parsed->key, "key"); assert_int_equal(parsed->min, 10); assert_string_equal(parsed->typedefs->name, "tpdf"); assert_string_equal(parsed->uniques->str, "utag"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST); lysp_node_free(&fctx, siblings); ly_set_erase(&YCTX->tpdfs_nodes, NULL); siblings = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_list *)&siblings[0]; CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0, "list-name", 0, LYS_LIST, 0, NULL, 0); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_notification_elem(void **state) { const char *data; struct lysp_node_notif *notifs = NULL; struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)¬ifs}; /* max subelems */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, ¬if_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(notifs->name, "notif-name"); assert_string_equal(notifs->child->name, "anyd"); assert_int_equal(notifs->child->nodetype, LYS_ANYDATA); assert_string_equal(notifs->child->next->name, "anyx"); assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML); assert_string_equal(notifs->child->next->next->name, "leaf"); assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF); assert_string_equal(notifs->child->next->next->next->name, "llist"); assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(notifs->child->next->next->next->next->name, "sub-list"); assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST); assert_true(notifs->flags & LYS_STATUS_DEPRC); assert_string_equal(notifs->groupings->name, "grp"); assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING); assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name"); assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES); assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont"); assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER); assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE); assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice"); assert_null(notifs->child->next->next->next->next->next->next->next->next); assert_string_equal(notifs->iffeatures[0].str, "iff"); assert_string_equal(notifs->musts->arg.str, "cond"); assert_int_equal(notifs->nodetype, LYS_NOTIF); assert_null(notifs->parent); assert_string_equal(notifs->ref, "ref"); assert_string_equal(notifs->typedefs->name, "tpdf"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION); lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, ¬if_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(notifs->name, "notif-name"); lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL; } static void test_grouping_elem(void **state) { const char *data; struct lysp_node_grp *grps = NULL; struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps}; /* max subelems */ data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(grps->name, "grp-name"); assert_string_equal(grps->child->name, "anyd"); assert_string_equal(grps->child->next->name, "anyx"); assert_string_equal(grps->child->next->next->name, "leaf"); assert_string_equal(grps->child->next->next->next->name, "llist"); assert_string_equal(grps->child->next->next->next->next->name, "list"); assert_string_equal(grps->dsc, "desc"); assert_true(grps->flags & LYS_STATUS_CURR); assert_string_equal(grps->groupings->name, "sub-grp"); assert_int_equal(grps->nodetype, LYS_GROUPING); assert_string_equal(grps->notifs->name, "notf"); assert_null(grps->parent); assert_string_equal(grps->ref, "ref"); assert_string_equal(grps->typedefs->name, "tpdf"); assert_string_equal(grps->actions->name, "act"); assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name"); assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES); assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont"); assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice"); assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING); lysp_node_free(&fctx, &grps->node); grps = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(grps->name, "grp-name"); lysp_node_free(&fctx, &grps->node); grps = NULL; } static void test_container_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {NULL, &siblings}; struct lysp_node_container *parsed = NULL; /* max subelems */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_container *)siblings; uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "cont-name", 0, LYS_CONTAINER, 0, "ref", 1); CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "iff"); assert_string_equal(parsed->presence, "presence"); assert_string_equal(parsed->typedefs->name, "tpdf"); assert_string_equal(parsed->groupings->name, "sub-grp"); assert_string_equal(parsed->child->name, "anyd"); assert_int_equal(parsed->child->nodetype, LYS_ANYDATA); assert_string_equal(parsed->child->next->name, "anyx"); assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML); assert_string_equal(parsed->child->next->next->name, "subcont"); assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER); assert_string_equal(parsed->child->next->next->next->name, "leaf"); assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF); assert_string_equal(parsed->child->next->next->next->next->name, "llist"); assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(parsed->child->next->next->next->next->next->name, "list"); assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES); assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice"); assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE); assert_null(parsed->child->next->next->next->next->next->next->next->next); assert_string_equal(parsed->notifs->name, "notf"); assert_string_equal(parsed->actions->name, "act"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER); lysp_node_free(&fctx, siblings); ly_set_erase(&YCTX->tpdfs_nodes, NULL); siblings = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_container *)siblings; CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0, "cont-name", 0, LYS_CONTAINER, 0, NULL, 0); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_case_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {NULL, &siblings}; struct lysp_node_case *parsed = NULL; /* max subelems */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_case *)siblings; uint16_t flags = LYS_STATUS_CURR; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "case-name", 0, LYS_CASE, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "iff"); assert_string_equal(parsed->child->name, "anyd"); assert_int_equal(parsed->child->nodetype, LYS_ANYDATA); assert_string_equal(parsed->child->next->name, "anyx"); assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML); assert_string_equal(parsed->child->next->next->name, "subcont"); assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER); assert_string_equal(parsed->child->next->next->next->name, "leaf"); assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF); assert_string_equal(parsed->child->next->next->next->next->name, "llist"); assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(parsed->child->next->next->next->next->next->name, "list"); assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES); assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice"); assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE); assert_null(parsed->child->next->next->next->next->next->next->next->next); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE); lysp_node_free(&fctx, siblings); siblings = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_case *)siblings; CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0, "case-name", 0, LYS_CASE, 0, NULL, 0); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_choice_elem(void **state) { const char *data; struct lysp_node *siblings = NULL; struct tree_node_meta node_meta = {NULL, &siblings}; struct lysp_node_choice *parsed = NULL; /* max subelems */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_choice *)siblings; uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR; CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1, "choice-name", 0, LYS_CHOICE, 0, "ref", 1); CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL); assert_string_equal(parsed->iffeatures[0].str, "iff"); assert_string_equal(parsed->child->name, "anyd"); assert_int_equal(parsed->child->nodetype, LYS_ANYDATA); assert_string_equal(parsed->child->next->name, "anyx"); assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML); assert_string_equal(parsed->child->next->next->name, "sub-case"); assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE); assert_string_equal(parsed->child->next->next->next->name, "choice"); assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE); assert_string_equal(parsed->child->next->next->next->next->name, "subcont"); assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf"); assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist"); assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list"); assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST); assert_null(parsed->child->next->next->next->next->next->next->next->next); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE); lysp_node_free(&fctx, siblings); siblings = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS); parsed = (struct lysp_node_choice *)siblings; assert_string_equal(parsed->name, "choice-name"); CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0, "choice-name", 0, LYS_CHOICE, 0, NULL, 0); lysp_node_free(&fctx, siblings); siblings = NULL; } static void test_inout_elem(void **state) { const char *data; struct lysp_node_action_inout inout = {0}; struct inout_meta inout_meta = {NULL, &inout}; /* max subelements */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS); CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1); CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL); assert_string_equal(inout.typedefs->name, "tpdf"); assert_string_equal(inout.groupings->name, "sub-grp"); assert_string_equal(inout.child->name, "anyd"); assert_int_equal(inout.child->nodetype, LYS_ANYDATA); assert_string_equal(inout.child->next->name, "anyx"); assert_int_equal(inout.child->next->nodetype, LYS_ANYXML); assert_string_equal(inout.child->next->next->name, "choice"); assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE); assert_string_equal(inout.child->next->next->next->name, "subcont"); assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(inout.child->next->next->next->next->name, "leaf"); assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(inout.child->next->next->next->next->next->name, "llist"); assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(inout.child->next->next->next->next->next->next->name, "list"); assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(inout.child->next->next->next->next->next->next->next->next); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT); lysp_node_free(&fctx, (struct lysp_node *)&inout); memset(&inout, 0, sizeof inout); /* max subelements */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS); CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1); assert_string_equal(inout.musts->arg.str, "cond"); assert_string_equal(inout.typedefs->name, "tpdf"); assert_string_equal(inout.groupings->name, "sub-grp"); assert_string_equal(inout.child->name, "anyd"); assert_int_equal(inout.child->nodetype, LYS_ANYDATA); assert_string_equal(inout.child->next->name, "anyx"); assert_int_equal(inout.child->next->nodetype, LYS_ANYXML); assert_string_equal(inout.child->next->next->name, "choice"); assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE); assert_string_equal(inout.child->next->next->next->name, "subcont"); assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(inout.child->next->next->next->next->name, "leaf"); assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(inout.child->next->next->next->next->next->name, "llist"); assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(inout.child->next->next->next->next->next->next->name, "list"); assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(inout.child->next->next->next->next->next->next->next->next); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT); lysp_node_free(&fctx, (struct lysp_node *)&inout); memset(&inout, 0, sizeof inout); /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS); lysp_node_free(&fctx, (struct lysp_node *)&inout); memset(&inout, 0, sizeof inout); data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS); lysp_node_free(&fctx, (struct lysp_node *)&inout); memset(&inout, 0, sizeof inout); /* invalid combinations */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID); lysp_node_free(&fctx, (struct lysp_node *)&inout); CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", NULL, 1); memset(&inout, 0, sizeof inout); } static void test_action_elem(void **state) { const char *data; struct lysp_node_action *actions = NULL; struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions}; uint16_t flags; /* max subelems */ PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " desc\n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; /* there must be parent for action */ act_meta.parent = (void *)1; assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS); act_meta.parent = NULL; flags = LYS_STATUS_DEPRC; CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\ 1, 0, 0, 0,\ 1, 0,\ "act", LYS_ACTION, \ 1, 0, 0, 1,\ 1, 0,\ 1, "ref", 1); assert_string_equal(actions->iffeatures[0].str, "iff"); assert_string_equal(actions->typedefs->name, "tpdf"); assert_string_equal(actions->groupings->name, "grouping"); assert_string_equal(actions->output.musts->arg.str, "cond"); assert_string_equal(actions->input.child->name, "uses-name"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION); lysp_node_free(&fctx, (struct lysp_node *)actions); actions = NULL; PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " desc\n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS); flags = LYS_STATUS_DEPRC; CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\ 1, 0, 0, 0,\ 1, 0,\ "act", LYS_RPC, \ 1, 0, 0, 1,\ 1, 0,\ 0, "ref", 1); assert_string_equal(actions->iffeatures[0].str, "iff"); assert_string_equal(actions->typedefs->name, "tpdf"); assert_string_equal(actions->groupings->name, "grouping"); assert_string_equal(actions->input.child->name, "uses-name"); assert_string_equal(actions->output.musts->arg.str, "cond"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC); lysp_node_free(&fctx, (struct lysp_node *)actions); actions = NULL; /* min subelems */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(actions->name, "act"); lysp_node_free(&fctx, (struct lysp_node *)actions); actions = NULL; } static void test_augment_elem(void **state) { const char *data; struct lysp_node_augment *augments = NULL; struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments}; PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1; data = ELEMENT_WRAPPER_START "\n" " \n" " \n" " \n" " \n" " \n" " \n" " desc\n" " \n" " \n" " \n" " \n" " \n" " ref\n" " \n" " \n" " \n" EXT_SUBELEM "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(augments->nodeid, "target"); assert_null(augments->parent); assert_int_equal(augments->nodetype, LYS_AUGMENT); assert_true(augments->flags & LYS_STATUS_CURR); assert_string_equal(augments->dsc, "desc"); assert_string_equal(augments->ref, "ref"); assert_string_equal(augments->when->cond, "when-cond"); assert_string_equal(augments->iffeatures[0].str, "iff"); assert_string_equal(augments->child->name, "anyd"); assert_int_equal(augments->child->nodetype, LYS_ANYDATA); assert_string_equal(augments->child->next->name, "anyx"); assert_int_equal(augments->child->next->nodetype, LYS_ANYXML); assert_string_equal(augments->child->next->next->name, "case"); assert_int_equal(augments->child->next->next->nodetype, LYS_CASE); assert_string_equal(augments->child->next->next->next->name, "choice"); assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE); assert_string_equal(augments->child->next->next->next->next->name, "subcont"); assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(augments->child->next->next->next->next->next->name, "leaf"); assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist"); assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list"); assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses"); assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(augments->child->next->next->next->next->next->next->next->next->next); assert_string_equal(augments->actions->name, "action"); assert_string_equal(augments->notifs->name, "notif"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT); lysp_node_free(&fctx, (struct lysp_node *)augments); augments = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS); assert_string_equal(augments->nodeid, "target"); lysp_node_free(&fctx, (struct lysp_node *)augments); augments = NULL; } static void test_deviate_elem(void **state) { const char *data; struct lysp_deviate *deviates = NULL; /* invalid arguments */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. " "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", NULL, 1); deviates = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. " "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", NULL, 1); deviates = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. " "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", NULL, 1); deviates = NULL; data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. " "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", NULL, 1); deviates = NULL; data = ELEMENT_WRAPPER_START "\n" " \n" "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Deviate of this type doesn't allow \"must\" as it's sub-element.", NULL, 2); } static void test_deviation_elem(void **state) { const char *data; struct lysp_deviation *deviations = NULL; /* invalid */ data = ELEMENT_WRAPPER_START "" ELEMENT_WRAPPER_END; assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID); CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", NULL, 1); } static struct lysp_module * mod_renew(struct lysp_yin_ctx *ctx) { struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx; struct lysp_module *pmod; lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0); pmod = calloc(1, sizeof *pmod); ctx->parsed_mods->objs[0] = pmod; pmod->mod = calloc(1, sizeof *pmod->mod); pmod->mod->parsed = pmod; pmod->mod->ctx = ly_ctx; fctx.mod = pmod->mod; return pmod; } static void test_module_elem(void **state) { const char *data; struct lysp_module *lysp_mod = mod_renew(YCTX); /* max subelems */ data = "\n" " \n" " \n" " \n" " \n" " \n" " org\n" " contact\n" " desc\n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "\n" "\n"; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS); assert_string_equal(lysp_mod->mod->name, "mod"); assert_string_equal(lysp_mod->revs[0].date, "2019-02-02"); assert_string_equal(lysp_mod->mod->ns, "ns"); assert_string_equal(lysp_mod->mod->prefix, "pref"); assert_null(lysp_mod->mod->filepath); assert_string_equal(lysp_mod->mod->org, "org"); assert_string_equal(lysp_mod->mod->contact, "contact"); assert_string_equal(lysp_mod->mod->dsc, "desc"); assert_string_equal(lysp_mod->mod->ref, "ref"); assert_int_equal(lysp_mod->version, LYS_VERSION_1_1); CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod", "imp-pref", NULL, ""); assert_string_equal(lysp_mod->includes->name, "b-mod"); assert_string_equal(lysp_mod->extensions->name, "ext"); assert_string_equal(lysp_mod->features->name, "feature"); assert_string_equal(lysp_mod->identities->name, "ident-name"); assert_string_equal(lysp_mod->typedefs->name, "tpdf"); assert_string_equal(lysp_mod->groupings->name, "grp"); assert_string_equal(lysp_mod->data->name, "anyd"); assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA); assert_string_equal(lysp_mod->data->next->name, "anyx"); assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML); assert_string_equal(lysp_mod->data->next->next->name, "choice"); assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE); assert_string_equal(lysp_mod->data->next->next->next->name, "cont"); assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf"); assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist"); assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list"); assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(lysp_mod->data->next->next->next->next->next->next->next->next); assert_string_equal(lysp_mod->augments->nodeid, "target"); assert_string_equal(lysp_mod->rpcs->name, "rpc-name"); assert_string_equal(lysp_mod->notifs->name, "notf"); assert_string_equal(lysp_mod->deviations->nodeid, "target"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LY_STMT_MODULE); /* min subelems */ ly_in_free(UTEST_IN, 0); lyxml_ctx_free(YCTX->xmlctx); lysp_mod = mod_renew(YCTX); data = "\n" " \n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS); assert_string_equal(lysp_mod->mod->name, "mod"); /* incorrect subelem order */ ly_in_free(UTEST_IN, 0); lyxml_ctx_free(YCTX->xmlctx); lysp_mod = mod_renew(YCTX); data = "\n" " \n" " \n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID); CHECK_LOG_CTX("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\".", NULL, 3); } static struct lysp_submodule * submod_renew(struct lysp_yin_ctx *ctx, const char *belongs_to) { struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx; struct lysp_submodule *submod; lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0); submod = calloc(1, sizeof *submod); ctx->parsed_mods->objs[0] = submod; submod->mod = calloc(1, sizeof *submod->mod); lydict_insert(ly_ctx, belongs_to, 0, &submod->mod->name); submod->mod->parsed = (struct lysp_module *)submod; submod->mod->ctx = ly_ctx; fctx.mod = submod->mod; return submod; } static void test_submodule_elem(void **state) { const char *data; struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name"); /* max subelements */ data = "\n" " \n" " \n" " \n" " \n" " \n" " \n" " org\n" " contact\n" " desc\n" " ref\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" EXT_SUBELEM "\n" "\n"; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS); CHECK_LOG_CTX("YANG version 1.1 expects all includes in main module, includes in submodules (mod) are not necessary.", NULL, 0); assert_string_equal(lysp_submod->name, "mod"); assert_string_equal(lysp_submod->revs[0].date, "2019-02-02"); assert_string_equal(lysp_submod->prefix, "pref"); assert_null(lysp_submod->filepath); assert_string_equal(lysp_submod->org, "org"); assert_string_equal(lysp_submod->contact, "contact"); assert_string_equal(lysp_submod->dsc, "desc"); assert_string_equal(lysp_submod->ref, "ref"); assert_int_equal(lysp_submod->version, LYS_VERSION_1_1); CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod", "imp-pref", NULL, ""); assert_string_equal(lysp_submod->includes->name, "b-mod"); assert_string_equal(lysp_submod->extensions->name, "ext"); assert_string_equal(lysp_submod->features->name, "feature"); assert_string_equal(lysp_submod->identities->name, "ident-name"); assert_string_equal(lysp_submod->typedefs->name, "tpdf"); assert_string_equal(lysp_submod->groupings->name, "grp"); assert_string_equal(lysp_submod->data->name, "anyd"); assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA); assert_string_equal(lysp_submod->data->next->name, "anyx"); assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML); assert_string_equal(lysp_submod->data->next->next->name, "choice"); assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE); assert_string_equal(lysp_submod->data->next->next->next->name, "cont"); assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER); assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf"); assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF); assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist"); assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST); assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list"); assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST); assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name"); assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES); assert_null(lysp_submod->data->next->next->next->next->next->next->next->next); assert_string_equal(lysp_submod->augments->nodeid, "target"); assert_string_equal(lysp_submod->rpcs->name, "rpc-name"); assert_string_equal(lysp_submod->notifs->name, "notf"); assert_string_equal(lysp_submod->deviations->nodeid, "target"); TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LY_STMT_SUBMODULE); /* min subelemnts */ ly_in_free(UTEST_IN, 0); lyxml_ctx_free(YCTX->xmlctx); lysp_submod = submod_renew(YCTX, "module-name"); data = "\n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS); assert_string_equal(lysp_submod->prefix, "pref"); assert_int_equal(lysp_submod->version, LYS_VERSION_1_0); /* incorrect subelem order */ ly_in_free(UTEST_IN, 0); lyxml_ctx_free(YCTX->xmlctx); lysp_submod = submod_renew(YCTX, "module-name"); data = "\n" " \n" " ref\n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS); assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS); assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID); CHECK_LOG_CTX("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\".", NULL, 4); } static void test_yin_parse_module(void **state) { const char *data; struct lys_module *mod; struct lysp_yin_ctx *yin_ctx = NULL; struct ly_in *in = NULL; mod = calloc(1, sizeof *mod); mod->ctx = UTEST_LYCTX; data = " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " test\n" " \n" " \n" " test\n" " \n" " \n" " \n" " \n" " \n" " \n" "\n"; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS); assert_null(mod->parsed->exts->child->next->child); assert_string_equal(mod->parsed->exts->child->next->arg, "test"); lys_module_free(&fctx, mod, 0); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); mod = NULL; yin_ctx = NULL; mod = calloc(1, sizeof *mod); mod->ctx = UTEST_LYCTX; data = "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " The MTU of the interface.\n" " \n" " \n" " \n" " \n" "\n"; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS); lys_module_free(&fctx, mod, 0); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); mod = NULL; yin_ctx = NULL; mod = calloc(1, sizeof *mod); mod->ctx = UTEST_LYCTX; data = "\n" " \n" " \n" " \n" "\n"; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS); lys_module_free(&fctx, mod, 0); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); mod = NULL; yin_ctx = NULL; mod = calloc(1, sizeof *mod); mod->ctx = UTEST_LYCTX; data = "" "\n"; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL); CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL, 0); lys_module_free(&fctx, mod, 0); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); mod = calloc(1, sizeof *mod); mod->ctx = UTEST_LYCTX; data = "\n" " \n" " \n" " \n" "\n" ""; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID); CHECK_LOG_CTX("Trailing garbage \"\" after module, expected end-of-input.", NULL, 6); lys_module_free(&fctx, mod, 0); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); mod = NULL; yin_ctx = NULL; } static void test_yin_parse_submodule(void **state) { const char *data; struct lysp_yin_ctx *yin_ctx = NULL; struct lysp_submodule *submod = NULL; struct ly_in *in; lydict_insert(UTEST_LYCTX, "a", 0, &PARSER_CUR_PMOD(YCTX)->mod->name); data = "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_SUCCESS); lysp_module_free(&fctx, (struct lysp_module *)submod); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); yin_ctx = NULL; submod = NULL; data = "\n" "\n" " \n" " \n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_SUCCESS); lysp_module_free(&fctx, (struct lysp_module *)submod); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); yin_ctx = NULL; submod = NULL; data = "\n" "" ""; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_EINVAL); CHECK_LOG_CTX("Input data contains module when a submodule is expected.", NULL, 0); lysp_module_free(&fctx, (struct lysp_module *)submod); lysp_yin_ctx_free(yin_ctx); ly_in_free(in, 0); yin_ctx = NULL; submod = NULL; data = "\n" "\n" " \n" " \n" " \n" " \n" "\n" "\n" " \n" " \n" " \n" " \n" ""; assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS); assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_EVALID); CHECK_LOG_CTX("Trailing garbage \"