Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file set.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from common.c |
| 5 | * |
| 6 | * Copyright (c) 2018 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 | |
| 15 | #include "../../src/tree_schema_helpers.c" |
| 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 19 | #include <stdio.h> |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 20 | #include <setjmp.h> |
| 21 | #include <cmocka.h> |
| 22 | |
| 23 | #include "libyang.h" |
| 24 | |
| 25 | #define BUFSIZE 1024 |
| 26 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 27 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 29 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 30 | #define ENABLE_LOGGER_CHECKING 1 |
| 31 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 32 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 33 | static void |
| 34 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 35 | { |
| 36 | (void) level; /* unused */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 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 | } |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 47 | } |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 48 | #endif |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 49 | |
| 50 | static int |
| 51 | logger_setup(void **state) |
| 52 | { |
| 53 | (void) state; /* unused */ |
| 54 | |
| 55 | ly_set_log_clb(logger, 0); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 60 | void |
| 61 | logbuf_clean(void) |
| 62 | { |
| 63 | logbuf[0] = '\0'; |
| 64 | } |
| 65 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 66 | #if ENABLE_LOGGER_CHECKING |
| 67 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 68 | #else |
| 69 | # define logbuf_assert(str) |
| 70 | #endif |
| 71 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 72 | static void |
| 73 | test_date(void **state) |
| 74 | { |
| 75 | (void) state; /* unused */ |
| 76 | |
| 77 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 78 | logbuf_assert("Invalid argument date (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 79 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 80 | logbuf_assert("Invalid argument date_len (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 81 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 82 | logbuf_assert("Invalid value \"nonsencexx\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 83 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 84 | logbuf_assert("Invalid value \"123x-11-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 85 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 86 | logbuf_assert("Invalid value \"2018-13-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 87 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 88 | logbuf_assert("Invalid value \"2018-11-41\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 89 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 90 | logbuf_assert("Invalid value \"2018-02-29\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 91 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 92 | logbuf_assert("Invalid value \"2018.02-28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 93 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 94 | logbuf_assert("Invalid value \"2018-02.28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 95 | |
| 96 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 97 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 98 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 99 | } |
| 100 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 101 | static void |
| 102 | test_revisions(void **state) |
| 103 | { |
| 104 | (void) state; /* unused */ |
| 105 | |
| 106 | struct lysp_revision *revs = NULL, *rev; |
| 107 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 108 | logbuf_clean(); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 109 | /* no error, it just does nothing */ |
| 110 | lysp_sort_revisions(NULL); |
| 111 | logbuf_assert(""); |
| 112 | |
| 113 | /* revisions are stored in wrong order - the newest is the last */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 114 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 115 | strcpy(rev->date, "2018-01-01"); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 116 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 117 | strcpy(rev->date, "2018-12-31"); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 118 | |
| 119 | assert_int_equal(2, LY_ARRAY_SIZE(revs)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 120 | assert_string_equal("2018-01-01", &revs[0]); |
| 121 | assert_string_equal("2018-12-31", &revs[1]); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 122 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 123 | lysp_sort_revisions(revs); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 124 | assert_string_equal("2018-12-31", &revs[0]); |
| 125 | assert_string_equal("2018-01-01", &revs[1]); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 126 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 127 | LY_ARRAY_FREE(revs); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 130 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 131 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 132 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 133 | { |
| 134 | *module_data = user_data; |
| 135 | *format = LYS_IN_YANG; |
| 136 | *free_module_data = NULL; |
| 137 | return LY_SUCCESS; |
| 138 | } |
| 139 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 140 | static void |
| 141 | test_typedef(void **state) |
| 142 | { |
| 143 | (void) state; /* unused */ |
| 144 | |
| 145 | struct ly_ctx *ctx = NULL; |
| 146 | const char *str; |
| 147 | |
| 148 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 149 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 150 | str = "module a {namespace urn:a; prefix a; typedef binary {type string;}}"; |
| 151 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 152 | logbuf_assert("Invalid name \"binary\" of typedef - name collision with a built-in type."); |
| 153 | str = "module a {namespace urn:a; prefix a; typedef bits {type string;}}"; |
| 154 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 155 | logbuf_assert("Invalid name \"bits\" of typedef - name collision with a built-in type."); |
| 156 | str = "module a {namespace urn:a; prefix a; typedef boolean {type string;}}"; |
| 157 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 158 | logbuf_assert("Invalid name \"boolean\" of typedef - name collision with a built-in type."); |
| 159 | str = "module a {namespace urn:a; prefix a; typedef decimal64 {type string;}}"; |
| 160 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 161 | logbuf_assert("Invalid name \"decimal64\" of typedef - name collision with a built-in type."); |
| 162 | str = "module a {namespace urn:a; prefix a; typedef empty {type string;}}"; |
| 163 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 164 | logbuf_assert("Invalid name \"empty\" of typedef - name collision with a built-in type."); |
| 165 | str = "module a {namespace urn:a; prefix a; typedef enumeration {type string;}}"; |
| 166 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 167 | logbuf_assert("Invalid name \"enumeration\" of typedef - name collision with a built-in type."); |
| 168 | str = "module a {namespace urn:a; prefix a; typedef int8 {type string;}}"; |
| 169 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 170 | logbuf_assert("Invalid name \"int8\" of typedef - name collision with a built-in type."); |
| 171 | str = "module a {namespace urn:a; prefix a; typedef int16 {type string;}}"; |
| 172 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 173 | logbuf_assert("Invalid name \"int16\" of typedef - name collision with a built-in type."); |
| 174 | str = "module a {namespace urn:a; prefix a; typedef int32 {type string;}}"; |
| 175 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 176 | logbuf_assert("Invalid name \"int32\" of typedef - name collision with a built-in type."); |
| 177 | str = "module a {namespace urn:a; prefix a; typedef int64 {type string;}}"; |
| 178 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 179 | logbuf_assert("Invalid name \"int64\" of typedef - name collision with a built-in type."); |
| 180 | str = "module a {namespace urn:a; prefix a; typedef instance-identifier {type string;}}"; |
| 181 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 182 | logbuf_assert("Invalid name \"instance-identifier\" of typedef - name collision with a built-in type."); |
| 183 | str = "module a {namespace urn:a; prefix a; typedef identityref {type string;}}"; |
| 184 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 185 | logbuf_assert("Invalid name \"identityref\" of typedef - name collision with a built-in type."); |
| 186 | str = "module a {namespace urn:a; prefix a; typedef leafref {type string;}}"; |
| 187 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 188 | logbuf_assert("Invalid name \"leafref\" of typedef - name collision with a built-in type."); |
| 189 | str = "module a {namespace urn:a; prefix a; typedef string {type int8;}}"; |
| 190 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 191 | logbuf_assert("Invalid name \"string\" of typedef - name collision with a built-in type."); |
| 192 | str = "module a {namespace urn:a; prefix a; typedef union {type string;}}"; |
| 193 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 194 | logbuf_assert("Invalid name \"union\" of typedef - name collision with a built-in type."); |
| 195 | str = "module a {namespace urn:a; prefix a; typedef uint8 {type string;}}"; |
| 196 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 197 | logbuf_assert("Invalid name \"uint8\" of typedef - name collision with a built-in type."); |
| 198 | str = "module a {namespace urn:a; prefix a; typedef uint16 {type string;}}"; |
| 199 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 200 | logbuf_assert("Invalid name \"uint16\" of typedef - name collision with a built-in type."); |
| 201 | str = "module a {namespace urn:a; prefix a; typedef uint32 {type string;}}"; |
| 202 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 203 | logbuf_assert("Invalid name \"uint32\" of typedef - name collision with a built-in type."); |
| 204 | str = "module a {namespace urn:a; prefix a; typedef uint64 {type string;}}"; |
| 205 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 206 | logbuf_assert("Invalid name \"uint64\" of typedef - name collision with a built-in type."); |
| 207 | |
| 208 | str = "module mytypes {namespace urn:types; prefix t; typedef binary_ {type string;} typedef bits_ {type string;} typedef boolean_ {type string;} " |
| 209 | "typedef decimal64_ {type string;} typedef empty_ {type string;} typedef enumeration_ {type string;} typedef int8_ {type string;} typedef int16_ {type string;}" |
| 210 | "typedef int32_ {type string;} typedef int64_ {type string;} typedef instance-identifier_ {type string;} typedef identityref_ {type string;}" |
| 211 | "typedef leafref_ {type string;} typedef string_ {type int8;} typedef union_ {type string;} typedef uint8_ {type string;} typedef uint16_ {type string;}" |
| 212 | "typedef uint32_ {type string;} typedef uint64_ {type string;}}"; |
| 213 | assert_non_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 214 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 215 | str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}"; |
| 216 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 217 | logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type."); |
| 218 | |
| 219 | str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}"; |
| 220 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 221 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type."); |
| 222 | |
| 223 | str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}"; |
| 224 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 225 | logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type."); |
| 226 | |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 227 | str = "module a {namespace urn:a; prefix a; container c {typedef y {type int8;} typedef y {type string;}}}"; |
| 228 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 229 | logbuf_assert("Invalid name \"y\" of typedef - name collision with sibling type."); |
| 230 | |
| 231 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type string;}}"); |
| 232 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 233 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 234 | logbuf_assert("Invalid name \"x\" of typedef - name collision with another top-level type."); |
| 235 | |
| 236 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} container c {typedef x {type string;}}}"); |
| 237 | str = "module a {namespace urn:a; prefix a; include b; typedef x {type int8;}}"; |
| 238 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 239 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type."); |
| 240 | |
| 241 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule b {belongs-to a {prefix a;} typedef x {type int8;}}"); |
| 242 | str = "module a {namespace urn:a; prefix a; include b; container c {typedef x {type string;}}}"; |
| 243 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 244 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type."); |
| 245 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 246 | ly_ctx_destroy(ctx, NULL); |
| 247 | } |
| 248 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 249 | int main(void) |
| 250 | { |
| 251 | const struct CMUnitTest tests[] = { |
| 252 | cmocka_unit_test_setup(test_date, logger_setup), |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 253 | cmocka_unit_test_setup(test_revisions, logger_setup), |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 254 | cmocka_unit_test_setup(test_typedef, logger_setup), |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 258 | } |