Commit fae0e1ac authored by Maria Fava's avatar Maria Fava
Browse files

added ADDHCmd test for path validation

parent 4b71da94
......@@ -92,10 +92,14 @@ bool ADDHCmd::isValidPath(std::string filePath, std::string filePathPrefix)
{
// filePath is relative, convert to absolute
char *absoluteFilePath = realpath(filePath.c_str(), NULL);
if(absoluteFilePath == NULL)
{
return false;
}
filePath = absoluteFilePath;
free(absoluteFilePath);
}
if (filePath.find(filePathPrefix) != std::string::npos)
if (filePath.find(filePathPrefix + "/") != std::string::npos)
{
return true;
}
......
......@@ -33,8 +33,6 @@ namespace addh {
*/
class ADDHCmd : public ntof::dim::DIMSuperCommand
{
private:
bool isValidPath(std::string filePath, std::string FilePathPrefix);
public:
/*!
* \brief Constructor of the class ADDHCmd
......@@ -46,6 +44,8 @@ public:
*/
~ADDHCmd();
bool isValidPath(std::string filePath, std::string FilePathPrefix);
/*!
* \brief Receive all the commands send to the DIM Super Command. This
* method is executed each time a command is received
......
......@@ -17,6 +17,7 @@ set(test_SRCS test_main.cpp test_init.cpp
test_helpers_dim.hpp test_helpers_dim.cpp
test_WriterFile.cpp
test_ADDH.cpp
test_ADDHCmd.cpp
)
add_executable(test_all ${test_SRCS})
......
#include <fstream>
#include <memory>
#include <boost/filesystem.hpp>
#include <DIMDataSet.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include "ADDH.h"
#include "Config.h"
#include "ADDHCmd.h"
#include "local-config.h"
#include "test_helpers.hpp"
namespace bfs = boost::filesystem;
using namespace ntof::addh;
class TestADDHCmdFile : public CppUnit::TestFixture
{
protected:
CPPUNIT_TEST_SUITE(TestADDHCmdFile);
CPPUNIT_TEST(validatePath);
CPPUNIT_TEST_SUITE_END();
bfs::path abs_tmp,abs_tmp_in, rel_tmp, rel_tmp_in;
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);
}
void tearDown()
{
bfs::remove_all(abs_tmp);
bfs::remove_all(rel_tmp);
bfs::remove_all(rel_tmp_in);
bfs::remove_all(rel_tmp_in);
}
void validatePath()
{
ADDHCmd *addhCmd_ = new ADDHCmd();
// create a filepath to be used as filepathPrefix
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);
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";
EQ(true, addhCmd_->isValidPath(absolute_path_str_correct, "test_ADDHCmd"));
EQ(true, addhCmd_->isValidPath(relative_path_str_correct, "test_ADDHCmd"));
EQ(false, addhCmd_->isValidPath(absolute_path_str_incorrect, "test_ADDHCmd"));
EQ(false, addhCmd_->isValidPath(relative_path_str_incorrect, "test_ADDHCmd"));
EQ(false, addhCmd_->isValidPath(relative_path_str_non_existent, "test_ADDHCmd"));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestADDHCmdFile);
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