Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_lyb.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief Cmocka tests for LYB binary data format. |
| 5 | * |
| 6 | * Copyright (c) 2020 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 17 | #include "hash_table.h" |
| 18 | #include "libyang.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 19 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 20 | #define CHECK_PARSE_LYD(INPUT, MODEL) \ |
| 21 | CHECK_PARSE_LYD_PARAM(INPUT, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0,LY_SUCCESS, MODEL) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 22 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 23 | #define CHECK_LYD_STRING(MODEL, TEXT) \ |
| 24 | CHECK_LYD_STRING_PARAM(MODEL, TEXT, LYD_XML, LYD_PRINT_WITHSIBLINGS | LYD_PRINT_SHRINK) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 25 | |
| 26 | static int |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 27 | setup(void **state) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 28 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 29 | UTEST_SETUP; |
| 30 | assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | test_ietf_interfaces(void **state) |
| 37 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 38 | const char *data_xml = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 39 | "<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">\n" |
| 40 | " <interface>\n" |
| 41 | " <name>eth0</name>\n" |
| 42 | " <description>Ethernet 0</description>\n" |
| 43 | " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n" |
| 44 | " <enabled>true</enabled>\n" |
| 45 | " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n" |
| 46 | " <enabled>true</enabled>\n" |
| 47 | " <mtu>1500</mtu>\n" |
| 48 | " <address>\n" |
| 49 | " <ip>192.168.2.100</ip>\n" |
| 50 | " <prefix-length>24</prefix-length>\n" |
| 51 | " </address>\n" |
| 52 | " </ipv4>\n" |
| 53 | " </interface>\n" |
| 54 | " <interface>\n" |
| 55 | " <name>eth1</name>\n" |
| 56 | " <description>Ethernet 1</description>\n" |
| 57 | " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n" |
| 58 | " <enabled>true</enabled>\n" |
| 59 | " <ipv4 xmlns=\"urn:ietf:params:xml:ns:yang:ietf-ip\">\n" |
| 60 | " <enabled>true</enabled>\n" |
| 61 | " <mtu>1500</mtu>\n" |
| 62 | " <address>\n" |
| 63 | " <ip>10.10.1.5</ip>\n" |
| 64 | " <prefix-length>16</prefix-length>\n" |
| 65 | " </address>\n" |
| 66 | " </ipv4>\n" |
| 67 | " </interface>\n" |
| 68 | " <interface>\n" |
| 69 | " <name>gigaeth0</name>\n" |
| 70 | " <description>GigabitEthernet 0</description>\n" |
| 71 | " <type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">ianaift:ethernetCsmacd</type>\n" |
| 72 | " <enabled>false</enabled>\n" |
| 73 | " </interface>\n" |
| 74 | "</interfaces>\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 75 | struct lyd_node *tree_1; |
| 76 | struct lyd_node *tree_2; |
| 77 | char *xml_out; /* tree_2 */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 78 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 79 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-ip", NULL, NULL)); |
| 80 | assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "iana-if-type", NULL, NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 81 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 82 | CHECK_PARSE_LYD(data_xml, tree_1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 83 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 84 | assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 85 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 86 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2)); |
| 87 | assert_non_null(tree_2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 88 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 89 | /* compare models */ |
| 90 | CHECK_LYD(tree_1, tree_2); |
| 91 | |
| 92 | /* clean */ |
| 93 | free(xml_out); |
| 94 | lyd_free_all(tree_1); |
| 95 | lyd_free_all(tree_2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void |
| 99 | test_origin(void **state) |
| 100 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 101 | const char *origin_yang = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 102 | "module test-origin {" |
| 103 | " namespace \"urn:test-origin\";" |
| 104 | " prefix to;" |
| 105 | " import ietf-origin {" |
| 106 | " prefix or;" |
| 107 | " }" |
| 108 | "" |
| 109 | " container cont {" |
| 110 | " leaf leaf1 {" |
| 111 | " type string;" |
| 112 | " }" |
| 113 | " leaf leaf2 {" |
| 114 | " type string;" |
| 115 | " }" |
| 116 | " leaf leaf3 {" |
| 117 | " type uint8;" |
| 118 | " }" |
| 119 | " }" |
| 120 | "}"; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 121 | const char *data_xml = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 122 | "<cont xmlns=\"urn:test-origin\">\n" |
| 123 | " <leaf1 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:default\">value1</leaf1>\n" |
| 124 | " <leaf2>value2</leaf2>\n" |
| 125 | " <leaf3 xmlns:or=\"urn:ietf:params:xml:ns:yang:ietf-origin\" or:origin=\"or:system\">125</leaf3>\n" |
| 126 | "</cont>\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 127 | struct lyd_node *tree_1; |
| 128 | struct lyd_node *tree_2; |
| 129 | char *xml_out; /* tree_2 */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 130 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 131 | UTEST_ADD_MODULE(origin_yang, LYS_IN_YANG, NULL, NULL); |
| 132 | assert_int_equal(LY_SUCCESS, lys_set_implemented(ly_ctx_get_module_latest(UTEST_LYCTX, "ietf-origin"), NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 133 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 134 | CHECK_PARSE_LYD(data_xml, tree_1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 135 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 136 | assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 137 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2)); |
| 139 | assert_non_null(tree_2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 140 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 141 | /* compare models */ |
| 142 | CHECK_LYD(tree_1, tree_2); |
| 143 | |
| 144 | /* clean */ |
| 145 | free(xml_out); |
| 146 | lyd_free_all(tree_1); |
| 147 | lyd_free_all(tree_2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void |
| 151 | test_statements(void **state) |
| 152 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 153 | const char *links_yang = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 154 | "module links {\n" |
| 155 | " yang-version 1.1;\n" |
| 156 | " namespace \"urn:module2\";\n" |
| 157 | " prefix mod2;\n" |
| 158 | "\n" |
| 159 | " identity just-another-identity;\n" |
| 160 | "\n" |
| 161 | " leaf one-leaf {\n" |
| 162 | " type string;\n" |
| 163 | " }\n" |
| 164 | "\n" |
| 165 | " list list-for-augment {\n" |
| 166 | " key keyleaf;\n" |
| 167 | "\n" |
| 168 | " leaf keyleaf {\n" |
| 169 | " type string;\n" |
| 170 | " }\n" |
| 171 | "\n" |
| 172 | " leaf just-leaf {\n" |
| 173 | " type int32;\n" |
| 174 | " }\n" |
| 175 | " }\n" |
| 176 | "\n" |
| 177 | " leaf rleaf {\n" |
| 178 | " type string;\n" |
| 179 | " }\n" |
| 180 | "\n" |
| 181 | " leaf-list llist {\n" |
| 182 | " type string;\n" |
| 183 | " min-elements 0;\n" |
| 184 | " max-elements 100;\n" |
| 185 | " ordered-by user;\n" |
| 186 | " }\n" |
| 187 | "\n" |
| 188 | " grouping rgroup {\n" |
| 189 | " leaf rg1 {\n" |
| 190 | " type string;\n" |
| 191 | " }\n" |
| 192 | "\n" |
| 193 | " leaf rg2 {\n" |
| 194 | " type string;\n" |
| 195 | " }\n" |
| 196 | " }\n" |
| 197 | "}\n"; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 198 | |
| 199 | const char *statements_yang = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 200 | "module statements {\n" |
| 201 | " namespace \"urn:module\";\n" |
| 202 | " prefix mod;\n" |
| 203 | " yang-version 1.1;\n" |
| 204 | "\n" |
| 205 | " import links {\n" |
| 206 | " prefix mod2;\n" |
| 207 | " }\n" |
| 208 | "\n" |
| 209 | " identity random-identity {\n" |
| 210 | " base \"mod2:just-another-identity\";\n" |
| 211 | " base \"another-identity\";\n" |
| 212 | " }\n" |
| 213 | "\n" |
| 214 | " identity another-identity {\n" |
| 215 | " base \"mod2:just-another-identity\";\n" |
| 216 | " }\n" |
| 217 | "\n" |
| 218 | " typedef percent {\n" |
| 219 | " type uint8 {\n" |
| 220 | " range \"0 .. 100\";\n" |
| 221 | " }\n" |
| 222 | " units percent;\n" |
| 223 | " }\n" |
| 224 | "\n" |
| 225 | " container ice-cream-shop {\n" |
| 226 | " container employees {\n" |
| 227 | " list employee {\n" |
| 228 | " config true;\n" |
| 229 | " key id;\n" |
| 230 | " unique name;\n" |
| 231 | " min-elements 0;\n" |
| 232 | " max-elements 100;\n" |
| 233 | "\n" |
| 234 | " leaf id {\n" |
| 235 | " type uint64;\n" |
| 236 | " mandatory true;\n" |
| 237 | " }\n" |
| 238 | "\n" |
| 239 | " leaf name {\n" |
| 240 | " type string;\n" |
| 241 | " }\n" |
| 242 | "\n" |
| 243 | " leaf age {\n" |
| 244 | " type uint32;\n" |
| 245 | " }\n" |
| 246 | " }\n" |
| 247 | " }\n" |
| 248 | " }\n" |
| 249 | "\n" |
| 250 | " container random {\n" |
| 251 | " choice switch {\n" |
| 252 | " case a {\n" |
| 253 | " leaf aleaf {\n" |
| 254 | " type string;\n" |
| 255 | " default aaa;\n" |
| 256 | " }\n" |
| 257 | " }\n" |
| 258 | "\n" |
| 259 | " case c {\n" |
| 260 | " leaf cleaf {\n" |
| 261 | " type string;\n" |
| 262 | " }\n" |
| 263 | " }\n" |
| 264 | " }\n" |
| 265 | "\n" |
| 266 | " anyxml xml-data;\n" |
| 267 | " anydata any-data;\n" |
| 268 | " leaf-list leaflist {\n" |
| 269 | " type string;\n" |
| 270 | " min-elements 0;\n" |
| 271 | " max-elements 20;\n" |
| 272 | " ordered-by system;\n" |
| 273 | " }\n" |
| 274 | "\n" |
| 275 | " grouping group {\n" |
| 276 | " leaf g1 {\n" |
| 277 | " mandatory false;\n" |
| 278 | " type percent;\n" |
| 279 | " }\n" |
| 280 | "\n" |
| 281 | " leaf g2 {\n" |
| 282 | " type string;\n" |
| 283 | " }\n" |
| 284 | " }\n" |
| 285 | "\n" |
| 286 | " uses group;\n" |
| 287 | " uses mod2:rgroup;\n" |
| 288 | "\n" |
| 289 | " leaf lref {\n" |
| 290 | " type leafref {\n" |
| 291 | " path \"/mod2:one-leaf\";\n" |
| 292 | " }\n" |
| 293 | " }\n" |
| 294 | "\n" |
| 295 | " leaf iref {\n" |
| 296 | " type identityref {\n" |
| 297 | " base \"mod2:just-another-identity\";\n" |
| 298 | " }\n" |
| 299 | " }\n" |
| 300 | " }\n" |
| 301 | "\n" |
| 302 | " augment \"/random\" {\n" |
| 303 | " leaf aug-leaf {\n" |
| 304 | " type string;\n" |
| 305 | " }\n" |
| 306 | " }\n" |
| 307 | "}\n"; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 308 | |
| 309 | const char *data_xml = |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 310 | "<ice-cream-shop xmlns=\"urn:module\">\n" |
| 311 | " <employees>\n" |
| 312 | " <employee>\n" |
| 313 | " <id>0</id>\n" |
| 314 | " <name>John Doe</name>\n" |
| 315 | " <age>28</age>\n" |
| 316 | " </employee>\n" |
| 317 | " <employee>\n" |
| 318 | " <id>1</id>\n" |
| 319 | " <name>Dohn Joe</name>\n" |
| 320 | " <age>20</age>\n" |
| 321 | " </employee>\n" |
| 322 | " </employees>\n" |
| 323 | "</ice-cream-shop>\n" |
| 324 | "<one-leaf xmlns=\"urn:module2\">reference leaf</one-leaf>\n" |
| 325 | "<random xmlns=\"urn:module\">\n" |
| 326 | " <aleaf>string</aleaf>\n" |
| 327 | " <xml-data><anyxml>data</anyxml></xml-data>\n" |
| 328 | " <any-data><data>any data</data></any-data>\n" |
| 329 | " <leaflist>l0</leaflist>\n" |
| 330 | " <leaflist>l1</leaflist>\n" |
| 331 | " <leaflist>l2</leaflist>\n" |
| 332 | " <g1>40</g1>\n" |
| 333 | " <g2>string</g2>\n" |
| 334 | " <aug-leaf>string</aug-leaf>\n" |
| 335 | " <rg1>string</rg1>\n" |
| 336 | " <rg2>string</rg2>\n" |
| 337 | " <lref>reference leaf</lref>\n" |
| 338 | " <iref>random-identity</iref>\n" |
| 339 | "</random>\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 340 | struct lyd_node *tree_1; |
| 341 | struct lyd_node *tree_2; |
| 342 | char *xml_out; /* tree_2 */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 343 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 344 | UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL); |
| 345 | UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 346 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 347 | CHECK_PARSE_LYD(data_xml, tree_1); |
| 348 | |
| 349 | assert_int_equal(lyd_print_mem(&xml_out, tree_1, LYD_LYB, LYD_PRINT_WITHSIBLINGS), 0); |
| 350 | |
| 351 | assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, xml_out, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree_2)); |
| 352 | assert_non_null(tree_2); |
| 353 | |
| 354 | /* compare models */ |
| 355 | CHECK_LYD(tree_1, tree_2); |
| 356 | |
| 357 | /* clean */ |
| 358 | free(xml_out); |
| 359 | lyd_free_all(tree_1); |
| 360 | lyd_free_all(tree_2); |
| 361 | } |
| 362 | |
| 363 | #if 0 |
| 364 | |
| 365 | static void |
| 366 | test_types(void **state) |
| 367 | { |
| 368 | struct state *st = (*state); |
| 369 | int ret; |
| 370 | |
| 371 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 372 | assert_non_null(ly_ctx_load_module(st->ctx, "types", NULL)); |
| 373 | |
| 374 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/types.xml", LYD_XML, LYD_OPT_CONFIG); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 375 | assert_ptr_not_equal(st->dt1, NULL); |
| 376 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 377 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 378 | assert_int_equal(ret, 0); |
| 379 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 380 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 381 | assert_ptr_not_equal(st->dt2, NULL); |
| 382 | |
| 383 | check_data_tree(st->dt1, st->dt2); |
| 384 | } |
| 385 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 386 | static void |
| 387 | test_annotations(void **state) |
| 388 | { |
| 389 | struct state *st = (*state); |
| 390 | int ret; |
| 391 | |
| 392 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 393 | assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 394 | |
| 395 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/annotations.xml", LYD_XML, LYD_OPT_CONFIG); |
| 396 | assert_ptr_not_equal(st->dt1, NULL); |
| 397 | |
| 398 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 399 | assert_int_equal(ret, 0); |
| 400 | |
| 401 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 402 | assert_ptr_not_equal(st->dt2, NULL); |
| 403 | |
| 404 | check_data_tree(st->dt1, st->dt2); |
| 405 | } |
| 406 | |
| 407 | static void |
| 408 | test_similar_annot_names(void **state) |
| 409 | { |
| 410 | struct state *st = (*state); |
| 411 | int ret; |
| 412 | |
| 413 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 414 | assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 415 | |
| 416 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/similar-annot-names.xml", LYD_XML, LYD_OPT_CONFIG); |
| 417 | assert_ptr_not_equal(st->dt1, NULL); |
| 418 | |
| 419 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 420 | assert_int_equal(ret, 0); |
| 421 | |
| 422 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 423 | assert_ptr_not_equal(st->dt2, NULL); |
| 424 | |
| 425 | check_data_tree(st->dt1, st->dt2); |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | test_many_child_annot(void **state) |
| 430 | { |
| 431 | struct state *st = (*state); |
| 432 | int ret; |
| 433 | |
| 434 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 435 | assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 436 | |
| 437 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/many-childs-annot.xml", LYD_XML, LYD_OPT_CONFIG); |
| 438 | assert_ptr_not_equal(st->dt1, NULL); |
| 439 | |
| 440 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 441 | assert_int_equal(ret, 0); |
| 442 | |
| 443 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 444 | assert_ptr_not_equal(st->dt2, NULL); |
| 445 | |
| 446 | check_data_tree(st->dt1, st->dt2); |
| 447 | } |
| 448 | |
| 449 | static void |
| 450 | test_union(void **state) |
| 451 | { |
| 452 | struct state *st = (*state); |
| 453 | int ret; |
| 454 | |
| 455 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 456 | assert_non_null(ly_ctx_load_module(st->ctx, "union", NULL)); |
| 457 | |
| 458 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union.xml", LYD_XML, LYD_OPT_CONFIG); |
| 459 | assert_ptr_not_equal(st->dt1, NULL); |
| 460 | |
| 461 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 462 | assert_int_equal(ret, 0); |
| 463 | |
| 464 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 465 | assert_ptr_not_equal(st->dt2, NULL); |
| 466 | |
| 467 | check_data_tree(st->dt1, st->dt2); |
| 468 | } |
| 469 | |
| 470 | static void |
| 471 | test_union2(void **state) |
| 472 | { |
| 473 | struct state *st = (*state); |
| 474 | int ret; |
| 475 | |
| 476 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 477 | assert_non_null(ly_ctx_load_module(st->ctx, "statements", NULL)); |
| 478 | |
| 479 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/union2.xml", LYD_XML, LYD_OPT_CONFIG); |
| 480 | assert_ptr_not_equal(st->dt1, NULL); |
| 481 | |
| 482 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 483 | assert_int_equal(ret, 0); |
| 484 | |
| 485 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 486 | assert_ptr_not_equal(st->dt2, NULL); |
| 487 | |
| 488 | check_data_tree(st->dt1, st->dt2); |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | test_collisions(void **state) |
| 493 | { |
| 494 | struct state *st = (*state); |
| 495 | int ret; |
| 496 | |
| 497 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 498 | assert_non_null(ly_ctx_load_module(st->ctx, "annotations", NULL)); |
| 499 | |
| 500 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/collisions.xml", LYD_XML, LYD_OPT_CONFIG); |
| 501 | assert_ptr_not_equal(st->dt1, NULL); |
| 502 | |
| 503 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 504 | assert_int_equal(ret, 0); |
| 505 | |
| 506 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 507 | assert_ptr_not_equal(st->dt2, NULL); |
| 508 | |
| 509 | check_data_tree(st->dt1, st->dt2); |
| 510 | } |
| 511 | |
| 512 | static void |
| 513 | test_anydata(void **state) |
| 514 | { |
| 515 | struct state *st = (*state); |
| 516 | const struct lys_module *mod; |
| 517 | int ret; |
| 518 | const char *test_anydata = |
| 519 | "module test-anydata {" |
| 520 | " namespace \"urn:test-anydata\";" |
| 521 | " prefix ya;" |
| 522 | "" |
| 523 | " container cont {" |
| 524 | " anydata ntf;" |
| 525 | " }" |
| 526 | "}"; |
| 527 | |
| 528 | assert_non_null(ly_ctx_load_module(st->ctx, "ietf-netconf-notifications", NULL)); |
| 529 | |
| 530 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL); |
| 531 | assert_ptr_not_equal(st->dt1, NULL); |
| 532 | |
| 533 | / *get notification in LYB format to set as anydata content * / |
| 534 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 535 | assert_int_equal(ret, 0); |
| 536 | |
| 537 | lyd_free_withsiblings(st->dt1); |
| 538 | st->dt1 = NULL; |
| 539 | |
| 540 | / *now comes the real test, test anydata * / |
| 541 | mod = lys_parse_mem(st->ctx, test_anydata, LYS_YANG); |
| 542 | assert_non_null(mod); |
| 543 | |
| 544 | st->dt1 = lyd_new(NULL, mod, "cont"); |
| 545 | assert_non_null(st->dt1); |
| 546 | |
| 547 | assert_non_null(lyd_new_anydata(st->dt1, NULL, "ntf", st->mem, LYD_ANYDATA_LYBD)); |
| 548 | st->mem = NULL; |
| 549 | |
| 550 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 551 | assert_int_equal(ret, 0); |
| 552 | |
| 553 | ret = lyd_validate(&st->dt1, LYD_OPT_CONFIG, NULL); |
| 554 | assert_int_equal(ret, 0); |
| 555 | |
| 556 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 557 | assert_ptr_not_equal(st->dt2, NULL); |
| 558 | |
| 559 | check_data_tree(st->dt1, st->dt2); |
| 560 | |
| 561 | /* and also test the embedded notification itself */ |
| 562 | free(st->mem); |
| 563 | ret = lyd_lyb_data_length(((struct lyd_node_anydata *)st->dt1->child)->value.mem); |
| 564 | st->mem = malloc(ret); |
| 565 | memcpy(st->mem, ((struct lyd_node_anydata *)st->dt1->child)->value.mem, ret); |
| 566 | |
| 567 | lyd_free_withsiblings(st->dt2); |
| 568 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_NOEXTDEPS, NULL); |
| 569 | assert_ptr_not_equal(st->dt2, NULL); |
| 570 | |
| 571 | /* parse the JSON again for this comparison */ |
| 572 | lyd_free_withsiblings(st->dt1); |
| 573 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/ietf-netconf-notifications.json", LYD_JSON, LYD_OPT_NOTIF | LYD_OPT_TRUSTED, NULL); |
| 574 | assert_ptr_not_equal(st->dt1, NULL); |
| 575 | |
| 576 | check_data_tree(st->dt1, st->dt2); |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | test_submodule_feature(void **state) |
| 581 | { |
| 582 | struct state *st = (*state); |
| 583 | const struct lys_module *mod; |
| 584 | int ret; |
| 585 | |
| 586 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 587 | mod = ly_ctx_load_module(st->ctx, "feature-submodule-main", NULL); |
| 588 | assert_non_null(mod); |
| 589 | assert_int_equal(lys_features_enable(mod, "test-submodule-feature"), 0); |
| 590 | |
| 591 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/test-submodule-feature.json", LYD_JSON, LYD_OPT_CONFIG); |
| 592 | assert_ptr_not_equal(st->dt1, NULL); |
| 593 | |
| 594 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 595 | assert_int_equal(ret, 0); |
| 596 | |
| 597 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 598 | assert_ptr_not_equal(st->dt2, NULL); |
| 599 | |
| 600 | check_data_tree(st->dt1, st->dt2); |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | test_coliding_augments(void **state) |
| 605 | { |
| 606 | struct state *st = (*state); |
| 607 | int ret; |
| 608 | |
| 609 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 610 | assert_non_null(ly_ctx_load_module(st->ctx, "augment-target", NULL)); |
| 611 | assert_non_null(ly_ctx_load_module(st->ctx, "augment0", NULL)); |
| 612 | assert_non_null(ly_ctx_load_module(st->ctx, "augment1", NULL)); |
| 613 | |
| 614 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/augment.xml", LYD_XML, LYD_OPT_CONFIG); |
| 615 | assert_ptr_not_equal(st->dt1, NULL); |
| 616 | |
| 617 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 618 | assert_int_equal(ret, 0); |
| 619 | |
| 620 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 621 | assert_ptr_not_equal(st->dt2, NULL); |
| 622 | |
| 623 | check_data_tree(st->dt1, st->dt2); |
| 624 | } |
| 625 | |
| 626 | static void |
| 627 | test_leafrefs(void **state) |
| 628 | { |
| 629 | struct state *st = (*state); |
| 630 | int ret; |
| 631 | |
| 632 | ly_ctx_set_searchdir(st->ctx, TESTS_DIR "/data/files"); |
| 633 | assert_non_null(ly_ctx_load_module(st->ctx, "leafrefs2", NULL)); |
| 634 | |
| 635 | st->dt1 = lyd_parse_path(st->ctx, TESTS_DIR "/data/files/leafrefs2.json", LYD_JSON, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 636 | assert_ptr_not_equal(st->dt1, NULL); |
| 637 | |
| 638 | ret = lyd_print_mem(&st->mem, st->dt1, LYD_LYB, LYP_WITHSIBLINGS); |
| 639 | assert_int_equal(ret, 0); |
| 640 | |
| 641 | st->dt2 = lyd_parse_mem(st->ctx, st->mem, LYD_LYB, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 642 | assert_ptr_not_equal(st->dt2, NULL); |
| 643 | |
| 644 | check_data_tree(st->dt1, st->dt2); |
| 645 | } |
| 646 | |
| 647 | #endif |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 648 | |
| 649 | int |
| 650 | main(void) |
| 651 | { |
| 652 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 653 | UTEST(test_ietf_interfaces, setup), |
| 654 | UTEST(test_origin, setup), |
| 655 | UTEST(test_statements, setup), |
| 656 | #if 0 |
| 657 | cmocka_unit_test_setup_teardown(test_types, setup_f, teardown_f), |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 658 | cmocka_unit_test_setup_teardown(test_annotations, setup_f, teardown_f), |
| 659 | cmocka_unit_test_setup_teardown(test_similar_annot_names, setup_f, teardown_f), |
| 660 | cmocka_unit_test_setup_teardown(test_many_child_annot, setup_f, teardown_f), |
| 661 | cmocka_unit_test_setup_teardown(test_union, setup_f, teardown_f), |
| 662 | cmocka_unit_test_setup_teardown(test_union2, setup_f, teardown_f), |
| 663 | cmocka_unit_test_setup_teardown(test_collisions, setup_f, teardown_f), |
| 664 | cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f), |
| 665 | cmocka_unit_test_setup_teardown(test_submodule_feature, setup_f, teardown_f), |
| 666 | cmocka_unit_test_setup_teardown(test_coliding_augments, setup_f, teardown_f), |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 667 | cmocka_unit_test_setup_teardown(test_leafrefs, setup_f, teardown_f), |
| 668 | #endif |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 669 | }; |
| 670 | |
| 671 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 672 | } |