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