166 lines
5.6 KiB
C++
166 lines
5.6 KiB
C++
#include "gtest/gtest.h"
|
|
#include "test.h"
|
|
#include "horcrux.h"
|
|
|
|
using namespace horcrux;
|
|
|
|
TEST(IntegrationTest, EndToEndEncryptDecrypt){
|
|
auto buf = generic_read_file(image);
|
|
|
|
//Prepare encrypt operation
|
|
auto in = FsPlainInput(image);
|
|
auto cipher = AES256_CBC();
|
|
auto out = FsCryptoOutput(folder, 3);
|
|
|
|
// Perform encrypt operation
|
|
out.write(cipher.encrypt(in.read()));
|
|
|
|
//Prepare decrypt operation
|
|
auto in2 = FsCryptoInput(get_created_filenames(out));
|
|
//Perform decrypt operation
|
|
auto buf2 = cipher.decrypt(in2.read());
|
|
|
|
//check outcome
|
|
EXPECT_EQ(buf, buf2);
|
|
|
|
auto out2 = FsPlainOutput(noexist);
|
|
out2.write(cipher.decrypt(in2.read()));
|
|
|
|
auto buf3 = generic_read_file(noexist);
|
|
EXPECT_EQ(buf, buf3);
|
|
|
|
delete_created_files(out);
|
|
std::filesystem::remove(noexist);
|
|
}
|
|
|
|
TEST(IntegrationTest, HorcruxOptions){
|
|
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});
|
|
}
|
|
TEST(IntegrationTest, HorcruxEncryptDecrypt) {
|
|
{
|
|
HorcruxOptions options {
|
|
.mode = Mode::kEncrypt,
|
|
.count = 3,
|
|
.input = {image},
|
|
.output = {folder}
|
|
};
|
|
Horcrux enc{options};
|
|
enc.run();
|
|
auto key = enc.key();
|
|
|
|
HorcruxOptions options2 {
|
|
.mode = Mode::kDecrypt,
|
|
.count = 3,
|
|
.input = {get_encrypted_files(folder, image)},
|
|
.output = {noexist},
|
|
.base64_key = key
|
|
};
|
|
|
|
Horcrux dec{options2};
|
|
dec.run();
|
|
std::for_each(options2.input.begin(), options2.input.end(), [](auto& p){
|
|
std::filesystem::remove(p);});
|
|
} // Close scope so that Output objects get flushed
|
|
|
|
//Compare input and ouput files
|
|
EXPECT_EQ(std::filesystem::file_size(image), std::filesystem::file_size(noexist));
|
|
EXPECT_EQ(generic_read_file(image), generic_read_file(noexist));
|
|
std::filesystem::remove(noexist);
|
|
}
|
|
|
|
TEST(IntegrationTest, HorcruxEncryptDecryptBuffer) {
|
|
{
|
|
HorcruxOptions options {
|
|
.mode = Mode::kEncrypt,
|
|
.count = 3,
|
|
.input = {image},
|
|
.output = {folder}
|
|
};
|
|
Horcrux enc{options};
|
|
// read and process 1024 bytes at a time
|
|
enc.run(1024);
|
|
auto key = enc.key();
|
|
|
|
HorcruxOptions options2 {
|
|
.mode = Mode::kDecrypt,
|
|
.count = 3,
|
|
.input = {get_encrypted_files(folder, image)},
|
|
.output = {noexist},
|
|
.base64_key = key
|
|
};
|
|
|
|
Horcrux dec{options2};
|
|
// read and process 2048 bytes at a time
|
|
dec.run(2048);
|
|
std::for_each(options2.input.begin(), options2.input.end(), [](auto& p){
|
|
std::filesystem::remove(p);});
|
|
} // Close scope so that Output objects get flushed
|
|
|
|
//Compare input and ouput files
|
|
EXPECT_EQ(std::filesystem::file_size(image), std::filesystem::file_size(noexist));
|
|
EXPECT_EQ(generic_read_file(image), generic_read_file(noexist));
|
|
std::filesystem::remove(noexist);
|
|
}
|
|
|
|
TEST(IntegrationTest, arguments){
|
|
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"}));
|
|
}
|