horcrux/test/io-test.cpp

177 lines
5.4 KiB
C++
Raw Normal View History

2020-12-12 00:44:49 +00:00
#include "gtest/gtest.h"
2020-12-12 14:36:38 +00:00
#include "io.h"
#include "test.h"
2020-12-12 14:00:13 +00:00
2020-12-12 00:44:49 +00:00
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());
2020-12-12 14:00:13 +00:00
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
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());
2020-12-12 14:00:13 +00:00
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
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);
2020-12-12 14:00:13 +00:00
EXPECT_THROW(horcrux::FsCryptoInput output(std::vector<std::string>{}), std::invalid_argument);
2020-12-12 00:44:49 +00:00
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());
2020-12-12 14:00:13 +00:00
auto files = get_created_filenames(out);
2020-12-12 00:44:49 +00:00
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);
2020-12-12 14:00:13 +00:00
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
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);
}
2020-12-12 14:00:13 +00:00
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);
2020-12-12 21:19:10 +00:00
delete_created_files(out_test);
2020-12-12 14:00:13 +00:00
std::filesystem::remove(noexist);
}