blob: 66ad1b0a795de6882a3faf4729489c767908b824 [file] [log] [blame]
Radek Krejci509e2592019-05-15 16:30:48 +02001/*
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
Radek Krejci509e2592019-05-15 16:30:48 +020015#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 Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/context.h"
24#include "../../src/tree_data_internal.h"
Radek Krejci509e2592019-05-15 16:30:48 +020025
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 = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
Radek Krejciee4cab22019-07-17 17:07:47 +020059 "leaf foo { type string;}"
60 "anydata any {config false;} }";
Radek Krejci509e2592019-05-15 16:30:48 +020061
62#if ENABLE_LOGGER_CHECKING
63 ly_set_log_clb(logger, 1);
64#endif
65
66 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
67 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
68
69 return 0;
70}
71
72static int
73teardown(void **state)
74{
75#if ENABLE_LOGGER_CHECKING
76 if (*state) {
77 fprintf(stderr, "%s\n", logbuf);
78 }
79#else
80 (void) state; /* unused */
81#endif
82
83 ly_ctx_destroy(ctx, NULL);
84 ctx = NULL;
85
86 return 0;
87}
88
89void
90logbuf_clean(void)
91{
92 logbuf[0] = '\0';
93}
94
95#if ENABLE_LOGGER_CHECKING
96# define logbuf_assert(str) assert_string_equal(logbuf, str)
97#else
98# define logbuf_assert(str)
99#endif
100
Radek Krejci509e2592019-05-15 16:30:48 +0200101static void
102test_leaf(void **state)
103{
104 *state = test_leaf;
105
106 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
107 struct lyd_node *tree;
108 struct lyd_node_term *leaf;
109
110 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, &tree));
111 assert_non_null(tree);
112 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
113 assert_string_equal("foo", tree->schema->name);
114 leaf = (struct lyd_node_term*)tree;
115 assert_string_equal("foo value", leaf->value.canonized);
116
117 lyd_free_all(tree);
118 *state = NULL;
119}
120
Radek Krejciee4cab22019-07-17 17:07:47 +0200121static void
122test_anydata(void **state)
123{
124 *state = test_anydata;
125
126 const char *data = "<any xmlns=\"urn:tests:a\">"
127 "<element1><x:element2 x:attr2=\"test\" xmlns:x=\"urn:x\">x:data</x:element2></element1><element1a/>"
128 "</any>";
129 struct lyd_node *tree;
130 struct lyd_node_any *any;
131
132 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, &tree));
133 assert_non_null(tree);
134 assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
135 assert_string_equal("any", tree->schema->name);
136 any = (struct lyd_node_any*)tree;
137 assert_int_equal(LYD_ANYDATA_XML, any->value_type);
138 assert_string_equal("<element1><x:element2 x:attr2=\"test\" xmlns:x=\"urn:x\">x:data</x:element2></element1><element1a/>", any->value.xml);
139
140 lyd_free_all(tree);
141 *state = NULL;
142}
143
Radek Krejci509e2592019-05-15 16:30:48 +0200144int main(void)
145{
146 const struct CMUnitTest tests[] = {
147 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
Radek Krejciee4cab22019-07-17 17:07:47 +0200148 cmocka_unit_test_setup_teardown(test_anydata, setup, teardown),
Radek Krejci509e2592019-05-15 16:30:48 +0200149 };
150
151 return cmocka_run_group_tests(tests, NULL, NULL);
152}