Skip to content
Snippets Groups Projects
Commit 1ae29d27 authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

add common lib for tests, start read file tests

parent 153e00bc
No related branches found
No related tags found
No related merge requests found
Showing
with 173 additions and 51 deletions
......@@ -23,7 +23,7 @@ IOErrors IOErrorFromErrno();
class IO {
public:
virtual FileData GetDataFromFile(const std::string &fname, IOErrors* err) = 0;
virtual FileData GetDataFromFile(const std::string& fname, IOErrors* err) = 0;
virtual int open(const char* __file, int __oflag) = 0;
virtual int close(int __fd) = 0;
......
......@@ -7,7 +7,7 @@ namespace hidra2 {
class SystemIO final : public IO {
public:
FileData GetDataFromFile(const std::string &fname, IOErrors* err);
FileData GetDataFromFile(const std::string& fname, IOErrors* err);
int open(const char* __file, int __oflag);
int close(int __fd);
ssize_t read(int __fd, void* buf, size_t count);
......
......@@ -2,12 +2,17 @@
#include <unistd.h>
#include <system_wrappers/system_io.h>
hidra2::FileData hidra2::SystemIO::GetDataFromFile(const std::string &fname, IOErrors* err) {
hidra2::FileData hidra2::SystemIO::GetDataFromFile(const std::string& fname, IOErrors* err) {
int fd = open(fname.c_str(), O_RDONLY);
*err = IOErrorFromErrno();
if (*err != IOErrors::NO_ERROR) {
return {};
}
*err = IOErrors::READ_ERROR;
close(fd);
return {};
}
......
CMAKE_MINIMUM_REQUIRED(VERSION 3.7) # needed for fixtures
add_subdirectory(common/cpp)
add_subdirectory(system_io)
set(TARGET_NAME test_common)
set(SOURCE_FILES
src/testing.cpp)
################################
# Library
################################
add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
target_include_directories(${TARGET_NAME} PUBLIC include)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
#ifndef HIDRA2_TESTING_H
#define HIDRA2_TESTING_H
#include <string>
namespace hidra2 {
void M_AssertEq(const std::string& expected, const std::string& got);
}
#endif //HIDRA2_TESTING_H
#include "testing.h"
#include <iostream>
namespace hidra2 {
void M_AssertEq(const std::string& expected, const std::string& got) {
if (expected != got) {
std::cerr << "Assert failed:\n"
<< "Expected:\t'" << expected << "'\n"
<< "Obtained:\t'" << got << "'\n";
abort();
}
}
}
set(TARGET_NAME read_folder_content)
set(SOURCE_FILES read_folder_content.cpp)
CMAKE_MINIMUM_REQUIRED(VERSION 3.7) # needed for fixtures
################################
# Executable and link
################################
add_executable(${TARGET_NAME} ${SOURCE_FILES})
target_link_libraries(${TARGET_NAME} common)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
################################
# Testing
################################
add_test_setup_cleanup(${TARGET_NAME})
add_integration_test(${TARGET_NAME} create_list "test 23subtest/subtest241")
add_integration_test(${TARGET_NAME} foldernotfound "test_notexist notfound")
add_integration_test(${TARGET_NAME} foldernoaccess "test_noaccess noaccess")
add_subdirectory(read_files_in_folder)
add_subdirectory(read_file_content)
set(TARGET_NAME read_file_content)
set(SOURCE_FILES read_file_content.cpp)
################################
# Executable and link
################################
add_executable(${TARGET_NAME} ${SOURCE_FILES})
target_link_libraries(${TARGET_NAME} test_common common )
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
################################
# Testing
################################
add_test_setup_cleanup(${TARGET_NAME})
add_integration_test(${TARGET_NAME} readfile "test/1 readerror")
add_integration_test(${TARGET_NAME} filenotfound "test_notexist notfound")
add_integration_test(${TARGET_NAME} filenoaccess "file_noaccess noaccess")
#!/usr/bin/env bash
rm -rf test
rm -f file_noaccess
#include <iostream>
#include <system_wrappers/system_io.h>
#include "testing.h"
using hidra2::SystemIO;
using hidra2::IOErrors;
using hidra2::M_AssertEq;
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "Wrong number of arguments" << std::endl;
return 1;
}
std::string expect{argv[2]};
IOErrors err;
auto io = std::unique_ptr<SystemIO> {new SystemIO};
auto data = io->GetDataFromFile(argv[1], &err);
std::string result;
switch (err) {
case IOErrors::FILE_NOT_FOUND:
result = "notfound";
break;
case IOErrors::NO_ERROR:
for(auto symbol : data)
result += symbol;
break;
case IOErrors::PERMISSIONS_DENIED:
result = "noaccess";
break;
case IOErrors::READ_ERROR:
result = "readerror";
break;
default:
result = "";
break;
}
M_AssertEq(expect, result);
return 0;
}
#!/usr/bin/env bash
mkdir -p test
echo 123 > test/1
touch file_noaccess
chmod -rx file_noaccess
set(TARGET_NAME read_folder_content)
set(SOURCE_FILES read_folder_content.cpp)
################################
# Executable and link
################################
add_executable(${TARGET_NAME} ${SOURCE_FILES})
target_link_libraries(${TARGET_NAME} test_common common)
#target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/tests/common/cpp/include)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
################################
# Testing
################################
add_test_setup_cleanup(${TARGET_NAME})
add_integration_test(${TARGET_NAME} create_list "test 23subtest/subtest241")
add_integration_test(${TARGET_NAME} foldernotfound "test_notexist notfound")
add_integration_test(${TARGET_NAME} foldernoaccess "test_noaccess noaccess")
#include <iostream>
#include <memory>
#include <system_wrappers/system_io.h>
#include "system_wrappers/system_io.h"
#include "testing.h"
using hidra2::SystemIO;
using hidra2::IOErrors;
void M_AssertEq(const std::string& expected, const std::string& got) {
if (expected != got) {
std::cerr << "Assert failed:\n"
<< "Expected:\t'" << expected << "'\n"
<< "Obtained:\t'" << got << "'\n";
abort();
}
}
using hidra2::M_AssertEq;
int main(int argc, char* argv[]) {
if (argc != 3) {
......
......@@ -7,18 +7,18 @@ namespace hidra2 {
WorkerErrorCode MapIOError(IOErrors io_err) {
WorkerErrorCode err;
switch (io_err) { // we do not use map due to performance reasons
case IOErrors::NO_ERROR:
err = WorkerErrorCode::OK;
break;
case IOErrors::FILE_NOT_FOUND:
err = WorkerErrorCode::SOURCE_NOT_FOUND;
break;
case IOErrors::PERMISSIONS_DENIED:
err = WorkerErrorCode::PERMISSIONS_DENIED;
break;
case IOErrors::READ_ERROR:
err = WorkerErrorCode::ERROR_READING_FROM_SOURCE;
break;
case IOErrors::NO_ERROR:
err = WorkerErrorCode::OK;
break;
case IOErrors::FILE_NOT_FOUND:
err = WorkerErrorCode::SOURCE_NOT_FOUND;
break;
case IOErrors::PERMISSIONS_DENIED:
err = WorkerErrorCode::PERMISSIONS_DENIED;
break;
case IOErrors::READ_ERROR:
err = WorkerErrorCode::ERROR_READING_FROM_SOURCE;
break;
default:
err = WorkerErrorCode::UNKNOWN_IO_ERROR;
break;
......@@ -78,8 +78,8 @@ WorkerErrorCode FolderDataBroker::GetNext(FileInfo* info, FileData* data) {
IOErrors ioerr;
*data = io__->GetDataFromFile(base_path_ + "/" + info->relative_path +
(info->relative_path.empty() ? "" : "/") +
info->base_name, &ioerr);
(info->relative_path.empty() ? "" : "/") +
info->base_name, &ioerr);
return MapIOError(ioerr);
}
......
......@@ -36,7 +36,7 @@ TEST(FolderDataBroker, SetCorrectIO) {
class FakeIO: public IO {
public:
FileData GetDataFromFile(const std::string &fname, IOErrors* err) {
FileData GetDataFromFile(const std::string& fname, IOErrors* err) {
*err = IOErrors::NO_ERROR;
return {};
};
......@@ -95,7 +95,7 @@ class IOEmptyFodler: public FakeIO {
class IOCannotOpenFile: public FakeIO {
public:
FileData GetDataFromFile(const std::string &fname, IOErrors* err) {
FileData GetDataFromFile(const std::string& fname, IOErrors* err) {
*err = IOErrors::PERMISSIONS_DENIED;
return {};
};
......@@ -229,11 +229,11 @@ TEST_F(FolderDataBrokerTests, GetNextReturnsData) {
FileData data;
EXPECT_CALL(mock, GetDataFromFile(_, _)).
WillOnce(DoAll(testing::SetArgPointee<1>(IOErrors::NO_ERROR), testing::Return(FileData{'1'})));
WillOnce(DoAll(testing::SetArgPointee<1>(IOErrors::NO_ERROR), testing::Return(FileData{'1'})));
data_broker->GetNext(&fi, &data);
data_broker->io__.release();
ASSERT_THAT(data[0],Eq('1'));
ASSERT_THAT(data[0], Eq('1'));
}
......@@ -245,11 +245,11 @@ TEST_F(FolderDataBrokerTests, GetNextReturnsErrorWhenCannotReadData) {
FileData data;
EXPECT_CALL(mock, GetDataFromFile(_, _)).
WillOnce(DoAll(testing::SetArgPointee<1>(IOErrors::READ_ERROR), testing::Return(FileData{})));
WillOnce(DoAll(testing::SetArgPointee<1>(IOErrors::READ_ERROR), testing::Return(FileData{})));
auto err = data_broker->GetNext(&fi, &data);
data_broker->io__.release();
ASSERT_THAT(err,Eq(WorkerErrorCode::ERROR_READING_FROM_SOURCE));
ASSERT_THAT(err, Eq(WorkerErrorCode::ERROR_READING_FROM_SOURCE));
ASSERT_TRUE(data.empty());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment