Moving more abi decoder tests to semanticTests.

This commit is contained in:
Djordje Mijovic 2020-11-03 13:53:21 +01:00
parent a838adc481
commit 490064590a
11 changed files with 127 additions and 188 deletions

View File

@ -73,69 +73,6 @@ BOOST_AUTO_TEST_CASE(value_types)
)
}
BOOST_AUTO_TEST_CASE(cleanup)
{
string sourceCode = R"(
contract C {
function f(uint16 a, int16 b, address c, bytes3 d, bool e)
public pure returns (uint v, uint w, uint x, uint y, uint z) {
assembly { v := a w := b x := c y := d z := e}
}
}
)";
bool newDecoder = solidity::test::CommonOptions::get().useABIEncoderV2;
BOTH_ENCODERS(
compileAndRun(sourceCode);
ABI_CHECK(
callContractFunction("f(uint16,int16,address,bytes3,bool)", 1, 2, 3, "a", true),
encodeArgs(u256(1), u256(2), u256(3), string("a"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0xffffff), u256(0x1ffff), u256(-1), string("abcd"), u256(1)
),
newDecoder ? bytes{} : encodeArgs(u256(0xffff), u256(-1), (u256(1) << 160) - 1, string("abc"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0xffffff), u256(0), u256(0), string("bcd"), u256(1)
),
newDecoder ? bytes{} : encodeArgs(u256(0xffff), u256(0), 0, string("bcd"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0), u256(0x1ffff), u256(0), string("ab"), u256(1)
),
newDecoder ? bytes{} : encodeArgs(u256(0), u256(-1), 0, string("ab"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0), u256(0), u256(-1), string("ad"), u256(1)
),
newDecoder ? bytes{} : encodeArgs(u256(0), u256(0), (u256(1) << 160) - 1, string("ad"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0), u256(0), u256(0), string("abcd"), u256(1)
),
newDecoder ? bytes{} : encodeArgs(u256(0), u256(0), 0, string("abc"), true)
);
ABI_CHECK(
callContractFunction(
"f(uint16,int16,address,bytes3,bool)",
u256(0), u256(0), u256(0), string("abc"), u256(2)
),
newDecoder ? bytes{} : encodeArgs(u256(0), u256(0), 0, string("abc"), true)
);
newDecoder = true;
)
}
BOOST_AUTO_TEST_CASE(decode_from_memory_simple)
{
string sourceCode = R"(
@ -683,108 +620,6 @@ BOOST_AUTO_TEST_CASE(complex_struct)
)
}
BOOST_AUTO_TEST_CASE(return_dynamic_types_cross_call_simple)
{
if (m_evmVersion == langutil::EVMVersion::homestead())
return;
string sourceCode = R"(
contract C {
function dyn() public returns (bytes memory) {
return "1234567890123456789012345678901234567890";
}
function f() public returns (bytes memory) {
return this.dyn();
}
}
)";
BOTH_ENCODERS(
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f()"), encodeArgs(0x20, 40, string("1234567890123456789012345678901234567890")));
)
}
BOOST_AUTO_TEST_CASE(return_dynamic_types_cross_call_advanced)
{
if (m_evmVersion == langutil::EVMVersion::homestead())
return;
string sourceCode = R"(
contract C {
function dyn() public returns (bytes memory a, uint b, bytes20[] memory c, uint d) {
a = "1234567890123456789012345678901234567890";
b = uint(-1);
c = new bytes20[](4);
c[0] = bytes20(uint160(1234));
c[3] = bytes20(uint160(6789));
d = 0x1234;
}
function f() public returns (bytes memory, uint, bytes20[] memory, uint) {
return this.dyn();
}
}
)";
BOTH_ENCODERS(
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f()"), encodeArgs(
0x80, u256(-1), 0xe0, 0x1234,
40, string("1234567890123456789012345678901234567890"),
4, u256(1234) << (8 * (32 - 20)), 0, 0, u256(6789) << (8 * (32 - 20))
));
)
}
BOOST_AUTO_TEST_CASE(return_dynamic_types_cross_call_out_of_range)
{
string sourceCode = R"(
contract C {
function dyn(uint x) public returns (bytes memory a) {
assembly {
mstore(0, 0x20)
mstore(0x20, 0x21)
return(0, x)
}
}
function f(uint x) public returns (bool) {
this.dyn(x);
return true;
}
}
)";
BOTH_ENCODERS(
compileAndRun(sourceCode, 0, "C");
if (m_evmVersion == langutil::EVMVersion::homestead())
{
ABI_CHECK(callContractFunction("f(uint256)", 0x60), encodeArgs(true));
ABI_CHECK(callContractFunction("f(uint256)", 0x7f), encodeArgs(true));
}
else
{
ABI_CHECK(callContractFunction("f(uint256)", 0x60), encodeArgs());
ABI_CHECK(callContractFunction("f(uint256)", 0x61), encodeArgs(true));
}
ABI_CHECK(callContractFunction("f(uint256)", 0x80), encodeArgs(true));
)
}
BOOST_AUTO_TEST_CASE(out_of_bounds_bool_value)
{
string sourceCode = R"(
contract C {
function f(bool b) public pure returns (bool) { return b; }
}
)";
bool newDecoder = solidity::test::CommonOptions::get().useABIEncoderV2;
BOTH_ENCODERS(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(true));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(false));
ABI_CHECK(callContractFunctionNoEncoding("f(bool)", bytes(32, 0)), encodeArgs(0));
ABI_CHECK(callContractFunctionNoEncoding("f(bool)", bytes(32, 0xff)), newDecoder ? encodeArgs() : encodeArgs(1));
newDecoder = true;
)
}
BOOST_AUTO_TEST_SUITE_END()
} // end namespaces

