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; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 112 | struct ly_in *in; |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 113 | |
| 114 | const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:metadata:a; prefix a;" |
| 115 | "import ietf-yang-metadata {prefix md;}" |
| 116 | "feature f;" |
| 117 | "md:annotation x {" |
| 118 | " description \"test\";" |
| 119 | " if-feature f;" |
| 120 | " reference \"test\";" |
| 121 | " status \"current\";" |
| 122 | " type uint8;" |
| 123 | " units meters;" |
| 124 | "}}"; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 125 | const char *feats[] = {"f", NULL}; |
| 126 | assert_int_equal(LY_SUCCESS, ly_in_new_memory(data, &in)); |
| 127 | assert_int_equal(LY_SUCCESS, lys_parse(s->ctx, in, LYS_IN_YANG, feats, &mod)); |
| 128 | ly_in_free(in, 0); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 129 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 130 | e = &mod->compiled->exts[0]; |
| 131 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
| 132 | assert_string_equal("meters", ant->units); |
| 133 | |
| 134 | /* invalid */ |
| 135 | /* missing mandatory type substatement */ |
| 136 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 137 | "import ietf-yang-metadata {prefix md;}" |
| 138 | "md:annotation aa;}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 139 | 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] | 140 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa"); |
| 141 | |
| 142 | /* not allowed substatement */ |
| 143 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 144 | "import ietf-yang-metadata {prefix md;}" |
| 145 | "md:annotation aa {default x;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 146 | 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] | 147 | logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa"); |
| 148 | |
| 149 | /* invalid cardinality of units substatement */ |
| 150 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 151 | "import ietf-yang-metadata {prefix md;}" |
| 152 | "md:annotation aa {type string; units x; units y;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 153 | 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] | 154 | logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa"); |
| 155 | |
| 156 | /* invalid cardinality of status substatement */ |
| 157 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 158 | "import ietf-yang-metadata {prefix md;}" |
| 159 | "md:annotation aa {type string; status current; status obsolete;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 160 | 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] | 161 | logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa"); |
| 162 | |
| 163 | /* invalid cardinality of status substatement */ |
| 164 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 165 | "import ietf-yang-metadata {prefix md;}" |
| 166 | "md:annotation aa {type string; type uint8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 167 | 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] | 168 | logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa"); |
| 169 | |
| 170 | /* duplication of the same annotation */ |
| 171 | data = "module aa {yang-version 1.1; namespace urn:tests:extensions:metadata:aa; prefix aa;" |
| 172 | "import ietf-yang-metadata {prefix md;}" |
| 173 | "md:annotation aa {type string;} md:annotation aa {type uint8;}}"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 174 | 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] | 175 | logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 176 | "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa"); |
| 177 | |
| 178 | s->func = NULL; |
| 179 | } |
| 180 | |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 181 | static void |
| 182 | test_yin(void **state) |
| 183 | { |
| 184 | struct state_s *s = (struct state_s*)(*state); |
| 185 | s->func = test_yin; |
| 186 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 187 | const struct lys_module *mod; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 188 | struct lysc_ext_instance *e; |
| 189 | struct lyext_metadata *ant; |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 190 | const char *data; |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 191 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 192 | /* TODO yin-extension-prefix-compilation-bug |
| 193 | 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] | 194 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:a\"/><prefix value=\"a\"/>\n" |
| 195 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 196 | "<feature name=\"f\"/>\n" |
| 197 | "<md:annotation name=\"x\">\n" |
| 198 | " <description><text>test</text></description>\n" |
| 199 | " <reference><text>test</text></reference>\n" |
| 200 | " <if-feature name=\"f\"/>\n" |
| 201 | " <status value=\"current\"/>\n" |
| 202 | " <type name=\"uint8\"/>\n" |
| 203 | " <units name=\"meters\"/>\n" |
| 204 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 205 | 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] | 206 | assert_int_equal(1, LY_ARRAY_COUNT(mod->compiled->exts)); |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 207 | e = &mod->compiled->exts[0]; |
| 208 | assert_non_null(ant = (struct lyext_metadata*)e->data); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 209 | assert_string_equal("meters", ant->units);*/ |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 210 | |
| 211 | /* invalid */ |
| 212 | /* missing mandatory type substatement */ |
| 213 | 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" |
| 214 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 215 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 216 | "<md:annotation name=\"aa\"/>\n" |
| 217 | "</module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 218 | 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] | 219 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"md:annotation aa\". /aa:{extension='md:annotation'}/aa"); |
| 220 | |
| 221 | /* not allowed substatement */ |
| 222 | 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" |
| 223 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 224 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 225 | "<md:annotation name=\"aa\">\n" |
| 226 | " <default value=\"x\"/>\n" |
| 227 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 228 | 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] | 229 | logbuf_assert("Invalid keyword \"default\" as a child of \"md:annotation aa\" extension instance. /aa:{extension='md:annotation'}/aa"); |
| 230 | |
| 231 | /* invalid cardinality of units substatement */ |
| 232 | 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" |
| 233 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 234 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 235 | "<md:annotation name=\"aa\">\n" |
| 236 | " <type name=\"string\"/>\n" |
| 237 | " <units name=\"x\"/>\n" |
| 238 | " <units name=\"y\"/>\n" |
| 239 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 240 | 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] | 241 | logbuf_assert("Duplicate keyword \"units\". /aa:{extension='md:annotation'}/aa"); |
| 242 | |
| 243 | /* invalid cardinality of status substatement */ |
| 244 | 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" |
| 245 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 246 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 247 | "<md:annotation name=\"aa\">\n" |
| 248 | " <type name=\"string\"/>\n" |
| 249 | " <status value=\"current\"/>\n" |
| 250 | " <status value=\"obsolete\"/>\n" |
| 251 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 252 | 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] | 253 | logbuf_assert("Duplicate keyword \"status\". /aa:{extension='md:annotation'}/aa"); |
| 254 | |
| 255 | /* invalid cardinality of status substatement */ |
| 256 | 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" |
| 257 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 258 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 259 | "<md:annotation name=\"aa\">\n" |
| 260 | " <type name=\"string\"/>\n" |
| 261 | " <type name=\"uint8\"/>\n" |
| 262 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 263 | 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] | 264 | logbuf_assert("Duplicate keyword \"type\". /aa:{extension='md:annotation'}/aa"); |
| 265 | |
| 266 | /* duplication of the same annotation */ |
| 267 | 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" |
| 268 | "<yang-version value=\"1.1\"/><namespace uri=\"urn:tests:extensions:metadata:aa\"/><prefix value=\"aa\"/>\n" |
| 269 | "<import module=\"ietf-yang-metadata\"><prefix value=\"md\"/></import>\n" |
| 270 | "<md:annotation name=\"aa\">\n" |
| 271 | " <type name=\"string\"/>\n" |
| 272 | "</md:annotation><md:annotation name=\"aa\">\n" |
| 273 | " <type name=\"uint8\"/>\n" |
| 274 | "</md:annotation></module>"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 275 | 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] | 276 | logbuf_assert("Extension plugin \"libyang 2 - metadata, version 1\": " |
| 277 | "Extension md:annotation is instantiated multiple times.) /aa:{extension='md:annotation'}/aa"); |
| 278 | s->func = NULL; |
| 279 | } |
| 280 | |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 281 | int main(void) |
| 282 | { |
| 283 | const struct CMUnitTest tests[] = { |
| 284 | cmocka_unit_test_setup_teardown(test_yang, setup, teardown), |
Radek Krejci | 7c96016 | 2019-09-18 14:16:12 +0200 | [diff] [blame] | 285 | cmocka_unit_test_setup_teardown(test_yin, setup, teardown), |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 289 | } |