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 | |
| 27 | static void |
| 28 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 29 | { |
| 30 | (void) level; /* unused */ |
| 31 | (void) path; /* unused */ |
| 32 | |
| 33 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 34 | } |
| 35 | |
| 36 | static int |
| 37 | logger_setup(void **state) |
| 38 | { |
| 39 | (void) state; /* unused */ |
| 40 | |
| 41 | ly_set_log_clb(logger, 0); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 46 | #if ENABLE_LOGGER_CHECKING |
| 47 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 48 | #else |
| 49 | # define logbuf_assert(str) |
| 50 | #endif |
| 51 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 52 | static void |
| 53 | test_date(void **state) |
| 54 | { |
| 55 | (void) state; /* unused */ |
| 56 | |
| 57 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 58 | logbuf_assert("Invalid argument date (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 59 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 60 | logbuf_assert("Invalid argument date_len (lysp_check_date())."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 61 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 62 | logbuf_assert("Invalid value \"nonsencexx\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 63 | 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^] | 64 | logbuf_assert("Invalid value \"123x-11-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 65 | 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^] | 66 | logbuf_assert("Invalid value \"2018-13-11\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 67 | 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^] | 68 | logbuf_assert("Invalid value \"2018-11-41\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 69 | 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^] | 70 | logbuf_assert("Invalid value \"2018-02-29\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 71 | 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^] | 72 | logbuf_assert("Invalid value \"2018.02-28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 73 | 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^] | 74 | logbuf_assert("Invalid value \"2018-02.28\" of \"date\"."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 75 | |
| 76 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 77 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 78 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 79 | } |
| 80 | |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 81 | static void |
| 82 | test_revisions(void **state) |
| 83 | { |
| 84 | (void) state; /* unused */ |
| 85 | |
| 86 | struct lysp_revision *revs = NULL, *rev; |
| 87 | |
| 88 | /* no error, it just does nothing */ |
| 89 | lysp_sort_revisions(NULL); |
| 90 | logbuf_assert(""); |
| 91 | |
| 92 | /* revisions are stored in wrong order - the newest is the last */ |
| 93 | LYSP_ARRAY_NEW_RET(NULL, revs, rev,); |
| 94 | strcpy(rev->rev, "2018-01-01"); |
| 95 | LYSP_ARRAY_NEW_RET(NULL, revs, rev,); |
| 96 | strcpy(rev->rev, "2018-12-31"); |
| 97 | |
| 98 | assert_int_equal(2, LY_ARRAY_SIZE(revs)); |
| 99 | assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 0)); |
| 100 | assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 1)); |
| 101 | /* the order should be fixed, so the newest revision will be the first in the array */ |
| 102 | lysp_sort_revisions(revs); |
| 103 | assert_string_equal("2018-12-31", LY_ARRAY_INDEX(revs, 0)); |
| 104 | assert_string_equal("2018-01-01", LY_ARRAY_INDEX(revs, 1)); |
| 105 | |
| 106 | free(revs); |
| 107 | } |
| 108 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 109 | int main(void) |
| 110 | { |
| 111 | const struct CMUnitTest tests[] = { |
| 112 | cmocka_unit_test_setup(test_date, logger_setup), |
Radek Krejci | a93621b | 2018-10-18 11:13:38 +0200 | [diff] [blame^] | 113 | cmocka_unit_test_setup(test_revisions, logger_setup), |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 117 | } |