Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
fail parsing dates pre-1900
Browse files Browse the repository at this point in the history
  • Loading branch information
weigon committed Jun 14, 2018
1 parent 914c1d7 commit 195718c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/http/src/http_time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ time_t time_from_rfc5322_fixdate(const char *date_buf) {
&t_m.tm_min,
&t_m.tm_sec
)) {
throw std::runtime_error("invalid date");
throw std::out_of_range("invalid date");
}

// throws out-of-range
Expand Down Expand Up @@ -124,6 +124,9 @@ time_t time_from_rfc5322_fixdate(const char *date_buf) {
{ "Nov", 10 },
{ "Dec", 11 },
}.at(mon);
if (t_m.tm_year < 1900) {
throw std::out_of_range("year too small");
}
t_m.tm_year -= 1900;

return time_from_struct_tm_utc(&t_m);
Expand Down
78 changes: 50 additions & 28 deletions src/http/tests/test_time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,70 @@
#include <WinSock2.h>
#endif

class HttpTimeTest : public ::testing::Test {
class HttpTimeParsesTest : public ::testing::Test, public ::testing::WithParamInterface<std::tuple<const char *, time_t, const char *>> {
protected:
virtual void SetUp() {
void SetUp() override {
}
};

TEST_F(HttpTimeTest, time_from_rfc5322_fixdate) {
SCOPED_TRACE("// parses a valid date");
class HttpTimeThrowsTest : public ::testing::Test, public ::testing::WithParamInterface<const char*> {
protected:
void SetUp() override {
}
};


TEST_P(HttpTimeParsesTest, time_from_rfc5322_fixdate) {
EXPECT_NO_THROW(
EXPECT_THAT(time_from_rfc5322_fixdate(std::get<0>(GetParam())), ::testing::Eq(std::get<1>(GetParam()))));

char date_buf[30];
EXPECT_NO_THROW(
EXPECT_THAT(time_from_rfc5322_fixdate("Thu, 31 May 2018 15:18:20 GMT"), ::testing::Eq(1527779900)));
SCOPED_TRACE("// ignores whitespace");
EXPECT_THAT(time_to_rfc5322_fixdate(std::get<1>(GetParam()), date_buf, sizeof(date_buf)), ::testing::Eq(29)));

// equal, if you ignore whitespace
EXPECT_NO_THROW(
EXPECT_THAT(time_from_rfc5322_fixdate("Thu, 31 May 2018 15:18:20 GMT"), ::testing::Eq(1527779900)));
EXPECT_THAT(date_buf, ::testing::StrEq(std::get<2>(GetParam()))));
}

SCOPED_TRACE("// throws at invalid weekday");
EXPECT_THROW(time_from_rfc5322_fixdate("Tho, 31 May 2018 15:18:20 GMT"), std::exception);
INSTANTIATE_TEST_CASE_P(HttpTimeParses,
HttpTimeParsesTest,
::testing::Values(
// parses a valid date
std::make_tuple("Thu, 31 May 2018 15:18:20 GMT", static_cast<time_t>(1527779900), "Thu, 31 May 2018 15:18:20 GMT"),
// whitespace get ignored
std::make_tuple("Thu, 31 May 2018 15:18:20 GMT", static_cast<time_t>(1527779900), "Thu, 31 May 2018 15:18:20 GMT"),

// other date
std::make_tuple("Thu, 31 May 2018 05:18:20 GMT", static_cast<time_t>(1527743900), "Thu, 31 May 2018 05:18:20 GMT")
));

TEST_P(HttpTimeThrowsTest, time_from_rfc5322_fixdate_p) {
SCOPED_TRACE("// year before 1900");
EXPECT_THROW(time_from_rfc5322_fixdate(GetParam()), std::out_of_range);
}

SCOPED_TRACE("// throws at invalid month");
EXPECT_THROW(time_from_rfc5322_fixdate("Thu, 31 Mai 2018 15:18:20 GMT"), std::exception);
INSTANTIATE_TEST_CASE_P(HttpTimeThrows,
HttpTimeThrowsTest,
::testing::Values(
// year too small
"Thu, 31 May 1899 15:18:20 GMT",

SCOPED_TRACE("// throws at short year");
EXPECT_THROW(time_from_rfc5322_fixdate("Thu, 31 Mai 201 15:18:20 GMT"), std::exception);
// wrong timezone
"Thu, 31 May 2018 5:18:20 UTC",

SCOPED_TRACE("// throws at long year");
EXPECT_THROW(time_from_rfc5322_fixdate("Thu, 31 Mai 20188 15:18:20 GMT"), std::exception);
// throws at invalid weekday");
"Tho, 31 May 2018 15:18:20 GMT",

SCOPED_TRACE("// throws at wrong timezone");
EXPECT_THROW(time_from_rfc5322_fixdate("Thu, 31 Mai 2018 15:18:20 UTC"), std::exception);
// throws at invalid month");
"Thu, 31 Mai 2018 15:18:20 GMT",

SCOPED_TRACE("// throws at short hour");
EXPECT_THROW(time_from_rfc5322_fixdate("Thu, 31 Mai 2018 5:18:20 UTC"), std::exception);
// throws at short year");
"Thu, 31 May 201 15:18:20 GMT",

SCOPED_TRACE("// throws at short hour");
EXPECT_NO_THROW(
EXPECT_THAT(time_from_rfc5322_fixdate("Thu, 31 May 2018 05:18:20 GMT"), ::testing::Eq(1527743900)));
// throws at long year
"Thu, 31 May 20188 15:18:20 GMT"));

char date_buf[30];
EXPECT_NO_THROW(
EXPECT_THAT(time_to_rfc5322_fixdate(1527779900, date_buf, sizeof(date_buf)), ::testing::Eq(29)));
EXPECT_NO_THROW(
EXPECT_THAT(date_buf, ::testing::StrEq("Thu, 31 May 2018 15:18:20 GMT")));
}

int main(int argc, char *argv[])
{
Expand Down

0 comments on commit 195718c

Please sign in to comment.