From b2a3e165cb6a7a057af0427bd4802e52f562923d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 4 Jun 2018 12:54:32 +0100 Subject: Extract test cases from ViewPureChecker --- test/libsolidity/ViewPureChecker.cpp | 359 ----------------------------------- 1 file changed, 359 deletions(-) (limited to 'test/libsolidity/ViewPureChecker.cpp') diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp index cd0a0b01..53761ff2 100644 --- a/test/libsolidity/ViewPureChecker.cpp +++ b/test/libsolidity/ViewPureChecker.cpp @@ -38,71 +38,6 @@ namespace test BOOST_FIXTURE_TEST_SUITE(ViewPureChecker, AnalysisFramework) -BOOST_AUTO_TEST_CASE(smoke_test) -{ - char const* text = R"( - contract C { - uint x; - function g() pure public {} - function f() view public returns (uint) { return now; } - function h() public { x = 2; } - function i() payable public { x = 2; } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(call_internal_functions_success) -{ - char const* text = R"( - contract C { - function g() pure public { g(); } - function f() view public returns (uint) { f(); g(); } - function h() public { h(); g(); f(); } - function i() payable public { i(); h(); g(); f(); } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(suggest_pure) -{ - char const* text = R"( - contract C { - function g() view public { } - } - )"; - CHECK_WARNING(text, "can be restricted to pure"); -} - -BOOST_AUTO_TEST_CASE(suggest_view) -{ - char const* text = R"( - contract C { - uint x; - function g() public returns (uint) { return x; } - } - )"; - CHECK_WARNING(text, "can be restricted to view"); -} - -BOOST_AUTO_TEST_CASE(call_internal_functions_fail) -{ - CHECK_ERROR( - "contract C{ function f() pure public { g(); } function g() view public {} }", - TypeError, - "Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires \"view\"" - ); -} - -BOOST_AUTO_TEST_CASE(write_storage_fail) -{ - CHECK_WARNING( - "contract C{ uint x; function f() view public { x = 2; } }", - "Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable." - ); -} - BOOST_AUTO_TEST_CASE(environment_access) { vector view{ @@ -163,275 +98,6 @@ BOOST_AUTO_TEST_CASE(environment_access) })); } -BOOST_AUTO_TEST_CASE(view_error_for_050) -{ - CHECK_ERROR( - "pragma experimental \"v0.5.0\"; contract C { uint x; function f() view public { x = 2; } }", - TypeError, - "Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable." - ); - -} - -BOOST_AUTO_TEST_CASE(modifiers) -{ - string text = R"( - contract D { - uint x; - modifier purem(uint) { _; } - modifier viewm(uint) { uint a = x; _; a; } - modifier nonpayablem(uint) { x = 2; _; } - } - contract C is D { - function f() purem(0) pure public {} - function g() viewm(0) view public {} - function h() nonpayablem(0) public {} - function i() purem(x) view public {} - function j() viewm(x) view public {} - function k() nonpayablem(x) public {} - function l() purem(x = 2) public {} - function m() viewm(x = 2) public {} - function n() nonpayablem(x = 2) public {} - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(interface) -{ - string text = R"( - interface D { - function f() view external; - } - contract C is D { - function f() view external {} - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(overriding) -{ - string text = R"( - contract D { - uint x; - function f() public { x = 2; } - } - contract C is D { - function f() public {} - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(returning_structs) -{ - string text = R"( - contract C { - struct S { uint x; } - S s; - function f() view internal returns (S storage) { - return s; - } - function g() public { - f().x = 2; - } - function h() view public { - f(); - f().x; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(mappings) -{ - string text = R"( - contract C { - mapping(uint => uint) a; - function f() view public { - a; - } - function g() view public { - a[2]; - } - function h() public { - a[2] = 3; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(local_storage_variables) -{ - string text = R"( - contract C { - struct S { uint a; } - S s; - function f() view public { - S storage x = s; - x; - } - function g() view public { - S storage x = s; - x = s; - } - function i() public { - s.a = 2; - } - function h() public { - S storage x = s; - x.a = 2; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(builtin_functions) -{ - string text = R"( - contract C { - function f() public { - address(this).transfer(1); - require(address(this).send(2)); - selfdestruct(address(this)); - require(address(this).delegatecall()); - require(address(this).call()); - } - function g() pure public { - bytes32 x = keccak256("abc"); - bytes32 y = sha256("abc"); - address z = ecrecover(1, 2, 3, 4); - require(true); - assert(true); - x; y; z; - } - function() payable public {} - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(function_types) -{ - string text = R"( - contract C { - function f() pure public { - function () external nonpayFun; - function () external view viewFun; - function () external pure pureFun; - - nonpayFun; - viewFun; - pureFun; - pureFun(); - } - function g() view public { - function () external view viewFun; - - viewFun(); - } - function h() public { - function () external nonpayFun; - - nonpayFun(); - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(selector) -{ - string text = R"( - contract C { - uint public x; - function f() payable public { - } - function g() pure public returns (bytes4) { - return this.f.selector ^ this.x.selector; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(selector_complex) -{ - string text = R"( - contract C { - function f(C c) pure public returns (C) { - return c; - } - function g() pure public returns (bytes4) { - // By passing `this`, we read from the state, even if f itself is pure. - return f(this).f.selector; - } - } - )"; - CHECK_ERROR(text, TypeError, "reads from the environment or state and thus requires \"view\""); -} - -BOOST_AUTO_TEST_CASE(selector_complex2) -{ - string text = R"( - contract C { - function f() payable public returns (C) { - return this; - } - function g() pure public returns (bytes4) { - C x = C(0x123); - return x.f.selector; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(creation) -{ - string text = R"( - contract D {} - contract C { - function f() public { new D(); } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - -BOOST_AUTO_TEST_CASE(assembly) -{ - string text = R"( - contract C { - struct S { uint x; } - S s; - function e() pure public { - assembly { mstore(keccak256(0, 20), mul(s_slot, 2)) } - } - function f() pure public { - uint x; - assembly { x := 7 } - } - function g() view public { - assembly { for {} 1 { pop(sload(0)) } { } pop(gas) } - } - function h() view public { - assembly { function g() { pop(blockhash(20)) } } - } - function j() public { - assembly { pop(call(0, 1, 2, 3, 4, 5, 6)) } - } - function k() public { - assembly { pop(call(gas, 1, 2, 3, 4, 5, 6)) } - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - BOOST_AUTO_TEST_CASE(assembly_staticcall) { string text = R"( @@ -447,31 +113,6 @@ BOOST_AUTO_TEST_CASE(assembly_staticcall) CHECK_SUCCESS_NO_WARNINGS(text); } -BOOST_AUTO_TEST_CASE(assembly_jump) -{ - string text = R"( - contract C { - function k() public { - assembly { jump(2) } - } - } - )"; - CHECK_WARNING(text, "low-level EVM features"); -} - -BOOST_AUTO_TEST_CASE(constant) -{ - string text = R"( - contract C { - uint constant x = 2; - function k() pure public returns (uint) { - return x; - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); -} - BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 8202d512e0bd4e6b4a19ed483afff288514e75bd Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Wed, 4 Jul 2018 11:42:05 +0200 Subject: Enforcing error on msg.gas and block.blockhash() --- test/libsolidity/ViewPureChecker.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'test/libsolidity/ViewPureChecker.cpp') diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp index 53761ff2..bb5480b2 100644 --- a/test/libsolidity/ViewPureChecker.cpp +++ b/test/libsolidity/ViewPureChecker.cpp @@ -43,13 +43,11 @@ BOOST_AUTO_TEST_CASE(environment_access) vector view{ "block.coinbase", "block.timestamp", - "block.blockhash(7)", "block.difficulty", "block.number", "block.gaslimit", "blockhash(7)", "gasleft()", - "msg.gas", "msg.value", "msg.sender", "tx.origin", @@ -90,12 +88,11 @@ BOOST_AUTO_TEST_CASE(environment_access) "Statement has no effect." })); - CHECK_WARNING_ALLOW_MULTI( + CHECK_ERROR( "contract C { function f() view public { block.blockhash; } }", - (std::vector{ - "Function state mutability can be restricted to pure", - "\"block.blockhash()\" has been deprecated in favor of \"blockhash()\"" - })); + TypeError, + "\"block.blockhash()\" has been deprecated in favor of \"blockhash()\"" + ); } BOOST_AUTO_TEST_CASE(assembly_staticcall) -- cgit v1.2.3 From 4116704442aff035acb5b707c6b211ac1f5524fe Mon Sep 17 00:00:00 2001 From: Cryptomental Date: Tue, 10 Jul 2018 09:18:19 +0200 Subject: test: Fix typos. Fix typos using codespell. Refs: #4442 --- test/libsolidity/ViewPureChecker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/libsolidity/ViewPureChecker.cpp') diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp index bb5480b2..299cd084 100644 --- a/test/libsolidity/ViewPureChecker.cpp +++ b/test/libsolidity/ViewPureChecker.cpp @@ -55,7 +55,7 @@ BOOST_AUTO_TEST_CASE(environment_access) "this", "address(1).balance" }; - // ``block.blockhash`` and ``blockhash`` are tested seperately below because their usage will + // ``block.blockhash`` and ``blockhash`` are tested separately below because their usage will // produce warnings that can't be handled in a generic way. vector pure{ "msg.data", -- cgit v1.2.3 From ed5265598baf629e2c36ae7d2f7d2913024fe4d8 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 15 Aug 2018 15:36:05 +0200 Subject: Add view pure checker tests for ``address.staticcall(...)``. --- test/libsolidity/ViewPureChecker.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'test/libsolidity/ViewPureChecker.cpp') diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp index 299cd084..d993b92e 100644 --- a/test/libsolidity/ViewPureChecker.cpp +++ b/test/libsolidity/ViewPureChecker.cpp @@ -53,8 +53,11 @@ BOOST_AUTO_TEST_CASE(environment_access) "tx.origin", "tx.gasprice", "this", - "address(1).balance" + "address(1).balance", }; + if (dev::test::Options::get().evmVersion().hasStaticCall()) + view.emplace_back("address(0x4242).staticcall(\"\")"); + // ``block.blockhash`` and ``blockhash`` are tested separately below because their usage will // produce warnings that can't be handled in a generic way. vector pure{ @@ -95,6 +98,22 @@ BOOST_AUTO_TEST_CASE(environment_access) ); } +BOOST_AUTO_TEST_CASE(address_staticcall) +{ + string text = R"( + contract C { + function i() view public returns (bool) { + return address(0x4242).staticcall(""); + } + } + )"; + if (!dev::test::Options::get().evmVersion().hasStaticCall()) + CHECK_ERROR(text, TypeError, "\"staticcall\" is not supported by the VM version."); + else + CHECK_SUCCESS_NO_WARNINGS(text); +} + + BOOST_AUTO_TEST_CASE(assembly_staticcall) { string text = R"( -- cgit v1.2.3 From 82f512a7d40a3bd6a13dd799be66dd164fded077 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 15 Aug 2018 23:30:09 +0200 Subject: Add return data to bare calls. --- test/libsolidity/ViewPureChecker.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test/libsolidity/ViewPureChecker.cpp') diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp index d993b92e..b7ea1efc 100644 --- a/test/libsolidity/ViewPureChecker.cpp +++ b/test/libsolidity/ViewPureChecker.cpp @@ -103,7 +103,8 @@ BOOST_AUTO_TEST_CASE(address_staticcall) string text = R"( contract C { function i() view public returns (bool) { - return address(0x4242).staticcall(""); + (bool success,) = address(0x4242).staticcall(""); + return success; } } )"; -- cgit v1.2.3