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 | |
| 46 | static void |
| 47 | test_date(void **state) |
| 48 | { |
| 49 | (void) state; /* unused */ |
| 50 | |
| 51 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date")); |
| 52 | assert_string_equal(logbuf, "Invalid argument date (lysp_check_date())."); |
| 53 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date")); |
| 54 | assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date())."); |
| 55 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date")); |
| 56 | assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\"."); |
| 57 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date")); |
| 58 | assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\"."); |
| 59 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date")); |
| 60 | assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\"."); |
| 61 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date")); |
| 62 | assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\"."); |
| 63 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date")); |
| 64 | assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\"."); |
| 65 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date")); |
| 66 | assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\"."); |
| 67 | assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date")); |
| 68 | assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\"."); |
| 69 | |
| 70 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date")); |
| 71 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date")); |
| 72 | assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date")); |
| 73 | } |
| 74 | |
| 75 | int main(void) |
| 76 | { |
| 77 | const struct CMUnitTest tests[] = { |
| 78 | cmocka_unit_test_setup(test_date, logger_setup), |
| 79 | }; |
| 80 | |
| 81 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 82 | } |