Skip to content

Commit 3dd8285

Browse files
authored
Merge pull request #12300 from nishant-sachdeva/extracting_tests_from_solEndToEnd.cpp
Extracted test cases from SolidityEndToEnd.cpp
2 parents de7b5a9 + 49d9f33 commit 3dd8285

8 files changed

+168
-210
lines changed

test/libsolidity/SolidityEndToEndTest.cpp

-210
Original file line numberDiff line numberDiff line change
@@ -1607,30 +1607,6 @@ BOOST_AUTO_TEST_CASE(library_call_protection)
16071607
)
16081608
}
16091609

1610-
BOOST_AUTO_TEST_CASE(library_staticcall_delegatecall)
1611-
{
1612-
char const* sourceCode = R"(
1613-
library Lib {
1614-
function x() public view returns (uint) {
1615-
return 1;
1616-
}
1617-
}
1618-
contract Test {
1619-
uint t;
1620-
function f() public returns (uint) {
1621-
t = 2;
1622-
return this.g();
1623-
}
1624-
function g() public view returns (uint) {
1625-
return Lib.x();
1626-
}
1627-
}
1628-
)";
1629-
compileAndRun(sourceCode, 0, "Lib");
1630-
compileAndRun(sourceCode, 0, "Test", bytes(), map<string, h160>{{":Lib", m_contractAddress}});
1631-
ABI_CHECK(callContractFunction("f()"), encodeArgs(1));
1632-
}
1633-
16341610
BOOST_AUTO_TEST_CASE(bytes_from_calldata_to_memory)
16351611
{
16361612
char const* sourceCode = R"(
@@ -1786,49 +1762,6 @@ BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data)
17861762
);
17871763
}
17881764

1789-
BOOST_AUTO_TEST_CASE(storing_invalid_boolean)
1790-
{
1791-
char const* sourceCode = R"(
1792-
contract C {
1793-
event Ev(bool);
1794-
bool public perm;
1795-
function set() public returns(uint) {
1796-
bool tmp;
1797-
assembly {
1798-
tmp := 5
1799-
}
1800-
perm = tmp;
1801-
return 1;
1802-
}
1803-
function ret() public returns(bool) {
1804-
bool tmp;
1805-
assembly {
1806-
tmp := 5
1807-
}
1808-
return tmp;
1809-
}
1810-
function ev() public returns(uint) {
1811-
bool tmp;
1812-
assembly {
1813-
tmp := 5
1814-
}
1815-
emit Ev(tmp);
1816-
return 1;
1817-
}
1818-
}
1819-
)";
1820-
compileAndRun(sourceCode);
1821-
ABI_CHECK(callContractFunction("set()"), encodeArgs(1));
1822-
ABI_CHECK(callContractFunction("perm()"), encodeArgs(1));
1823-
ABI_CHECK(callContractFunction("ret()"), encodeArgs(1));
1824-
ABI_CHECK(callContractFunction("ev()"), encodeArgs(1));
1825-
BOOST_REQUIRE_EQUAL(numLogs(), 1);
1826-
BOOST_CHECK_EQUAL(logAddress(0), m_contractAddress);
1827-
BOOST_CHECK(logData(0) == encodeArgs(1));
1828-
BOOST_REQUIRE_EQUAL(numLogTopics(0), 1);
1829-
BOOST_CHECK_EQUAL(logTopic(0, 0), util::keccak256(string("Ev(bool)")));
1830-
}
1831-
18321765
BOOST_AUTO_TEST_CASE(struct_referencing)
18331766
{
18341767
static char const* sourceCode = R"(
@@ -2059,70 +1992,6 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi)
20591992
// ABI_CHECK(callContractFunction("f()"), encodeArgs(5));
20601993
//}
20611994

