Radek Krejci | f69a6d8 | 2016-05-31 16:05:47 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file test_time.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 tests - time functions |
| 5 | * |
| 6 | * Copyright (c) 2015 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 <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | #include <libnetconf.h> |
| 22 | |
| 23 | #include "config.h" |
| 24 | |
| 25 | static void |
| 26 | test_2time(void **state) |
| 27 | { |
| 28 | (void) state; /* unused */ |
| 29 | const char *date1 = "2010-02-28T12:34:56Z"; /* 1267360496 */ |
| 30 | const char *date2 = "2010-02-28T22:34:56+10:00"; /* 1267360496 */ |
| 31 | const char *date3 = "2010-02-28T02:34:56-10:00"; /* 1267360496 */ |
| 32 | const char *date4 = "2010-02-28T12:34:56+00:00"; /* 1267360496 */ |
| 33 | const char *date5 = "2010-02-28T12:34:56-00:00"; /* 1267360496 */ |
| 34 | const char *date6 = "2010-02-28T12:34:56.789Z"; /* 1267360496 */ |
| 35 | time_t t; |
| 36 | |
| 37 | t = nc_datetime2time(date1); |
| 38 | assert_int_equal(t, 1267360496); |
| 39 | |
| 40 | t = nc_datetime2time(date2); |
| 41 | assert_int_equal(t, 1267360496); |
| 42 | |
| 43 | t = nc_datetime2time(date3); |
| 44 | assert_int_equal(t, 1267360496); |
| 45 | |
| 46 | t = nc_datetime2time(date4); |
| 47 | assert_int_equal(t, 1267360496); |
| 48 | |
| 49 | t = nc_datetime2time(date5); |
| 50 | assert_int_equal(t, 1267360496); |
| 51 | |
| 52 | t = nc_datetime2time(date6); |
| 53 | assert_int_equal(t, 1267360496); |
| 54 | |
| 55 | t = nc_datetime2time(NULL); |
| 56 | assert_int_equal(t, -1); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | test_2datetime(void **state) |
| 61 | { |
| 62 | (void) state; /* unused */ |
| 63 | time_t t = 1267360496; |
| 64 | char buf[30]; |
| 65 | |
| 66 | assert_ptr_not_equal(NULL, nc_time2datetime(t, NULL, buf)); |
| 67 | assert_string_equal(buf, "2010-02-28T12:34:56Z"); |
| 68 | |
| 69 | assert_ptr_not_equal(NULL, nc_time2datetime(t, "Pacific/Honolulu", buf)); |
| 70 | assert_string_equal(buf, "2010-02-28T02:34:56-10:00"); |
| 71 | |
| 72 | assert_ptr_not_equal(NULL, nc_time2datetime(t, "Asia/Vladivostok", buf)); |
| 73 | assert_string_equal(buf, "2010-02-28T22:34:56+10:00"); |
| 74 | |
Michal Vasko | 62714bd | 2016-06-03 12:08:00 +0200 | [diff] [blame] | 75 | assert_ptr_not_equal(NULL, nc_time2datetime(t, "CET", buf)); |
| 76 | assert_string_equal(buf, "2010-02-28T13:34:56+01:00"); |
| 77 | |
Radek Krejci | f69a6d8 | 2016-05-31 16:05:47 +0200 | [diff] [blame] | 78 | /* negative years are prohibited */ |
| 79 | assert_ptr_equal(NULL, nc_time2datetime(-69999999999, NULL, buf)); |
| 80 | |
| 81 | /* unknown timezone -> UTC */ |
| 82 | assert_ptr_not_equal(NULL, nc_time2datetime(t, "xxx", buf)); |
| 83 | assert_string_equal(buf, "2010-02-28T12:34:56Z"); |
| 84 | } |
| 85 | |
| 86 | int main(void) |
| 87 | { |
| 88 | const struct CMUnitTest tests[] = { |
| 89 | cmocka_unit_test(test_2time), |
| 90 | cmocka_unit_test(test_2datetime), |
| 91 | }; |
| 92 | |
| 93 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 94 | } |