diff options
Diffstat (limited to 'test/libsolidity/SolidityEndToEndTest.cpp')
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 323 |
1 files changed, 308 insertions, 15 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 94d3e168..166c4660 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1950,6 +1950,87 @@ BOOST_AUTO_TEST_CASE(ripemd) testContractAgainstCpp("a(bytes32)", f, u256(-1)); } +BOOST_AUTO_TEST_CASE(packed_keccak256) +{ + char const* sourceCode = R"( + contract test { + function a(bytes32 input) returns (bytes32 hash) { + var b = 65536; + uint c = 256; + return keccak256(8, input, b, input, c); + } + } + )"; + compileAndRun(sourceCode); + auto f = [&](u256 const& _x) -> u256 + { + return dev::keccak256( + toCompactBigEndian(unsigned(8)) + + toBigEndian(_x) + + toCompactBigEndian(unsigned(65536)) + + toBigEndian(_x) + + toBigEndian(u256(256)) + ); + }; + testContractAgainstCpp("a(bytes32)", f, u256(4)); + testContractAgainstCpp("a(bytes32)", f, u256(5)); + testContractAgainstCpp("a(bytes32)", f, u256(-1)); +} + +BOOST_AUTO_TEST_CASE(packed_sha256) +{ + char const* sourceCode = R"( + contract test { + function a(bytes32 input) returns (bytes32 hash) { + var b = 65536; + uint c = 256; + return sha256(8, input, b, input, c); + } + } + )"; + compileAndRun(sourceCode); + auto f = [&](u256 const& _x) -> bytes + { + if (_x == u256(4)) + return fromHex("804e0d7003cfd70fc925dc103174d9f898ebb142ecc2a286da1abd22ac2ce3ac"); + if (_x == u256(5)) + return fromHex("e94921945f9068726c529a290a954f412bcac53184bb41224208a31edbf63cf0"); + if (_x == u256(-1)) + return fromHex("f14def4d07cd185ddd8b10a81b2238326196a38867e6e6adbcc956dc913488c7"); + return fromHex(""); + }; + testContractAgainstCpp("a(bytes32)", f, u256(4)); + testContractAgainstCpp("a(bytes32)", f, u256(5)); + testContractAgainstCpp("a(bytes32)", f, u256(-1)); +} + +BOOST_AUTO_TEST_CASE(packed_ripemd160) +{ + char const* sourceCode = R"( + contract test { + function a(bytes32 input) returns (bytes32 hash) { + var b = 65536; + uint c = 256; + return ripemd160(8, input, b, input, c); + } + } + )"; + compileAndRun(sourceCode); + auto f = [&](u256 const& _x) -> bytes + { + if (_x == u256(4)) + return fromHex("f93175303eba2a7b372174fc9330237f5ad202fc000000000000000000000000"); + if (_x == u256(5)) + return fromHex("04f4fc112e2bfbe0d38f896a46629e08e2fcfad5000000000000000000000000"); + if (_x == u256(-1)) + return fromHex("c0a2e4b1f3ff766a9a0089e7a410391730872495000000000000000000000000"); + return fromHex(""); + }; + testContractAgainstCpp("a(bytes32)", f, u256(4)); + testContractAgainstCpp("a(bytes32)", f, u256(5)); + testContractAgainstCpp("a(bytes32)", f, u256(-1)); +} + BOOST_AUTO_TEST_CASE(ecrecover) { char const* sourceCode = R"( @@ -2318,21 +2399,6 @@ BOOST_AUTO_TEST_CASE(gas_and_value_basic) BOOST_REQUIRE(callContractFunction("checkState()") == encodeArgs(false, 20 - 5)); } -BOOST_AUTO_TEST_CASE(gas_for_builtin) -{ - char const* sourceCode = R"( - contract Contract { - function test(uint g) returns (bytes32 data, bool flag) { - data = ripemd160.gas(g)("abc"); - flag = true; - } - } - )"; - compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("test(uint256)", 500) == bytes()); - BOOST_CHECK(callContractFunction("test(uint256)", 800) == encodeArgs(u256("0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc000000000000000000000000"), true)); -} - BOOST_AUTO_TEST_CASE(value_complex) { char const* sourceCode = R"( @@ -2696,6 +2762,34 @@ BOOST_AUTO_TEST_CASE(function_modifier_for_constructor) BOOST_CHECK(callContractFunction("getData()") == encodeArgs(4 | 2)); } +BOOST_AUTO_TEST_CASE(function_modifier_multiple_times) +{ + char const* sourceCode = R"( + contract C { + uint public a; + modifier mod(uint x) { a += x; _; } + function f(uint x) mod(2) mod(5) mod(x) returns(uint) { return a; } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f(uint256)", u256(3)) == encodeArgs(2 + 5 + 3)); + BOOST_CHECK(callContractFunction("a()") == encodeArgs(2 + 5 + 3)); +} + +BOOST_AUTO_TEST_CASE(function_modifier_multiple_times_local_vars) +{ + char const* sourceCode = R"( + contract C { + uint public a; + modifier mod(uint x) { uint b = x; a += b; _; a -= b; assert(b == x); } + function f(uint x) mod(2) mod(5) mod(x) returns(uint) { return a; } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f(uint256)", u256(3)) == encodeArgs(2 + 5 + 3)); + BOOST_CHECK(callContractFunction("a()") == encodeArgs(0)); +} + BOOST_AUTO_TEST_CASE(crazy_elementary_typenames_on_stack) { char const* sourceCode = R"( @@ -3063,6 +3157,31 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data_from_storage) BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("Deposit(uint256,bytes,uint256)"))); } +BOOST_AUTO_TEST_CASE(event_really_really_lots_of_data_from_storage) +{ + char const* sourceCode = R"( + contract ClientReceipt { + bytes x; + event Deposit(uint fixeda, bytes dynx, uint fixedb); + function deposit() { + x.length = 31; + x[0] = "A"; + x[1] = "B"; + x[2] = "C"; + x[30] = "Z"; + Deposit(10, x, 15); + } + } + )"; + compileAndRun(sourceCode); + callContractFunction("deposit()"); + BOOST_REQUIRE_EQUAL(m_logs.size(), 1); + BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); + BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 31, string("ABC") + string(27, 0) + "Z")); + BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); + BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("Deposit(uint256,bytes,uint256)"))); +} + BOOST_AUTO_TEST_CASE(event_indexed_string) { char const* sourceCode = R"( @@ -4308,6 +4427,75 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_storage_struct) BOOST_CHECK(storageEmpty(m_contractAddress)); } +BOOST_AUTO_TEST_CASE(array_copy_storage_abi) +{ + // NOTE: This does not really test copying from storage to ABI directly, + // because it will always copy to memory first. + char const* sourceCode = R"( + contract c { + uint8[] x; + uint16[] y; + uint24[] z; + function test1() returns (uint8[]) { + for (uint i = 0; i < 101; ++i) + x.push(uint8(i)); + return x; + } + function test2() returns (uint16[]) { + for (uint i = 0; i < 101; ++i) + y.push(uint16(i)); + return y; + } + function test3() returns (uint24[]) { + for (uint i = 0; i < 101; ++i) + z.push(uint24(i)); + return z; + } + } + )"; + compileAndRun(sourceCode); + bytes valueSequence; + for (size_t i = 0; i < 101; ++i) + valueSequence += toBigEndian(u256(i)); + BOOST_CHECK(callContractFunction("test1()") == encodeArgs(0x20, 101) + valueSequence); + BOOST_CHECK(callContractFunction("test2()") == encodeArgs(0x20, 101) + valueSequence); + BOOST_CHECK(callContractFunction("test3()") == encodeArgs(0x20, 101) + valueSequence); +} + +BOOST_AUTO_TEST_CASE(array_copy_storage_abi_signed) +{ + // NOTE: This does not really test copying from storage to ABI directly, + // because it will always copy to memory first. + char const* sourceCode = R"( + contract c { + int16[] x; + function test() returns (int16[]) { + x.push(int16(-1)); + x.push(int16(-1)); + x.push(int16(8)); + x.push(int16(-16)); + x.push(int16(-2)); + x.push(int16(6)); + x.push(int16(8)); + x.push(int16(-1)); + return x; + } + } + )"; + compileAndRun(sourceCode); + bytes valueSequence; + BOOST_CHECK(callContractFunction("test()") == encodeArgs(0x20, 8, + u256(-1), + u256(-1), + u256(8), + u256(-16), + u256(-2), + u256(6), + u256(8), + u256(-1) + )); +} + BOOST_AUTO_TEST_CASE(array_push) { char const* sourceCode = R"( @@ -8223,6 +8411,53 @@ BOOST_AUTO_TEST_CASE(failing_ecrecover_invalid_input) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); } +BOOST_AUTO_TEST_CASE(failing_ecrecover_invalid_input_proper) +{ + char const* sourceCode = R"( + contract C { + function f() returns (address) { + return recover( + 0x77e5189111eb6557e8a637b27ef8fbb15bc61d61c2f00cc48878f3a296e5e0ca, + 0, // invalid v value + 0x6944c77849b18048f6abe0db8084b0d0d0689cdddb53d2671c36967b58691ad4, + 0xef4f06ba4f78319baafd0424365777241af4dfd3da840471b4b4b087b7750d0d, + 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, + 0xca35b7d915458ef540ade6068dfe2f44e8fa733c + ); + } + function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s, uint blockExpired, bytes32 salt) + returns (address) + { + require(hash == keccak256(blockExpired, salt)); + return ecrecover(hash, v, r, s); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); +} + +BOOST_AUTO_TEST_CASE(failing_ecrecover_invalid_input_asm) +{ + char const* sourceCode = R"( + contract C { + function f() returns (address) { + assembly { + mstore(mload(0x40), 0xca35b7d915458ef540ade6068dfe2f44e8fa733c) + } + return ecrecover( + 0x77e5189111eb6557e8a637b27ef8fbb15bc61d61c2f00cc48878f3a296e5e0ca, + 0, // invalid v value + 0x6944c77849b18048f6abe0db8084b0d0d0689cdddb53d2671c36967b58691ad4, + 0xef4f06ba4f78319baafd0424365777241af4dfd3da840471b4b4b087b7750d0d + ); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); +} + BOOST_AUTO_TEST_CASE(calling_nonexisting_contract_throws) { char const* sourceCode = R"( @@ -9741,6 +9976,64 @@ BOOST_AUTO_TEST_CASE(inlineasm_empty_let) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0), u256(0))); } +BOOST_AUTO_TEST_CASE(bare_call_invalid_address) +{ + char const* sourceCode = R"( + contract C { + /// Calling into non-existant account is successful (creates the account) + function f() external constant returns (bool) { + return address(0x4242).call(); + } + function g() external constant returns (bool) { + return address(0x4242).callcode(); + } + function h() external constant returns (bool) { + return address(0x4242).delegatecall(); + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("h()") == encodeArgs(u256(1))); +} + +BOOST_AUTO_TEST_CASE(delegatecall_return_value) +{ + char const* sourceCode = R"DELIMITER( + contract C { + uint value; + function set(uint _value) external { + value = _value; + } + function get() external constant returns (uint) { + return value; + } + function get_delegated() external constant returns (bool) { + return this.delegatecall(bytes4(sha3("get()"))); + } + function assert0() external constant { + assert(value == 0); + } + function assert0_delegated() external constant returns (bool) { + return this.delegatecall(bytes4(sha3("assert0()"))); + } + } + )DELIMITER"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("get()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("assert0_delegated()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("get_delegated()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("set(uint256)", u256(1)) == encodeArgs()); + BOOST_CHECK(callContractFunction("get()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("assert0_delegated()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("get_delegated()") == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("set(uint256)", u256(42)) == encodeArgs()); + BOOST_CHECK(callContractFunction("get()") == encodeArgs(u256(42))); + BOOST_CHECK(callContractFunction("assert0_delegated()") == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("get_delegated()") == encodeArgs(u256(1))); +} + BOOST_AUTO_TEST_SUITE_END() } |