Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 219 additions & 88 deletions src/test/app/HostFuncImpl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1906,30 +1906,88 @@ struct HostFuncImpl_test : public beast::unit_test::suite
}
}

// env test logs don't check severity, so we add it.
class SuiteJournalSink2 : public SuiteJournalSink
Copy link
Collaborator

Choose a reason for hiding this comment

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

You don't need to rewrite this - you can use test::StreamSink.

Copy link
Collaborator Author

@oleks-rip oleks-rip Dec 10, 2025

Choose a reason for hiding this comment

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

No, StreamSink is not from base Sink hierarchy

{
public:
SuiteJournalSink2(
std::string const& partition,
beast::severities::Severity threshold,
beast::unit_test::suite& suite)
: SuiteJournalSink(partition, threshold, suite)
{
}
inline bool
active(beast::severities::Severity level) const override
{
return level >= threshold();
}
};

class SuiteLogs2 : public Logs
{
beast::unit_test::suite& suite_;

public:
explicit SuiteLogs2(beast::unit_test::suite& suite)
: Logs(beast::severities::kError), suite_(suite)
{
}
~SuiteLogs2() override = default;
std::unique_ptr<beast::Journal::Sink>
makeSink(
std::string const& partition,
beast::severities::Severity threshold) override
{
return std::make_unique<SuiteJournalSink2>(
partition, threshold, suite_);
}
};

void
testTrace()
{
testcase("trace");
using namespace test::jtx;

Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + data.size());
std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + data.size());

auto const resultHex = hfs.trace(msg, slice, true);
BEAST_EXPECT(resultHex.has_value());
BEAST_EXPECT(resultHex.value() == msg.size() + data.size() * 2);
}

auto const resultHex = hfs.trace(msg, slice, true);
BEAST_EXPECT(resultHex.has_value());
BEAST_EXPECT(resultHex.value() == msg.size() + data.size() * 2);
{
// logs disabled (trace < error)
auto logs = std::make_unique<SuiteLogs2>(*this);
Env env(*this, envconfig(), std::move(logs));
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "test trace";
std::string data = "abc";
auto const slice = Slice(data.data(), data.size());
auto const result = hfs.trace(msg, slice, false);
BEAST_EXPECT(result && *result == msg.size() + data.size());
}
}

void
Expand All @@ -1938,19 +1996,38 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceNum");
using namespace test::jtx;

Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + sizeof(num));
std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(result.value() == msg.size() + sizeof(num));
}

{
// logs disabled
auto logs = std::make_unique<SuiteLogs2>(*this);
Env env(*this, envconfig(), std::move(logs));
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace number";
int64_t num = 123456789;
auto const result = hfs.traceNum(msg, num);
BEAST_EXPECT(result && *result == msg.size() + sizeof(int64_t));
}
}

void
Expand All @@ -1959,22 +2036,39 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceAccount");
using namespace test::jtx;

Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace account";
// Valid account
{
auto const result = hfs.traceAccount(msg, env.master.id());
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() == msg.size() + env.master.id().size());
}
}

std::string msg = "trace account";
// Valid account
{
// logs disabled
auto logs = std::make_unique<SuiteLogs2>(*this);
Env env(*this, envconfig(), std::move(logs));
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
std::string msg = "trace account";
auto const result = hfs.traceAccount(msg, env.master.id());
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() ==
msg.size() + toBase58(env.master.id()).size());
BEAST_EXPECT(
result && *result == msg.size() + env.master.id().size());
}
}

Expand All @@ -1984,46 +2078,60 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("traceAmount");
using namespace test::jtx;

Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace amount";
STAmount amount = XRP(12345);
{
auto const result = hfs.traceAmount(msg, amount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() == msg.size() + amount.getFullText().size());
}
std::string msg = "trace amount";
STAmount amount = XRP(12345);
{
auto const result = hfs.traceAmount(msg, amount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(*result == msg.size());
}

// IOU amount
Account const alice("alice");
env.fund(XRP(1000), alice);
env.close();
STAmount iouAmount = env.master["USD"](100);
{
auto const result = hfs.traceAmount(msg, iouAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() ==
msg.size() + iouAmount.getFullText().size());
// IOU amount
Account const alice("alice");
env.fund(XRP(1000), alice);
env.close();
STAmount iouAmount = env.master["USD"](100);
{
auto const result = hfs.traceAmount(msg, iouAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(*result == msg.size());
}

// MPT amount
{
auto const mptId = makeMptID(42, env.master.id());
Asset mptAsset = Asset(mptId);
STAmount mptAmount(mptAsset, 123456);
auto const result = hfs.traceAmount(msg, mptAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(*result == msg.size());
}
}

// MPT amount
{
auto const mptId = makeMptID(42, env.master.id());
Asset mptAsset = Asset(mptId);
STAmount mptAmount(mptAsset, 123456);
auto const result = hfs.traceAmount(msg, mptAmount);
if (BEAST_EXPECT(result.has_value()))
BEAST_EXPECT(
result.value() ==
msg.size() + mptAmount.getFullText().size());
// logs disabled
auto logs = std::make_unique<SuiteLogs2>(*this);
Env env(*this, envconfig(), std::move(logs));
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace amount";
STAmount amount = XRP(12345);
auto const result = hfs.traceAmount(msg, amount);
BEAST_EXPECT(result && *result == msg.size());
}
}

Expand Down Expand Up @@ -2058,28 +2166,51 @@ struct HostFuncImpl_test : public beast::unit_test::suite
testcase("FloatTrace");
using namespace test::jtx;

Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);
{
Env env{*this};
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);
auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace float";
std::string msg = "trace float";

{
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result &&
*result ==
msg.size() + 14 /* error msg size*/ + invalid.size() * 2);
{
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result &&
*result == msg.size() + makeSlice(invalid).size());
}

{
auto const result = hfs.traceFloat(msg, makeSlice(floatMaxExp));
BEAST_EXPECT(
result &&
*result == msg.size() + makeSlice(floatMaxExp).size());
}
}

{
auto const result = hfs.traceFloat(msg, makeSlice(floatMaxExp));
BEAST_EXPECT(
result && *result == msg.size() + 19 /* string represenation*/);
// logs disabled
auto logs = std::make_unique<SuiteLogs2>(*this);
Env env(*this, envconfig(), std::move(logs));
OpenView ov{*env.current()};
ApplyContext ac = createApplyContext(env, ov);

auto const dummyEscrow =
keylet::escrow(env.master, env.seq(env.master));
WasmHostFunctionsImpl hfs(ac, dummyEscrow);

std::string msg = "trace float";

{
auto const result = hfs.traceFloat(msg, makeSlice(invalid));
BEAST_EXPECT(
result &&
*result == msg.size() + makeSlice(invalid).size());
}
}
}

Expand Down
Loading
Loading