blob: 7f3fe4079e7040056aba7e64953e5a9e40622cc7 [file] [log] [blame]
Radek Krejci5902be92021-03-25 21:25:14 +01001/*
aPiecek023f83a2021-05-11 07:37:03 +02002 * @file test_plugins.c
Radek Krejci5902be92021-03-25 21:25:14 +01003 * @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{
Michal Vaskodd349cf2021-09-01 15:51:27 +020038 (void)state;
Radek Krejci5902be92021-03-25 21:25:14 +010039 assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX));
Radek Krejci5902be92021-03-25 21:25:14 +010040}
41
42static void
43test_add_simple(void **state)
44{
Michal Vasko4de7d072021-07-09 09:13:18 +020045 struct lys_module *mod;
Radek Krejci5902be92021-03-25 21:25:14 +010046 struct lysc_node_leaf *leaf;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020047 struct lyplg_ext_record *record_e;
Radek Krejci5902be92021-03-25 21:25:14 +010048 struct lyplg_type *plugin_t;
49
50 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
51
52 UTEST_ADD_MODULE(simple, LYS_IN_YANG, NULL, &mod);
53
54 leaf = (struct lysc_node_leaf *)mod->compiled->data;
55 assert_int_equal(LYS_LEAF, leaf->nodetype);
56
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020057 assert_non_null(plugin_t = lyplg_type_plugin_find("libyang-plugins-simple", NULL, "note"));
Michal Vasko193dacd2022-10-13 08:43:05 +020058 assert_string_equal("ly2 simple test v1", plugin_t->id);
Radek Krejci5902be92021-03-25 21:25:14 +010059 assert_ptr_equal(leaf->type->plugin, plugin_t);
60
61 assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts));
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020062 assert_non_null(record_e = lyplg_ext_record_find("libyang-plugins-simple", NULL, "hint"));
Michal Vasko193dacd2022-10-13 08:43:05 +020063 assert_string_equal("ly2 simple test v1", record_e->plugin.id);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +020064 assert_ptr_equal(leaf->exts[0].def->plugin, &record_e->plugin);
Radek Krejci5902be92021-03-25 21:25:14 +010065
66 /* the second loading of the same plugin - still success */
67 assert_int_equal(LY_SUCCESS, lyplg_add(TESTS_BIN "/plugins/plugin_simple" LYPLG_SUFFIX));
68}
69
Radek Krejci4f2e3e52021-03-30 14:20:28 +020070static void
Radek Krejci4a4d1e02021-04-09 14:04:40 +020071test_not_implemented(void **state)
72{
Michal Vasko4de7d072021-07-09 09:13:18 +020073 struct lys_module *mod;
Radek Krejci4a4d1e02021-04-09 14:04:40 +020074 struct lyd_node *tree;
75 const char *schema = "module libyang-plugins-unknown {"
76 " namespace urn:libyang:tests:plugins:unknown;"
77 " prefix u;"
78 " extension myext;"
79 " typedef mytype { type string;}"
80 " leaf test {"
81 " u:myext;"
82 " type mytype;"
83 " }"
84 "}";
85 const char *data = "<test xmlns=\"urn:libyang:tests:plugins:unknown\">xxx</test>";
86 char *printed = NULL;
87
88 UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
89
90 assert_int_equal(LY_SUCCESS, lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0));
91 free(printed);
92
93 assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
94 CHECK_LOG_CTX(NULL, NULL);
95
96 lyd_free_all(tree);
97}
98
Radek Krejci5902be92021-03-25 21:25:14 +010099int
100main(void)
101{
102 const struct CMUnitTest tests[] = {
103 UTEST(test_add_invalid),
104 UTEST(test_add_simple),
Radek Krejci4a4d1e02021-04-09 14:04:40 +0200105 UTEST(test_not_implemented),
Radek Krejci5902be92021-03-25 21:25:14 +0100106 };
107
108 return cmocka_run_group_tests(tests, NULL, NULL);
109}