Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 1 | /* |
aPiecek | 023f83a | 2021-05-11 07:37:03 +0200 | [diff] [blame] | 2 | * @file test_plugins.c |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 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 | |
| 24 | const 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 | |
| 35 | static void |
| 36 | test_add_invalid(void **state) |
| 37 | { |
Michal Vasko | dd349cf | 2021-09-01 15:51:27 +0200 | [diff] [blame] | 38 | (void)state; |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 39 | assert_int_equal(LY_ESYS, lyplg_add(TESTS_BIN "/plugins/plugin_does_not_exist" LYPLG_SUFFIX)); |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void |
| 43 | test_add_simple(void **state) |
| 44 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 45 | struct lys_module *mod; |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 46 | struct lysc_node_leaf *leaf; |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame^] | 47 | struct lyplg_ext_record *record_e; |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 48 | 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 Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame^] | 57 | assert_non_null(plugin_t = lyplg_type_plugin_find("libyang-plugins-simple", NULL, "note")); |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 58 | assert_string_equal("libyang 2 - simple test, version 1", plugin_t->id); |
| 59 | assert_ptr_equal(leaf->type->plugin, plugin_t); |
| 60 | |
| 61 | assert_int_equal(1, LY_ARRAY_COUNT(leaf->exts)); |
Michal Vasko | c0c64ae | 2022-10-06 10:15:23 +0200 | [diff] [blame^] | 62 | assert_non_null(record_e = lyplg_ext_record_find("libyang-plugins-simple", NULL, "hint")); |
| 63 | assert_string_equal("libyang 2 - simple test, version 1", record_e->plugin.id); |
| 64 | assert_ptr_equal(leaf->exts[0].def->plugin, &record_e->plugin); |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 65 | |
| 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 Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 70 | static void |
Radek Krejci | 4a4d1e0 | 2021-04-09 14:04:40 +0200 | [diff] [blame] | 71 | test_not_implemented(void **state) |
| 72 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 73 | struct lys_module *mod; |
Radek Krejci | 4a4d1e0 | 2021-04-09 14:04:40 +0200 | [diff] [blame] | 74 | 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 Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 99 | int |
| 100 | main(void) |
| 101 | { |
| 102 | const struct CMUnitTest tests[] = { |
| 103 | UTEST(test_add_invalid), |
| 104 | UTEST(test_add_simple), |
Radek Krejci | 4a4d1e0 | 2021-04-09 14:04:40 +0200 | [diff] [blame] | 105 | UTEST(test_not_implemented), |
Radek Krejci | 5902be9 | 2021-03-25 21:25:14 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 109 | } |