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