Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add more test cases for Base58 parser #5174

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
3 changes: 2 additions & 1 deletion include/xrpl/protocol/detail/b58_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ inplace_bigint_div_rem(std::span<uint64_t> numerator, std::uint64_t divisor)
[[nodiscard]] inline std::array<std::uint8_t, 10>
b58_10_to_b58_be(std::uint64_t input)
{
constexpr std::uint64_t B_58_10 = 430804206899405824; // 58^10;
static constexpr std::uint64_t B_58_10 = 430804206899405824; // 58^10;
assert(input < B_58_10);
(void)B_58_10;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fools the compiler into thinking the variable is used, so you don't get unused variable warnings when asserts are disabled.

In file included from /home/extra/ed/dev/rippled/current/src/ripple/protocol/impl/tokens.cpp:32:
/home/extra/ed/dev/rippled/current/src/ripple/protocol/impl/b58_utils.h: In function ‘std::array<unsigned char, 10> ripple::b58_fast::detail::b58_10_to_b58_be(uint64_t)’:
/home/extra/ed/dev/rippled/current/src/ripple/protocol/impl/b58_utils.h:172:29: warning: unused variable ‘B_58_10’ [-Wunused-variable]
  172 |     constexpr std::uint64_t B_58_10 = 430804206899405824;  // 58^10;
      |                             ^~~~~~~

It's just an annoyance, but it was on the branch I had sitting around, so I included it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it would be better to use maybe_unused attribute:

static constexpr [[maybe_unused]] std::uint64_t B_58_10 = 430804206899405824; // 58^10

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I keep forgetting that exists. LOL

constexpr std::size_t resultSize = 10;
std::array<std::uint8_t, resultSize> result{};
int i = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/test/protocol/types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct types_test : public beast::unit_test::suite
auto const s = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
if (BEAST_EXPECT(parseBase58<AccountID>(s)))
BEAST_EXPECT(toBase58(*parseBase58<AccountID>(s)) == s);
{
Copy link
Collaborator

@vlntb vlntb Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] For me the intention is not very clear - is this a deliberate scope introduction or a mistake in using if statement. To make it clearer I would mark the borders of the scope of if explicitly to make clearly separate for the new scope.

if (BEAST_EXPECT(parseBase58<AccountID>(s)))
{
    BEAST_EXPECT(toBase58(*parseBase58<AccountID>(s)) == s);
}

{
    auto const s =
        "âabcd1rNxp4h8apvRis6mJf9Sh8C6iRxfrDWNâabcdAVâ\xc2\x80\xc2\x8f";
    BEAST_EXPECT(!parseBase58<AccountID>(s));
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, now that you mention it, it does look a little off. I changed the test a little bit to make that all clearer, and to remove the need to parse s twice.

auto const s =
"âabcd1rNxp4h8apvRis6mJf9Sh8C6iRxfrDWNâabcdAVâ\xc2\x80\xc2\x8f";
BEAST_EXPECT(!parseBase58<AccountID>(s));
}
}

void
Expand Down
29 changes: 29 additions & 0 deletions src/test/rpc/AccountObjects_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,35 @@ class AccountObjects_test : public beast::unit_test::suite
}
}

void
testAccountNFTs()
{
testcase("account_nfts");

using namespace jtx;
Env env(*this);

// test validation
{
auto testInvalidAccountParam = [&](auto const& param) {
Json::Value params;
params[jss::account] = param;
auto jrr = env.rpc(
"json", "account_nfts", to_string(params))[jss::result];
BEAST_EXPECT(jrr[jss::error] == "invalidParams");
BEAST_EXPECT(
jrr[jss::error_message] == "Invalid field 'account'.");
};

testInvalidAccountParam(1);
testInvalidAccountParam(1.1);
testInvalidAccountParam(true);
testInvalidAccountParam(Json::Value(Json::nullValue));
testInvalidAccountParam(Json::Value(Json::objectValue));
testInvalidAccountParam(Json::Value(Json::arrayValue));
}
}

void
testAccountObjectMarker()
{
Expand Down
Loading