Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EOF validation test fixture #810

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

Check warning on line 14 in test/unittests/eof_validation.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/eof_validation.cpp#L14

Added line #L14 was not covered by tests
<< 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

Check warning on line 19 in test/unittests/eof_validation.hpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/eof_validation.hpp#L19

Added line #L19 was not covered by tests
{
protected:
/// EOF validation test case.
struct TestCase

Check warning on line 23 in test/unittests/eof_validation.hpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/eof_validation.hpp#L23

Added line #L23 was not covered by tests
{
/// 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