Add IO tests

This commit is contained in:
Michele Rodolfi 2020-12-12 15:00:13 +01:00
parent f7801f40ef
commit bb9c27aba3

View File

@ -20,6 +20,17 @@ static std::vector<unsigned char> generic_read_file(const std::string& filename)
return buf;
}
static std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out){
std::vector<std::string> result(out.created_files.size());
transform(out.created_files.begin(), out.created_files.end(), result.begin(),
[](auto path){return path.string();});
return result;
}
static void delete_created_files(const horcrux::FsCryptoOutput& out){
std::for_each(out.created_files.begin(), out.created_files.end(),
[](auto f){std::filesystem::remove(f);});
}
TEST(IoTests, FsPlainInput) {
EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument);
EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument);
@ -58,8 +69,7 @@ TEST(IoTests, FsCryptoOutputWriteImage){
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);});
delete_created_files(out);
}
TEST(IoTests, FsCryptoOutputWriteText){
@ -70,14 +80,13 @@ TEST(IoTests, FsCryptoOutputWriteText){
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);});
delete_created_files(out);
}
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(std::vector<std::string>{}), 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}));
@ -91,8 +100,7 @@ TEST(IoTest, FsCryptoInputImage){
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 files = get_created_filenames(out);
auto in = horcrux::FsCryptoInput(files);
auto read = in.read();
EXPECT_EQ(read, buf);
@ -102,8 +110,7 @@ TEST(IoTest, FsCryptoInputImage){
read = in.read();
EXPECT_NE(read, buf);
for_each(out.created_files.begin(), out.created_files.end(),
[](auto f){std::filesystem::remove(f);});
delete_created_files(out);
}
TEST(IoTest, FsPlainOuput){
@ -124,3 +131,74 @@ TEST(IoTest, FsPlainOutputWrite){
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
}
TEST(IoTest, PlainToPlain){
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsPlainOutput(noexist);
//Execute Plain To Plain
out.write(in.read());
//Check outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
}
TEST(IoTest, PlainToCrypto){
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsCryptoOutput(folder, 5);
//Execute Plain to Crypto
out.write(in.read());
//use FsCryptoInput to re-join the files and check the outcome
auto files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
delete_created_files(out);
}
TEST(IoTest, CryptoToCrypto){
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsCryptoOutput(folder, 5, "test");
//Execute Crypto to Crypto
out.write(in.read());
//use FsCryptoInput to re-join the files and check the outcome
files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
delete_created_files(out_test);
delete_created_files(out);
}
TEST(IoTest, CryptoToPlain){
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsPlainOutput(noexist);
//Execute Crypto to Plain
out.write(in.read());
//check the outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
}