Skip to content

Commit

Permalink
Move ledger_entry tests to LedgerRPC_test.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed May 6, 2024
1 parent 017fc2c commit b67d3c3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 79 deletions.
79 changes: 0 additions & 79 deletions src/test/app/Oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,83 +678,6 @@ struct Oracle_test : public beast::unit_test::suite
}
}

void
testInvalidLedgerEntry()
{
testcase("Invalid Ledger Entry");
using namespace jtx;

Env env(*this);
Account const owner("owner");
env.fund(XRP(1'000), owner);
Oracle oracle(env, {.owner = owner});

// Malformed document id
std::vector<AnyValue> invalid = {"%None%", -1, 1.2, "", "Invalid"};
for (auto const& v : invalid)
{
auto const res = Oracle::ledgerEntry(env, owner, v);
BEAST_EXPECT(res[jss::error].asString() == "malformedDocumentID");
}
// Missing document id
auto res = Oracle::ledgerEntry(env, owner, std::nullopt);
BEAST_EXPECT(res[jss::error].asString() == "malformedRequest");

// Missing account
res = Oracle::ledgerEntry(env, std::nullopt, 1);
BEAST_EXPECT(res[jss::error].asString() == "malformedRequest");

// Malformed account
std::string malfAccount = to_string(owner.id());
malfAccount.replace(10, 1, 1, '!');
res = Oracle::ledgerEntry(env, malfAccount, 1);
BEAST_EXPECT(res[jss::error].asString() == "malformedAddress");
}

void
testLedgerEntry()
{
testcase("Ledger Entry");
using namespace jtx;

Env env(*this);
std::vector<AccountID> accounts;
std::vector<std::uint32_t> oracles;
for (int i = 0; i < 10; ++i)
{
Account const owner(std::string("owner") + std::to_string(i));
env.fund(XRP(1'000), owner);
// different accounts can have the same asset pair
Oracle oracle(env, {.owner = owner, .documentID = i});
accounts.push_back(owner.id());
oracles.push_back(oracle.documentID());
// same account can have different asset pair
Oracle oracle1(env, {.owner = owner, .documentID = i + 10});
accounts.push_back(owner.id());
oracles.push_back(oracle1.documentID());
}
for (int i = 0; i < accounts.size(); ++i)
{
auto const jv = [&]() {
// document id is uint32
if (i % 2)
return Oracle::ledgerEntry(env, accounts[i], oracles[i]);
// document id is string
return Oracle::ledgerEntry(
env, accounts[i], std::to_string(oracles[i]));
}();
try
{
BEAST_EXPECT(
jv[jss::node][jss::Owner] == to_string(accounts[i]));
}
catch (...)
{
fail();
}
}
}

public:
void
run() override
Expand All @@ -772,8 +695,6 @@ struct Oracle_test : public beast::unit_test::suite
all - featureMultiSignReserve - featureExpandedSignerList,
all - featureExpandedSignerList})
testMultisig(features);
testInvalidLedgerEntry();
testLedgerEntry();
}
};

Expand Down
82 changes: 82 additions & 0 deletions src/test/rpc/LedgerRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <ripple/protocol/STXChainBridge.h>
#include <ripple/protocol/jss.h>
#include <test/jtx.h>
#include <test/jtx/Oracle.h>
#include <test/jtx/attester.h>
#include <test/jtx/multisign.h>
#include <test/jtx/xchain_bridge.h>
Expand Down Expand Up @@ -2279,6 +2280,85 @@ class LedgerRPC_test : public beast::unit_test::suite
}
}

void
testInvalidOracleLedgerEntry()
{
testcase("Invalid Oracle Ledger Entry");
using namespace ripple::test::jtx;
using namespace ripple::test::jtx::oracle;

Env env(*this);
Account const owner("owner");
env.fund(XRP(1'000), owner);
Oracle oracle(env, {.owner = owner});

// Malformed document id
std::vector<AnyValue> invalid = {"%None%", -1, 1.2, "", "Invalid"};
for (auto const& v : invalid)
{
auto const res = Oracle::ledgerEntry(env, owner, v);
BEAST_EXPECT(res[jss::error].asString() == "malformedDocumentID");
}
// Missing document id
auto res = Oracle::ledgerEntry(env, owner, std::nullopt);
BEAST_EXPECT(res[jss::error].asString() == "malformedRequest");

// Missing account
res = Oracle::ledgerEntry(env, std::nullopt, 1);
BEAST_EXPECT(res[jss::error].asString() == "malformedRequest");

// Malformed account
std::string malfAccount = to_string(owner.id());
malfAccount.replace(10, 1, 1, '!');
res = Oracle::ledgerEntry(env, malfAccount, 1);
BEAST_EXPECT(res[jss::error].asString() == "malformedAddress");
}

void
testOracleLedgerEntry()
{
testcase("Oracle Ledger Entry");
using namespace ripple::test::jtx;
using namespace ripple::test::jtx::oracle;

Env env(*this);
std::vector<AccountID> accounts;
std::vector<std::uint32_t> oracles;
for (int i = 0; i < 10; ++i)
{
Account const owner(std::string("owner") + std::to_string(i));
env.fund(XRP(1'000), owner);
// different accounts can have the same asset pair
Oracle oracle(env, {.owner = owner, .documentID = i});
accounts.push_back(owner.id());
oracles.push_back(oracle.documentID());
// same account can have different asset pair
Oracle oracle1(env, {.owner = owner, .documentID = i + 10});
accounts.push_back(owner.id());
oracles.push_back(oracle1.documentID());
}
for (int i = 0; i < accounts.size(); ++i)
{
auto const jv = [&]() {
// document id is uint32
if (i % 2)
return Oracle::ledgerEntry(env, accounts[i], oracles[i]);
// document id is string
return Oracle::ledgerEntry(
env, accounts[i], std::to_string(oracles[i]));
}();
try
{
BEAST_EXPECT(
jv[jss::node][jss::Owner] == to_string(accounts[i]));
}
catch (...)
{
fail();
}
}
}

public:
void
run() override
Expand All @@ -2304,6 +2384,8 @@ class LedgerRPC_test : public beast::unit_test::suite
testQueue();
testLedgerAccountsOption();
testLedgerEntryDID();
testInvalidOracleLedgerEntry();
testOracleLedgerEntry();

forAllApiVersions(std::bind_front(
&LedgerRPC_test::testLedgerEntryInvalidParams, this));
Expand Down

0 comments on commit b67d3c3

Please sign in to comment.