Skip to content

Commit

Permalink
Merge pull request #469 from ethereum/tests_hex_literal
Browse files Browse the repository at this point in the history
test: Introduce _hex literal to build bytes
  • Loading branch information
chfast authored Jun 3, 2022
2 parents 5b5bf0a + 3db7174 commit fa20744
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 93 deletions.
22 changes: 11 additions & 11 deletions test/unittests/eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ TEST(eof, code_begin)

TEST(eof, is_eof_code)
{
EXPECT_FALSE(is_eof_code(from_hex("")));
EXPECT_FALSE(is_eof_code(from_hex("EF")));
EXPECT_FALSE(is_eof_code(from_hex("EF01")));
EXPECT_FALSE(is_eof_code(from_hex("EF02")));
EXPECT_FALSE(is_eof_code(from_hex("EFFF")));
EXPECT_FALSE(is_eof_code(from_hex("00")));
EXPECT_FALSE(is_eof_code(from_hex("FE")));
EXPECT_FALSE(is_eof_code(""_hex));
EXPECT_FALSE(is_eof_code("EF"_hex));
EXPECT_FALSE(is_eof_code("EF01"_hex));
EXPECT_FALSE(is_eof_code("EF02"_hex));
EXPECT_FALSE(is_eof_code("EFFF"_hex));
EXPECT_FALSE(is_eof_code("00"_hex));
EXPECT_FALSE(is_eof_code("FE"_hex));

EXPECT_TRUE(is_eof_code(from_hex("EF00")));
EXPECT_TRUE(is_eof_code(from_hex("EF00 01 010001 00 00")));
EXPECT_TRUE(is_eof_code(from_hex("EF00 01 010001 020004 00 00 AABBCCDD")));
EXPECT_TRUE(is_eof_code(from_hex("EF00 02 ABCFEF")));
EXPECT_TRUE(is_eof_code("EF00"_hex));
EXPECT_TRUE(is_eof_code("EF00 01 010001 00 00"_hex));
EXPECT_TRUE(is_eof_code("EF00 01 010001 020004 00 00 AABBCCDD"_hex));
EXPECT_TRUE(is_eof_code("EF00 02 ABCFEF"_hex));
}

TEST(eof, read_valid_eof1_header)
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ TEST(eof_validation, EOF1_trailing_bytes)

TEST(eof_validation, EOF1_undefined_opcodes)
{
auto cont = from_hex("EF0001 010002 00 0000");
auto cont = "EF0001 010002 00 0000"_hex;

const auto& gas_table = evmone::instr::gas_costs[EVMC_SHANGHAI];

Expand All @@ -180,12 +180,12 @@ TEST(eof_validation, EOF1_undefined_opcodes)
EXPECT_EQ(validate_eof(cont), expected) << hex(cont);
}

EXPECT_EQ(validate_eof(from_hex("EF0001 010001 00 FE")), EOFValidationError::success);
EXPECT_EQ(validate_eof("EF0001 010001 00 FE"), EOFValidationError::success);
}

TEST(eof_validation, EOF1_truncated_push)
{
auto eof_header = from_hex("EF0001 010001 00");
auto eof_header = "EF0001 010001 00"_hex;
auto& code_size_byte = eof_header[5];
for (uint8_t opcode = OP_PUSH1; opcode <= OP_PUSH32; ++opcode)
{
Expand All @@ -210,7 +210,7 @@ TEST(eof_validation, EOF1_truncated_push)

TEST(eof_validation, EOF1_terminating_instructions)
{
auto eof_header = from_hex("EF0001 010001 00");
auto eof_header = "EF0001 010001 00"_hex;
auto& code_size_byte = eof_header[5];

const auto& traits = evmone::instr::traits;
Expand Down
20 changes: 9 additions & 11 deletions test/unittests/evm_eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ TEST_P(evm, eof1_codecopy_full)
execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
from_hex("ef000101000c006013600060003960136000f3"));
"ef000101000c006013600060003960136000f3"_hex);

code = eof1_bytecode(bytecode{26} + 0 + 0 + OP_CODECOPY + ret(0, 26), "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
from_hex("ef000101000c02000400601a6000600039601a6000f3deadbeef"));
"ef000101000c02000400601a6000600039601a6000f3deadbeef"_hex);
}

TEST_P(evm, eof1_codecopy_header)
Expand All @@ -163,13 +163,13 @@ TEST_P(evm, eof1_codecopy_header)

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), from_hex("ef000101000c00"));
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "ef000101000c00"_hex);

code = eof1_bytecode(bytecode{10} + 0 + 0 + OP_CODECOPY + ret(0, 10), "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), from_hex("ef000101000c02000400"));
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "ef000101000c02000400"_hex);
}

TEST_P(evm, eof1_codecopy_code)
Expand All @@ -179,15 +179,13 @@ TEST_P(evm, eof1_codecopy_code)

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(
bytes_view(result.output_data, result.output_size), from_hex("600c6007600039600c6000f3"));
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "600c6007600039600c6000f3"_hex);

