Skip to content

Commit 2411d77

Browse files
committed
Tests for EOF code validation
1 parent c350486 commit 2411d77

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/unittests/eof_validation_test.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include <evmone/eof.hpp>
6+
#include <evmone/instruction_traits.hpp>
67
#include <gtest/gtest.h>
78
#include <test/utils/utils.hpp>
89

@@ -88,3 +89,51 @@ TEST(eof_validation, EOF1_multiple_data_sections)
8889
EXPECT_EQ(validate_eof(from_hex("EF0001 010001 020001 020001 00 FE DA DA")),
8990
EOFValidationErrror::multiple_data_sections);
9091
}
92+
93+
TEST(eof_validation, EOF1_undefined_opcodes)
94+
{
95+
auto code = from_hex("EF0001 010001 00 00");
96+
97+
const auto& gas_table = evmone::instr::gas_costs[EVMC_SHANGHAI];
98+
99+
for (uint16_t opcode = 0; opcode <= 0xff; ++opcode)
100+
{
101+
// PUSH* require immediate argument to be valid, checked in a separate test
102+
if (opcode >= OP_PUSH1 && opcode <= OP_PUSH32)
103+
continue;
104+
105+
code.back() = static_cast<uint8_t>(opcode);
106+
107+
const auto expected = (gas_table[opcode] == evmone::instr::undefined ?
108+
EOFValidationErrror::undefined_instruction :
109+
EOFValidationErrror::success);
110+
EXPECT_EQ(validate_eof(code), expected) << hex(code);
111+
}
112+
113+
EXPECT_EQ(validate_eof(from_hex("EF0001 010001 00 FE")), EOFValidationErrror::success);
114+
}
115+
116+
TEST(eof_validation, EOF1_truncated_push)
117+
{
118+
auto eof_header = from_hex("EF0001 010001 00");
119+
auto& code_size_byte = eof_header[5];
120+
for (uint8_t opcode = OP_PUSH1; opcode <= OP_PUSH32; ++opcode)
121+
{
122+
const auto required_bytes = static_cast<size_t>(opcode - OP_PUSH1 + 1);
123+
for (size_t i = 0; i < required_bytes; ++i)
124+
{
125+
bytes code{opcode + bytes(i, 0)};
126+
code_size_byte = static_cast<uint8_t>(code.size());
127+
const auto container = eof_header + code;
128+
129+
EXPECT_EQ(validate_eof(container), EOFValidationErrror::truncated_immediate)
130+
<< hex(container);
131+
}
132+
133+
bytes code{opcode + bytes(required_bytes, 0)};
134+
code_size_byte = static_cast<uint8_t>(code.size());
135+
const auto container = eof_header + code;
136+
137+
EXPECT_EQ(validate_eof(container), EOFValidationErrror::success) << hex(container);
138+
}
139+
}

0 commit comments

Comments
 (0)