Debian Codegraph @

podofo/test/unit/DateTest.cpp

raw
1 // SPDX-FileCopyrightText: 2012 Dominik Seichter <[email protected]>
2 // SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]>
3 // SPDX-License-Identifier: MIT-0
4
5 #include <PdfTest.h>
6
7 #include <date/date.h>
8
9 using namespace std;
10 using namespace PoDoFo;
11
12 static void deconstruct(const PdfDate& date, short& y, unsigned char& m, unsigned char& d,
13 unsigned char& h, unsigned char& M, unsigned char& s);
14 static void deconstruct(const PdfDate& date, date::year_month_day& ymd, date::hh_mm_ss<chrono::seconds>& time);
15
16 static void checkExpected(const string_view& datestr, bool expectedValid)
17 {
18 auto now = PdfDate::LocalNow();
19 bool valid = PdfDate::TryParse(datestr, now);
20
21 if (datestr.empty())
22 {
23 INFO("NULL");
24 }
25 else
26 {
27 INFO(datestr);
28 }
29
30 REQUIRE(valid == expectedValid);
31 }
32
33 TEST_CASE("TestCreateDateFromString")
34 {
35 checkExpected({ }, false); // default string_view (data() == nullptr)
36 checkExpected(""sv, false); // empty string_view (data() != nullptr)
37 checkExpected("D:2012", true);
38 checkExpected("D:20120", true);
39 checkExpected("D:201201", true);
40 checkExpected("D:201213", false);
41 checkExpected("D:2012010", true);
42 checkExpected("D:20120101", true);
43 checkExpected("D:201201012", true);
44 checkExpected("D:20120132", false);
45 checkExpected("D:2012010123", true);
46 checkExpected("D:2012010125", false);
47 checkExpected("D:20120101235", true);
48 checkExpected("D:201201012359", true);
49 checkExpected("D:2012010123595", true);
50 checkExpected("D:20120101235959", true);
51 checkExpected("D:20120120135959Z", true);
52 checkExpected("D:20120120135959Z00", true);
53 checkExpected("D:20120120135959Z00'", true);
54 checkExpected("D:20120120135959Z00'00", true);
55 checkExpected("D:20120120135959Z00'00'", true);
56 checkExpected("D:20120120135959+0", true);
57 checkExpected("D:20120120135959+00", true);
58 checkExpected("D:20120120135959+00'", true);
59 checkExpected("D:20120120135959+00'0", true);
60 checkExpected("D:20120120135959+00'00", true);
61 checkExpected("D:20120120135959-00'00", true);
62
63 checkExpected("INVALID", false);
64
65 // Regression: non-null-terminated inputs must not read out of bounds.
66 // The tryParse() implementation previously used const char* from
67 // string_view::data() and checked for '\0' terminators, but string_view
68 // is not guaranteed to be null-terminated. These short inputs (from
69 // fuzzer crash artifacts) triggered ASan heap-buffer-overflow.
70 checkExpected("7+"sv, true); // year 7, zone shift '+'
71 checkExpected("7"sv, true); // year 7, no zone shift
72 checkExpected("+"sv, true); // year 0, zone shift '+'
73 checkExpected("-"sv, true); // year 0, zone shift '-'
74 checkExpected("."sv, false); // not a digit or 'D'
75 checkExpected("D:"sv, true); // "D:" with no year digits (year 0)
76 }
77
78 TEST_CASE("TestParseW3CShortInputs")
79 {
80 // Regression: tryParseW3C had the same out-of-bounds read bug as tryParse
81 // when given non-null-terminated string_view inputs.
82 auto checkW3C = [](const string_view& datestr, bool expectedValid)
83 {
84 PdfDate date;
85 bool valid = PdfDate::TryParseW3C(datestr, date);
86 INFO(datestr);
87 REQUIRE(valid == expectedValid);
88 };
89
90 checkW3C(""sv, false);
91 checkW3C("2"sv, true); // partial year
92 checkW3C("2024"sv, true); // year only
93 checkW3C("2024-"sv, false); // trailing separator, no month digits
94 checkW3C("2024-01"sv, true);
95 checkW3C("2024-01-15T10:30Z"sv, true);
96 checkW3C("2024-01-15T10:30+05:30"sv, true);
97 checkW3C("+"sv, false); // not a digit
98 checkW3C("-"sv, false); // not a digit
99 checkW3C("."sv, false); // not a digit
100 }
101
102 TEST_CASE("TestRoundTrip")
103 {
104 auto testRoundTrip = [](const string_view& dateStr1)
105 {
106 auto date1 = PdfDate::Parse(dateStr1);
107 string dateStr2 = (string)date1.ToString().GetString();
108 auto date2 = PdfDate::Parse(dateStr2);
109 REQUIRE(dateStr1 == dateStr2);
110 REQUIRE(date1 == date2);
111 };
112
113 testRoundTrip("D:20221217220858+01'00'");
114 testRoundTrip("D:20221217220858");
115 }
116
117 TEST_CASE("TestNoZoneShift")
118 {
119 auto date1 = PdfDate::Parse("D:20221217220858+00'00'");
120 auto date2 = PdfDate::Parse("D:20221217220858");
121 // No zone shift should be equivalent to UTC
122 REQUIRE(date1.GetSecondsFromEpoch() == date2.GetSecondsFromEpoch());
123 }
124
125 TEST_CASE("TestAdditional")
126 {
127 struct name_date
128 {
129 string name;
130 string date;
131 };
132
133 name_date data[] = {
134 {"sample from pdf_reference_1_7.pdf", "D:199812231952-08'00'"},
135 // UTC 1998-12-24 03:52:00
136 {"all fields set", "D:20201223195200-08'00'"}, // UTC 2020-12-03:52:00
137 {"set year", "D:2020"}, // UTC 2020-01-01 00:00:00
138 {"set year, month", "D:202001"}, // UTC 2020-01-01 00:00:00
139 {"set year, month, day", "D:20200101"}, // UTC 202001-01 00:00:00
140 {"only year and timezone set", "D:2020-08'00'"}, // UTC 2020-01-01 08:00:00
141 {"berlin", "D:20200315120820+01'00'"}, // UTC 2020-03-15 11:08:20
142 };
143
144 for (auto& d : data)
145 {
146 INFO(utls::Format("Parse {}", d.name));
147 checkExpected(d.date, true);
148 }
149 }
150
151 TEST_CASE("TestParseDateValid")
152 {
153 // (Sun Feb 05 2012 13:24:56 GMT+0000)
154 auto date = PdfDate::Parse("D:20120205132456");
155
156 short y; unsigned char m, d, h, M, s;
157 deconstruct(date, y, m, d, h, M, s);
158
159 INFO("Year"); REQUIRE(y == 2012);
160 INFO("Month"); REQUIRE(m == 2);
161 INFO("Day"); REQUIRE(d == 5);
162 INFO("Hour"); REQUIRE(h == 13);
163 INFO("Minute"); REQUIRE(M == 24);
164 INFO("Second"); REQUIRE(s == 56);
165
166 unsigned timeExpected = 1328448296;
167 REQUIRE(date.GetSecondsFromEpoch().count() == timeExpected);
168
169 PdfDate date2 = PdfDate::Parse("D:20120205192456+06'00'");
170 REQUIRE(date2.GetSecondsFromEpoch().count() == timeExpected);
171
172 PdfDate date3 = PdfDate::Parse("D:20120205072456-06'00'");
173 REQUIRE(date3.GetSecondsFromEpoch().count() == timeExpected);
174
175 PdfDate date4 = PdfDate::Parse("D:20120205175456+04'30'");
176 REQUIRE(date4.GetSecondsFromEpoch().count() == timeExpected);
177 }
178
179
180 static void deconstruct(const PdfDate& date, short& y, unsigned char& m, unsigned char& d,
181 unsigned char& h, unsigned char& M, unsigned char& s)
182 {
183
184 date::year_month_day ymd;
185 date::hh_mm_ss<chrono::seconds> time;
186 deconstruct(date, ymd, time);
187
188 y = (short)(int)ymd.year();
189 m = (unsigned char)(unsigned)ymd.month();
190 d = (unsigned char)(unsigned)ymd.day();
191 h = (unsigned char)time.hours().count();
192 M = (unsigned char)time.minutes().count();
193 s = (unsigned char)time.seconds().count();
194 }
195
196 void deconstruct(const PdfDate& date, date::year_month_day& ymd, date::hh_mm_ss<chrono::seconds>& time)
197 {
198 auto minutesFromUtc = date.GetMinutesFromUtc();
199 if (minutesFromUtc.has_value())
200 {
201 // Assume sys time
202 auto secondsFromEpoch = (date::sys_seconds)(date.GetSecondsFromEpoch() + *minutesFromUtc);
203 auto dp = date::floor<date::days>(secondsFromEpoch);
204 ymd = date::year_month_day(dp);
205 time = date::hh_mm_ss<chrono::seconds>(chrono::floor<chrono::seconds>(secondsFromEpoch - dp));
206 }
207 else
208 {
209 // Assume local time
210 auto secondsFromEpoch = (date::local_seconds)date.GetSecondsFromEpoch();
211 auto dp = date::floor<date::days>(secondsFromEpoch);
212 ymd = date::year_month_day(dp);
213 time = date::hh_mm_ss<chrono::seconds>(chrono::floor<chrono::seconds>(secondsFromEpoch - dp));
214 }
215 }