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 | * |
| 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 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" |
| 18 | #include "tests/config.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 19 | |
| 20 | #define BUFSIZE 1024 |
| 21 | char logbuf[BUFSIZE] = {0}; |
| 22 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 23 | |
| 24 | struct 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 |
| 33 | static void |
| 34 | logger(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 Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 48 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | static int |
| 52 | setup(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 Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 64 | assert_non_null(ly_ctx_load_module(s->ctx, "ietf-netconf-acm", "2018-02-14", NULL)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 65 | |
| 66 | *state = s; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | teardown(void **state) |
| 73 | { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 74 | struct state_s *s = (struct state_s *)(*state); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 75 | |
| 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 | |
| 88 | void |
| 89 | logbuf_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 | |
| 100 | static void |
| 101 | test_deny_all(void **state) |
| 102 | { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 103 | struct state_s *s = (struct state_s *)(*state); |
| 104 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 105 | s->func = test_deny_all; |
| 106 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 107 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 108 | 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 118 | assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 119 | assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data); |
| 120 | assert_non_null(leaf = (struct lysc_node_leaf *)cont->child); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 121 | assert_non_null(e = &cont->exts[0]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 122 | assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1); |
| 123 | assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 124 | assert_ptr_equal(e->def, leaf->exts[0].def); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 125 | 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] | 126 | 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 132 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 133 | logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": " |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 134 | "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 Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 135 | |
| 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 139 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 140 | logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": " |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 141 | "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.) /aa:l/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 142 | |
| 143 | s->func = NULL; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | test_deny_write(void **state) |
| 148 | { |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 149 | struct state_s *s = (struct state_s *)(*state); |
| 150 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 151 | s->func = test_deny_write; |
| 152 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 153 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 154 | 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 164 | assert_int_equal(LY_SUCCESS, lys_parse_mem(s->ctx, data, LYS_IN_YANG, &mod)); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 165 | assert_non_null(cont = (struct lysc_node_container *)mod->compiled->data); |
| 166 | assert_non_null(leaf = (struct lysc_node_leaf *)cont->child); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 167 | assert_non_null(e = &cont->exts[0]); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 168 | assert_int_equal(LY_ARRAY_COUNT(cont->exts), 1); |
| 169 | assert_int_equal(LY_ARRAY_COUNT(leaf->exts), 1); /* NACM extensions inherit */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 170 | assert_ptr_equal(e->def, leaf->exts[0].def); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 171 | 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] | 172 | 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 178 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 179 | logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": " |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 180 | "Extension nacm:default-deny-write is not allowed in notification statement.) /aa:notif/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 181 | |
| 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 Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 185 | assert_int_equal(LY_EVALID, lys_parse_mem(s->ctx, data, LYS_IN_YANG, NULL)); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 186 | logbuf_assert("Extension plugin \"libyang 2 - NACM, version 1\": " |
Radek Krejci | ad5963b | 2019-09-06 16:03:05 +0200 | [diff] [blame] | 187 | "Extension nacm:default-deny-write is instantiated multiple times.) /aa:l/{extension='nacm:default-deny-write'}"); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 188 | |
| 189 | s->func = NULL; |
| 190 | } |
| 191 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame^] | 192 | int |
| 193 | main(void) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 194 | { |
| 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 | } |