Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [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 <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <stdio.h> |
| 18 | #include <setjmp.h> |
| 19 | #include <cmocka.h> |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | static void |
| 24 | test_getnext(void **state) |
| 25 | { |
| 26 | *state = test_getnext; |
| 27 | |
| 28 | struct ly_ctx *ctx; |
| 29 | struct lys_module *mod; |
| 30 | const struct lysc_node *node = NULL, *four; |
| 31 | const struct lysc_node_container *cont; |
| 32 | const struct lysc_action *rpc; |
| 33 | |
| 34 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 35 | |
| 36 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;" |
| 37 | "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}" |
| 38 | " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}" |
| 39 | " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}" |
| 40 | " notification nine {leaf nine-data {type string;}}}" |
| 41 | "leaf b {type string;} leaf-list c {type string;} list d {config false;}" |
| 42 | "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;" |
| 43 | "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}" |
| 44 | "rpc i;" |
| 45 | "notification j {leaf i-data {type string;}}" |
| 46 | "notification k;}", LYS_IN_YANG)); |
| 47 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 48 | assert_string_equal("a", node->name); |
| 49 | cont = (const struct lysc_node_container*)node; |
| 50 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 51 | assert_string_equal("b", node->name); |
| 52 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 53 | assert_string_equal("c", node->name); |
| 54 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 55 | assert_string_equal("d", node->name); |
| 56 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 57 | assert_string_equal("e", node->name); |
| 58 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 59 | assert_string_equal("f", node->name); |
| 60 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 61 | assert_string_equal("g", node->name); |
| 62 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 63 | assert_string_equal("h", node->name); |
| 64 | rpc = (const struct lysc_action*)node; |
| 65 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 66 | assert_string_equal("i", node->name); |
| 67 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 68 | assert_string_equal("j", node->name); |
| 69 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 70 | assert_string_equal("k", node->name); |
| 71 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 72 | /* Inside container */ |
| 73 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 74 | assert_string_equal("one", node->name); |
| 75 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 76 | assert_string_equal("two", node->name); |
| 77 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 78 | assert_string_equal("three", node->name); |
| 79 | assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 80 | assert_string_equal("four", node->name); |
| 81 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 82 | assert_string_equal("five", node->name); |
| 83 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 84 | assert_string_equal("six", node->name); |
| 85 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 86 | assert_string_equal("seven", node->name); |
| 87 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 88 | assert_string_equal("eight", node->name); |
| 89 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 90 | assert_string_equal("nine", node->name); |
| 91 | assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 92 | /* Inside RPC */ |
| 93 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 94 | assert_string_equal("h-input", node->name); |
| 95 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 96 | |
| 97 | /* options */ |
| 98 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 99 | assert_string_equal("x", node->name); |
| 100 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 101 | assert_string_equal("seven", node->name); |
| 102 | |
| 103 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE)); |
| 104 | assert_string_equal("seven", node->name); |
| 105 | |
| 106 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 107 | assert_string_equal("five", node->name); |
| 108 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 109 | assert_string_equal("y", node->name); |
| 110 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 111 | assert_string_equal("seven", node->name); |
| 112 | |
| 113 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 114 | assert_string_equal("one", node->name); |
| 115 | |
| 116 | assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 117 | assert_string_equal("h-output", node->name); |
| 118 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 119 | |
| 120 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;" |
| 121 | "leaf a {type string; if-feature f;}" |
| 122 | "leaf b {type string;}}", LYS_IN_YANG)); |
| 123 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 124 | assert_string_equal("b", node->name); |
| 125 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 126 | assert_string_equal("a", node->name); |
| 127 | |
| 128 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG)); |
| 129 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 130 | assert_string_equal("c", node->name); |
| 131 | assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 132 | |
| 133 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG)); |
| 134 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 135 | assert_string_equal("d", node->name); |
| 136 | assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 137 | |
| 138 | *state = NULL; |
| 139 | ly_ctx_destroy(ctx, NULL); |
| 140 | } |
| 141 | static void |
| 142 | test_date(void **state) |
| 143 | { |
| 144 | *state = test_date; |
| 145 | |
| 146 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
| 147 | logbuf_assert("Invalid argument date (lysp_check_date())."); |
| 148 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
| 149 | logbuf_assert("Invalid argument date_len (lysp_check_date())."); |
| 150 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
| 151 | logbuf_assert("Invalid value \"nonsencexx\" of \"date\"."); |
| 152 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
| 153 | logbuf_assert("Invalid value \"123x-11-11\" of \"date\"."); |
| 154 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
| 155 | logbuf_assert("Invalid value \"2018-13-11\" of \"date\"."); |
| 156 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
| 157 | logbuf_assert("Invalid value \"2018-11-41\" of \"date\"."); |
| 158 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
| 159 | logbuf_assert("Invalid value \"2018-02-29\" of \"date\"."); |
| 160 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date")); |
| 161 | logbuf_assert("Invalid value \"2018.02-28\" of \"date\"."); |
| 162 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date")); |
| 163 | logbuf_assert("Invalid value \"2018-02.28\" of \"date\"."); |
| 164 | |
| 165 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 166 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 167 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 168 | |
| 169 | *state = NULL; |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | test_revisions(void **state) |
| 174 | { |
| 175 | (void) state; /* unused */ |
| 176 | |
| 177 | struct lysp_revision *revs = NULL, *rev; |
| 178 | |
| 179 | logbuf_clean(); |
| 180 | /* no error, it just does nothing */ |
| 181 | lysp_sort_revisions(NULL); |
| 182 | logbuf_assert(""); |
| 183 | |
| 184 | /* revisions are stored in wrong order - the newest is the last */ |
| 185 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
| 186 | strcpy(rev->date, "2018-01-01"); |
| 187 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
| 188 | strcpy(rev->date, "2018-12-31"); |
| 189 | |
| 190 | assert_int_equal(2, LY_ARRAY_SIZE(revs)); |
| 191 | assert_string_equal("2018-01-01", &revs[0]); |
| 192 | assert_string_equal("2018-12-31", &revs[1]); |
| 193 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 194 | lysp_sort_revisions(revs); |
| 195 | assert_string_equal("2018-12-31", &revs[0]); |
| 196 | assert_string_equal("2018-01-01", &revs[1]); |
| 197 | |
| 198 | LY_ARRAY_FREE(revs); |
| 199 | } |
| 200 | |
| 201 | LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 202 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 203 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 204 | { |
| 205 | *module_data = user_data; |
| 206 | *format = LYS_IN_YANG; |
| 207 | *free_module_data = NULL; |
| 208 | return LY_SUCCESS; |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | test_typedef(void **state) |
| 213 | { |
| 214 | *state = test_typedef; |
| 215 | |
| 216 | struct ly_ctx *ctx = NULL; |
| 217 | const char *str; |
| 218 | |
| 219 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 220 | |
| 221 | str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}"; |
| 222 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 223 | logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1."); |
| 224 | str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}"; |
| 225 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 226 | logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1."); |
| 227 | str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}"; |
| 228 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 229 | logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1."); |
| 230 | str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}"; |
| 231 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 232 | logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1."); |
| 233 | str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}"; |
| 234 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 235 | logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1."); |
| 236 | str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}"; |
| 237 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 238 | logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1."); |
| 239 | str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}"; |
| 240 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 241 | logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1."); |
| 242 | str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}"; |
| 243 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 244 | logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1."); |
| 245 | str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}"; |
| 246 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 247 | logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1."); |
| 248 | str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}"; |
| 249 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 250 | logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1."); |
| 251 | str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}"; |
| 252 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 253 | logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1."); |
| 254 | str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}"; |
| 255 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 256 | logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1."); |
| 257 | str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}"; |
| 258 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 259 | logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1."); |
| 260 | str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}"; |
| 261 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 262 | logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1."); |
| 263 | str = "module a {namespace urn:a; prefix a; typedef union {type string;}}"; |
| 264 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 265 | logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1."); |
| 266 | str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}"; |
| 267 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 268 | logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1."); |
| 269 | str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}"; |
| 270 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 271 | logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1."); |
| 272 | str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}"; |
| 273 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 274 | logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1."); |
| 275 | str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}"; |
| 276 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 277 | logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1."); |
| 278 | |
| 279 | str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} " |
| 280 | "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}" |
| 281 | "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}" |
| 282 | "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}" |
| 283 | "typedef uint32_ {type string;} typedef uint64_ {type string;}}"; |
| 284 | assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 285 | |
| 286 | str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}"; |
| 287 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 288 | logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1."); |
| 289 | |
| 290 | str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}"; |
| 291 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 292 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 293 | |
| 294 | str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}"; |
| 295 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 296 | logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type. Line number 1."); |
| 297 | |
| 298 | str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}"; |
| 299 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 300 | logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type. Line number 1."); |
| 301 | |
| 302 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}"); |
| 303 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 304 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 305 | logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1."); |
| 306 | |
| 307 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}"); |
| 308 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 309 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 310 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 311 | |
| 312 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}"); |
| 313 | str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}"; |
| 314 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 315 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 316 | |
| 317 | *state = NULL; |
| 318 | ly_ctx_destroy(ctx, NULL); |
| 319 | } |