1 /*
2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "internal/nelem.h"
20
21 typedef struct {
22 const char *data;
23 int time_result;
24 int type;
25 } TESTDATA;
26
27 /**********************************************************************
28 *
29 * Test driver
30 *
31 ***/
32
33 static TESTDATA tests[] = {
34 { "20001201000000Z", 0, V_ASN1_GENERALIZEDTIME },
35 { "20001201010000+0100", 0, V_ASN1_GENERALIZEDTIME },
36 { "20001201050000+0500", 0, V_ASN1_GENERALIZEDTIME },
37 { "20001130230000-0100", 0, V_ASN1_GENERALIZEDTIME },
38 { "20001130190000-0500", 0, V_ASN1_GENERALIZEDTIME },
39 { "20001130190001-0500", 1, V_ASN1_GENERALIZEDTIME }, /* +1 second */
40 { "20001130185959-0500", -1, V_ASN1_GENERALIZEDTIME }, /* -1 second */
41 { "001201000000Z", 0, V_ASN1_UTCTIME },
42 { "001201010000+0100", 0, V_ASN1_UTCTIME },
43 { "001201050000+0500", 0, V_ASN1_UTCTIME },
44 { "001130230000-0100", 0, V_ASN1_UTCTIME },
45 { "001130190000-0500", 0, V_ASN1_UTCTIME },
46 { "001201000000-0000", 0, V_ASN1_UTCTIME },
47 { "001201000001-0000", 1, V_ASN1_UTCTIME }, /* +1 second */
48 { "001130235959-0000", -1, V_ASN1_UTCTIME }, /* -1 second */
49 { "20001201000000+0000", 0, V_ASN1_GENERALIZEDTIME },
50 { "20001201000000+0100", -1, V_ASN1_GENERALIZEDTIME },
51 { "001201000000+0100", -1, V_ASN1_UTCTIME },
52 { "20001201000000-0100", 1, V_ASN1_GENERALIZEDTIME },
53 { "001201000000-0100", 1, V_ASN1_UTCTIME },
54 { "20001201123400+1234", 0, V_ASN1_GENERALIZEDTIME },
55 { "20001130112600-1234", 0, V_ASN1_GENERALIZEDTIME },
56 };
57
58 static time_t the_time = 975628800;
59 static ASN1_TIME the_asn1_time = {
60 15,
61 V_ASN1_GENERALIZEDTIME,
62 (unsigned char *)"20001201000000Z",
63 0
64 };
65
test_offset(int idx)66 static int test_offset(int idx)
67 {
68 ASN1_TIME at;
69 const TESTDATA *testdata = &tests[idx];
70 int ret = -2;
71 int day, sec;
72
73 at.data = (unsigned char *)testdata->data;
74 at.length = strlen(testdata->data);
75 at.type = testdata->type;
76 at.flags = 0;
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 ret = ASN1_TIME_cmp_time_t(&at, the_time);
99
100 if (!TEST_int_eq(testdata->time_result, ret)) {
101 TEST_info("ASN1_UTCTIME_cmp_time_t() test failed for %s\n", at.data);
102 return 0;
103 }
104
105 return 1;
106 }
107
setup_tests(void)108 int setup_tests(void)
109 {
110 ADD_ALL_TESTS(test_offset, OSSL_NELEM(tests));
111 return 1;
112 }
113