blob: 015d4b01673e2fb3d5fea83b51912ac1187594cd [file] [log] [blame]
Radek Krejci5902be92021-03-25 21:25:14 +01001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from set.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17#include <stdlib.h>
18#include <string.h>
19
20#include "config.h"
21#include "plugins.h"
22#include "plugins_internal.h"
23
24const char *simple = "module libyang-plugins-simple {"
25 " namespace urn:libyang:tests:plugins:simple;"
26 " prefix s;"
27 " typedef note { type string; }"
28 " extension hint { argument value; }"
29 " leaf test {"
30 " type s:note {length 255;}"
31 " s:hint \"some hint here\";"
32 " }"
33 "}";
34
35static void
36test_add_invalid(void **state)
37{
38 assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX));
39
40#ifdef __APPLE__
41 CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
42 "(dlopen("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ", 2): image not found).", NULL);
43#else
44 CHECK_LOG("Loading \""TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX "\" as a plugin failed "
45 "("TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX ": cannot open shared object file: "
46 "No such file or directory).", NULL);
47#endif
48
49 assert_int_equal(LY_EINVAL, lyplg_add(TESTS_BIN "/plugins/plugin_invalid" LYPLG_SUFFIX));
50#ifndef __APPLE__
51 /* OS X prints address of the symbol being searched and cmocka doesn't support wildcards in string checking assert */
52 CHECK_LOG("Processing user type plugin \""TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX "\" failed, "
53 "missing type plugins information ("TESTS_BIN "/plugins/plugin_invalid"LYPLG_SUFFIX ": "
54 "undefined symbol: plugins_types__).", NULL);
55#endif
56}
57
58static void
59test_add_simple(void **state)
60{
61 const struct lys_module *mod;
62 struct lysc_node_leaf *leaf;
63 struct lyplg_ext *plugin_e;
64 struct lyplg_type *plugin_t;
65
66 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
67
68 UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);
69
70 leaf = (struct lysc_node_leaf *)mod->compiled->data;
71 assert_int_equal(LYS_LEAF, leaf->nodetype);
72
73 assert_non_null(plugin_t = lyplg_find(LYPLG_TYPE, "libyang-plugins-simple", NULL, "note"));
74 assert_string_equal("libyang 2 - simple test, version 1", plugin_t->id);
75 assert_ptr_equal(leaf->type->plugin, plugin_t);
76
77 assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
78 assert_non_null(plugin_e = lyplg_find(LYPLG_EXTENSION, "libyang-plugins-simple", NULL, "hint"));
79 assert_string_equal("libyang 2 - simple test, version 1", plugin_e->id);
80 assert_ptr_equal(leaf->exts[0].def->plugin, plugin_e);
81
82 /* the second loading of the same plugin - still success */
83 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
84}
85
86int
87main(void)
88{
89 const struct CMUnitTest tests[] = {
90 UTEST(test_add_invalid),
91 UTEST(test_add_simple),
92 };
93
94 return cmocka_run_group_tests(tests, NULL, NULL);
95}