code = eof1_bytecode(bytecode{12} + 10 + 0 + OP_CODECOPY + ret(0, 12), "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(
bytes_view(result.output_data, result.output_size), from_hex("600c600a600039600c6000f3"));
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "600c600a600039600c6000f3"_hex);
}

TEST_P(evm, eof1_codecopy_data)
Expand All @@ -198,7 +196,7 @@ TEST_P(evm, eof1_codecopy_data)

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size), from_hex("deadbeef"));
EXPECT_EQ(bytes_view(result.output_data, result.output_size), "deadbeef"_hex);
}

TEST_P(evm, eof1_codecopy_out_of_bounds)
Expand All @@ -210,12 +208,12 @@ TEST_P(evm, eof1_codecopy_out_of_bounds)
execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
from_hex("ef000101000c006017600060003960176000f300000000"));
"ef000101000c006017600060003960176000f300000000"_hex);

code = eof1_bytecode(bytecode{30} + 0 + 0 + OP_CODECOPY + ret(0, 30), "deadbeef");

execute(code);
EXPECT_STATUS(EVMC_SUCCESS);
EXPECT_EQ(bytes_view(result.output_data, result.output_size),
from_hex("ef000101000c02000400601e6000600039601e6000f3deadbeef00000000"));
"ef000101000c02000400601e6000600039601e6000f3deadbeef00000000"_hex);
}
3 changes: 1 addition & 2 deletions test/unittests/evm_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ TEST_P(evm, calldatacopy)
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(gas_used, 23);
ASSERT_EQ(result.output_size, 10);
auto a = from_hex("02030405000000000000");
EXPECT_EQ(bytes(&result.output_data[0], 10), a);
EXPECT_EQ(bytes_view(&result.output_data[0], 10), "02030405000000000000"_hex);

execute(s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
Expand Down
6 changes: 3 additions & 3 deletions test/unittests/evm_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ TEST_P(evm, codecopy_combinations)
EXPECT_EQ(output, bytes_view{});

execute(code, "1301");
EXPECT_EQ(output, from_hex("00"));
EXPECT_EQ(output, "00"_hex);

execute(code, "1401");
EXPECT_EQ(output, from_hex("00"));
EXPECT_EQ(output, "00"_hex);

execute(code, "1201");
EXPECT_EQ(output, code.substr(0x12, 1));
Expand Down Expand Up @@ -647,7 +647,7 @@ TEST_P(evm, extcodecopy_big_index)
constexpr auto index = uint64_t{std::numeric_limits<uint32_t>::max()} + 1;
const auto code = dup1(1) + push(index) + dup1(0) + OP_EXTCODECOPY + ret(0, {});
execute(code);
EXPECT_EQ(output, from_hex("00"));
EXPECT_EQ(output, "00"_hex);
}

TEST_P(evm, extcodehash)
Expand Down
93 changes: 31 additions & 62 deletions test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,10 @@ TEST_P(evm, addmod_mulmod)
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
ASSERT_EQ(result.output_size, 64);
auto a = from_hex("65ef55f81fe142622955e990252cb5209a11d4db113d842408fd9c7ae2a29a5a");
EXPECT_EQ(a, bytes(&result.output_data[0], 32));
auto p = from_hex("34e04890131a297202753cae4c72efd508962c9129aed8b08c8e87ab425b7258");
EXPECT_EQ(p, bytes(&result.output_data[32], 32));
EXPECT_EQ(bytes_view(&result.output_data[0], 32),
"65ef55f81fe142622955e990252cb5209a11d4db113d842408fd9c7ae2a29a5a"_hex);
EXPECT_EQ(bytes_view(&result.output_data[32], 32),
"34e04890131a297202753cae4c72efd508962c9129aed8b08c8e87ab425b7258"_hex);
}

TEST_P(evm, divmod)
Expand All @@ -447,10 +447,10 @@ TEST_P(evm, divmod)
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(gas_used, 61);
ASSERT_EQ(result.output_size, 64);
auto a = from_hex("0000000000000000000000000000000000000000000000000000000000000013");
EXPECT_EQ(a, bytes(&result.output_data[0], 32));
auto p = from_hex("08ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
EXPECT_EQ(p, bytes(&result.output_data[32], 32));
EXPECT_EQ(bytes_view(&result.output_data[0], 32),
"0000000000000000000000000000000000000000000000000000000000000013"_hex);
EXPECT_EQ(bytes_view(&result.output_data[32], 32),
"08ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"_hex);
}

TEST_P(evm, div_by_zero)
Expand Down Expand Up @@ -547,18 +547,11 @@ TEST_P(evm, signextend_fuzzing)