2062-
BOOST_AUTO_TEST_CASE(packed_storage_structs_delete)
2063-
{
2064-
char const* sourceCode = R"(
2065-
contract C {
2066-
struct str { uint8 a; uint16 b; uint8 c; }
2067-
uint8 x;
2068-
uint16 y;
2069-
str data;
2070-
function test() public returns (uint) {
2071-
x = 1;
2072-
y = 2;
2073-
data.a = 2;
2074-
data.b = 0xabcd;
2075-
data.c = 0xfa;
2076-
if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa)
2077-
return 2;
2078-
delete y;
2079-
delete data.b;
2080-
if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa)
2081-
return 3;
2082-
delete x;
2083-
delete data;
2084-
return 1;
2085-
}
2086-
}
2087-
)";
2088-
compileAndRun(sourceCode);
2089-
ABI_CHECK(callContractFunction("test()"), encodeArgs(1));
2090-
BOOST_CHECK(storageEmpty(m_contractAddress));
2091-
}
2092-
2093-
BOOST_AUTO_TEST_CASE(invalid_enum_logged)
2094-
{
2095-
char const* sourceCode = R"(
2096-
contract C {
2097-
enum X { A, B }
2098-
event Log(X);
2099-
2100-
function test_log() public returns (uint) {
2101-
X garbled = X.A;
2102-
assembly {
2103-
garbled := 5
2104-
}
2105-
emit Log(garbled);
2106-
return 1;
2107-
}
2108-
function test_log_ok() public returns (uint) {
2109-
X x = X.A;
2110-
emit Log(x);
2111-
return 1;
2112-
}
2113-
}
2114-
)";
2115-
compileAndRun(sourceCode, 0, "C");
2116-
ABI_CHECK(callContractFunction("test_log_ok()"), encodeArgs(u256(1)));
2117-
BOOST_REQUIRE_EQUAL(numLogs(), 1);
2118-
BOOST_CHECK_EQUAL(logAddress(0), m_contractAddress);
2119-
BOOST_REQUIRE_EQUAL(numLogTopics(0), 1);
2120-
BOOST_REQUIRE_EQUAL(logTopic(0, 0), util::keccak256(string("Log(uint8)")));
2121-
BOOST_CHECK_EQUAL(h256(logData(0)), h256(u256(0)));
2122-
2123-
ABI_CHECK(callContractFunction("test_log()"), panicData(PanicCode::EnumConversionError));
2124-
}
2125-
21261995
BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund)
21271996
{
21281997
char const* sourceCode = R"(
@@ -2164,31 +2033,6 @@ BOOST_AUTO_TEST_CASE(failing_send)
21642033
BOOST_REQUIRE(callContractFunction("callHelper(address)", c_helperAddress) == encodeArgs(true, 20));
21652034
}
21662035

2167-
BOOST_AUTO_TEST_CASE(return_string)
2168-
{
2169-
char const* sourceCode = R"(
2170-
contract Main {
2171-
string public s;
2172-
function set(string calldata _s) external {
2173-
s = _s;
2174-
}
2175-
function get1() public returns (string memory r) {
2176-
return s;
2177-
}
2178-
function get2() public returns (string memory r) {
2179-
r = s;
2180-
}
2181-
}
2182-
)";
2183-
compileAndRun(sourceCode, 0, "Main");
2184-
string s("Julia");
2185-
bytes args = encodeArgs(u256(0x20), u256(s.length()), s);
2186-
BOOST_REQUIRE(callContractFunction("set(string)", asString(args)) == encodeArgs());
2187-
ABI_CHECK(callContractFunction("get1()"), args);
2188-
ABI_CHECK(callContractFunction("get2()"), args);
2189-
ABI_CHECK(callContractFunction("s()"), args);
2190-
}
2191-
21922036
BOOST_AUTO_TEST_CASE(return_multiple_strings_of_various_sizes)
21932037
{
21942038
char const* sourceCode = R"(
@@ -2343,28 +2187,6 @@ BOOST_AUTO_TEST_CASE(return_bytes_internal)
23432187
}
23442188
}
23452189

2346-
BOOST_AUTO_TEST_CASE(memory_types_initialisation)
2347-
{
2348-
char const* sourceCode = R"(
2349-
contract Test {
2350-
mapping(uint=>uint) data;
2351-
function stat() public returns (uint[5] memory)
2352-
{
2353-
data[2] = 3; // make sure to use some memory
2354-
}
2355-
function dyn() public returns (uint[] memory) { stat(); }
2356-
function nested() public returns (uint[3][] memory) { stat(); }
2357-
function nestedStat() public returns (uint[3][7] memory) { stat(); }
2358-
}
2359-
)";
2360-
compileAndRun(sourceCode, 0, "Test");
2361-
2362-
ABI_CHECK(callContractFunction("stat()"), encodeArgs(vector<u256>(5)));
2363-
ABI_CHECK(callContractFunction("dyn()"), encodeArgs(u256(0x20), u256(0)));
2364-
ABI_CHECK(callContractFunction("nested()"), encodeArgs(u256(0x20), u256(0)));
2365-
ABI_CHECK(callContractFunction("nestedStat()"), encodeArgs(vector<u256>(3 * 7)));
2366-
}
2367-
23682190
BOOST_AUTO_TEST_CASE(calldata_struct_short)
23692191
{
23702192
char const* sourceCode = R"(
@@ -2718,38 +2540,6 @@ BOOST_AUTO_TEST_CASE(nested_mixed_string_as_public_mapping_key)
27182540
), encodeArgs(u256(i - 3)));
27192541
}
27202542

