Commit 8eefede1 authored by Maria Fava's avatar Maria Fava
Browse files

applied modifications

parent e0c20837
......@@ -7,11 +7,13 @@
#include "ADDHCmd.h"
#include <cstdlib>
#include <boost/filesystem.hpp>
#include "ADDHWriter.h"
#include "Config.h"
namespace bfs = boost::filesystem;
/*! \namespace ntof
*
* namespace used for all the element of the nTOF
......@@ -25,7 +27,15 @@ namespace addh {
ADDHCmd::ADDHCmd() :
DIMSuperCommand(
Config::instance().getValue("commandServiceName", "ADDH/Command"))
{}
{
char hostname[_SC_HOST_NAME_MAX];
gethostname(hostname, _SC_HOST_NAME_MAX);
std::string hostnameString(hostname);
std::string defaultPrefixDir = bfs::temp_directory_path().string() +
"ADDH" + hostnameString;
this->filePathPrefix = Config::instance().getValue("outputDirPrefix",
defaultPrefixDir);
}
ADDHCmd::~ADDHCmd() {}
......@@ -55,9 +65,6 @@ void ADDHCmd::commandReceived(ntof::dim::DIMCmd &cmdData)
command.timingEvent = timingEvent;
command.filePath = filePath;
std::string filePathPrefix = Config::instance().getValue(
"outputDirPrefix", "ADDH/Prefix");
// Check if filePath is subpath of filePathPrefix
if (isValidPath(filePath, filePathPrefix))
{
......@@ -76,7 +83,7 @@ void ADDHCmd::commandReceived(ntof::dim::DIMCmd &cmdData)
}
else
{
// setError();
setError(cmdData.getKey(), WRITE_ERR, "Failed to write.");
}
}
else
......@@ -86,24 +93,23 @@ void ADDHCmd::commandReceived(ntof::dim::DIMCmd &cmdData)
}
}
bool ADDHCmd::isValidPath(std::string filePath, std::string filePathPrefix)
bool ADDHCmd::isValidPath(std::string filePath, const std::string filePathPrefix)
{
if (filePath[0] != '/')
{
// filePath is relative, convert to absolute
char *absoluteFilePath = realpath(filePath.c_str(), NULL);
if (absoluteFilePath == NULL)
bfs::path tempPath = filePath;
try
{
filePath = bfs::canonical(tempPath).string();
}
catch (std::exception const &e)
{
// No such file or directory
return false;
}
filePath = absoluteFilePath;
free(absoluteFilePath);
}
if (filePath.find(filePathPrefix + "/") != std::string::npos)
{
return true;
}
return false;
return (filePath.compare(0, filePathPrefix.length(), filePathPrefix) == 0);
}
} // namespace addh
......
......@@ -33,6 +33,9 @@ namespace addh {
*/
class ADDHCmd : public ntof::dim::DIMSuperCommand
{
private:
std::string filePathPrefix;
public:
/*!
* \brief Constructor of the class ADDHCmd
......
......@@ -21,36 +21,29 @@ class TestADDHCmdFile : public CppUnit::TestFixture
{
protected:
CPPUNIT_TEST_SUITE(TestADDHCmdFile);
CPPUNIT_TEST(validatePath);
CPPUNIT_TEST(receiveCommandWithRelativePath);
CPPUNIT_TEST(receiveCommandWithAbsolutePath);
CPPUNIT_TEST(validatePathCheck);
CPPUNIT_TEST(receiveCommandWithCorrectAbsolutePath);
CPPUNIT_TEST(receiveCommandWithCorrectRelativePath);
CPPUNIT_TEST_SUITE_END();
bfs::path abs_tmp, abs_tmp_in, rel_tmp, rel_tmp_in;
bfs::path top_level_dir, correct_prefix_dir, incorrect_prefix_dir;
std::unique_ptr<DimTestHelper> m_dim;
public:
void setUp()
{
abs_tmp = bfs::temp_directory_path() / "test_ADDHCmd";
bfs::create_directories(abs_tmp);
rel_tmp = "./test_ADDHCmd";
bfs::create_directories(rel_tmp);
abs_tmp_in = bfs::temp_directory_path() / "test_ADDHCmd_in";
bfs::create_directories(abs_tmp_in);
rel_tmp_in = "./test_ADDHCmd_in";
bfs::create_directories(rel_tmp_in);
top_level_dir = bfs::temp_directory_path() / "ADDHCmd_test";
bfs::create_directories(top_level_dir);
correct_prefix_dir = top_level_dir / "correct_prefix_dir";
bfs::create_directories(correct_prefix_dir);
incorrect_prefix_dir = top_level_dir / "incorrect_prefix_dir";
bfs::create_directories(incorrect_prefix_dir);
m_dim.reset(new DimTestHelper());
}
void tearDown()
{
bfs::remove_all(abs_tmp);
bfs::remove_all(rel_tmp);
bfs::remove_all(rel_tmp_in);
bfs::remove_all(rel_tmp_in);
bfs::remove_all(top_level_dir);
m_dim.reset();
}
......@@ -59,59 +52,75 @@ public:
std::shared_ptr<ADDH> addh(new ADDH);
WriterFile writer(addh);
COMMAND_ELEMENT command = {std::string("write"), 6789, 12345,
filename.string()};
DimInfoWaiter info("ADDH/Events");
EQ(true, info.waitUpdate());
writer.addCommand(command);
writer.addCommand(
{std::string("write"), 6789, 12345, filename.string()});
EQ(true, info.waitUpdate(2));
EQ(true, bfs::exists(filename));
}
void receiveCommandWithRelativePath()
void receiveCommandWithCorrectRelativePath()
{
bfs::path filename = rel_tmp / "path";
receiveCommand(filename);
bfs::path filename = correct_prefix_dir / "correct_path";
receiveCommand(filename.relative_path());
}
void receiveCommandWithAbsolutePath()
void receiveCommandWithCorrectAbsolutePath()
{
bfs::path filename = abs_tmp / "path";
bfs::path filename = correct_prefix_dir / "correct_path";
receiveCommand(filename);
}
void validatePath()
void validatePathCheck()
{
ADDHCmd *addhCmd_ = new ADDHCmd();
bfs::path abs_path_tmp = abs_tmp / "path";
bfs::create_directories(abs_path_tmp);
bfs::path rel_path_tmp = rel_tmp / "path";
bfs::create_directories(rel_path_tmp);
bfs::path correct_prefixed_path = correct_prefix_dir / "correct_path";
bfs::create_directories(correct_prefixed_path);
bfs::path incorrect_prefixed_path = incorrect_prefix_dir /
"incorrect_path";
bfs::create_directories(incorrect_prefixed_path);
std::string absolute_path_str_correct = "/tmp/test_ADDHCmd/path";
std::string relative_path_str_correct = "./test_ADDHCmd/path";
std::string absolute_path_str_incorrect = "/tmp/test_ADDHCmd_in";
std::string relative_path_str_incorrect = "./test_ADDHCmd_in";
std::string relative_path_str_non_existent = "./test_ADDHCmd_non";
bfs::path non_existent_path = top_level_dir / "non_existent_path";
// correct absolute path
EQ(true,
addhCmd_->isValidPath(absolute_path_str_correct, "test_ADDHCmd"));
addhCmd_->isValidPath(correct_prefixed_path.string(),
correct_prefix_dir.string()));
cout << endl << correct_prefix_dir.relative_path().string() << endl;
// correct relative path
EQ(true,
addhCmd_->isValidPath(relative_path_str_correct, "test_ADDHCmd"));
addhCmd_->isValidPath(correct_prefixed_path.relative_path().string(),
correct_prefix_dir.string()));
// incorrect absolute path
EQ(false,
addhCmd_->isValidPath(absolute_path_str_incorrect, "test_ADDHCmd"));
addhCmd_->isValidPath(incorrect_prefixed_path.string(),
correct_prefix_dir.string()));
// incorrect relative path
EQ(false,
addhCmd_->isValidPath(relative_path_str_incorrect, "test_ADDHCmd"));
addhCmd_->isValidPath(
incorrect_prefixed_path.relative_path().string(),
correct_prefix_dir.string()));
// non existent absolute path
EQ(false,
addhCmd_->isValidPath(relative_path_str_non_existent,
"test_ADDHCmd"));
addhCmd_->isValidPath(non_existent_path.string(),
correct_prefix_dir.string()));
// non existent relative path
EQ(false,
addhCmd_->isValidPath(non_existent_path.relative_path().string(),
correct_prefix_dir.string()));
delete addhCmd_;
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment