From 44c0d7ca5e1d97027e4f1e24fb02fd805eda578d Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 21 Mar 2018 14:45:32 +0100 Subject: Tests. --- test/libsolidity/SolidityEndToEndTest.cpp | 221 +++++++++++++++++---- test/libsolidity/SolidityNameAndTypeResolution.cpp | 63 ------ .../syntaxTests/tight_packing_literals.sol | 25 +++ .../syntaxTests/tight_packing_literals_fine.sol | 11 + 4 files changed, 218 insertions(+), 102 deletions(-) create mode 100644 test/libsolidity/syntaxTests/tight_packing_literals.sol create mode 100644 test/libsolidity/syntaxTests/tight_packing_literals_fine.sol (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index a5b990ac..8b0e3f83 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -11095,69 +11095,212 @@ BOOST_AUTO_TEST_CASE(abi_encode) { char const* sourceCode = R"( contract C { - function f() returns (bytes) { + function f0() returns (bytes) { + return abi.encode(); + } + function f1() returns (bytes) { return abi.encode(1, 2); } - function g() returns (bytes) { - return abi.encodePacked(uint256(1), uint256(2)); + function f2() returns (bytes) { + string memory x = "abc"; + return abi.encode(1, x, 2); + } + function f3() returns (bytes r) { + // test that memory is properly allocated + string memory x = "abc"; + r = abi.encode(1, x, 2); + bytes memory y = "def"; + require(y[0] == "d"); + y[0] = "e"; + require(y[0] == "e"); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 0)); + ABI_CHECK(callContractFunction("f1()"), encodeArgs(0x20, 0x40, 1, 2)); + ABI_CHECK(callContractFunction("f2()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); + ABI_CHECK(callContractFunction("f3()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); +} + +BOOST_AUTO_TEST_CASE(abi_encode_v2) +{ + char const* sourceCode = R"( + pragma experimental ABIEncoderV2; + contract C { + struct S { uint a; uint[] b; } + function f0() public pure returns (bytes) { + return abi.encode(); + } + function f1() public pure returns (bytes) { + return abi.encode(1, 2); + } + function f2() public pure returns (bytes) { + string memory x = "abc"; + return abi.encode(1, x, 2); + } + function f3() public pure returns (bytes r) { + // test that memory is properly allocated + string memory x = "abc"; + r = abi.encode(1, x, 2); + bytes memory y = "def"; + require(y[0] == "d"); + y[0] = "e"; + require(y[0] == "e"); + } + S s; + function f4() public view returns (bytes r) { + string memory x = "abc"; + s.a = 7; + s.b.push(2); + s.b.push(3); + r = abi.encode(1, x, s, 2); + bytes memory y = "def"; + require(y[0] == "d"); + y[0] = "e"; + require(y[0] == "e"); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 0)); + ABI_CHECK(callContractFunction("f1()"), encodeArgs(0x20, 0x40, 1, 2)); + ABI_CHECK(callContractFunction("f2()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); + ABI_CHECK(callContractFunction("f3()"), encodeArgs(0x20, 0xa0, 1, 0x60, 2, 3, "abc")); + ABI_CHECK(callContractFunction("f4()"), encodeArgs(0x20, 0x160, 1, 0x80, 0xc0, 2, 3, "abc", 7, 0x40, 2, 2, 3)); +} + + +BOOST_AUTO_TEST_CASE(abi_encodePacked) +{ + char const* sourceCode = R"( + contract C { + function f0() public pure returns (bytes) { + return abi.encodePacked(); + } + function f1() public pure returns (bytes) { + return abi.encodePacked(uint8(1), uint8(2)); + } + function f2() public pure returns (bytes) { + string memory x = "abc"; + return abi.encodePacked(uint8(1), x, uint8(2)); + } + function f3() public pure returns (bytes r) { + // test that memory is properly allocated + string memory x = "abc"; + r = abi.encodePacked(uint8(1), x, uint8(2)); + bytes memory y = "def"; + require(y[0] == "d"); + y[0] = "e"; + require(y[0] == "e"); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 0)); + ABI_CHECK(callContractFunction("f1()"), encodeArgs(0x20, 2, "\x01\x02")); + ABI_CHECK(callContractFunction("f2()"), encodeArgs(0x20, 5, "\x01" "abc" "\x02")); + ABI_CHECK(callContractFunction("f3()"), encodeArgs(0x20, 5, "\x01" "abc" "\x02")); +} + +BOOST_AUTO_TEST_CASE(abi_encode_with_selector) +{ + char const* sourceCode = R"( + contract C { + function f0() public pure returns (bytes) { + return abi.encodeWithSelector(0x12345678); + } + function f1() public pure returns (bytes) { + return abi.encodeWithSelector(0x12345678, "abc"); + } + function f2() public pure returns (bytes) { + bytes4 x = 0x12345678; + return abi.encodeWithSelector(x, "abc"); + } + function f3() public pure returns (bytes) { + bytes4 x = 0x12345678; + return abi.encodeWithSelector(x, uint(-1)); } } )"; compileAndRun(sourceCode, 0, "C"); - ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(0x20), u256(0x40), u256(1), u256(2))); - ABI_CHECK(callContractFunction("g()"), encodeArgs(u256(0x20), u256(0x40), u256(1), u256(2))); + 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_complex_call) +BOOST_AUTO_TEST_CASE(abi_encode_with_signature) { char const* sourceCode = R"T( contract C { - function f(uint8 a, string b, string c) { - require(a == 42); - require(bytes(b).length == 2); - require(bytes(b)[0] == 72); // 'H' - require(bytes(b)[1] == 101); // 'e' - require(keccak256(b) == keccak256(c)); - } - function g(uint8 a, string b) returns (bool) { - bytes request = abi.encode( - bytes4(keccak256(abi.encodePacked("f(uint8,string)"))), - a, - b, - "He" - ); - return this.call(request); + function f0() public pure returns (bytes) { + return abi.encodeWithSignature("f(uint256)"); + } + function f1() public pure returns (bytes) { + string memory x = "f(uint256)"; + return abi.encodeWithSignature(x, "abc"); + } + string xstor; + function f1s() public returns (bytes) { + xstor = "f(uint256)"; + return abi.encodeWithSignature(xstor, "abc"); + } + function f2() public pure returns (bytes r, uint[] 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] = uint(-1); + y[1] = uint(-2); + y[2] = uint(-3); + y[3] = uint(-4); + 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("g(uint8,string)", u256(42), u256(0x40), u256(2), string("He")), encodeArgs(u256(1))); + 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_encodewithselector_complex_call) +BOOST_AUTO_TEST_CASE(abi_encode_call) { char const* sourceCode = R"T( contract C { - function f(uint8 a, string b, string c) { - require(a == 42); - require(bytes(b).length == 2); - require(bytes(b)[0] == 72); // 'H' - require(bytes(b)[1] == 101); // 'e' - require(keccak256(b) == keccak256(c)); - } - function g(uint8 a, string b) returns (bool) { - bytes request = abi.encodeWithSignature( - "f(uint8,string)", - a, - b, - "He" - ); - return this.call(request); + bool x; + function c(uint a, uint[] b) public { + require(a == 5); + require(b.length == 2); + require(b[0] == 6); + require(b[1] == 7); + x = true; + } + function f() public returns (bool) { + uint a = 5; + uint[] memory b = new uint[](2); + b[0] = 6; + b[1] = 7; + require(this.call(abi.encodeWithSignature("c(uint256,uint256[])", a, b))); + return x; } } )T"; compileAndRun(sourceCode, 0, "C"); - ABI_CHECK(callContractFunction("g(uint8,string)", u256(42), u256(0x40), u256(2), string("He")), encodeArgs(u256(1))); + ABI_CHECK(callContractFunction("f()"), encodeArgs(true)); } BOOST_AUTO_TEST_CASE(staticcall_for_view_and_pure) diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 7678bf4a..a301e6d2 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -7075,69 +7075,6 @@ BOOST_AUTO_TEST_CASE(reject_interface_constructors) CHECK_ERROR(text, TypeError, "Wrong argument count for constructor call: 1 arguments given but expected 0."); } -BOOST_AUTO_TEST_CASE(tight_packing_literals) -{ - char const* text = R"( - contract C { - function f() pure public returns (bytes32) { - return keccak256(1); - } - } - )"; - CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); - text = R"( - contract C { - function f() pure public returns (bytes32) { - return keccak256(uint8(1)); - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); - text = R"( - contract C { - function f() pure public returns (bytes32) { - return sha3(1); - } - } - )"; - CHECK_WARNING_ALLOW_MULTI(text, (std::vector{ - "The type of \"int_const 1\" was inferred as uint8.", - "\"sha3\" has been deprecated in favour of \"keccak256\"" - })); - text = R"( - contract C { - function f() pure public returns (bytes32) { - return sha256(1); - } - } - )"; - CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); - text = R"( - contract C { - function f() pure public returns (bytes32) { - return ripemd160(1); - } - } - )"; - CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); - text = R"( - contract C { - function f() pure public returns (bytes) { - return abi.encode(1); - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); - text = R"( - contract C { - function f() pure public returns (bytes) { - return abi.encodePacked(1); - } - } - )"; - CHECK_WARNING(text, "The type of \"int_const 1\" was inferred as uint8."); -} - BOOST_AUTO_TEST_CASE(non_external_fallback) { char const* text = R"( diff --git a/test/libsolidity/syntaxTests/tight_packing_literals.sol b/test/libsolidity/syntaxTests/tight_packing_literals.sol new file mode 100644 index 00000000..8258a8a6 --- /dev/null +++ b/test/libsolidity/syntaxTests/tight_packing_literals.sol @@ -0,0 +1,25 @@ +contract C { + function f() pure public returns (bytes32) { + return keccak256(1); + } + function g() pure public returns (bytes32) { + return sha3(1); + } + function h() pure public returns (bytes32) { + return sha256(1); + } + function j() pure public returns (bytes32) { + return ripemd160(1); + } + function k() pure public returns (bytes) { + return abi.encodePacked(1); + } +} + +// ---- +// Warning: (87-88): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning. +// Warning: (161-168): "sha3" has been deprecated in favour of "keccak256" +// Warning: (166-167): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning. +// Warning: (247-248): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning. +// Warning: (331-332): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning. +// Warning: (420-421): The type of "int_const 1" was inferred as uint8. This is probably not desired. Use an explicit type to silence this warning. diff --git a/test/libsolidity/syntaxTests/tight_packing_literals_fine.sol b/test/libsolidity/syntaxTests/tight_packing_literals_fine.sol new file mode 100644 index 00000000..46407f71 --- /dev/null +++ b/test/libsolidity/syntaxTests/tight_packing_literals_fine.sol @@ -0,0 +1,11 @@ +contract C { + function f() pure public returns (bytes32) { + return keccak256(uint8(1)); + } + function g() pure public returns (bytes) { + return abi.encode(1); + } + function h() pure public returns (bytes) { + return abi.encodePacked(uint8(1)); + } +} -- cgit