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