horcrux/test/integration-test.cpp

129 lines
4.3 KiB
C++
Raw Normal View History

2020-12-12 14:36:38 +00:00
#include "gtest/gtest.h"
#include "test.h"
2020-12-12 21:19:10 +00:00
#include "horcrux.h"
2020-12-12 14:36:38 +00:00
2020-12-12 21:19:10 +00:00
using namespace horcrux;
2020-12-12 14:36:38 +00:00
TEST(IntegrationTest, EndToEndEncryptDecrypt){
2020-12-12 23:56:18 +00:00
auto buf = generic_read_file(image);
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
//Prepare encrypt operation
auto in = FsPlainInput(image);
auto cipher = AES256_CBC();
auto out = FsCryptoOutput(folder, 3);
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
// Perform encrypt operation
out.write(cipher.encrypt(in.read()));
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
//Prepare decrypt operation
auto in2 = FsCryptoInput(get_created_filenames(out));
//Perform decrypt operation
auto buf2 = cipher.decrypt(in2.read());
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
//check outcome
EXPECT_EQ(buf, buf2);
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
auto out2 = FsPlainOutput(noexist);
out2.write(cipher.decrypt(in2.read()));
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
auto buf3 = generic_read_file(noexist);
EXPECT_EQ(buf, buf3);
2020-12-12 14:36:38 +00:00
2020-12-12 23:56:18 +00:00
delete_created_files(out);
std::filesystem::remove(noexist);
2020-12-12 14:36:38 +00:00
}
2020-12-12 21:19:10 +00:00
TEST(IntegrationTest, HorcruxOptions){
2020-12-12 23:56:18 +00:00
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = -1,
.input = {noexist},
.output = {image}
};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {folder};
options.count = 0;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_NO_THROW(Horcrux {options});
options.mode = Mode::kDecrypt;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image, text};
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {noexist};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.base64_key = to_base64(generate_random(32));
EXPECT_NO_THROW(Horcrux {options});
2020-12-12 21:19:10 +00:00
}
TEST(IntegrationTest, HorcruxEncryptiDecrypt) {
2020-12-12 23:56:18 +00:00
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = 3,
.input = {image},
.output = {folder}
};
Horcrux enc{options};
2020-12-12 21:19:10 +00:00
2020-12-13 22:14:30 +00:00
enc.run();
2020-12-12 23:56:18 +00:00
auto key = enc.key();
2020-12-12 21:19:10 +00:00
2020-12-12 23:56:18 +00:00
HorcruxOptions options2 {
.mode = Mode::kDecrypt,
.count = 3,
.input = {get_encrypted_files(folder, image)},
.output = {noexist},
.base64_key = key
};
2020-12-12 21:19:10 +00:00
2020-12-12 23:56:18 +00:00
Horcrux dec{options2};
2020-12-13 22:14:30 +00:00
dec.run();
2020-12-12 21:19:10 +00:00
2020-12-12 23:56:18 +00:00
//Compare input and ouput files
EXPECT_EQ(generic_read_file(image), generic_read_file(noexist));
std::filesystem::remove(noexist);
std::for_each(options2.input.begin(), options2.input.end(), [](auto& p){
std::filesystem::remove(p);});
2020-12-12 21:19:10 +00:00
}
TEST(IntegrationTest, arguments){
2020-12-12 23:56:18 +00:00
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "pippo", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file", "garbage"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-h", "4", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "boh", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file"}));
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-r", "lshgflushlgkjusghl", "input", "input", "output"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "output"}));
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "input", "output"}));
2020-12-12 21:19:10 +00:00
}