tests FEATURE basic external plugins tests
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a4a3d78..10e4862 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -94,6 +94,7 @@
endif()
add_subdirectory(style)
+add_subdirectory(plugins)
add_subdirectory(utests)
add_subdirectory(fuzz)
diff --git a/tests/plugins/CMakeLists.txt b/tests/plugins/CMakeLists.txt
new file mode 100644
index 0000000..3eae019
--- /dev/null
+++ b/tests/plugins/CMakeLists.txt
@@ -0,0 +1,18 @@
+
+include_directories(${CMAKE_CURRENT_SOURCE_DIR})
+
+function(ly_add_plugin)
+ cmake_parse_arguments(ADDPLUGIN "" "NAME" "SOURCES" ${ARGN})
+ set(PLUGIN_NAME plugin_${ADDPLUGIN_NAME})
+
+ add_library(${PLUGIN_NAME} MODULE)
+ set_target_properties(${PLUGIN_NAME} PROPERTIES PREFIX "")
+ foreach(PLUGIN_SOURCE ${ADDPLUGIN_SOURCES})
+ target_sources(${PLUGIN_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${PLUGIN_SOURCE})
+ endforeach()
+
+ target_link_libraries(${PLUGIN_NAME} yang)
+endfunction(ly_add_plugin)
+
+ly_add_plugin(NAME invalid SOURCES invalid.c)
+ly_add_plugin(NAME simple SOURCES simple.c)
\ No newline at end of file
diff --git a/tests/plugins/invalid.c b/tests/plugins/invalid.c
new file mode 100644
index 0000000..54f5566
--- /dev/null
+++ b/tests/plugins/invalid.c
@@ -0,0 +1,41 @@
+/**
+ * @file plugins/invalid.c
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @brief Invalid testing plugins implementation
+ *
+ * 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 <stdint.h>
+
+#include "libyang.h"
+#include "plugins_exts.h"
+#include "plugins_types.h"
+
+/*
+ * EXTENSION PLUGIN
+ */
+
+/**
+ * @brief Instead of plugin description, only the API version is declared.
+ *
+ * Here should be LY_PLUGINS_EXTENSIONS used.
+ */
+uint32_t plugins_extensions_apiver__ = LYPLG_EXT_API_VERSION;
+
+/*
+ * TYPE PLUGIN
+ */
+
+/**
+ * @brief Instead of plugin description, only the API version is declared.
+ *
+ * Here should be LYPLG_TYPES used.
+ */
+uint32_t plugins_types_apiver__ = LYPLG_TYPE_API_VERSION;
diff --git a/tests/plugins/simple.c b/tests/plugins/simple.c
new file mode 100644
index 0000000..34f9e87
--- /dev/null
+++ b/tests/plugins/simple.c
@@ -0,0 +1,89 @@
+/**
+ * @file plugins/simple.c
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @brief Simple testing plugins implementation
+ *
+ * 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 <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libyang.h"
+#include "plugins_exts.h"
+#include "plugins_types.h"
+
+/*
+ * EXTENSION PLUGIN
+ */
+
+/**
+ * @brief Compile NAMC's extension instances.
+ *
+ * Implementation of lyext_clb_compile callback set as lyext_plugin::compile.
+ */
+static LY_ERR
+hint_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+{
+ /* check that the extension is instantiated at an allowed place - data node */
+ if (!LY_STMT_IS_DATA_NODE(c_ext->parent_stmt)) {
+ lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
+ "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
+ p_ext->name, ly_stmt2str(c_ext->parent_stmt));
+ return LY_ENOT;
+ }
+
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Plugin descriptions for the test extensions
+ */
+LYPLG_EXTENSIONS = {
+ {
+ .module = "libyang-plugins-simple",
+ .revision = NULL,
+ .name = "hint",
+
+ .plugin.id = "libyang 2 - simple test, version 1",
+ .plugin.compile = &hint_compile,
+ .plugin.validate = NULL,
+ .plugin.sprinter = NULL,
+ .plugin.free = NULL
+ },
+ {0} /* terminating zeroed item */
+};
+
+/*
+ * TYPE PLUGIN
+ */
+
+/**
+ * @brief Plugin information for note (string) type implementation.
+ *
+ * Everything is just the same as for built-in string.
+ */
+LYPLG_TYPES = {
+ {
+ .module = "libyang-plugins-simple",
+ .revision = NULL,
+ .name = "note",
+
+ .plugin.id = "libyang 2 - simple test, version 1",
+ .plugin.type = LY_TYPE_UNKNOWN,
+ .plugin.store = ly_type_store_string,
+ .plugin.validate = NULL,
+ .plugin.compare = ly_type_compare_simple,
+ .plugin.print = ly_type_print_simple,
+ .plugin.duplicate = ly_type_dup_simple,
+ .plugin.free = ly_type_free_simple
+ },
+ {0}
+};
diff --git a/tests/utests/CMakeLists.txt b/tests/utests/CMakeLists.txt
index cb5b643..e6ea88e 100644
--- a/tests/utests/CMakeLists.txt
+++ b/tests/utests/CMakeLists.txt
@@ -28,6 +28,7 @@
ly_add_utest(NAME hash_table SOURCES basic/test_hash_table.c)
ly_add_utest(NAME inout SOURCES basic/test_inout.c)
ly_add_utest(NAME context SOURCES basic/test_context.c)
+ly_add_utest(NAME plugins SOURCES basic/test_plugins.c)
ly_add_utest(NAME xml SOURCES basic/test_xml.c)
ly_add_utest(NAME json SOURCES basic/test_json.c)
ly_add_utest(NAME xpath SOURCES basic/test_xpath.c)
diff --git a/tests/utests/basic/test_plugins.c b/tests/utests/basic/test_plugins.c
new file mode 100644
index 0000000..015d4b0
--- /dev/null
+++ b/tests/utests/basic/test_plugins.c
@@ -0,0 +1,95 @@
+/*
+ * @file set.c
+ * @author: Radek Krejci <rkrejci@cesnet.cz>
+ * @brief unit tests for functions from set.c
+ *
+ * Copyright (c) 2018 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 <stdlib.h>
+#include <string.h>
+
+#include "config.h"
+#include "plugins.h"
+#include "plugins_internal.h"
+
+const char *simple = "module libyang-plugins-simple {"
+ " namespace urn:libyang:tests:plugins:simple;"
+ " prefix s;"
+ " typedef note { type string; }"
+ " extension hint { argument value; }"
+ " leaf test {"
+ " type s:note {length 255;}"
+ " s:hint \"some hint here\";"
+ " }"
+ "}";
+
+static void
+test_add_invalid(void **state)
+{
+ assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX));
+
+#ifdef __APPLE__
+ CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
+ "(dlopen("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ", 2): image not found).", NULL);
+#else
+ CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
+ "("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ": cannot open shared object file: "
+ "No such file or directory).", NULL);
+#endif
+
+ assert_int_equal(LY_EINVAL, lyplg_add(TESTS_BIN "/plugins/plugin_invalid" LYPLG_SUFFIX));
+#ifndef __APPLE__
+ /* OS X prints address of the symbol being searched and cmocka doesn't support wildcards in string checking assert */
+ CHECK_LOG("Processing user type plugin \""TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX "\" failed, "
+ "missing type plugins information ("TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX ": "
+ "undefined symbol: plugins_types__).", NULL);
+#endif
+}
+
+static void
+test_add_simple(void **state)
+{
+ const struct lys_module *mod;
+ struct lysc_node_leaf *leaf;
+ struct lyplg_ext *plugin_e;
+ struct lyplg_type *plugin_t;
+
+ assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
+
+ UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);
+
+ leaf = (struct lysc_node_leaf *)mod->compiled->data;
+ assert_int_equal(LYS_LEAF, leaf->nodetype);
+
+ assert_non_null(plugin_t = lyplg_find(LYPLG_TYPE, "libyang-plugins-simple", NULL, "note"));
+ assert_string_equal("libyang 2 - simple test, version 1", plugin_t->id);
+ assert_ptr_equal(leaf->type->plugin, plugin_t);
+
+ assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
+ assert_non_null(plugin_e = lyplg_find(LYPLG_EXTENSION, "libyang-plugins-simple", NULL, "hint"));
+ assert_string_equal("libyang 2 - simple test, version 1", plugin_e->id);
+ assert_ptr_equal(leaf->exts[0].def->plugin, plugin_e);
+
+ /* the second loading of the same plugin - still success */
+ assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_add_invalid),
+ UTEST(test_add_simple),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}