View File

@ -0,0 +1,10 @@
contract C {
function f(bool b) public pure returns (bool) { return b; }
}
// ====
// ABIEncoderV1Only: true
// ----
// f(bool): true -> true
// f(bool): false -> false
// f(bool): 0x000000 -> false
// f(bool): 0xffffff -> true

View File

@ -0,0 +1,16 @@
contract C {
function f(uint16 a, int16 b, address c, bytes3 d, bool e)
public pure returns (uint v, uint w, uint x, uint y, uint z) {
assembly { v := a w := b x := c y := d z := e}
}
}
// ====
// ABIEncoderV1Only: true
// ----
// f(uint16,int16,address,bytes3,bool): 1, 2, 3, "a", true -> 1, 2, 3, "a", true
// f(uint16,int16,address,bytes3,bool): 0xffffff, 0x1ffff, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "abcd", 1 -> 0xffff, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffffffffffffff, "abc", true
// f(uint16,int16,address,bytes3,bool): 0xffffff, 0, 0, "bcd", 1 -> 0xffff, 0, 0, "bcd", true
// f(uint16,int16,address,bytes3,bool): 0, 0x1ffff, 0, "ab", 1 -> 0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, 0, "ab", true
// f(uint16,int16,address,bytes3,bool): 0, 0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "ad", 1 -> 0, 0, 0xffffffffffffffffffffffffffffffffffffffff, "ad", true
// f(uint16,int16,address,bytes3,bool): 0, 0, 0, "abcd", 1 -> 0, 0, 0, "abc", true
// f(uint16,int16,address,bytes3,bool): 0, 0, 0, "abc", 2 -> 0, 0, 0, "abc", true

View File

@ -0,0 +1,18 @@
contract C {
function dyn() public returns (bytes memory a, uint b, bytes20[] memory c, uint d) {
a = "1234567890123456789012345678901234567890";
b = uint(-1);
c = new bytes20[](4);
c[0] = bytes20(uint160(1234));
c[3] = bytes20(uint160(6789));
d = 0x1234;
}
function f() public returns (bytes memory, uint, bytes20[] memory, uint) {
return this.dyn();
}
}
// ====
// compileViaYul: also
// EVMVersion: >homestead
// ----
// f() -> 0x80, -1, 0xe0, 0x1234, 40, "12345678901234567890123456789012", "34567890", 4, 97767552542602192590433234714624, 0, 0, 537879995309340587922569878831104

