x509_time_test.cc
Go to the documentation of this file.
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 // Tests for X509 time functions.
11 
12 #include <openssl/x509.h>
13 
14 #include <string.h>
15 #include <time.h>
16 
17 #include <gtest/gtest.h>
18 #include <openssl/asn1.h>
19 
20 struct TestData {
21  const char *data;
22  int type;
23  time_t cmp_time;
24  // -1 if asn1_time <= cmp_time, 1 if asn1_time > cmp_time, 0 if error.
25  int expected;
26 };
27 
28 static TestData kX509CmpTests[] = {
29  {
30  "20170217180154Z",
32  // The same in seconds since epoch.
33  1487354514,
34  -1,
35  },
36  {
37  "20170217180154Z",
39  // One second more.
40  1487354515,
41  -1,
42  },
43  {
44  "20170217180154Z",
46  // One second less.
47  1487354513,
48  1,
49  },
50  // Same as UTC time.
51  {
52  "170217180154Z",
54  // The same in seconds since epoch.
55  1487354514,
56  -1,
57  },
58  {
59  "170217180154Z",
61  // One second more.
62  1487354515,
63  -1,
64  },
65  {
66  "170217180154Z",
68  // One second less.
69  1487354513,
70  1,
71  },
72  // UTCTime from the 20th century.
73  {
74  "990217180154Z",
76  // The same in seconds since epoch.
77  919274514,
78  -1,
79  },
80  {
81  "990217180154Z",
83  // One second more.
84  919274515,
85  -1,
86  },
87  {
88  "990217180154Z",
90  // One second less.
91  919274513,
92  1,
93  },
94  // Various invalid formats.
95  {
96  // No trailing Z.
97  "20170217180154",
99  0,
100  0,
101  },
102  {
103  // No trailing Z, UTCTime.
104  "170217180154",
106  0,
107  0,
108  },
109  {
110  // No seconds.
111  "201702171801Z",
113  0,
114  0,
115  },
116  {
117  // No seconds, UTCTime.
118  "1702171801Z",
120  0,
121  0,
122  },
123  {
124  // Fractional seconds.
125  "20170217180154.001Z",
127  0,
128  0,
129  },
130  {
131  // Fractional seconds, UTCTime.
132  "170217180154.001Z",
134  0,
135  0,
136  },
137  {
138  // Timezone offset.
139  "20170217180154+0100",
141  0,
142  0,
143  },
144  {
145  // Timezone offset, UTCTime.
146  "170217180154+0100",
148  0,
149  0,
150  },
151  {
152  // Extra digits.
153  "2017021718015400Z",
155  0,
156  0,
157  },
158  {
159  // Extra digits, UTCTime.
160  "17021718015400Z",
162  0,
163  0,
164  },
165  {
166  // Non-digits.
167  "2017021718015aZ",
169  0,
170  0,
171  },
172  {
173  // Non-digits, UTCTime.
174  "17021718015aZ",
176  0,
177  0,
178  },
179  {
180  // Trailing garbage.
181  "20170217180154Zlongtrailinggarbage",
183  0,
184  0,
185  },
186  {
187  // Trailing garbage, UTCTime.
188  "170217180154Zlongtrailinggarbage",
190  0,
191  0,
192  },
193  {
194  // Swapped type.
195  "20170217180154Z",
197  0,
198  0,
199  },
200  {
201  // Swapped type.
202  "170217180154Z",
204  0,
205  0,
206  },
207  {
208  // Bad type.
209  "20170217180154Z",
211  0,
212  0,
213  },
214 };
215 
216 TEST(X509TimeTest, TestCmpTime) {
217  for (auto &test : kX509CmpTests) {
218  SCOPED_TRACE(test.data);
219 
220  ASN1_TIME t;
221 
222  memset(&t, 0, sizeof(t));
223  t.type = test.type;
224  t.data = (unsigned char*) test.data;
225  t.length = strlen(test.data);
226 
227  EXPECT_EQ(test.expected,
228  X509_cmp_time(&t, &test.cmp_time));
229  }
230 }
231 
232 TEST(X509TimeTest, TestCmpTimeCurrent) {
233  time_t now = time(NULL);
234  // Pick a day earlier and later, relative to any system clock.
235  bssl::UniquePtr<ASN1_TIME> asn1_before(ASN1_TIME_adj(NULL, now, -1, 0));
236  bssl::UniquePtr<ASN1_TIME> asn1_after(ASN1_TIME_adj(NULL, now, 1, 0));
237 
238  ASSERT_EQ(-1, X509_cmp_time(asn1_before.get(), NULL));
239  ASSERT_EQ(1, X509_cmp_time(asn1_after.get(), NULL));
240 }
now
static double now(void)
Definition: test/core/fling/client.cc:130
memset
return memset(p, 0, total)
TestData::expected
int expected
Definition: x509_time_test.cc:25
test
Definition: spinlock_test.cc:36
string.h
V_ASN1_OCTET_STRING
#define V_ASN1_OCTET_STRING
Definition: asn1.h:128
V_ASN1_GENERALIZEDTIME
#define V_ASN1_GENERALIZEDTIME
Definition: asn1.h:145
TestData::type
int type
Definition: x509_time_test.cc:22
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
TEST(X509TimeTest, TestCmpTime)
Definition: x509_time_test.cc:216
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
TestData::cmp_time
time_t cmp_time
Definition: x509_time_test.cc:23
V_ASN1_UTCTIME
#define V_ASN1_UTCTIME
Definition: asn1.h:144
ASN1_TIME_adj
#define ASN1_TIME_adj
Definition: boringssl_prefix_symbols.h:700
X509_cmp_time
#define X509_cmp_time
Definition: boringssl_prefix_symbols.h:2625
asn1_string_st
Definition: asn1.h:543
kX509CmpTests
static TestData kX509CmpTests[]
Definition: x509_time_test.cc:28
asn1.h
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
TestData::data
const char * data
Definition: x509_time_test.cc:21
x509.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:55