Add integration tests
This commit is contained in:
parent
bb9c27aba3
commit
1fb526e711
@ -12,6 +12,8 @@
|
||||
#include "crypto.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace horcrux {
|
||||
|
||||
class EvpCipherCtx {
|
||||
EVP_CIPHER_CTX *ptr;
|
||||
|
||||
@ -193,3 +195,4 @@ std::vector<unsigned char> AES256_CBC::decrypt(
|
||||
process_all(Cipher::Mode::kDecrypt, ctx, begin, end, output, 0);
|
||||
return output;
|
||||
}
|
||||
}; //namespace
|
||||
|
@ -1,7 +1,10 @@
|
||||
#ifndef HORCRUX_SRC_CRYPTO_H
|
||||
#define HORCRUX_SRC_CRYPTO_H
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstddef>
|
||||
|
||||
namespace horcrux {
|
||||
enum class CipherType {AES256_CBC};
|
||||
|
||||
class Cipher {
|
||||
@ -108,3 +111,5 @@ static std::unique_ptr<Cipher> createCipher(CipherType type){
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
#endif //HORCRUX_SRC_CRYPTO_H
|
||||
|
@ -64,8 +64,8 @@ size_t FsPlainOutput::write(const std::vector<unsigned char>& 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)
|
||||
{
|
||||
|
39
src/io.h
39
src/io.h
@ -1,3 +1,5 @@
|
||||
#ifndef HORCRUX_SRC_IO_H
|
||||
#define HORCRUX_SRC_IO_H
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
@ -5,43 +7,62 @@
|
||||
|
||||
namespace horcrux {
|
||||
|
||||
class FsPlainInput {
|
||||
class Input {
|
||||
public:
|
||||
virtual std::vector<unsigned char> read() = 0;
|
||||
};
|
||||
class Output {
|
||||
public:
|
||||
virtual size_t write(const std::vector<unsigned char>& 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<unsigned char> read();
|
||||
std::vector<unsigned char> read() override;
|
||||
};
|
||||
|
||||
class FsCryptoInput {
|
||||
class FsCryptoInput : public Input{
|
||||
std::vector<std::pair<std::filesystem::path, size_t>> file_paths;
|
||||
size_t total_size{0};
|
||||
FsCryptoInput() = delete;
|
||||
|
||||
public:
|
||||
FsCryptoInput(const std::vector<std::string>& filenames);
|
||||
std::vector<unsigned char> read();
|
||||
std::vector<unsigned char> 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<unsigned char>& to_write);
|
||||
size_t write(const std::vector<unsigned char>& 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<std::filesystem::path> 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<unsigned char>& 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<unsigned char>& to_write) override;
|
||||
};
|
||||
};
|
||||
#endif //HORCRUX_SRC_IO_H
|
||||
|
@ -1,3 +1,5 @@
|
||||
#ifndef HORCRUX_SRC_UTILS_H
|
||||
#define HORCRUX_SRC_UTILS_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -8,3 +10,4 @@ std::vector<unsigned char> from_base64(const std::string& base64);
|
||||
std::vector<unsigned char> generate_random(const size_t buf_len);
|
||||
|
||||
}
|
||||
#endif //HORCRUX_SRC_UTILS_H
|
||||
|
@ -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})
|
||||
|
||||
|
@ -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<unsigned char> 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<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
||||
size_t output_len = plaintext.size() + 16;
|
||||
|
32
test/integration-test.cpp
Normal file
32
test/integration-test.cpp
Normal file
@ -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);
|
||||
}
|
@ -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<unsigned char> 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<unsigned char> buf(size);
|
||||
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out){
|
||||
std::vector<std::string> 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);
|
||||
|
57
test/test.h
Normal file
57
test/test.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef HORCRUX_TEST_TEST_H
|
||||
#define HORCRUX_TEST_TEST_H
|
||||
#include <filesystem>
|
||||
#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<unsigned char> 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<unsigned char> buf(size);
|
||||
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out){
|
||||
std::vector<std::string> 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<unsigned char> 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
|
Loading…
Reference in New Issue
Block a user