Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_metadata.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for Metadata extension (annotation) support |
| 5 | * |
| 6 | * Copyright (c) 2019 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 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 23 | #include "libyang.h" |
| 24 | #include "plugins_exts_metadata.h" |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 25 | |
| 26 | #define BUFSIZE 1024 |
| 27 | char logbuf[BUFSIZE] = {0}; |
| 28 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 29 | |
| 30 | struct state_s { |
| 31 | void *func; |
| 32 | struct ly_ctx *ctx; |
| 33 | }; |
| 34 | |
| 35 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 36 | #define ENABLE_LOGGER_CHECKING 1 |
| 37 | |
| 38 | #if ENABLE_LOGGER_CHECKING |
| 39 | static void |
| 40 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 41 | { |
| 42 | (void) level; /* unused */ |
| 43 | if (store) { |
| 44 | if (path && path[0]) { |
| 45 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 46 | } else { |
| 47 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 48 | } |
| 49 | if (store > 0) { |
| 50 | --store; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | setup(void **state) |
| 58 | { |
| 59 | struct state_s *s; |
| 60 | |
| 61 | s = calloc(1, sizeof *s); |
| 62 | assert_non_null(s); |
| 63 | |
| 64 | #if ENABLE_LOGGER_CHECKING |
| 65 | ly_set_log_clb(logger, 1); |
| 66 | #endif |
| 67 | |
| 68 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &s->ctx)); |
| 69 | *state = s; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int |
| 75 | teardown(void **state) |
| 76 | { |
| 77 | struct state_s *s = (struct state_s*)(*state); |
| 78 | |
| 79 | #if ENABLE_LOGGER_CHECKING |
| 80 | if (s->func) { |
| 81 | fprintf(stderr, "%s\n", logbuf); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | ly_ctx_destroy(s->ctx, NULL); |
| 86 | free(s); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | logbuf_clean(void) |
| 93 | { |
| 94 | logbuf[0] = '\0'; |
| 95 | } |
| 96 | |
| 97 | #if ENABLE_LOGGER_CHECKING |
| 98 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 99 | #else |
| 100 | # define logbuf_assert(str) |
| 101 | #endif |
| 102 | |
| 103 | static void |
| 104 | test_yang(void **state) |
| 105 | { |
| 106 | struct state_s *s = (struct state_s*)(*state); |
| 107 | s->func = test_yang; |
| 108 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 109 | const struct lys_module *mod; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 110 | struct lysc_ext_instance *e; |
| 111 | struct lyext_metadata *ant; |
| 112 | |
| 113 | const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;" |
| 114 | "import ietf-yang-metadata {prefix md;}" |
| 115 | "feature f;" |
| 116 | "md:annotation x {" |
| 117 | " description \"test\";" |
| 118 | " if-feature f;" |
| 119 | " reference \"test\";" |
| 120 | " status \"current\";" |
| 121 | " type uint8;" |
| 122 | " units meters;" |
| 123 | "}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 124 | assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod)); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 125 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 126 | e = &mod->compiled->exts[0]; |
| 127 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
| 128 | assert_string_equal("meters", ant->units); |
| 129 | |
| 130 | /* invalid */ |
| 131 | /* missing mandatory type substatement */ |
| 132 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 133 | "import ietf-yang-metadata {prefix md;}" |
| 134 | "md:annotation aa;}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 135 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 136 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa"); |
| 137 | |
| 138 | /* not allowed substatement */ |
| 139 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 140 | "import ietf-yang-metadata {prefix md;}" |
| 141 | "md:annotation aa {default x;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 142 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 143 | logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa"); |
| 144 | |
| 145 | /* invalid cardinality of units substatement */ |
| 146 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 147 | "import ietf-yang-metadata {prefix md;}" |
| 148 | "md:annotation aa {type string; units x; units y;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 149 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 150 | logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa"); |
| 151 | |
| 152 | /* invalid cardinality of status substatement */ |
| 153 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 154 | "import ietf-yang-metadata {prefix md;}" |
| 155 | "md:annotation aa {type string; status current; status obsolete;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 156 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 157 | logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa"); |
| 158 | |
| 159 | /* invalid cardinality of status substatement */ |
| 160 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 161 | "import ietf-yang-metadata {prefix md;}" |
| 162 | "md:annotation aa {type string; type uint8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 163 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 164 | logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa"); |
| 165 | |
| 166 | /* duplication of the same annotation */ |
| 167 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 168 | "import ietf-yang-metadata {prefix md;}" |
| 169 | "md:annotation aa {type string;} md:annotation aa {type uint8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 171 | logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 172 | "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa"); |
| 173 | |
| 174 | s->func = NULL; |
| 175 | } |
| 176 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 177 | static void |
| 178 | test_yin(void **state) |
| 179 | { |
| 180 | struct state_s *s = (struct state_s*)(*state); |
| 181 | s->func = test_yin; |
| 182 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 183 | const struct lys_module *mod; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 184 | struct lysc_ext_instance *e; |
| 185 | struct lyext_metadata *ant; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 186 | const char *data; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 187 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 188 | /* TODO yin-extension-prefix-compilation-bug |
| 189 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"a\">\n" |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 190 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n" |
| 191 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 192 | "<feature name=\"f\"/>\n" |
| 193 | "<md:annotation name=\"x\">\n" |
| 194 | " <description><text>test</text></description>\n" |
| 195 | " <reference><text>test</text></reference>\n" |
| 196 | " <if-feature name=\"f\"/>\n" |
| 197 | " <status value=\"current\"/>\n" |
| 198 | " <type name=\"uint8\"/>\n" |
| 199 | " <units name=\"meters\"/>\n" |
| 200 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YIN, &mod)); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 202 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 203 | e = &mod->compiled->exts[0]; |
| 204 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 205 | assert_string_equal("meters", ant->units);*/ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 206 | |
| 207 | /* invalid */ |
| 208 | /* missing mandatory type substatement */ |
| 209 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 210 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 211 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 212 | "<md:annotation name=\"aa\"/>\n" |
| 213 | "</module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 214 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 215 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa"); |
| 216 | |
| 217 | /* not allowed substatement */ |
| 218 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 219 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 220 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 221 | "<md:annotation name=\"aa\">\n" |
| 222 | " <default value=\"x\"/>\n" |
| 223 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 224 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 225 | logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa"); |
| 226 | |
| 227 | /* invalid cardinality of units substatement */ |
| 228 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 229 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 230 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 231 | "<md:annotation name=\"aa\">\n" |
| 232 | " <type name=\"string\"/>\n" |
| 233 | " <units name=\"x\"/>\n" |
| 234 | " <units name=\"y\"/>\n" |
| 235 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 236 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 237 | logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa"); |
| 238 | |
| 239 | /* invalid cardinality of status substatement */ |
| 240 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 241 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 242 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 243 | "<md:annotation name=\"aa\">\n" |
| 244 | " <type name=\"string\"/>\n" |
| 245 | " <status value=\"current\"/>\n" |
| 246 | " <status value=\"obsolete\"/>\n" |
| 247 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 248 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 249 | logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa"); |
| 250 | |
| 251 | /* invalid cardinality of status substatement */ |
| 252 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 253 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 254 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 255 | "<md:annotation name=\"aa\">\n" |
| 256 | " <type name=\"string\"/>\n" |
| 257 | " <type name=\"uint8\"/>\n" |
| 258 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 259 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 260 | logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa"); |
| 261 | |
| 262 | /* duplication of the same annotation */ |
| 263 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"aa\">\n" |
| 264 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 265 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 266 | "<md:annotation name=\"aa\">\n" |
| 267 | " <type name=\"string\"/>\n" |
| 268 | "</md:annotation><md:annotation name=\"aa\">\n" |
| 269 | " <type name=\"uint8\"/>\n" |
| 270 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 271 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YIN, NULL)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 272 | logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 273 | "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa"); |
| 274 | s->func = NULL; |
| 275 | } |
| 276 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 277 | int main(void) |
| 278 | { |
| 279 | const struct CMUnitTest tests[] = { |
| 280 | cmocka_unit_test_setup_teardown(test_yang, setup, teardown), |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 281 | cmocka_unit_test_setup_teardown(test_yin, setup, teardown), |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 285 | } |