test REFACTOR move type tests into separate files
diff --git a/tests/utests/CMakeLists.txt b/tests/utests/CMakeLists.txt
index d36ebaf..ef99981 100644
--- a/tests/utests/CMakeLists.txt
+++ b/tests/utests/CMakeLists.txt
@@ -14,7 +14,15 @@
${CMAKE_CURRENT_SOURCE_DIR}/node/*.c
PARENT_SCOPE)
+ly_add_utest(NAME uint8 SOURCES types/uint8.c)
+ly_add_utest(NAME uint16 SOURCES types/uint16.c)
+ly_add_utest(NAME uint32 SOURCES types/uint32.c)
+ly_add_utest(NAME uint64 SOURCES types/uint64.c)
ly_add_utest(NAME int8 SOURCES types/int8.c)
+ly_add_utest(NAME int16 SOURCES types/int16.c)
+ly_add_utest(NAME int32 SOURCES types/int32.c)
+ly_add_utest(NAME int64 SOURCES types/int64.c)
+ly_add_utest(NAME dec64 SOURCES types/dec64.c)
ly_add_utest(NAME string SOURCES types/string.c)
ly_add_utest(NAME bits SOURCES types/bits.c)
ly_add_utest(NAME binary SOURCES types/binary.c)
@@ -60,7 +68,6 @@
ly_add_utest(NAME parser_json SOURCES data/test_parser_json.c)
ly_add_utest(NAME lyb SOURCES data/test_lyb.c)
ly_add_utest(NAME validation SOURCES data/test_validation.c)
-ly_add_utest(NAME types SOURCES data/test_types.c)
ly_add_utest(NAME merge SOURCES data/test_merge.c)
ly_add_utest(NAME diff SOURCES data/test_diff.c)
diff --git a/tests/utests/data/test_types.c b/tests/utests/data/test_types.c
deleted file mode 100644
index 3944486..0000000
--- a/tests/utests/data/test_types.c
+++ /dev/null
@@ -1,1064 +0,0 @@
-/*
- * @file test_types.c
- * @author: Radek Krejci <rkrejci@cesnet.cz>
- * @brief unit tests for support of YANG data types
- *
- * Copyright (c) 2019-2020 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 <stdio.h>
-#include <string.h>
-
-#include "libyang.h"
-#include "path.h"
-#include "plugins_types.h"
-
-const char *schema_a = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;"
- "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}"
- "typedef iref {type identityref {base interface-type;}}}";
-const char *schema_b = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}"
- "feature f; identity gigabit-ethernet { base defs:ethernet;}"
- "typedef tboolean {type boolean;}"
- "typedef tempty {type empty;}"
- "container cont {leaf leaftarget {type empty;}"
- " list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}}"
- " leaf-list leaflisttarget {type uint8; max-elements 5;}}"
- "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}"
- "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}"
- "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}"
- "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}"
- "list list_keyless {config \"false\"; leaf id {type string;} leaf value {type string;}}"
- "leaf-list leaflisttarget {type string;}"
- "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}"
- "leaf binary-norestr {type binary;}"
- "leaf int8 {type int8 {range 10..20;}}"
- "leaf uint8 {type uint8 {range 150..200;}}"
- "leaf int16 {type int16 {range -20..-10;}}"
- "leaf uint16 {type uint16 {range 150..200;}}"
- "leaf int32 {type int32;}"
- "leaf uint32 {type uint32;}"
- "leaf int64 {type int64;}"
- "leaf uint64 {type uint64;}"
- "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}"
- "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}"
- "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}"
- "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}"
- "leaf str {type string {length 8..10; pattern '[a-z ]*';}}"
- "leaf str-invert {type string {pattern '[a-z ]*' {modifier invert-match;}}}"
- "leaf str-norestr {type string;}"
- "leaf str-utf8 {type string{length 2..5; pattern '€*';}}"
- "leaf bool {type boolean;}"
- "leaf tbool {type tboolean;}"
- "leaf empty {type empty;}"
- "leaf tempty {type tempty;}"
- "leaf ident {type identityref {base defs:interface-type;}}"
- "leaf iref {type defs:iref;}"
- "leaf inst {type instance-identifier {require-instance true;}}"
- "leaf inst-noreq {type instance-identifier {require-instance false;}}"
- "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}"
- "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}"
- "leaf un1 {type union {"
- " type leafref {path /int8; require-instance true;}"
- " type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }"
- " type string {length 1..20;}}}}";
-
-static int
-setup(void **state)
-{
- UTEST_SETUP;
- UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, NULL, NULL);
- UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
-
- return 0;
-}
-
-#define CHECK_PARSE_LYD(INPUT, MODEL) \
- CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, MODEL)
-
-#define TEST_PATTERN_1(INPUT, SCHEMA_NAME, SCHEMA_NEXT, VALUE_TYPE, ...) \
- { \
- struct lyd_node_term *leaf; \
- struct lyd_value value = {0}; \
- /* create model */ \
- CHECK_LYSC_NODE((INPUT)->schema, NULL, 0, 0x5, 1, SCHEMA_NAME, SCHEMA_NEXT, LYS_LEAF, 0, 0, NULL, 0); \
- /* CHECK_LYSC_NODE((INPUT)->schema, LYS_LEAF, SCHEMA_NAME); */ \
- leaf = (struct lyd_node_term*)(INPUT); \
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 0, 1, VALUE_TYPE, ## __VA_ARGS__); \
- /* copy value */ \
- assert_int_equal(LY_SUCCESS, leaf->value.realtype->plugin->duplicate(UTEST_LYCTX, &leaf->value, &value)); \
- CHECK_LYD_VALUE(value, VALUE_TYPE, ## __VA_ARGS__); \
- if (LY_TYPE_INST == LY_TYPE_ ## VALUE_TYPE) { \
- for (LY_ARRAY_COUNT_TYPE it = 0; it < LY_ARRAY_COUNT(leaf->value.target); it++) { \
- assert_ptr_equal(value.target[it].node, leaf->value.target[it].node); \
- LY_ARRAY_COUNT_TYPE arr_size_predicates = 0; \
- if (value.target[it].pred_type == LY_PATH_PREDTYPE_NONE) { \
- assert_null(value.target[it].predicates); \
- } else { \
- assert_non_null(value.target[it].predicates); \
- arr_size_predicates = LY_ARRAY_COUNT(value.target[it].predicates); \
- assert_int_equal(LY_ARRAY_COUNT(value.target[it].predicates), LY_ARRAY_COUNT(leaf->value.target[it].predicates)); \
- } \
- for (LY_ARRAY_COUNT_TYPE jt = 0; jt < arr_size_predicates; jt++) { \
- if (value.target[it].pred_type == LY_PATH_PREDTYPE_POSITION) { \
- assert_int_equal(value.target[it].predicates[jt].position, leaf->value.target[it].predicates[jt].position); \
- } else { \
- assert_true(LY_SUCCESS == value.realtype->plugin->compare(&value, &leaf->value)); \
- } \
- } \
- } \
- } \
- value.realtype->plugin->free(UTEST_LYCTX, &value); \
- }
-
-#define TEST_TYPE_ERROR(TYPE, VALUE, ERROR_MSG, LINE) \
- { \
- const char *data = "<" TYPE " xmlns=\"urn:tests:types\">" VALUE "</" TYPE">"; \
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
- CHECK_LOG_CTX(ERROR_MSG, "Schema location /types:"TYPE", line number "LINE"."); \
- }
-
-#define TEST_PRINTED_VALUE(VALUE, EXPECTED, FORMAT, PREFIX_DATA) \
- { \
- const char *_str; \
- uint8_t _dynamic; \
- assert_non_null(_str = (VALUE)->realtype->plugin->print(UTEST_LYCTX, VALUE, FORMAT, (void *)PREFIX_DATA, &_dynamic, NULL)); \
- assert_string_equal(EXPECTED, _str); \
- if (_dynamic) { \
- free((char *)_str); \
- } \
- }
-
-static void
-test_int(void **state)
-{
- struct lyd_node *tree;
- const char *data;
- char *error_msg;
-
-#if 0
- /* type int8 is tested in file ./tests/utests/types/int8.c */
- /* valid data */
- data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n </int8>";
- CHECK_PARSE_LYD(data, tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "int8", 1, INT8, "15", 15);
- lyd_free_all(tree);
-
- /* invalid range */
- error_msg = "Value \"1\" does not satisfy the range constraint.";
- TEST_TYPE_ERROR("int8", "1", error_msg, "1");
-#endif
-
- error_msg = "Unsatisfied range - value \"100\" is out of the allowed range.";
- TEST_TYPE_ERROR("int16", "100", error_msg, "1");
-
- /* invalid value */
- error_msg = "Invalid int32 value \"0x01\".";
- TEST_TYPE_ERROR("int32", "0x01", error_msg, "1");
- error_msg = "Invalid empty int64 value.";
- TEST_TYPE_ERROR("int64", "", error_msg, "1");
- error_msg = "Invalid empty int64 value.";
- TEST_TYPE_ERROR("int64", " ", error_msg, "1");
- error_msg = "Invalid int64 value \"-10 xxx\".";
- TEST_TYPE_ERROR("int64", "-10 xxx", error_msg, "1");
-}
-
-static void
-test_uint(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<uint8 xmlns=\"urn:tests:types\">\n 150 \t\n </uint8>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "uint8", 1, UINT8, "150", 150);
- lyd_free_all(tree);
-
- /* invalid range */
- TEST_TYPE_ERROR("uint8", "\n 15 \t\n ",
- "Unsatisfied range - value \"15\" is out of the allowed range.", "3");
- TEST_TYPE_ERROR("uint16", "\n 1500 \t\n ",
- "Unsatisfied range - value \"1500\" is out of the allowed range.", "3");
-
- /* invalid value */
- TEST_TYPE_ERROR("uint32", "-10",
- "Value \"-10\" is out of uint32's min/max bounds.", "1");
- CHECK_PARSE_LYD_PARAM("<uint64 xmlns=\"urn:tests:types\"/>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid empty uint64 value.", "Schema location /types:uint64, line number 1.");
- TEST_TYPE_ERROR("uint64", " ", "Invalid empty uint64 value.", "1");
- TEST_TYPE_ERROR("uint64", "10 xxx", "Invalid uint64 value \"10 xxx\".", "1");
-}
-
-static void
-test_dec64(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<dec64 xmlns=\"urn:tests:types\">\n +8 \t\n </dec64>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "dec64", 1, DEC64, "8.0", 80);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<dec64 xmlns=\"urn:tests:types\">8.00</dec64>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "dec64", 1, DEC64, "8.0", 80);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<dec64-norestr xmlns=\"urn:tests:types\">-9.223372036854775808</dec64-norestr>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "dec64-norestr", 1, DEC64, "-9.223372036854775808", INT64_C(-9223372036854775807) - INT64_C(1));
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<dec64-norestr xmlns=\"urn:tests:types\">9.223372036854775807</dec64-norestr>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "dec64-norestr", 1, DEC64, "9.223372036854775807", INT64_C(9223372036854775807));
- lyd_free_all(tree);
-
- /* invalid range */
- TEST_TYPE_ERROR("dec64", "\n 15 \t\n ", "Unsatisfied range - value \"15.0\" is out of the allowed range.", "3");
- TEST_TYPE_ERROR("dec64", "\n 0 \t\n ", "Unsatisfied range - value \"0.0\" is out of the allowed range.", "3");
-
- /* invalid value */
- TEST_TYPE_ERROR("dec64", "xxx", "Invalid 1. character of decimal64 value \"xxx\".", "1");
- CHECK_PARSE_LYD_PARAM("<dec64 xmlns=\"urn:tests:types\"/>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid empty decimal64 value.", "Schema location /types:dec64, line number 1.");
- TEST_TYPE_ERROR("dec64", " ", "Invalid empty decimal64 value.", "1");
- TEST_TYPE_ERROR("dec64", "8.5 xxx", "Invalid 6. character of decimal64 value \"8.5 xxx\".", "1");
- TEST_TYPE_ERROR("dec64", "8.55 xxx", "Value \"8.55\" of decimal64 type exceeds defined number (1) of fraction digits.", "1");
-}
-
-/* test string is now in file string.c */
-#if 0
-static void
-test_string(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<str xmlns=\"urn:tests:types\">teststring</str>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "str", 1, STRING, "teststring");
- lyd_free_all(tree);
-
- /* inverted match */
- CHECK_PARSE_LYD("<str-invert xmlns=\"urn:tests:types\">TESTSTRING</str-invert>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "str-invert", 1, STRING, "TESTSTRING");
- lyd_free_all(tree);
-
- TEST_TYPE_ERROR("str-invert", "teststring",
- "Unsatisfied pattern - \"teststring\" does not conform to inverted \"[a-z ]*\".", "1");
-
- /* multibyte characters (€ encodes as 3-byte UTF8 character, length restriction is 2-5) */
- CHECK_PARSE_LYD("<str-utf8 xmlns=\"urn:tests:types\">€€</str-utf8>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "str-utf8", 1, STRING, "€€");
- lyd_free_all(tree);
-
- /*error */
- TEST_TYPE_ERROR("str-utf8", "€",
- "Unsatisfied length - string length \"1\" is not allowed.", "1");
- TEST_TYPE_ERROR("str-utf8", "€€€€€€",
- "Unsatisfied length - string length \"6\" is not allowed.", "1");
- TEST_TYPE_ERROR("str-utf8", "€€x",
- "Unsatisfied pattern - \"€€x\" does not conform to \"€*\".", "1");
-
- /* invalid length */
- TEST_TYPE_ERROR("str", "short",
- "Unsatisfied length - string length \"5\" is not allowed.", "1");
- TEST_TYPE_ERROR("str", "tooooo long",
- "Unsatisfied length - string length \"11\" is not allowed.", "1");
-
- /* invalid pattern */
- TEST_TYPE_ERROR("str", "string15", "Unsatisfied pattern - \"string15\" does not conform to \"[a-z ]*\".", "1");
-}
-
-#endif
-
-#if 0
-static void
-test_bits(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<bits xmlns=\"urn:tests:types\">\n two \t\nzero\n </bits>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "bits", 1, BITS, "zero two", "zero", "two");
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<bits xmlns=\"urn:tests:types\">zero two</bits>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "bits", 1, BITS, "zero two", "zero", "two");
- lyd_free_all(tree);
-
- /* disabled feature */
- TEST_TYPE_ERROR("bits", " \t one \n\t ", "Invalid bit value \"one\".", "2");
-
- /* disabled feature */
- TEST_TYPE_ERROR("bits", "\t one \n\t", "Invalid bit value \"one\".", "2");
-
- /* multiple instances of the bit */
- TEST_TYPE_ERROR("bits", "one zero one", "Invalid bit value \"one\".", "1");
-
- /* invalid bit value */
- TEST_TYPE_ERROR("bits", "one xero one", "Invalid bit value \"one\".", "1");
-}
-
-#endif
-
-static void
-test_enums(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<enums xmlns=\"urn:tests:types\">white</enums>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "enums", 1, ENUM, "white", "white");
- lyd_free_all(tree);
-
- /* disabled feature */
- TEST_TYPE_ERROR("enums", "yellow", "Invalid enumeration value \"yellow\".", "1");
-
- /* leading/trailing whitespaces are not valid */
- TEST_TYPE_ERROR("enums", " white", "Invalid enumeration value \" white\".", "1");
- TEST_TYPE_ERROR("enums", "white\n", "Invalid enumeration value \"white\n\".", "2");
-
- /* invalid enumeration value */
- TEST_TYPE_ERROR("enums", "black", "Invalid enumeration value \"black\".", "1");
-}
-
-#if 0
-static void
-test_binary(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data (hello) */
- CHECK_PARSE_LYD("<binary xmlns=\"urn:tests:types\">\n aGVs\nbG8= \t\n </binary>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "binary", 1, BINARY, "aGVs\nbG8=");
- lyd_free_all(tree);
- CHECK_PARSE_LYD("<binary-norestr xmlns=\"urn:tests:types\">TQ==</binary-norestr>", tree);
- assert_non_null(tree);
- tree = tree->next;
- assert_non_null(tree);
- TEST_PATTERN_1(tree, "binary-norestr", 1, BINARY, "TQ==");
- lyd_free_all(tree);
-
- /* no data */
- CHECK_PARSE_LYD("<binary-norestr xmlns=\"urn:tests:types\">\n \t\n </binary-norestr>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "binary-norestr", 1, BINARY, "");
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<binary-norestr xmlns=\"urn:tests:types\"></binary-norestr>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "binary-norestr", 1, BINARY, "");
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<binary-norestr xmlns=\"urn:tests:types\"/>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "binary-norestr", 1, BINARY, "");
- lyd_free_all(tree);
-
- /* invalid base64 character */
- TEST_TYPE_ERROR("binary-norestr", "a@bcd=", "Invalid Base64 character (@).", "1");
-
- /* missing data */
- TEST_TYPE_ERROR("binary-norestr", "aGVsbG8", "Base64 encoded value length must be divisible by 4.", "1");
-
- TEST_TYPE_ERROR("binary-norestr", "VsbG8=", "Base64 encoded value length must be divisible by 4.", "1");
-
- /* invalid binary length */
- /* helloworld */
- TEST_TYPE_ERROR("binary", "aGVsbG93b3JsZA==", "This base64 value must be of length 5.", "1");
-
- /* M */
- TEST_TYPE_ERROR("binary", "TQ==", "This base64 value must be of length 5.", "1");
-}
-
-#endif
-
-static void
-test_boolean(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<bool xmlns=\"urn:tests:types\">true</bool>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "bool", 1, BOOL, "true", 1);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<bool xmlns=\"urn:tests:types\">false</bool>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "bool", 1, BOOL, "false", 0);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<tbool xmlns=\"urn:tests:types\">false</tbool>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "tbool", 1, BOOL, "false", 0);
- lyd_free_all(tree);
-
- /* invalid value */
- TEST_TYPE_ERROR("bool", "unsure", "Invalid boolean value \"unsure\".", "1");
-
- TEST_TYPE_ERROR("bool", " true", "Invalid boolean value \" true\".", "1");
-}
-
-static void
-test_empty(void **state)
-{
- struct lyd_node *tree;
-
- /* valid data */
- CHECK_PARSE_LYD("<empty xmlns=\"urn:tests:types\"></empty>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "empty", 1, EMPTY, "");
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<empty xmlns=\"urn:tests:types\"/>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "empty", 1, EMPTY, "");
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<tempty xmlns=\"urn:tests:types\"/>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "tempty", 1, EMPTY, "");
- lyd_free_all(tree);
-
- /* invalid value */
- TEST_TYPE_ERROR("empty", "x", "Invalid empty value length 1.", "1");
-
- TEST_TYPE_ERROR("empty", " ", "Invalid empty value length 1.", "1");
-}
-
-static void
-test_identityref(void **state)
-{
- struct lyd_node *tree;
- struct lyd_node_term *leaf;
- const struct lys_module *mod_types;
- const struct lys_module *mod_defs;
-
- assert_non_null(mod_types = ly_ctx_get_module_implemented(UTEST_LYCTX, "types"));
- assert_non_null(mod_defs = ly_ctx_get_module_implemented(UTEST_LYCTX, "defs"));
-
- /* valid data */
- CHECK_PARSE_LYD("<ident xmlns=\"urn:tests:types\">gigabit-ethernet</ident>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "ident", 1, IDENT, "types:gigabit-ethernet", "gigabit-ethernet");
- leaf = (struct lyd_node_term *)tree;
- TEST_PRINTED_VALUE(&leaf->value, "t:gigabit-ethernet", LY_VALUE_SCHEMA, mod_types->parsed);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<ident xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:fast-ethernet</ident>", tree);
- assert_non_null(tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "ident", 1, IDENT, "defs:fast-ethernet", "fast-ethernet");
- leaf = (struct lyd_node_term *)tree;
- TEST_PRINTED_VALUE(&leaf->value, "d:fast-ethernet", LY_VALUE_SCHEMA, mod_defs->parsed);
- lyd_free_all(tree);
-
- /* invalid value */
- TEST_TYPE_ERROR("ident", "fast-ethernet",
- "Invalid identityref \"fast-ethernet\" value - identity not found in module \"types\".", "1");
-
- CHECK_PARSE_LYD_PARAM("<ident xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:slow-ethernet</ident>",
- LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid identityref \"x:slow-ethernet\" value - identity not found in module \"defs\".",
- "Schema location /types:ident, line number 1.");
-
- CHECK_PARSE_LYD_PARAM("<ident xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:crypto-alg</ident>",
- LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid identityref \"x:crypto-alg\" value - identity not derived from the base \"defs:interface-type\".",
- "Schema location /types:ident, line number 1.");
-
- CHECK_PARSE_LYD_PARAM("<ident xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:unknown\">x:fast-ethernet</ident>",
- LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid identityref \"x:fast-ethernet\" value - unable to map prefix to YANG schema.",
- "Schema location /types:ident, line number 1.");
-}
-
-/* dummy get_prefix callback for test_instanceid() */
-const struct lys_module *
-test_instanceid_getprefix(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void * private)
-{
- (void)ctx;
- (void)prefix;
- (void)prefix_len;
-
- return private;
-}
-
-static void
-test_instanceid(void **state)
-{
- struct lyd_node *tree;
- const struct lyd_node_term *leaf;
- const struct lys_module *mod_types;
- const char *data;
-
- assert_non_null(mod_types = ly_ctx_get_module_implemented(UTEST_LYCTX, "types"));
-
- /* valid data */
- CHECK_PARSE_LYD("<cont xmlns=\"urn:tests:types\"><leaftarget/></cont>"
- "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:cont/xdf:leaftarget</xdf:inst>", tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_1[] = {LY_PATH_PREDTYPE_NONE, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:cont/leaftarget", result_1);
- leaf = (struct lyd_node_term *)tree;
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x5, 1, "cont", 1, LYS_CONTAINER, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "leaftarget", 1, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<list xmlns=\"urn:tests:types\"><id>a</id></list><list xmlns=\"urn:tests:types\"><id>b</id></list>"
- "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:list[xdf:id='b']/xdf:id</xdf:inst>", tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_2[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list[id='b']/id", result_2);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x105, 1, "id", 1, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- CHECK_PARSE_LYD("<leaflisttarget xmlns=\"urn:tests:types\">1</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">2</leaflisttarget>"
- "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:leaflisttarget[.='1']</xdf:inst>", tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_3[] = {LY_PATH_PREDTYPE_LEAFLIST};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:leaflisttarget[.='1']", result_3);
- leaf = (struct lyd_node_term *)tree;
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "leaflisttarget", 1, LYS_LEAFLIST, 0, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='a']</id><value>x</value></list_inst>"
- "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='b']</id><value>y</value></list_inst>"
- "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list_inst[a:id=\"/a:leaflisttarget[.='b']\"]/a:value</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_4[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list_inst[id=\"/types:leaflisttarget[.='b']\"]/value", result_4);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list_inst", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "value", 0, LYS_LEAF, 1, 0, NULL, 0);
-
- assert_int_equal(1, LY_ARRAY_COUNT(leaf->value.target[0].predicates));
- assert_null(leaf->value.target[1].predicates);
- TEST_PRINTED_VALUE(&leaf->value, "/t:list_inst[t:id=\"/t:leaflisttarget[.='b']\"]/t:value", LY_VALUE_SCHEMA, mod_types->parsed);
- TEST_PRINTED_VALUE(&leaf->value, "/types:list_inst[id=\"/types:leaflisttarget[.='b']\"]/value", LY_VALUE_JSON, NULL);
- lyd_free_all(tree);
-
- data = "<list xmlns=\"urn:tests:types\"><id>a</id></list><list xmlns=\"urn:tests:types\"><id>b</id><value>x</value></list>"
- "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:list[xdf:id='b']/xdf:value</xdf:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_5[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list[id='b']/value", result_5);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "value", 1, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='a']</id><value>x</value></list_inst>"
- "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='b']</id><value>y</value></list_inst>"
- "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list_inst[a:id=\"/a:leaflisttarget[.='a']\"]/a:value</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_6[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list_inst[id=\"/types:leaflisttarget[.='a']\"]/value", result_6);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list_inst", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "value", 0, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list_ident xmlns=\"urn:tests:types\"><id xmlns:dfs=\"urn:tests:defs\">dfs:ethernet</id><value>x</value></list_ident>"
- "<list_ident xmlns=\"urn:tests:types\"><id xmlns:dfs=\"urn:tests:defs\">dfs:fast-ethernet</id><value>y</value></list_ident>"
- "<a:inst xmlns:a=\"urn:tests:types\" xmlns:d=\"urn:tests:defs\">/a:list_ident[a:id='d:fast-ethernet']/a:value</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_7[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list_ident[id='defs:fast-ethernet']/value", result_7);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list_ident", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "value", 0, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list2 xmlns=\"urn:tests:types\"><id>types:xxx</id><value>x</value></list2>"
- "<list2 xmlns=\"urn:tests:types\"><id>a:xxx</id><value>y</value></list2>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a:xxx'][a:value='y']/a:value</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_8[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list2[id='a:xxx'][value='y']/value", result_8);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list2", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x105, 1, "value", 0, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list xmlns=\"urn:tests:types\"><id>types:xxx</id><value>x</value></list>"
- "<list xmlns=\"urn:tests:types\"><id>a:xxx</id><value>y</value></list>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list[a:id='a:xxx']/a:value</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_9[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list[id='a:xxx']/value", result_9);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x5, 1, "value", 1, LYS_LEAF, 1, 0, NULL, 0);
- lyd_free_all(tree);
-
- data = "<list2 xmlns=\"urn:tests:types\"><id>a</id><value>a</value></list2>"
- "<list2 xmlns=\"urn:tests:types\"><id>c</id><value>b</value></list2>"
- "<list2 xmlns=\"urn:tests:types\"><id>a</id><value>b</value></list2>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a'][a:value='b']/a:id</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_10[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
-
- TEST_PATTERN_1(tree, "inst", 1, INST, "/types:list2[id='a'][value='b']/id", result_10);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(2, LY_ARRAY_COUNT(leaf->value.target));
- CHECK_LYSC_NODE(leaf->value.target[0].node, NULL, 0, 0x85, 1, "list2", 1, LYS_LIST, 0, 0, NULL, 0);
- CHECK_LYSC_NODE(leaf->value.target[1].node, NULL, 0, 0x105, 1, "id", 1, LYS_LEAF, 1, 0, NULL, 0);
- assert_non_null(leaf = lyd_target(leaf->value.target, tree));
- assert_string_equal("a", lyd_get_value(&leaf->node));
- assert_string_equal("b", lyd_get_value(leaf->next));
- lyd_free_all(tree);
-
- /* invalid value */
- data = "<list xmlns=\"urn:tests:types\"><id>a</id></list>"
- "<list xmlns=\"urn:tests:types\"><id>b</id><value>x</value></list>"
- "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:list[2]/xdf:value</xdf:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list[2]/xdf:value\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:1leaftarget</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:1leaftarget\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<t:inst xmlns:t=\"urn:tests:types\">/t:cont:t:1leaftarget</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont:t:1leaftarget\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:invalid/t:path</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:invalid/t:path\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<inst xmlns=\"urn:tests:types\" xmlns:t=\"urn:tests:invalid\">/t:cont/t:leaftarget</inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaftarget\" value - semantic error.", "Schema location /types:inst.");
-
- TEST_TYPE_ERROR("inst", "/cont/leaftarget", "Invalid instance-identifier \"/cont/leaftarget\" value - syntax error.", "1");
-
- /* instance-identifier is here in JSON format because it is already in internal representation without canonical prefixes */
- data = "<cont xmlns=\"urn:tests:types\"/><t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaftarget</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_ENOTFOUND, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:cont/leaftarget\" value - required instance not found.", "Schema location /types:inst, data location /types:inst.");
-
- /* instance-identifier is here in JSON format because it is already in internal representation without canonical prefixes */
- data = "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaftarget</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_ENOTFOUND, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:cont/leaftarget\" value - required instance not found.", "Schema location /types:inst, data location /types:inst.");
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:leaflisttarget[1</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:leaflisttarget[1\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<cont xmlns=\"urn:tests:types\"/><t:inst xmlns:t=\"urn:tests:types\">/t:cont[1]</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont[1]\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<cont xmlns=\"urn:tests:types\"/><t:inst xmlns:t=\"urn:tests:types\">[1]</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"[1]\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<cont xmlns=\"urn:tests:types\"><leaflisttarget>1</leaflisttarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[id='1']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaflisttarget[id='1']\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<cont xmlns=\"urn:tests:types\"><leaflisttarget>1</leaflisttarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[t:id='1']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaflisttarget[t:id='1']\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<cont xmlns=\"urn:tests:types\"><leaflisttarget>1</leaflisttarget><leaflisttarget>2</leaflisttarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[4]</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaflisttarget[4]\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<t:inst-noreq xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[6]</t:inst-noreq>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaflisttarget[6]\" value - semantic error.", "Schema location /types:inst-noreq.");
-
- data = "<cont xmlns=\"urn:tests:types\"><listtarget><id>1</id><value>x</value></listtarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[t:value='x']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:listtarget[t:value='x']\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<t:inst-noreq xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[t:value='x']</t:inst-noreq>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:listtarget[t:value='x']\" value - semantic error.", "Schema location /types:inst-noreq.");
-
- data = "<t:inst-noreq xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[t:x='x']</t:inst-noreq>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_ENOTFOUND, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:listtarget[t:x='x']\" value - semantic error.", "Schema location /types:inst-noreq.");
-
- data = "<cont xmlns=\"urn:tests:types\"><listtarget><id>1</id><value>x</value></listtarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[.='x']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:listtarget[.='x']\" value - semantic error.", "Schema location /types:inst.");
-
- /* instance-identifier is here in JSON format because it is already in internal representation without canonical prefixes */
- data = "<cont xmlns=\"urn:tests:types\"><leaflisttarget>1</leaflisttarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[.='2']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_ENOTFOUND, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:cont/leaflisttarget[.='2']\" value - required instance not found.", "Schema location /types:inst, data location /types:inst.");
-
- data = "<cont xmlns=\"urn:tests:types\"><leaflisttarget>1</leaflisttarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:leaflisttarget[.='x']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:leaflisttarget[.='x']\" value - semantic error.", "Schema location /types:inst.");
-
- data = "<cont xmlns=\"urn:tests:types\"><listtarget><id>1</id><value>x</value></listtarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[t:id='x']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:listtarget[t:id='x']\" value - semantic error.", "Schema location /types:inst.");
-
- /* instance-identifier is here in JSON format because it is already in internal representation without canonical prefixes */
- data = "<cont xmlns=\"urn:tests:types\"><listtarget><id>1</id><value>x</value></listtarget></cont>"
- "<t:inst xmlns:t=\"urn:tests:types\">/t:cont/t:listtarget[t:id='2']</t:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_ENOTFOUND, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:cont/listtarget[id='2']\" value - required instance not found.", "Schema location /types:inst, data location /types:inst.");
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget>"
- "<leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:leaflisttarget[1][2]</a:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/a:leaflisttarget[1][2]\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget>"
- "<leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:leaflisttarget[.='a'][.='b']</a:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/a:leaflisttarget[.='a'][.='b']\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<list xmlns=\"urn:tests:types\"><id>a</id><value>x</value></list>"
- "<list xmlns=\"urn:tests:types\"><id>b</id><value>y</value></list>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list[a:id='a'][a:id='b']/a:value</a:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/a:list[a:id='a'][a:id='b']/a:value\" value - syntax error.", "Schema location /types:inst, line number 1.");
-
- data = "<list2 xmlns=\"urn:tests:types\"><id>a</id><value>x</value></list2>"
- "<list2 xmlns=\"urn:tests:types\"><id>b</id><value>y</value></list2>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a']/a:value</a:inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/a:list2[a:id='a']/a:value\" value - semantic error.", "Schema location /types:inst.");
-
- /* check for validting instance-identifier with a complete data tree */
- data = "<list2 xmlns=\"urn:tests:types\"><id>a</id><value>a</value></list2>"
- "<list2 xmlns=\"urn:tests:types\"><id>c</id><value>b</value></list2>"
- "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget>"
- "<leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a'][a:value='a']/a:id</a:inst>";
- CHECK_PARSE_LYD(data, tree);
- /* key-predicate */
- data = "/types:list2[id='a'][value='b']/id";
- assert_int_equal(LY_ENOTFOUND, lyd_value_validate(UTEST_LYCTX, tree->prev->schema, data, strlen(data), tree->prev,
- NULL, NULL));
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:list2[id='a'][value='b']/id\" value - required instance not found.", "Data location /types:inst.");
- /* leaf-list-predicate */
- data = "/types:leaflisttarget[.='c']";
- assert_int_equal(LY_ENOTFOUND, lyd_value_validate(UTEST_LYCTX, tree->prev->schema, data, strlen(data), tree->prev,
- NULL, NULL));
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:leaflisttarget[.='c']\" value - required instance not found.", "Data location /types:inst.");
- /* position predicate */
- data = "/types:list_keyless[4]";
- assert_int_equal(LY_ENOTFOUND, lyd_value_validate(UTEST_LYCTX, tree->prev->schema, data, strlen(data), tree->prev,
- NULL, NULL));
- CHECK_LOG_CTX("Invalid instance-identifier \"/types:list_keyless[4]\" value - required instance not found.", "Data location /types:inst.");
-
- lyd_free_all(tree);
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
- "<inst xmlns=\"urn:tests:types\">/a:leaflisttarget[1]</inst>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid instance-identifier \"/a:leaflisttarget[1]\" value - semantic error.", "Schema location /types:inst.");
-}
-
-static void
-test_leafref(void **state)
-{
- struct lyd_node *tree;
- struct lyd_node_term *leaf;
- const char *data;
-
- /* types:lref: /leaflisttarget */
- /* types:lref2: ../list[id = current()/../str-norestr]/targets */
-
- const char *schema = "module leafrefs {yang-version 1.1; namespace urn:tests:leafrefs; prefix lr; import types {prefix t;}"
- "container c { container x {leaf x {type string;}} list l {key \"id value\"; leaf id {type string;} leaf value {type string;}"
- "leaf lr1 {type leafref {path \"../../../t:str-norestr\"; require-instance true;}}"
- "leaf lr2 {type leafref {path \"../../l[id=current()/../../../t:str-norestr][value=current()/../../../t:str-norestr]/value\"; require-instance true;}}"
- "leaf lr3 {type leafref {path \"/t:list[t:id=current ( )/../../x/x]/t:targets\";}}"
- "}}}";
-
- /* additional schema */
- UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
-
- /* valid data */
- data = "<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget>"
- "<leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget>"
- "<lref xmlns=\"urn:tests:types\">y</lref>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "lref", 1, LYS_LEAF, 0, 0, NULL, 0);
- leaf = (struct lyd_node_term *)tree;
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 0, 1, STRING, "y");
- lyd_free_all(tree);
-
- data = "<list xmlns=\"urn:tests:types\"><id>x</id><targets>a</targets><targets>b</targets></list>"
- "<list xmlns=\"urn:tests:types\"><id>y</id><targets>x</targets><targets>y</targets></list>"
- "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr><lref2 xmlns=\"urn:tests:types\">y</lref2>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "lref2", 1, LYS_LEAF, 0, 0, NULL, 0);
- leaf = (struct lyd_node_term *)tree;
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 0, 1, STRING, "y");
- lyd_free_all(tree);
-
- data = "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr>"
- "<c xmlns=\"urn:tests:leafrefs\"><l><id>x</id><value>x</value><lr1>y</lr1></l></c>";
- CHECK_PARSE_LYD(data, tree);
- CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "c", 0, LYS_CONTAINER, 0, 0, NULL, 0);
- leaf = (struct lyd_node_term *)(lyd_child(lyd_child(tree)->next)->prev);
- CHECK_LYSC_NODE(leaf->schema, NULL, 0, 0x5, 1, "lr1", 1, LYS_LEAF, 1, 0, NULL, 0);
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 1, 1, STRING, "y");
- lyd_free_all(tree);
-
- data = "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr>"
- "<c xmlns=\"urn:tests:leafrefs\"><l><id>y</id><value>y</value></l>"
- "<l><id>x</id><value>x</value><lr2>y</lr2></l></c>";
- CHECK_PARSE_LYD(data, tree);
- CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "c", 0, LYS_CONTAINER, 0, 0, NULL, 0);
- leaf = (struct lyd_node_term *)(lyd_child(lyd_child(tree)->prev)->prev);
- CHECK_LYSC_NODE(leaf->schema, NULL, 0, 0x5, 1, "lr2", 1, LYS_LEAF, 1, 0, NULL, 0);
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 1, 1, STRING, "y");
- lyd_free_all(tree);
-
- data = "<list xmlns=\"urn:tests:types\"><id>x</id><targets>a</targets><targets>b</targets></list>"
- "<list xmlns=\"urn:tests:types\"><id>y</id><targets>c</targets><targets>d</targets></list>"
- "<c xmlns=\"urn:tests:leafrefs\"><x><x>y</x></x>"
- "<l><id>x</id><value>x</value><lr3>c</lr3></l></c>";
- CHECK_PARSE_LYD(data, tree);
- CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "c", 0, LYS_CONTAINER, 0, 0, NULL, 0);
- leaf = (struct lyd_node_term *)(lyd_child(lyd_child(tree)->prev)->prev);
- CHECK_LYSC_NODE(leaf->schema, NULL, 0, 0x5, 1, "lr3", 0, LYS_LEAF, 1, 0, NULL, 0);
- CHECK_LYD_NODE_TERM(leaf, 0, 0, 0, 1, 1, STRING, "c");
- lyd_free_all(tree);
-
- /* invalid value */
- data = "<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><lref xmlns=\"urn:tests:types\">y</lref>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"y\" - "
- "no target instance \"/leaflisttarget\" with the same value.",
- "Schema location /types:lref, data location /types:lref.");
-
- data = "<list xmlns=\"urn:tests:types\"><id>x</id><targets>a</targets><targets>b</targets></list>"
- "<list xmlns=\"urn:tests:types\"><id>y</id><targets>x</targets><targets>y</targets></list>"
- "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr><lref2 xmlns=\"urn:tests:types\">b</lref2>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"b\" - "
- "no target instance \"../list[id = current()/../str-norestr]/targets\" with the same value.",
- "Schema location /types:lref2, data location /types:lref2.");
-
- data = "<list xmlns=\"urn:tests:types\"><id>x</id><targets>a</targets><targets>b</targets></list>"
- "<list xmlns=\"urn:tests:types\"><id>y</id><targets>x</targets><targets>y</targets></list>"
- "<lref2 xmlns=\"urn:tests:types\">b</lref2>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"b\" - "
- "no existing target instance \"../list[id = current()/../str-norestr]/targets\".",
- "Schema location /types:lref2, data location /types:lref2.");
-
- data = "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr><lref2 xmlns=\"urn:tests:types\">b</lref2>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"b\" - "
- "no existing target instance \"../list[id = current()/../str-norestr]/targets\".",
- "Schema location /types:lref2, data location /types:lref2.");
-
- data = "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr>"
- "<c xmlns=\"urn:tests:leafrefs\"><l><id>x</id><value>x</value><lr1>a</lr1></l></c>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"a\" - no target instance \"../../../t:str-norestr\" with the same value.",
- "Schema location /leafrefs:c/l/lr1, data location /leafrefs:c/l[id='x'][value='x']/lr1.");
-
- data = "<str-norestr xmlns=\"urn:tests:types\">z</str-norestr>"
- "<c xmlns=\"urn:tests:leafrefs\"><l><id>y</id><value>y</value></l>"
- "<l><id>x</id><value>x</value><lr2>z</lr2></l></c>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
- CHECK_LOG_CTX("Invalid leafref value \"z\" - no existing target instance \"../../l[id=current()/../../../t:str-norestr]"
- "[value=current()/../../../t:str-norestr]/value\".",
- "Schema location /leafrefs:c/l/lr2, data location /leafrefs:c/l[id='x'][value='x']/lr2.");
-
- data = "<lref xmlns=\"urn:tests:types\">%n</lref>";
- CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
-}
-
-static void
-test_union(void **state)
-{
- struct lyd_node *tree;
- struct lyd_node_term *leaf;
- const char *data;
- const struct lys_module *mod_defs;
-
- assert_non_null(mod_defs = ly_ctx_get_module_implemented(UTEST_LYCTX, "defs"));
-
- /*
- * leaf un1 {type union {
- * type leafref {path /int8; require-instance true;}
- * type union {
- * type identityref {base defs:interface-type;}
- * type instance-identifier {require-instance true;}
- * }
- * type string {range 1..20;};
- * }
- * }
- */
-
- /* valid data */
- data = "<int8 xmlns=\"urn:tests:types\">12</int8><un1 xmlns=\"urn:tests:types\">12</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->next->next;
- TEST_PATTERN_1(tree, "un1", 0, UNION, "12", INT8, "12", 12);
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 0);
- TEST_PRINTED_VALUE(&leaf->value, "12", LY_VALUE_SCHEMA, NULL);
- lyd_free_all(tree);
-
- data = "<int8 xmlns=\"urn:tests:types\">12</int8><un1 xmlns=\"urn:tests:types\">2</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->next->next;
- TEST_PATTERN_1(tree, "un1", 0, UNION, "2", STRING, "2");
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 0);
- TEST_PRINTED_VALUE(&leaf->value, "2", LY_VALUE_SCHEMA, NULL);
- lyd_free_all(tree);
-
- data = "<un1 xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:fast-ethernet</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "un1", 0, UNION, "defs:fast-ethernet", IDENT, "defs:fast-ethernet", "fast-ethernet");
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 1);
- TEST_PRINTED_VALUE(&leaf->value, "d:fast-ethernet", LY_VALUE_SCHEMA, mod_defs->parsed);
- TEST_PRINTED_VALUE(&leaf->value.subvalue->value, "d:fast-ethernet", LY_VALUE_SCHEMA, mod_defs->parsed);
- lyd_free_all(tree);
-
- data = "<un1 xmlns=\"urn:tests:types\" xmlns:d=\"urn:tests:defs\">d:superfast-ethernet</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->next;
- TEST_PATTERN_1(tree, "un1", 0, UNION, "d:superfast-ethernet", STRING, "d:superfast-ethernet");
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 1);
- lyd_free_all(tree);
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget>"
- "<un1 xmlns=\"urn:tests:types\" xmlns:a=\"urn:tests:types\">/a:leaflisttarget[.='y']</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- const enum ly_path_pred_type result_1[] = {LY_PATH_PREDTYPE_LEAFLIST};
-
- TEST_PATTERN_1(tree, "un1", 0, UNION, "/types:leaflisttarget[.='y']", INST, "/types:leaflisttarget[.='y']", result_1);
- leaf = (struct lyd_node_term *)tree;
-
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 1);
- lyd_free_all(tree);
-
- data = "<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget>"
- "<un1 xmlns=\"urn:tests:types\" xmlns:a=\"urn:tests:types\">/a:leaflisttarget[3]</un1>";
- CHECK_PARSE_LYD(data, tree);
- tree = tree->prev;
- TEST_PATTERN_1(tree, "un1", 0, UNION, "/a:leaflisttarget[3]", STRING, "/a:leaflisttarget[3]");
- leaf = (struct lyd_node_term *)tree;
- assert_int_equal(((struct ly_set *)leaf->value.subvalue->prefix_data)->count, 1);
- lyd_free_all(tree);
-
- TEST_TYPE_ERROR("un1", "123456789012345678901",
- "Invalid union value \"123456789012345678901\" - no matching subtype found.", "1");
-}
-
-int
-main(void)
-{
- const struct CMUnitTest tests[] = {
- UTEST(test_int, setup),
- UTEST(test_uint, setup),
- UTEST(test_dec64, setup),
- /* UTEST(test_string, setup),*/
- /* UTEST(test_bits, setup), */
- UTEST(test_enums, setup),
- /* UTEST(test_binary, setup), */
- UTEST(test_boolean, setup),
- UTEST(test_empty, setup),
- UTEST(test_identityref, setup),
- UTEST(test_instanceid, setup),
- UTEST(test_leafref, setup),
- UTEST(test_union, setup),
- };
-
- return cmocka_run_group_tests(tests, NULL, NULL);
-}
diff --git a/tests/utests/types/boolean.c b/tests/utests/types/boolean.c
index 5b0ac5c..6d2eb6c 100644
--- a/tests/utests/types/boolean.c
+++ b/tests/utests/types/boolean.c
@@ -27,6 +27,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML(MOD_NAME, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, NODE_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
@@ -44,6 +61,33 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "typedef tboolean {type boolean;}"
+ "leaf l1 {type boolean;}"
+ "leaf l2 {type tboolean;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "l1", "true", BOOL, "true", 1);
+
+ TEST_SUCCESS_XML("defs", "l1", "false", BOOL, "false", 0);
+
+ TEST_SUCCESS_XML("defs", "l2", "false", BOOL, "false", 0);
+
+ /* invalid value */
+ TEST_ERROR_XML("defs", "l1", "unsure");
+ CHECK_LOG_CTX("Invalid boolean value \"unsure\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", " true");
+ CHECK_LOG_CTX("Invalid boolean value \" true\".",
+ "Schema location /defs:l1, line number 1.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -59,6 +103,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/dec64.c b/tests/utests/types/dec64.c
new file mode 100644
index 0000000..44da16e
--- /dev/null
+++ b/tests/utests/types/dec64.c
@@ -0,0 +1,102 @@
+/**
+ * @file dec64.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for decimal64 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, NODE_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf l1 {type decimal64 {fraction-digits 1; range 1.5..10;}}"
+ "leaf l2 {type decimal64 {fraction-digits 18;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "l1", "\n +8 \t\n ", DEC64, "8.0", 80);
+ TEST_SUCCESS_XML("defs", "l1", "8.00", DEC64, "8.0", 80);
+
+ TEST_SUCCESS_XML("defs", "l2", "-9.223372036854775808", DEC64, "-9.223372036854775808",
+ INT64_C(-9223372036854775807) - INT64_C(1));
+ TEST_SUCCESS_XML("defs", "l2", "9.223372036854775807", DEC64, "9.223372036854775807", INT64_C(9223372036854775807));
+
+ TEST_ERROR_XML("defs", "l1", "\n 15 \t\n ");
+ CHECK_LOG_CTX("Unsatisfied range - value \"15.0\" is out of the allowed range.",
+ "Schema location /defs:l1, line number 3.");
+
+ TEST_ERROR_XML("defs", "l1", "\n 0 \t\n ");
+ CHECK_LOG_CTX("Unsatisfied range - value \"0.0\" is out of the allowed range.",
+ "Schema location /defs:l1, line number 3.");
+
+ TEST_ERROR_XML("defs", "l1", "xxx");
+ CHECK_LOG_CTX("Invalid 1. character of decimal64 value \"xxx\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", "");
+ CHECK_LOG_CTX("Invalid empty decimal64 value.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", "8.5 xxx");
+ CHECK_LOG_CTX("Invalid 6. character of decimal64 value \"8.5 xxx\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", "8.55 xxx");
+ CHECK_LOG_CTX("Value \"8.55\" of decimal64 type exceeds defined number (1) of fraction digits.",
+ "Schema location /defs:l1, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/empty.c b/tests/utests/types/empty.c
index 3fd5583..673068c 100644
--- a/tests/utests/types/empty.c
+++ b/tests/utests/types/empty.c
@@ -27,6 +27,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML(MOD_NAME, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, NODE_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
@@ -44,6 +61,31 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "typedef tempty {type empty;}"
+ "leaf l1 {type empty;}"
+ "leaf l2 {type tempty;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "l1", "", EMPTY, "");
+
+ TEST_SUCCESS_XML("defs", "l2", "", EMPTY, "");
+
+ /* invalid value */
+ TEST_ERROR_XML("defs", "l1", "x");
+ CHECK_LOG_CTX("Invalid empty value length 1.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", " ");
+ CHECK_LOG_CTX("Invalid empty value length 1.",
+ "Schema location /defs:l1, line number 1.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -58,6 +100,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/enumeration.c b/tests/utests/types/enumeration.c
index 47d02d1..01a6d7d 100644
--- a/tests/utests/types/enumeration.c
+++ b/tests/utests/types/enumeration.c
@@ -27,6 +27,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML(MOD_NAME, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, NODE_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
@@ -44,20 +61,52 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "feature f; leaf l1 {type enumeration {enum white; enum yellow {if-feature f;}}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "l1", "white", ENUM, "white", "white");
+
+ /* disabled feature */
+ TEST_ERROR_XML("defs", "l1", "yellow");
+ CHECK_LOG_CTX("Invalid enumeration value \"yellow\".",
+ "Schema location /defs:l1, line number 1.");
+
+ /* leading/trailing whitespaces */
+ TEST_ERROR_XML("defs", "l1", " white");
+ CHECK_LOG_CTX("Invalid enumeration value \" white\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "l1", "white\n");
+ CHECK_LOG_CTX("Invalid enumeration value \"white\n\".",
+ "Schema location /defs:l1, line number 2.");
+
+ /* invalid value */
+ TEST_ERROR_XML("defs", "l1", "black");
+ CHECK_LOG_CTX("Invalid enumeration value \"black\".",
+ "Schema location /defs:l1, line number 1.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
- schema = MODULE_CREATE_YANG("lyb", "leaf port {type enumeration {enum white; enum yellow; enum black;}}");
+ schema = MODULE_CREATE_YANG("lyb", "leaf l1 {type enumeration {enum white; enum yellow; enum black;}}");
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
- TEST_SUCCESS_LYB("lyb", "port", "white");
- TEST_SUCCESS_LYB("lyb", "port", "black");
+ TEST_SUCCESS_LYB("lyb", "l1", "white");
+ TEST_SUCCESS_LYB("lyb", "l1", "black");
}
int
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/identityref.c b/tests/utests/types/identityref.c
index db73bde..5f710b4 100644
--- a/tests/utests/types/identityref.c
+++ b/tests/utests/types/identityref.c
@@ -27,6 +27,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML(MOD_NAME, NAMESPACES, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, NAMESPACES, NODE_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
@@ -44,6 +61,44 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema, *schema2;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("ident-base", "identity ident-base;"
+ "identity ident-imp {base ident-base;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ schema2 = MODULE_CREATE_YANG("defs", "import ident-base {prefix ib;}"
+ "identity ident1 {base ib:ident-base;}"
+ "leaf l1 {type identityref {base ib:ident-base;}}");
+ UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "", "l1", "ident1", IDENT, "defs:ident1", "ident1");
+
+ TEST_SUCCESS_XML("defs", "xmlns:i=\"urn:tests:ident-base\"", "l1", "i:ident-imp", IDENT, "ident-base:ident-imp",
+ "ident-imp");
+
+ /* invalid value */
+ TEST_ERROR_XML("defs", "", "l1", "fast-ethernet");
+ CHECK_LOG_CTX("Invalid identityref \"fast-ethernet\" value - identity not found in module \"defs\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "xmlns:x=\"urn:tests:defs\"", "l1", "x:slow-ethernet");
+ CHECK_LOG_CTX("Invalid identityref \"x:slow-ethernet\" value - identity not found in module \"defs\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "xmlns:x=\"urn:tests:ident-base\"", "l1", "x:ident-base");
+ CHECK_LOG_CTX("Invalid identityref \"x:ident-base\" value - identity not derived from the base \"ident-base:ident-base\".",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML("defs", "xmlns:x=\"urn:tests:unknown\"", "l1", "x:ident-base");
+ CHECK_LOG_CTX("Invalid identityref \"x:ident-base\" value - unable to map prefix to YANG schema.",
+ "Schema location /defs:l1, line number 1.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -60,6 +115,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/instanceid.c b/tests/utests/types/instanceid.c
index 81f198b..8bb1e66 100644
--- a/tests/utests/types/instanceid.c
+++ b/tests/utests/types/instanceid.c
@@ -18,6 +18,7 @@
/* LOCAL INCLUDE HEADERS */
#include "libyang.h"
+#include "path.h"
#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
"module " MOD_NAME " {\n" \
@@ -27,6 +28,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = XML1 "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 1, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, RET) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, RET, tree); \
+ assert_null(tree); \
+ }
+
#define LYB_CHECK_START \
struct lyd_node *tree_1; \
struct lyd_node *tree_2; \
@@ -58,6 +76,190 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema, *schema2;
+ const enum ly_path_pred_type val1[] = {LY_PATH_PREDTYPE_NONE, LY_PATH_PREDTYPE_NONE};
+ const enum ly_path_pred_type val2[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
+ const enum ly_path_pred_type val3[] = {LY_PATH_PREDTYPE_LEAFLIST};
+ const enum ly_path_pred_type val4[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
+ const enum ly_path_pred_type val5[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
+ const enum ly_path_pred_type val6[] = {LY_PATH_PREDTYPE_LIST, LY_PATH_PREDTYPE_NONE};
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("mod", "container cont {leaf l2 {type empty;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ schema2 = MODULE_CREATE_YANG("defs", "identity ident; identity ident-der1 {base ident;} identity ident-der2 {base ident;}"
+ "leaf l1 {type instance-identifier {require-instance true;}}"
+ "leaf l2 {type instance-identifier {require-instance false;}}"
+ "container cont {leaf l {type empty;}}"
+ "list list {key \"id\"; leaf id {type string;} leaf value {type string;}}"
+ "leaf-list llist {type uint32;}"
+ "list list-inst {key \"id\"; leaf id {type instance-identifier;} leaf value {type string;}}"
+ "list list-ident {key \"id\"; leaf id {type identityref {base ident;}} leaf value {type string;}}"
+ "list list2 {key \"id id2\"; leaf id {type string;} leaf id2 {type string;}}"
+ "list list-keyless {config false; leaf value {type string;}}");
+ UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML2("<cont xmlns=\"urn:tests:defs\"><l/></cont>", "defs", "xmlns:xdf=\"urn:tests:defs\"", "l1",
+ "/xdf:cont/xdf:l", INST, "/defs:cont/l", val1);
+
+ TEST_SUCCESS_XML2("<list xmlns=\"urn:tests:defs\"><id>a</id></list><list xmlns=\"urn:tests:defs\"><id>b</id></list>",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/xdf:list[xdf:id='b']/xdf:id", INST,
+ "/defs:list[id='b']/id", val2);
+
+ TEST_SUCCESS_XML2("<llist xmlns=\"urn:tests:defs\">1</llist><llist xmlns=\"urn:tests:defs\">2</llist>",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/xdf:llist[.='1']", INST, "/defs:llist[.='1']", val3);
+
+ TEST_SUCCESS_XML2("<list-inst xmlns=\"urn:tests:defs\"><id xmlns:b=\"urn:tests:defs\">/b:llist[.='1']</id>"
+ "<value>x</value></list-inst>"
+ "<list-inst xmlns=\"urn:tests:defs\"><id xmlns:b=\"urn:tests:defs\">/b:llist[.='2']</id>"
+ "<value>y</value></list-inst>"
+ "<llist xmlns=\"urn:tests:defs\">1</llist><llist xmlns=\"urn:tests:defs\">2</llist>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "a:l1", "/a:list-inst[a:id=\"/a:llist[.='1']\"]/a:value",
+ INST, "/defs:list-inst[id=\"/defs:llist[.='1']\"]/value", val4);
+
+ TEST_SUCCESS_XML2("<list-ident xmlns=\"urn:tests:defs\"><id xmlns:b=\"urn:tests:defs\">b:ident-der1</id>"
+ "<value>x</value></list-ident>"
+ "<list-ident xmlns=\"urn:tests:defs\"><id xmlns:b=\"urn:tests:defs\">b:ident-der2</id>"
+ "<value>y</value></list-ident>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "a:l1", "/a:list-ident[a:id='a:ident-der1']/a:value",
+ INST, "/defs:list-ident[id='defs:ident-der1']/value", val5);
+
+ TEST_SUCCESS_XML2("<list2 xmlns=\"urn:tests:defs\"><id>defs:xxx</id><id2>x</id2></list2>"
+ "<list2 xmlns=\"urn:tests:defs\"><id>a:xxx</id><id2>y</id2></list2>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "a:l1", "/a:list2[a:id='a:xxx'][a:id2='y']/a:id2",
+ INST, "/defs:list2[id='a:xxx'][id2='y']/id2", val6);
+
+ /* syntax/semantic errors */
+ TEST_ERROR_XML2("<list xmlns=\"urn:tests:defs\"><id>a</id></list>"
+ "<list xmlns=\"urn:tests:defs\"><id>b</id><value>x</value></list>",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/xdf:list[2]/xdf:value", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list[2]/xdf:value\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/t:cont/t:1l", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont/t:1l\" value - syntax error.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/t:cont:t:1l", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:cont:t:1l\" value - syntax error.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l1", "/xdf:cont/xdf:invalid/xdf:path", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:cont/xdf:invalid/xdf:path\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ /* non-existing instances, instance-identifier is here in JSON format because it is already in internal
+ * representation without canonical prefixes */
+ TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"/>",
+ "defs", "xmlns:m=\"urn:tests:mod\"", "l1", "/m:cont/m:l2", LY_ENOTFOUND);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/mod:cont/l2\" value - required instance not found.",
+ "Schema location /defs:l1, data location /defs:l1.");
+
+ TEST_ERROR_XML2("<llist xmlns=\"urn:tests:defs\">1</llist>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:llist[.='2']", LY_ENOTFOUND);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/defs:llist[.='2']\" value - required instance not found.",
+ "Schema location /defs:l1, data location /defs:l1.");
+
+ TEST_ERROR_XML2("<list2 xmlns=\"urn:tests:defs\"><id>a</id><id2>a</id2></list2>"
+ "<list2 xmlns=\"urn:tests:defs\"><id>c</id><id2>b</id2></list2>"
+ "<llist xmlns=\"urn:tests:defs\">a</llist>"
+ "<llist xmlns=\"urn:tests:defs\">b</llist>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:list2[a:id='a'][a:id2='a']/a:id", LY_ENOTFOUND);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/defs:list2[id='a'][id2='a']/id\" value - required instance not found.",
+ "Schema location /defs:l1, data location /defs:l1.");
+
+ TEST_ERROR_XML2("<list2 xmlns=\"urn:tests:defs\"><id>a</id><id2>a</id2></list2>"
+ "<list2 xmlns=\"urn:tests:defs\"><id>c</id><id2>b</id2></list2>"
+ "<llist xmlns=\"urn:tests:defs\">1</llist>"
+ "<llist xmlns=\"urn:tests:defs\">2</llist>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:llist[.='3']", LY_ENOTFOUND);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/defs:llist[.='3']\" value - required instance not found.",
+ "Schema location /defs:l1, data location /defs:l1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:list-keyless[3]", LY_ENOTFOUND);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/defs:list-keyless[3]\" value - required instance not found.",
+ "Schema location /defs:l1, data location /defs:l1.");
+
+ /* more errors */
+ TEST_ERROR_XML2("<llist xmlns=\"urn:tests:defs\">x</llist>",
+ "defs", "xmlns:t=\"urn:tests:defs\"", "t:l1", "/t:llist[1", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[1\" value - syntax error.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"/>",
+ "defs", "xmlns:m=\"urn:tests:mod\"", "l1", "/m:cont[1]", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/m:cont[1]\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"/>",
+ "defs", "xmlns:m=\"urn:tests:mod\"", "l1", "[1]", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"[1]\" value - syntax error.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"><l2/></cont>",
+ "defs", "xmlns:m=\"urn:tests:mod\"", "l1", "/m:cont/m:l2[l2='1']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/m:cont/m:l2[l2='1']\" value - syntax error.",
+ "Schema location /defs:l1, line number 1.");
+
+ TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"><l2/></cont>",
+ "defs", "xmlns:m=\"urn:tests:mod\"", "l1", "/m:cont/m:l2[m:l2='1']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/m:cont/m:l2[m:l2='1']\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ TEST_ERROR_XML2("<llist xmlns=\"urn:tests:defs\">1</llist><llist xmlns=\"urn:tests:defs\">2</llist>",
+ "defs", "xmlns:t=\"urn:tests:defs\"", "t:l1", "/t:llist[4]", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[4]\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/t:llist[6]", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[6]\" value - semantic error.",
+ "Schema location /defs:l2.");
+
+ TEST_ERROR_XML2("<list xmlns=\"urn:tests:defs\"><id>1</id><value>x</value></list>",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/xdf:list[xdf:value='x']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list[xdf:value='x']\" value - semantic error.",
+ "Schema location /defs:l2.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/xdf:list[.='x']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list[.='x']\" value - semantic error.",
+ "Schema location /defs:l2.");
+
+ TEST_ERROR_XML2("<llist xmlns=\"urn:tests:defs\">1</llist>",
+ "defs", "xmlns:t=\"urn:tests:defs\"", "t:l1", "/t:llist[.='x']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[.='x']\" value - semantic error.",
+ "Schema location /defs:l1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/t:llist[1][2]", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[1][2]\" value - syntax error.",
+ "Schema location /defs:l2, line number 1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/t:llist[.='a'][.='b']", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/t:llist[.='a'][.='b']\" value - syntax error.",
+ "Schema location /defs:l2, line number 1.");
+
+ TEST_ERROR_XML2("<list xmlns=\"urn:tests:defs\"><id>1</id><value>x</value></list>",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/xdf:list[xdf:id='1'][xdf:id='2']/xdf:value", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list[xdf:id='1'][xdf:id='2']/xdf:value\" value - syntax error.",
+ "Schema location /defs:l2, line number 1.");
+
+ TEST_ERROR_XML2("",
+ "defs", "xmlns:xdf=\"urn:tests:defs\"", "xdf:l2", "/xdf:list2[xdf:id='1']/xdf:value", LY_EVALID);
+ CHECK_LOG_CTX("Invalid instance-identifier \"/xdf:list2[xdf:id='1']/xdf:value\" value - semantic error.",
+ "Schema location /defs:l2.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -82,6 +284,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/int16.c b/tests/utests/types/int16.c
new file mode 100644
index 0000000..7d021be
--- /dev/null
+++ b/tests/utests/types/int16.c
@@ -0,0 +1,75 @@
+/**
+ * @file int16.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for int16 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type int16 {range -20..-10;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "100");
+ CHECK_LOG_CTX("Unsatisfied range - value \"100\" is out of the allowed range.",
+ "Schema location /defs:port, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/int32.c b/tests/utests/types/int32.c
new file mode 100644
index 0000000..f614c62
--- /dev/null
+++ b/tests/utests/types/int32.c
@@ -0,0 +1,75 @@
+/**
+ * @file int32.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for int32 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type int32;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "0x01");
+ CHECK_LOG_CTX("Invalid int32 value \"0x01\".",
+ "Schema location /defs:port, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/int64.c b/tests/utests/types/int64.c
new file mode 100644
index 0000000..a5eafd2
--- /dev/null
+++ b/tests/utests/types/int64.c
@@ -0,0 +1,83 @@
+/**
+ * @file int64.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for int32 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type int64;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "");
+ CHECK_LOG_CTX("Invalid empty int64 value.",
+ "Schema location /defs:port, line number 1.");
+
+ TEST_ERROR_XML("defs", " ");
+ CHECK_LOG_CTX("Invalid empty int64 value.",
+ "Schema location /defs:port, line number 1.");
+
+ TEST_ERROR_XML("defs", "-10 xxx");
+ CHECK_LOG_CTX("Invalid int64 value \"-10 xxx\".",
+ "Schema location /defs:port, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/leafref.c b/tests/utests/types/leafref.c
index 41d5d45..74c618c 100644
--- a/tests/utests/types/leafref.c
+++ b/tests/utests/types/leafref.c
@@ -27,6 +27,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = XML1 "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 1, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, RET) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = XML1 "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, RET, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME1, DATA1, NODE_NAME2, DATA2) \
{ \
struct lyd_node *tree_1; \
@@ -45,6 +62,102 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema, *schema2, *data2;
+ struct lyd_node *tree2;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}"
+ "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}"
+ "leaf str-norestr {type string;}"
+ "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}"
+ "container cont {leaf leaftarget {type empty;}"
+ " list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}}"
+ " leaf-list leaflisttarget {type uint8; max-elements 5;}}"
+ "leaf-list leaflisttarget {type string;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ schema2 = MODULE_CREATE_YANG("leafrefs", "import defs {prefix t;}"
+ "container c { container x {leaf x {type string;}} list l {"
+ " key \"id value\"; leaf id {type string;} leaf value {type string;}"
+ " leaf lr1 {type leafref {path \"../../../t:str-norestr\"; require-instance true;}}"
+ " leaf lr2 {type leafref {path \"../../l[id=current()/../../../t:str-norestr]\" +"
+ " \"[value=current()/../../../t:str-norestr]/value\"; require-instance true;}}"
+ " leaf lr3 {type leafref {path \"/t:list[t:id=current ( )/../../x/x]/t:targets\";}}"
+ "}}");
+ UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML2("<leaflisttarget xmlns=\"urn:tests:defs\">x</leaflisttarget>"
+ "<leaflisttarget xmlns=\"urn:tests:defs\">y</leaflisttarget>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "a:lref", "y", STRING, "y");
+
+ TEST_SUCCESS_XML2("<list xmlns=\"urn:tests:defs\"><id>x</id><targets>a</targets><targets>b</targets></list>"
+ "<list xmlns=\"urn:tests:defs\"><id>y</id><targets>x</targets><targets>y</targets></list>"
+ "<str-norestr xmlns=\"urn:tests:defs\">y</str-norestr>",
+ "defs", "xmlns:a=\"urn:tests:defs\"", "a:lref2", "y", STRING, "y");
+
+ data2 = "<str-norestr xmlns=\"urn:tests:defs\">y</str-norestr>"
+ "<c xmlns=\"urn:tests:leafrefs\"><l><id>x</id><value>x</value><lr1>y</lr1></l></c>";
+ CHECK_PARSE_LYD_PARAM(data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree2);
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)lyd_child(lyd_child(tree2->next->next)->next)->next->next,
+ 0, 0, 0, 1, 1, STRING, "y");
+ lyd_free_all(tree2);
+
+ data2 = "<list xmlns=\"urn:tests:defs\"><id>x</id><targets>a</targets><targets>b</targets></list>"
+ "<list xmlns=\"urn:tests:defs\"><id>y</id><targets>c</targets><targets>d</targets></list>"
+ "<c xmlns=\"urn:tests:leafrefs\"><x><x>y</x></x>"
+ "<l><id>x</id><value>x</value><lr3>c</lr3></l></c>";
+ CHECK_PARSE_LYD_PARAM(data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree2);
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)lyd_child(lyd_child(tree2->next->next->next)->next)->next->next,
+ 0, 0, 0, 1, 1, STRING, "c");
+ lyd_free_all(tree2);
+
+ /* invalid value */
+ TEST_ERROR_XML2("<leaflisttarget xmlns=\"urn:tests:defs\">x</leaflisttarget>",
+ "defs", "", "lref", "y", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"y\" - no target instance \"/leaflisttarget\" with the same value.",
+ "Schema location /defs:lref, data location /defs:lref.");
+
+ TEST_ERROR_XML2("<list xmlns=\"urn:tests:defs\"><id>x</id><targets>a</targets><targets>b</targets></list>"
+ "<list xmlns=\"urn:tests:defs\"><id>y</id><targets>x</targets><targets>y</targets></list>"
+ "<str-norestr xmlns=\"urn:tests:defs\">y</str-norestr>",
+ "defs", "", "lref2", "b", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"b\" - "
+ "no target instance \"../list[id = current()/../str-norestr]/targets\" with the same value.",
+ "Schema location /defs:lref2, data location /defs:lref2.");
+
+ TEST_ERROR_XML2("<list xmlns=\"urn:tests:defs\"><id>x</id><targets>a</targets><targets>b</targets></list>"
+ "<list xmlns=\"urn:tests:defs\"><id>y</id><targets>x</targets><targets>y</targets></list>",
+ "defs", "", "lref2", "b", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"b\" - "
+ "no existing target instance \"../list[id = current()/../str-norestr]/targets\".",
+ "Schema location /defs:lref2, data location /defs:lref2.");
+
+ TEST_ERROR_XML2("<str-norestr xmlns=\"urn:tests:defs\">y</str-norestr>",
+ "defs", "", "lref2", "b", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"b\" - "
+ "no existing target instance \"../list[id = current()/../str-norestr]/targets\".",
+ "Schema location /defs:lref2, data location /defs:lref2.");
+
+ TEST_ERROR_XML2("<str-norestr xmlns=\"urn:tests:defs\">y</str-norestr>",
+ "leafrefs", "", "c", "<l><id>x</id><value>x</value><lr1>a</lr1></l>", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"a\" - no target instance \"../../../t:str-norestr\" with the same value.",
+ "Schema location /leafrefs:c/l/lr1, data location /leafrefs:c/l[id='x'][value='x']/lr1.");
+
+ TEST_ERROR_XML2("<str-norestr xmlns=\"urn:tests:defs\">z</str-norestr>",
+ "leafrefs", "", "c", "<l><id>y</id><value>y</value></l><l><id>x</id><value>x</value><lr2>z</lr2></l>", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"z\" - no existing target instance \"../../l[id=current()/../../../t:str-norestr]"
+ "[value=current()/../../../t:str-norestr]/value\".",
+ "Schema location /leafrefs:c/l/lr2, data location /leafrefs:c/l[id='x'][value='x']/lr2.");
+
+ TEST_ERROR_XML2("",
+ "defs", "", "lref", "%n", LY_EVALID);
+ CHECK_LOG_CTX("Invalid leafref value \"%n\" - no existing target instance \"/leaflisttarget\".",
+ "Schema location /defs:lref, data location /defs:lref.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -60,6 +173,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};
diff --git a/tests/utests/types/uint16.c b/tests/utests/types/uint16.c
new file mode 100644
index 0000000..3cb12fb
--- /dev/null
+++ b/tests/utests/types/uint16.c
@@ -0,0 +1,75 @@
+/**
+ * @file uint16.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for uint16 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type uint16 {range 150..200;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "\n 1500 \t\n ");
+ CHECK_LOG_CTX("Unsatisfied range - value \"1500\" is out of the allowed range.",
+ "Schema location /defs:port, line number 3.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/uint32.c b/tests/utests/types/uint32.c
new file mode 100644
index 0000000..9461471
--- /dev/null
+++ b/tests/utests/types/uint32.c
@@ -0,0 +1,75 @@
+/**
+ * @file uint32.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for uint32 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type uint32;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "-10");
+ CHECK_LOG_CTX("Value \"-10\" is out of uint32's min/max bounds.",
+ "Schema location /defs:port, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/uint64.c b/tests/utests/types/uint64.c
new file mode 100644
index 0000000..f160f66
--- /dev/null
+++ b/tests/utests/types/uint64.c
@@ -0,0 +1,83 @@
+/**
+ * @file uint64.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for uint64 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type uint64;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_ERROR_XML("defs", "");
+ CHECK_LOG_CTX("Invalid empty uint64 value.",
+ "Schema location /defs:port, line number 1.");
+
+ TEST_ERROR_XML("defs", " ");
+ CHECK_LOG_CTX("Invalid empty uint64 value.",
+ "Schema location /defs:port, line number 1.");
+
+ TEST_ERROR_XML("defs", "10 xxx");
+ CHECK_LOG_CTX("Invalid uint64 value \"10 xxx\".",
+ "Schema location /defs:port, line number 1.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/uint8.c b/tests/utests/types/uint8.c
new file mode 100644
index 0000000..4d8b6a0
--- /dev/null
+++ b/tests/utests/types/uint8.c
@@ -0,0 +1,77 @@
+/**
+ * @file uint8.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief test for uint8 values
+ *
+ * Copyright (c) 2021 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
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* GLOBAL INCLUDE HEADERS */
+#include <ctype.h>
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+#include "path.h"
+#include "plugins_internal.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+#define TEST_SUCCESS_XML(MOD_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYSC_NODE(tree->schema, NULL, 0, 0x5, 1, "port", 0, LYS_LEAF, 0, 0, 0, 0); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 0, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML(MOD_NAME, DATA) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = "<port xmlns=\"urn:tests:" MOD_NAME "\">" DATA "</port>"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+ assert_null(tree); \
+ }
+
+static void
+test_data_xml(void **state)
+{
+ const char *schema;
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "leaf port {type uint8 {range 150..200;}}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML("defs", "\n 150 \t\n ", UINT8, "150", 150);
+
+ TEST_ERROR_XML("defs", "\n 15 \t\n ");
+ CHECK_LOG_CTX("Unsatisfied range - value \"15\" is out of the allowed range.",
+ "Schema location /defs:port, line number 3.");
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/types/union.c b/tests/utests/types/union.c
index ac6043b..c8361d1 100644
--- a/tests/utests/types/union.c
+++ b/tests/utests/types/union.c
@@ -18,6 +18,7 @@
/* LOCAL INCLUDE HEADERS */
#include "libyang.h"
+#include "path.h"
#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
"module " MOD_NAME " {\n" \
@@ -27,6 +28,23 @@
NODES \
"}\n"
+#define TEST_SUCCESS_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, TYPE, ...) \
+ { \
+ struct lyd_node *tree; \
+ const char *data = XML1 "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+ CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 0, 0, 1, 0, 1, TYPE, ## __VA_ARGS__); \
+ lyd_free_all(tree); \
+ }
+
+#define TEST_ERROR_XML2(XML1, MOD_NAME, NAMESPACES, NODE_NAME, DATA, RET) \
+ {\
+ struct lyd_node *tree; \
+ const char *data = XML1 "<" NODE_NAME " xmlns=\"urn:tests:" MOD_NAME "\" " NAMESPACES ">" DATA "</" NODE_NAME ">"; \
+ CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, RET, tree); \
+ assert_null(tree); \
+ }
+
#define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
{ \
struct lyd_node *tree_1; \
@@ -44,6 +62,51 @@
}
static void
+test_data_xml(void **state)
+{
+ const char *schema;
+ const enum ly_path_pred_type val1[] = {LY_PATH_PREDTYPE_LEAFLIST};
+
+ /* xml test */
+ schema = MODULE_CREATE_YANG("defs", "identity ident1; identity ident2 {base ident1;}"
+ "leaf un1 {type union {"
+ " type leafref {path /int8; require-instance true;}"
+ " type union { type identityref {base ident1;} type instance-identifier {require-instance true;} }"
+ " type string {length 1..20;}}}"
+ "leaf int8 {type int8 {range 10..20;}}"
+ "leaf-list llist {type string;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+ TEST_SUCCESS_XML2("<int8 xmlns=\"urn:tests:defs\">12</int8>",
+ "defs", "", "un1", "12", UNION, "12", INT8, "12", 12);
+
+ TEST_SUCCESS_XML2("<int8 xmlns=\"urn:tests:defs\">12</int8>",
+ "defs", "", "un1", "2", UNION, "2", STRING, "2");
+
+ TEST_SUCCESS_XML2("<int8 xmlns=\"urn:tests:defs\">10</int8>",
+ "defs", "xmlns:x=\"urn:tests:defs\"", "un1", "x:ident2", UNION, "defs:ident2", IDENT, "defs:ident2", "ident2");
+
+ TEST_SUCCESS_XML2("<int8 xmlns=\"urn:tests:defs\">10</int8>",
+ "defs", "xmlns:x=\"urn:tests:defs\"", "un1", "x:ident55", UNION, "x:ident55", STRING, "x:ident55");
+
+ TEST_SUCCESS_XML2("<llist xmlns=\"urn:tests:defs\">x</llist>"
+ "<llist xmlns=\"urn:tests:defs\">y</llist>",
+ "defs", "xmlns:x=\"urn:tests:defs\"", "un1", "/x:llist[.='y']", UNION, "/defs:llist[.='y']",
+ INST, "/defs:llist[.='y']", val1);
+
+ TEST_SUCCESS_XML2("<llist xmlns=\"urn:tests:defs\">x</llist>"
+ "<llist xmlns=\"urn:tests:defs\">y</llist>",
+ "defs", "xmlns:x=\"urn:tests:defs\"", "un1", "/x:llist[3]", UNION, "/x:llist[3]",
+ STRING, "/x:llist[3]");
+
+ /* invalid value */
+ TEST_ERROR_XML2("",
+ "defs", "", "un1", "123456789012345678901", LY_EVALID);
+ CHECK_LOG_CTX("Invalid union value \"123456789012345678901\" - no matching subtype found.",
+ "Schema location /defs:un1, line number 1.");
+}
+
+static void
test_plugin_lyb(void **state)
{
const char *schema;
@@ -63,6 +126,7 @@
main(void)
{
const struct CMUnitTest tests[] = {
+ UTEST(test_data_xml),
UTEST(test_plugin_lyb),
};