blob: d8eb15764298381f3ca27554d9872738f0fcbbdc [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 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
Radek Krejci0935f412019-08-20 16:15:18 +02007 *
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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
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"
Radek Krejci0935f412019-08-20 16:15:18 +020018
19static int
20setup(void **state)
21{
Radek Iša56ca9e42020-09-08 18:42:00 +020022 UTEST_SETUP;
Radek Krejci0935f412019-08-20 16:15:18 +020023
Radek Iša56ca9e42020-09-08 18:42:00 +020024 assert_int_equal(LY_SUCCESS, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_DIR_MODULES_YANG));
25 assert_non_null(ly_ctx_load_module(UTEST_LYCTX, "ietf-netconf-acm", "2018-02-14", NULL));
Radek Krejci0935f412019-08-20 16:15:18 +020026
27 return 0;
28}
29
Radek Krejci0935f412019-08-20 16:15:18 +020030static void
31test_deny_all(void **state)
32{
Michal Vasko3a41dff2020-07-15 14:30:28 +020033 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +020034 struct lysc_node_container *cont;
35 struct lysc_node_leaf *leaf;
36 struct lysc_ext_instance *e;
37
38 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:nacm:a; prefix en;"
39 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
40 "container a { nacm:default-deny-all; leaf aa {type string;}}"
41 "leaf b {type string;}}";
42
43 /* valid data */
Radek Iša56ca9e42020-09-08 18:42:00 +020044 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010045 assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data);
46 assert_non_null(leaf = (struct lysc_node_leaf *)cont->child);
Radek Krejci0935f412019-08-20 16:15:18 +020047 assert_non_null(e = &cont->exts[0]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020048 assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1);
49 assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */
Radek Krejci0935f412019-08-20 16:15:18 +020050 assert_ptr_equal(e->def, leaf->exts[0].def);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010051 assert_int_equal(1, *((uint8_t *)e->data)); /* plugin's value for default-deny-all */
Radek Krejci0935f412019-08-20 16:15:18 +020052 assert_null(cont->next->exts);
53
54 /* invalid */
55 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
56 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
57 "nacm:default-deny-all;}";
Radek Iša56ca9e42020-09-08 18:42:00 +020058 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
59 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
60 "Extension nacm:default-deny-all is allowed only in a data nodes, but it is placed in \"module\" statement.)",
61 "/aa:{extension='nacm:default-deny-all'}");
Radek Krejci0935f412019-08-20 16:15:18 +020062
63 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
64 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
65 "leaf l { type string; nacm:default-deny-all; nacm:default-deny-write;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +020066 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
67 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
68 "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.)",
69 "/aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +020070}
71
72static void
73test_deny_write(void **state)
74{
Michal Vasko3a41dff2020-07-15 14:30:28 +020075 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +020076 struct lysc_node_container *cont;
77 struct lysc_node_leaf *leaf;
78 struct lysc_ext_instance *e;
79
80 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:nacm:a; prefix en;"
81 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
82 "container a { nacm:default-deny-write; leaf aa {type string;}}"
83 "leaf b {type string;}}";
84
85 /* valid data */
Radek Iša56ca9e42020-09-08 18:42:00 +020086 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010087 assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data);
88 assert_non_null(leaf = (struct lysc_node_leaf *)cont->child);
Radek Krejci0935f412019-08-20 16:15:18 +020089 assert_non_null(e = &cont->exts[0]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020090 assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1);
91 assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */
Radek Krejci0935f412019-08-20 16:15:18 +020092 assert_ptr_equal(e->def, leaf->exts[0].def);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010093 assert_int_equal(2, *((uint8_t *)e->data)); /* plugin's value for default-deny-write */
Radek Krejci0935f412019-08-20 16:15:18 +020094 assert_null(cont->next->exts);
95
96 /* invalid */
97 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
98 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
99 "notification notif {nacm:default-deny-write;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200100 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
101 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
102 "Extension nacm:default-deny-write is not allowed in notification statement.)",
103 "/aa:notif/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200104
105 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
106 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
107 "leaf l { type string; nacm:default-deny-write; nacm:default-deny-write;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
109 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
110 "Extension nacm:default-deny-write is instantiated multiple times.)",
111 "/aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200112}
113
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100114int
115main(void)
Radek Krejci0935f412019-08-20 16:15:18 +0200116{
117 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200118 UTEST(test_deny_all, setup),
119 UTEST(test_deny_write, setup),
Radek Krejci0935f412019-08-20 16:15:18 +0200120 };
121
122 return cmocka_run_group_tests(tests, NULL, NULL);
123}