From 879251a78b2d4e26dc71299d2d7ca989d0855d61 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 12 Sep 2018 16:21:43 +0200 Subject: Update test suite to use address payable. --- test/libsolidity/SolidityEndToEndTest.cpp | 62 +++++++++++++++++++--- .../nameAndTypeResolution/413_address_methods.sol | 2 +- .../535_address_overload_resolution.sol | 2 + .../types/address/address_members_in_contract.sol | 6 +++ .../types/address/address_to_contract.sol | 6 +++ .../types/address/contract_to_address.sol | 7 +++ .../address/contract_to_address_implicitly.sol | 8 +++ .../types/address_members_in_contract.sol | 6 --- .../syntaxTests/types/address_to_contract.sol | 6 --- .../syntaxTests/types/contract_to_address.sol | 7 --- .../types/contract_to_address_implicitly.sol | 8 --- .../builtin_functions_view_fail.sol | 2 + 12 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 test/libsolidity/syntaxTests/types/address/address_members_in_contract.sol create mode 100644 test/libsolidity/syntaxTests/types/address/address_to_contract.sol create mode 100644 test/libsolidity/syntaxTests/types/address/contract_to_address.sol create mode 100644 test/libsolidity/syntaxTests/types/address/contract_to_address_implicitly.sol delete mode 100644 test/libsolidity/syntaxTests/types/address_members_in_contract.sol delete mode 100644 test/libsolidity/syntaxTests/types/address_to_contract.sol delete mode 100644 test/libsolidity/syntaxTests/types/contract_to_address.sol delete mode 100644 test/libsolidity/syntaxTests/types/contract_to_address_implicitly.sol (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 6be9d95b..642c9929 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -2215,7 +2215,7 @@ BOOST_AUTO_TEST_CASE(send_ether) char const* sourceCode = R"( contract test { constructor() payable public {} - function a(address addr, uint amount) public returns (uint ret) { + function a(address payable addr, uint amount) public returns (uint ret) { addr.send(amount); return address(this).balance; } @@ -2233,11 +2233,11 @@ BOOST_AUTO_TEST_CASE(transfer_ether) char const* sourceCode = R"( contract A { constructor() public payable {} - function a(address addr, uint amount) public returns (uint) { + function a(address payable addr, uint amount) public returns (uint) { addr.transfer(amount); return address(this).balance; } - function b(address addr, uint amount) public { + function b(address payable addr, uint amount) public { addr.transfer(amount); } } @@ -2406,7 +2406,7 @@ BOOST_AUTO_TEST_CASE(selfdestruct) char const* sourceCode = R"( contract test { constructor() public payable {} - function a(address receiver) public returns (uint ret) { + function a(address payable receiver) public returns (uint ret) { selfdestruct(receiver); return 10; } @@ -7151,7 +7151,7 @@ BOOST_AUTO_TEST_CASE(failing_send) } contract Main { constructor() public payable {} - function callHelper(address _a) public returns (bool r, uint bal) { + function callHelper(address payable _a) public returns (bool r, uint bal) { r = !_a.send(5); bal = address(this).balance; } @@ -8838,7 +8838,7 @@ BOOST_AUTO_TEST_CASE(reject_ether_sent_to_library) library lib {} contract c { constructor() public payable {} - function f(address x) public returns (bool) { + function f(address payable x) public returns (bool) { return x.send(1); } function () external payable {} @@ -10095,6 +10095,54 @@ BOOST_AUTO_TEST_CASE(cleanup_bytes_types_shortening) ABI_CHECK(callContractFunction("f()"), encodeArgs("\xff\xff\xff\xff")); } +BOOST_AUTO_TEST_CASE(cleanup_address_types) +{ + // Checks that address types are properly cleaned before they are compared. + char const* sourceCode = R"( + contract C { + function f(address a) public returns (uint) { + if (a != 0x1234567890123456789012345678901234567890) return 1; + return 0; + } + function g(address payable a) public returns (uint) { + if (a != 0x1234567890123456789012345678901234567890) return 1; + return 0; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + // We input longer data on purpose. + ABI_CHECK(callContractFunction("f(address)", u256("0xFFFF1234567890123456789012345678901234567890")), encodeArgs(0)); + ABI_CHECK(callContractFunction("g(address)", u256("0xFFFF1234567890123456789012345678901234567890")), encodeArgs(0)); +} + +BOOST_AUTO_TEST_CASE(cleanup_address_types_shortening) +{ + char const* sourceCode = R"( + contract C { + function f() public pure returns (address r) { + bytes21 x = 0x1122334455667788990011223344556677889900ff; + bytes20 y; + assembly { y := x } + address z = address(y); + assembly { r := z } + require(z == 0x1122334455667788990011223344556677889900); + } + function g() public pure returns (address payable r) { + bytes21 x = 0x1122334455667788990011223344556677889900ff; + bytes20 y; + assembly { y := x } + address payable z = address(y); + assembly { r := z } + require(z == 0x1122334455667788990011223344556677889900); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + ABI_CHECK(callContractFunction("f()"), encodeArgs(u256("0x1122334455667788990011223344556677889900"))); + ABI_CHECK(callContractFunction("g()"), encodeArgs(u256("0x1122334455667788990011223344556677889900"))); +} + BOOST_AUTO_TEST_CASE(skip_dynamic_types) { // The EVM cannot provide access to dynamically-sized return values, so we have to skip them. @@ -12446,7 +12494,7 @@ BOOST_AUTO_TEST_CASE(interface_contract) } contract C { - function f(address _interfaceAddress) public returns (bool) { + function f(address payable _interfaceAddress) public returns (bool) { I i = I(_interfaceAddress); return i.f(); } diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol index b63d2a55..ad57224c 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol @@ -1,6 +1,6 @@ contract C { function f() public { - address addr; + address payable addr; uint balance = addr.balance; (bool callSuc,) = addr.call(""); (bool delegatecallSuc,) = addr.delegatecall(""); diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/535_address_overload_resolution.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/535_address_overload_resolution.sol index 157ea36b..01b7b294 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/535_address_overload_resolution.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/535_address_overload_resolution.sol @@ -6,6 +6,8 @@ contract C { function transfer(uint amount) public { address(this).transfer(amount); // to avoid pureness warning } + function() payable external { + } } contract D { function f() public { diff --git a/test/libsolidity/syntaxTests/types/address/address_members_in_contract.sol b/test/libsolidity/syntaxTests/types/address/address_members_in_contract.sol new file mode 100644 index 00000000..eafc8268 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/address_members_in_contract.sol @@ -0,0 +1,6 @@ +contract C { + function f() public returns (C) { return this; } + function g() public returns (uint) { return f().balance(); } +} +// ---- +// TypeError: (114-125): Member "balance" not found or not visible after argument-dependent lookup in contract C. Use "address(...).balance" to access this address member. diff --git a/test/libsolidity/syntaxTests/types/address/address_to_contract.sol b/test/libsolidity/syntaxTests/types/address/address_to_contract.sol new file mode 100644 index 00000000..629a3df0 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/address_to_contract.sol @@ -0,0 +1,6 @@ +contract C { + function f() public pure returns (C c) { + c = C(address(2)); + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/types/address/contract_to_address.sol b/test/libsolidity/syntaxTests/types/address/contract_to_address.sol new file mode 100644 index 00000000..ec2f8184 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/contract_to_address.sol @@ -0,0 +1,7 @@ +contract C { + function f() public view { + address a = address(this); + a; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/types/address/contract_to_address_implicitly.sol b/test/libsolidity/syntaxTests/types/address/contract_to_address_implicitly.sol new file mode 100644 index 00000000..8be9daac --- /dev/null +++ b/test/libsolidity/syntaxTests/types/address/contract_to_address_implicitly.sol @@ -0,0 +1,8 @@ +contract C { + function f() public view { + address a = this; + a; + } +} +// ---- +// TypeError: (46-62): Type contract C is not implicitly convertible to expected type address. diff --git a/test/libsolidity/syntaxTests/types/address_members_in_contract.sol b/test/libsolidity/syntaxTests/types/address_members_in_contract.sol deleted file mode 100644 index eafc8268..00000000 --- a/test/libsolidity/syntaxTests/types/address_members_in_contract.sol +++ /dev/null @@ -1,6 +0,0 @@ -contract C { - function f() public returns (C) { return this; } - function g() public returns (uint) { return f().balance(); } -} -// ---- -// TypeError: (114-125): Member "balance" not found or not visible after argument-dependent lookup in contract C. Use "address(...).balance" to access this address member. diff --git a/test/libsolidity/syntaxTests/types/address_to_contract.sol b/test/libsolidity/syntaxTests/types/address_to_contract.sol deleted file mode 100644 index 629a3df0..00000000 --- a/test/libsolidity/syntaxTests/types/address_to_contract.sol +++ /dev/null @@ -1,6 +0,0 @@ -contract C { - function f() public pure returns (C c) { - c = C(address(2)); - } -} -// ---- diff --git a/test/libsolidity/syntaxTests/types/contract_to_address.sol b/test/libsolidity/syntaxTests/types/contract_to_address.sol deleted file mode 100644 index ec2f8184..00000000 --- a/test/libsolidity/syntaxTests/types/contract_to_address.sol +++ /dev/null @@ -1,7 +0,0 @@ -contract C { - function f() public view { - address a = address(this); - a; - } -} -// ---- diff --git a/test/libsolidity/syntaxTests/types/contract_to_address_implicitly.sol b/test/libsolidity/syntaxTests/types/contract_to_address_implicitly.sol deleted file mode 100644 index 8be9daac..00000000 --- a/test/libsolidity/syntaxTests/types/contract_to_address_implicitly.sol +++ /dev/null @@ -1,8 +0,0 @@ -contract C { - function f() public view { - address a = this; - a; - } -} -// ---- -// TypeError: (46-62): Type contract C is not implicitly convertible to expected type address. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions_view_fail.sol b/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions_view_fail.sol index f951feb4..5356f0b8 100644 --- a/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions_view_fail.sol +++ b/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions_view_fail.sol @@ -16,6 +16,8 @@ contract C { (bool success,) = address(this).call(""); require(success); } + function() payable external { + } } // ---- // TypeError: (52-77): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. -- cgit