Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 1 | /* |
Radek Krejci | d0676f1 | 2019-02-25 13:34:44 +0100 | [diff] [blame] | 2 | * @file test_tree_schema.c |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | d0676f1 | 2019-02-25 13:34:44 +0100 | [diff] [blame] | 4 | * @brief unit tests for functions from tress_schema.c |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 5 | * |
Radek Krejci | d0676f1 | 2019-02-25 13:34:44 +0100 | [diff] [blame] | 6 | * Copyright (c) 2018-2019 CESNET, z.s.p.o. |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 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 | |
| 15 | #include "../../src/common.c" |
| 16 | #include "../../src/log.c" |
| 17 | #include "../../src/set.c" |
| 18 | #include "../../src/parser_yang.c" |
| 19 | #include "../../src/tree_schema.c" |
| 20 | #include "../../src/tree_schema_compile.c" |
| 21 | #include "../../src/tree_schema_free.c" |
| 22 | #include "../../src/tree_schema_helpers.c" |
| 23 | #include "../../src/hash_table.c" |
| 24 | #include "../../src/xpath.c" |
| 25 | #include "../../src/context.c" |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdio.h> |
| 30 | #include <setjmp.h> |
| 31 | #include <cmocka.h> |
| 32 | |
| 33 | #include "libyang.h" |
| 34 | |
| 35 | #define BUFSIZE 1024 |
| 36 | char logbuf[BUFSIZE] = {0}; |
| 37 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 38 | |
| 39 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 40 | #define ENABLE_LOGGER_CHECKING 1 |
| 41 | |
| 42 | #if ENABLE_LOGGER_CHECKING |
| 43 | static void |
| 44 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 45 | { |
| 46 | (void) level; /* unused */ |
| 47 | if (store) { |
| 48 | if (path && path[0]) { |
| 49 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 50 | } else { |
| 51 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 52 | } |
| 53 | if (store > 0) { |
| 54 | --store; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | static int |
| 61 | logger_setup(void **state) |
| 62 | { |
| 63 | (void) state; /* unused */ |
| 64 | |
| 65 | ly_set_log_clb(logger, 0); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | logger_teardown(void **state) |
| 72 | { |
| 73 | (void) state; /* unused */ |
| 74 | #if ENABLE_LOGGER_CHECKING |
| 75 | if (*state) { |
| 76 | fprintf(stderr, "%s\n", logbuf); |
| 77 | } |
| 78 | #endif |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | logbuf_clean(void) |
| 84 | { |
| 85 | logbuf[0] = '\0'; |
| 86 | } |
| 87 | |
| 88 | #if ENABLE_LOGGER_CHECKING |
| 89 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 90 | #else |
| 91 | # define logbuf_assert(str) |
| 92 | #endif |
| 93 | |
| 94 | static void |
| 95 | test_getnext(void **state) |
| 96 | { |
| 97 | *state = test_getnext; |
| 98 | |
| 99 | struct ly_ctx *ctx; |
| 100 | struct lys_module *mod; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 101 | const struct lysc_node *node = NULL, *four; |
| 102 | const struct lysc_node_container *cont; |
| 103 | const struct lysc_action *rpc; |
| 104 | |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 105 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 106 | |
| 107 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;" |
| 108 | "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}" |
| 109 | " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}" |
| 110 | " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}" |
| 111 | " notification nine {leaf nine-data {type string;}}}" |
| 112 | "leaf b {type string;} leaf-list c {type string;} list d {config false;}" |
| 113 | "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;" |
| 114 | "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}" |
Radek Krejci | b0e4937 | 2019-02-25 13:43:21 +0100 | [diff] [blame] | 115 | "rpc i;" |
| 116 | "notification j {leaf i-data {type string;}}" |
| 117 | "notification k;}", LYS_IN_YANG)); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 118 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 119 | assert_string_equal("a", node->name); |
| 120 | cont = (const struct lysc_node_container*)node; |
| 121 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 122 | assert_string_equal("b", node->name); |
| 123 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 124 | assert_string_equal("c", node->name); |
| 125 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 126 | assert_string_equal("d", node->name); |
| 127 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 128 | assert_string_equal("e", node->name); |
| 129 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 130 | assert_string_equal("f", node->name); |
| 131 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 132 | assert_string_equal("g", node->name); |
| 133 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 134 | assert_string_equal("h", node->name); |
| 135 | rpc = (const struct lysc_action*)node; |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 136 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 137 | assert_string_equal("i", node->name); |
Radek Krejci | b0e4937 | 2019-02-25 13:43:21 +0100 | [diff] [blame] | 138 | /* TODO Notifications |
| 139 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 140 | assert_string_equal("j", node->name); |
| 141 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 142 | assert_string_equal("k", node->name); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 143 | */ |
| 144 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 145 | /* Inside container */ |
| 146 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 147 | assert_string_equal("one", node->name); |
| 148 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 149 | assert_string_equal("two", node->name); |
| 150 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 151 | assert_string_equal("three", node->name); |
| 152 | assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 153 | assert_string_equal("four", node->name); |
| 154 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 155 | assert_string_equal("five", node->name); |
| 156 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 157 | assert_string_equal("six", node->name); |
| 158 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 159 | assert_string_equal("seven", node->name); |
| 160 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 161 | assert_string_equal("eight", node->name); |
| 162 | /* TODO Notifications |
| 163 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 164 | assert_string_equal("nine", node->name); |
| 165 | */ |
| 166 | assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 167 | /* Inside RPC */ |
| 168 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 169 | assert_string_equal("h-input", node->name); |
| 170 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 171 | |
| 172 | /* options */ |
| 173 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 174 | assert_string_equal("x", node->name); |
| 175 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 176 | assert_string_equal("seven", node->name); |
| 177 | |
| 178 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE)); |
| 179 | assert_string_equal("seven", node->name); |
| 180 | |
| 181 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 182 | assert_string_equal("five", node->name); |
| 183 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 184 | assert_string_equal("y", node->name); |
| 185 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 186 | assert_string_equal("seven", node->name); |
| 187 | |
| 188 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 189 | assert_string_equal("one", node->name); |
| 190 | |
| 191 | assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 192 | assert_string_equal("h-output", node->name); |
| 193 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 194 | |
Radek Krejci | b0e4937 | 2019-02-25 13:43:21 +0100 | [diff] [blame] | 195 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;" |
| 196 | "leaf a {type string; if-feature f;}" |
| 197 | "leaf b {type string;}}", LYS_IN_YANG)); |
| 198 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 199 | assert_string_equal("b", node->name); |
| 200 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 201 | assert_string_equal("a", node->name); |
| 202 | |
| 203 | |
Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 204 | *state = NULL; |
| 205 | ly_ctx_destroy(ctx, NULL); |
| 206 | } |
| 207 | |
| 208 | int main(void) |
| 209 | { |
| 210 | const struct CMUnitTest tests[] = { |
| 211 | cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown), |
| 212 | }; |
| 213 | |
| 214 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 215 | } |