2019-02-06 11:10:05 +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/SemanticTest.h>
|
|
|
|
#include <test/Options.h>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace dev::solidity::test;
|
2019-02-06 11:10:48 +00:00
|
|
|
using namespace dev::formatting;
|
2019-02-06 11:10:05 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace boost::algorithm;
|
|
|
|
using namespace boost::unit_test;
|
2019-02-06 11:10:48 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2019-02-06 11:10:05 +00:00
|
|
|
|
|
|
|
|
2019-03-18 13:08:56 +00:00
|
|
|
SemanticTest::SemanticTest(string const& _filename, string const& _ipcPath, langutil::EVMVersion _evmVersion):
|
2019-03-15 16:22:04 +00:00
|
|
|
SolidityExecutionFramework(_ipcPath, _evmVersion)
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
|
|
|
ifstream file(_filename);
|
2019-02-06 11:10:48 +00:00
|
|
|
soltestAssert(file, "Cannot open test contract: \"" + _filename + "\".");
|
2019-02-06 11:10:05 +00:00
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
2019-07-13 10:04:22 +00:00
|
|
|
std::tie(m_source, m_lineOffset) = parseSourceAndSettingsWithLineNumbers(file);
|
|
|
|
|
2019-03-27 18:45:53 +00:00
|
|
|
if (m_settings.count("compileViaYul"))
|
|
|
|
{
|
2019-06-06 16:02:48 +00:00
|
|
|
if (m_settings["compileViaYul"] == "also")
|
|
|
|
{
|
|
|
|
m_validatedSettings["compileViaYul"] = m_settings["compileViaYul"];
|
|
|
|
m_runWithYul = true;
|
|
|
|
m_runWithoutYul = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_validatedSettings["compileViaYul"] = "only";
|
|
|
|
m_runWithYul = true;
|
|
|
|
m_runWithoutYul = false;
|
|
|
|
}
|
2019-03-27 18:45:53 +00:00
|
|
|
m_settings.erase("compileViaYul");
|
|
|
|
}
|
2019-02-06 11:10:05 +00:00
|
|
|
parseExpectations(file);
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult SemanticTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2019-06-06 16:02:48 +00:00
|
|
|
for(bool compileViaYul: set<bool>{!m_runWithoutYul, m_runWithYul})
|
|
|
|
{
|
|
|
|
bool success = true;
|
2019-05-15 10:01:14 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
m_compileViaYul = compileViaYul;
|
|
|
|
if (compileViaYul)
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Running via Yul:" << endl;
|
2019-02-06 11:10:05 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
for (auto& test: m_tests)
|
|
|
|
test.reset();
|
2019-02-06 11:10:05 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
for (auto& test: m_tests)
|
|
|
|
{
|
|
|
|
if (&test == &m_tests.front())
|
|
|
|
if (test.call().isConstructor)
|
2019-07-13 10:33:28 +00:00
|
|
|
deploy("", test.call().value, test.call().arguments.rawBytes());
|
2019-06-06 16:02:48 +00:00
|
|
|
else
|
|
|
|
soltestAssert(deploy("", 0, bytes()), "Failed to deploy contract.");
|
2019-05-15 10:01:14 +00:00
|
|
|
else
|
2019-06-06 16:02:48 +00:00
|
|
|
soltestAssert(!test.call().isConstructor, "Constructor has to be the first function call.");
|
2019-05-15 10:01:14 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
if (test.call().isConstructor)
|
|
|
|
{
|
|
|
|
if (m_transactionSuccessful == test.call().expectations.failure)
|
|
|
|
success = false;
|
2019-05-15 10:24:23 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
test.setFailure(!m_transactionSuccessful);
|
|
|
|
test.setRawBytes(bytes());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes output = callContractFunctionWithValueNoEncoding(
|
|
|
|
test.call().signature,
|
|
|
|
test.call().value,
|
|
|
|
test.call().arguments.rawBytes()
|
|
|
|
);
|
|
|
|
|
|
|
|
if ((m_transactionSuccessful == test.call().expectations.failure) || (output != test.call().expectations.rawBytes()))
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
test.setFailure(!m_transactionSuccessful);
|
|
|
|
test.setRawBytes(std::move(output));
|
|
|
|
test.setContractABI(m_compiler.contractABI(m_compiler.lastContractName()));
|
|
|
|
}
|
2019-05-15 10:01:14 +00:00
|
|
|
}
|
2019-02-06 11:10:05 +00:00
|
|
|
|
2019-06-06 16:02:48 +00:00
|
|
|
if (!success)
|
2019-04-11 13:42:48 +00:00
|
|
|
{
|
2019-06-06 16:02:48 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
|
|
|
|
for (auto const& test: m_tests)
|
|
|
|
{
|
|
|
|
ErrorReporter errorReporter;
|
|
|
|
_stream << test.format(errorReporter, _linePrefix, false, _formatted) << endl;
|
|
|
|
_stream << errorReporter.format(_linePrefix, _formatted);
|
|
|
|
}
|
|
|
|
_stream << endl;
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
|
|
|
|
for (auto const& test: m_tests)
|
|
|
|
{
|
|
|
|
ErrorReporter errorReporter;
|
|
|
|
_stream << test.format(errorReporter, _linePrefix, true, _formatted) << endl;
|
|
|
|
_stream << errorReporter.format(_linePrefix, _formatted);
|
|
|
|
}
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, RED}) << _linePrefix << endl << _linePrefix
|
|
|
|
<< "Attention: Updates on the test will apply the detected format displayed." << endl;
|
|
|
|
if (compileViaYul && m_runWithoutYul)
|
|
|
|
{
|
|
|
|
_stream << _linePrefix << endl << _linePrefix;
|
|
|
|
AnsiColorized(_stream, _formatted, {RED_BACKGROUND}) << "Note that the test passed without Yul.";
|
|
|
|
_stream << endl;
|
|
|
|
}
|
|
|
|
else if (!compileViaYul && m_runWithYul)
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, YELLOW}) << _linePrefix << endl << _linePrefix
|
|
|
|
<< "Note that the test also has to pass via Yul." << endl;
|
|
|
|
return TestResult::Failure;
|
2019-04-11 13:42:48 +00:00
|
|
|
}
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
2019-06-06 16:02:48 +00:00
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::Success;
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 13:08:56 +00:00
|
|
|
void SemanticTest::printSource(ostream& _stream, string const& _linePrefix, bool) const
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
|
|
|
stringstream stream(m_source);
|
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << _linePrefix << line << endl;
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:10:48 +00:00
|
|
|
void SemanticTest::printUpdatedExpectations(ostream& _stream, string const&) const
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
|
|
|
for (auto const& test: m_tests)
|
2019-02-21 00:50:41 +00:00
|
|
|
_stream << test.format("", true, false) << endl;
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SemanticTest::parseExpectations(istream& _stream)
|
|
|
|
{
|
|
|
|
TestFileParser parser{_stream};
|
2019-07-13 10:04:22 +00:00
|
|
|
auto functionCalls = parser.parseFunctionCalls(m_lineOffset);
|
2019-03-27 18:45:53 +00:00
|
|
|
std::move(functionCalls.begin(), functionCalls.end(), back_inserter(m_tests));
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SemanticTest::deploy(string const& _contractName, u256 const& _value, bytes const& _arguments)
|
|
|
|
{
|
|
|
|
auto output = compileAndRunWithoutCheck(m_source, _value, _contractName, _arguments);
|
|
|
|
return !output.empty() && m_transactionSuccessful;
|
|
|
|
}
|