trace testcase

This commit is contained in:
Richard Holland
2023-01-10 13:48:15 +00:00
parent 61e0a3766c
commit e299f099d8
2 changed files with 85 additions and 21 deletions

View File

@@ -1077,32 +1077,51 @@ DEFINE_HOOK_FUNCTION(
if (dread_len > 1023)
dread_len = 1023;
uint8_t output[2048];
uint8_t output_storage[2200];
size_t out_len = 0;
uint8_t* output = output_storage;
if (mread_len > 0)
{
memcpy(output, memory + mread_ptr, mread_len);
out_len += mread_len;
output[out_len++] = ':';
output[out_len++] = ' ';
}
output = output_storage + out_len;
if (dread_len > 0)
{
if (as_hex)
{
out_len = dread_len * 2;
out_len += dread_len * 2;
for (int i = 0; i < dread_len && i < memory_length; ++i)
{
unsigned char high = (memory[dread_ptr + i] >> 4) & 0xF;
unsigned char low = (memory[dread_ptr + i] & 0xF);
uint8_t high = (memory[dread_ptr + i] >> 4) & 0xFU;
uint8_t low = (memory[dread_ptr + i] & 0xFU);
high += ( high < 10U ? '0' : 'A' - 10 );
low += ( low < 10U ? '0' : 'A' - 10 );
output[i*2 + 0] = high;
output[i*2 + 1] = low;
}
// output[out_len++] = '\0';
}
else if (is_UTF16LE(memory + dread_ptr, dread_len))
{
out_len = dread_len / 2; //is_UTF16LE will only return true if read_len is even
for (int i = 0; i < out_len; ++i)
out_len += dread_len / 2; //is_UTF16LE will only return true if read_len is even
for (int i = 0; i < (dread_len / 2); ++i)
output[i] = memory[dread_ptr + i * 2];
// output[out_len++] = '\0';
}
else
{
out_len += dread_len;
memcpy(output, memory + dread_ptr, dread_len);
}
}
RETURN_HOOK_TRACE(mread_ptr, mread_len,
std::string((const char*)output, out_len));
std::string((const char*)output_storage, out_len));
}

View File

@@ -9078,7 +9078,52 @@ public:
void
test_trace()
{
// TODO
testcase("Test trace");
using namespace jtx;
Env env{*this, supported_amendments()};
auto const alice = Account{"alice"};
auto const bob = Account{"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 trace (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
#define OUT_OF_BOUNDS -1
#define ASSERT(x)\
if (!(x))\
rollback((uint32_t)#x, sizeof(#x), __LINE__);
int64_t hook(uint32_t reservmaed )
{
_g(1,1);
// Test out of bounds check
ASSERT(trace(1000000, 10, 0, 10, 0) == OUT_OF_BOUNDS);
ASSERT(trace(0, 1000000, 0, 10, 0) == OUT_OF_BOUNDS);
ASSERT(trace(0, 10, 1000000, 10, 0) == OUT_OF_BOUNDS);
ASSERT(trace(0, 10, 0, 1000000, 0) == OUT_OF_BOUNDS);
ASSERT(trace(0,0,0,0,0) == 0);
ASSERT(trace(0,1,2,3,1) == 0);
return accept(0,0,0);
}
)[test.hook]"];
// install the hook on alice
env(ripple::test::jtx::hook(alice, {{hso(hook, overrideFlag)}}, 0),
M("set trace"),
HSFEE);
env.close();
// invoke the hook
env(pay(bob, alice, XRP(1)), M("test trace"), fee(XRP(1)));
}
void
@@ -10974,7 +11019,7 @@ public:
test_sto_subfield(); //
test_sto_validate(); //
test_trace();
test_trace(); //
test_trace_float();
test_trace_num();