View File

@ -0,0 +1,20 @@
contract C {
function dyn(uint x) public returns (bytes memory a) {
assembly {
mstore(0, 0x20)
mstore(0x20, 0x21)
return(0, x)
}
}
function f(uint x) public returns (bool) {
this.dyn(x);
return true;
}
}
// ====
// compileViaYul: also
// EVMVersion: =homestead
// ----
// f(uint256): 0x60 -> true
// f(uint256): 0x7f -> true
// f(uint256): 0x80 -> true

View File

@ -0,0 +1,20 @@
contract C {
function dyn(uint x) public returns (bytes memory a) {
assembly {
mstore(0, 0x20)
mstore(0x20, 0x21)
return(0, x)
}
}
function f(uint x) public returns (bool) {
this.dyn(x);
return true;
}
}
// ====
// compileViaYul: also
// EVMVersion: >homestead
// ----
// f(uint256): 0x60 -> FAILURE
// f(uint256): 0x61 -> true
// f(uint256): 0x80 -> true

View File

@ -0,0 +1,13 @@
contract C {
function dyn() public returns (bytes memory) {
return "1234567890123456789012345678901234567890";
}
function f() public returns (bytes memory) {
return this.dyn();
}
}
// ====
// compileViaYul: also
// EVMVersion: >homestead
// ----
// f() -> 0x20, 40, "12345678901234567890123456789012", "34567890"

View File

@ -1,12 +0,0 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint16[3] memory a, uint16[2][3] memory b, uint i, uint j, uint k)
public pure returns (uint, uint) {
return (a[i], b[j][k]);
}
}
// ====
// compileViaYul: also
// ----
// f(uint16[3],uint16[2][3],uint256,uint256,uint256): 1, 2, 3, 11, 12, 21, 22, 31, 32, 1, 2, 1 -> 2, 32

View File

@ -0,0 +1,12 @@
pragma experimental ABIEncoderV2;
contract C {
function f(bool b) public pure returns (bool) { return b; }
}
// ====
// compileViaYul: also
// ----
// f(bool): true -> true
// f(bool): false -> false
// f(bool): 0x000000 -> false
// f(bool): 0xffffff -> FAILURE

View File

@ -1,11 +0,0 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint a, uint[] calldata b, uint c) external pure returns (uint) {
return 7;
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,uint256[],uint256): 6, 0x60, 9, 0x8000000000000000000000000000000000000000000000000000000000000002, 1, 2 -> FAILURE

View File

@ -0,0 +1,18 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint16 a, int16 b, address c, bytes3 d, bool e)
public pure returns (uint v, uint w, uint x, uint y, uint z) {
assembly { v := a w := b x := c y := d z := e}
}
}
// ====
// compileViaYul: also
// ----
// f(uint16,int16,address,bytes3,bool): 1, 2, 3, "a", true -> 1, 2, 3, "a", true
// f(uint16,int16,address,bytes3,bool): 0xffffff, 0x1ffff, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "abcd", 1 -> FAILURE
// f(uint16,int16,address,bytes3,bool): 0xffffff, 0, 0, "bcd", 1 -> FAILURE
// f(uint16,int16,address,bytes3,bool): 0, 0x1ffff, 0, "ab", 1 -> FAILURE
// f(uint16,int16,address,bytes3,bool): 0, 0, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, "ad", 1 -> FAILURE
// f(uint16,int16,address,bytes3,bool): 0, 0, 0, "abcd", 1 -> FAILURE
// f(uint16,int16,address,bytes3,bool): 0, 0, 0, "abc", 2 -> FAILURE