]>
Commit | Line | Data |
---|---|---|
440e5d80 | 1 | /* |
3c2bdd7d | 2 | * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. |
3a3cb629 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
440e5d80 RS |
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 | |
3a3cb629 RS |
8 | */ |
9 | ||
10 | #include <openssl/crypto.h> | |
7635e5bc | 11 | |
7635e5bc | 12 | #include "testutil.h" |
3a3cb629 RS |
13 | |
14 | #define SECS_PER_DAY (24 * 60 * 60) | |
15 | ||
16 | /* | |
17 | * Time checking test code. Check times are identical for a wide range of | |
18 | * offsets. This should be run on a machine with 64 bit time_t or it will | |
19 | * trigger the very errors the routines fix. | |
20 | */ | |
21 | ||
22 | static int check_time(long offset) | |
23 | { | |
24 | struct tm tm1, tm2, o1; | |
25 | int off_day, off_sec; | |
26 | long toffset; | |
27 | time_t t1, t2; | |
7635e5bc | 28 | |
3a3cb629 RS |
29 | time(&t1); |
30 | ||
31 | t2 = t1 + offset; | |
32 | OPENSSL_gmtime(&t2, &tm2); | |
33 | OPENSSL_gmtime(&t1, &tm1); | |
34 | o1 = tm1; | |
6a6844a2 P |
35 | if (!TEST_true(OPENSSL_gmtime_adj(&tm1, 0, offset)) |
36 | || !TEST_int_eq(tm1.tm_year, tm2.tm_year) | |
7635e5bc P |
37 | || !TEST_int_eq(tm1.tm_mon, tm2.tm_mon) |
38 | || !TEST_int_eq(tm1.tm_mday, tm2.tm_mday) | |
39 | || !TEST_int_eq(tm1.tm_hour, tm2.tm_hour) | |
40 | || !TEST_int_eq(tm1.tm_min, tm2.tm_min) | |
41 | || !TEST_int_eq(tm1.tm_sec, tm2.tm_sec) | |
42 | || !TEST_true(OPENSSL_gmtime_diff(&off_day, &off_sec, &o1, &tm1))) | |
3a3cb629 | 43 | return 0; |
7635e5bc P |
44 | toffset = (long)off_day * SECS_PER_DAY + off_sec; |
45 | if (!TEST_long_eq(offset, toffset)) | |
3a3cb629 | 46 | return 0; |
3a3cb629 RS |
47 | return 1; |
48 | } | |
49 | ||
7635e5bc | 50 | static int test_gmtime(int offset) |
3a3cb629 | 51 | { |
a7a7e6e3 P |
52 | return check_time(offset) |
53 | && check_time(-offset) | |
54 | && check_time(offset * 1000L) | |
55 | && check_time(-offset * 1000L) | |
56 | && check_time(offset * 1000000L) | |
57 | && check_time(-offset * 1000000L); | |
7635e5bc | 58 | } |
3a3cb629 | 59 | |
ad887416 | 60 | int setup_tests(void) |
7635e5bc P |
61 | { |
62 | if (sizeof(time_t) < 8) | |
63 | TEST_info("Skipping; time_t is less than 64-bits"); | |
64 | else | |
a7a7e6e3 | 65 | ADD_ALL_TESTS_NOSUBTEST(test_gmtime, 1000); |
ad887416 | 66 | return 1; |
3a3cb629 | 67 | } |