libyang UPDATE optional value validation and context type plugins (#2171)

This patch separates the build-in type validations from store
operations to allow storing a node even if the value doesn't pass the
validations. To do multiple changes were done:
 - introduces LY_CTX_BASIC_PLUGINS_ONLY flag, which prevents loading of
   any advanced plugins
 - introduces LYPLG_TYPE_STORE_ONLY flag, which skip validation and
   perform just storing
 - introduces LYD_PARSE_STORE_ONLY flag, which implies usage of LYPLG_TYPE_STORE_ONLY
 - introduces options for lyd_new_* API
 - removes lyd_new_(term|list)_(bin|canon) API
 - added sanity checks within lyd_new_(term|list) APIs for proper
   combinations of options
 - refactored lyd_new_* APIs to use common flags to use common options attributes
diff --git a/tests/utests/basic/test_context.c b/tests/utests/basic/test_context.c
index 789c80a..2126ffe 100644
--- a/tests/utests/basic/test_context.c
+++ b/tests/utests/basic/test_context.c
@@ -222,11 +222,21 @@
     assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
     assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
 
+    /* LY_CTX_LEAFREF_EXTENDED */
+    assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_EXTENDED);
+    assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_LEAFREF_EXTENDED));
+    assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_EXTENDED);
+
     /* LY_CTX_LEAFREF_LINKING */
     assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
     assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_LEAFREF_LINKING));
     assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
 
+    /* LY_CTX_BUILTIN_PLUGINS_ONLY */
+    assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_BUILTIN_PLUGINS_ONLY);
+    assert_int_equal(LY_SUCCESS, ly_ctx_unset_options(UTEST_LYCTX, LY_CTX_BUILTIN_PLUGINS_ONLY));
+    assert_int_equal(0, UTEST_LYCTX->flags & LY_CTX_BUILTIN_PLUGINS_ONLY);
+
     assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
 
     /* set back */
@@ -250,10 +260,18 @@
     assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_PREFER_SEARCHDIRS));
     assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_PREFER_SEARCHDIRS);
 
+    /* LY_CTX_LEAFREF_EXTENDED */
+    assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_LEAFREF_EXTENDED));
+    assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_EXTENDED);
+
     /* LY_CTX_LEAFREF_LINKING */
     assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_LEAFREF_LINKING));
     assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_LEAFREF_LINKING);
 
+    /* LY_CTX_BUILTIN_PLUGINS_ONLY */
+    assert_int_equal(LY_SUCCESS, ly_ctx_set_options(UTEST_LYCTX, LY_CTX_BUILTIN_PLUGINS_ONLY));
+    assert_int_not_equal(0, UTEST_LYCTX->flags & LY_CTX_BUILTIN_PLUGINS_ONLY);
+
     assert_int_equal(UTEST_LYCTX->flags, ly_ctx_get_options(UTEST_LYCTX));
 }
 
diff --git a/tests/utests/data/test_new.c b/tests/utests/data/test_new.c
index d75f667..df1d8c5 100644
--- a/tests/utests/data/test_new.c
+++ b/tests/utests/data/test_new.c
@@ -136,8 +136,27 @@
 
     uint32_t val_lens[] = {1, 1};
 
+    assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_BIN_VALUE, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(options & 0x04) (lyd_new_list3()).", NULL, 0);
     assert_int_equal(lyd_new_list3_bin(NULL, mod, "l1", (const void **)key_vals, val_lens, 0, &node), LY_SUCCESS);
     lyd_free_tree(node);
+    assert_int_equal(lyd_new_list3_bin(NULL, mod, "l1", (const void **)key_vals, val_lens, LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_list3()).", NULL, 0);
+
+    assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON_VALUE, &node), LY_SUCCESS);
+    lyd_free_tree(node);
+    assert_int_equal(lyd_new_list3(NULL, mod, "l1", key_vals, val_lens, LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_list3()).", NULL, 0);
+
+    assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN_VALUE, &node, "val_a", 5, "val_b", 5), LY_SUCCESS);
+    lyd_free_tree(node);
+    assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_BIN_VALUE | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", 5, "val_b", 5), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0);
+
+    assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON_VALUE, &node, "val_a", "val_b"), LY_SUCCESS);
+    lyd_free_tree(node);
+    assert_int_equal(lyd_new_list(NULL, mod, "l1", LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node, "val_a", "val_b"), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (lyd_new_list()).", NULL, 0);
 
     /* leaf */
     assert_int_equal(lyd_new_term(NULL, mod, "foo", "[a='a'][b='b'][c='c']", 0, &node), LY_EVALID);
@@ -149,6 +168,18 @@
     assert_int_equal(lyd_new_term(NULL, mod, "foo", "256", 0, &node), LY_SUCCESS);
     lyd_free_tree(node);
 
+    assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_BIN_VALUE, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(options & 0x04) (lyd_new_term()).", NULL, 0);
+    assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_BIN_VALUE, &node), LY_SUCCESS);
+    lyd_free_tree(node);
+    assert_int_equal(lyd_new_term_bin(NULL, mod, "foo", "25", 2, LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0);
+
+    assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON_VALUE, &node), LY_SUCCESS);
+    lyd_free_tree(node);
+    assert_int_equal(lyd_new_term(NULL, mod, "foo", "25", LYD_NEW_VAL_CANON_VALUE | LYD_NEW_VAL_STORE_ONLY, &node), LY_EINVAL);
+    CHECK_LOG_CTX("Invalid argument !(store_only && (format == LY_VALUE_CANON || format == LY_VALUE_LYB)) (_lyd_new_term()).", NULL, 0);
+
     /* leaf-list */
     assert_int_equal(lyd_new_term(NULL, mod, "ll", "ahoy", 0, &node), LY_SUCCESS);
     lyd_free_tree(node);
