#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, HorcruxEncryptiDecrypt) { HorcruxOptions options { .mode = Mode::kEncrypt, .count = 3, .input = {image}, .output = {folder} }; Horcrux enc{options}; enc.execute(); 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.execute(); //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);}); } 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"})); }