462436ff4851da6afcd4aea10debf95ab530a8f4
[openssl.git] / test / gmdifftest.c
1 /*
2  * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/crypto.h>
11
12 #include "test_main.h"
13 #include "testutil.h"
14
15 #define SECS_PER_DAY (24 * 60 * 60)
16
17 /*
18  * Time checking test code. Check times are identical for a wide range of
19  * offsets. This should be run on a machine with 64 bit time_t or it will
20  * trigger the very errors the routines fix.
21  */
22
23 static int check_time(long offset)
24 {
25     struct tm tm1, tm2, o1;
26     int off_day, off_sec;
27     long toffset;
28     time_t t1, t2;
29
30     time(&t1);
31
32     t2 = t1 + offset;
33     OPENSSL_gmtime(&t2, &tm2);
34     OPENSSL_gmtime(&t1, &tm1);
35     o1 = tm1;
36     OPENSSL_gmtime_adj(&tm1, 0, offset);
37     if (!TEST_int_eq(tm1.tm_year, tm2.tm_year)
38         || !TEST_int_eq(tm1.tm_mon, tm2.tm_mon)
39         || !TEST_int_eq(tm1.tm_mday, tm2.tm_mday)
40         || !TEST_int_eq(tm1.tm_hour, tm2.tm_hour)
41         || !TEST_int_eq(tm1.tm_min, tm2.tm_min)
42         || !TEST_int_eq(tm1.tm_sec, tm2.tm_sec)
43         || !TEST_true(OPENSSL_gmtime_diff(&off_day, &off_sec, &o1, &tm1)))
44         return 0;
45     toffset = (long)off_day * SECS_PER_DAY + off_sec;
46     if (!TEST_long_eq(offset, toffset))
47         return 0;
48     return 1;
49 }
50
51 static int test_gmtime(int offset)
52 {
53     return check_time(offset) &&
54            check_time(-offset) &&
55            check_time(offset * 1000L) &&
56            check_time(-offset * 1000L);
57 }
58
59 void register_tests(void)
60 {
61     if (sizeof(time_t) < 8)
62         TEST_info("Skipping; time_t is less than 64-bits");
63     else
64         ADD_ALL_TESTS(test_gmtime, 1000000);
65 }