blob: 03f6db4b2471e98e78eb4efd2f341af267887717 [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
15#include "../../src/common.c"
16#include "../../src/set.c"
17#include "../../src/log.c"
18#include "../../src/hash_table.c"
19#include "../../src/xpath.c"
20#include "../../src/parser_yang.c"
21#include "../../src/context.c"
22#include "../../src/tree_schema_helpers.c"
23#include "../../src/tree_schema_free.c"
24#include "../../src/tree_schema_compile.c"
25#include "../../src/tree_schema.c"
26#include "../../src/plugins_types.c"
27#include "../../src/tree_data_free.c"
28#include "../../src/tree_data_helpers.c"
29#include "../../src/tree_data_hash.c"
30#include "../../src/printer.c"
31#include "../../src/xml.c"
32#include "../../src/parser_xml.c"
33
34#include <stdarg.h>
35#include <stddef.h>
36#include <setjmp.h>
37#include <cmocka.h>
38
39#include <stdio.h>
40#include <string.h>
41
42#include "libyang.h"
43
44#define BUFSIZE 1024
45char logbuf[BUFSIZE] = {0};
46int store = -1; /* negative for infinite logging, positive for limited logging */
47
48struct ly_ctx *ctx; /* context for tests */
49
50/* set to 0 to printing error messages to stderr instead of checking them in code */
51#define ENABLE_LOGGER_CHECKING 1
52
53#if ENABLE_LOGGER_CHECKING
54static void
55logger(LY_LOG_LEVEL level, const char *msg, const char *path)
56{
57 (void) level; /* unused */
58 if (store) {
59 if (path && path[0]) {
60 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
61 } else {
62 strncpy(logbuf, msg, BUFSIZE - 1);
63 }
64 if (store > 0) {
65 --store;
66 }
67 }
68}
69#endif
70
71static int
72setup(void **state)
73{
74 (void) state; /* unused */
75
76 const char *schema_a = "module a {namespace urn:tests:a;prefix a;yang-version 1.1;"
77 "leaf foo { type string;}}";
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
118#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
119 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
120 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
121 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
122 CLEANUP
123
124static void
125test_leaf(void **state)
126{
127 *state = test_leaf;
128
129 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
130 struct lyd_node *tree;
131 struct lyd_node_term *leaf;
132
133 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, &tree));
134 assert_non_null(tree);
135 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
136 assert_string_equal("foo", tree->schema->name);
137 leaf = (struct lyd_node_term*)tree;
138 assert_string_equal("foo value", leaf->value.canonized);
139
140 lyd_free_all(tree);
141 *state = NULL;
142}
143
144int main(void)
145{
146 const struct CMUnitTest tests[] = {
147 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
148 };
149
150 return cmocka_run_group_tests(tests, NULL, NULL);
151}