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> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include "libyang.h" |
| 23 | |
| 24 | #define BUFSIZE 1024 |
| 25 | char logbuf[BUFSIZE] = {0}; |
| 26 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame^] | 27 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 28 | #define ENABLE_LOGGER_CHECKING 1 |
| 29 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 30 | static void |
| 31 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 32 | { |
| 33 | (void) level; /* unused */ |
| 34 | (void) path; /* unused */ |
| 35 | |
| 36 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | logger_setup(void **state) |
| 41 | { |
| 42 | (void) state; /* unused */ |
| 43 | |
| 44 | ly_set_log_clb(logger, 0); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame^] | 49 | void |
| 50 | logbuf_clean(void) |
| 51 | { |
| 52 | logbuf[0] = '\0'; |
| 53 | } |
| 54 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 55 | #if ENABLE_LOGGER_CHECKING |
| 56 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 57 | #else |
| 58 | # define logbuf_assert(str) |
| 59 | #endif |
| 60 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 61 | static void |
| 62 | test_date(void **state) |
| 63 | { |
| 64 | (void) state; /* unused */ |
| 65 | |
| 66 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 67 | logbuf_assert("Invalid argument date (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 68 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 69 | logbuf_assert("Invalid argument date_len (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 70 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 71 | logbuf_assert("Invalid value \"nonsencexx\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 72 | 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] | 73 | logbuf_assert("Invalid value \"123x-11-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 74 | 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] | 75 | logbuf_assert("Invalid value \"2018-13-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 76 | 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] | 77 | logbuf_assert("Invalid value \"2018-11-41\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 78 | 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] | 79 | logbuf_assert("Invalid value \"2018-02-29\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 80 | 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] | 81 | logbuf_assert("Invalid value \"2018.02-28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 82 | 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] | 83 | logbuf_assert("Invalid value \"2018-02.28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 84 | |
| 85 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 86 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 87 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 88 | } |
| 89 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 90 | static void |
| 91 | test_revisions(void **state) |
| 92 | { |
| 93 | (void) state; /* unused */ |
| 94 | |
| 95 | struct lysp_revision *revs = NULL, *rev; |
| 96 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame^] | 97 | logbuf_clean(); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 98 | /* no error, it just does nothing */ |
| 99 | lysp_sort_revisions(NULL); |
| 100 | logbuf_assert(""); |
| 101 | |
| 102 | /* revisions are stored in wrong order - the newest is the last */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 103 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 104 | strcpy(rev->date, "2018-01-01"); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 105 | LY_ARRAY_NEW_RET(NULL, revs, rev,); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 106 | strcpy(rev->date, "2018-12-31"); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 107 | |
| 108 | assert_int_equal(2, LY_ARRAY_SIZE(revs)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 109 | assert_string_equal("2018-01-01", &revs[0]); |
| 110 | assert_string_equal("2018-12-31", &revs[1]); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 111 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 112 | lysp_sort_revisions(revs); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 113 | assert_string_equal("2018-12-31", &revs[0]); |
| 114 | assert_string_equal("2018-01-01", &revs[1]); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 115 | |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 116 | LY_ARRAY_FREE(revs); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame^] | 119 | static void |
| 120 | test_typedef(void **state) |
| 121 | { |
| 122 | (void) state; /* unused */ |
| 123 | |
| 124 | struct ly_ctx *ctx = NULL; |
| 125 | const char *str; |
| 126 | |
| 127 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 128 | |
| 129 | str = "module a {namespace urn:a; prefix a; typedef test {type string;} typedef test {type int8;}}"; |
| 130 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 131 | logbuf_assert("Invalid name \"test\" of typedef - name collision with another top-level type."); |
| 132 | |
| 133 | str = "module a {namespace urn:a; prefix a; typedef x {type string;} container c {typedef x {type int8;}}}"; |
| 134 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 135 | logbuf_assert("Invalid name \"x\" of typedef - scoped type collide with a top-level type."); |
| 136 | |
| 137 | str = "module a {namespace urn:a; prefix a; container c {container d {typedef y {type int8;}} typedef y {type string;}}}"; |
| 138 | assert_null(lys_parse_mem(ctx, str, LYS_IN_YANG)); |
| 139 | logbuf_assert("Invalid name \"y\" of typedef - name collision with another scoped type."); |
| 140 | |
| 141 | ly_ctx_destroy(ctx, NULL); |
| 142 | } |
| 143 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 144 | int main(void) |
| 145 | { |
| 146 | const struct CMUnitTest tests[] = { |
| 147 | cmocka_unit_test_setup(test_date, logger_setup), |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame] | 148 | cmocka_unit_test_setup(test_revisions, logger_setup), |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame^] | 149 | cmocka_unit_test_setup(test_typedef, logger_setup), |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 153 | } |