@@ -164,9 +195,9 @@
     CHECK_LOG_CTX("Inner node (container, notif, RPC, or action) \"l2\" not found.", NULL, 0);
 
     /* anydata */
-    assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
+    assert_int_equal(lyd_new_any(NULL, mod, "any", "{\"node\":\"val\"}", 0, LYD_ANYDATA_STRING, &node), LY_SUCCESS);
     lyd_free_tree(node);
-    assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, 0, &node), LY_SUCCESS);
+    assert_int_equal(lyd_new_any(NULL, mod, "any", "<node>val</node>", 0, LYD_ANYDATA_STRING, &node), LY_SUCCESS);
     lyd_free_tree(node);
 
     /* key-less list */
@@ -186,7 +217,7 @@
     assert_int_equal(lyd_new_inner(NULL, mod, "oper", 0, &rpc), LY_SUCCESS);
     assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 0, &node), LY_SUCCESS);
     assert_int_equal(LY_TYPE_STRING, ((struct lysc_node_leaf *)node->schema)->type->basetype);
-    assert_int_equal(lyd_new_term(rpc, mod, "param", "22", 1, &node), LY_SUCCESS);
+    assert_int_equal(lyd_new_term(rpc, mod, "param", "22", LYD_NEW_VAL_OUTPUT, &node), LY_SUCCESS);
     assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf *)node->schema)->type->basetype);
     lyd_free_tree(rpc);
 }
diff --git a/tests/utests/types/binary.c b/tests/utests/types/binary.c
index 35da7d5..910f756 100644
--- a/tests/utests/types/binary.c
+++ b/tests/utests/types/binary.c
@@ -232,6 +232,15 @@
     assert_int_equal(LY_EVALID, ly_ret);
     assert_string_equal(err->msg, "Unsatisfied length - string \"MTIz\" length is not allowed.");
     ly_err_free(err);
+
+    /* LYPLG_TYPE_STORE_ONLY test */
+    val = "MTIz";
+    err = NULL;
+    ly_ret = type->store(UTEST_LYCTX, lysc_type2, val, strlen(val),
+            LYPLG_TYPE_STORE_ONLY, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err);
+    assert_int_equal(LY_SUCCESS, ly_ret);
+    type->free(UTEST_LYCTX, &value);
+    ly_err_free(err);
 }
 
 static void
diff --git a/tests/utests/types/decimal64.c b/tests/utests/types/decimal64.c
index ebfb2b0..fdd118a 100644
--- a/tests/utests/types/decimal64.c
+++ b/tests/utests/types/decimal64.c
@@ -60,6 +60,14 @@
         lyd_free_all(tree_2); \
     }
 
+#define TEST_SUCCESS_PARSE_STORE_ONLY_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, LYD_PARSE_STORE_ONLY, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+        lyd_free_all(tree); \
+    }
+
 static void
 test_data_xml(void **state)
 {
@@ -94,6 +102,9 @@
 
     TEST_ERROR_XML("defs", "l1", "8.55  xxx");
     CHECK_LOG_CTX("Value \"8.55\" of decimal64 type exceeds defined number (1) of fraction digits.", "/defs:l1", 1);
+
+    /* LYPLG_TYPE_STORE_ONLY test */
+    TEST_SUCCESS_PARSE_STORE_ONLY_XML("defs", "l1", "\n 15 \t\n  ");
 }
 
 static void
diff --git a/tests/utests/types/inet_types.c b/tests/utests/types/inet_types.c
index 3874cc3..016d5bb 100644
--- a/tests/utests/types/inet_types.c
+++ b/tests/utests/types/inet_types.c
@@ -50,6 +50,31 @@
         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_PARSE_STORE_ONLY_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, LYD_PARSE_ONLY | LYD_PARSE_STORE_ONLY, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+        CHECK_LYD_NODE_TERM((struct lyd_node_term *)tree, 4, 0, 0, 0, 1, TYPE, __VA_ARGS__); \
