blob: 9b7e79fbf21bc1d8ad996cc9a36a46d3c3ba62b9 [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;"
59 "leaf foo { type string;}}";
60
61#if ENABLE_LOGGER_CHECKING
62 ly_set_log_clb(logger, 1);
63#endif
64
65 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
66 assert_non_null(lys_parse_mem(ctx, schema_a, LYS_IN_YANG));
67
68 return 0;
69}
70
71static int
72teardown(void **state)
73{
74#if ENABLE_LOGGER_CHECKING
75 if (*state) {
76 fprintf(stderr, "%s\n", logbuf);
77 }
78#else
79 (void) state; /* unused */
80#endif
81
82 ly_ctx_destroy(ctx, NULL);
83 ctx = NULL;
84
85 return 0;
86}
87
88void
89logbuf_clean(void)
90{
91 logbuf[0] = '\0';
92}
93
94#if ENABLE_LOGGER_CHECKING
95# define logbuf_assert(str) assert_string_equal(logbuf, str)
96#else
97# define logbuf_assert(str)
98#endif
99
Radek Krejci509e2592019-05-15 16:30:48 +0200100static void
101test_leaf(void **state)
102{
103 *state = test_leaf;
104
105 const char *data = "<foo xmlns=\"urn:tests:a\">foo value</foo>";
106 struct lyd_node *tree;
107 struct lyd_node_term *leaf;
108
109 assert_int_equal(LY_SUCCESS, lyd_parse_xml(ctx, data, 0, &tree));
110 assert_non_null(tree);
111 assert_int_equal(LYS_LEAF, tree->schema->nodetype);
112 assert_string_equal("foo", tree->schema->name);
113 leaf = (struct lyd_node_term*)tree;
114 assert_string_equal("foo value", leaf->value.canonized);
115
116 lyd_free_all(tree);
117 *state = NULL;
118}
119
120int main(void)
121{
122 const struct CMUnitTest tests[] = {
123 cmocka_unit_test_setup_teardown(test_leaf, setup, teardown),
124 };
125
126 return cmocka_run_group_tests(tests, NULL, NULL);
127}