blob: 7efa2bd32f9fb915934619e1ffd2af0c1487ff7e [file] [log] [blame]
Radek Krejcib9726b22019-02-22 16:35:37 +01001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from common.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#include "../../src/common.c"
16#include "../../src/log.c"
17#include "../../src/set.c"
18#include "../../src/parser_yang.c"
19#include "../../src/tree_schema.c"
20#include "../../src/tree_schema_compile.c"
21#include "../../src/tree_schema_free.c"
22#include "../../src/tree_schema_helpers.c"
23#include "../../src/hash_table.c"
24#include "../../src/xpath.c"
25#include "../../src/context.c"
26
27#include <stdarg.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include "libyang.h"
34
35#define BUFSIZE 1024
36char logbuf[BUFSIZE] = {0};
37int store = -1; /* negative for infinite logging, positive for limited logging */
38
39/* set to 0 to printing error messages to stderr instead of checking them in code */
40#define ENABLE_LOGGER_CHECKING 1
41
42#if ENABLE_LOGGER_CHECKING
43static void
44logger(LY_LOG_LEVEL level, const char *msg, const char *path)
45{
46 (void) level; /* unused */
47 if (store) {
48 if (path && path[0]) {
49 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
50 } else {
51 strncpy(logbuf, msg, BUFSIZE - 1);
52 }
53 if (store > 0) {
54 --store;
55 }
56 }
57}
58#endif
59
60static int
61logger_setup(void **state)
62{
63 (void) state; /* unused */
64
65 ly_set_log_clb(logger, 0);
66
67 return 0;
68}
69
70static int
71logger_teardown(void **state)
72{
73 (void) state; /* unused */
74#if ENABLE_LOGGER_CHECKING
75 if (*state) {
76 fprintf(stderr, "%s\n", logbuf);
77 }
78#endif
79 return 0;
80}
81
82void
83logbuf_clean(void)
84{
85 logbuf[0] = '\0';
86}
87
88#if ENABLE_LOGGER_CHECKING
89# define logbuf_assert(str) assert_string_equal(logbuf, str)
90#else
91# define logbuf_assert(str)
92#endif
93
94static void
95test_getnext(void **state)
96{
97 *state = test_getnext;
98
99 struct ly_ctx *ctx;
100 struct lys_module *mod;
101 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx));
102
103 assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;"
104 "container a { container one {presence test;} leaf two {type string;} leaf-list three {type string;}"
105 " list four {config false;} choice x { leaf five {type string;} case y {leaf six {type string;}}}"
106 " anyxml seven; action eight {input {leaf eight-input {type string;}} output {leaf eight-output {type string;}}}"
107 " notification nine {leaf nine-data {type string;}}}"
108 "leaf b {type string;} leaf-list c {type string;} list d {config false;}"
109 "choice x { leaf e {type string;} case y {leaf f {type string;}}} anyxml g;"
110 "rpc h {input {leaf h-input {type string;}} output {leaf h-output {type string;}}}"
111 "notification i {leaf i-data {type string;}}}", LYS_IN_YANG));
112
113 *state = NULL;
114 ly_ctx_destroy(ctx, NULL);
115}
116
117int main(void)
118{
119 const struct CMUnitTest tests[] = {
120 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
121 };
122
123 return cmocka_run_group_tests(tests, NULL, NULL);
124}