+        lyd_free_all(tree); \
+    }
+
+#define TEST_ERROR_PARSE_STORE_ONLY_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, LYD_PARSE_ONLY | LYD_PARSE_STORE_ONLY, LYD_VALIDATE_PRESENT, LY_EVALID, tree); \
+        assert_null(tree); \
+    }
+
 #define TEST_SUCCESS_LYB(MOD_NAME, NODE_NAME, DATA) \
     { \
         struct lyd_node *tree_1; \
@@ -114,6 +139,33 @@
 }
 
 static void
+test_data_basic_plugins_only_xml(void **state)
+{
+    const char *schema;
+
+    schema = MODULE_CREATE_YANG("a", "leaf l {type inet:ipv4-address;}");
+    UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+    /* Stored via ipv4-address plugin */
+    TEST_SUCCESS_XML("a", "l", "192.168.0.1", STRING, "192.168.0.1");
+    TEST_ERROR_XML("a", "l", "192.168.0.333");
+    TEST_ERROR_PARSE_STORE_ONLY_XML("a", "l", "192.168.0.333");
+
+    /* Recreate context to get rid of all plugins */
+    ly_ctx_destroy(UTEST_LYCTX);
+    assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_BUILTIN_PLUGINS_ONLY, &UTEST_LYCTX));
+    UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
+
+    /* Stored via string plugin */
+    TEST_SUCCESS_XML("a", "l", "192.168.0.1", STRING, "192.168.0.1");
+    TEST_ERROR_XML("a", "l", "192.168.0.333");
+    CHECK_LOG_CTX("Unsatisfied pattern - \"192.168.0.333\" does not conform to \""
+            "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9]"
+            "[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?\".", "/a:l", 1);
+    TEST_SUCCESS_PARSE_STORE_ONLY_XML("a", "l", "192.168.0.333", STRING, "192.168.0.333");
+}
+
+static void
 test_data_lyb(void **state)
 {
     const char *schema;
@@ -278,6 +330,7 @@
         UTEST(test_data_xml),
         UTEST(test_data_lyb),
         UTEST(test_plugin_sort),
+        UTEST(test_data_basic_plugins_only_xml),
     };
 
     return cmocka_run_group_tests(tests, NULL, NULL);
diff --git a/tests/utests/types/int8.c b/tests/utests/types/int8.c
index d369a0e..d92d146 100644
--- a/tests/utests/types/int8.c
+++ b/tests/utests/types/int8.c
@@ -1527,6 +1527,16 @@
     assert_int_equal(LY_EVALID, ly_ret);
     ly_err_free(err);
 
+    /* LYPLG_TYPE_STORE_ONLY test */
+    val_text = "-60";
+    err = NULL;
+    ly_ret = type->store(UTEST_LYCTX, lysc_type, val_text, strlen(val_text),
+            LYPLG_TYPE_STORE_ONLY, LY_VALUE_XML, NULL, LYD_VALHINT_DECNUM, NULL,
+            &value, NULL, &err);
+    assert_int_equal(LY_SUCCESS, ly_ret);
+    type->free(UTEST_LYCTX, &value);
+    ly_err_free(err);
+
     UTEST_LOG_CTX_CLEAN;
 }
 
diff --git a/tests/utests/types/string.c b/tests/utests/types/string.c
index 73d9677..f5aec34 100644
--- a/tests/utests/types/string.c
+++ b/tests/utests/types/string.c
@@ -1184,6 +1184,15 @@
     assert_int_equal(LY_EVALID, ly_ret);
     ly_err_free(err);
 
+    /* LYPLG_TYPE_STORE_ONLY test */
+    val_text = "10 \"| bcdei";
+    err = NULL;
+    ly_ret = type->store(UTEST_LYCTX, lysc_type, val_text, strlen(val_text),
+            LYPLG_TYPE_STORE_ONLY, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL,
+            &value, NULL, &err);
+    assert_int_equal(LY_SUCCESS, ly_ret);
+    type->free(UTEST_LYCTX, &value);
+    ly_err_free(err);
 }
 
 static void
diff --git a/tests/utests/types/uint8.c b/tests/utests/types/uint8.c
index 89b89a7..618e422 100644
--- a/tests/utests/types/uint8.c
+++ b/tests/utests/types/uint8.c
@@ -50,6 +50,14 @@
         assert_null(tree); \
     }
 
+#define TEST_SUCCESS_PARSE_STORE_ONLY_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, LYD_PARSE_STORE_ONLY, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); \
+        lyd_free_all(tree); \
+    }
+
 static void
 test_data_xml(void **state)
 {
@@ -63,6 +71,9 @@
 
     TEST_ERROR_XML("defs", "\n 15 \t\n  ");
     CHECK_LOG_CTX("Unsatisfied range - value \"15\" is out of the allowed range.", "/defs:port", 3);
+
+    /* LYPLG_TYPE_STORE_ONLY test */
+    TEST_SUCCESS_PARSE_STORE_ONLY_XML("defs", "\n 15 \t\n  ");
 }
 
 int