blob: fe1f53247b228739fb370c118119c4177bd25e20 [file] [log] [blame]
Radek Krejcif69a6d82016-05-31 16:05:47 +02001/**
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 Krejci323b8492016-06-03 14:09:20 +020015#include <inttypes.h>
Radek Krejcif69a6d82016-05-31 16:05:47 +020016#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
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020024#include "tests/config.h"
Radek Krejcif69a6d82016-05-31 16:05:47 +020025
26static void
27test_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
60static void
61test_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 Vaskocf5131e2016-06-03 12:08:00 +020076 assert_ptr_not_equal(NULL, nc_time2datetime(t, "CET", buf));
77 assert_string_equal(buf, "2010-02-28T13:34:56+01:00");
78
Radek Krejci323b8492016-06-03 14:09:20 +020079#if __WORDSIZE == 64
Radek Krejcif69a6d82016-05-31 16:05:47 +020080 /* negative years are prohibited */
81 assert_ptr_equal(NULL, nc_time2datetime(-69999999999, NULL, buf));
Radek Krejci323b8492016-06-03 14:09:20 +020082#endif
Radek Krejcif69a6d82016-05-31 16:05:47 +020083
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
89int 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}