Enable some tests.

This commit is contained in:
chriseth 2021-01-18 13:44:40 +01:00
parent 4f3839e4ba
commit 611d2b4de5
5 changed files with 136 additions and 180 deletions

View File

@ -5317,186 +5317,6 @@ BOOST_AUTO_TEST_CASE(event_signature_in_library)
)
}
BOOST_AUTO_TEST_CASE(abi_encode_with_selector)
{
char const* sourceCode = R"(
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678);
}
function f1() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678, "abc");
}
function f2() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, "abc");
}
function f3() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, type(uint).max);
}
}
)";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\x12\x34\x56\x78"));
bytes expectation;
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f1()"), expectation);
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f2()"), expectation);
expectation = encodeArgs(0x20, 4 + 0x20) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(u256(-1)) + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f3()"), expectation);
}
BOOST_AUTO_TEST_CASE(abi_encode_with_selectorv2)
{
char const* sourceCode = R"(
pragma abicoder v2;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678);
}
function f1() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678, "abc");
}
function f2() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, "abc");
}
function f3() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, type(uint).max);
}
struct S { uint a; string b; uint16 c; }
function f4() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
S memory s;
s.a = 0x1234567;
s.b = "Lorem ipsum dolor sit ethereum........";
s.c = 0x1234;
return abi.encodeWithSelector(x, type(uint).max, s, uint(3));
}
}
)";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\x12\x34\x56\x78"));
bytes expectation;
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f1()"), expectation);
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f2()"), expectation);
expectation = encodeArgs(0x20, 4 + 0x20) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(u256(-1)) + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f3()"), expectation);
expectation =
encodeArgs(0x20, 4 + 0x120) +
bytes{0x12, 0x34, 0x56, 0x78} +
encodeArgs(u256(-1), 0x60, u256(3), 0x1234567, 0x60, 0x1234, 38, "Lorem ipsum dolor sit ethereum........") +
bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f4()"), expectation);
}
BOOST_AUTO_TEST_CASE(abi_encode_with_signature)
{
char const* sourceCode = R"T(
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSignature("f(uint256)");
}
function f1() public pure returns (bytes memory) {
string memory x = "f(uint256)";
return abi.encodeWithSignature(x, "abc");
}
string xstor;
function f1s() public returns (bytes memory) {
xstor = "f(uint256)";
return abi.encodeWithSignature(xstor, "abc");
}
function f2() public pure returns (bytes memory r, uint[] memory ar) {
string memory x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
uint[] memory y = new uint[](4);
y[0] = type(uint).max;
y[1] = type(uint).max - 1;
y[2] = type(uint).max - 2;
y[3] = type(uint).max - 3;
r = abi.encodeWithSignature(x, y);
// The hash uses temporary memory. This allocation re-uses the memory
// and should initialize it properly.
ar = new uint[](2);
}
}
)T";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\xb3\xde\x64\x8b"));
bytes expectation;
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0xb3, 0xde, 0x64, 0x8b} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f1()"), expectation);
ABI_CHECK(callContractFunction("f1s()"), expectation);
expectation =
encodeArgs(0x40, 0x140, 4 + 0xc0) +
(bytes{0xe9, 0xc9, 0x21, 0xcd} + encodeArgs(0x20, 4, u256(-1), u256(-2), u256(-3), u256(-4)) + bytes(0x20 - 4)) +
encodeArgs(2, 0, 0);
ABI_CHECK(callContractFunction("f2()"), expectation);
}
BOOST_AUTO_TEST_CASE(abi_encode_with_signaturev2)
{
char const* sourceCode = R"T(
pragma abicoder v2;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSignature("f(uint256)");
}
function f1() public pure returns (bytes memory) {
string memory x = "f(uint256)";
return abi.encodeWithSignature(x, "abc");
}
string xstor;
function f1s() public returns (bytes memory) {
xstor = "f(uint256)";
return abi.encodeWithSignature(xstor, "abc");
}
function f2() public pure returns (bytes memory r, uint[] memory ar) {
string memory x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
uint[] memory y = new uint[](4);
y[0] = type(uint).max;
y[1] = type(uint).max - 1;
y[2] = type(uint).max - 2;
y[3] = type(uint).max - 3;
r = abi.encodeWithSignature(x, y);
// The hash uses temporary memory. This allocation re-uses the memory
// and should initialize it properly.
ar = new uint[](2);
}
struct S { uint a; string b; uint16 c; }
function f4() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
S memory s;
s.a = 0x1234567;
s.b = "Lorem ipsum dolor sit ethereum........";
s.c = 0x1234;
return abi.encodeWithSignature(s.b, type(uint).max, s, uint(3));
}
}
)T";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\xb3\xde\x64\x8b"));
bytes expectation;
expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0xb3, 0xde, 0x64, 0x8b} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f1()"), expectation);
ABI_CHECK(callContractFunction("f1s()"), expectation);
expectation =
encodeArgs(0x40, 0x140, 4 + 0xc0) +
(bytes{0xe9, 0xc9, 0x21, 0xcd} + encodeArgs(0x20, 4, u256(-1), u256(-2), u256(-3), u256(-4)) + bytes(0x20 - 4)) +
encodeArgs(2, 0, 0);
ABI_CHECK(callContractFunction("f2()"), expectation);
expectation =
encodeArgs(0x20, 4 + 0x120) +
bytes{0x7c, 0x79, 0x30, 0x02} +
encodeArgs(u256(-1), 0x60, u256(3), 0x1234567, 0x60, 0x1234, 38, "Lorem ipsum dolor sit ethereum........") +
bytes(0x20 - 4);
ABI_CHECK(callContractFunction("f4()"), expectation);
}
BOOST_AUTO_TEST_CASE(code_access)
{
char const* sourceCode = R"(

View File

@ -0,0 +1,24 @@
pragma abicoder v1;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678);
}
function f1() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678, "abc");
}
function f2() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, "abc");
}
function f3() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, type(uint).max);
}
}
// ====
// compileViaYul: also
// ----
// f0() -> 0x20, 4, 8234104107246695022420661102507966550300666591269321702959126607540084801536
// f1() -> 0x20, 0x64, 8234104107246695022420661102507966550300666591269321702959126607540084801536, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f2() -> 0x20, 0x64, 8234104107246695022420661102507966550300666591269321702959126607540084801536, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f3() -> 0x20, 0x24, 0x12345678ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -26959946667150639794667015087019630673637144422540572481103610249216

