1 /* 2 * Copyright 2015-2016 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 #include <stdio.h> 12 13 #define SECS_PER_DAY (24 * 60 * 60) 14 15 /* 16 * Time checking test code. Check times are identical for a wide range of 17 * offsets. This should be run on a machine with 64 bit time_t or it will 18 * trigger the very errors the routines fix. 19 */ 20 21 static int check_time(long offset) 22 { 23 struct tm tm1, tm2, o1; 24 int off_day, off_sec; 25 long toffset; 26 time_t t1, t2; 27 time(&t1); 28 29 t2 = t1 + offset; 30 OPENSSL_gmtime(&t2, &tm2); 31 OPENSSL_gmtime(&t1, &tm1); 32 o1 = tm1; 33 OPENSSL_gmtime_adj(&tm1, 0, offset); 34 if ((tm1.tm_year != tm2.tm_year) || 35 (tm1.tm_mon != tm2.tm_mon) || 36 (tm1.tm_mday != tm2.tm_mday) || 37 (tm1.tm_hour != tm2.tm_hour) || 38 (tm1.tm_min != tm2.tm_min) || (tm1.tm_sec != tm2.tm_sec)) { 39 fprintf(stderr, "TIME ERROR!!\n"); 40 fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n", 41 tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900, 42 tm2.tm_hour, tm2.tm_min, tm2.tm_sec); 43 fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n", 44 tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900, 45 tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 46 return 0; 47 } 48 if (!OPENSSL_gmtime_diff(&off_day, &off_sec, &o1, &tm1)) 49 return 0; 50 toffset = (long)off_day *SECS_PER_DAY + off_sec; 51 if (offset != toffset) { 52 fprintf(stderr, "TIME OFFSET ERROR!!\n"); 53 fprintf(stderr, "Expected %ld, Got %ld (%d:%d)\n", 54 offset, toffset, off_day, off_sec); 55 return 0; 56 } 57 return 1; 58 } 59 60 int main(int argc, char **argv) 61 { 62 long offset; 63 int fails; 64 65 if (sizeof(time_t) < 8) { 66 fprintf(stderr, "Skipping; time_t is less than 64-bits\n"); 67 return 0; 68 } 69 for (fails = 0, offset = 0; offset < 1000000; offset++) { 70 if (!check_time(offset)) 71 fails++; 72 if (!check_time(-offset)) 73 fails++; 74 if (!check_time(offset * 1000)) 75 fails++; 76 if (!check_time(-offset * 1000)) 77 fails++; 78 } 79 80 return fails ? 1 : 0; 81 } 82