aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Warta <[email protected]>2015-07-16 15:44:27 +0200
committerSimon Warta <[email protected]>2015-07-16 15:44:27 +0200
commit1b1f3c936eb83ca2edeccb664ebcb1fecbff64ef (patch)
tree6edebd24be2a611fddd94a9391c21595af484791
parent9bca04a5999060f098221615ab4ce5f89ea67c8a (diff)
parente9ef1088bced02c0ed10a460dc0fbc296b999a07 (diff)
Merge pull request #200 from webmaster128/time-test
Add date calendar_point to timepoint test of year 2100
-rw-r--r--src/lib/utils/calendar.cpp26
-rw-r--r--src/lib/utils/calendar.h19
-rw-r--r--src/tests/catchy/test_utils.cpp69
-rw-r--r--src/tests/unit_x509.cpp4
4 files changed, 87 insertions, 31 deletions
diff --git a/src/lib/utils/calendar.cpp b/src/lib/utils/calendar.cpp
index b578f6be9..0ef8be356 100644
--- a/src/lib/utils/calendar.cpp
+++ b/src/lib/utils/calendar.cpp
@@ -1,6 +1,7 @@
/*
* Calendar Functions
* (C) 1999-2010 Jack Lloyd
+* (C) 2015 Simon Warta (Kullo GmbH)
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -8,6 +9,8 @@
#include <botan/calendar.h>
#include <botan/exceptn.h>
#include <ctime>
+#include <sstream>
+#include <iomanip>
namespace Botan {
@@ -38,6 +41,13 @@ std::chrono::system_clock::time_point calendar_point::to_std_timepoint()
if (year < 1900)
throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years before 1990.");
+ // 32 bit time_t ends at January 19, 2038
+ // https://msdn.microsoft.com/en-us/library/2093ets1.aspx
+ // For consistency reasons, throw after 2037 as long as
+ // no other implementation is available.
+ if (year > 2037)
+ throw Invalid_Argument("calendar_point::to_std_timepoint() does not support years after 2037.");
+
std::tm tm;
tm.tm_sec = seconds;
tm.tm_min = minutes;
@@ -52,10 +62,26 @@ std::chrono::system_clock::time_point calendar_point::to_std_timepoint()
#define timegm _mkgmtime
#endif
std::time_t tt = timegm(&tm);
+ if (tt == -1)
+ throw Invalid_Argument("calendar_point couldn't be converted: " + to_string());
return std::chrono::system_clock::from_time_t(tt);
}
+std::string calendar_point::to_string() const
+ {
+ // desired format: <YYYY>-<MM>-<dd>T<HH>:<mm>:<ss>
+ std::stringstream output;
+ {
+ using namespace std;
+ output << setfill('0')
+ << setw(4) << year << "-" << setw(2) << month << "-" << setw(2) << day
+ << "T"
+ << setw(2) << hour << ":" << setw(2) << minutes << ":" << setw(2) << seconds;
+ }
+ return output.str();
+ }
+
calendar_point calendar_value(
const std::chrono::system_clock::time_point& time_point)
diff --git a/src/lib/utils/calendar.h b/src/lib/utils/calendar.h
index 4f882e49f..aa34d8448 100644
--- a/src/lib/utils/calendar.h
+++ b/src/lib/utils/calendar.h
@@ -1,6 +1,7 @@
/*
* Calendar Functions
* (C) 1999-2009 Jack Lloyd
+* (C) 2015 Simon Warta (Kullo GmbH)
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -22,21 +23,21 @@ struct BOTAN_DLL calendar_point
u32bit year;
/** The month, 1 through 12 for Jan to Dec */
- byte month;
+ u32bit month;
/** The day of the month, 1 through 31 (or 28 or 30 based on month */
- byte day;
+ u32bit day;
/** Hour in 24-hour form, 0 to 23 */
- byte hour;
+ u32bit hour;
/** Minutes in the hour, 0 to 60 */
- byte minutes;
+ u32bit minutes;
/** Seconds in the minute, 0 to 60, but might be slightly
larger to deal with leap seconds on some systems
*/
- byte seconds;
+ u32bit seconds;
/**
* Initialize a calendar_point
@@ -47,13 +48,19 @@ struct BOTAN_DLL calendar_point
* @param min the minute
* @param sec the second
*/
- calendar_point(u32bit y, byte mon, byte d, byte h, byte min, byte sec) :
+ calendar_point(u32bit y, u32bit mon, u32bit d, u32bit h, u32bit min, u32bit sec) :
year(y), month(mon), day(d), hour(h), minutes(min), seconds(sec) {}
/**
* Returns an STL timepoint object
*/
std::chrono::system_clock::time_point to_std_timepoint();
+
+ /**
+ * Returns a human readable string of the struct's components.
+ * Formatting might change over time. Currently it is RFC339 'iso-date-time'.
+ */
+ std::string to_string() const;
};
/**
diff --git a/src/tests/catchy/test_utils.cpp b/src/tests/catchy/test_utils.cpp
index 41eef3e45..ad4191e3c 100644
--- a/src/tests/catchy/test_utils.cpp
+++ b/src/tests/catchy/test_utils.cpp
@@ -57,29 +57,45 @@ TEST_CASE("round_up invalid input", "[utils]")
TEST_CASE("calendar_point constructor works", "[utils]")
{
- auto point1 = calendar_point(1988, 04, 23, 14, 37, 28);
- CHECK(( point1.year == 1988 ));
- CHECK(( point1.month == 4 ));
- CHECK(( point1.day == 23 ));
- CHECK(( point1.hour == 14 ));
- CHECK(( point1.minutes == 37 ));
- CHECK(( point1.seconds == 28 ));
-
- auto point2 = calendar_point(1800, 01, 01, 0, 0, 0);
- CHECK(( point2.year == 1800 ));
- CHECK(( point2.month == 1 ));
- CHECK(( point2.day == 1 ));
- CHECK(( point2.hour == 0 ));
- CHECK(( point2.minutes == 0 ));
- CHECK(( point2.seconds == 0 ));
-
- auto point3 = calendar_point(2037, 12, 31, 24, 59, 59);
- CHECK(( point3.year == 2037 ));
- CHECK(( point3.month == 12 ));
- CHECK(( point3.day == 31 ));
- CHECK(( point3.hour == 24 ));
- CHECK(( point3.minutes == 59 ));
- CHECK(( point3.seconds == 59 ));
+ {
+ auto point1 = calendar_point(1988, 04, 23, 14, 37, 28);
+ CHECK(( point1.year == 1988 ));
+ CHECK(( point1.month == 4 ));
+ CHECK(( point1.day == 23 ));
+ CHECK(( point1.hour == 14 ));
+ CHECK(( point1.minutes == 37 ));
+ CHECK(( point1.seconds == 28 ));
+ }
+
+ {
+ auto point2 = calendar_point(1800, 01, 01, 0, 0, 0);
+ CHECK(( point2.year == 1800 ));
+ CHECK(( point2.month == 1 ));
+ CHECK(( point2.day == 1 ));
+ CHECK(( point2.hour == 0 ));
+ CHECK(( point2.minutes == 0 ));
+ CHECK(( point2.seconds == 0 ));
+ }
+
+ {
+ auto point = calendar_point(2037, 12, 31, 24, 59, 59);
+ CHECK(( point.year == 2037 ));
+ CHECK(( point.month == 12 ));
+ CHECK(( point.day == 31 ));
+ CHECK(( point.hour == 24 ));
+ CHECK(( point.minutes == 59 ));
+ CHECK(( point.seconds == 59 ));
+ }
+
+ {
+ auto point = calendar_point(2100, 5, 1, 0, 0, 0);
+ CHECK(( point.year == 2100 ));
+ CHECK(( point.month == 5 ));
+ CHECK(( point.day == 1 ));
+ CHECK(( point.hour == 0 ));
+ CHECK(( point.minutes == 0 ));
+ CHECK(( point.seconds == 0 ));
+ }
}
TEST_CASE("calendar_point to stl timepoint and back", "[utils]")
@@ -95,6 +111,7 @@ TEST_CASE("calendar_point to stl timepoint and back", "[utils]")
CHECK(( out.seconds == 28 ));
}
+ SECTION("latest possible time point")
{
auto in = calendar_point(2037, 12, 31, 23, 59, 59);
auto out = calendar_value(in.to_std_timepoint());
@@ -111,4 +128,10 @@ TEST_CASE("calendar_point to stl timepoint and back", "[utils]")
auto in = calendar_point(1800, 01, 01, 0, 0, 0);
CHECK_THROWS( in.to_std_timepoint() );
}
+
+ SECTION("year too late")
+ {
+ auto in = calendar_point(2038, 01, 01, 0, 0, 0);
+ CHECK_THROWS( in.to_std_timepoint() );
+ }
}
diff --git a/src/tests/unit_x509.cpp b/src/tests/unit_x509.cpp
index 95890b9e9..677ccedd3 100644
--- a/src/tests/unit_x509.cpp
+++ b/src/tests/unit_x509.cpp
@@ -172,11 +172,11 @@ size_t test_x509()
/* Sign the requests to create the certs */
X509_Certificate user1_cert =
ca.sign_request(user1_req, rng,
- from_date(2008, 01, 01), from_date(2100, 01, 01));
+ from_date(2008, 01, 01), from_date(2033, 01, 01));
X509_Certificate user2_cert = ca.sign_request(user2_req, rng,
from_date(2008, 01, 01),
- from_date(2100, 01, 01));
+ from_date(2033, 01, 01));
X509_CRL crl1 = ca.new_crl(rng);
/* Verify the certs */