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 | |
| 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 58 | 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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 62 | |
| 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 66 | 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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void |
| 73 | test_deny_write(void **state) |
| 74 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 75 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 76 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 86 | 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] | 87 | assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data); |
| 88 | assert_non_null(leaf = (struct lysc_node_leaf *)cont->child); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 89 | assert_non_null(e = &cont->exts[0]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 90 | assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1); |
| 91 | assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 92 | assert_ptr_equal(e->def, leaf->exts[0].def); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 93 | 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] | 94 | 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 100 | 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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 104 | |
| 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ša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 108 | 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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 114 | int |
| 115 | main(void) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 116 | { |
| 117 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame^] | 118 | UTEST(test_deny_all, setup), |
| 119 | UTEST(test_deny_write, setup), |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 123 | } |