Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_nacm.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for NACM extensions support |
| 5 | * |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 CESNET, z.s.p.o. |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 15 | #include "utests.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 17 | #include "libyang.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 18 | |
| 19 | static int |
| 20 | setup(void **state) |
| 21 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 22 | UTEST_SETUP; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 23 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 24 | 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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | static void |
| 31 | test_deny_all(void **state) |
| 32 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 33 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 34 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 44 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 45 | assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data); |
| 46 | assert_non_null(leaf = (struct lysc_node_leaf *)cont->child); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 47 | assert_non_null(e = &cont->exts[0]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 48 | assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1); |
| 49 | assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 50 | assert_ptr_equal(e->def, leaf->exts[0].def); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 51 | assert_int_equal(1, *((uint8_t *)e->data)); /* plugin's value for default-deny-all */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 52 | assert_null(cont->next->exts); |
| 53 | |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 54 | /* ignored - valid with warning */ |
| 55 | data = "module b {yang-version 1.1; namespace urn:tests:extensions:nacm:b; prefix en;" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 56 | "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}" |
| 57 | "nacm:default-deny-all;}"; |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 58 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 59 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": " |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame^] | 60 | "Extension nacm:default-deny-all is allowed only in a data nodes, but it is placed in \"module\" statement.", |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 61 | "/b:{extension='nacm:default-deny-all'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 62 | |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 63 | /* invalid */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 64 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 67 | 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 Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame^] | 69 | "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 70 | "/aa:l/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void |
| 74 | test_deny_write(void **state) |
| 75 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 76 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 77 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, &mod)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 88 | assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data); |
| 89 | assert_non_null(leaf = (struct lysc_node_leaf *)cont->child); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 90 | assert_non_null(e = &cont->exts[0]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 91 | assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1); |
| 92 | assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 93 | assert_ptr_equal(e->def, leaf->exts[0].def); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 94 | assert_int_equal(2, *((uint8_t *)e->data)); /* plugin's value for default-deny-write */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 95 | |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 96 | /* ignored - valid with warning */ |
| 97 | data = "module b {yang-version 1.1; namespace urn:tests:extensions:nacm:b; prefix en;" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 98 | "import ietf-netconf-acm {revision-date 2018-02-14; prefix nacm;}" |
| 99 | "notification notif {nacm:default-deny-write;}}"; |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 100 | assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX, data, LYS_IN_YANG, NULL)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 101 | CHECK_LOG_CTX("Extension plugin \"libyang 2 - NACM, version 1\": " |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame^] | 102 | "Extension nacm:default-deny-write is not allowed in notification statement.", |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 103 | "/b:notif/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 104 | |
Radek Krejci | 80bde9c | 2021-02-09 15:25:02 +0100 | [diff] [blame] | 105 | /* invalid */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 106 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 109 | 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 Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame^] | 111 | "Extension nacm:default-deny-write is instantiated multiple times.", |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 112 | "/aa:l/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 115 | int |
| 116 | main(void) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 117 | { |
| 118 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 119 | UTEST(test_deny_all, setup), |
| 120 | UTEST(test_deny_write, setup), |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 124 | } |