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 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 23 | #include "context.h" |
| 24 | #include "log.h" |
| 25 | #include "tree_schema.h" |
| 26 | #include "tree_schema_internal.h" |
| 27 | |
| 28 | #include "test_schema.h" |
| 29 | |
| 30 | void |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 31 | test_getnext(void **state) |
| 32 | { |
| 33 | *state = test_getnext; |
| 34 | |
| 35 | struct ly_ctx *ctx; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 36 | const struct lys_module *mod; |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 37 | const struct lysc_node *node = NULL, *four; |
| 38 | const struct lysc_node_container *cont; |
| 39 | const struct lysc_action *rpc; |
| 40 | |
| 41 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 42 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 43 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 44 | "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}" |
| 45 | " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}" |
| 46 | " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}" |
| 47 | " notification nine {leaf nine-data {type string;}}}" |
| 48 | "leaf b {type string;} leaf-list c {type string;} list d {config false;}" |
Radek Krejci | ff2dbc5 | 2020-11-02 11:05:09 +0100 | [diff] [blame^] | 49 | "choice x { case empty-x; leaf e {type string;} case y {leaf f {type string;}}} anyxml g;" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 50 | "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}" |
| 51 | "rpc i;" |
| 52 | "notification j {leaf i-data {type string;}}" |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 53 | "notification k;}", LYS_IN_YANG, &mod)); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 54 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 55 | assert_string_equal("a", node->name); |
| 56 | cont = (const struct lysc_node_container*)node; |
| 57 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 58 | assert_string_equal("b", node->name); |
| 59 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 60 | assert_string_equal("c", node->name); |
| 61 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 62 | assert_string_equal("d", node->name); |
| 63 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 64 | assert_string_equal("e", node->name); |
| 65 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 66 | assert_string_equal("f", node->name); |
| 67 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 68 | assert_string_equal("g", node->name); |
| 69 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 70 | assert_string_equal("h", node->name); |
| 71 | rpc = (const struct lysc_action*)node; |
| 72 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 73 | assert_string_equal("i", node->name); |
| 74 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 75 | assert_string_equal("j", node->name); |
| 76 | assert_non_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 77 | assert_string_equal("k", node->name); |
| 78 | assert_null(node = lys_getnext(node, NULL, mod->compiled, 0)); |
| 79 | /* Inside container */ |
| 80 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 81 | assert_string_equal("one", node->name); |
| 82 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 83 | assert_string_equal("two", node->name); |
| 84 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 85 | assert_string_equal("three", node->name); |
| 86 | assert_non_null(node = four = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 87 | assert_string_equal("four", node->name); |
| 88 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 89 | assert_string_equal("five", node->name); |
| 90 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 91 | assert_string_equal("six", node->name); |
| 92 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 93 | assert_string_equal("seven", node->name); |
| 94 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 95 | assert_string_equal("eight", node->name); |
| 96 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 97 | assert_string_equal("nine", node->name); |
| 98 | assert_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, 0)); |
| 99 | /* Inside RPC */ |
| 100 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 101 | assert_string_equal("h-input", node->name); |
| 102 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, 0)); |
| 103 | |
| 104 | /* options */ |
| 105 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 106 | assert_string_equal("x", node->name); |
| 107 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCHOICE)); |
| 108 | assert_string_equal("seven", node->name); |
| 109 | |
| 110 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_NOCHOICE)); |
| 111 | assert_string_equal("seven", node->name); |
| 112 | |
| 113 | assert_non_null(node = lys_getnext(four, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 114 | assert_string_equal("five", node->name); |
| 115 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 116 | assert_string_equal("y", node->name); |
| 117 | assert_non_null(node = lys_getnext(node, (const struct lysc_node*)cont, mod->compiled, LYS_GETNEXT_WITHCASE)); |
| 118 | assert_string_equal("seven", node->name); |
| 119 | |
| 120 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_INTONPCONT)); |
| 121 | assert_string_equal("one", node->name); |
| 122 | |
| 123 | assert_non_null(node = lys_getnext(NULL, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 124 | assert_string_equal("h-output", node->name); |
| 125 | assert_null(node = lys_getnext(node, (const struct lysc_node*)rpc, mod->compiled, LYS_GETNEXT_OUTPUT)); |
| 126 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 127 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; feature f;" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 128 | "leaf a {type string; if-feature f;}" |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 129 | "leaf b {type string;}}", LYS_IN_YANG, &mod)); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 130 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 131 | assert_string_equal("b", node->name); |
| 132 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 133 | assert_string_equal("a", node->name); |
| 134 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 135 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; rpc c;}", LYS_IN_YANG, &mod)); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 136 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 137 | assert_string_equal("c", node->name); |
| 138 | assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 139 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 140 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; notification d;}", LYS_IN_YANG, &mod)); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 141 | assert_non_null(node = lys_getnext(NULL, NULL, mod->compiled, 0)); |
| 142 | assert_string_equal("d", node->name); |
| 143 | assert_null(node = lys_getnext(node, NULL, mod->compiled, LYS_GETNEXT_NOSTATECHECK)); |
| 144 | |
| 145 | *state = NULL; |
| 146 | ly_ctx_destroy(ctx, NULL); |
| 147 | } |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 148 | void |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 149 | test_date(void **state) |
| 150 | { |
| 151 | *state = test_date; |
| 152 | |
| 153 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
| 154 | logbuf_assert("Invalid argument date (lysp_check_date())."); |
| 155 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
| 156 | logbuf_assert("Invalid argument date_len (lysp_check_date())."); |
| 157 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
| 158 | logbuf_assert("Invalid value \"nonsencexx\" of \"date\"."); |
| 159 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
| 160 | logbuf_assert("Invalid value \"123x-11-11\" of \"date\"."); |
| 161 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
| 162 | logbuf_assert("Invalid value \"2018-13-11\" of \"date\"."); |
| 163 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
| 164 | logbuf_assert("Invalid value \"2018-11-41\" of \"date\"."); |
| 165 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
| 166 | logbuf_assert("Invalid value \"2018-02-29\" of \"date\"."); |
| 167 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date")); |
| 168 | logbuf_assert("Invalid value \"2018.02-28\" of \"date\"."); |
| 169 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date")); |
| 170 | logbuf_assert("Invalid value \"2018-02.28\" of \"date\"."); |
| 171 | |
| 172 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 173 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 174 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 175 | |
| 176 | *state = NULL; |
| 177 | } |
| 178 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 179 | void |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 180 | test_revisions(void **state) |
| 181 | { |
| 182 | (void) state; /* unused */ |
| 183 | |
| 184 | struct lysp_revision *revs = NULL, *rev; |
| 185 | |
| 186 | logbuf_clean(); |
| 187 | /* no error, it just does nothing */ |
| 188 | lysp_sort_revisions(NULL); |
| 189 | logbuf_assert(""); |
| 190 | |
| 191 | /* revisions are stored in wrong order - the newest is the last */ |
| 192 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
| 193 | strcpy(rev->date, "2018-01-01"); |
| 194 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
| 195 | strcpy(rev->date, "2018-12-31"); |
| 196 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 197 | assert_int_equal(2, LY_ARRAY_COUNT(revs)); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 198 | assert_string_equal("2018-01-01", &revs[0]); |
| 199 | assert_string_equal("2018-12-31", &revs[1]); |
| 200 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 201 | lysp_sort_revisions(revs); |
| 202 | assert_string_equal("2018-12-31", &revs[0]); |
| 203 | assert_string_equal("2018-01-01", &revs[1]); |
| 204 | |
| 205 | LY_ARRAY_FREE(revs); |
| 206 | } |
| 207 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 208 | void |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 209 | test_typedef(void **state) |
| 210 | { |
| 211 | *state = test_typedef; |
| 212 | |
| 213 | struct ly_ctx *ctx = NULL; |
| 214 | const char *str; |
| 215 | |
| 216 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 217 | |
| 218 | str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 219 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 220 | logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type. Line number 1."); |
| 221 | str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 222 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 223 | logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type. Line number 1."); |
| 224 | str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 225 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 226 | logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type. Line number 1."); |
| 227 | str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 228 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 229 | logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type. Line number 1."); |
| 230 | str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 231 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 232 | logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type. Line number 1."); |
| 233 | str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 234 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 235 | logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type. Line number 1."); |
| 236 | str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 237 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 238 | logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type. Line number 1."); |
| 239 | str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 240 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 241 | logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type. Line number 1."); |
| 242 | str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 243 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 244 | logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type. Line number 1."); |
| 245 | str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 246 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 247 | logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type. Line number 1."); |
| 248 | str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 249 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 250 | logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type. Line number 1."); |
| 251 | str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 252 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 253 | logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type. Line number 1."); |
| 254 | str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 255 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 256 | logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type. Line number 1."); |
| 257 | str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 258 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 259 | logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type. Line number 1."); |
| 260 | str = "module a {namespace urn:a; prefix a; typedef union {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 261 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 262 | logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type. Line number 1."); |
| 263 | str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 264 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 265 | logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type. Line number 1."); |
| 266 | str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 267 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 268 | logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type. Line number 1."); |
| 269 | str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 270 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 271 | logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type. Line number 1."); |
| 272 | str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 273 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 274 | logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type. Line number 1."); |
| 275 | |
| 276 | str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} " |
| 277 | "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}" |
| 278 | "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}" |
| 279 | "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}" |
| 280 | "typedef uint32_ {type string;} typedef uint64_ {type string;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 281 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 282 | |
| 283 | str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 284 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 285 | logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type. Line number 1."); |
| 286 | |
| 287 | str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 288 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 289 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 290 | |
| 291 | str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 292 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 293 | logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type. Line number 1."); |
| 294 | |
| 295 | str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 296 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 297 | logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type. Line number 1."); |
| 298 | |
| 299 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}"); |
| 300 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 301 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 302 | logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type. Line number 1."); |
| 303 | |
| 304 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}"); |
| 305 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 306 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 307 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 308 | |
| 309 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}"); |
| 310 | str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 311 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 312 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type. Line number 1."); |
| 313 | |
| 314 | *state = NULL; |
| 315 | ly_ctx_destroy(ctx, NULL); |
| 316 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 317 | |
| 318 | void |
| 319 | test_accessible_tree(void **state) |
| 320 | { |
| 321 | *state = test_accessible_tree; |
| 322 | |
| 323 | struct ly_ctx *ctx = NULL; |
| 324 | const char *str; |
| 325 | |
| 326 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 327 | logbuf_clean(); |
| 328 | |
| 329 | /* config -> config */ |
| 330 | str = |
| 331 | "module a {" |
| 332 | "namespace urn:a;" |
| 333 | "prefix a;" |
| 334 | "container cont {" |
| 335 | "leaf l {" |
| 336 | "type empty;" |
| 337 | "}" |
| 338 | "}" |
| 339 | "container cont2 {" |
| 340 | "leaf l2 {" |
| 341 | "must ../../cont/l;" |
| 342 | "type leafref {" |
| 343 | "path /cont/l;" |
| 344 | "}" |
| 345 | "}" |
| 346 | "}" |
| 347 | "}"; |
| 348 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 349 | logbuf_assert(""); |
| 350 | |
| 351 | /* config -> state leafref */ |
| 352 | str = |
| 353 | "module b {" |
| 354 | "namespace urn:a;" |
| 355 | "prefix a;" |
| 356 | "container cont {" |
| 357 | "config false;" |
| 358 | "leaf l {" |
| 359 | "type empty;" |
| 360 | "}" |
| 361 | "}" |
| 362 | "container cont2 {" |
| 363 | "leaf l2 {" |
| 364 | "type leafref {" |
| 365 | "path /cont/l;" |
| 366 | "}" |
| 367 | "}" |
| 368 | "}" |
| 369 | "}"; |
| 370 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
| 371 | logbuf_assert("Invalid leafref path \"/cont/l\" - target is supposed to represent configuration data" |
| 372 | " (as the leafref does), but it does not. /b:cont2/l2"); |
| 373 | logbuf_clean(); |
| 374 | |
| 375 | /* config -> state must */ |
| 376 | str = |
| 377 | "module b {" |
| 378 | "namespace urn:a;" |
| 379 | "prefix a;" |
| 380 | "container cont {" |
| 381 | "config false;" |
| 382 | "leaf l {" |
| 383 | "type empty;" |
| 384 | "}" |
| 385 | "}" |
| 386 | "container cont2 {" |
| 387 | "leaf l2 {" |
| 388 | "must ../../cont/l;" |
| 389 | "type empty;" |
| 390 | "}" |
| 391 | "}" |
| 392 | "}"; |
| 393 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 394 | logbuf_assert("Schema node \"l\" not found (../../cont/) with context node \"/b:cont2/l2\"."); |
| 395 | logbuf_clean(); |
| 396 | |
| 397 | /* state -> config */ |
| 398 | str = |
| 399 | "module c {" |
| 400 | "namespace urn:a;" |
| 401 | "prefix a;" |
| 402 | "container cont {" |
| 403 | "leaf l {" |
| 404 | "type empty;" |
| 405 | "}" |
| 406 | "}" |
| 407 | "container cont2 {" |
| 408 | "config false;" |
| 409 | "leaf l2 {" |
| 410 | "must ../../cont/l;" |
| 411 | "type leafref {" |
| 412 | "path /cont/l;" |
| 413 | "}" |
| 414 | "}" |
| 415 | "}" |
| 416 | "}"; |
| 417 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 418 | logbuf_assert(""); |
| 419 | |
| 420 | /* notif -> state */ |
| 421 | str = |
| 422 | "module d {" |
| 423 | "namespace urn:a;" |
| 424 | "prefix a;" |
| 425 | "container cont {" |
| 426 | "config false;" |
| 427 | "leaf l {" |
| 428 | "type empty;" |
| 429 | "}" |
| 430 | "}" |
| 431 | "notification notif {" |
| 432 | "leaf l2 {" |
| 433 | "must ../../cont/l;" |
| 434 | "type leafref {" |
| 435 | "path /cont/l;" |
| 436 | "}" |
| 437 | "}" |
| 438 | "}" |
| 439 | "}"; |
| 440 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 441 | logbuf_assert(""); |
| 442 | |
| 443 | /* notif -> notif */ |
| 444 | str = |
| 445 | "module e {" |
| 446 | "namespace urn:a;" |
| 447 | "prefix a;" |
| 448 | "notification notif {" |
| 449 | "leaf l {" |
| 450 | "type empty;" |
| 451 | "}" |
| 452 | "leaf l2 {" |
| 453 | "must ../../notif/l;" |
| 454 | "type leafref {" |
| 455 | "path /notif/l;" |
| 456 | "}" |
| 457 | "}" |
| 458 | "}" |
| 459 | "}"; |
| 460 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 461 | logbuf_assert(""); |
| 462 | |
| 463 | /* rpc input -> state */ |
| 464 | str = |
| 465 | "module f {" |
| 466 | "namespace urn:a;" |
| 467 | "prefix a;" |
| 468 | "container cont {" |
| 469 | "config false;" |
| 470 | "leaf l {" |
| 471 | "type empty;" |
| 472 | "}" |
| 473 | "}" |
| 474 | "rpc rp {" |
| 475 | "input {" |
| 476 | "leaf l2 {" |
| 477 | "must ../../cont/l;" |
| 478 | "type leafref {" |
| 479 | "path /cont/l;" |
| 480 | "}" |
| 481 | "}" |
| 482 | "}" |
| 483 | "}" |
| 484 | "}"; |
| 485 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 486 | logbuf_assert(""); |
| 487 | |
| 488 | /* rpc input -> rpc input */ |
| 489 | str = |
| 490 | "module g {" |
| 491 | "namespace urn:a;" |
| 492 | "prefix a;" |
| 493 | "rpc rp {" |
| 494 | "input {" |
| 495 | "leaf l {" |
| 496 | "type empty;" |
| 497 | "}" |
| 498 | "leaf l2 {" |
| 499 | "must ../l;" |
| 500 | "type leafref {" |
| 501 | "path /rp/l;" |
| 502 | "}" |
| 503 | "}" |
| 504 | "}" |
| 505 | "}" |
| 506 | "}"; |
| 507 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 508 | logbuf_assert(""); |
| 509 | |
| 510 | /* rpc input -> rpc output leafref */ |
| 511 | str = |
| 512 | "module h {" |
| 513 | "namespace urn:a;" |
| 514 | "prefix a;" |
| 515 | "rpc rp {" |
| 516 | "input {" |
| 517 | "leaf l2 {" |
| 518 | "type leafref {" |
| 519 | "path /rp/l;" |
| 520 | "}" |
| 521 | "}" |
| 522 | "}" |
| 523 | "output {" |
| 524 | "leaf l {" |
| 525 | "type empty;" |
| 526 | "}" |
| 527 | "}" |
| 528 | "}" |
| 529 | "}"; |
| 530 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
| 531 | logbuf_assert("Not found node \"l\" in path. /h:rp/l2"); |
| 532 | logbuf_clean(); |
| 533 | |
| 534 | /* rpc input -> rpc output must */ |
| 535 | str = |
| 536 | "module h {" |
| 537 | "namespace urn:a;" |
| 538 | "prefix a;" |
| 539 | "rpc rp {" |
| 540 | "input {" |
| 541 | "leaf l2 {" |
| 542 | "must ../l;" |
| 543 | "type empty;" |
| 544 | "}" |
| 545 | "}" |
| 546 | "output {" |
| 547 | "leaf l {" |
| 548 | "type empty;" |
| 549 | "}" |
| 550 | "}" |
| 551 | "}" |
| 552 | "}"; |
| 553 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 554 | logbuf_assert("Schema node \"l\" not found (../) with context node \"/h:rp/l2\"."); |
| 555 | logbuf_clean(); |
| 556 | |
| 557 | /* rpc input -> notif leafref */ |
| 558 | str = |
| 559 | "module i {" |
| 560 | "namespace urn:a;" |
| 561 | "prefix a;" |
| 562 | "rpc rp {" |
| 563 | "input {" |
| 564 | "leaf l2 {" |
| 565 | "type leafref {" |
| 566 | "path ../../notif/l;" |
| 567 | "}" |
| 568 | "}" |
| 569 | "}" |
| 570 | "}" |
| 571 | "notification notif {" |
| 572 | "leaf l {" |
| 573 | "type empty;" |
| 574 | "}" |
| 575 | "}" |
| 576 | "}"; |
| 577 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
| 578 | logbuf_assert("Not found node \"notif\" in path. /i:rp/l2"); |
| 579 | logbuf_clean(); |
| 580 | |
| 581 | /* rpc input -> notif must */ |
| 582 | str = |
| 583 | "module i {" |
| 584 | "namespace urn:a;" |
| 585 | "prefix a;" |
| 586 | "rpc rp {" |
| 587 | "input {" |
| 588 | "leaf l2 {" |
| 589 | "must /notif/l;" |
| 590 | "type empty;" |
| 591 | "}" |
| 592 | "}" |
| 593 | "}" |
| 594 | "notification notif {" |
| 595 | "leaf l {" |
| 596 | "type empty;" |
| 597 | "}" |
| 598 | "}" |
| 599 | "}"; |
| 600 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 601 | logbuf_assert("Schema node \"l\" not found (/notif/) with context node \"/i:rp/l2\"."); |
| 602 | logbuf_clean(); |
| 603 | |
| 604 | /* action output -> state */ |
| 605 | str = |
| 606 | "module j {" |
| 607 | "yang-version 1.1;" |
| 608 | "namespace urn:a;" |
| 609 | "prefix a;" |
| 610 | "container cont {" |
| 611 | "list ll {" |
| 612 | "key k;" |
| 613 | "leaf k {" |
| 614 | "type string;" |
| 615 | "}" |
| 616 | "action act {" |
| 617 | "output {" |
| 618 | "leaf l2 {" |
| 619 | "must /cont/l;" |
| 620 | "type leafref {" |
| 621 | "path ../../../l;" |
| 622 | "}" |
| 623 | "}" |
| 624 | "}" |
| 625 | "}" |
| 626 | "}" |
| 627 | "leaf l {" |
| 628 | "config false;" |
| 629 | "type empty;" |
| 630 | "}" |
| 631 | "}" |
| 632 | "}"; |
| 633 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 634 | logbuf_assert(""); |
| 635 | |
| 636 | /* action output -> action input leafref */ |
| 637 | str = |
| 638 | "module k {" |
| 639 | "yang-version 1.1;" |
| 640 | "namespace urn:a;" |
| 641 | "prefix a;" |
| 642 | "container cont {" |
| 643 | "list ll {" |
| 644 | "key k;" |
| 645 | "leaf k {" |
| 646 | "type string;" |
| 647 | "}" |
| 648 | "action act {" |
| 649 | "input {" |
| 650 | "leaf l {" |
| 651 | "type empty;" |
| 652 | "}" |
| 653 | "}" |
| 654 | "output {" |
| 655 | "leaf l2 {" |
| 656 | "type leafref {" |
| 657 | "path ../l;" |
| 658 | "}" |
| 659 | "}" |
| 660 | "}" |
| 661 | "}" |
| 662 | "}" |
| 663 | "}" |
| 664 | "}"; |
| 665 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_EVALID); |
| 666 | logbuf_assert("Not found node \"l\" in path. /k:cont/ll/act/l2"); |
| 667 | logbuf_clean(); |
| 668 | |
| 669 | /* action output -> action input must */ |
| 670 | str = |
| 671 | "module k {" |
| 672 | "yang-version 1.1;" |
| 673 | "namespace urn:a;" |
| 674 | "prefix a;" |
| 675 | "container cont {" |
| 676 | "list ll {" |
| 677 | "key k;" |
| 678 | "leaf k {" |
| 679 | "type string;" |
| 680 | "}" |
| 681 | "action act {" |
| 682 | "input {" |
| 683 | "leaf l {" |
| 684 | "type empty;" |
| 685 | "}" |
| 686 | "}" |
| 687 | "output {" |
| 688 | "leaf l2 {" |
| 689 | "must /cont/ll/act/l;" |
| 690 | "type empty;" |
| 691 | "}" |
| 692 | "}" |
| 693 | "}" |
| 694 | "}" |
| 695 | "}" |
| 696 | "}"; |
| 697 | assert_int_equal(lys_parse_mem(ctx, str, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 698 | logbuf_assert("Schema node \"l\" not found (/cont/ll/act/) with context node \"/k:cont/ll/act/l2\"."); |
| 699 | logbuf_clean(); |
| 700 | |
| 701 | *state = NULL; |
| 702 | ly_ctx_destroy(ctx, NULL); |
| 703 | } |