Skip to content

Commit

Permalink
safely parse files from json/yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Nov 26, 2019
1 parent 92f50d6 commit 1945410
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
40 changes: 39 additions & 1 deletion retesteth/TestHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <csignal>
#include <mutex>

#include <retesteth/Options.h>
#include <retesteth/TestHelper.h>
#include <retesteth/TestOutputHelper.h>
#include <retesteth/Options.h>
#include <retesteth/dataObject/ConvertFile.h>
#include <retesteth/dataObject/ConvertYaml.h>

using namespace std;
namespace fs = boost::filesystem;
Expand All @@ -37,6 +39,42 @@ Json::Value readJson(fs::path const& _file)
}
#endif

/// Safely read the json file into DataObject
DataObject readJsonData(fs::path const& _file, string const& _stopper, bool _autosort)
{
try
{
string s = dev::contentsString(_file);
ETH_ERROR_REQUIRE_MESSAGE(s.length() > 0,
"Contents of " + _file.string() + " is empty. Trying to parse empty file.");
return dataobject::ConvertJsoncppStringToData(s, _stopper, _autosort);
}
catch (std::exception const& _ex)
{
ETH_ERROR_MESSAGE(
string("\nError when parsing file (") + _file.c_str() + ") " + _ex.what());
return DataObject();
}
}

/// Safely read the yaml file into DataObject
DataObject readYamlData(fs::path const& _file)
{
try
{
string s = dev::contentsString(_file);
ETH_ERROR_REQUIRE_MESSAGE(s.length() > 0,
"Contents of " + _file.string() + " is empty. Trying to parse empty file.");
return dataobject::ConvertYamlToData(YAML::Load(s));
}
catch (std::exception const& _ex)
{
ETH_ERROR_MESSAGE(
string("\nError when parsing file (") + _file.c_str() + ") " + _ex.what());
return DataObject();
}
}

vector<fs::path> getFiles(
fs::path const& _dirPath, set<string> const _extentionMask, string const& _particularFile)
{
Expand Down
5 changes: 5 additions & 0 deletions retesteth/TestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace test {
Json::Value readJson(fs::path const& _path);
#endif

/// Safely read the json file into DataObject
DataObject readJsonData(
fs::path const& _file, string const& _stopper = string(), bool _autosort = false);
DataObject readYamlData(fs::path const& _file);

/// Get files from directory
std::vector<boost::filesystem::path> getFiles(boost::filesystem::path const& _dirPath, std::set<std::string> _extentionMask, std::string const& _particularFile = {});

Expand Down
20 changes: 4 additions & 16 deletions retesteth/TestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Base functions for all test suites
*/

#include <dataObject/ConvertFile.h>
#include <dataObject/ConvertYaml.h>
#include <dataObject/DataObject.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/Log.h>
Expand Down Expand Up @@ -51,16 +49,10 @@ struct TestFileData
TestFileData readTestFile(fs::path const& _testFileName)
{
TestFileData testData;

// Check that file is not empty
string const s = dev::contentsString(_testFileName);
ETH_ERROR_REQUIRE_MESSAGE(
s.length() > 0, "Contents of " + _testFileName.string() + " is empty.");

if (_testFileName.extension() == ".json")
testData.data = dataobject::ConvertJsoncppStringToData(s, string(), true);
testData.data = test::readJsonData(_testFileName, string(), true);
else if (_testFileName.extension() == ".yml")
testData.data = dataobject::ConvertYamlToData(YAML::Load(s));
testData.data = test::readYamlData(_testFileName);
else
ETH_ERROR_MESSAGE(
"Unknown test format!" + test::TestOutputHelper::get().testFile().string());
Expand Down Expand Up @@ -142,8 +134,7 @@ void addClientInfo(

void checkFillerHash(fs::path const& _compiledTest, fs::path const& _sourceTest)
{
dataobject::DataObject v =
dataobject::ConvertJsoncppStringToData(dev::contentsString(_compiledTest), "_info");
dataobject::DataObject v = test::readJsonData(_compiledTest, "_info");
TestFileData fillerData = readTestFile(_sourceTest);
for (auto const& i: v.getSubObjects())
{
Expand Down Expand Up @@ -525,10 +516,7 @@ void TestSuite::executeTest(string const& _testFolder, fs::path const& _testFile
void TestSuite::executeFile(boost::filesystem::path const& _file) const
{
TestSuiteOptions opt;
std::string s = dev::contentsString(_file);
ETH_ERROR_REQUIRE_MESSAGE(
s.length() > 0, "Contents of " + _file.string() + " is empty. Have you filled the test?");
doTests(dataobject::ConvertJsoncppStringToData(s), opt);
doTests(test::readJsonData(_file), opt);
}

}
Expand Down
5 changes: 2 additions & 3 deletions retesteth/configs/ClientConfig.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <retesteth/Socket.h>
#include <retesteth/dataObject/ConvertFile.h>
#include <retesteth/TestHelper.h>
#include <retesteth/ethObjects/object.h>
#include <boost/asio.hpp>
#include <mutex>
Expand Down Expand Up @@ -111,8 +111,7 @@ class ClientConfig : public object
std::vector<string> const& getAdditionalNetworks() const { return m_additional_networks; }
void addGenesisTemplate(string const& _network, fs::path const& _pathToJson)
{
string s = dev::contentsString(_pathToJson);
m_genesisTemplate[_network] = dataobject::ConvertJsoncppStringToData(s);
m_genesisTemplate[_network] = test::readJsonData(_pathToJson);
}
DataObject const& getGenesisTemplate(string const& _network) const
{
Expand Down
8 changes: 3 additions & 5 deletions retesteth/configs/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ void deployFirstRunConfigs()
if (!fs::exists(homeDir / "version"))
return;
string version = dev::contentsString(homeDir / "version");
version = version.substr(0, version.length() - 1); // new line char
if (version != string(ETH_PROJECT_VERSION))
ETH_WARNING("Retesteth configs version is different (running: '" +
string(ETH_PROJECT_VERSION) + "' vs config '" + version +
Expand Down Expand Up @@ -113,8 +112,8 @@ std::vector<ClientConfig> const& Options::DynamicOptions::getClientConfigs()
fs::path configFilePath = configPath / "config";
ETH_FAIL_REQUIRE_MESSAGE(fs::exists(configFilePath),
string("Client config not found: ") + configFilePath.c_str());
string s = dev::contentsString(configFilePath);
ClientConfig cfg(dataobject::ConvertJsoncppStringToData(s), ClientConfigID(),

ClientConfig cfg(test::readJsonData(configFilePath), ClientConfigID(),
configPath / string(clientName + ".sh"));

// Load genesis templates
Expand All @@ -129,8 +128,7 @@ std::vector<ClientConfig> const& Options::DynamicOptions::getClientConfigs()
fs::path correctMiningRewardPath = genesisTemplatePath / "correctMiningReward.json";
ETH_FAIL_REQUIRE_MESSAGE(fs::exists(correctMiningRewardPath),
"correctMiningReward.json client config not found!");
s = dev::contentsString(correctMiningRewardPath);
cfg.setMiningRewardInfo(dataobject::ConvertJsoncppStringToData(s));
cfg.setMiningRewardInfo(test::readJsonData(correctMiningRewardPath));
cfg.setCorrectMiningRewardFilePath(correctMiningRewardPath);

auto registerGenesisTemplate = [&cfg, &genesisTemplatePath, &clientName](
Expand Down

0 comments on commit 1945410

Please sign in to comment.