From 8438ec3225d4cfea38978073685b6637fd0033a6 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 17 Jan 2021 15:53:04 -0600 Subject: [PATCH] :recycle: refactor --- bare_minimum_crypto/cpp/bmc_test.cpp | 23 +---------------------- bare_minimum_crypto/cpp/time_helpers.cpp | 22 ++++++++++++++++++++++ bare_minimum_crypto/cpp/time_helpers.h | 2 ++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/bare_minimum_crypto/cpp/bmc_test.cpp b/bare_minimum_crypto/cpp/bmc_test.cpp index 000ab94..56c369c 100644 --- a/bare_minimum_crypto/cpp/bmc_test.cpp +++ b/bare_minimum_crypto/cpp/bmc_test.cpp @@ -169,27 +169,6 @@ public: } }; -// Most tests will use a virtual clock. But just as a smoke test, -// make sure real time is realistic. - -int check_real_time () { - const auto seconds_since_epoch = get_seconds_since_epoch (); - - const auto time_of_writing = 1610844872; - if (seconds_since_epoch < time_of_writing) { - cerr << "Error: Real time is in the past." << endl; - return 1; - } - - const int64_t about_100_years = (int64_t)100 * 365 * 86400; - if (seconds_since_epoch > time_of_writing + about_100_years) { - cerr << "Error: Real time is in the far future." << endl; - return 1; - } - - return 0; -} - int check_base64 () { // I assume that char is 8 bits // char is C++ nonsense inherited from C nonsense @@ -231,7 +210,7 @@ int happy_path () { SigningKey root_key; cerr << "Root pub key " << root_key.pub_to_base64 () << endl; - if (check_real_time () != 0) { + if (test_time () != 0) { return 1; } diff --git a/bare_minimum_crypto/cpp/time_helpers.cpp b/bare_minimum_crypto/cpp/time_helpers.cpp index adee6ec..8035df3 100644 --- a/bare_minimum_crypto/cpp/time_helpers.cpp +++ b/bare_minimum_crypto/cpp/time_helpers.cpp @@ -1,6 +1,7 @@ #include "time_helpers.h" #include +#include namespace BareMinimumCrypto { using namespace std; @@ -10,4 +11,25 @@ namespace BareMinimumCrypto { const auto utc_now = system_clock::now (); return duration_cast (utc_now.time_since_epoch ()).count (); } + + // Most tests will use a virtual clock. But just as a smoke test, + // make sure real time is realistic. + + int test_time () { + const auto seconds_since_epoch = get_seconds_since_epoch (); + + const auto time_of_writing = 1610844872; + if (seconds_since_epoch < time_of_writing) { + cerr << "Error: Real time is in the past." << endl; + return 1; + } + + const int64_t about_100_years = (int64_t)100 * 365 * 86400; + if (seconds_since_epoch > time_of_writing + about_100_years) { + cerr << "Error: Real time is in the far future." << endl; + return 1; + } + + return 0; + } } diff --git a/bare_minimum_crypto/cpp/time_helpers.h b/bare_minimum_crypto/cpp/time_helpers.h index 763aa3c..5f22580 100644 --- a/bare_minimum_crypto/cpp/time_helpers.h +++ b/bare_minimum_crypto/cpp/time_helpers.h @@ -8,4 +8,6 @@ namespace BareMinimumCrypto { const int64_t about_1_year = (int64_t)365 * 86400; int64_t get_seconds_since_epoch (); + + int test_time (); }