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