2721-
BOOST_AUTO_TEST_CASE(constant_string_literal)
2722-
{
2723-
char const* sourceCode = R"(
2724-
contract Test {
2725-
bytes32 constant public b = "abcdefghijklmnopq";
2726-
string constant public x = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca";
2727-
2728-
constructor() {
2729-
string memory xx = x;
2730-
bytes32 bb = b;
2731-
}
2732-
function getB() public returns (bytes32) { return b; }
2733-
function getX() public returns (string memory) { return x; }
2734-
function getX2() public returns (string memory r) { r = x; }
2735-
function unused() public returns (uint) {
2736-
"unusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunused";
2737-
return 2;
2738-
}
2739-
}
2740-
)";
2741-
2742-
compileAndRun(sourceCode);
2743-
string longStr = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca";
2744-
string shortStr = "abcdefghijklmnopq";
2745-
ABI_CHECK(callContractFunction("b()"), encodeArgs(shortStr));
2746-
ABI_CHECK(callContractFunction("x()"), encodeDyn(longStr));
2747-
ABI_CHECK(callContractFunction("getB()"), encodeArgs(shortStr));
2748-
ABI_CHECK(callContractFunction("getX()"), encodeDyn(longStr));
2749-
ABI_CHECK(callContractFunction("getX2()"), encodeDyn(longStr));
2750-
ABI_CHECK(callContractFunction("unused()"), encodeArgs(2));
2751-
}
2752-
27532543
BOOST_AUTO_TEST_CASE(library_call)
27542544
{
27552545
char const* sourceCode = R"(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
contract C {
2+
enum X { A, B }
3+
event Log(X);
4+
5+
function test_log() public returns (uint) {
6+
X garbled = X.A;
7+
assembly {
8+
garbled := 5
9+
}
10+
emit Log(garbled);
11+
return 1;
12+
}
13+
function test_log_ok() public returns (uint) {
14+
X x = X.A;
15+
emit Log(x);
16+
return 1;
17+
}
18+
}
19+
// ====
20+
// compileViaYul: also
21+
// ----
22+
// test_log_ok() -> 1
23+
// ~ emit Log(uint8): 0x00
24+
// test_log() -> FAILURE, hex"4e487b71", 0x21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library Lib {
2+
function x() public view returns (uint) {
3+
return 1;
4+
}
5+
}
6+
contract Test {
7+
uint t;
8+
function f() public returns (uint) {
9+
t = 2;
10+
return this.g();
11+
}
12+
function g() public view returns (uint) {
13+
return Lib.x();
14+
}
15+
}
16+
// ====
17+
// compileViaYul: also
18+
// ----
19+
// library: Lib
20+
// f() -> 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
contract Test {
2+
mapping(uint=>uint) data;
3+
function stat() public returns (uint[5] memory)
4+
{
5+
data[2] = 3; // make sure to use some memory
6+
}
7+
function dyn() public returns (uint[] memory) { stat(); }
8+
function nested() public returns (uint[3][] memory) { stat(); }
9+
function nestedStat() public returns (uint[3][7] memory) { stat(); }
10+
}
11+
// ====
12+
// compileViaYul: also
13+
// ----
14+
// stat() -> 0, 0, 0, 0, 0
15+
// dyn() -> 0x20, 0
16+
// nested() -> 0x20, 0
17+
// nestedStat() -> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
contract Test {
2+
bytes32 constant public b = "abcdefghijklmnopq";
3+
string constant public x = "abefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabca";
4+
5+
constructor() {
6+
string memory xx = x;
7+
bytes32 bb = b;
8+
}
9+
function getB() public returns (bytes32) { return b; }
10+
function getX() public returns (string memory) { return x; }
11+
function getX2() public returns (string memory r) { r = x; }
12+
function unused() public returns (uint) {
13+
"unusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunusedunused";
14+
return 2;
15+
}
16+
}
17+
// ====
18+
// compileViaYul: also
19+
// ----
20+
// b() -> 0x6162636465666768696a6b6c6d6e6f7071000000000000000000000000000000
21+
// x() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096
22+
// getB() -> 0x6162636465666768696a6b6c6d6e6f7071000000000000000000000000000000
23+
// getX() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096
24+
// getX2() -> 0x20, 0x35, 0x616265666768696a6b6c6d6e6f70716162636465666768696a6b6c6d6e6f7071, 44048183304486788312148433451363384677562177293131179093971701692629931524096
25+
// unused() -> 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
contract Main {
2+
string public s;
3+
function set(string calldata _s) external {
4+
s = _s;
5+
}
6+
function get1() public returns (string memory r) {
7+
return s;
8+
}
9+
function get2() public returns (string memory r) {
10+
r = s;
11+
}
12+
}
13+
// ====
14+
// compileToEwasm: also
15+
// compileViaYul: also
16+
// ----
17+
// set(string): 0x20, 5, "Julia" ->
18+
// get1() -> 0x20, 5, "Julia"
19+
// get2() -> 0x20, 5, "Julia"
20+
// s() -> 0x20, 5, "Julia"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
contract C {
2+
struct str { uint8 a; uint16 b; uint8 c; }
3+
uint8 x;
4+
uint16 y;
5+
str data;
6+
function test() public returns (uint) {
7+
x = 1;
8+
y = 2;
9+
data.a = 2;
10+
data.b = 0xabcd;
11+
data.c = 0xfa;
12+
if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa)
13+
return 2;
14+
delete y;
15+
delete data.b;
16+
if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa)
17+
return 3;
18+
delete x;
19+
delete data;
20+
return 1;
21+
}
22+
}
23+
// ====
24+
// compileViaYul: also
25+
// ----
26+
// test() -> 1
27+
// storageEmpty -> 1

0 commit comments

Comments
 (0)