Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/plugins_types/instanceid.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ lyplg_type_validate_tree_instanceid(const struct ly_ctx *ctx, const struct lysc_
const struct lyd_node *ctx_node, const struct lyd_node *UNUSED(tree), struct lyd_value *storage,
struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)storage->realtype;
const char *value;
char *path;
Expand All @@ -253,10 +252,10 @@ lyplg_type_validate_tree_instanceid(const struct ly_ctx *ctx, const struct lysc_
}

/* find the target in data */
if ((ret = ly_path_eval(storage->target, ctx_node, NULL, NULL))) {
if (ly_path_eval(storage->target, ctx_node, NULL, NULL)) {
value = lyd_value_get_canonical(ctx, storage);
path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
return ly_err_new(err, ret, LYVE_DATA, path, strdup("instance-required"), LY_ERRMSG_NOINST, value);
return ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), LY_ERRMSG_NOINST, value);
}

return LY_SUCCESS;
Expand Down
47 changes: 47 additions & 0 deletions tests/utests/data/test_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,52 @@ test_multi_error(void **state)
CHECK_LOG_CTX_APPTAG("Duplicate instance of \"l\".", "/ii:cont/l", 0, NULL);
}

static void
test_require_instance(void **state)
{
struct lyd_node *tree;
const char *schema =
"module ri {\n"
" namespace urn:tests:ri;\n"
" prefix ri;\n"
" yang-version 1.1;\n"
"\n"
" leaf t {\n"
" type string;\n"
" }\n"
" leaf lr {\n"
" type leafref {\n"
" path \"/ri:t\";\n"
" }\n"
" }\n"
" leaf ii {\n"
" type instance-identifier;\n"
" }\n"
"}";
const char *data;

UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);

CHECK_PARSE_LYD_PARAM("<lr xmlns=\"urn:tests:ri\">x</lr>", LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_EVALID, tree);
CHECK_LOG_CTX_APPTAG("Invalid leafref value \"x\" - no target instance \"/ri:t\" with the same value.", "/ri:lr", 0,
"instance-required");

CHECK_PARSE_LYD_PARAM("<ii xmlns=\"urn:tests:ri\" xmlns:ri=\"urn:tests:ri\">/ri:t</ii>", LYD_XML, 0,
LYD_VALIDATE_PRESENT, LY_EVALID, tree);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/ri:t\" value - required instance not found.", "/ri:ii", 0,
"instance-required");

/* both errors */
data =
"<lr xmlns=\"urn:tests:ri\">x</lr>\n"
"<ii xmlns=\"urn:tests:ri\" xmlns:ri=\"urn:tests:ri\">/ri:t</ii>\n";
CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT | LYD_VALIDATE_MULTI_ERROR, LY_EVALID, tree);
CHECK_LOG_CTX_APPTAG("Invalid leafref value \"x\" - no target instance \"/ri:t\" with the same value.", "/ri:lr", 0,
"instance-required");
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/ri:t\" value - required instance not found.", "/ri:ii", 0,
"instance-required");
}

const char *schema_j =
"module j {\n"
" namespace urn:tests:j;\n"
Expand Down Expand Up @@ -1950,6 +1996,7 @@ main(void)
UTEST(test_state),
UTEST(test_must),
UTEST(test_multi_error),
UTEST(test_require_instance),
UTEST(test_when_must_cross_ref),
UTEST(test_action),
UTEST(test_rpc),
Expand Down
2 changes: 1 addition & 1 deletion tests/utests/extensions/test_structure.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ test_parse(void **state)
lyd_free_tree(node->next);
assert_string_equal(LYD_NAME(node), "x");
lyd_free_tree(node);
assert_int_equal(LY_ENOTFOUND, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL));
assert_int_equal(LY_EVALID, lyd_validate_all(&tree, NULL, LYD_VALIDATE_PRESENT, NULL));
CHECK_LOG_CTX("Invalid instance-identifier \"/a:x/y\" value - required instance not found.",
"/a:struct/x/struct/x/z", 0);
lyd_free_all(tree);
Expand Down
10 changes: 5 additions & 5 deletions tests/utests/types/instanceid.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,33 @@ test_data_xml(void **state)
/* 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);
"defs", "xmlns:m=\"urn:tests:mod\"", "l1", "/m:cont/m:l2", LY_EVALID);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/mod:cont/l2\" value - required instance not found.",
"/defs:l1", 0, "instance-required");

TEST_ERROR_XML2("<llist xmlns=\"urn:tests:defs\">1</llist>",
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:llist[.='2']", LY_ENOTFOUND);
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:llist[.='2']", LY_EVALID);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/defs:llist[.='2']\" value - required instance not found.",
"/defs:l1", 0, "instance-required");

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);
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:list2[a:id='a'][a:id2='a']/a:id", LY_EVALID);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/defs:list2[id='a'][id2='a']/id\" value - required instance not found.",
"/defs:l1", 0, "instance-required");

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);
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:llist[.='3']", LY_EVALID);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/defs:llist[.='3']\" value - required instance not found.",
"/defs:l1", 0, "instance-required");

TEST_ERROR_XML2("",
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:list-keyless[3]", LY_ENOTFOUND);
"defs", "xmlns:a=\"urn:tests:defs\"", "l1", "/a:list-keyless[3]", LY_EVALID);
CHECK_LOG_CTX_APPTAG("Invalid instance-identifier \"/defs:list-keyless[3]\" value - required instance not found.",
"/defs:l1", 0, "instance-required");

Expand Down
Loading