horcrux/test/io-test.cpp

177 lines
5.8 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) {
2020-12-12 23:56:18 +00:00
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});
2020-12-12 00:44:49 +00:00
}
TEST(IoTests, FsPlainInputReadText) {
2020-12-12 23:56:18 +00:00
auto input = horcrux::FsPlainInput{text};
auto buf = generic_read_file(text);
EXPECT_EQ(buf, input.read());
2020-12-12 00:44:49 +00:00
}
TEST(IoTests, FsPlainInputReadImg) {
2020-12-12 23:56:18 +00:00
auto input = horcrux::FsPlainInput{image};
auto buf = generic_read_file(image);
EXPECT_EQ(buf, input.read());
2020-12-12 00:44:49 +00:00
}
TEST(IoTests, FsCryptoOutput) {
2020-12-12 23:56:18 +00:00
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);
2020-12-12 00:44:49 +00:00
}
TEST(IoTests, FsCryptoOutputWriteImage){
2020-12-12 23:56:18 +00:00
auto ifstream = std::ifstream{image, std::ios::binary};
auto buf = generic_read_file(image);
2020-12-12 00:44:49 +00:00
2020-12-12 23:56:18 +00:00
horcrux::FsCryptoOutput out(folder, 10);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
2020-12-12 00:44:49 +00:00
2020-12-12 23:56:18 +00:00
EXPECT_EQ(10, out.created_files.size());
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
TEST(IoTests, FsCryptoOutputWriteText){
2020-12-12 23:56:18 +00:00
auto buf = generic_read_file(text);
2020-12-12 00:44:49 +00:00
2020-12-12 23:56:18 +00:00
horcrux::FsCryptoOutput out(folder, 2);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
2020-12-12 00:44:49 +00:00
2020-12-12 23:56:18 +00:00
EXPECT_EQ(2, out.created_files.size());
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
TEST(IoTest, FsCryptoInput){
2020-12-12 23:56:18 +00:00
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::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}));
EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text}));
2020-12-12 00:44:49 +00:00
}
TEST(IoTest, FsCryptoInputImage){
2020-12-12 23:56:18 +00:00
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());
auto files = get_created_filenames(out);
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);
delete_created_files(out);
2020-12-12 00:44:49 +00:00
}
TEST(IoTest, FsPlainOuput){
2020-12-12 23:56:18 +00:00
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});
2020-12-12 00:44:49 +00:00
}
TEST(IoTest, FsPlainOutputWrite){
2020-12-12 23:56:18 +00:00
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 00:44:49 +00:00
}
2020-12-12 14:00:13 +00:00
TEST(IoTest, PlainToPlain){
2020-12-12 23:56:18 +00:00
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsPlainOutput(noexist);
2020-12-12 14:00:13 +00:00
2020-12-12 23:56:18 +00:00
//Execute Plain To Plain
out.write(in.read());
2020-12-12 14:00:13 +00:00
2020-12-12 23:56:18 +00:00
//Check outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
2020-12-12 14:00:13 +00:00
}
TEST(IoTest, PlainToCrypto){
2020-12-12 23:56:18 +00:00
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);
2020-12-12 14:00:13 +00:00
}
TEST(IoTest, CryptoToCrypto){
2020-12-12 23:56:18 +00:00
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);
2020-12-12 14:00:13 +00:00
}
TEST(IoTest, CryptoToPlain){
2020-12-12 23:56:18 +00:00
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);
delete_created_files(out_test);
std::filesystem::remove(noexist);
2020-12-12 14:00:13 +00:00
}