blob: 5f30a9dd6450ea581684de5027063a02fc7b24ee [file] [log] [blame]
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 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 Krejci86d106e2018-10-18 09:53:19 +020015#include "../../src/tree_schema.c"
16#include "../../src/parser_yang.c"
17
Radek Krejcidd4e8d42018-10-16 14:55:43 +020018#include <stdarg.h>
19#include <stddef.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
23#include <stdio.h>
24#include <string.h>
25
26#include "libyang.h"
Radek Krejcidd4e8d42018-10-16 14:55:43 +020027
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34#if ENABLE_LOGGER_CHECKING
35static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
39
40 if (path) {
41 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45}
46#endif
47
48static int
49logger_setup(void **state)
50{
51 (void) state; /* unused */
52#if ENABLE_LOGGER_CHECKING
53 ly_set_log_clb(logger, 1);
54#endif
55 return 0;
56}
57
58void
59logbuf_clean(void)
60{
61 logbuf[0] = '\0';
62}
63
64#if ENABLE_LOGGER_CHECKING
65# define logbuf_assert(str) assert_string_equal(logbuf, str)
66#else
67# define logbuf_assert(str)
68#endif
69
70static void
71test_module(void **state)
72{
73 (void) state; /* unused */
74
75 const char *str;
76 struct ly_ctx *ctx;
77 struct lys_module mod = {0};
78
79 str = "module test {namespace urn:test; prefix t;"
80 "feature f1;}";
81 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
82
83 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, NULL));
84 logbuf_assert("Invalid argument sc (lys_compile()).");
85 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, &mod.compiled));
86 logbuf_assert("Invalid argument sp (lys_compile()).");
87 assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
88 assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled));
89 assert_non_null(mod.compiled);
90 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
91 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
92
Radek Krejci86d106e2018-10-18 09:53:19 +020093 lysc_module_free(mod.compiled, NULL);
Radek Krejcidd4e8d42018-10-16 14:55:43 +020094
95 assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, LYSC_OPT_FREE_SP, &mod.compiled));
96 assert_non_null(mod.compiled);
97 assert_string_equal("test", mod.compiled->name);
98 assert_string_equal("urn:test", mod.compiled->ns);
99
Radek Krejci86d106e2018-10-18 09:53:19 +0200100 lysc_module_free(mod.compiled, NULL);
101 mod.compiled = NULL;
102
103 str = "submodule test {belongs-to xxx {prefix x;}}";
104 assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
105 assert_int_equal(LY_EINVAL, lys_compile(mod.parsed, 0, &mod.compiled));
106 logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
107 assert_null(mod.compiled);
108
109 lysp_module_free(mod.parsed);
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200110 ly_ctx_destroy(ctx, NULL);
111}
112
113
114int main(void)
115{
116 const struct CMUnitTest tests[] = {
117 cmocka_unit_test_setup(test_module, logger_setup),
118 };
119
120 return cmocka_run_group_tests(tests, NULL, NULL);
121}