Test both encoders.

This commit is contained in:
chriseth 2017-08-10 18:27:57 +02:00 committed by Alex Beregszaszi
parent d1ad62fccc
commit 63b556b206

View File

@ -42,11 +42,36 @@ namespace test
BOOST_CHECK_EQUAL(toHex(m_logs[0].data), toHex(DATA)); \ BOOST_CHECK_EQUAL(toHex(m_logs[0].data), toHex(DATA)); \
} while (false) } while (false)
static string const NewEncoderPragma = "pragma experimental ABIEncoderV2;\n";
#define NEW_ENCODER(CODE) \
{ \
sourceCode = NewEncoderPragma + sourceCode; \
{ CODE } \
}
#define BOTH_ENCODERS(CODE) \
{ \
{ CODE } \
NEW_ENCODER(CODE) \
}
BOOST_FIXTURE_TEST_SUITE(ABIEncoderTest, SolidityExecutionFramework) BOOST_FIXTURE_TEST_SUITE(ABIEncoderTest, SolidityExecutionFramework)
BOOST_AUTO_TEST_CASE(both_encoders_macro)
{
// This tests that the "both encoders macro" at least runs twice and
// modifies the source.
string sourceCode;
int runs = 0;
BOTH_ENCODERS(runs++;)
BOOST_CHECK(sourceCode == NewEncoderPragma);
BOOST_CHECK_EQUAL(runs, 2);
}
BOOST_AUTO_TEST_CASE(value_types) BOOST_AUTO_TEST_CASE(value_types)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(uint a, uint16 b, uint24 c, int24 d, bytes3 x, bool, C); event E(uint a, uint16 b, uint24 c, int24 d, bytes3 x, bool, C);
function f() { function f() {
@ -59,16 +84,18 @@ BOOST_AUTO_TEST_CASE(value_types)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
10, u256(65534), u256(0x121212), u256(-1), string("\x1b\xab\xab"), true, u160(u256(-5)) REQUIRE_LOG_DATA(encodeArgs(
)); 10, u256(65534), u256(0x121212), u256(-1), string("\x1b\xab\xab"), true, u160(u256(-5))
));
)
} }
BOOST_AUTO_TEST_CASE(string_literal) BOOST_AUTO_TEST_CASE(string_literal)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(string, bytes20, string); event E(string, bytes20, string);
function f() { function f() {
@ -76,19 +103,21 @@ BOOST_AUTO_TEST_CASE(string_literal)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
0x60, string("abcde"), 0xa0, REQUIRE_LOG_DATA(encodeArgs(
6, string("abcdef"), 0x60, string("abcde"), 0xa0,
0x8b, string("abcdefabcdefgehabcabcasdfjklabcdefabcedefghabcabcasdfjklabcdefabcdefghabcabcasdfjklabcdeefabcdefghabcabcasdefjklabcdefabcdefghabcabcasdfjkl") 6, string("abcdef"),
)); 0x8b, string("abcdefabcdefgehabcabcasdfjklabcdefabcedefghabcabcasdfjklabcdefabcdefghabcabcasdfjklabcdeefabcdefghabcabcasdefjklabcdefabcdefghabcabcasdfjkl")
));
)
} }
BOOST_AUTO_TEST_CASE(enum_type_cleanup) BOOST_AUTO_TEST_CASE(enum_type_cleanup)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
enum E { A, B } enum E { A, B }
function f(uint x) returns (E en) { function f(uint x) returns (E en) {
@ -96,15 +125,17 @@ BOOST_AUTO_TEST_CASE(enum_type_cleanup)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
BOOST_CHECK(callContractFunction("f(uint256)", 0) == encodeArgs(0)); compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f(uint256)", 1) == encodeArgs(1)); BOOST_CHECK(callContractFunction("f(uint256)", 0) == encodeArgs(0));
BOOST_CHECK(callContractFunction("f(uint256)", 2) == encodeArgs()); BOOST_CHECK(callContractFunction("f(uint256)", 1) == encodeArgs(1));
BOOST_CHECK(callContractFunction("f(uint256)", 2) == encodeArgs());
)
} }
BOOST_AUTO_TEST_CASE(conversion) BOOST_AUTO_TEST_CASE(conversion)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(bytes4, bytes4, uint16, uint8, int16, int8); event E(bytes4, bytes4, uint16, uint8, int16, int8);
function f() { function f() {
@ -118,17 +149,19 @@ BOOST_AUTO_TEST_CASE(conversion)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
string(3, 0) + string("\x0a"), string("\xf1\xf2"), REQUIRE_LOG_DATA(encodeArgs(
0xff, 0xff, u256(-1), u256(1) string(3, 0) + string("\x0a"), string("\xf1\xf2"),
)); 0xff, 0xff, u256(-1), u256(1)
));
)
} }
BOOST_AUTO_TEST_CASE(memory_array_one_dim) BOOST_AUTO_TEST_CASE(memory_array_one_dim)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(uint a, int16[] b, uint c); event E(uint a, int16[] b, uint c);
function f() { function f() {
@ -142,14 +175,20 @@ BOOST_AUTO_TEST_CASE(memory_array_one_dim)
} }
} }
)"; )";
compileAndRun(sourceCode); compileAndRun(sourceCode);
callContractFunction("f()"); callContractFunction("f()");
// The old encoder does not clean array elements.
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 3, u256("0xfffffffe"), u256("0xffffffff"), u256("0x100000000")));
compileAndRun(NewEncoderPragma + sourceCode);
callContractFunction("f()");
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 3, u256(-2), u256(-1), u256(0))); REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 3, u256(-2), u256(-1), u256(0)));
} }
BOOST_AUTO_TEST_CASE(memory_array_two_dim) BOOST_AUTO_TEST_CASE(memory_array_two_dim)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(uint a, int16[][2] b, uint c); event E(uint a, int16[][2] b, uint c);
function f() { function f() {
@ -165,14 +204,16 @@ BOOST_AUTO_TEST_CASE(memory_array_two_dim)
} }
} }
)"; )";
compileAndRun(sourceCode); NEW_ENCODER(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 0x40, 0xc0, 3, 7, 0x0506, u256(-1), 2, 4, 5)); callContractFunction("f()");
REQUIRE_LOG_DATA(encodeArgs(10, 0x60, 11, 0x40, 0xc0, 3, 7, 0x0506, u256(-1), 2, 4, 5));
)
} }
BOOST_AUTO_TEST_CASE(memory_byte_array) BOOST_AUTO_TEST_CASE(memory_byte_array)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(uint a, bytes[] b, uint c); event E(uint a, bytes[] b, uint c);
function f() { function f() {
@ -183,19 +224,21 @@ BOOST_AUTO_TEST_CASE(memory_byte_array)
} }
} }
)"; )";
compileAndRun(sourceCode); NEW_ENCODER(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
10, 0x60, 11, REQUIRE_LOG_DATA(encodeArgs(
2, 0x40, 0xc0, 10, 0x60, 11,
66, string("abcabcdefghjklmnopqrsuvwabcdefgijklmnopqrstuwabcdefgijklmnoprstuvw"), 2, 0x40, 0xc0,
63, string("abcdefghijklmnopqrtuvwabcfghijklmnopqstuvwabcdeghijklmopqrstuvw") 66, string("abcabcdefghjklmnopqrsuvwabcdefgijklmnopqrstuwabcdefgijklmnoprstuvw"),
)); 63, string("abcdefghijklmnopqrtuvwabcfghijklmnopqstuvwabcdeghijklmopqrstuvw")
));
)
} }
BOOST_AUTO_TEST_CASE(storage_byte_array) BOOST_AUTO_TEST_CASE(storage_byte_array)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
bytes short; bytes short;
bytes long; bytes long;
@ -207,18 +250,20 @@ BOOST_AUTO_TEST_CASE(storage_byte_array)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
0x40, 0x80, REQUIRE_LOG_DATA(encodeArgs(
31, string("123456789012345678901234567890a"), 0x40, 0x80,
75, string("ffff123456789012345678901234567890afffffffff123456789012345678901234567890a") 31, string("123456789012345678901234567890a"),
)); 75, string("ffff123456789012345678901234567890afffffffff123456789012345678901234567890a")
));
)
} }
BOOST_AUTO_TEST_CASE(storage_array) BOOST_AUTO_TEST_CASE(storage_array)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
address[3] addr; address[3] addr;
event E(address[3] a); event E(address[3] a);
@ -232,14 +277,16 @@ BOOST_AUTO_TEST_CASE(storage_array)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs(u160(-1), u160(-2), u160(-3))); callContractFunction("f()");
REQUIRE_LOG_DATA(encodeArgs(u160(-1), u160(-2), u160(-3)));
)
} }
BOOST_AUTO_TEST_CASE(storage_array_dyn) BOOST_AUTO_TEST_CASE(storage_array_dyn)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
address[] addr; address[] addr;
event E(address[] a); event E(address[] a);
@ -251,14 +298,16 @@ BOOST_AUTO_TEST_CASE(storage_array_dyn)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs(0x20, 3, u160(1), u160(2), u160(3))); callContractFunction("f()");
REQUIRE_LOG_DATA(encodeArgs(0x20, 3, u160(1), u160(2), u160(3)));
)
} }
BOOST_AUTO_TEST_CASE(storage_array_compact) BOOST_AUTO_TEST_CASE(storage_array_compact)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
int72[] x; int72[] x;
event E(int72[]); event E(int72[]);
@ -275,16 +324,18 @@ BOOST_AUTO_TEST_CASE(storage_array_compact)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f()"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs( callContractFunction("f()");
0x20, 8, u256(-1), 2, u256(-3), 4, u256(-5), 6, u256(-7), 8 REQUIRE_LOG_DATA(encodeArgs(
)); 0x20, 8, u256(-1), 2, u256(-3), 4, u256(-5), 6, u256(-7), 8
));
)
} }
BOOST_AUTO_TEST_CASE(external_function) BOOST_AUTO_TEST_CASE(external_function)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(function(uint) external returns (uint), function(uint) external returns (uint)); event E(function(uint) external returns (uint), function(uint) external returns (uint));
function(uint) external returns (uint) g; function(uint) external returns (uint) g;
@ -294,15 +345,17 @@ BOOST_AUTO_TEST_CASE(external_function)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f(uint256)"); compileAndRun(sourceCode);
string functionIdF = asString(m_contractAddress.ref()) + asString(FixedHash<4>(dev::keccak256("f(uint256)")).ref()); callContractFunction("f(uint256)");
REQUIRE_LOG_DATA(encodeArgs(functionIdF, functionIdF)); string functionIdF = asString(m_contractAddress.ref()) + asString(FixedHash<4>(dev::keccak256("f(uint256)")).ref());
REQUIRE_LOG_DATA(encodeArgs(functionIdF, functionIdF));
)
} }
BOOST_AUTO_TEST_CASE(external_function_cleanup) BOOST_AUTO_TEST_CASE(external_function_cleanup)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(function(uint) external returns (uint), function(uint) external returns (uint)); event E(function(uint) external returns (uint), function(uint) external returns (uint));
// This test relies on the fact that g is stored in slot zero. // This test relies on the fact that g is stored in slot zero.
@ -314,14 +367,16 @@ BOOST_AUTO_TEST_CASE(external_function_cleanup)
} }
} }
)"; )";
compileAndRun(sourceCode); BOTH_ENCODERS(
callContractFunction("f(uint256)"); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs(string(24, char(-1)), string(24, char(-1)))); callContractFunction("f(uint256)");
REQUIRE_LOG_DATA(encodeArgs(string(24, char(-1)), string(24, char(-1))));
)
} }
BOOST_AUTO_TEST_CASE(calldata) BOOST_AUTO_TEST_CASE(calldata)
{ {
char const* sourceCode = R"( string sourceCode = R"(
contract C { contract C {
event E(bytes); event E(bytes);
function f(bytes a) external { function f(bytes a) external {
@ -329,13 +384,18 @@ BOOST_AUTO_TEST_CASE(calldata)
} }
} }
)"; )";
compileAndRun(sourceCode);
string s("abcdef"); string s("abcdef");
string t("abcdefgggggggggggggggggggggggggggggggggggggggghhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeggg"); string t("abcdefgggggggggggggggggggggggggggggggggggggggghhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeggg");
callContractFunction("f(bytes)", 0x20, s.size(), s); bool newEncoder = false;
REQUIRE_LOG_DATA(encodeArgs(0x20, s.size(), s)); BOTH_ENCODERS(
callContractFunction("f(bytes)", 0x20, t.size(), t); compileAndRun(sourceCode);
REQUIRE_LOG_DATA(encodeArgs(0x20, t.size(), t)); callContractFunction("f(bytes)", 0x20, s.size(), s);
// The old encoder did not pad to multiples of 32 bytes
REQUIRE_LOG_DATA(encodeArgs(0x20, s.size()) + (newEncoder ? encodeArgs(s) : asBytes(s)));
callContractFunction("f(bytes)", 0x20, t.size(), t);
REQUIRE_LOG_DATA(encodeArgs(0x20, t.size()) + (newEncoder ? encodeArgs(t) : asBytes(t)));
newEncoder = true;
)
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()