aboutsummaryrefslogtreecommitdiffstats
path: root/solidityEndToEndTest.cpp
blob: dc8d34cb796e8942941b5a8723163a415a608645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    cpp-ethereum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Unit tests for the solidity expression compiler, testing the behaviour of the code.
 */

#include <string>
#include <boost/test/unit_test.hpp>
#include <libethereum/State.h>
#include <libethereum/Executive.h>
#include <libsolidity/CompilerStack.h>

using namespace std;

namespace dev
{
namespace solidity
{
namespace test
{

class ExecutionFramework
{
public:
    ExecutionFramework() { g_logVerbosity = 0; }

    bytes const& compileAndRun(std::string const& _sourceCode)
    {
        bytes code = dev::solidity::CompilerStack::compile(_sourceCode);
        sendMessage(code, true);
        BOOST_REQUIRE(!m_output.empty());
        return m_output;
    }

    bytes const& callFunction(byte _index, bytes const& _data)
    {
        sendMessage(bytes(1, _index) + _data, false);
        return m_output;
    }

    bytes const& callFunction(byte _index, u256 const& _argument1)
    {
        return callFunction(_index, toBigEndian(_argument1));
    }

private:
    void sendMessage(bytes const& _data, bool _isCreation)
    {
        eth::Executive executive(m_state);
        eth::Transaction t = _isCreation ? eth::Transaction(0, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec())
                                         : eth::Transaction(0, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec());
        bytes transactionRLP = t.rlp();
        try
        {
            // this will throw since the transaction is invalid, but it should nevertheless store the transaction
            executive.setup(&transactionRLP);
        }
        catch (...) {}
        if (_isCreation)
        {
            BOOST_REQUIRE(!executive.create(Address(), 0, m_gasPrice, m_gas, &_data, Address()));
            m_contractAddress = executive.newAddress();
            BOOST_REQUIRE(m_contractAddress);
            BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
        }
        else
            BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), 0, m_gasPrice, &_data, m_gas, Address()));
        BOOST_REQUIRE(executive.go());
        executive.finalize();
        m_output = executive.out().toVector();
    }

    Address m_contractAddress;
    eth::State m_state;
    u256 const m_gasPrice = 100 * eth::szabo;
    u256 const m_gas = 1000000;
    bytes m_output;
};

BOOST_FIXTURE_TEST_SUITE(SolidityCompilerEndToEndTest, ExecutionFramework)

BOOST_AUTO_TEST_CASE(smoke_test)
{
    char const* sourceCode = "contract test {\n"
                             "  function f(uint a) returns(uint d) { return a * 7; }\n"
                             "}\n";
    compileAndRun(sourceCode);
    u256 a = 0x200030004;
    BOOST_CHECK(callFunction(0, a) == toBigEndian(a * 7));
}

BOOST_AUTO_TEST_CASE(empty_contract)
{
    char const* sourceCode = "contract test {\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes()).empty());
}

BOOST_AUTO_TEST_CASE(recursive_calls)
{
    char const* sourceCode = "contract test {\n"
                             "  function f(uint n) returns(uint nfac) {\n"
                             "    if (n <= 1) return 1;\n"
                             "    else return n * f(n - 1);\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, u256(0)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(0, u256(1)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(0, u256(2)) == toBigEndian(u256(2)));
    BOOST_CHECK(callFunction(0, u256(3)) == toBigEndian(u256(6)));
    BOOST_CHECK(callFunction(0, u256(4)) == toBigEndian(u256(24)));
}

BOOST_AUTO_TEST_CASE(while_loop)
{
    char const* sourceCode = "contract test {\n"
                             "  function f(uint n) returns(uint nfac) {\n"
                             "    nfac = 1;\n"
                             "    var i = 2;\n"
                             "    while (i <= n) nfac *= i++;\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, u256(0)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(0, u256(1)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(0, u256(2)) == toBigEndian(u256(2)));
    BOOST_CHECK(callFunction(0, u256(3)) == toBigEndian(u256(6)));
    BOOST_CHECK(callFunction(0, u256(4)) == toBigEndian(u256(24)));
}

