aboutsummaryrefslogtreecommitdiffstats
path: root/test/contracts/ContractInterface.h
blob: 052b0db2d6ea4166c5796a14a42f8184072158f6 (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
/*
    This file is part of solidity.

    solidity 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.

    solidity 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 solidity.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <boost/test/unit_test.hpp>
#include <test/ExecutionFramework.h>

#include <functional>

namespace dev
{
namespace test
{

class ContractInterface
{
public:
    ContractInterface(ExecutionFramework& _framework): m_framework(_framework) {}

    void setNextValue(u256 const& _value) { m_nextValue = _value; }

protected:
    template <class... Args>
    bytes const& call(std::string const& _sig, Args const&... _arguments)
    {
        auto const& ret = m_framework.callContractFunctionWithValue(_sig, m_nextValue, _arguments...);
        m_nextValue = 0;
        return ret;
    }

    void callString(std::string const& _name, std::string const& _arg)
    {
        BOOST_CHECK(call(_name + "(string)", u256(0x20), _arg.length(), _arg).empty());
    }

    void callStringAddress(std::string const& _name, std::string const& _arg1, u160 const& _arg2)
    {
        BOOST_CHECK(call(_name + "(string,address)", u256(0x40), _arg2, _arg1.length(), _arg1).empty());
    }

    void callStringAddressBool(std::string const& _name, std::string const& _arg1, u160 const& _arg2, bool _arg3)
    {
        BOOST_CHECK(call(_name + "(string,address,bool)", u256(0x60), _arg2, _arg3, _arg1.length(), _arg1).empty());
    }

    void callStringBytes32(std::string const& _name, std::string const& _arg1, h256 const& _arg2)
    {
        BOOST_CHECK(call(_name + "(string,bytes32)", u256(0x40), _arg2, _arg1.length(), _arg1).empty());
    }

    u160 callStringReturnsAddress(std::string const& _name, std::string const& _arg)
    {
        bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg);
        BOOST_REQUIRE(ret.size() == 0x20);
        BOOST_CHECK(std::count(ret.begin(), ret.begin() + 12, 0) == 12);
        return u160(u256(h256(ret)));
    }

    std::string callAddressReturnsString(std::string const& _name, u160 const& _arg)
    {
        bytesConstRef const ret(&call(_name + "(address)", _arg));
        BOOST_REQUIRE(ret.size() >= 0x40);
        u256 offset(h256(ret.cropped(0, 0x20)));
        BOOST_REQUIRE_EQUAL(offset, 0x20);
        u256 len(h256(ret.cropped(0x20, 0x20)));
        BOOST_REQUIRE_EQUAL(ret.size(), 0x40 + ((len + 0x1f) / 0x20) * 0x20);
        return ret.cropped(0x40, size_t(len)).toString();
    }

    h256 callStringReturnsBytes32(std::string const& _name, std::string const& _arg)
    {
        bytes const& ret = call(_name + "(string)", u256(0x20), _arg.length(), _arg);
        BOOST_REQUIRE(ret.size() == 0x20);
        return h256(ret);
    }

private:
    u256 m_nextValue;
    ExecutionFramework& m_framework;
};

}
} // end namespaces