//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2023, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include "util/JsonUtils.hpp" #include "util/NameGenerator.hpp" #include #include #include #include #include #include #include #include TEST(JsonUtils, RemoveSecrets) { auto json = boost::json::parse(R"JSON({ "secret": "snoopy", "seed": "woodstock", "seed_hex": "charlie", "passphrase": "lucy" })JSON") .as_object(); auto json2 = util::removeSecret(json); EXPECT_EQ(json2.at("secret").as_string(), "*"); EXPECT_EQ(json2.at("seed").as_string(), "*"); EXPECT_EQ(json2.at("seed_hex").as_string(), "*"); EXPECT_EQ(json2.at("passphrase").as_string(), "*"); json = boost::json::parse(R"JSON({ "params": [ { "secret": "snoopy", "seed": "woodstock", "seed_hex": "charlie", "passphrase": "lucy" } ] })JSON") .as_object(); json2 = util::removeSecret(json); EXPECT_TRUE(json2.contains("params")); EXPECT_TRUE(json2.at("params").is_array()); EXPECT_TRUE(!json2.at("params").as_array().empty()); json2 = json2.at("params").as_array()[0].as_object(); EXPECT_EQ(json2.at("secret").as_string(), "*"); EXPECT_EQ(json2.at("seed").as_string(), "*"); EXPECT_EQ(json2.at("seed_hex").as_string(), "*"); EXPECT_EQ(json2.at("passphrase").as_string(), "*"); } TEST(JsonUtils, integralValueAs) { auto const expectedResultUint64 = static_cast(std::numeric_limits::max()) + 1u; auto const uint64Json = boost::json::value(expectedResultUint64); EXPECT_EQ(util::integralValueAs(uint64Json), std::numeric_limits::min()); EXPECT_EQ(util::integralValueAs(uint64Json), expectedResultUint64); EXPECT_EQ(util::integralValueAs(uint64Json), expectedResultUint64); EXPECT_EQ(util::integralValueAs(uint64Json), expectedResultUint64); auto const expectedResultInt64 = static_cast(std::numeric_limits::max()) + 1u; auto const int64Json = boost::json::value(expectedResultInt64); EXPECT_EQ(util::integralValueAs(int64Json), std::numeric_limits::min()); EXPECT_EQ(util::integralValueAs(int64Json), expectedResultInt64); EXPECT_EQ(util::integralValueAs(int64Json), expectedResultInt64); EXPECT_EQ(util::integralValueAs(int64Json), expectedResultInt64); auto const doubleJson = boost::json::value(3.14); EXPECT_THROW(util::integralValueAs(doubleJson), std::logic_error); auto const stringJson = boost::json::value("not a number"); EXPECT_THROW(util::integralValueAs(stringJson), std::logic_error); } TEST(JsonUtils, tryIntegralValueAs) { auto const expectedResultUint64 = static_cast(std::numeric_limits::max()) + 1u; auto const uint64Json = boost::json::value(expectedResultUint64); auto const expectedResultInt64 = static_cast(std::numeric_limits::max()) + 1u; auto const int64Json = boost::json::value(expectedResultInt64); auto checkHasValue = [&](boost::json::value const& jv, auto const& expectedValue) { using T = std::remove_cvref_t; auto const res = util::tryIntegralValueAs(jv); ASSERT_TRUE(res.has_value()); EXPECT_EQ(res.value(), expectedValue); }; auto checkError = [&](boost::json::value const& jv) { auto res = util::tryIntegralValueAs(jv); EXPECT_FALSE(res.has_value()); EXPECT_EQ(res.error(), "Value neither uint64 nor int64"); }; // checks for uint64Json checkHasValue(uint64Json, std::numeric_limits::min()); checkHasValue(uint64Json, static_cast(expectedResultUint64)); checkHasValue(uint64Json, static_cast(expectedResultUint64)); checkHasValue(uint64Json, expectedResultUint64); // checks for int64Json checkHasValue(int64Json, std::numeric_limits::min()); checkHasValue(int64Json, static_cast(expectedResultInt64)); checkHasValue(int64Json, expectedResultInt64); checkHasValue(int64Json, static_cast(expectedResultInt64)); // non-integral inputs checkError(boost::json::value()); checkError(boost::json::value(false)); checkError(boost::json::value(3.14)); checkError(boost::json::value("not a number")); } struct GetLedgerIndexParameterTestBundle { std::string testName; boost::json::value jv; std::expected expectedResult; }; // parameterized test cases for parameters check struct GetLedgerIndexParameterTest : ::testing::TestWithParam {}; INSTANTIATE_TEST_CASE_P( JsonUtils, GetLedgerIndexParameterTest, testing::Values( GetLedgerIndexParameterTestBundle{ .testName = "EmptyValue", .jv = boost::json::value(), .expectedResult = std::unexpected{"Value neither uint64 nor int64"} }, GetLedgerIndexParameterTestBundle{ .testName = "BoolValue", .jv = boost::json::value(false), .expectedResult = std::unexpected{"Value neither uint64 nor int64"} }, GetLedgerIndexParameterTestBundle{ .testName = "NumberValue", .jv = boost::json::value(123), .expectedResult = 123u }, GetLedgerIndexParameterTestBundle{ .testName = "StringNumberValue", .jv = boost::json::value("123"), .expectedResult = 123u }, GetLedgerIndexParameterTestBundle{ .testName = "StringNumberWithPlusSignValue", .jv = boost::json::value("+123"), .expectedResult = 123u }, GetLedgerIndexParameterTestBundle{ .testName = "StringEmptyValue", .jv = boost::json::value(""), .expectedResult = std::unexpected{"Invalid ledger index string"} }, GetLedgerIndexParameterTestBundle{ .testName = "StringWithLeadingCharsValue", .jv = boost::json::value("123invalid"), .expectedResult = std::unexpected{"Invalid ledger index string"} }, GetLedgerIndexParameterTestBundle{ .testName = "StringWithTrailingCharsValue", .jv = boost::json::value("invalid123"), .expectedResult = std::unexpected{"Invalid ledger index string"} }, GetLedgerIndexParameterTestBundle{ .testName = "StringWithLeadingAndTrailingCharsValue", .jv = boost::json::value("123invalid123"), .expectedResult = std::unexpected{"Invalid ledger index string"} }, GetLedgerIndexParameterTestBundle{ .testName = "ValidatedStringValue", .jv = boost::json::value("validated"), .expectedResult = std::unexpected{"'validated' ledger index is requested"} } ), tests::util::kNAME_GENERATOR ); TEST_P(GetLedgerIndexParameterTest, getLedgerIndexParams) { auto const& testBundle = GetParam(); auto const ledgerIndex = util::getLedgerIndex(testBundle.jv); if (testBundle.expectedResult.has_value()) { EXPECT_TRUE(ledgerIndex.has_value()); EXPECT_EQ(ledgerIndex.value(), testBundle.expectedResult.value()); } else { EXPECT_FALSE(ledgerIndex.has_value()); EXPECT_EQ(ledgerIndex.error(), testBundle.expectedResult.error()); } }