1 // Copyright 2016 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "encode_values.h"
16 
17 #include <string_view>
18 
19 #include <gtest/gtest.h>
20 #include "parse_values.h"
21 
22 BSSL_NAMESPACE_BEGIN
23 namespace der::test {
24 
TEST(EncodeValuesTest,EncodePosixTimeAsGeneralizedTime)25 TEST(EncodeValuesTest, EncodePosixTimeAsGeneralizedTime) {
26   // Fri, 24 Jun 2016 17:04:54 GMT
27   int64_t time = 1466787894;
28   GeneralizedTime generalized_time;
29   ASSERT_TRUE(EncodePosixTimeAsGeneralizedTime(time, &generalized_time));
30   EXPECT_EQ(2016, generalized_time.year);
31   EXPECT_EQ(6, generalized_time.month);
32   EXPECT_EQ(24, generalized_time.day);
33   EXPECT_EQ(17, generalized_time.hours);
34   EXPECT_EQ(4, generalized_time.minutes);
35   EXPECT_EQ(54, generalized_time.seconds);
36 }
37 
TEST(EncodeValuesTest,GeneralizedTimeToPosixTime)38 TEST(EncodeValuesTest, GeneralizedTimeToPosixTime) {
39   GeneralizedTime generalized_time;
40   generalized_time.year = 2016;
41   generalized_time.month = 6;
42   generalized_time.day = 24;
43   generalized_time.hours = 17;
44   generalized_time.minutes = 4;
45   generalized_time.seconds = 54;
46   int64_t time;
47   ASSERT_TRUE(GeneralizedTimeToPosixTime(generalized_time, &time));
48   EXPECT_EQ(1466787894, time);
49 }
50 
51 // As Posix times use BoringSSL's POSIX time routines underneath the covers
52 // these should not have any issues on 32 bit platforms.
TEST(EncodeValuesTest,GeneralizedTimeToPosixTimeAfter32BitPosixMaxYear)53 TEST(EncodeValuesTest, GeneralizedTimeToPosixTimeAfter32BitPosixMaxYear) {
54   GeneralizedTime generalized_time;
55   generalized_time.year = 2039;
56   generalized_time.month = 1;
57   generalized_time.day = 1;
58   generalized_time.hours = 0;
59   generalized_time.minutes = 0;
60   generalized_time.seconds = 0;
61   int64_t time;
62   ASSERT_TRUE(GeneralizedTimeToPosixTime(generalized_time, &time));
63 
64   generalized_time.day = 0;  // Invalid day of month should fail.
65   EXPECT_FALSE(GeneralizedTimeToPosixTime(generalized_time, &time));
66 }
67 
TEST(EncodeValuesTest,EncodeGeneralizedTime)68 TEST(EncodeValuesTest, EncodeGeneralizedTime) {
69   GeneralizedTime time;
70   time.year = 2014;
71   time.month = 12;
72   time.day = 18;
73   time.hours = 16;
74   time.minutes = 12;
75   time.seconds = 59;
76 
77   // Encode a time where no components have leading zeros.
78   uint8_t out[kGeneralizedTimeLength];
79   ASSERT_TRUE(EncodeGeneralizedTime(time, out));
80   EXPECT_EQ("20141218161259Z", bssl::BytesAsStringView(out));
81 
82   // Test bounds on all components. Note the encoding function does not validate
83   // the input is a valid time, only that it is encodable.
84   time.year = 0;
85   time.month = 0;
86   time.day = 0;
87   time.hours = 0;
88   time.minutes = 0;
89   time.seconds = 0;
90   ASSERT_TRUE(EncodeGeneralizedTime(time, out));
91   EXPECT_EQ("00000000000000Z", bssl::BytesAsStringView(out));
92 
93   time.year = 9999;
94   time.month = 99;
95   time.day = 99;
96   time.hours = 99;
97   time.minutes = 99;
98   time.seconds = 99;
99   ASSERT_TRUE(EncodeGeneralizedTime(time, out));
100   EXPECT_EQ("99999999999999Z", bssl::BytesAsStringView(out));
101 
102   time.year = 10000;
103   EXPECT_FALSE(EncodeGeneralizedTime(time, out));
104 
105   time.year = 2000;
106   time.month = 100;
107   EXPECT_FALSE(EncodeGeneralizedTime(time, out));
108 }
109 
TEST(EncodeValuesTest,EncodeUTCTime)110 TEST(EncodeValuesTest, EncodeUTCTime) {
111   GeneralizedTime time;
112   time.year = 2014;
113   time.month = 12;
114   time.day = 18;
115   time.hours = 16;
116   time.minutes = 12;
117   time.seconds = 59;
118 
119   // Encode a time where no components have leading zeros.
120   uint8_t out[kUTCTimeLength];
121   ASSERT_TRUE(EncodeUTCTime(time, out));
122   EXPECT_EQ("141218161259Z", bssl::BytesAsStringView(out));
123 
124   time.year = 2049;
125   ASSERT_TRUE(EncodeUTCTime(time, out));
126   EXPECT_EQ("491218161259Z", bssl::BytesAsStringView(out));
127 
128   time.year = 2000;
129   ASSERT_TRUE(EncodeUTCTime(time, out));
130   EXPECT_EQ("001218161259Z", bssl::BytesAsStringView(out));
131 
132   time.year = 1999;
133   ASSERT_TRUE(EncodeUTCTime(time, out));
134   EXPECT_EQ("991218161259Z", bssl::BytesAsStringView(out));
135 
136   time.year = 1950;
137   ASSERT_TRUE(EncodeUTCTime(time, out));
138   EXPECT_EQ("501218161259Z", bssl::BytesAsStringView(out));
139 
140   time.year = 2050;
141   EXPECT_FALSE(EncodeUTCTime(time, out));
142 
143   time.year = 1949;
144   EXPECT_FALSE(EncodeUTCTime(time, out));
145 
146   // Test bounds on all components. Note the encoding function does not validate
147   // the input is a valid time, only that it is encodable.
148   time.year = 2000;
149   time.month = 0;
150   time.day = 0;
151   time.hours = 0;
152   time.minutes = 0;
153   time.seconds = 0;
154   ASSERT_TRUE(EncodeUTCTime(time, out));
155   EXPECT_EQ("000000000000Z", bssl::BytesAsStringView(out));
156 
157   time.year = 1999;
158   time.month = 99;
159   time.day = 99;
160   time.hours = 99;
161   time.minutes = 99;
162   time.seconds = 99;
163   ASSERT_TRUE(EncodeUTCTime(time, out));
164   EXPECT_EQ("999999999999Z", bssl::BytesAsStringView(out));
165 
166   time.year = 2000;
167   time.month = 100;
168   EXPECT_FALSE(EncodeUTCTime(time, out));
169 }
170 
171 }  // namespace der::test
172 BSSL_NAMESPACE_END
173