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