Skip to content

Commit

Permalink
Add test cases for maximally broad stack range and jumps to self
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Feb 21, 2024
1 parent 786c173 commit 1837002
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/unittests/eof_stack_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ TEST_F(eof_validation, non_constant_stack_height)
EOFValidationError::stack_underflow);
}

TEST_F(eof_validation, stack_range_maximally_broad)
{
// Construct series of RJUMPIs all targeting final STOP.
// Stack range at STOP is [0, 1023]
bytecode code = OP_STOP;
int16_t offset = 1;
for (auto i = 0; i < 1023; ++i)
{
code = rjumpi(offset, OP_PUSH0) + OP_PUSH0 + code;
offset += 5;
}

add_test_case(eof_bytecode(code, 1023), EOFValidationError::success, "valid_1023_rjumpis");

code = rjumpi(offset, OP_PUSH0) + OP_PUSH0 + code;
add_test_case(eof_bytecode(code, 1023), EOFValidationError::invalid_max_stack_height,
"invalid_1024_rjumpis");
}

TEST_F(eof_validation, backwards_rjump)
{
add_test_case(eof_bytecode(rjump(-3)), EOFValidationError::success);
Expand Down Expand Up @@ -802,6 +821,34 @@ TEST_F(eof_validation, forwards_rjumpv_variable_stack)
EOFValidationError::success);
}

TEST_F(eof_validation, self_referencing_jumps)
{
// rjumpf from stack 0 to stack 0
add_test_case(eof_bytecode(rjump(-3)), EOFValidationError::success, "rjump");

// rjumpi from stack 0 to stack 1
add_test_case(
eof_bytecode(rjumpi(-3, 0) + OP_STOP), EOFValidationError::stack_height_mismatch, "rjumpi");

// rjumpv from stack 0 to stack 1
add_test_case(eof_bytecode(rjumpv({-4}, 0) + OP_STOP),
EOFValidationError::stack_height_mismatch, "rjumpv");
}

TEST_F(eof_validation, self_referencing_jumps_variable_stack)
{
// rjumpf from stack [1, 3] to stack [1, 3]
add_test_case(eof_bytecode(varstack + rjump(-3), 3), EOFValidationError::success, "rjump");

// rjumpi from stack [1, 3] to stack [2, 4]
add_test_case(eof_bytecode(varstack + rjumpi(-3, 0) + OP_STOP, 4),
EOFValidationError::stack_height_mismatch, "rjumpi");

// rjumpv from stack [1, 3] to stack [2, 4]
add_test_case(eof_bytecode(varstack + rjumpv({-4}, 0) + OP_STOP, 4),
EOFValidationError::stack_height_mismatch, "rjumpv");
}

TEST_F(eof_validation, underflow)
{
add_test_case(eof_bytecode(bytecode{OP_ADD} + OP_STOP, 0), EOFValidationError::stack_underflow);
Expand Down

0 comments on commit 1837002

Please sign in to comment.