]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/time_offset_test.c
Fix time offset calculation.
[thirdparty/openssl.git] / test / time_offset_test.c
CommitLineData
20ee2bf1
TS
1/*
2 * Copyright 2017 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/* time_t/offset (+/-XXXX) tests for ASN1 and X509 */
11
12#include <stdio.h>
13#include <string.h>
14#include <time.h>
15
16#include <openssl/asn1.h>
17#include <openssl/x509.h>
18#include "testutil.h"
19#include "e_os.h"
20
21typedef struct {
22 const char *data;
23 int time_result;
24 int type;
25} TESTDATA;
26
27
28/**********************************************************************
29 *
30 * Test driver
31 *
32 ***/
33
34static TESTDATA tests[] = {
35 { "20001201000000Z", 0, V_ASN1_GENERALIZEDTIME },
36 { "20001201010000+0100", 0, V_ASN1_GENERALIZEDTIME },
37 { "20001201050000+0500", 0, V_ASN1_GENERALIZEDTIME },
38 { "20001130230000-0100", 0, V_ASN1_GENERALIZEDTIME },
39 { "20001130190000-0500", 0, V_ASN1_GENERALIZEDTIME },
40 { "20001130190001-0500", 1, V_ASN1_GENERALIZEDTIME }, /* +1 second */
41 { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, /* -1 second */
42 { "001201000000Z", 0, V_ASN1_UTCTIME },
43 { "001201010000+0100", 0, V_ASN1_UTCTIME },
44 { "001201050000+0500", 0, V_ASN1_UTCTIME },
45 { "001130230000-0100", 0, V_ASN1_UTCTIME },
46 { "001130190000-0500", 0, V_ASN1_UTCTIME },
47 { "001201000000-0000", 0, V_ASN1_UTCTIME },
48 { "001201000001-0000", 1, V_ASN1_UTCTIME }, /* +1 second */
49 { "001130235959-0000", -1, V_ASN1_UTCTIME }, /* -1 second */
50 { "20001201000000+0000", 0, V_ASN1_GENERALIZEDTIME },
51 { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME },
52 { "001201000000+0100", -1, V_ASN1_UTCTIME },
53 { "20001201000000-0100", 1, V_ASN1_GENERALIZEDTIME },
54 { "001201000000-0100", 1, V_ASN1_UTCTIME },
55 { "20001201123400+1234", 0, V_ASN1_GENERALIZEDTIME },
56 { "20001130112600-1234", 0, V_ASN1_GENERALIZEDTIME },
57};
58
59static time_t the_time = 975628800;
60static ASN1_TIME the_asn1_time = {
61 15,
62 V_ASN1_GENERALIZEDTIME,
63 (unsigned char*)"20001201000000Z",
64 0
65};
66
67static int test_offset(int idx)
68{
69 ASN1_TIME at;
70 const TESTDATA *testdata = &tests[idx];
71 int ret = -2;
72 int day, sec;
73
74 at.data = (unsigned char*)testdata->data;
75 at.length = strlen(testdata->data);
76 at.type = testdata->type;
77
78 if (!TEST_true(ASN1_TIME_diff(&day, &sec, &the_asn1_time, &at))) {
79 TEST_info("ASN1_TIME_diff() failed for %s\n", at.data);
80 return 0;
81 }
82 if (day > 0)
83 ret = 1;
84 else if (day < 0)
85 ret = -1;
86 else if (sec > 0)
87 ret = 1;
88 else if (sec < 0)
89 ret = -1;
90 else
91 ret = 0;
92
93 if (!TEST_int_eq(testdata->time_result, ret)) {
94 TEST_info("ASN1_TIME_diff() test failed for %s day=%d sec=%d\n", at.data, day, sec);
95 return 0;
96 }
97
98 if (at.type == V_ASN1_UTCTIME)
99 ret = ASN1_UTCTIME_cmp_time_t(&at, the_time);
100 else
101 return 1; /* no other cmp_time_t() functions available, yet */
102
103 if (!TEST_int_eq(testdata->time_result, ret)) {
104 TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data);
105 return 0;
106 }
107
108 return 1;
109}
110
111void register_tests()
112{
113 ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests));
114}