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