View File

@ -0,0 +1,34 @@
pragma abicoder v2;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678);
}
function f1() public pure returns (bytes memory) {
return abi.encodeWithSelector(0x12345678, "abc");
}
function f2() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, "abc");
}
function f3() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
return abi.encodeWithSelector(x, type(uint).max);
}
struct S { uint a; string b; uint16 c; }
function f4() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
S memory s;
s.a = 0x1234567;
s.b = "Lorem ipsum dolor sit ethereum........";
s.c = 0x1234;
return abi.encodeWithSelector(x, type(uint).max, s, uint(3));
}
}
// ====
// compileViaYul: also
// ----
// f0() -> 0x20, 4, 8234104107246695022420661102507966550300666591269321702959126607540084801536
// f1() -> 0x20, 0x64, 8234104107246695022420661102507966550300666591269321702959126607540084801536, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f2() -> 0x20, 0x64, 8234104107246695022420661102507966550300666591269321702959126607540084801536, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f3() -> 0x20, 0x24, 0x12345678ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -26959946667150639794667015087019630673637144422540572481103610249216
// f4() -> 0x20, 292, 0x12345678ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -26959946667150639794667015087019630673637144422540572481103610249216, 2588154880046461420288033448353884544669165864563894958185946583924736, 80879840001451919384001045261058892020911433267621717443310830747648, 514631493222945105325971421573240365883976325135760395164659172419450175488, 2588154880046461420288033448353884544669165864563894958185946583924736, 125633351468921981443148290305511478939149093009039067761942823761346560, 0x264c6f72656d20697073756d20646f6c6f722073697420657468657265, 53113508339655873314659021564971517366334151400493876485713881232784043802624, 0

View File

