blob: 2298deafd4fab5402eaa6c0e686ec21460709bcd [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 Vasko4de7d072021-07-09 09:13:18 +020033 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
Radek Krejci80bde9c2021-02-09 15:25:02 +010054 /* ignored - valid with warning */
55 data = "module b {yang-version 1.1; namespace urn:tests:extensions:nacm:b; prefix en;"
Radek Krejci0935f412019-08-20 16:15:18 +020056 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
57 "nacm:default-deny-all;}";
Radek Krejci80bde9c2021-02-09 15:25:02 +010058 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +020059 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejci932d3bb2021-02-09 16:29:38 +010060 "Extension nacm:default-deny-all is allowed only in a data nodes, but it is placed in \"module\" statement.",
Radek Krejci80bde9c2021-02-09 15:25:02 +010061 "/b:{extension='nacm:default-deny-all'}");
Radek Krejci0935f412019-08-20 16:15:18 +020062
Radek Krejci80bde9c2021-02-09 15:25:02 +010063 /* invalid */
Radek Krejci0935f412019-08-20 16:15:18 +020064 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
65 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
66 "leaf l { type string; nacm:default-deny-all; nacm:default-deny-write;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +020067 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
68 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejci932d3bb2021-02-09 16:29:38 +010069 "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.",
Radek Iša56ca9e42020-09-08 18:42:00 +020070 "/aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +020071}
72
73static void
74test_deny_write(void **state)
75{
Michal Vasko4de7d072021-07-09 09:13:18 +020076 struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +020077 struct lysc_node_container *cont;
78 struct lysc_node_leaf *leaf;
79 struct lysc_ext_instance *e;
80
81 const char *data = "module a {yang-version 1.1; namespace urn:tests:extensions:nacm:a; prefix en;"
82 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
83 "container a { nacm:default-deny-write; leaf aa {type string;}}"
84 "leaf b {type string;}}";
85
86 /* valid data */
Radek Iša56ca9e42020-09-08 18:42:00 +020087 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod));
Radek Krejcib4ac5a92020-11-23 17:54:33 +010088 assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data);
89 assert_non_null(leaf = (struct lysc_node_leaf *)cont->child);
Radek Krejci0935f412019-08-20 16:15:18 +020090 assert_non_null(e = &cont->exts[0]);
Michal Vaskofd69e1d2020-07-03 11:57:17 +020091 assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1);
92 assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */
Radek Krejci0935f412019-08-20 16:15:18 +020093 assert_ptr_equal(e->def, leaf->exts[0].def);
Radek Krejcib4ac5a92020-11-23 17:54:33 +010094 assert_int_equal(2, *((uint8_t *)e->data)); /* plugin's value for default-deny-write */
Radek Krejci0935f412019-08-20 16:15:18 +020095
Radek Krejci80bde9c2021-02-09 15:25:02 +010096 /* ignored - valid with warning */
97 data = "module b {yang-version 1.1; namespace urn:tests:extensions:nacm:b; prefix en;"
Radek Krejci0935f412019-08-20 16:15:18 +020098 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
99 "notification notif {nacm:default-deny-write;}}";
Radek Krejci80bde9c2021-02-09 15:25:02 +0100100 assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200101 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejci932d3bb2021-02-09 16:29:38 +0100102 "Extension nacm:default-deny-write is not allowed in notification statement.",
Radek Krejci80bde9c2021-02-09 15:25:02 +0100103 "/b:notif/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200104
Radek Krejci80bde9c2021-02-09 15:25:02 +0100105 /* invalid */
Radek Krejci0935f412019-08-20 16:15:18 +0200106 data = "module aa {yang-version 1.1; namespace urn:tests:extensions:nacm:aa; prefix en;"
107 "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}"
108 "leaf l { type string; nacm:default-deny-write; nacm:default-deny-write;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 assert_int_equal(LY_EVALID, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL));
110 CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": "
Radek Krejci932d3bb2021-02-09 16:29:38 +0100111 "Extension nacm:default-deny-write is instantiated multiple times.",
Radek Iša56ca9e42020-09-08 18:42:00 +0200112 "/aa:l/{extension='nacm:default-deny-write'}");
Radek Krejci0935f412019-08-20 16:15:18 +0200113}
114
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100115int
116main(void)
Radek Krejci0935f412019-08-20 16:15:18 +0200117{
118 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +0200119 UTEST(test_deny_all, setup),
120 UTEST(test_deny_write, setup),
Radek Krejci0935f412019-08-20 16:15:18 +0200121 };
122
123 return cmocka_run_group_tests(tests, NULL, NULL);
124}