blob: 2736af95aa2a32c2d0be8c22220ab98aba400040 [file] [log] [blame]
Radek Krejci0935f412019-08-20 16:15:18 +02001/*
2 * @file test_nacm.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for NACM extensions support
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 Krejcib4ac5a92020-11-23 17:54:33 +010015#include "utests.h"
Radek Krejci0935f412019-08-20 16:15:18 +020016
Radek Krejci70593c12020-06-13 20:48:09 +020017#include "libyang.h"
18#include "tests/config.h"
Radek Krejci0935f412019-08-20 16:15:18 +020019
20#define BUFSIZE 1024
21char logbuf[BUFSIZE] = {0};
22int store = -1; /* negative for infinite logging, positive for limited logging */
23
24struct state_s {
25 void *func;
26 struct ly_ctx *ctx;
27};
28
29/* set to 0 to printing error messages to stderr instead of checking them in code */
30#define ENABLE_LOGGER_CHECKING 1
31
32#if ENABLE_LOGGER_CHECKING
33static void
34logger(LY_LOG_LEVEL level, const char *msg, const char *path)
35{
36 (void) level; /* unused */
37 if (store) {
38 if (path && path[0]) {
39 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
40 } else {
41 strncpy(logbuf, msg, BUFSIZE - 1);
42 }
43 if (store > 0) {
44 --store;
45 }
46 }
47}
Radek Krejcib4ac5a92020-11-23 17:54:33 +010048
Radek Krejci0935f412019-08-20 16:15:18 +020049#endif
50
51static int
52setup(void **state)
53{
54 struct state_s *s;
55
56 s = calloc(1, sizeof *s);
57 assert_non_null(s);
58
59#if ENABLE_LOGGER_CHECKING
60 ly_set_log_clb(logger, 1);
61#endif
62
63 assert_int_equal(LY_SUCCESS, ly_ctx_new(TESTS_DIR_MODULES_YANG, 0, &s->ctx));
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010064 assert_non_null(ly_ctx_load_module(s->ctx, "ietf-netconf-acm", "2018-02-14", NULL));
Radek Krejci0935f412019-08-20 16:15:18 +020065
66 *state = s;
67
68 return 0;
69}
70
71static int
72teardown(void **state)
73{
Radek Krejcib4ac5a92020-11-23 17:54:33 +010074 struct state_s *s = (struct state_s *)(*state);
Radek Krejci0935f412019-08-20 16:15:18 +020075
76#if ENABLE_LOGGER_CHECKING
77 if (s->func) {
78 fprintf(stderr, "%s\n", logbuf);
79 }
80#endif
81
82 ly_ctx_destroy(s->ctx, NULL);
83 free(s);
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
100static void
101test_deny_all(void **state)
102{
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100103 struct state_s *s = (struct state_s *)(*state);
104
Radek Krejci0935f412019-08-20 16:15:18 +0200105 s->func = test_deny_all;
106
Michal Vasko3a41dff2020-07-15 14:30:28 +0200107 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200108 struct lysc_node_container *cont;
109 struct lysc_node_leaf *leaf;
110 struct lysc_ext_instance *e;
111
112 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:nacm:a; prefix en;"
113 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
114 "container a { nacm:default-deny-all; leaf aa {type string;}}"
115 "leaf b {type string;}}";
116
117 /* valid data */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200118 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100119 assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data);
120 assert_non_null(leaf = (struct lysc_node_leaf *)cont->child);
Radek Krejci0935f412019-08-20 16:15:18 +0200121 assert_non_null(e = &cont->exts[0]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200122 assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1);
123 assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */
Radek Krejci0935f412019-08-20 16:15:18 +0200124 assert_ptr_equal(e->def, leaf->exts[0].def);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100125 assert_int_equal(1, *((uint8_t *)e->data)); /* plugin's value for default-deny-all */
Radek Krejci0935f412019-08-20 16:15:18 +0200126 assert_null(cont->next->exts);
127
128 /* invalid */
129 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
130 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
131 "nacm:default-deny-all;}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200132 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejci0935f412019-08-20 16:15:18 +0200133 logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejciad5963b2019-09-06 16:03:05 +0200134 "Extension nacm:default-deny-all is allowed only in a data nodes, but it is placed in \"module\" statement.) /aa:{extension='nacm:default-deny-all'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200135
136 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
137 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
138 "leaf l { type string; nacm:default-deny-all; nacm:default-deny-write;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200139 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejci0935f412019-08-20 16:15:18 +0200140 logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejciad5963b2019-09-06 16:03:05 +0200141 "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.) /aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200142
143 s->func = NULL;
144}
145
146static void
147test_deny_write(void **state)
148{
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100149 struct state_s *s = (struct state_s *)(*state);
150
Radek Krejci0935f412019-08-20 16:15:18 +0200151 s->func = test_deny_write;
152
Michal Vasko3a41dff2020-07-15 14:30:28 +0200153 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200154 struct lysc_node_container *cont;
155 struct lysc_node_leaf *leaf;
156 struct lysc_ext_instance *e;
157
158 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:nacm:a; prefix en;"
159 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
160 "container a { nacm:default-deny-write; leaf aa {type string;}}"
161 "leaf b {type string;}}";
162
163 /* valid data */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200164 assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod));
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100165 assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data);
166 assert_non_null(leaf = (struct lysc_node_leaf *)cont->child);
Radek Krejci0935f412019-08-20 16:15:18 +0200167 assert_non_null(e = &cont->exts[0]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200168 assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1);
169 assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */
Radek Krejci0935f412019-08-20 16:15:18 +0200170 assert_ptr_equal(e->def, leaf->exts[0].def);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100171 assert_int_equal(2, *((uint8_t *)e->data)); /* plugin's value for default-deny-write */
Radek Krejci0935f412019-08-20 16:15:18 +0200172 assert_null(cont->next->exts);
173
174 /* invalid */
175 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
176 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
177 "notification notif {nacm:default-deny-write;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200178 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejci0935f412019-08-20 16:15:18 +0200179 logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": "
Michal Vaskoa3881362020-01-21 15:57:35 +0100180 "Extension nacm:default-deny-write is not allowed in notification statement.) /aa:notif/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200181
182 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
183 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
184 "leaf l { type string; nacm:default-deny-write; nacm:default-deny-write;}}";
Michal Vasko3a41dff2020-07-15 14:30:28 +0200185 assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL));
Radek Krejci0935f412019-08-20 16:15:18 +0200186 logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejciad5963b2019-09-06 16:03:05 +0200187 "Extension nacm:default-deny-write is instantiated multiple times.) /aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200188
189 s->func = NULL;
190}
191
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100192int
193main(void)
Radek Krejci0935f412019-08-20 16:15:18 +0200194{
195 const struct CMUnitTest tests[] = {
196 cmocka_unit_test_setup_teardown(test_deny_all, setup, teardown),
197 cmocka_unit_test_setup_teardown(test_deny_write, setup, teardown),
198 };
199
200 return cmocka_run_group_tests(tests, NULL, NULL);
201}