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 --- .../syntaxTests/viewPureChecker/assembly.sol | 23 ++++++++++++++++++++++ .../syntaxTests/viewPureChecker/assembly_jump.sol | 7 +++++++ .../viewPureChecker/builtin_functions.sol | 18 +++++++++++++++++ .../call_internal_functions_fail.sol | 7 +++++++ .../call_internal_functions_success.sol | 6 ++++++ .../syntaxTests/viewPureChecker/constant.sol | 6 ++++++ .../syntaxTests/viewPureChecker/creation.sol | 4 ++++ .../syntaxTests/viewPureChecker/function_types.sol | 22 +++++++++++++++++++++ .../syntaxTests/viewPureChecker/interface.sol | 6 ++++++ .../viewPureChecker/local_storage_variables.sol | 19 ++++++++++++++++++ .../syntaxTests/viewPureChecker/mappings.sol | 12 +++++++++++ .../syntaxTests/viewPureChecker/modifiers.sol | 17 ++++++++++++++++ .../syntaxTests/viewPureChecker/overriding.sol | 7 +++++++ .../viewPureChecker/returning_structs.sol | 14 +++++++++++++ .../syntaxTests/viewPureChecker/selector.sol | 8 ++++++++ .../viewPureChecker/selector_complex.sol | 11 +++++++++++ .../viewPureChecker/selector_complex2.sol | 9 +++++++++ .../syntaxTests/viewPureChecker/smoke_test.sol | 7 +++++++ .../syntaxTests/viewPureChecker/suggest_pure.sol | 5 +++++ .../syntaxTests/viewPureChecker/suggest_view.sol | 6 ++++++ .../viewPureChecker/write_storage_fail.sol | 6 ++++++ .../viewPureChecker/write_storage_fail_v050.sol | 7 +++++++ 22 files changed, 227 insertions(+) create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/assembly.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/assembly_jump.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/builtin_functions.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_fail.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_success.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/constant.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/creation.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/function_types.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/interface.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/local_storage_variables.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/mappings.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/modifiers.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/overriding.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/returning_structs.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/selector.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/selector_complex.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/selector_complex2.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/smoke_test.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/suggest_pure.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/suggest_view.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail_v050.sol (limited to 'test/libsolidity/syntaxTests/viewPureChecker') diff --git a/test/libsolidity/syntaxTests/viewPureChecker/assembly.sol b/test/libsolidity/syntaxTests/viewPureChecker/assembly.sol new file mode 100644 index 00000000..0a11dc3a --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/assembly.sol @@ -0,0 +1,23 @@ +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)) } + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/assembly_jump.sol b/test/libsolidity/syntaxTests/viewPureChecker/assembly_jump.sol new file mode 100644 index 00000000..418be561 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/assembly_jump.sol @@ -0,0 +1,7 @@ +contract C { + function k() public { + assembly { jump(2) } + } +} +// ---- +// Warning: (58-65): Jump instructions and labels are low-level EVM features that can lead to incorrect stack access. Because of that they are discouraged. Please consider using "switch", "if" or "for" statements instead. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions.sol b/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions.sol new file mode 100644 index 00000000..dbefb8b6 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/builtin_functions.sol @@ -0,0 +1,18 @@ +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 {} +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_fail.sol b/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_fail.sol new file mode 100644 index 00000000..22855c34 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_fail.sol @@ -0,0 +1,7 @@ +contract C { + function f() pure public { g(); } + function g() view public {} +} +// ---- +// TypeError: (44-47): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// Warning: (55-82): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_success.sol b/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_success.sol new file mode 100644 index 00000000..5aa21ce1 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/call_internal_functions_success.sol @@ -0,0 +1,6 @@ +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(); } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/constant.sol b/test/libsolidity/syntaxTests/viewPureChecker/constant.sol new file mode 100644 index 00000000..36d93497 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/constant.sol @@ -0,0 +1,6 @@ +contract C { + uint constant x = 2; + function k() pure public returns (uint) { + return x; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/creation.sol b/test/libsolidity/syntaxTests/viewPureChecker/creation.sol new file mode 100644 index 00000000..d80edd1b --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/creation.sol @@ -0,0 +1,4 @@ +contract D {} +contract C { + function f() public { new D(); } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/function_types.sol b/test/libsolidity/syntaxTests/viewPureChecker/function_types.sol new file mode 100644 index 00000000..92943889 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/function_types.sol @@ -0,0 +1,22 @@ +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(); + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/interface.sol b/test/libsolidity/syntaxTests/viewPureChecker/interface.sol new file mode 100644 index 00000000..0874e78a --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/interface.sol @@ -0,0 +1,6 @@ +interface D { + function f() view external; +} +contract C is D { + function f() view external {} +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/local_storage_variables.sol b/test/libsolidity/syntaxTests/viewPureChecker/local_storage_variables.sol new file mode 100644 index 00000000..7d01118a --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/local_storage_variables.sol @@ -0,0 +1,19 @@ +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; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/mappings.sol b/test/libsolidity/syntaxTests/viewPureChecker/mappings.sol new file mode 100644 index 00000000..eb0ccbfb --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/mappings.sol @@ -0,0 +1,12 @@ +contract C { + mapping(uint => uint) a; + function f() view public { + a; + } + function g() view public { + a[2]; + } + function h() public { + a[2] = 3; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/modifiers.sol b/test/libsolidity/syntaxTests/viewPureChecker/modifiers.sol new file mode 100644 index 00000000..f8f6b2cb --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/modifiers.sol @@ -0,0 +1,17 @@ +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 {} +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/overriding.sol b/test/libsolidity/syntaxTests/viewPureChecker/overriding.sol new file mode 100644 index 00000000..c82c7908 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/overriding.sol @@ -0,0 +1,7 @@ +contract D { + uint x; + function f() public { x = 2; } +} +contract C is D { + function f() public {} +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/returning_structs.sol b/test/libsolidity/syntaxTests/viewPureChecker/returning_structs.sol new file mode 100644 index 00000000..9b4eb466 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/returning_structs.sol @@ -0,0 +1,14 @@ +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; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/selector.sol b/test/libsolidity/syntaxTests/viewPureChecker/selector.sol new file mode 100644 index 00000000..2ad4518d --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/selector.sol @@ -0,0 +1,8 @@ +contract C { + uint public x; + function f() payable public { + } + function g() pure public returns (bytes4) { + return this.f.selector ^ this.x.selector; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/selector_complex.sol b/test/libsolidity/syntaxTests/viewPureChecker/selector_complex.sol new file mode 100644 index 00000000..311dec4a --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/selector_complex.sol @@ -0,0 +1,11 @@ +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; + } +} +// ---- +// TypeError: (228-232): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". diff --git a/test/libsolidity/syntaxTests/viewPureChecker/selector_complex2.sol b/test/libsolidity/syntaxTests/viewPureChecker/selector_complex2.sol new file mode 100644 index 00000000..d1543fed --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/selector_complex2.sol @@ -0,0 +1,9 @@ +contract C { + function f() payable public returns (C) { + return this; + } + function g() pure public returns (bytes4) { + C x = C(0x123); + return x.f.selector; + } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/smoke_test.sol b/test/libsolidity/syntaxTests/viewPureChecker/smoke_test.sol new file mode 100644 index 00000000..0e397efc --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/smoke_test.sol @@ -0,0 +1,7 @@ +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; } +} diff --git a/test/libsolidity/syntaxTests/viewPureChecker/suggest_pure.sol b/test/libsolidity/syntaxTests/viewPureChecker/suggest_pure.sol new file mode 100644 index 00000000..87719eb3 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/suggest_pure.sol @@ -0,0 +1,5 @@ +contract C { + function g() view public { } +} +// ---- +// Warning: (17-45): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/viewPureChecker/suggest_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/suggest_view.sol new file mode 100644 index 00000000..c045dfc4 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/suggest_view.sol @@ -0,0 +1,6 @@ +contract C { + uint x; + function g() public returns (uint) { return x; } +} +// ---- +// Warning: (29-77): Function state mutability can be restricted to view diff --git a/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail.sol b/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail.sol new file mode 100644 index 00000000..2a8bba31 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail.sol @@ -0,0 +1,6 @@ +contract C { + uint x; + function f() view public { x = 2; } +} +// ---- +// Warning: (56-57): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail_v050.sol b/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail_v050.sol new file mode 100644 index 00000000..b85078ed --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/write_storage_fail_v050.sol @@ -0,0 +1,7 @@ +pragma experimental "v0.5.0"; +contract C { + uint x; + function f() view public { x = 2; } +} +// ---- +// TypeError: (86-87): Function declared as view, but this expression (potentially) modifies the state and thus requires non-payable (the default) or payable. -- cgit