TEST_P(evm, exp)
{
std::string s;
s += "612019"; // 0x2019
s += "6003"; // 3
s += "0a"; // EXP
s += "600052"; // m[0..]
s += "60206000f3"; // RETURN(0,32)
execute(131, s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
const auto code = push(0x2019) + push(3) + OP_EXP + ret_top();
execute(131, code);
EXPECT_GAS_USED(EVMC_SUCCESS, 131);
ASSERT_EQ(result.output_size, 32);
auto a = from_hex("263cf24662b24c371a647c1340022619306e431bf3a4298d4b5998a3f1c1aaa3");
EXPECT_EQ(bytes(&result.output_data[0], 32), a);
EXPECT_OUTPUT_INT(0x263cf24662b24c371a647c1340022619306e431bf3a4298d4b5998a3f1c1aaa3_u256);
}

TEST_P(evm, exp_1_0)
Expand Down Expand Up @@ -592,32 +585,19 @@ TEST_P(evm, exp_oog)
TEST_P(evm, exp_pre_spurious_dragon)
{
rev = EVMC_TANGERINE_WHISTLE;
std::string s;
s += "62012019"; // 0x012019
s += "6003"; // 3
s += "0a"; // EXP
s += "600052"; // m[0..]
s += "60206000f3"; // RETURN(0,32)
execute(131 - 70, s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
ASSERT_EQ(result.output_size, 32);
auto a = from_hex("422ea3761c4f6517df7f102bb18b96abf4735099209ca21256a6b8ac4d1daaa3");
EXPECT_EQ(bytes(&result.output_data[0], 32), a);
const auto code = push(0x012019) + push(3) + OP_EXP + ret_top();
execute(131 - 70, code);
EXPECT_GAS_USED(EVMC_SUCCESS, 131 - 70);
EXPECT_OUTPUT_INT(0x422ea3761c4f6517df7f102bb18b96abf4735099209ca21256a6b8ac4d1daaa3_u256);
}

TEST_P(evm, calldataload)
{
std::string s;
s += "600335"; // CALLDATALOAD(3)
s += "600052"; // m[0..]
s += "600a6000f3"; // RETURN(0,10)
execute(21, s, "0102030405");
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
const auto code = calldataload(3) + mstore(0) + ret(0, 10);
execute(21, code, "0102030405");
EXPECT_GAS_USED(EVMC_SUCCESS, 21);
ASSERT_EQ(result.output_size, 10);
auto a = from_hex("04050000000000000000");
EXPECT_EQ(bytes(&result.output_data[0], 10), a);
EXPECT_EQ(bytes(&result.output_data[0], 10), "04050000000000000000"_hex);
}

TEST_P(evm, calldataload_outofrange)
Expand All @@ -629,31 +609,23 @@ TEST_P(evm, calldataload_outofrange)

TEST_P(evm, address)
{
std::string s;
s += "30600052"; // ADDRESS MSTORE(0)
s += "600a600af3"; // RETURN(10,10)
msg.recipient.bytes[0] = 0xcc;
execute(17, s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
const auto code = mstore(0, OP_ADDRESS) + ret(10, 10);
execute(17, code);
EXPECT_GAS_USED(EVMC_SUCCESS, 17);
ASSERT_EQ(result.output_size, 10);
auto a = from_hex("0000cc00000000000000");
EXPECT_EQ(bytes(&result.output_data[0], 10), a);
EXPECT_EQ(bytes_view(&result.output_data[0], 10), "0000cc00000000000000"_hex);
}

TEST_P(evm, caller_callvalue)
{
std::string s;
s += "333401600052"; // CALLER CALLVALUE ADD MSTORE(0)
s += "600a600af3"; // RETURN(10,10)
msg.sender.bytes[0] = 0xdd;
msg.value.bytes[13] = 0xee;
execute(22, s);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(result.gas_left, 0);
const auto code = add(OP_CALLVALUE, OP_CALLER) + mstore(0) + ret(10, 10);
execute(22, code);
EXPECT_GAS_USED(EVMC_SUCCESS, 22);
ASSERT_EQ(result.output_size, 10);
auto a = from_hex("0000ddee000000000000");
EXPECT_EQ(bytes(&result.output_data[0], 10), a);
EXPECT_EQ(bytes_view(&result.output_data[0], 10), "0000ddee000000000000"_hex);
}

TEST_P(evm, undefined)
Expand Down Expand Up @@ -708,12 +680,9 @@ TEST_P(evm, inner_selfdestruct)

TEST_P(evm, keccak256)
{
execute("6108006103ff2060005260206000f3");
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(gas_used, 738);
ASSERT_EQ(result.output_size, 32);
auto hash = from_hex("aeffb38c06e111d84216396baefeb7fed397f303d5cb84a33f1e8b485c4a22da");
EXPECT_EQ(bytes(&result.output_data[0], 32), hash);
execute(push(0x0800) + push(0x03ff) + OP_KECCAK256 + ret_top());
EXPECT_GAS_USED(EVMC_SUCCESS, 738);
EXPECT_OUTPUT_INT(0xaeffb38c06e111d84216396baefeb7fed397f303d5cb84a33f1e8b485c4a22da_u256);
}

TEST_P(evm, keccak256_empty)
Expand Down
5 changes: 5 additions & 0 deletions test/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ using evmc::bytes_view;
using evmc::from_hex;
using evmc::hex;

inline bytes operator""_hex(const char* s, size_t size)
{
return from_hex({s, size});
}

/// Decodes the hexx encoded string.
///
/// The hexx encoding format is the hex format (base 16) with the extension
Expand Down

0 comments on commit fa20744

Please sign in to comment.