From 49d9f334aa3490590eeb4a339cf74565e38052ea Mon Sep 17 00:00:00 2001 From: nishant-sachdeva Date: Sun, 21 Nov 2021 00:14:34 +0530 Subject: [PATCH] Extracted some test cases from SolidityEndToEnd.cpp --- test/libsolidity/SolidityEndToEndTest.cpp | 210 ------------------ .../enums/invalid_enum_logged.sol | 24 ++ .../library_staticcall_delegatecall.sol | 20 ++ .../memory_types_initialisation.sol | 17 ++ .../strings/constant_string_literal.sol | 25 +++ .../semanticTests/strings/return_string.sol | 20 ++ .../structs/packed_storage_structs_delete.sol | 27 +++ .../variables/storing_invalid_boolean.sol | 35 +++ 8 files changed, 168 insertions(+), 210 deletions(-) create mode 100644 test/libsolidity/semanticTests/enums/invalid_enum_logged.sol create mode 100644 test/libsolidity/semanticTests/libraries/library_staticcall_delegatecall.sol create mode 100644 test/libsolidity/semanticTests/memoryManagement/memory_types_initialisation.sol create mode 100644 test/libsolidity/semanticTests/strings/constant_string_literal.sol create mode 100644 test/libsolidity/semanticTests/strings/return_string.sol create mode 100644 test/libsolidity/semanticTests/structs/packed_storage_structs_delete.sol create mode 100644 test/libsolidity/semanticTests/variables/storing_invalid_boolean.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index e5a6b8cf7..473114961 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1607,30 +1607,6 @@ BOOST_AUTO_TEST_CASE(library_call_protection) ) } -BOOST_AUTO_TEST_CASE(library_staticcall_delegatecall) -{ - char const* sourceCode = R"( - library Lib { - function x() public view returns (uint) { - return 1; - } - } - contract Test { - uint t; - function f() public returns (uint) { - t = 2; - return this.g(); - } - function g() public view returns (uint) { - return Lib.x(); - } - } - )"; - compileAndRun(sourceCode, 0, "Lib"); - compileAndRun(sourceCode, 0, "Test", bytes(), map{{":Lib", m_contractAddress}}); - ABI_CHECK(callContractFunction("f()"), encodeArgs(1)); -} - BOOST_AUTO_TEST_CASE(bytes_from_calldata_to_memory) { char const* sourceCode = R"( @@ -1786,49 +1762,6 @@ BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) ); } -BOOST_AUTO_TEST_CASE(storing_invalid_boolean) -{ - char const* sourceCode = R"( - contract C { - event Ev(bool); - bool public perm; - function set() public returns(uint) { - bool tmp; - assembly { - tmp := 5 - } - perm = tmp; - return 1; - } - function ret() public returns(bool) { - bool tmp; - assembly { - tmp := 5 - } - return tmp; - } - function ev() public returns(uint) { - bool tmp; - assembly { - tmp := 5 - } - emit Ev(tmp); - return 1; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("set()"), encodeArgs(1)); - ABI_CHECK(callContractFunction("perm()"), encodeArgs(1)); - ABI_CHECK(callContractFunction("ret()"), encodeArgs(1)); - ABI_CHECK(callContractFunction("ev()"), encodeArgs(1)); - BOOST_REQUIRE_EQUAL(numLogs(), 1); - BOOST_CHECK_EQUAL(logAddress(0), m_contractAddress); - BOOST_CHECK(logData(0) == encodeArgs(1)); - BOOST_REQUIRE_EQUAL(numLogTopics(0), 1); - BOOST_CHECK_EQUAL(logTopic(0, 0), util::keccak256(string("Ev(bool)"))); -} - BOOST_AUTO_TEST_CASE(struct_referencing) { static char const* sourceCode = R"( @@ -2059,70 +1992,6 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi) // ABI_CHECK(callContractFunction("f()"), encodeArgs(5)); //} -BOOST_AUTO_TEST_CASE(packed_storage_structs_delete) -{ - char const* sourceCode = R"( - contract C { - struct str { uint8 a; uint16 b; uint8 c; } - uint8 x; - uint16 y; - str data; - function test() public returns (uint) { - x = 1; - y = 2; - data.a = 2; - data.b = 0xabcd; - data.c = 0xfa; - if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa) - return 2; - delete y; - delete data.b; - if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa) - return 3; - delete x; - delete data; - return 1; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(1)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - -BOOST_AUTO_TEST_CASE(invalid_enum_logged) -{ - char const* sourceCode = R"( - contract C { - enum X { A, B } - event Log(X); - - function test_log() public returns (uint) { - X garbled = X.A; - assembly { - garbled := 5 - } - emit Log(garbled); - return 1; - } - function test_log_ok() public returns (uint) { - X x = X.A; - emit Log(x); - return 1; - } - } - )"; - compileAndRun(sourceCode, 0, "C"); - ABI_CHECK(callContractFunction("test_log_ok()"), encodeArgs(u256(1))); - BOOST_REQUIRE_EQUAL(numLogs(), 1); - BOOST_CHECK_EQUAL(logAddress(0), m_contractAddress); - BOOST_REQUIRE_EQUAL(numLogTopics(0), 1); - BOOST_REQUIRE_EQUAL(logTopic(0, 0), util::keccak256(string("Log(uint8)"))); - BOOST_CHECK_EQUAL(h256(logData(0)), h256(u256(0))); - - ABI_CHECK(callContractFunction("test_log()"), panicData(PanicCode::EnumConversionError)); -} - BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) { char const* sourceCode = R"( @@ -2164,31 +2033,6 @@ BOOST_AUTO_TEST_CASE(failing_send) BOOST_REQUIRE(callContractFunction("callHelper(address)", c_helperAddress) == encodeArgs(true, 20)); } -BOOST_AUTO_TEST_CASE(return_string) -{ - char const* sourceCode = R"( - contract Main { - string public s; - function set(string calldata _s) external { - s = _s; - } - function get1() public returns (string memory r) { - return s; - } - function get2() public returns (string memory r) { - r = s; - } - } - )"; - compileAndRun(sourceCode, 0, "Main"); - string s("Julia"); - bytes args = encodeArgs(u256(0x20), u256(s.length()), s); - BOOST_REQUIRE(callContractFunction("set(string)", asString(args)) == encodeArgs()); - ABI_CHECK(callContractFunction("get1()"), args); - ABI_CHECK(callContractFunction("get2()"), args); - ABI_CHECK(callContractFunction("s()"), args); -} - BOOST_AUTO_TEST_CASE(return_multiple_strings_of_various_sizes) { char const* sourceCode = R"( @@ -2343,28 +2187,6 @@ BOOST_AUTO_TEST_CASE(return_bytes_internal) } } -BOOST_AUTO_TEST_CASE(memory_types_initialisation) -{ - char const* sourceCode = R"( - contract Test { - mapping(uint=>uint) data; - function stat() public returns (uint[5] memory) - { - data[2] = 3; // make sure to use some memory - } - function dyn() public returns (uint[] memory) { stat(); } - function nested() public returns (uint[3][] memory) { stat(); } - function nestedStat() public returns (uint[3][7] memory) { stat(); } - } - )"; - compileAndRun(sourceCode, 0, "Test"); - - ABI_CHECK(callContractFunction("stat()"), encodeArgs(vector(5))); - ABI_CHECK(callContractFunction("dyn()"), encodeArgs(u256(0x20), u256(0))); - ABI_CHECK(callContractFunction("nested()"), encodeArgs(u256(0x20), u256(0))); - ABI_CHECK(callContractFunction("nestedStat()"), encodeArgs(vector(3 * 7))); -} - BOOST_AUTO_TEST_CASE(calldata_struct_short) { char const* sourceCode = R"( @@ -2718,38 +2540,6 @@ BOOST_AUTO_TEST_CASE(nested_mixed_string_as_public_mapping_key) ), encodeArgs(u256(i - 3))); } -BOOST_AUTO_TEST_CASE(constant_string_literal) -{ - char const* sourceCode = R"( - contract Test { - bytes32 constant public b = "abcdefghijklmnopq"; - string constant public x = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca"; - - constructor() { - string memory xx = x; - bytes32 bb = b; - } - function getB() public returns (bytes32) { return b; } - function getX() public returns (string memory) { return x; } - function getX2() public returns (string memory r) { r = x; } - function unused() public returns (uint) { - "unusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunused"; - return 2; - } - } - )"; - - compileAndRun(sourceCode); - string longStr = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca"; - string shortStr = "abcdefghijklmnopq"; - ABI_CHECK(callContractFunction("b()"), encodeArgs(shortStr)); - ABI_CHECK(callContractFunction("x()"), encodeDyn(longStr)); - ABI_CHECK(callContractFunction("getB()"), encodeArgs(shortStr)); - ABI_CHECK(callContractFunction("getX()"), encodeDyn(longStr)); - ABI_CHECK(callContractFunction("getX2()"), encodeDyn(longStr)); - ABI_CHECK(callContractFunction("unused()"), encodeArgs(2)); -} - BOOST_AUTO_TEST_CASE(library_call) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/enums/invalid_enum_logged.sol b/test/libsolidity/semanticTests/enums/invalid_enum_logged.sol new file mode 100644 index 000000000..33cec7482 --- /dev/null +++ b/test/libsolidity/semanticTests/enums/invalid_enum_logged.sol @@ -0,0 +1,24 @@ +contract C { + enum X { A, B } + event Log(X); + + function test_log() public returns (uint) { + X garbled = X.A; + assembly { + garbled := 5 + } + emit Log(garbled); + return 1; + } + function test_log_ok() public returns (uint) { + X x = X.A; + emit Log(x); + return 1; + } +} +// ==== +// compileViaYul: also +// ---- +// test_log_ok() -> 1 +// ~ emit Log(uint8): 0x00 +// test_log() -> FAILURE, hex"4e487b71", 0x21 diff --git a/test/libsolidity/semanticTests/libraries/library_staticcall_delegatecall.sol b/test/libsolidity/semanticTests/libraries/library_staticcall_delegatecall.sol new file mode 100644 index 000000000..d566f7180 --- /dev/null +++ b/test/libsolidity/semanticTests/libraries/library_staticcall_delegatecall.sol @@ -0,0 +1,20 @@ +library Lib { + function x() public view returns (uint) { + return 1; + } +} +contract Test { + uint t; + function f() public returns (uint) { + t = 2; + return this.g(); + } + function g() public view returns (uint) { + return Lib.x(); + } +} +// ==== +// compileViaYul: also +// ---- +// library: Lib +// f() -> 1 diff --git a/test/libsolidity/semanticTests/memoryManagement/memory_types_initialisation.sol b/test/libsolidity/semanticTests/memoryManagement/memory_types_initialisation.sol new file mode 100644 index 000000000..7ad6b6167 --- /dev/null +++ b/test/libsolidity/semanticTests/memoryManagement/memory_types_initialisation.sol @@ -0,0 +1,17 @@ +contract Test { + mapping(uint=>uint) data; + function stat() public returns (uint[5] memory) + { + data[2] = 3; // make sure to use some memory + } + function dyn() public returns (uint[] memory) { stat(); } + function nested() public returns (uint[3][] memory) { stat(); } + function nestedStat() public returns (uint[3][7] memory) { stat(); } +} +// ==== +// compileViaYul: also +// ---- +// stat() -> 0, 0, 0, 0, 0 +// dyn() -> 0x20, 0 +// nested() -> 0x20, 0 +// nestedStat() -> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 diff --git a/test/libsolidity/semanticTests/strings/constant_string_literal.sol b/test/libsolidity/semanticTests/strings/constant_string_literal.sol new file mode 100644 index 000000000..9c112c1fa --- /dev/null +++ b/test/libsolidity/semanticTests/strings/constant_string_literal.sol @@ -0,0 +1,25 @@ +contract Test { + bytes32 constant public b = "abcdefghijklmnopq"; + string constant public x = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca"; + + constructor() { + string memory xx = x; + bytes32 bb = b; + } + function getB() public returns (bytes32) { return b; } + function getX() public returns (string memory) { return x; } + function getX2() public returns (string memory r) { r = x; } + function unused() public returns (uint) { + "unusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunused"; + return 2; + } +} +// ==== +// compileViaYul: also +// ---- +// b() -> 0x6162636465666768696a6b6c6d6e6f7071000000000000000000000000000000 +// x() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096 +// getB() -> 0x6162636465666768696a6b6c6d6e6f7071000000000000000000000000000000 +// getX() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096 +// getX2() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096 +// unused() -> 2 diff --git a/test/libsolidity/semanticTests/strings/return_string.sol b/test/libsolidity/semanticTests/strings/return_string.sol new file mode 100644 index 000000000..f221a5e15 --- /dev/null +++ b/test/libsolidity/semanticTests/strings/return_string.sol @@ -0,0 +1,20 @@ +contract Main { + string public s; + function set(string calldata _s) external { + s = _s; + } + function get1() public returns (string memory r) { + return s; + } + function get2() public returns (string memory r) { + r = s; + } +} +// ==== +// compileToEwasm: also +// compileViaYul: also +// ---- +// set(string): 0x20, 5, "Julia" -> +// get1() -> 0x20, 5, "Julia" +// get2() -> 0x20, 5, "Julia" +// s() -> 0x20, 5, "Julia" diff --git a/test/libsolidity/semanticTests/structs/packed_storage_structs_delete.sol b/test/libsolidity/semanticTests/structs/packed_storage_structs_delete.sol new file mode 100644 index 000000000..8d4931554 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/packed_storage_structs_delete.sol @@ -0,0 +1,27 @@ +contract C { + struct str { uint8 a; uint16 b; uint8 c; } + uint8 x; + uint16 y; + str data; + function test() public returns (uint) { + x = 1; + y = 2; + data.a = 2; + data.b = 0xabcd; + data.c = 0xfa; + if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa) + return 2; + delete y; + delete data.b; + if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa) + return 3; + delete x; + delete data; + return 1; + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> 1 +// storageEmpty -> 1 diff --git a/test/libsolidity/semanticTests/variables/storing_invalid_boolean.sol b/test/libsolidity/semanticTests/variables/storing_invalid_boolean.sol new file mode 100644 index 000000000..930ded40b --- /dev/null +++ b/test/libsolidity/semanticTests/variables/storing_invalid_boolean.sol @@ -0,0 +1,35 @@ +contract C { + event Ev(bool); + bool public perm; + function set() public returns(uint) { + bool tmp; + assembly { + tmp := 5 + } + perm = tmp; + return 1; + } + function ret() public returns(bool) { + bool tmp; + assembly { + tmp := 5 + } + return tmp; + } + function ev() public returns(uint) { + bool tmp; + assembly { + tmp := 5 + } + emit Ev(tmp); + return 1; + } +} +// ==== +// compileViaYul: also +// ---- +// set() -> 1 +// perm() -> true +// ret() -> true +// ev() -> 1 +// ~ emit Ev(bool): true