Skip to content

Commit

Permalink
EOF validation test fixture (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 authored Feb 12, 2024
2 parents 75eb1f2 + e026e94 commit 77cf1e9
Show file tree
Hide file tree
Showing 5 changed files with 569 additions and 663 deletions.
2 changes: 2 additions & 0 deletions test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ target_sources(
blockchaintest_loader_test.cpp
bytecode_test.cpp
eof_test.cpp
eof_validation.hpp
eof_validation.cpp
eof_validation_test.cpp
evm_fixture.cpp
evm_fixture.hpp
Expand Down
11 changes: 11 additions & 0 deletions test/unittests/eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ TEST(eof, read_valid_eof1_header)
EXPECT_EQ(header.types.size() * 4, test_case.types_size) << test_case.code;
}
}

TEST(eof, get_error_message)
{
EXPECT_EQ(evmone::get_error_message(EOFValidationError::success), "success");
EXPECT_EQ(evmone::get_error_message(EOFValidationError::invalid_prefix), "invalid_prefix");
EXPECT_EQ(evmone::get_error_message(EOFValidationError::stack_overflow), "stack_overflow");
EXPECT_EQ(evmone::get_error_message(EOFValidationError::impossible), "impossible");

// NOLINTNEXTLINE(*.EnumCastOutOfRange)
EXPECT_EQ(evmone::get_error_message(static_cast<EOFValidationError>(-1)), "<unknown>");
}
18 changes: 18 additions & 0 deletions test/unittests/eof_validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2024 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0

#include "eof_validation.hpp"

namespace evmone::test
{
void eof_validation::TearDown()
{
for (const auto& test_case : test_cases)
{
EXPECT_EQ(evmone::validate_eof(rev, test_case.container), test_case.error)
<< test_case.name << "\n"
<< hex(test_case.container);
}
}
} // namespace evmone::test
50 changes: 50 additions & 0 deletions test/unittests/eof_validation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// evmone: Fast Ethereum Virtual Machine implementation
// Copyright 2024 The evmone Authors.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <evmc/evmc.hpp>
#include <evmone/eof.hpp>
#include <evmone/evmone.h>
#include <gtest/gtest.h>
#include <test/utils/bytecode.hpp>

namespace evmone::test
{
using evmc::bytes;

/// Fixture for defining test cases for EOF validation.
///
/// Each test contains multiple cases, which are validated during test teardown.
class eof_validation : public testing::Test
{
protected:
/// EOF validation test case.
struct TestCase
{
/// Container to be validated.
bytes container;
/// Expected error if container is expected to be invalid,
/// or EOFValidationError::success if it is expected to be valid.
EOFValidationError error = EOFValidationError::success;
/// (Optional) Test case description.
std::string name;
};

evmc_revision rev = EVMC_PRAGUE;
std::vector<TestCase> test_cases;

/// Adds the case to test cases.
///
/// Can be called as add_test_case(string_view hex, error, name)
/// or add_test_case(bytes_view cont, error, name).
void add_test_case(bytecode container, EOFValidationError error, std::string name = {})
{
test_cases.push_back({std::move(container), error, std::move(name)});
}

/// The test runner.
void TearDown() override;
};

} // namespace evmone::test
Loading

0 comments on commit 77cf1e9

Please sign in to comment.