blob: 291e51508421b909d019dda910c34232bc1d98a3 [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
15#define _BSD_SOURCE
16#define _DEFAULT_SOURCE
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "libyang.h"
26#include "../../src/parser_yang.c"
27#include "../../src/tree_schema.c"
28
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
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
41 if (path) {
42 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46}
47#endif
48
49static int
50logger_setup(void **state)
51{
52 (void) state; /* unused */
53#if ENABLE_LOGGER_CHECKING
54 ly_set_log_clb(logger, 1);
55#endif
56 return 0;
57}
58
59void
60logbuf_clean(void)
61{
62 logbuf[0] = '\0';
63}
64
65#if ENABLE_LOGGER_CHECKING
66# define logbuf_assert(str) assert_string_equal(logbuf, str)
67#else
68# define logbuf_assert(str)
69#endif
70
71static void
72test_module(void **state)
73{
74 (void) state; /* unused */
75
76 const char *str;
77 struct ly_ctx *ctx;
78 struct lys_module mod = {0};
79
80 str = "module test {namespace urn:test; prefix t;"
81 "feature f1;}";
82 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
83
84 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, NULL));
85 logbuf_assert("Invalid argument sc (lys_compile()).");
86 assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, &mod.compiled));
87 logbuf_assert("Invalid argument sp (lys_compile()).");
88 assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
89 assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled));
90 assert_non_null(mod.compiled);
91 assert_ptr_equal(mod.parsed->name, mod.compiled->name);
92 assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
93
94 lysc_module_free(mod.compiled);
95
96 assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, LYSC_OPT_FREE_SP, &mod.compiled));
97 assert_non_null(mod.compiled);
98 assert_string_equal("test", mod.compiled->name);
99 assert_string_equal("urn:test", mod.compiled->ns);
100
101 lysc_module_free(mod.compiled);
102 ly_ctx_destroy(ctx, NULL);
103}
104
105
106int main(void)
107{
108 const struct CMUnitTest tests[] = {
109 cmocka_unit_test_setup(test_module, logger_setup),
110 };
111
112 return cmocka_run_group_tests(tests, NULL, NULL);
113}