Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_printer_xml.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from printer_yang.c |
| 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 | |
| 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 | |
| 23 | #include "../../src/context.h" |
| 24 | #include "../../src/printer_data.h" |
| 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 | const char *schema_a = "module defs {namespace urn:tests:defs;prefix d;yang-version 1.1;" |
| 61 | "identity crypto-alg; identity interface-type; identity ethernet {base interface-type;} identity fast-ethernet {base ethernet;}}"; |
| 62 | const char *schema_b = "module types {namespace urn:tests:types;prefix t;yang-version 1.1; import defs {prefix defs;}" |
| 63 | "feature f; identity gigabit-ethernet { base defs:ethernet;}" |
| 64 | "container cont {leaf leaftarget {type empty;}" |
| 65 | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 66 | "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 67 | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
| 68 | "list list {key id; leaf id {type string;} leaf value {type string;} leaf-list targets {type string;}}" |
| 69 | "list list2 {key \"id value\"; leaf id {type string;} leaf value {type string;}}" |
| 70 | "list list_inst {key id; leaf id {type instance-identifier {require-instance true;}} leaf value {type string;}}" |
| 71 | "list list_ident {key id; leaf id {type identityref {base defs:interface-type;}} leaf value {type string;}}" |
| 72 | "leaf-list leaflisttarget {type string;}" |
| 73 | "leaf binary {type binary {length 5 {error-message \"This base64 value must be of length 5.\";}}}" |
| 74 | "leaf binary-norestr {type binary;}" |
| 75 | "leaf int8 {type int8 {range 10..20;}}" |
| 76 | "leaf uint8 {type uint8 {range 150..200;}}" |
| 77 | "leaf int16 {type int16 {range -20..-10;}}" |
| 78 | "leaf uint16 {type uint16 {range 150..200;}}" |
| 79 | "leaf int32 {type int32;}" |
| 80 | "leaf uint32 {type uint32;}" |
| 81 | "leaf int64 {type int64;}" |
| 82 | "leaf uint64 {type uint64;}" |
| 83 | "leaf bits {type bits {bit zero; bit one {if-feature f;} bit two;}}" |
| 84 | "leaf enums {type enumeration {enum white; enum yellow {if-feature f;}}}" |
| 85 | "leaf dec64 {type decimal64 {fraction-digits 1; range 1.5..10;}}" |
| 86 | "leaf dec64-norestr {type decimal64 {fraction-digits 18;}}" |
| 87 | "leaf str {type string {length 8..10; pattern '[a-z ]*';}}" |
| 88 | "leaf str-norestr {type string;}" |
| 89 | "leaf bool {type boolean;}" |
| 90 | "leaf empty {type empty;}" |
| 91 | "leaf ident {type identityref {base defs:interface-type;}}" |
| 92 | "leaf inst {type instance-identifier {require-instance true;}}" |
| 93 | "leaf inst-noreq {type instance-identifier {require-instance false;}}" |
| 94 | "leaf lref {type leafref {path /leaflisttarget; require-instance true;}}" |
| 95 | "leaf lref2 {type leafref {path \"../list[id = current()/../str-norestr]/targets\"; require-instance true;}}" |
| 96 | "leaf un1 {type union {" |
| 97 | "type leafref {path /int8; require-instance true;}" |
| 98 | "type union { type identityref {base defs:interface-type;} type instance-identifier {require-instance true;} }" |
| 99 | "type string {length 1..20;}}}" |
| 100 | "anydata any;" |
| 101 | "rpc sum {input {leaf x {type uint8;} leaf y {type uint8;}} output {leaf result {type uint16;}}}}"; |
| 102 | |
| 103 | s = calloc(1, sizeof *s); |
| 104 | assert_non_null(s); |
| 105 | |
| 106 | #if ENABLE_LOGGER_CHECKING |
| 107 | ly_set_log_clb(logger, 1); |
| 108 | #endif |
| 109 | |
| 110 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &s->ctx)); |
| 111 | assert_non_null(lys_parse_mem(s->ctx, schema_a, LYS_IN_YANG)); |
| 112 | assert_non_null(lys_parse_mem(s->ctx, schema_b, LYS_IN_YANG)); |
| 113 | |
| 114 | *state = s; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | teardown(void **state) |
| 121 | { |
| 122 | struct state_s *s = (struct state_s*)(*state); |
| 123 | |
| 124 | #if ENABLE_LOGGER_CHECKING |
| 125 | if (s->func) { |
| 126 | fprintf(stderr, "%s\n", logbuf); |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | ly_ctx_destroy(s->ctx, NULL); |
| 131 | free(s); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | logbuf_clean(void) |
| 138 | { |
| 139 | logbuf[0] = '\0'; |
| 140 | } |
| 141 | |
| 142 | #if ENABLE_LOGGER_CHECKING |
| 143 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 144 | #else |
| 145 | # define logbuf_assert(str) |
| 146 | #endif |
| 147 | |
| 148 | static void |
| 149 | test_leaf(void **state) |
| 150 | { |
| 151 | struct state_s *s = (struct state_s*)(*state); |
| 152 | struct lyd_node *tree; |
| 153 | const char *data; |
| 154 | const char *result; |
| 155 | char *printed; |
| 156 | ssize_t len; |
| 157 | |
| 158 | s->func = test_leaf; |
| 159 | |
| 160 | data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n </int8>"; |
| 161 | result = "<int8 xmlns=\"urn:tests:types\">15</int8>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 162 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 163 | assert_true((len = lyd_print_mem(&printed, tree, LYD_XML, 0)) >= 0); |
| 164 | assert_int_equal(len, strlen(printed)); |
| 165 | assert_string_equal(printed, result); |
| 166 | free(printed); |
| 167 | lyd_free_all(tree); |
| 168 | |
| 169 | s->func = NULL; |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | test_anydata(void **state) |
| 174 | { |
| 175 | struct state_s *s = (struct state_s*)(*state); |
| 176 | struct lyd_node *tree; |
| 177 | const char *data; |
| 178 | char *printed; |
| 179 | ssize_t len; |
| 180 | |
| 181 | s->func = test_anydata; |
| 182 | |
| 183 | data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 184 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 185 | assert_true((len = lyd_print_mem(&printed, tree, LYD_XML, 0)) >= 0); |
| 186 | assert_int_equal(len, strlen(printed)); |
| 187 | assert_string_equal(printed, data); |
| 188 | free(printed); |
| 189 | lyd_free_all(tree); |
| 190 | |
| 191 | data = "<any xmlns=\"urn:tests:types\"/>"; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 192 | assert_non_null(tree = lyd_parse_mem(s->ctx, data, LYD_XML, LYD_VALOPT_DATA_ONLY)); |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 193 | assert_true((len = lyd_print_mem(&printed, tree, LYD_XML, 0)) >= 0); |
| 194 | assert_int_equal(len, strlen(printed)); |
| 195 | assert_string_equal(printed, data); |
| 196 | free(printed); |
| 197 | lyd_free_all(tree); |
| 198 | |
| 199 | s->func = NULL; |
| 200 | } |
| 201 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 202 | #if 0 |
| 203 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 204 | static void |
| 205 | test_rpc(void **state) |
| 206 | { |
| 207 | struct state_s *s = (struct state_s*)(*state); |
| 208 | struct lyd_node *tree1; |
| 209 | struct lyd_node *tree2; |
| 210 | const struct lyd_node **trees; |
| 211 | const char *request; |
| 212 | const char *reply, *result; |
| 213 | char *printed; |
| 214 | ssize_t len; |
| 215 | |
| 216 | s->func = test_rpc; |
| 217 | |
| 218 | request = "<sum xmlns=\"urn:tests:types\"><x>10</x><y>20</y></sum>"; |
| 219 | reply = "<result xmlns=\"urn:tests:types\">30</result>"; |
| 220 | result = "<sum xmlns=\"urn:tests:types\"><result>30</result></sum>"; |
| 221 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
| 222 | assert_true((len = lyd_print_mem(&printed, tree1, LYD_XML, 0)) >= 0); |
| 223 | assert_int_equal(len, strlen(printed)); |
| 224 | assert_string_equal(printed, request); |
| 225 | free(printed); |
| 226 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 227 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
| 228 | assert_true((len = lyd_print_mem(&printed, tree2, LYD_XML, 0)) >= 0); |
| 229 | assert_int_equal(len, strlen(printed)); |
| 230 | assert_string_equal(printed, result); |
| 231 | free(printed); |
| 232 | lyd_trees_free(trees, 0); |
| 233 | lyd_free_all(tree1); |
| 234 | lyd_free_all(tree2); |
| 235 | |
| 236 | /* no arguments */ |
| 237 | request = "<sum xmlns=\"urn:tests:types\"/>"; |
| 238 | reply = ""; |
| 239 | result = "<sum xmlns=\"urn:tests:types\"/>"; |
| 240 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
| 241 | assert_true((len = lyd_print_mem(&printed, tree1, LYD_XML, 0)) >= 0); |
| 242 | assert_int_equal(len, strlen(printed)); |
| 243 | assert_string_equal(printed, request); |
| 244 | free(printed); |
| 245 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 246 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
| 247 | assert_true((len = lyd_print_mem(&printed, tree2, LYD_XML, 0)) >= 0); |
| 248 | assert_int_equal(len, strlen(printed)); |
| 249 | assert_string_equal(printed, result); |
| 250 | free(printed); |
| 251 | lyd_trees_free(trees, 0); |
| 252 | lyd_free_all(tree1); |
| 253 | lyd_free_all(tree2); |
| 254 | |
| 255 | /* action |
| 256 | * "container cont {leaf leaftarget {type empty;}" |
| 257 | "list listtarget {key id; max-elements 5;leaf id {type uint8;} leaf value {type string;}" |
| 258 | "action test {input {leaf a {type string;}} output {leaf b {type string;}}}}" |
| 259 | "leaf-list leaflisttarget {type uint8; max-elements 5;}}" |
| 260 | */ |
| 261 | request = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><a>test</a></test></listtarget></cont>"; |
| 262 | reply = "<b xmlns=\"urn:tests:types\">test-reply</b>"; |
| 263 | result = "<cont xmlns=\"urn:tests:types\"><listtarget><id>10</id><test><b>test-reply</b></test></listtarget></cont>";; |
| 264 | assert_non_null(tree1 = lyd_parse_mem(s->ctx, request, LYD_XML, LYD_OPT_RPC, NULL)); |
| 265 | assert_true((len = lyd_print_mem(&printed, tree1, LYD_XML, 0)) >= 0); |
| 266 | assert_int_equal(len, strlen(printed)); |
| 267 | assert_string_equal(printed, request); |
| 268 | free(printed); |
| 269 | assert_non_null(trees = lyd_trees_new(1, tree1)); |
| 270 | assert_non_null(tree2 = lyd_parse_mem(s->ctx, reply, LYD_XML, LYD_OPT_RPCREPLY, trees)); |
| 271 | assert_true((len = lyd_print_mem(&printed, tree2, LYD_XML, 0)) >= 0); |
| 272 | assert_int_equal(len, strlen(printed)); |
| 273 | assert_string_equal(printed, result); |
| 274 | free(printed); |
| 275 | lyd_trees_free(trees, 0); |
| 276 | lyd_free_all(tree1); |
| 277 | lyd_free_all(tree2); |
| 278 | |
| 279 | s->func = NULL; |
| 280 | } |
| 281 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 282 | #endif |
| 283 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 284 | int main(void) |
| 285 | { |
| 286 | const struct CMUnitTest tests[] = { |
| 287 | cmocka_unit_test_setup_teardown(test_leaf, setup, teardown), |
| 288 | cmocka_unit_test_setup_teardown(test_anydata, setup, teardown), |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 292 | } |