@ -0,0 +1,34 @@
pragma abicoder v1;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSignature("f(uint256)");
}
function f1() public pure returns (bytes memory) {
string memory x = "f(uint256)";
return abi.encodeWithSignature(x, "abc");
}
string xstor;
function f1s() public returns (bytes memory) {
xstor = "f(uint256)";
return abi.encodeWithSignature(xstor, "abc");
}
function f2() public pure returns (bytes memory r, uint[] memory ar) {
string memory x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
uint[] memory y = new uint[](4);
y[0] = type(uint).max;
y[1] = type(uint).max - 1;
y[2] = type(uint).max - 2;
y[3] = type(uint).max - 3;
r = abi.encodeWithSignature(x, y);
// The hash uses temporary memory. This allocation re-uses the memory
// and should initialize it properly.
ar = new uint[](2);
}
}
// ====
// compileViaYul: also
// ----
// f0() -> 0x20, 4, -34435155370463444793260793355178157075203752403645521721995013737368954863616
// f1() -> 0x20, 0x64, -34435155370463444793260793355178157075203752403645521721995013737368954863616, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f1s() -> 0x20, 0x64, -34435155370463444793260793355178157075203752403645521721995013737368954863616, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f2() -> 0x40, 0x0140, 0xc4, -10047825972976160827854069633043429618646681939320956771263895477211642200064, 862718293348820473429344482784628181556388621521298319395315527974912, 0x04ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -1, -26959946667150639794667015087019630673637144422540572481103610249217, -53919893334301279589334030174039261347274288845081144962207220498433, -107839786668602559178668060348078522694548577690162289924414440996864, 2, 0, 0

View File

@ -0,0 +1,44 @@
pragma abicoder v2;
contract C {
function f0() public pure returns (bytes memory) {
return abi.encodeWithSignature("f(uint256)");
}
function f1() public pure returns (bytes memory) {
string memory x = "f(uint256)";
return abi.encodeWithSignature(x, "abc");
}
string xstor;
function f1s() public returns (bytes memory) {
xstor = "f(uint256)";
return abi.encodeWithSignature(xstor, "abc");
}
function f2() public pure returns (bytes memory r, uint[] memory ar) {
string memory x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
uint[] memory y = new uint[](4);
y[0] = type(uint).max;
y[1] = type(uint).max - 1;
y[2] = type(uint).max - 2;
y[3] = type(uint).max - 3;
r = abi.encodeWithSignature(x, y);
// The hash uses temporary memory. This allocation re-uses the memory
// and should initialize it properly.
ar = new uint[](2);
}
struct S { uint a; string b; uint16 c; }
function f4() public pure returns (bytes memory) {
bytes4 x = 0x12345678;
S memory s;
s.a = 0x1234567;
s.b = "Lorem ipsum dolor sit ethereum........";
s.c = 0x1234;
return abi.encodeWithSignature(s.b, type(uint).max, s, uint(3));
}
}
// ====
// compileViaYul: also
// ----
// f0() -> 0x20, 4, -34435155370463444793260793355178157075203752403645521721995013737368954863616
// f1() -> 0x20, 0x64, -34435155370463444793260793355178157075203752403645521721995013737368954863616, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f1s() -> 0x20, 0x64, -34435155370463444793260793355178157075203752403645521721995013737368954863616, 862718293348820473429344482784628181556388621521298319395315527974912, 91135606241822717681769169345594720818313984248279388438121731325952, 0
// f2() -> 0x40, 0x0140, 0xc4, -10047825972976160827854069633043429618646681939320956771263895477211642200064, 862718293348820473429344482784628181556388621521298319395315527974912, 0x04ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -1, -26959946667150639794667015087019630673637144422540572481103610249217, -53919893334301279589334030174039261347274288845081144962207220498433, -107839786668602559178668060348078522694548577690162289924414440996864, 2, 0, 0
// f4() -> 0x20, 292, 0x7c793002ffffffffffffffffffffffffffffffffffffffffffffffffffffffff, -26959946667150639794667015087019630673637144422540572481103610249216, 2588154880046461420288033448353884544669165864563894958185946583924736, 80879840001451919384001045261058892020911433267621717443310830747648, 514631493222945105325971421573240365883976325135760395164659172419450175488, 2588154880046461420288033448353884544669165864563894958185946583924736, 125633351468921981443148290305511478939149093009039067761942823761346560, 0x264c6f72656d20697073756d20646f6c6f722073697420657468657265, 53113508339655873314659021564971517366334151400493876485713881232784043802624, 0