Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | 9e68d87 | 2016-09-15 14:50:48 +0200 | [diff] [blame] | 2 | * \file test_conformance.c |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | 9e68d87 | 2016-09-15 14:50:48 +0200 | [diff] [blame] | 4 | * \brief libyang tests - setting up modules as imported or implemented |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 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 <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <pthread.h> |
| 18 | #include <setjmp.h> |
| 19 | #include <stdarg.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
| 28 | #include "../../src/libyang.h" |
| 29 | #include "../config.h" |
| 30 | |
| 31 | #define SCHEMA_FOLDER_YIN TESTS_DIR"/schema/yin/conformance" |
| 32 | #define SCHEMA_FOLDER_YANG TESTS_DIR"/schema/yang/conformance" |
| 33 | |
| 34 | static int |
| 35 | setup_ctx(void **state) |
| 36 | { |
| 37 | *state = ly_ctx_new(NULL); |
| 38 | if (!*state) { |
| 39 | return -1; |
| 40 | } |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int |
| 45 | teardown_ctx(void **state) |
| 46 | { |
| 47 | ly_ctx_destroy(*state, NULL); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | test_implemented1_yin(void **state) |
| 53 | { |
| 54 | struct ly_ctx *ctx = *state; |
| 55 | const struct lys_module *a, *b, *b2, *c, *c2; |
| 56 | |
| 57 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN); |
| 58 | |
| 59 | /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */ |
| 60 | a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN); |
| 61 | assert_ptr_not_equal(a, NULL); |
| 62 | assert_int_equal(a->implemented, 1); |
| 63 | |
| 64 | b = ly_ctx_get_module(ctx, "b", NULL); |
| 65 | assert_ptr_not_equal(b, NULL); |
| 66 | assert_int_equal(b->implemented, 1); |
| 67 | |
| 68 | c = ly_ctx_get_module(ctx, "c", NULL); |
| 69 | assert_ptr_not_equal(c, NULL); |
| 70 | assert_int_equal(c->implemented, 0); |
| 71 | |
| 72 | /* another b cannot be loaded, since it is already implemented */ |
| 73 | b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/b@2015-04-04.yin", LYS_IN_YIN); |
| 74 | assert_ptr_equal(b2, NULL); |
| 75 | assert_int_equal(ly_errno, LY_EINVAL); |
| 76 | assert_string_equal(ly_errmsg(), "Module \"b\" parsing failed."); |
| 77 | |
| 78 | /* older c can be loaded and it will be marked as implemented */ |
| 79 | c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/c@2015-01-01.yin", LYS_IN_YIN); |
| 80 | assert_ptr_not_equal(c2, NULL); |
| 81 | assert_int_equal(c2->implemented, 1); |
| 82 | assert_ptr_not_equal(c, c2); |
| 83 | } |
| 84 | |
| 85 | static void |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 86 | test_implemented1_yang(void **state) |
| 87 | { |
| 88 | struct ly_ctx *ctx = *state; |
| 89 | const struct lys_module *a, *b, *b2, *c, *c2; |
| 90 | |
| 91 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG); |
| 92 | |
| 93 | /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */ |
| 94 | a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG); |
| 95 | assert_ptr_not_equal(a, NULL); |
| 96 | assert_int_equal(a->implemented, 1); |
| 97 | |
| 98 | b = ly_ctx_get_module(ctx, "b", NULL); |
| 99 | assert_ptr_not_equal(b, NULL); |
| 100 | assert_int_equal(b->implemented, 1); |
| 101 | |
| 102 | c = ly_ctx_get_module(ctx, "c", NULL); |
| 103 | assert_ptr_not_equal(c, NULL); |
| 104 | assert_int_equal(c->implemented, 0); |
| 105 | |
| 106 | /* another b cannot be loaded, since it is already implemented */ |
| 107 | b2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/b@2015-04-04.yang", LYS_IN_YANG); |
| 108 | assert_ptr_equal(b2, NULL); |
| 109 | assert_int_equal(ly_errno, LY_EINVAL); |
| 110 | assert_string_equal(ly_errmsg(), "Module \"b\" parsing failed."); |
| 111 | |
| 112 | /* older c can be loaded and it will be marked as implemented */ |
| 113 | c2 = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/c@2015-01-01.yang", LYS_IN_YANG); |
| 114 | assert_ptr_not_equal(c2, NULL); |
| 115 | assert_int_equal(c2->implemented, 1); |
| 116 | assert_ptr_not_equal(c, c2); |
| 117 | } |
| 118 | |
| 119 | static void |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 120 | test_implemented2_yin(void **state) |
| 121 | { |
| 122 | struct ly_ctx *ctx = *state; |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 123 | const struct lys_module *a, *b2; |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 124 | |
| 125 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN); |
| 126 | |
| 127 | /* load the newest b first, it is implemented */ |
| 128 | b2 = ly_ctx_load_module(ctx, "b", "2015-04-04"); |
| 129 | assert_ptr_not_equal(b2, NULL); |
| 130 | assert_int_equal(b2->implemented, 1); |
| 131 | |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 132 | /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */ |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 133 | a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN); |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 134 | assert_ptr_equal(a, NULL); |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 138 | test_implemented2_yang(void **state) |
| 139 | { |
| 140 | struct ly_ctx *ctx = *state; |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 141 | const struct lys_module *a, *b2; |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 142 | |
| 143 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG); |
| 144 | |
| 145 | /* load the newest b first, it is implemented */ |
| 146 | b2 = ly_ctx_load_module(ctx, "b", "2015-04-04"); |
| 147 | assert_ptr_not_equal(b2, NULL); |
| 148 | assert_int_equal(b2->implemented, 1); |
| 149 | |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 150 | /* loads a.yin (impl), b@2015-04-04 is augmented by a, but cannot be implemented */ |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 151 | a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG); |
Michal Vasko | a51f3ad | 2017-05-23 10:54:37 +0200 | [diff] [blame^] | 152 | assert_ptr_equal(a, NULL); |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 156 | test_implemented_info_yin(void **state) |
| 157 | { |
| 158 | struct ly_ctx *ctx = *state; |
| 159 | struct lyd_node *info; |
| 160 | const struct lys_module *a; |
| 161 | char *data; |
| 162 | const char *template = "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 163 | " <module>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 164 | " <name>ietf-yang-metadata</name>\n" |
| 165 | " <revision>2016-08-05</revision>\n" |
| 166 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n" |
| 167 | " <conformance-type>import</conformance-type>\n" |
| 168 | " </module>\n" |
| 169 | " <module>\n" |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 170 | " <name>yang</name>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 171 | " <revision>2017-02-20</revision>\n" |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 172 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 173 | " <conformance-type>implement</conformance-type>\n" |
| 174 | " </module>\n" |
| 175 | " <module>\n" |
| 176 | " <name>ietf-inet-types</name>\n" |
| 177 | " <revision>2013-07-15</revision>\n" |
| 178 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n" |
| 179 | " <conformance-type>import</conformance-type>\n" |
| 180 | " </module>\n" |
| 181 | " <module>\n" |
| 182 | " <name>ietf-yang-types</name>\n" |
| 183 | " <revision>2013-07-15</revision>\n" |
| 184 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n" |
| 185 | " <conformance-type>import</conformance-type>\n" |
| 186 | " </module>\n" |
| 187 | " <module>\n" |
| 188 | " <name>ietf-yang-library</name>\n" |
Radek Krejci | d972391 | 2016-09-16 17:06:06 +0200 | [diff] [blame] | 189 | " <revision>2016-06-21</revision>\n" |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 190 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n" |
| 191 | " <conformance-type>implement</conformance-type>\n" |
| 192 | " </module>\n" |
| 193 | " <module>\n" |
| 194 | " <name>b</name>\n" |
| 195 | " <revision>2015-01-01</revision>\n" |
| 196 | " <schema>file://"SCHEMA_FOLDER_YIN"/b@2015-01-01.yin</schema>\n" |
| 197 | " <namespace>urn:example:b</namespace>\n" |
| 198 | " <conformance-type>implement</conformance-type>\n" |
| 199 | " </module>\n" |
| 200 | " <module>\n" |
| 201 | " <name>c</name>\n" |
| 202 | " <revision>2015-03-03</revision>\n" |
| 203 | " <schema>file://"SCHEMA_FOLDER_YIN"/c@2015-03-03.yin</schema>\n" |
| 204 | " <namespace>urn:example:c</namespace>\n" |
| 205 | " <conformance-type>import</conformance-type>\n" |
| 206 | " </module>\n" |
| 207 | " <module>\n" |
| 208 | " <name>a</name>\n" |
| 209 | " <revision>2015-01-01</revision>\n" |
| 210 | " <schema>file://"SCHEMA_FOLDER_YIN"/a.yin</schema>\n" |
| 211 | " <namespace>urn:example:a</namespace>\n" |
| 212 | " <feature>foo</feature>\n" |
| 213 | " <conformance-type>implement</conformance-type>\n" |
| 214 | " </module>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 215 | " <module-set-id>9</module-set-id>\n" |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 216 | "</modules-state>\n"; |
| 217 | |
| 218 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YIN); |
| 219 | |
| 220 | /* loads a.yin (impl), b@2015-01-01.yin (impl by augment) and c@2015-03-03.yin (imp) */ |
| 221 | assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YIN"/a.yin", LYS_IN_YIN)), NULL); |
| 222 | assert_int_equal(lys_features_enable(a, "foo"), 0); |
| 223 | |
| 224 | /* get yang-library data */ |
| 225 | info = ly_ctx_info(ctx); |
| 226 | assert_ptr_not_equal(info, NULL); |
| 227 | |
| 228 | lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT); |
| 229 | lyd_free_withsiblings(info); |
| 230 | assert_string_equal(data, template); |
| 231 | free(data); |
| 232 | } |
| 233 | |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 234 | static void |
| 235 | test_implemented_info_yang(void **state) |
| 236 | { |
| 237 | struct ly_ctx *ctx = *state; |
| 238 | struct lyd_node *info; |
| 239 | const struct lys_module *a; |
| 240 | char *data; |
| 241 | const char *template = "<modules-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-library\">\n" |
| 242 | " <module>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 243 | " <name>ietf-yang-metadata</name>\n" |
| 244 | " <revision>2016-08-05</revision>\n" |
| 245 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-metadata</namespace>\n" |
| 246 | " <conformance-type>import</conformance-type>\n" |
| 247 | " </module>\n" |
| 248 | " <module>\n" |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 249 | " <name>yang</name>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 250 | " <revision>2017-02-20</revision>\n" |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 251 | " <namespace>urn:ietf:params:xml:ns:yang:1</namespace>\n" |
| 252 | " <conformance-type>implement</conformance-type>\n" |
| 253 | " </module>\n" |
| 254 | " <module>\n" |
| 255 | " <name>ietf-inet-types</name>\n" |
| 256 | " <revision>2013-07-15</revision>\n" |
| 257 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-inet-types</namespace>\n" |
| 258 | " <conformance-type>import</conformance-type>\n" |
| 259 | " </module>\n" |
| 260 | " <module>\n" |
| 261 | " <name>ietf-yang-types</name>\n" |
| 262 | " <revision>2013-07-15</revision>\n" |
| 263 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-types</namespace>\n" |
| 264 | " <conformance-type>import</conformance-type>\n" |
| 265 | " </module>\n" |
| 266 | " <module>\n" |
| 267 | " <name>ietf-yang-library</name>\n" |
| 268 | " <revision>2016-06-21</revision>\n" |
| 269 | " <namespace>urn:ietf:params:xml:ns:yang:ietf-yang-library</namespace>\n" |
| 270 | " <conformance-type>implement</conformance-type>\n" |
| 271 | " </module>\n" |
| 272 | " <module>\n" |
| 273 | " <name>b</name>\n" |
| 274 | " <revision>2015-01-01</revision>\n" |
| 275 | " <schema>file://"SCHEMA_FOLDER_YANG"/b@2015-01-01.yang</schema>\n" |
| 276 | " <namespace>urn:example:b</namespace>\n" |
| 277 | " <conformance-type>implement</conformance-type>\n" |
| 278 | " </module>\n" |
| 279 | " <module>\n" |
| 280 | " <name>c</name>\n" |
| 281 | " <revision>2015-03-03</revision>\n" |
| 282 | " <schema>file://"SCHEMA_FOLDER_YANG"/c@2015-03-03.yang</schema>\n" |
| 283 | " <namespace>urn:example:c</namespace>\n" |
| 284 | " <conformance-type>import</conformance-type>\n" |
| 285 | " </module>\n" |
| 286 | " <module>\n" |
| 287 | " <name>a</name>\n" |
| 288 | " <revision>2015-01-01</revision>\n" |
| 289 | " <schema>file://"SCHEMA_FOLDER_YANG"/a.yang</schema>\n" |
| 290 | " <namespace>urn:example:a</namespace>\n" |
| 291 | " <feature>foo</feature>\n" |
| 292 | " <conformance-type>implement</conformance-type>\n" |
| 293 | " </module>\n" |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 294 | " <module-set-id>9</module-set-id>\n" |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 295 | "</modules-state>\n"; |
| 296 | |
| 297 | ly_ctx_set_searchdir(ctx, SCHEMA_FOLDER_YANG); |
| 298 | |
| 299 | /* loads a.yang (impl), b@2015-01-01.yang (impl by augment) and c@2015-03-03.yang (imp) */ |
| 300 | assert_ptr_not_equal((a = lys_parse_path(ctx, SCHEMA_FOLDER_YANG"/a.yang", LYS_IN_YANG)), NULL); |
| 301 | assert_int_equal(lys_features_enable(a, "foo"), 0); |
| 302 | |
| 303 | /* get yang-library data */ |
| 304 | info = ly_ctx_info(ctx); |
| 305 | assert_ptr_not_equal(info, NULL); |
| 306 | |
| 307 | lyd_print_mem(&data, info, LYD_XML, LYP_FORMAT); |
| 308 | lyd_free_withsiblings(info); |
| 309 | assert_string_equal(data, template); |
| 310 | free(data); |
| 311 | } |
| 312 | |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 313 | int |
| 314 | main(void) |
| 315 | { |
| 316 | const struct CMUnitTest cmut[] = { |
| 317 | cmocka_unit_test_setup_teardown(test_implemented1_yin, setup_ctx, teardown_ctx), |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 318 | cmocka_unit_test_setup_teardown(test_implemented1_yang, setup_ctx, teardown_ctx), |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 319 | cmocka_unit_test_setup_teardown(test_implemented2_yin, setup_ctx, teardown_ctx), |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 320 | cmocka_unit_test_setup_teardown(test_implemented2_yang, setup_ctx, teardown_ctx), |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 321 | cmocka_unit_test_setup_teardown(test_implemented_info_yin, setup_ctx, teardown_ctx), |
Pavol Vican | 5c186c2 | 2016-09-27 10:46:14 +0200 | [diff] [blame] | 322 | cmocka_unit_test_setup_teardown(test_implemented_info_yang, setup_ctx, teardown_ctx), |
Radek Krejci | 1a4a1c8 | 2016-09-14 16:07:25 +0200 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | return cmocka_run_group_tests(cmut, NULL, NULL); |
| 326 | } |