2019-02-21 00:50:41 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/util/TestFunctionCall.h>
|
2019-04-11 18:25:20 +00:00
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
|
|
|
#include <test/libsolidity/util/ContractABIUtils.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
2019-04-11 18:25:20 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
2019-02-21 00:50:41 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace solidity;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend::test;
|
2019-02-21 00:50:41 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2020-11-03 23:39:29 +00:00
|
|
|
using Token = soltest::Token;
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
string TestFunctionCall::format(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
string const& _linePrefix,
|
|
|
|
bool const _renderResult,
|
|
|
|
bool const _highlight
|
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stringstream stream;
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
bool highlight = !matchesExpectation() && _highlight;
|
|
|
|
|
|
|
|
auto formatOutput = [&](bool const _singleLine)
|
|
|
|
{
|
|
|
|
string ws = " ";
|
|
|
|
string arrow = formatToken(Token::Arrow);
|
|
|
|
string colon = formatToken(Token::Colon);
|
|
|
|
string comma = formatToken(Token::Comma);
|
|
|
|
string comment = formatToken(Token::Comment);
|
|
|
|
string ether = formatToken(Token::Ether);
|
2020-02-25 16:55:14 +00:00
|
|
|
string wei = formatToken(Token::Wei);
|
2019-02-21 00:50:41 +00:00
|
|
|
string newline = formatToken(Token::Newline);
|
|
|
|
string failure = formatToken(Token::Failure);
|
|
|
|
|
2020-10-28 18:04:24 +00:00
|
|
|
if (m_call.kind == FunctionCall::Kind::Library)
|
2019-11-12 13:13:57 +00:00
|
|
|
{
|
|
|
|
stream << _linePrefix << newline << ws << "library:" << ws << m_call.signature;
|
|
|
|
return;
|
|
|
|
}
|
2020-10-28 18:48:16 +00:00
|
|
|
else if (m_call.kind == FunctionCall::Kind::Storage)
|
|
|
|
{
|
|
|
|
stream << _linePrefix << newline << ws << "storage" << colon << ws;
|
|
|
|
soltestAssert(m_rawBytes.size() == 1, "");
|
|
|
|
soltestAssert(m_call.expectations.rawBytes().size() == 1, "");
|
|
|
|
bool isEmpty = _renderResult ? m_rawBytes.front() == 0 : m_call.expectations.rawBytes().front() == 0;
|
|
|
|
string output = isEmpty ? "empty" : "nonempty";
|
|
|
|
if (_renderResult && !matchesExpectation())
|
|
|
|
AnsiColorized(stream, highlight, {util::formatting::RED_BACKGROUND}) << output;
|
|
|
|
else
|
|
|
|
stream << output;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-11-12 13:13:57 +00:00
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
/// Formats the function signature. This is the same independent from the display-mode.
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << _linePrefix << newline << ws << m_call.signature;
|
2020-02-18 11:57:48 +00:00
|
|
|
if (m_call.value.value > u256(0))
|
2020-02-25 16:55:14 +00:00
|
|
|
{
|
|
|
|
if (m_call.value.unit == FunctionValueUnit::Ether)
|
|
|
|
stream << comma << ws << (m_call.value.value / exp256(10, 18)) << ws << ether;
|
|
|
|
else if (m_call.value.unit == FunctionValueUnit::Wei)
|
|
|
|
stream << comma << ws << m_call.value.value << ws << wei;
|
|
|
|
else
|
|
|
|
soltestAssert(false, "");
|
|
|
|
}
|
2019-02-21 00:50:41 +00:00
|
|
|
if (!m_call.arguments.rawBytes().empty())
|
|
|
|
{
|
|
|
|
string output = formatRawParameters(m_call.arguments.parameters, _linePrefix);
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << colon;
|
2019-07-13 12:02:20 +00:00
|
|
|
if (!m_call.arguments.parameters.at(0).format.newline)
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws;
|
|
|
|
stream << output;
|
2019-02-28 13:16:16 +00:00
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Formats comments on the function parameters and the arrow taking
|
|
|
|
/// the display-mode into account.
|
|
|
|
if (_singleLine)
|
|
|
|
{
|
|
|
|
if (!m_call.arguments.comment.empty())
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws << comment << m_call.arguments.comment << comment;
|
2019-07-13 11:31:55 +00:00
|
|
|
|
|
|
|
if (m_call.omitsArrow)
|
|
|
|
{
|
|
|
|
if (_renderResult && (m_failure || !matchesExpectation()))
|
2019-07-13 12:02:20 +00:00
|
|
|
stream << ws << arrow;
|
2019-07-13 11:31:55 +00:00
|
|
|
}
|
|
|
|
else
|
2019-07-13 12:02:20 +00:00
|
|
|
stream << ws << arrow;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << endl << _linePrefix << newline << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
if (!m_call.arguments.comment.empty())
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << comment << m_call.arguments.comment << comment;
|
|
|
|
stream << endl << _linePrefix << newline << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
2019-07-13 12:02:20 +00:00
|
|
|
stream << arrow;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format either the expected output or the actual result output
|
|
|
|
string result;
|
|
|
|
if (!_renderResult)
|
|
|
|
{
|
|
|
|
bool const isFailure = m_call.expectations.failure;
|
2019-02-28 13:16:16 +00:00
|
|
|
result = isFailure ?
|
2019-08-07 09:24:02 +00:00
|
|
|
formatFailure(_errorReporter, m_call, m_rawBytes, _renderResult, highlight) :
|
2019-02-28 13:16:16 +00:00
|
|
|
formatRawParameters(m_call.expectations.result);
|
2019-07-13 12:02:20 +00:00
|
|
|
if (!result.empty())
|
2019-12-23 15:50:30 +00:00
|
|
|
AnsiColorized(stream, highlight, {util::formatting::RED_BACKGROUND}) << ws << result;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-12 17:28:30 +00:00
|
|
|
if (m_calledNonExistingFunction)
|
|
|
|
_errorReporter.warning("The function \"" + m_call.signature + "\" is not known to the compiler.");
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
bytes output = m_rawBytes;
|
|
|
|
bool const isFailure = m_failure;
|
2019-02-28 13:16:16 +00:00
|
|
|
result = isFailure ?
|
2019-08-07 09:24:02 +00:00
|
|
|
formatFailure(_errorReporter, m_call, output, _renderResult, highlight) :
|
2019-02-28 13:16:16 +00:00
|
|
|
matchesExpectation() ?
|
|
|
|
formatRawParameters(m_call.expectations.result) :
|
2019-04-11 18:25:20 +00:00
|
|
|
formatBytesParameters(
|
|
|
|
_errorReporter,
|
|
|
|
output,
|
|
|
|
m_call.signature,
|
|
|
|
m_call.expectations.result,
|
|
|
|
highlight
|
|
|
|
);
|
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
if (!matchesExpectation())
|
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<ParameterList> abiParams;
|
2019-08-14 19:05:26 +00:00
|
|
|
|
2019-09-09 20:46:43 +00:00
|
|
|
if (isFailure)
|
|
|
|
{
|
|
|
|
if (!output.empty())
|
2019-10-28 10:39:30 +00:00
|
|
|
abiParams = ContractABIUtils::failureParameters(output);
|
2019-09-09 20:46:43 +00:00
|
|
|
}
|
2019-08-14 19:05:26 +00:00
|
|
|
else
|
|
|
|
abiParams = ContractABIUtils::parametersFromJsonOutputs(
|
|
|
|
_errorReporter,
|
|
|
|
m_contractABI,
|
|
|
|
m_call.signature
|
|
|
|
);
|
|
|
|
|
|
|
|
string bytesOutput = abiParams ?
|
2019-10-28 10:39:30 +00:00
|
|
|
BytesUtils::formatRawBytes(output, abiParams.value(), _linePrefix) :
|
2019-09-11 13:45:53 +00:00
|
|
|
BytesUtils::formatRawBytes(
|
|
|
|
output,
|
2020-10-13 10:55:46 +00:00
|
|
|
ContractABIUtils::defaultParameters((output.size() + 31) / 32),
|
2019-09-11 13:45:53 +00:00
|
|
|
_linePrefix
|
|
|
|
);
|
2019-08-14 19:05:26 +00:00
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
_errorReporter.warning(
|
|
|
|
"The call to \"" + m_call.signature + "\" returned \n" +
|
2019-08-14 19:05:26 +00:00
|
|
|
bytesOutput
|
2019-08-07 09:24:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
if (isFailure)
|
2019-12-23 15:50:30 +00:00
|
|
|
AnsiColorized(stream, highlight, {util::formatting::RED_BACKGROUND}) << ws << result;
|
2019-04-11 18:25:20 +00:00
|
|
|
else
|
2019-07-13 12:02:20 +00:00
|
|
|
if (!result.empty())
|
|
|
|
stream << ws << result;
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format comments on expectations taking the display-mode into account.
|
|
|
|
if (_singleLine)
|
|
|
|
{
|
|
|
|
if (!m_call.expectations.comment.empty())
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws << comment << m_call.expectations.comment << comment;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_call.expectations.comment.empty())
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << endl << _linePrefix << newline << ws;
|
|
|
|
stream << comment << m_call.expectations.comment << comment;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
formatOutput(m_call.displayMode == FunctionCall::DisplayMode::SingleLine);
|
|
|
|
return stream.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
string TestFunctionCall::formatBytesParameters(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
bytes const& _bytes,
|
2019-04-11 18:25:20 +00:00
|
|
|
string const& _signature,
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::test::ParameterList const& _parameters,
|
2019-08-07 09:24:02 +00:00
|
|
|
bool _highlight,
|
|
|
|
bool _failure
|
2019-04-11 13:42:48 +00:00
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-12-23 15:50:30 +00:00
|
|
|
using ParameterList = solidity::frontend::test::ParameterList;
|
2019-04-11 18:25:20 +00:00
|
|
|
|
|
|
|
stringstream os;
|
2019-08-07 09:24:02 +00:00
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
if (_bytes.empty())
|
|
|
|
return {};
|
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
if (_failure)
|
|
|
|
{
|
2019-08-14 19:05:26 +00:00
|
|
|
os << BytesUtils::formatBytesRange(
|
|
|
|
_bytes,
|
2019-08-19 12:54:31 +00:00
|
|
|
ContractABIUtils::failureParameters(_bytes),
|
2019-08-14 19:05:26 +00:00
|
|
|
_highlight
|
|
|
|
);
|
2019-08-07 09:24:02 +00:00
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
else
|
2019-07-21 14:58:48 +00:00
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<ParameterList> abiParams = ContractABIUtils::parametersFromJsonOutputs(
|
2019-07-21 14:58:48 +00:00
|
|
|
_errorReporter,
|
2019-08-07 09:24:02 +00:00
|
|
|
m_contractABI,
|
|
|
|
_signature
|
2019-07-21 14:58:48 +00:00
|
|
|
);
|
2019-04-11 18:25:20 +00:00
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
if (abiParams)
|
2019-07-21 14:58:48 +00:00
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
std::optional<ParameterList> preferredParams = ContractABIUtils::preferredParameters(
|
2019-08-07 09:24:02 +00:00
|
|
|
_errorReporter,
|
|
|
|
_parameters,
|
2019-10-28 10:39:30 +00:00
|
|
|
abiParams.value(),
|
2019-08-07 09:24:02 +00:00
|
|
|
_bytes
|
|
|
|
);
|
|
|
|
|
|
|
|
if (preferredParams)
|
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
ContractABIUtils::overwriteParameters(_errorReporter, preferredParams.value(), abiParams.value());
|
|
|
|
os << BytesUtils::formatBytesRange(_bytes, preferredParams.value(), _highlight);
|
2019-08-07 09:24:02 +00:00
|
|
|
}
|
2019-07-21 14:58:48 +00:00
|
|
|
}
|
2019-08-07 09:24:02 +00:00
|
|
|
else
|
|
|
|
{
|
2020-10-13 10:55:46 +00:00
|
|
|
ParameterList defaultParameters = ContractABIUtils::defaultParameters((_bytes.size() + 31) / 32);
|
2019-08-14 19:05:26 +00:00
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
ContractABIUtils::overwriteParameters(_errorReporter, defaultParameters, _parameters);
|
|
|
|
os << BytesUtils::formatBytesRange(_bytes, defaultParameters, _highlight);
|
|
|
|
}
|
|
|
|
return os.str();
|
2019-04-11 18:25:20 +00:00
|
|
|
}
|
2019-08-07 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string TestFunctionCall::formatFailure(
|
|
|
|
ErrorReporter& _errorReporter,
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::test::FunctionCall const& _call,
|
2019-08-07 09:24:02 +00:00
|
|
|
bytes const& _output,
|
|
|
|
bool _renderResult,
|
|
|
|
bool _highlight
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
stringstream os;
|
|
|
|
|
|
|
|
os << formatToken(Token::Failure);
|
|
|
|
|
|
|
|
if (!_output.empty())
|
|
|
|
os << ", ";
|
|
|
|
|
|
|
|
if (_renderResult)
|
|
|
|
os << formatBytesParameters(
|
|
|
|
_errorReporter,
|
|
|
|
_output,
|
|
|
|
_call.signature,
|
|
|
|
_call.expectations.result,
|
|
|
|
_highlight,
|
|
|
|
true
|
2019-07-21 14:58:48 +00:00
|
|
|
);
|
2019-08-07 09:24:02 +00:00
|
|
|
else
|
|
|
|
os << formatRawParameters(_call.expectations.result);
|
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
return os.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
string TestFunctionCall::formatRawParameters(
|
2019-12-23 15:50:30 +00:00
|
|
|
solidity::frontend::test::ParameterList const& _params,
|
2019-04-11 18:25:20 +00:00
|
|
|
std::string const& _linePrefix
|
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-04-11 18:25:20 +00:00
|
|
|
stringstream os;
|
2019-02-21 00:50:41 +00:00
|
|
|
for (auto const& param: _params)
|
2019-07-21 14:15:02 +00:00
|
|
|
if (!param.rawString.empty())
|
2019-07-13 12:02:20 +00:00
|
|
|
{
|
|
|
|
if (param.format.newline)
|
2019-07-21 14:15:02 +00:00
|
|
|
os << endl << _linePrefix << "// ";
|
2020-08-29 19:57:37 +00:00
|
|
|
for (auto const c: param.rawString)
|
|
|
|
os << (c >= ' ' ? string(1, c) : "\\x" + toHex(static_cast<uint8_t>(c)));
|
2019-07-21 14:15:02 +00:00
|
|
|
if (¶m != &_params.back())
|
2019-07-13 12:02:20 +00:00
|
|
|
os << ", ";
|
|
|
|
}
|
2019-04-11 18:25:20 +00:00
|
|
|
return os.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestFunctionCall::reset()
|
|
|
|
{
|
|
|
|
m_rawBytes = bytes{};
|
|
|
|
m_failure = true;
|
2020-02-12 17:28:30 +00:00
|
|
|
m_calledNonExistingFunction = false;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TestFunctionCall::matchesExpectation() const
|
|
|
|
{
|
|
|
|
return m_failure == m_call.expectations.failure && m_rawBytes == m_call.expectations.rawBytes();
|
|
|
|
}
|