Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_yanglib.c |
| 3 | * @author: Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief unit tests for ietf-yang-library data |
| 5 | * |
| 6 | * Copyright (c) 2020 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 <string.h> |
| 18 | |
| 19 | #include "context.h" |
| 20 | #include "in.h" |
| 21 | #include "log.h" |
| 22 | #include "set.h" |
| 23 | #include "tests/config.h" |
| 24 | #include "tree_data.h" |
| 25 | #include "tree_schema.h" |
| 26 | |
| 27 | const char *schema_a = |
| 28 | "module a {\n" |
| 29 | " namespace urn:tests:a;\n" |
| 30 | " prefix a;\n" |
| 31 | " yang-version 1.1;\n" |
| 32 | "\n" |
| 33 | " include a_sub;\n" |
| 34 | "\n" |
| 35 | " list l2 {\n" |
| 36 | " key \"a\";\n" |
| 37 | " leaf a {\n" |
| 38 | " type uint16;\n" |
| 39 | " }\n" |
| 40 | " leaf b {\n" |
| 41 | " type uint16;\n" |
| 42 | " }\n" |
| 43 | " }\n" |
| 44 | "}"; |
| 45 | const char *schema_b = |
| 46 | "module b {\n" |
| 47 | " namespace urn:tests:b;\n" |
| 48 | " prefix b;\n" |
| 49 | " yang-version 1.1;\n" |
| 50 | "\n" |
| 51 | " import a {\n" |
| 52 | " prefix a;\n" |
| 53 | " }\n" |
| 54 | "\n" |
| 55 | " deviation /a:l2 {\n" |
| 56 | " deviate add {\n" |
| 57 | " max-elements 40;\n" |
| 58 | " }\n" |
| 59 | " }\n" |
| 60 | "\n" |
| 61 | " leaf foo {\n" |
| 62 | " type string;\n" |
| 63 | " }\n" |
| 64 | "}"; |
| 65 | |
| 66 | static LY_ERR |
| 67 | test_imp_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, void *user_data, |
| 68 | LYS_INFORMAT *format, const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 69 | { |
| 70 | const char *schema_a_sub = |
| 71 | "submodule a_sub {\n" |
| 72 | " belongs-to a {\n" |
| 73 | " prefix a;\n" |
| 74 | " }\n" |
| 75 | " yang-version 1.1;\n" |
| 76 | "\n" |
| 77 | " feature feat1;\n" |
| 78 | "\n" |
| 79 | " list l3 {\n" |
| 80 | " key \"a\";\n" |
| 81 | " leaf a {\n" |
| 82 | " type uint16;\n" |
| 83 | " }\n" |
| 84 | " leaf b {\n" |
| 85 | " type uint16;\n" |
| 86 | " }\n" |
| 87 | " }\n" |
| 88 | "}\n"; |
| 89 | |
| 90 | assert_string_equal(mod_name, "a"); |
| 91 | assert_null(mod_rev); |
| 92 | if (!submod_name) { |
| 93 | return LY_ENOTFOUND; |
| 94 | } |
| 95 | assert_string_equal(submod_name, "a_sub"); |
| 96 | assert_null(sub_rev); |
| 97 | assert_null(user_data); |
| 98 | |
| 99 | *format = LYS_IN_YANG; |
| 100 | *module_data = schema_a_sub; |
| 101 | *free_module_data = NULL; |
| 102 | return LY_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | test_yanglib(void **state) |
| 107 | { |
| 108 | const char *feats[] = {"feat1", NULL}; |
| 109 | struct lyd_node *tree; |
| 110 | struct ly_set *set; |
| 111 | LY_ERR ret; |
| 112 | |
| 113 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, NULL); |
| 114 | UTEST_ADD_MODULE(schema_a, LYS_IN_YANG, feats, NULL); |
| 115 | UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL); |
| 116 | |
| 117 | assert_int_equal(LY_SUCCESS, ly_ctx_get_yanglib_data(UTEST_LYCTX, &tree)); |
| 118 | |
| 119 | /* make sure there is "a" with a submodule and deviation */ |
| 120 | ret = lyd_find_xpath(tree, "/ietf-yang-library:yang-library/module-set/module[name='a'][submodule/name='a_sub']" |
| 121 | "[feature='feat1'][deviation='b']", &set); |
| 122 | assert_int_equal(ret, LY_SUCCESS); |
| 123 | |
| 124 | assert_int_equal(set->count, 1); |
| 125 | ly_set_free(set, NULL); |
| 126 | |
| 127 | lyd_free_all(tree); |
| 128 | } |
| 129 | |
| 130 | int |
| 131 | main(void) |
| 132 | { |
| 133 | const struct CMUnitTest tests[] = { |
| 134 | UTEST(test_yanglib), |
| 135 | }; |
| 136 | |
| 137 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 138 | } |