127 lines
4.4 KiB
C++
127 lines
4.4 KiB
C++
|
#include "gtest/gtest.h"
|
||
|
#include "io.cpp"
|
||
|
|
||
|
#ifndef TEST_WORK_DIR
|
||
|
#error Please define TEST_WORK_DIR
|
||
|
#endif
|
||
|
|
||
|
const std::string folder{TEST_WORK_DIR};
|
||
|
const std::string noexist{TEST_WORK_DIR "/nope"};
|
||
|
const std::string empty{TEST_WORK_DIR "/empty"};
|
||
|
const std::string text{TEST_WORK_DIR "/test.txt"};
|
||
|
const std::string image{TEST_WORK_DIR "/mangoni.jpg"};
|
||
|
|
||
|
static std::vector<unsigned char> generic_read_file(const std::string& filename){
|
||
|
auto ifstream = std::ifstream{filename, std::ios::binary};
|
||
|
auto path = std::filesystem::path{filename};
|
||
|
auto size = std::filesystem::file_size(path);
|
||
|
std::vector<unsigned char> buf(size);
|
||
|
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsPlainInput) {
|
||
|
EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsPlainInput input{empty}, std::invalid_argument);
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainInput input{text});
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainInput input{image});
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsPlainInputReadText) {
|
||
|
auto input = horcrux::FsPlainInput{text};
|
||
|
auto buf = generic_read_file(text);
|
||
|
EXPECT_EQ(buf, input.read());
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsPlainInputReadImg) {
|
||
|
auto input = horcrux::FsPlainInput{image};
|
||
|
auto buf = generic_read_file(image);
|
||
|
EXPECT_EQ(buf, input.read());
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsCryptoOutput) {
|
||
|
EXPECT_THROW(horcrux::FsCryptoOutput output(noexist, 1), std::invalid_argument);
|
||
|
EXPECT_NO_THROW(horcrux::FsCryptoOutput output(folder, 1));
|
||
|
EXPECT_THROW(horcrux::FsCryptoOutput output(folder, 0), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoOutput output(empty, 1), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoOutput output(text, 1), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoOutput output(image, 1), std::invalid_argument);
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsCryptoOutputWriteImage){
|
||
|
auto ifstream = std::ifstream{image, std::ios::binary};
|
||
|
auto buf = generic_read_file(image);
|
||
|
|
||
|
horcrux::FsCryptoOutput out(folder, 10);
|
||
|
auto written = out.write(buf);
|
||
|
EXPECT_EQ(written, buf.size());
|
||
|
|
||
|
EXPECT_EQ(10, out.created_files.size());
|
||
|
for_each(out.created_files.begin(), out.created_files.end(),
|
||
|
[](auto f){std::filesystem::remove(f);});
|
||
|
}
|
||
|
|
||
|
TEST(IoTests, FsCryptoOutputWriteText){
|
||
|
auto buf = generic_read_file(text);
|
||
|
|
||
|
horcrux::FsCryptoOutput out(folder, 2);
|
||
|
auto written = out.write(buf);
|
||
|
EXPECT_EQ(written, buf.size());
|
||
|
|
||
|
EXPECT_EQ(2, out.created_files.size());
|
||
|
for_each(out.created_files.begin(), out.created_files.end(),
|
||
|
[](auto f){std::filesystem::remove(f);});
|
||
|
}
|
||
|
|
||
|
TEST(IoTest, FsCryptoInput){
|
||
|
EXPECT_THROW(horcrux::FsCryptoInput output({noexist, text, image}), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoInput output({folder, text, image}), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoInput output({}), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoInput output({empty}), std::invalid_argument);
|
||
|
EXPECT_THROW(horcrux::FsCryptoInput output({empty, empty}), std::invalid_argument);
|
||
|
EXPECT_NO_THROW(horcrux::FsCryptoInput output({empty, empty, text}));
|
||
|
EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text}));
|
||
|
}
|
||
|
|
||
|
TEST(IoTest, FsCryptoInputImage){
|
||
|
auto buf = generic_read_file(image);
|
||
|
//split image using FsCryptoOutput
|
||
|
auto out = horcrux::FsCryptoOutput(folder, 7);
|
||
|
auto written = out.write(buf);
|
||
|
EXPECT_EQ(written, buf.size());
|
||
|
|
||
|
std::vector<std::string> files;
|
||
|
transform(out.created_files.begin(), out.created_files.end(),std::back_inserter(files), [](auto p){return p.string();});
|
||
|
auto in = horcrux::FsCryptoInput(files);
|
||
|
auto read = in.read();
|
||
|
EXPECT_EQ(read, buf);
|
||
|
|
||
|
files.push_back(text);
|
||
|
in = horcrux::FsCryptoInput(files);
|
||
|
read = in.read();
|
||
|
EXPECT_NE(read, buf);
|
||
|
|
||
|
for_each(out.created_files.begin(), out.created_files.end(),
|
||
|
[](auto f){std::filesystem::remove(f);});
|
||
|
}
|
||
|
|
||
|
TEST(IoTest, FsPlainOuput){
|
||
|
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainOutput output{noexist});
|
||
|
EXPECT_THROW(horcrux::FsPlainOutput output{folder}, std::invalid_argument);
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainOutput output{empty});
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainOutput output{text});
|
||
|
EXPECT_NO_THROW(horcrux::FsPlainOutput output{image});
|
||
|
}
|
||
|
|
||
|
TEST(IoTest, FsPlainOutputWrite){
|
||
|
auto buf = generic_read_file(image);
|
||
|
auto out = horcrux::FsPlainOutput(noexist);
|
||
|
auto written = out.write(buf);
|
||
|
EXPECT_EQ(written, buf.size());
|
||
|
auto buf2 = generic_read_file(noexist);
|
||
|
EXPECT_EQ(buf, buf2);
|
||
|
std::filesystem::remove(noexist);
|
||
|
}
|