From 708a567f1048dd113f1ff8c0ba14702f030f4f5d Mon Sep 17 00:00:00 2001 From: Richard Holland Date: Tue, 3 Jan 2023 16:58:54 +0000 Subject: [PATCH] hook_again test case --- src/test/app/SetHook_test.cpp | 66 ++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/test/app/SetHook_test.cpp b/src/test/app/SetHook_test.cpp index 78204874d..5173a9bf7 100644 --- a/src/test/app/SetHook_test.cpp +++ b/src/test/app/SetHook_test.cpp @@ -4153,6 +4153,70 @@ public: void test_hook_again() { + testcase("Test hook_again"); + 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 + extern int32_t _g (uint32_t id, uint32_t maxiter); + 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 hook_again (void); + #define PREREQUISITE_NOT_MET (-9) + #define ALREADY_SET (-8) + int64_t hook(uint32_t r) + { + _g(1,1); + + if (r > 0) + { + if (hook_again() != PREREQUISITE_NOT_MET) + return rollback(0,0,253); + + return accept(0,0,1); + } + + if (hook_again() != 1) + return rollback(0,0,254); + + if (hook_again() != ALREADY_SET) + return rollback(0,0,255); + + return accept(0,0,0); + } + )[test.hook]"]; + + // install the hook on alice + env(ripple::test::jtx::hook(alice, {{hso(hook, overrideFlag)}}, 0), + M("set hook_again"), + HSFEE); + env.close(); + + env(pay(bob, alice, XRP(1)), M("test hook_again"), fee(XRP(1))); + env.close(); + + auto meta = env.meta(); + + // ensure hook execution occured + BEAST_REQUIRE(meta); + BEAST_REQUIRE(meta->isFieldPresent(sfHookExecutions)); + + // ensure there were two executions + auto const hookExecutions = + meta->getFieldArray(sfHookExecutions); + BEAST_REQUIRE(hookExecutions.size() == 2); + + // get the data in the return code of the execution + BEAST_EXPECT(hookExecutions[0].getFieldU64(sfHookReturnCode) == 0); + BEAST_EXPECT(hookExecutions[1].getFieldU64(sfHookReturnCode) == 1); + + // RH TODO: test hook_again on a weak execution not following a strong execution to make sure it fails } void @@ -9493,7 +9557,7 @@ public: test_float_sum(); // test_hook_account(); // - test_hook_again(); + test_hook_again(); // test_hook_hash(); // test_hook_param(); test_hook_param_set();