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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <test/libsolidity/util/TestFileParser.h>
|
2019-07-21 13:46:03 +00:00
|
|
|
#include <test/libsolidity/util/SoltestErrors.h>
|
2019-02-21 00:50:41 +00:00
|
|
|
|
|
|
|
#include <liblangutil/Exceptions.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
|
|
|
#include <libsolutil/CommonData.h>
|
2019-02-21 00:50:41 +00:00
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
#include <json/json.h>
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
#include <iosfwd>
|
|
|
|
#include <numeric>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a function call and the result it returned. It stores the call
|
|
|
|
* representation itself, the actual byte result (if any) and a string representation
|
|
|
|
* used for the interactive update routine provided by isoltest. It also provides
|
|
|
|
* functionality to compare the actual result with the expectations attached to the
|
|
|
|
* call object, as well as a way to reset the result if executed multiple times.
|
|
|
|
*/
|
|
|
|
class TestFunctionCall
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestFunctionCall(FunctionCall _call): m_call(std::move(_call)) {}
|
|
|
|
|
|
|
|
/// Formats this function call test and applies the format that was detected during parsing.
|
2019-10-15 10:50:32 +00:00
|
|
|
/// If _renderResult is false, the expected result of the call will be used, if it's true
|
2019-02-21 00:50:41 +00:00
|
|
|
/// the actual result is used.
|
|
|
|
/// If _highlight is false, it's formatted without colorized highlighting. If it's true, AnsiColorized is
|
|
|
|
/// used to apply a colorized highlighting.
|
2019-04-11 18:25:20 +00:00
|
|
|
/// If test expectations do not match, the contract ABI is consulted in order to get the
|
|
|
|
/// right encoding for returned bytes, based on the parsed return types.
|
2019-04-11 13:42:48 +00:00
|
|
|
/// Reports warnings and errors to the error reporter.
|
|
|
|
std::string format(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
std::string const& _linePrefix = "",
|
|
|
|
bool const _renderResult = false,
|
|
|
|
bool const _highlight = false
|
|
|
|
) const;
|
|
|
|
|
|
|
|
/// Overloaded version that passes an error reporter which is never used outside
|
|
|
|
/// of this function.
|
|
|
|
std::string format(
|
|
|
|
std::string const& _linePrefix = "",
|
|
|
|
bool const _renderResult = false,
|
|
|
|
bool const _highlight = false
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
ErrorReporter reporter;
|
|
|
|
return format(reporter, _linePrefix, _renderResult, _highlight);
|
|
|
|
}
|
2019-02-21 00:50:41 +00:00
|
|
|
|
|
|
|
/// Resets current results in case the function was called and the result
|
|
|
|
/// stored already (e.g. if test case was updated via isoltest).
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
FunctionCall const& call() const { return m_call; }
|
2020-02-12 17:28:30 +00:00
|
|
|
void calledNonExistingFunction() { m_calledNonExistingFunction = true; }
|
2019-02-21 00:50:41 +00:00
|
|
|
void setFailure(const bool _failure) { m_failure = _failure; }
|
|
|
|
void setRawBytes(const bytes _rawBytes) { m_rawBytes = _rawBytes; }
|
2019-04-11 18:25:20 +00:00
|
|
|
void setContractABI(Json::Value _contractABI) { m_contractABI = std::move(_contractABI); }
|
2019-02-21 00:50:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Tries to format the given `bytes`, applying the detected ABI types that have be set for each parameter.
|
|
|
|
/// Throws if there's a mismatch in the size of `bytes` and the desired formats that are specified
|
|
|
|
/// in the ABI type.
|
2019-04-11 13:42:48 +00:00
|
|
|
/// Reports warnings and errors to the error reporter.
|
|
|
|
std::string formatBytesParameters(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
bytes const& _bytes,
|
2019-04-11 18:25:20 +00:00
|
|
|
std::string const& _signature,
|
|
|
|
ParameterList const& _params,
|
2019-08-07 09:24:02 +00:00
|
|
|
bool highlight = false,
|
|
|
|
bool failure = false
|
2019-04-11 18:25:20 +00:00
|
|
|
) const;
|
|
|
|
|
|
|
|
/// Formats a given _bytes applying the _abiType.
|
|
|
|
std::string formatBytesRange(
|
|
|
|
bytes const& _bytes,
|
|
|
|
ABIType const& _abiType
|
2019-04-11 13:42:48 +00:00
|
|
|
) const;
|
2019-02-21 00:50:41 +00:00
|
|
|
|
2019-08-07 09:24:02 +00:00
|
|
|
/// Formats a FAILURE plus additional parameters, if e.g. a revert message was returned.
|
|
|
|
std::string formatFailure(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
FunctionCall const& _call,
|
|
|
|
bytes const& _output,
|
|
|
|
bool _renderResult,
|
|
|
|
bool _highlight
|
|
|
|
) const;
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
/// Formats the given parameters using their raw string representation.
|
2019-04-11 18:25:20 +00:00
|
|
|
std::string formatRawParameters(
|
|
|
|
ParameterList const& _params,
|
|
|
|
std::string const& _linePrefix = ""
|
|
|
|
) const;
|
2019-02-21 00:50:41 +00:00
|
|
|
|
|
|
|
/// Compares raw expectations (which are converted to a byte representation before),
|
|
|
|
/// and also the expected transaction status of the function call to the actual test results.
|
|
|
|
bool matchesExpectation() const;
|
|
|
|
|
|
|
|
/// Function call that has been parsed and which holds all parameters / expectations.
|
|
|
|
FunctionCall m_call;
|
|
|
|
/// Result of the actual call been made.
|
|
|
|
bytes m_rawBytes = bytes{};
|
|
|
|
/// Transaction status of the actual call. False in case of a REVERT or any other failure.
|
|
|
|
bool m_failure = true;
|
2019-04-11 18:25:20 +00:00
|
|
|
/// JSON object which holds the contract ABI and that is used to set the output formatting
|
|
|
|
/// in the interactive update routine.
|
|
|
|
Json::Value m_contractABI;
|
2020-02-12 17:28:30 +00:00
|
|
|
/// Flags that the test failed because the called function is not known to exist on the contract.
|
|
|
|
bool m_calledNonExistingFunction = false;
|
2019-02-21 00:50:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|