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