BOOST_AUTO_TEST_CASE(calling_other_functions)
{
    // note that the index of a function is its index in the sorted sequence of functions
    char const* sourceCode = "contract collatz {\n"
                             "  function run(uint x) returns(uint y) {\n"
                             "    while ((y = x) > 1) {\n"
                             "      if (x % 2 == 0) x = evenStep(x);\n"
                             "      else x = oddStep(x);\n"
                             "    }\n"
                             "  }\n"
                             "  function evenStep(uint x) returns(uint y) {\n"
                             "    return x / 2;\n"
                             "  }\n"
                             "  function oddStep(uint x) returns(uint y) {\n"
                             "    return 3 * x + 1;\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(2, u256(0)) == toBigEndian(u256(0)));
    BOOST_CHECK(callFunction(2, u256(1)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(2, u256(2)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(2, u256(8)) == toBigEndian(u256(1)));
    BOOST_CHECK(callFunction(2, u256(127)) == toBigEndian(u256(1)));
}

BOOST_AUTO_TEST_CASE(many_local_variables)
{
    char const* sourceCode = "contract test {\n"
                             "  function run(uint x1, uint x2, uint x3) returns(uint y) {\n"
                             "    var a = 0x1; var b = 0x10; var c = 0x100;\n"
                             "    y = a + b + c + x1 + x2 + x3;\n"
                             "    y += b + x2;\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, toBigEndian(u256(0x1000)) + toBigEndian(u256(0x10000)) + toBigEndian(u256(0x100000)))
                == toBigEndian(u256(0x121121)));
}

BOOST_AUTO_TEST_CASE(packing_unpacking_types)
{
    char const* sourceCode = "contract test {\n"
                             "  function run(bool a, uint32 b, uint64 c) returns(uint256 y) {\n"
                             "    if (a) y = 1;\n"
                             "    y = y * 0x100000000 | ~b;\n"
                             "    y = y * 0x10000000000000000 | ~c;\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0"))
                == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f"));
}

BOOST_AUTO_TEST_CASE(multiple_return_values)
{
    char const* sourceCode = "contract test {\n"
                             "  function run(bool x1, uint x2) returns(uint y1, bool y2, uint y3) {\n"
                             "    y1 = x2; y2 = x1;\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes(1, 1) + toBigEndian(u256(0xcd)))
                == toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0)));
}

BOOST_AUTO_TEST_CASE(short_circuiting)
{
    char const* sourceCode = "contract test {\n"
                             "  function run(uint x) returns(uint y) {\n"
                             "    x == 0 || ((x = 8) > 0);\n"
                             "    return x;"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, u256(0)) == toBigEndian(u256(0)));
    BOOST_CHECK(callFunction(0, u256(1)) == toBigEndian(u256(8)));
}

BOOST_AUTO_TEST_CASE(high_bits_cleaning)
{
    char const* sourceCode = "contract test {\n"
                             "  function run() returns(uint256 y) {\n"
                             "    uint32 x = uint32(0xffffffff) + 10;\n"
                             "    if (x >= 0xffffffff) return 0;\n"
                             "    return x;"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes()) == toBigEndian(u256(9)));
}

BOOST_AUTO_TEST_CASE(sign_extension)
{
    char const* sourceCode = "contract test {\n"
                             "  function run() returns(uint256 y) {\n"
                             "    int64 x = -int32(0xff);\n"
                             "    if (x >= 0xff) return 0;\n"
                             "    return -uint256(x);"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes()) == toBigEndian(u256(0xff)));
}

BOOST_AUTO_TEST_CASE(small_unsigned_types)
{
    char const* sourceCode = "contract test {\n"
                             "  function run() returns(uint256 y) {\n"
                             "    uint32 x = uint32(0xffffff) * 0xffffff;\n"
                             "    return x / 0x100;"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes()) == toBigEndian(u256(0xfe0000)));
}

BOOST_AUTO_TEST_CASE(small_signed_types)
{
    char const* sourceCode = "contract test {\n"
                             "  function run() returns(int256 y) {\n"
                             "    return -int32(10) * -int64(20);\n"
                             "  }\n"
                             "}\n";
    compileAndRun(sourceCode);
    BOOST_CHECK(callFunction(0, bytes()) == toBigEndian(u256(200)));
}

BOOST_AUTO_TEST_SUITE_END()

}
}
} // end namespaces