From 1fb526e711c47014e65c635950f2acf1c6878a6c Mon Sep 17 00:00:00 2001 From: Michele Rodolfi Date: Sat, 12 Dec 2020 15:36:38 +0100 Subject: [PATCH] Add integration tests --- src/crypto.cpp | 3 +++ src/crypto.h | 5 ++++ src/io.cpp | 4 +-- src/io.h | 39 ++++++++++++++++++++------- src/utils.h | 3 +++ test/CMakeLists.txt | 8 +++++- test/crypto-test.cpp | 18 ++----------- test/integration-test.cpp | 32 ++++++++++++++++++++++ test/io-test.cpp | 33 ++--------------------- test/test.h | 57 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 143 insertions(+), 59 deletions(-) create mode 100644 test/integration-test.cpp create mode 100644 test/test.h diff --git a/src/crypto.cpp b/src/crypto.cpp index 13da463..6d5798b 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -12,6 +12,8 @@ #include "crypto.h" #include "utils.h" +namespace horcrux { + class EvpCipherCtx { EVP_CIPHER_CTX *ptr; @@ -193,3 +195,4 @@ std::vector AES256_CBC::decrypt( process_all(Cipher::Mode::kDecrypt, ctx, begin, end, output, 0); return output; } +}; //namespace diff --git a/src/crypto.h b/src/crypto.h index cfeef66..3bd98f9 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -1,7 +1,10 @@ +#ifndef HORCRUX_SRC_CRYPTO_H +#define HORCRUX_SRC_CRYPTO_H #include #include #include +namespace horcrux { enum class CipherType {AES256_CBC}; class Cipher { @@ -108,3 +111,5 @@ static std::unique_ptr createCipher(CipherType type){ } } +}; +#endif //HORCRUX_SRC_CRYPTO_H diff --git a/src/io.cpp b/src/io.cpp index 2792955..8d6a625 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -64,8 +64,8 @@ size_t FsPlainOutput::write(const std::vector& to_write){ return to_write.size(); } -FsCryptoOutput::FsCryptoOutput(const std::string& folder, const int horcrux_num, const size_t horcrux_size = 0, - const std::string& filename = "horcrux") +FsCryptoOutput::FsCryptoOutput(const std::string& folder, const int horcrux_num, + const std::string& filename, const size_t horcrux_size) : folder_path(folder), base_name(filename), num(horcrux_num), size(horcrux_size) { diff --git a/src/io.h b/src/io.h index e92ea35..404e61d 100644 --- a/src/io.h +++ b/src/io.h @@ -1,3 +1,5 @@ +#ifndef HORCRUX_SRC_IO_H +#define HORCRUX_SRC_IO_H #include #include #include @@ -5,43 +7,62 @@ namespace horcrux { -class FsPlainInput { +class Input { +public: + virtual std::vector read() = 0; +}; +class Output { +public: + virtual size_t write(const std::vector& data) = 0; +}; +class PlainInput : public Input {}; +class CryptoInput : public Input {}; +class PlainOutput : public Output {}; +class CryptoOutput : public Output {}; + +class FsPlainInput : public Input { std::filesystem::path file_path; size_t file_size; std::ifstream file_stream; + FsPlainInput() = delete; public: FsPlainInput(const std::string& path); - std::vector read(); + std::vector read() override; }; -class FsCryptoInput { +class FsCryptoInput : public Input{ std::vector> file_paths; size_t total_size{0}; + FsCryptoInput() = delete; public: FsCryptoInput(const std::vector& filenames); - std::vector read(); + std::vector read() override; }; -class FsPlainOutput { +class FsPlainOutput : public Output { std::filesystem::path file_path; + FsPlainOutput() = delete; public: FsPlainOutput(const std::string& filename); - size_t write(const std::vector& to_write); + size_t write(const std::vector& to_write) override; }; -class FsCryptoOutput { +class FsCryptoOutput : public Output { std::filesystem::path folder_path; std::string base_name; int num; size_t size; + FsCryptoOutput() = delete; public: std::vector created_files; - FsCryptoOutput(const std::string& folder, const int horcrux_num, const size_t horcrux_size, const std::string& filename); - size_t write(const std::vector& to_write); + FsCryptoOutput(const std::string& folder, const int horcrux_num, + const std::string& filename = "horcrux", const size_t horcrux_size = 0); + size_t write(const std::vector& to_write) override; }; }; +#endif //HORCRUX_SRC_IO_H diff --git a/src/utils.h b/src/utils.h index 17abed2..f2d8868 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,3 +1,5 @@ +#ifndef HORCRUX_SRC_UTILS_H +#define HORCRUX_SRC_UTILS_H #include #include @@ -8,3 +10,4 @@ std::vector from_base64(const std::string& base64); std::vector generate_random(const size_t buf_len); } +#endif //HORCRUX_SRC_UTILS_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b954fe4..104ae90 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,12 @@ set(BINARY ${CMAKE_PROJECT_NAME}_test) -add_executable(${BINARY} main.cpp crypto-test.cpp io-test.cpp utils-test.cpp) +add_executable(${BINARY} + main.cpp + crypto-test.cpp + io-test.cpp + utils-test.cpp + integration-test.cpp +) add_test(NAME test COMMAND ${BINARY}) diff --git a/test/crypto-test.cpp b/test/crypto-test.cpp index b542706..973888d 100644 --- a/test/crypto-test.cpp +++ b/test/crypto-test.cpp @@ -2,23 +2,9 @@ #include "gtest/gtest.h" #include "utils.h" #include "crypto.h" +#include "test.h" -/* test command: -echo "ditemi perche' se la mucca fa mu il merlo non fa me" | - scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF | - xxd -i -*/ -const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me"; -const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF"; -const std::string test1_iv = "0123456789ABCDEF"; -const std::vector test1_enc { - 0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f, - 0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d, - 0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81, - 0x12, 0x96, 0x50, 0xd6, 0x5c, 0xe2, 0xc1, 0x99, 0xe3, 0x38, 0x39, 0x8e, - 0x55, 0xd2, 0x04, 0x73, 0x16, 0x39, 0xc7, 0x6a, 0xd3, 0x61, 0x2c, 0x22, - 0x59, 0x25, 0xa6, 0x20 }; - +using namespace horcrux; TEST(CryptoTest, encrypt1){ const std::vector plaintext(test1_str.begin(), test1_str.end()); size_t output_len = plaintext.size() + 16; diff --git a/test/integration-test.cpp b/test/integration-test.cpp new file mode 100644 index 0000000..963ae73 --- /dev/null +++ b/test/integration-test.cpp @@ -0,0 +1,32 @@ +#include "gtest/gtest.h" +#include "test.h" + + +TEST(IntegrationTest, EndToEndEncryptDecrypt){ + auto buf = generic_read_file(image); + + //Prepare encrypt operation + auto in = horcrux::FsPlainInput(image); + auto cipher = horcrux::AES256_CBC(); + auto out = horcrux::FsCryptoOutput(folder, 3); + + // Perform encrypt operation + out.write(cipher.encrypt(in.read())); + + //Prepare decrypt operation + auto in2 = horcrux::FsCryptoInput(get_created_filenames(out)); + //Perform decrypt operation + auto buf2 = cipher.decrypt(in2.read()); + + //check outcome + EXPECT_EQ(buf, buf2); + + auto out2 = horcrux::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); +} diff --git a/test/io-test.cpp b/test/io-test.cpp index d5ed79e..9c3a0d1 100644 --- a/test/io-test.cpp +++ b/test/io-test.cpp @@ -1,35 +1,6 @@ #include "gtest/gtest.h" -#include "io.cpp" - -#ifndef TEST_WORK_DIR -#error Please define TEST_WORK_DIR -#endif - -const std::string folder{TEST_WORK_DIR}; -const std::string noexist{TEST_WORK_DIR "/nope"}; -const std::string empty{TEST_WORK_DIR "/empty"}; -const std::string text{TEST_WORK_DIR "/test.txt"}; -const std::string image{TEST_WORK_DIR "/mangoni.jpg"}; - -static std::vector generic_read_file(const std::string& filename){ - auto ifstream = std::ifstream{filename, std::ios::binary}; - auto path = std::filesystem::path{filename}; - auto size = std::filesystem::file_size(path); - std::vector buf(size); - ifstream.read(reinterpret_cast(buf.data()), size); - return buf; -} - -static std::vector get_created_filenames(const horcrux::FsCryptoOutput& out){ - std::vector result(out.created_files.size()); - transform(out.created_files.begin(), out.created_files.end(), result.begin(), - [](auto path){return path.string();}); - return result; -} -static void delete_created_files(const horcrux::FsCryptoOutput& out){ - std::for_each(out.created_files.begin(), out.created_files.end(), - [](auto f){std::filesystem::remove(f);}); -} +#include "io.h" +#include "test.h" TEST(IoTests, FsPlainInput) { EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument); diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..ae17385 --- /dev/null +++ b/test/test.h @@ -0,0 +1,57 @@ +#ifndef HORCRUX_TEST_TEST_H +#define HORCRUX_TEST_TEST_H +#include +#include "io.h" +#include "crypto.h" + +/* IO Test utils */ +#ifndef TEST_WORK_DIR +#error Please define TEST_WORK_DIR +#endif + +const std::string folder{TEST_WORK_DIR}; +const std::string noexist{TEST_WORK_DIR "/nope"}; +const std::string empty{TEST_WORK_DIR "/empty"}; +const std::string text{TEST_WORK_DIR "/test.txt"}; +const std::string image{TEST_WORK_DIR "/mangoni.jpg"}; + +static std::vector generic_read_file(const std::string& filename){ + auto ifstream = std::ifstream{filename, std::ios::binary}; + auto path = std::filesystem::path{filename}; + auto size = std::filesystem::file_size(path); + std::vector buf(size); + ifstream.read(reinterpret_cast(buf.data()), size); + return buf; +} + +static std::vector get_created_filenames(const horcrux::FsCryptoOutput& out){ + std::vector result(out.created_files.size()); + transform(out.created_files.begin(), out.created_files.end(), result.begin(), + [](auto path){return path.string();}); + return result; +} +static void delete_created_files(const horcrux::FsCryptoOutput& out){ + std::for_each(out.created_files.begin(), out.created_files.end(), + [](auto f){std::filesystem::remove(f);}); +} + +/* Crypto Test utils */ + +/* test command: +echo "ditemi perche' se la mucca fa mu il merlo non fa me" | + scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF | + xxd -i +*/ +const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me"; +const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF"; +const std::string test1_iv = "0123456789ABCDEF"; +const std::vector test1_enc { + 0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f, + 0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d, + 0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81, + 0x12, 0x96, 0x50, 0xd6, 0x5c, 0xe2, 0xc1, 0x99, 0xe3, 0x38, 0x39, 0x8e, + 0x55, 0xd2, 0x04, 0x73, 0x16, 0x39, 0xc7, 0x6a, 0xd3, 0x61, 0x2c, 0x22, + 0x59, 0x25, 0xa6, 0x20 }; + + +#endif //HORCRUX_TEST_TEST_H