diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -70,6 +70,7 @@ ../wallet/test/init_tests.cpp ../wallet/test/ismine_tests.cpp ../wallet/test/psbt_wallet_tests.cpp + ../wallet/test/rpc_util_tests.cpp ../wallet/test/scriptpubkeyman_tests.cpp ../wallet/test/wallet_tests.cpp ../wallet/test/walletdb_tests.cpp diff --git a/src/test/fuzz/CMakeLists.txt b/src/test/fuzz/CMakeLists.txt --- a/src/test/fuzz/CMakeLists.txt +++ b/src/test/fuzz/CMakeLists.txt @@ -129,7 +129,6 @@ netaddress p2p_transport_deserializer parse_hd_keypath - parse_iso8601 parse_numbers parse_script parse_univalue @@ -164,6 +163,8 @@ tx_in tx_out txrequest + # TODO: move the following to src/wallet/test/fuzz/CMakeLists.txt when backporting core#23334 + ../../wallet/test/fuzz/parse_iso8601 ) add_deserialize_fuzz_targets( diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -197,14 +197,10 @@ test_replaceall("'", "foo", "A test \"%s\" string foo%sfoo."); } -BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime) { +BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime) { BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z"); BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z"); - - BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0); - BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); - BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777); } BOOST_AUTO_TEST_CASE(util_FormatISO8601Date) { diff --git a/src/util/time.h b/src/util/time.h --- a/src/util/time.h +++ b/src/util/time.h @@ -100,7 +100,6 @@ */ std::string FormatISO8601DateTime(int64_t nTime); std::string FormatISO8601Date(int64_t nTime); -int64_t ParseISO8601DateTime(const std::string &str); /** * Convert milliseconds to a struct timeval for e.g. select. diff --git a/src/util/time.cpp b/src/util/time.cpp --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -12,8 +12,6 @@ #include #include -#include - #include #include @@ -144,22 +142,6 @@ ts.tm_mday); } -int64_t ParseISO8601DateTime(const std::string &str) { - static const boost::posix_time::ptime epoch = - boost::posix_time::from_time_t(0); - static const std::locale loc( - std::locale::classic(), - new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ")); - std::istringstream iss(str); - iss.imbue(loc); - boost::posix_time::ptime ptime(boost::date_time::not_a_date_time); - iss >> ptime; - if (ptime.is_not_a_date_time() || epoch > ptime) { - return 0; - } - return (ptime - epoch).total_seconds(); -} - struct timeval MillisToTimeval(int64_t nTimeout) { struct timeval timeout; timeout.tv_sec = nTimeout / 1000; diff --git a/src/wallet/rpc/util.h b/src/wallet/rpc/util.h --- a/src/wallet/rpc/util.h +++ b/src/wallet/rpc/util.h @@ -44,4 +44,6 @@ LoadWalletHelper(WalletContext &context, UniValue load_on_start_param, const std::string wallet_name); +int64_t ParseISO8601DateTime(const std::string &str); + #endif // BITCOIN_WALLET_RPC_UTIL_H diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp --- a/src/wallet/rpc/util.cpp +++ b/src/wallet/rpc/util.cpp @@ -12,11 +12,29 @@ #include +#include + static const std::string WALLET_ENDPOINT_BASE = "/wallet/"; const std::string HELP_REQUIRING_PASSPHRASE{ "\nRequires wallet passphrase to be set with walletpassphrase call if " "wallet is encrypted.\n"}; +int64_t ParseISO8601DateTime(const std::string &str) { + static const boost::posix_time::ptime epoch = + boost::posix_time::from_time_t(0); + static const std::locale loc( + std::locale::classic(), + new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ")); + std::istringstream iss(str); + iss.imbue(loc); + boost::posix_time::ptime ptime(boost::date_time::not_a_date_time); + iss >> ptime; + if (ptime.is_not_a_date_time() || epoch > ptime) { + return 0; + } + return (ptime - epoch).total_seconds(); +} + bool GetAvoidReuseFlag(const CWallet *const pwallet, const UniValue ¶m) { bool can_avoid_reuse = pwallet->IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE); bool avoid_reuse = param.isNull() ? can_avoid_reuse : param.get_bool(); diff --git a/src/test/fuzz/parse_iso8601.cpp b/src/wallet/test/fuzz/parse_iso8601.cpp rename from src/test/fuzz/parse_iso8601.cpp rename to src/wallet/test/fuzz/parse_iso8601.cpp --- a/src/test/fuzz/parse_iso8601.cpp +++ b/src/wallet/test/fuzz/parse_iso8601.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include diff --git a/src/wallet/test/rpc_util_tests.cpp b/src/wallet/test/rpc_util_tests.cpp new file mode 100644 --- /dev/null +++ b/src/wallet/test/rpc_util_tests.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +BOOST_AUTO_TEST_SUITE(rpc_util_tests) + +BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime) { + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z"), 946684801); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z"), 4133980799); +} + +BOOST_AUTO_TEST_SUITE_END()