otxn_field unit test

This commit is contained in:
Richard Holland
2023-01-10 17:08:08 +00:00
parent 98d8efdb6a
commit 9adf190b75
2 changed files with 75 additions and 2 deletions

View File

@@ -2078,7 +2078,14 @@ DEFINE_HOOK_FUNCTION(
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx, hookCtx on current stack
if (write_ptr != 0 && NOT_IN_BOUNDS(write_ptr, write_len, memory_length))
if (write_ptr == 0)
{
if (write_len != 0)
return INVALID_ARGUMENT;
// otherwise pass, we're trying to return the data as an int64_t
}
else if NOT_IN_BOUNDS(write_ptr, write_len, memory_length)
return OUT_OF_BOUNDS;
@@ -2095,11 +2102,12 @@ DEFINE_HOOK_FUNCTION(
? hookCtx.emitFailure->getField(fieldType)
: const_cast<ripple::STTx&>(applyCtx.tx).getField(fieldType);
bool is_account = field.getSType() == STI_ACCOUNT; //RH TODO improve this hack
bool is_account = field.getSType() == STI_ACCOUNT;
Serializer s;
field.add(s);
if (write_ptr == 0)
return data_as_int64(s.getDataPtr(), s.getDataLength());

View File

@@ -6029,6 +6029,71 @@ public:
void
test_otxn_field()
{
testcase("Test otxn_field");
using namespace jtx;
Env env{*this, supported_amendments()};
Account const alice{"alice"};
Account const bob{"bob"};
env.fund(XRP(10000), alice);
env.fund(XRP(10000), bob);
TestHook hook = wasm[R"[test.hook](
#include <stdint.h>
extern int32_t _g (uint32_t id, uint32_t maxiter);
#define GUARD(maxiter) _g((1ULL << 31U) + __LINE__, (maxiter)+1)
extern int64_t accept (uint32_t read_ptr, uint32_t read_len, int64_t error_code);
extern int64_t rollback (uint32_t read_ptr, uint32_t read_len, int64_t error_code);
extern int64_t otxn_field (uint32_t write_ptr, uint32_t write_len, uint32_t sfcode);
extern int64_t hook_account(uint32_t, uint32_t);
#define OUT_OF_BOUNDS -1
#define TOO_BIG -3
#define TOO_SMALL -4
#define INVALID_ARGUMENT -7
#define INVALID_FIELD -17
#define ASSERT(x)\
if (!(x))\
rollback((uint32_t)#x, sizeof(#x), __LINE__);
#define SBUF(x) (uint32_t)(x), sizeof(x)
#define sfAccount ((8U << 16U) + 1U)
int64_t hook(uint32_t reserved )
{
_g(1,1);
// bounds check
ASSERT(otxn_field(1, 1000000, sfAccount) == OUT_OF_BOUNDS);
ASSERT(otxn_field(1000000, 20, sfAccount) == OUT_OF_BOUNDS);
// sanity check
ASSERT(otxn_field(0, 1, sfAccount) == INVALID_ARGUMENT);
// size check
ASSERT(otxn_field(0, 0, sfAccount) == TOO_BIG);
uint8_t acc[20];
ASSERT(otxn_field(acc, 19, sfAccount) == TOO_SMALL);
ASSERT(otxn_field(acc, 20, sfAccount) == 20);
ASSERT(otxn_field(acc, 20, 1) == INVALID_FIELD);
uint8_t acc2[20];
ASSERT(hook_account(acc2, 20) == 20);
for (int i = 0; GUARD(20), i < 20; ++i)
ASSERT(acc[i] == acc2[i]);
accept(0,0,0);
}
)[test.hook]"];
// install the hook on alice
env(ripple::test::jtx::hook(alice, {{hso(hook, overrideFlag)}}, 0),
M("set otxn_field"),
HSFEE);
env.close();
// invoke the hook
env(pay(alice, bob, XRP(1)), M("test otxn_field"), fee(XRP(1)));
}
void