Merge pull request #12300 from nishant-sachdeva/extracting_tests_from_solEndToEnd.cpp

Extracted test cases from SolidityEndToEnd.cpp
This commit is contained in:
chriseth 2021-11-29 17:45:35 +01:00 committed by GitHub
commit 3dd8285cc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 168 additions and 210 deletions

View File

@ -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<string, h160>{{":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<u256>(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<u256>(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"(

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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