Radek Krejci | b9726b2 | 2019-02-22 16:35:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from common.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 | |
| 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;}}}" |
| 115 | "notification i {leaf i-data {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame^] | 116 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 117 | assert_string_equal("a", node->name); |
| 118 | cont = (const struct lysc_node_container*)node; |
| 119 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 120 | assert_string_equal("b", node->name); |
| 121 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 122 | assert_string_equal("c", node->name); |
| 123 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 124 | assert_string_equal("d", node->name); |
| 125 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 126 | assert_string_equal("e", node->name); |
| 127 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 128 | assert_string_equal("f", node->name); |
| 129 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 130 | assert_string_equal("g", node->name); |
| 131 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 132 | assert_string_equal("h", node->name); |
| 133 | rpc = (const struct lysc_action*)node; |
| 134 | /* TODO Notifications |
| 135 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 136 | assert_string_equal("i", node->name); |
| 137 | */ |
| 138 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 139 | /* Inside container */ |
| 140 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 141 | assert_string_equal("one", node->name); |
| 142 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 143 | assert_string_equal("two", node->name); |
| 144 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 145 | assert_string_equal("three", node->name); |
| 146 | assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 147 | assert_string_equal("four", node->name); |
| 148 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 149 | assert_string_equal("five", node->name); |
| 150 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 151 | assert_string_equal("six", node->name); |
| 152 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 153 | assert_string_equal("seven", node->name); |
| 154 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 155 | assert_string_equal("eight", node->name); |
| 156 | /* TODO Notifications |
| 157 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 158 | assert_string_equal("nine", node->name); |
| 159 | */ |
| 160 | assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 161 | /* Inside RPC */ |
| 162 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 163 | assert_string_equal("h-input", node->name); |
| 164 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 165 | |
| 166 | /* options */ |
| 167 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 168 | assert_string_equal("x", node->name); |
| 169 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 170 | assert_string_equal("seven", node->name); |
| 171 | |
| 172 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE)); |
| 173 | assert_string_equal("seven", node->name); |
| 174 | |
| 175 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 176 | assert_string_equal("five", node->name); |
| 177 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 178 | assert_string_equal("y", node->name); |
| 179 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 180 | assert_string_equal("seven", node->name); |
| 181 | |
| 182 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 183 | assert_string_equal("one", node->name); |
| 184 | |
| 185 | assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 186 | assert_string_equal("h-output", node->name); |
| 187 | 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] | 188 | |
| 189 | *state = NULL; |
| 190 | ly_ctx_destroy(ctx, NULL); |
| 191 | } |
| 192 | |
| 193 | int main(void) |
| 194 | { |
| 195 | const struct CMUnitTest tests[] = { |
| 196 | cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown), |
| 197 | }; |
| 198 | |
| 199 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 200 | } |