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