2014-03-19 04:35:16 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-03-19 04:35:16 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-03-19 04:35:16 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-03-19 04:35:16 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-19 04:35:16 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-03-19 04:35:16 +00:00
|
|
|
/** @file boostTest.cpp
|
|
|
|
* @author Marko Simovic <markobarko@gmail.com>
|
|
|
|
* @date 2014
|
|
|
|
* Stub for generating main boost.test module.
|
2015-08-13 10:27:47 +00:00
|
|
|
* Original code taken from boost sources.
|
2014-03-19 04:35:16 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-04 20:01:56 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2016-03-21 23:05:30 +00:00
|
|
|
|
2016-08-06 09:37:52 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4535) // calling _set_se_translator requires /EHa
|
|
|
|
#endif
|
2018-07-25 07:17:53 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2021-12-20 18:03:48 +00:00
|
|
|
#include <boost/test/tree/traverse.hpp>
|
2016-08-06 09:37:52 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
2017-04-26 09:53:44 +00:00
|
|
|
|
2018-12-05 17:00:23 +00:00
|
|
|
#include <test/InteractiveTests.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2019-07-16 17:05:13 +00:00
|
|
|
#include <test/EVMHost.h>
|
2017-04-26 09:53:44 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
2018-12-05 17:00:23 +00:00
|
|
|
#include <string>
|
2018-06-08 12:17:50 +00:00
|
|
|
|
2017-04-26 09:53:44 +00:00
|
|
|
using namespace boost::unit_test;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::frontend::test;
|
2018-06-08 12:17:50 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace std;
|
|
|
|
|
2017-09-28 08:28:41 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void removeTestSuite(std::string const& _name)
|
|
|
|
{
|
|
|
|
master_test_suite_t& master = framework::master_test_suite();
|
|
|
|
auto id = master.get(_name);
|
2022-02-05 14:12:49 +00:00
|
|
|
soltestAssert(id != INV_TEST_UNIT_ID, "Removing non-existent test suite!");
|
2017-09-28 08:28:41 +00:00
|
|
|
master.remove(id);
|
|
|
|
}
|
2018-06-08 12:17:50 +00:00
|
|
|
|
2021-12-20 18:03:48 +00:00
|
|
|
/**
|
|
|
|
* Class that traverses the boost test tree and removes unit tests that are
|
|
|
|
* not in the current batch.
|
|
|
|
*/
|
|
|
|
class BoostBatcher: public test_tree_visitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BoostBatcher(solidity::test::Batcher& _batcher):
|
|
|
|
m_batcher(_batcher)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void visit(test_case const& _testCase) override
|
|
|
|
{
|
|
|
|
if (!m_batcher.checkAndAdvance())
|
|
|
|
// disabling them would be nicer, but it does not work like this:
|
|
|
|
// const_cast<test_case&>(_testCase).p_run_status.value = test_unit::RS_DISABLED;
|
|
|
|
m_path.back()->remove(_testCase.p_id);
|
|
|
|
}
|
|
|
|
bool test_suite_start(test_suite const& _testSuite) override
|
|
|
|
{
|
|
|
|
m_path.push_back(&const_cast<test_suite&>(_testSuite));
|
|
|
|
return test_tree_visitor::test_suite_start(_testSuite);
|
|
|
|
}
|
|
|
|
void test_suite_finish(test_suite const& _testSuite) override
|
|
|
|
{
|
|
|
|
m_path.pop_back();
|
|
|
|
test_tree_visitor::test_suite_finish(_testSuite);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
solidity::test::Batcher& m_batcher;
|
|
|
|
std::vector<test_suite*> m_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-06-25 11:22:15 +00:00
|
|
|
void runTestCase(TestCase::Config const& _config, TestCase::TestCaseCreator const& _testCaseCreator)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
stringstream errorStream;
|
|
|
|
auto testCase = _testCaseCreator(_config);
|
|
|
|
if (testCase->shouldRun())
|
|
|
|
switch (testCase->run(errorStream))
|
|
|
|
{
|
|
|
|
case TestCase::TestResult::Success:
|
|
|
|
break;
|
|
|
|
case TestCase::TestResult::Failure:
|
|
|
|
BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str());
|
|
|
|
break;
|
|
|
|
case TestCase::TestResult::FatalError:
|
|
|
|
BOOST_ERROR("Fatal error during test.\n" + errorStream.str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (boost::exception const& _e)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Exception during extracted test: " << boost::diagnostic_information(_e));
|
|
|
|
}
|
2021-06-25 11:25:10 +00:00
|
|
|
catch (std::exception const& _e)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Exception during extracted test: " << boost::diagnostic_information(_e));
|
|
|
|
}
|
2021-10-07 15:40:57 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Unknown exception during extracted test: " << boost::current_exception_diagnostic_information());
|
|
|
|
}
|
2021-06-25 11:22:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
int registerTests(
|
|
|
|
boost::unit_test::test_suite& _suite,
|
|
|
|
boost::filesystem::path const& _basepath,
|
|
|
|
boost::filesystem::path const& _path,
|
2020-11-19 21:32:35 +00:00
|
|
|
bool _enforceCompileToEwasm,
|
2020-07-10 09:22:57 +00:00
|
|
|
vector<string> const& _labels,
|
2021-12-20 18:03:48 +00:00
|
|
|
TestCase::TestCaseCreator _testCaseCreator,
|
|
|
|
solidity::test::Batcher& _batcher
|
2018-06-08 12:17:50 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
int numTestsAdded = 0;
|
|
|
|
fs::path fullpath = _basepath / _path;
|
2021-01-29 13:07:22 +00:00
|
|
|
TestCase::Config config{
|
|
|
|
fullpath.string(),
|
|
|
|
solidity::test::CommonOptions::get().evmVersion(),
|
2022-11-22 12:05:20 +00:00
|
|
|
solidity::test::CommonOptions::get().eofVersion(),
|
2021-01-29 13:07:22 +00:00
|
|
|
solidity::test::CommonOptions::get().vmPaths,
|
2020-11-19 21:32:35 +00:00
|
|
|
_enforceCompileToEwasm,
|
2021-02-12 11:55:07 +00:00
|
|
|
solidity::test::CommonOptions::get().enforceGasTest,
|
2020-11-19 21:32:35 +00:00
|
|
|
solidity::test::CommonOptions::get().enforceGasTestMinValue,
|
2021-01-29 13:07:22 +00:00
|
|
|
};
|
2018-06-08 12:17:50 +00:00
|
|
|
if (fs::is_directory(fullpath))
|
|
|
|
{
|
|
|
|
test_suite* sub_suite = BOOST_TEST_SUITE(_path.filename().string());
|
|
|
|
for (auto const& entry: boost::iterator_range<fs::directory_iterator>(
|
|
|
|
fs::directory_iterator(fullpath),
|
|
|
|
fs::directory_iterator()
|
|
|
|
))
|
2021-03-15 22:52:24 +00:00
|
|
|
if (
|
|
|
|
solidity::test::isValidSemanticTestPath(entry) &&
|
|
|
|
(fs::is_directory(entry.path()) || TestCase::isTestFilename(entry.path().filename()))
|
|
|
|
)
|
2020-04-17 20:24:33 +00:00
|
|
|
numTestsAdded += registerTests(
|
|
|
|
*sub_suite,
|
|
|
|
_basepath, _path / entry.path().filename(),
|
2020-11-19 21:32:35 +00:00
|
|
|
_enforceCompileToEwasm,
|
2020-07-10 09:22:57 +00:00
|
|
|
_labels,
|
2021-12-20 18:03:48 +00:00
|
|
|
_testCaseCreator,
|
|
|
|
_batcher
|
2020-04-17 20:24:33 +00:00
|
|
|
);
|
2018-06-08 12:17:50 +00:00
|
|
|
_suite.add(sub_suite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-20 18:03:48 +00:00
|
|
|
// TODO would be better to set the test to disabled.
|
|
|
|
if (_batcher.checkAndAdvance())
|
|
|
|
{
|
|
|
|
// This must be a vector of unique_ptrs because Boost.Test keeps the equivalent of a string_view to the filename
|
|
|
|
// that is passed in. If the strings were stored directly in the vector, pointers/references to them would be
|
|
|
|
// invalidated on reallocation.
|
|
|
|
static vector<unique_ptr<string const>> filenames;
|
|
|
|
|
|
|
|
filenames.emplace_back(make_unique<string>(_path.string()));
|
|
|
|
auto test_case = make_test_case(
|
|
|
|
[config, _testCaseCreator]
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE_NO_THROW({
|
|
|
|
runTestCase(config, _testCaseCreator);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
_path.stem().string(),
|
|
|
|
*filenames.back(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
for (auto const& _label: _labels)
|
|
|
|
test_case->add_label(_label);
|
|
|
|
_suite.add(test_case);
|
|
|
|
numTestsAdded = 1;
|
|
|
|
}
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
|
|
|
return numTestsAdded;
|
|
|
|
}
|
2020-01-14 16:48:17 +00:00
|
|
|
|
2021-11-15 16:49:27 +00:00
|
|
|
bool initializeOptions()
|
2020-01-14 16:48:17 +00:00
|
|
|
{
|
|
|
|
auto const& suite = boost::unit_test::framework::master_test_suite();
|
|
|
|
|
|
|
|
auto options = std::make_unique<solidity::test::CommonOptions>();
|
2021-11-15 16:49:27 +00:00
|
|
|
bool shouldContinue = options->parse(suite.argc, suite.argv);
|
|
|
|
if (!shouldContinue)
|
|
|
|
return false;
|
2020-01-14 16:48:17 +00:00
|
|
|
options->validate();
|
|
|
|
|
|
|
|
solidity::test::CommonOptions::setSingleton(std::move(options));
|
2021-11-15 16:49:27 +00:00
|
|
|
return true;
|
2020-01-14 16:48:17 +00:00
|
|
|
}
|
2021-12-20 18:03:48 +00:00
|
|
|
|
2017-09-28 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:06:10 +00:00
|
|
|
// TODO: Prototype -- why isn't this declared in the boost headers?
|
|
|
|
// TODO: replace this with a (global) fixture.
|
2021-11-15 16:51:18 +00:00
|
|
|
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]);
|
2020-12-08 20:06:10 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[])
|
2017-04-26 09:53:44 +00:00
|
|
|
{
|
2021-12-20 18:03:48 +00:00
|
|
|
using namespace solidity::test;
|
|
|
|
|
2017-04-26 09:53:44 +00:00
|
|
|
master_test_suite_t& master = framework::master_test_suite();
|
|
|
|
master.p_name.value = "SolidityTests";
|
2018-12-05 17:00:23 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
bool shouldContinue = initializeOptions();
|
|
|
|
if (!shouldContinue)
|
2021-11-30 13:26:59 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2020-01-14 16:48:17 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
if (!solidity::test::loadVMs(solidity::test::CommonOptions::get()))
|
2021-11-30 13:26:59 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2021-05-10 15:23:23 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
|
|
|
cout << endl << "--- SKIPPING ALL SEMANTICS TESTS ---" << endl << endl;
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
if (!solidity::test::CommonOptions::get().enforceGasTest)
|
|
|
|
cout << endl << "WARNING :: Gas Cost Expectations are not being enforced" << endl << endl;
|
2021-12-24 17:53:51 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
Batcher batcher(CommonOptions::get().selectedBatch, CommonOptions::get().batches);
|
|
|
|
if (CommonOptions::get().batches > 1)
|
|
|
|
cout << "Batch " << CommonOptions::get().selectedBatch << " out of " << CommonOptions::get().batches << endl;
|
2021-12-20 18:03:48 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
// Batch the boost tests
|
|
|
|
BoostBatcher boostBatcher(batcher);
|
|
|
|
traverse_test_tree(master, boostBatcher, true);
|
|
|
|
|
|
|
|
// Include the interactive tests in the automatic tests as well
|
|
|
|
for (auto const& ts: g_interactiveTestsuites)
|
|
|
|
{
|
|
|
|
auto const& options = solidity::test::CommonOptions::get();
|
|
|
|
|
|
|
|
if (ts.smt && options.disableSMT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ts.needsVM && solidity::test::CommonOptions::get().disableSemanticTests)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
//solAssert(
|
|
|
|
registerTests(
|
|
|
|
master,
|
|
|
|
options.testPath / ts.path,
|
|
|
|
ts.subpath,
|
|
|
|
options.enforceCompileToEwasm,
|
|
|
|
ts.labels,
|
|
|
|
ts.testCaseCreator,
|
|
|
|
batcher
|
|
|
|
);
|
|
|
|
// > 0, std::string("no ") + ts.title + " tests found");
|
|
|
|
}
|
2021-12-20 18:03:48 +00:00
|
|
|
|
2021-11-15 16:51:18 +00:00
|
|
|
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
|
|
|
{
|
|
|
|
for (auto suite: {
|
|
|
|
"ABIDecoderTest",
|
|
|
|
"ABIEncoderTest",
|
|
|
|
"SolidityAuctionRegistrar",
|
|
|
|
"SolidityWallet",
|
|
|
|
"GasMeterTests",
|
|
|
|
"GasCostTests",
|
|
|
|
"SolidityEndToEndTest",
|
|
|
|
"SolidityOptimizer"
|
|
|
|
})
|
|
|
|
removeTestSuite(suite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (solidity::test::ConfigException const& exception)
|
2018-11-15 15:47:52 +00:00
|
|
|
{
|
2021-11-15 16:51:18 +00:00
|
|
|
cerr << exception.what() << endl;
|
2021-11-30 13:26:59 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2018-11-15 15:47:52 +00:00
|
|
|
}
|
2021-11-15 16:51:18 +00:00
|
|
|
catch (std::runtime_error const& exception)
|
2017-04-26 09:53:44 +00:00
|
|
|
{
|
2021-11-15 16:51:18 +00:00
|
|
|
cerr << exception.what() << endl;
|
2021-11-30 13:26:59 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2017-04-26 09:53:44 +00:00
|
|
|
}
|
2018-12-05 17:00:23 +00:00
|
|
|
|
2020-04-01 01:42:05 +00:00
|
|
|
return nullptr;
|
2017-04-26 09:53:44 +00:00
|
|
|
}
|
2018-07-26 14:23:53 +00:00
|
|
|
|
|
|
|
// BOOST_TEST_DYN_LINK should be defined if user want to link against shared boost test library
|
|
|
|
#ifdef BOOST_TEST_DYN_LINK
|
|
|
|
|
|
|
|
// Because we want to have customized initialization function and support shared boost libraries at the same time,
|
|
|
|
// we are forced to customize the entry point.
|
|
|
|
// see: https://www.boost.org/doc/libs/1_67_0/libs/test/doc/html/boost_test/adv_scenarios/shared_lib_customizations/init_func.html
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
auto init_unit_test = []() -> bool { init_unit_test_suite(0, nullptr); return true; };
|
|
|
|
return boost::unit_test::unit_test_main(init_unit_test, argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|