blob: eaea51d7b0b2db174014601d7307db4eee96e440 [file] [log] [blame]
Michal Vaskocde73ac2019-11-14 16:10:27 +01001/*
2 * @file test_parser_xml.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_xml.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/tree_data_internal.h"
25
26#define BUFSIZE 1024
27char logbuf[BUFSIZE] = {0};
28int store = -1; /* negative for infinite logging, positive for limited logging */
29
30struct ly_ctx *ctx; /* context for tests */
31
32/* set to 0 to printing error messages to stderr instead of checking them in code */
33#define ENABLE_LOGGER_CHECKING 1
34
35#if ENABLE_LOGGER_CHECKING
36static void
37logger(LY_LOG_LEVEL level, const char *msg, const char *path)
38{
39 (void) level; /* unused */
40 if (store) {
41 if (path && path[0]) {
42 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46 if (store > 0) {
47 --store;
48 }
49 }
50}
51#endif
52
53static int
54setup(void **state)
55{
56 (void) state; /* unused */
57
58 const char *schema_a =
59 "module a {"
60 "namespace urn:tests:a;"
61 "prefix a;"
62 "yang-version 1.1;"
63
64 "container cont {"
65 "leaf a {"
66 "when \"../../c = 'val_c'\";"
67 "type string;"
68 "}"
69 "leaf b {"
70 "type string;"
71 "}"
72 "}"
73 "leaf c {"
74 "when \"/cont/b = 'val_b'\";"
75 "type string;"
76 "}"
77 "}";
78
79#if ENABLE_LOGGER_CHECKING
80 ly_set_log_clb(logger, 1);
81#endif
82
83 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
84 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
85
86 return 0;
87}
88
89static int
90teardown(void **state)
91{
92#if ENABLE_LOGGER_CHECKING
93 if (*state) {
94 fprintf(stderr, "%s\n", logbuf);
95 }
96#else
97 (void) state; /* unused */
98#endif
99
100 ly_ctx_destroy(ctx, NULL);
101 ctx = NULL;
102
103 return 0;
104}
105
106void
107logbuf_clean(void)
108{
109 logbuf[0] = '\0';
110}
111
112#if ENABLE_LOGGER_CHECKING
113# define logbuf_assert(str) assert_string_equal(logbuf, str)
114#else
115# define logbuf_assert(str)
116#endif
117
118static void
119test_when(void **state)
120{
121 *state = test_when;
122
123 const char *data;
124 struct lyd_node *tree;
125
126 data = "<c xmlns=\"urn:tests:a\">hey</c>";
127 assert_int_equal(LY_EVALID, lyd_parse_xml(ctx, data, 0, NULL, &tree));
128 assert_null(tree);
129 logbuf_assert("When condition \"/cont/b = 'val_b'\" not satisfied.");
130
131 data = "<cont xmlns=\"urn:tests:a\"><b>val_b</b></cont><c xmlns=\"urn:tests:a\">hey</c>";
132 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, NULL, &tree));
133 assert_non_null(tree);
134 assert_string_equal("c", tree->next->schema->name);
135 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
136 lyd_free_all(tree);
137
138 data = "<cont xmlns=\"urn:tests:a\"><a>val</a><b>val_b</b></cont><c xmlns=\"urn:tests:a\">val_c</c>";
139 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, NULL, &tree));
140 assert_non_null(tree);
141 assert_string_equal("a", lyd_node_children(tree)->schema->name);
142 assert_int_equal(LYD_WHEN_TRUE, lyd_node_children(tree)->flags);
143 assert_string_equal("c", tree->next->schema->name);
144 assert_int_equal(LYD_WHEN_TRUE, tree->next->flags);
145 lyd_free_all(tree);
146
147 *state = NULL;
148}
149
150int main(void)
151{
152 const struct CMUnitTest tests[] = {
153 cmocka_unit_test_setup_teardown(test_when, setup, teardown),
154 };
155
156 return cmocka_run_group_tests(tests, NULL, NULL);
157}