diff --git a/.editorconfig b/.editorconfig index 7825f84..c629ac0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ root = true [*] end_of_line = lf -indent_style = tab +indent_style = space indent_size = 4 trim_trailing_whitespace = true diff --git a/CMakeLists.txt b/CMakeLists.txt index 24f39e2..c9c95cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,16 +6,16 @@ set(CMAKE_CXX_STANDARD 20) # Download and unpack googletest at configure time configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) - message(FATAL_ERROR "CMake step for googletest failed: ${result}") + message(FATAL_ERROR "CMake step for googletest failed: ${result}") endif() execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) if(result) - message(FATAL_ERROR "Build step for googletest failed: ${result}") + message(FATAL_ERROR "Build step for googletest failed: ${result}") endif() # Prevent overriding the parent project's compiler/linker @@ -32,7 +32,7 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src # dependencies automatically when using CMake 2.8.11 or # later. Otherwise we have to add them here ourselves. if (CMAKE_VERSION VERSION_LESS 2.8.11) - include_directories("${gtest_SOURCE_DIR}/include") + include_directories("${gtest_SOURCE_DIR}/include") endif() diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in index c6247af..9954228 100644 --- a/CMakeLists.txt.in +++ b/CMakeLists.txt.in @@ -4,12 +4,12 @@ project(googletest-download NONE) include(ExternalProject) ExternalProject_Add(googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG master - SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f4a4f4d..1b71980 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,9 +1,9 @@ set(SOURCES - main.cpp - horcrux.cpp - crypto.cpp - io.cpp - utils.cpp + main.cpp + horcrux.cpp + crypto.cpp + io.cpp + utils.cpp ) add_executable(${CMAKE_PROJECT_NAME} ${SOURCES}) @@ -16,5 +16,5 @@ target_link_libraries(${CMAKE_PROJECT_NAME} OpenSSL::Crypto) target_link_libraries(${CMAKE_PROJECT_NAME}_lib OpenSSL::Crypto) install(TARGETS ${CMAKE_PROJECT_NAME} - DESTINATION /usr/local/bin + DESTINATION /usr/local/bin ) diff --git a/src/crypto.cpp b/src/crypto.cpp index 081b0cf..c52766b 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -1,3 +1,5 @@ +#include "crypto.h" +#include #include #include #include @@ -7,188 +9,203 @@ #include #include -#include - -#include "crypto.h" #include "utils.h" namespace horcrux { class EvpCipherCtx { - EVP_CIPHER_CTX *ptr; + EVP_CIPHER_CTX *ptr; public: - EvpCipherCtx(){ - ptr = EVP_CIPHER_CTX_new(); - } + EvpCipherCtx() { + ptr = EVP_CIPHER_CTX_new(); + } - ~EvpCipherCtx(){ - EVP_CIPHER_CTX_free(ptr); - } + ~EvpCipherCtx() { + EVP_CIPHER_CTX_free(ptr); + } - EVP_CIPHER_CTX* get(){ - return ptr; - } + EVP_CIPHER_CTX* get() { + return ptr; + } }; EvpCipherCtx AES256_CBC::init(Mode mode, - const std::vector& key, const std::vector& iv){ - if (key.size() != kKeySize){ - throw std::runtime_error("Wrong key size"); - } - if (iv.size() != kIvSize){ - throw std::runtime_error("Wrong IV size"); - } + const std::vector& key, + const std::vector& iv) { + if (key.size() != kKeySize) { + throw std::runtime_error("Wrong key size"); + } + if (iv.size() != kIvSize) { + throw std::runtime_error("Wrong IV size"); + } - EvpCipherCtx ctx; - if (mode == Mode::kEncrypt){ - if (EVP_EncryptInit(ctx.get(), EVP_aes_256_cbc(), - (key.data()), (iv.data())) == 0){ - throw std::runtime_error("EVP_EncryptInit"); - } - } else if (mode == Mode::kDecrypt){ - if (EVP_DecryptInit(ctx.get(), EVP_aes_256_cbc(), - (key.data()), (iv.data())) == 0){ - throw std::runtime_error("EVP_DecryptInit"); - } - } else throw std::invalid_argument("Invalid Cipher mode"); - return ctx; + EvpCipherCtx ctx; + if (mode == Mode::kEncrypt) { + if (EVP_EncryptInit(ctx.get(), EVP_aes_256_cbc(), + key.data(), iv.data()) == 0) { + throw std::runtime_error("EVP_EncryptInit"); + } + } else if (mode == Mode::kDecrypt) { + if (EVP_DecryptInit(ctx.get(), EVP_aes_256_cbc(), + key.data(), iv.data()) == 0) { + throw std::runtime_error("EVP_DecryptInit"); + } + } else { + throw std::invalid_argument("Invalid Cipher mode"); + } + return ctx; } -size_t AES256_CBC::process_chunk(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in, bool resize_out) -{ - auto chunk_size = end - begin; - int len; - if(resize_in){ - // Make sure ouput is large enough to add encrypted data + padding - output.resize(output_offset + chunk_size + kIvSize); - } - if (mode == Mode::kEncrypt) { - if (1 != EVP_EncryptUpdate(ctx.get(), - output.data() + output_offset, &len, &*begin, chunk_size)){ - throw std::runtime_error("EVP_EncryptUpdate"); - } - } else { - if (1 != EVP_DecryptUpdate(ctx.get(), - output.data() + output_offset, &len, &*begin, chunk_size)){ - throw std::runtime_error("EVP_DecryptUpdate"); - } - } - if (resize_out) - output.resize(output_offset + len); - return len; +size_t AES256_CBC::process_chunk( + Mode mode, EvpCipherCtx& ctx, + std::vector::const_iterator begin, + std::vector::const_iterator end, + std::vector& output, size_t output_offset, + bool resize_in, bool resize_out) { + auto chunk_size = end - begin; + int len; + if (resize_in) { + // Make sure ouput is large enough to add encrypted data + padding + output.resize(output_offset + chunk_size + kIvSize); + } + if (mode == Mode::kEncrypt) { + if (1 != EVP_EncryptUpdate(ctx.get(), + output.data() + output_offset, &len, + &*begin, chunk_size)) { + throw std::runtime_error("EVP_EncryptUpdate"); + } + } else { + if (1 != EVP_DecryptUpdate(ctx.get(), + output.data() + output_offset, &len, + &*begin, chunk_size)) { + throw std::runtime_error("EVP_DecryptUpdate"); + } + } + if (resize_out) + output.resize(output_offset + len); + return len; } size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx, - std::vector& output, size_t output_offset, - bool resize_in, bool resize_out) -{ - int len; - if (resize_in) { - // Make sure output is large enough to add the last block - output.resize(output_offset + kIvSize); - } - if (mode == Mode::kEncrypt) { - if (1 != EVP_EncryptFinal_ex(ctx.get(), output.data() + output_offset, &len)){ - throw std::runtime_error("EVP_EncryptFinal"); - } - } else { - if (1 != EVP_DecryptFinal_ex(ctx.get(), (output.data()) + output_offset, &len)){ - throw std::runtime_error("EVP_DecryptFinal"); - } - } - if (resize_out) - output.resize(output_offset + len); - return len; + std::vector& output, + size_t output_offset, + bool resize_in, bool resize_out) { + int len; + if (resize_in) { + // Make sure output is large enough to add the last block + output.resize(output_offset + kIvSize); + } + if (mode == Mode::kEncrypt) { + if (1 != EVP_EncryptFinal_ex(ctx.get(), output.data() + output_offset, + &len)) { + throw std::runtime_error("EVP_EncryptFinal"); + } + } else { + if (1 != EVP_DecryptFinal_ex(ctx.get(), (output.data()) + output_offset, + &len)) { + throw std::runtime_error("EVP_DecryptFinal"); + } + } + if (resize_out) + output.resize(output_offset + len); + return len; } -size_t AES256_CBC::process_all(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in, bool resize_out) -{ - int len = process_chunk(mode, ctx, begin, end, - output, output_offset, resize_in, false); - len += process_final(mode, ctx, - output, output_offset + len, false, resize_out); - return len; +size_t AES256_CBC::process_all( + Mode mode, EvpCipherCtx& ctx, + std::vector::const_iterator begin, + std::vector::const_iterator end, + std::vector& output, size_t output_offset, + bool resize_in, bool resize_out) { + int len = process_chunk(mode, ctx, begin, end, output, output_offset, + resize_in, false); + len += process_final(mode, ctx, output, output_offset + len, false, + resize_out); + return len; } -std::vector AES256_CBC::process(const std::vector& inputdata) { - if (mode == Mode::kEncrypt) - return encrypt(inputdata); - else - return decrypt(inputdata); +std::vector AES256_CBC::process( + const std::vector& inputdata) { + if (mode == Mode::kEncrypt) + return encrypt(inputdata); + else + return decrypt(inputdata); } -std::vector AES256_CBC::encrypt(const std::vector& plaintext) { - return encrypt(encryption_key, horcrux::generate_random(kIvSize), plaintext); -} -std::vector AES256_CBC::decrypt(const std::vector& ciphertext) { - return decrypt(encryption_key, ciphertext); -} - -std::vector AES256_CBC::encrypt(const std::vector& key, const std::vector& iv, - const std::vector& input) -{ - auto ctx = init(Mode::kEncrypt, key, iv); - - // Make sure ouput is large enough to contain IV + encrypted data + padding - std::vector output(input.size() + (2 * kIvSize)); - std::copy(iv.begin(), iv.end(), output.begin()); - process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, kIvSize); - return output; -} - - -int AES256_CBC::encrypt(const std::vector& key, const std::vector& iv, - const std::vector& input, std::vector& output) -{ - auto ctx = init(Mode::kEncrypt, key, iv); - process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, 0); - return 0; -} - -int AES256_CBC::encrypt(const std::vector& key, const std::vector& iv, - std::istream input, const size_t input_len, - std::ostream output, size_t& output_len){ - - auto inbuf = std::vector(input_len); - auto outbuf = std::vector(input_len + 16); - input.read(reinterpret_cast(inbuf.data()), input_len); - - encrypt(key, iv, inbuf, outbuf); - - output.write(reinterpret_cast(outbuf.data()), outbuf.size()); - - return 0; -} - -int AES256_CBC::decrypt(const std::vector& key, const std::vector& iv, - const std::vector& input, std::vector& output) -{ - auto ctx = init(Mode::kDecrypt, key, iv); - process_all(Mode::kDecrypt, ctx, input.begin(), input.end(), output, 0); - return 0; -} -std::vector AES256_CBC::decrypt(const std::vector& key, - const std::vector& ciphertext){ - std::vector iv(ciphertext.begin(), ciphertext.begin() + kIvSize); - return decrypt(key, iv, ciphertext.begin() + kIvSize, ciphertext.end()); +std::vector AES256_CBC::encrypt( + const std::vector& plaintext) { + return encrypt(encryption_key, horcrux::generate_random(kIvSize), + plaintext); } std::vector AES256_CBC::decrypt( - const std::vector& key, const std::vector& iv, - std::vector::const_iterator begin, std::vector::const_iterator end) -{ - auto ctx = init(Mode::kDecrypt, key, iv); - std::vector output; - process_all(Mode::kDecrypt, ctx, begin, end, output, 0); - return output; + const std::vector& ciphertext) { + return decrypt(encryption_key, ciphertext); } -}; //namespace + +std::vector AES256_CBC::encrypt( + const std::vector& key, + const std::vector& iv, + const std::vector& input) { + auto ctx = init(Mode::kEncrypt, key, iv); + + // Make sure ouput is large enough to contain IV + encrypted data + padding + std::vector output(input.size() + (2 * kIvSize)); + std::copy(iv.begin(), iv.end(), output.begin()); + process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, + kIvSize); + return output; +} + + +int AES256_CBC::encrypt(const std::vector& key, + const std::vector& iv, + const std::vector& input, + std::vector& output) { + auto ctx = init(Mode::kEncrypt, key, iv); + process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, 0); + return 0; +} + +int AES256_CBC::encrypt(const std::vector& key, + const std::vector& iv, + std::istream input, const size_t input_len, + std::ostream output, size_t& output_len) { + auto inbuf = std::vector(input_len); + auto outbuf = std::vector(input_len + 16); + input.read(reinterpret_cast(inbuf.data()), input_len); + + encrypt(key, iv, inbuf, outbuf); + + output.write(reinterpret_cast(outbuf.data()), outbuf.size()); + + return 0; +} + +int AES256_CBC::decrypt(const std::vector& key, + const std::vector& iv, + const std::vector& input, + std::vector& output) { + auto ctx = init(Mode::kDecrypt, key, iv); + process_all(Mode::kDecrypt, ctx, input.begin(), input.end(), output, 0); + return 0; +} +std::vector AES256_CBC::decrypt( + const std::vector& key, + const std::vector& ciphertext) { + std::vector iv(ciphertext.begin(), + ciphertext.begin() + kIvSize); + return decrypt(key, iv, ciphertext.begin() + kIvSize, ciphertext.end()); +} +std::vector AES256_CBC::decrypt( + const std::vector& key, + const std::vector& iv, + std::vector::const_iterator begin, + std::vector::const_iterator end) { + auto ctx = init(Mode::kDecrypt, key, iv); + std::vector output; + process_all(Mode::kDecrypt, ctx, begin, end, output, 0); + return output; +} +}; // namespace horcrux diff --git a/src/crypto.h b/src/crypto.h index 4c440fb..7aefd23 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -8,93 +8,114 @@ namespace horcrux { -enum class Mode {kEncrypt, kDecrypt}; +enum class Mode { kEncrypt, kDecrypt }; class Cipher { protected: - Cipher() = delete; - Cipher(Mode m) : mode(m) { } - Cipher(Mode m, const std::vector& key) : - mode(m), encryption_key(key) {} + Cipher() = delete; + explicit Cipher(Mode m) : mode(m) { } + Cipher(Mode m, const std::vector& key) : + mode(m), encryption_key(key) {} - Cipher(Mode m, std::vector&& key) : - mode(m), encryption_key(key) {} + Cipher(Mode m, std::vector&& key) : + mode(m), encryption_key(key) {} + + std::vector encryption_key; - std::vector encryption_key; public: - const Mode mode; - const std::vector& get_encryption_key() {return encryption_key;} + const Mode mode; + const std::vector& get_encryption_key() { + return encryption_key; + } - virtual std::vector process(const std::vector& inputdata) = 0; - virtual std::vector encrypt(const std::vector& plaintext) = 0; - virtual std::vector decrypt(const std::vector& ciphertext) = 0; + virtual std::vector process( + const std::vector& inputdata) = 0; + + virtual std::vector encrypt( + const std::vector& plaintext) = 0; + + virtual std::vector decrypt( + const std::vector& ciphertext) = 0; }; class EvpCipherCtx; class AES256_CBC : public Cipher { - - const size_t kKeySize = 32; - const size_t kIvSize = 16; + const size_t kKeySize = 32; + const size_t kIvSize = 16; public: - AES256_CBC() : Cipher(Mode::kEncrypt) { - encryption_key = horcrux::generate_random(kKeySize); - } - AES256_CBC(const std::vector& key) : Cipher(Mode::kDecrypt, key) - { - if (key.size() != kKeySize) - throw std::invalid_argument("Invalid key size"); - } - AES256_CBC(std::vector&& key) : Cipher(Mode::kDecrypt, key) - { - if (key.size() != kKeySize) - throw std::invalid_argument("Invalid key size"); - } + AES256_CBC() : Cipher(Mode::kEncrypt) { + encryption_key = horcrux::generate_random(kKeySize); + } + explicit AES256_CBC(const std::vector& key) + : Cipher(Mode::kDecrypt, key) { + if (key.size() != kKeySize) + throw std::invalid_argument("Invalid key size"); + } + explicit AES256_CBC(std::vector&& key) + : Cipher(Mode::kDecrypt, key) { + if (key.size() != kKeySize) + throw std::invalid_argument("Invalid key size"); + } - std::vector process(const std::vector& inputdata) override; - std::vector encrypt(const std::vector& plaintext) override; - std::vector decrypt(const std::vector& ciphertext) override; + std::vector process( + const std::vector& inputdata) override; + std::vector encrypt( + const std::vector& plaintext) override; + std::vector decrypt( + const std::vector& ciphertext) override; - std::vector encrypt(const std::vector& key, const std::vector& iv, - const std::vector& input); + std::vector encrypt(const std::vector& key, + const std::vector& iv, + const std::vector& input); - int encrypt(const std::vector& key, const std::vector& iv, - const std::vector& input, std::vector& output); + int encrypt(const std::vector& key, + const std::vector& iv, + const std::vector& input, + std::vector& output); - int encrypt(const std::vector& key, const std::vector& iv, - std::istream input, const size_t input_len, - std::ostream output, size_t& output_len); + int encrypt(const std::vector& key, + const std::vector& iv, + std::istream input, const size_t input_len, + std::ostream output, size_t& output_len); - int decrypt(const std::vector& key, const std::vector& iv, - const std::vector& input, std::vector& output); + int decrypt(const std::vector& key, + const std::vector& iv, + const std::vector& input, + std::vector& output); - std::vector decrypt(const std::vector& key, - const std::vector& ciphertext); + std::vector decrypt( + const std::vector& key, + const std::vector& ciphertext); - std::vector decrypt( - const std::vector& key, const std::vector& iv, - std::vector::const_iterator begin, std::vector::const_iterator end); + std::vector decrypt( + const std::vector& key, + const std::vector& iv, + std::vector::const_iterator begin, + std::vector::const_iterator end); private: - EvpCipherCtx init(Mode mode, - const std::vector& key, const std::vector& iv); + EvpCipherCtx init(Mode mode, const std::vector& key, + const std::vector& iv); - size_t process_chunk(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in = true, bool resize_out = true); + size_t process_chunk(Mode mode, EvpCipherCtx& ctx, + std::vector::const_iterator begin, + std::vector::const_iterator end, + std::vector& output, + size_t output_offset, + bool resize_in = true, bool resize_out = true); - size_t process_final(Mode mode, EvpCipherCtx& ctx, - std::vector& output, size_t output_offset, - bool resize_in = true, bool resize_out = true); + size_t process_final(Mode mode, EvpCipherCtx& ctx, + std::vector& output, + size_t output_offset, + bool resize_in = true, bool resize_out = true); - size_t process_all(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in = true, bool resize_out = true); + size_t process_all(Mode mode, EvpCipherCtx& ctx, + std::vector::const_iterator begin, + std::vector::const_iterator end, + std::vector& output, size_t output_offset, + bool resize_in = true, bool resize_out = true); }; -}; //namespace -#endif //HORCRUX_SRC_CRYPTO_H +}; // namespace horcrux +#endif // HORCRUX_SRC_CRYPTO_H diff --git a/src/horcrux.cpp b/src/horcrux.cpp index 6cd6012..3fd9899 100644 --- a/src/horcrux.cpp +++ b/src/horcrux.cpp @@ -1,82 +1,82 @@ +#include "horcrux.h" #include #include -#include "horcrux.h" #include "utils.h" namespace horcrux { -Horcrux::Horcrux(HorcruxOptions&& opt) : options(opt){ init(); } -Horcrux::Horcrux(const HorcruxOptions& opt) : options(opt){ init(); } -void Horcrux::init(){ - if (options.count <= 0){ - throw std::invalid_argument("Invalid horcrux count"); - } - switch (options.mode){ - case Mode::kEncrypt: - cipher = std::make_unique(); - input = std::make_unique(options.input[0]); - output = std::make_unique(options.output, options.count, - std::filesystem::path(options.input[0]).filename().string()); - options.base64_key = to_base64(cipher->get_encryption_key()); - break; - case Mode::kDecrypt: - if (options.count != options.input.size()) - throw std::invalid_argument("Invalid horcrux count"); - cipher = std::make_unique(from_base64(options.base64_key)); - input = std::make_unique(options.input); - output = std::make_unique(options.output); - break; - default: - throw std::invalid_argument("Invalid horcrux mode"); - } +Horcrux::Horcrux(HorcruxOptions&& opt) : options(opt) { init(); } +Horcrux::Horcrux(const HorcruxOptions& opt) : options(opt) { init(); } +void Horcrux::init() { + if (options.count <= 0) { + throw std::invalid_argument("Invalid horcrux count"); + } + switch (options.mode) { + case Mode::kEncrypt: + cipher = std::make_unique(); + input = std::make_unique(options.input[0]); + output = std::make_unique(options.output, options.count, + std::filesystem::path(options.input[0]).filename().string()); + options.base64_key = to_base64(cipher->get_encryption_key()); + break; + case Mode::kDecrypt: + if (options.count != options.input.size()) { + throw std::invalid_argument("Invalid horcrux count"); + } + cipher = std::make_unique(from_base64(options.base64_key)); + input = std::make_unique(options.input); + output = std::make_unique(options.output); + break; + default: + throw std::invalid_argument("Invalid horcrux mode"); + } } -void Horcrux::execute(){ - if (options.mode == Mode::kEncrypt) - output->write(cipher->encrypt(input->read())); - else - output->write(cipher->decrypt(input->read())); +void Horcrux::execute() { + if (options.mode == Mode::kEncrypt) + output->write(cipher->encrypt(input->read())); + else + output->write(cipher->decrypt(input->read())); } -static void print_usage(const std::string& program){ - std::cout << "Usage:" << std::endl << - "\t\t" << program << " create -n " << std::endl << - "\t\t" << program << " load -k " << std::endl; +static void print_usage(const std::string& program) { + std::cout << "Usage:" << std::endl << + "\t\t" << program << " create -n " << std::endl << + "\t\t" << program << " load -k " << std::endl; } -static void parse_error(const std::string& program, const std::string& error){ - print_usage(program); - throw std::invalid_argument(error); +static void parse_error(const std::string& program, const std::string& error) { + print_usage(program); + throw std::invalid_argument(error); } -HorcruxOptions parse_arguments(std::vector&& arguments){ - if (arguments.size() < 6){ - parse_error(std::string(arguments[0]), "Wrong argument number"); - } - HorcruxOptions opt; - if (arguments[1] == "create") { - if (arguments.size() != 6){ - parse_error(std::string(arguments[0]), "Wrong argument number"); - } - opt.mode = Mode::kEncrypt; - if (arguments[2] != "-n") - parse_error(arguments[0], "create: unknown option " + arguments[2]); - opt.count = std::stoi(arguments[3]); - opt.input.emplace_back(arguments[4]); - opt.output = arguments[5]; - } else if (arguments[1] == "load"){ - opt.mode = Mode::kDecrypt; - if (arguments[2] != "-k") - parse_error(arguments[0], "load: unknown option " + arguments[2]); - opt.base64_key = arguments[3]; - opt.input.insert(opt.input.begin(), arguments.begin() + 4, arguments.end() - 1); - opt.count = opt.input.size(); - opt.output = arguments.back(); - } else { - parse_error(arguments[0], "unknown mode: " + arguments[1]); - } - return opt; +HorcruxOptions parse_arguments(std::vector&& arguments) { + if (arguments.size() < 6) { + parse_error(std::string(arguments[0]), "Wrong argument number"); + } + HorcruxOptions opt; + if (arguments[1] == "create") { + if (arguments.size() != 6) { + parse_error(std::string(arguments[0]), "Wrong argument number"); + } + opt.mode = Mode::kEncrypt; + if (arguments[2] != "-n") + parse_error(arguments[0], "create: unknown option " + arguments[2]); + opt.count = std::stoi(arguments[3]); + opt.input.emplace_back(arguments[4]); + opt.output = arguments[5]; + } else if (arguments[1] == "load") { + opt.mode = Mode::kDecrypt; + if (arguments[2] != "-k") + parse_error(arguments[0], "load: unknown option " + arguments[2]); + opt.base64_key = arguments[3]; + opt.input.insert(opt.input.begin(), arguments.begin() + 4, + arguments.end() - 1); + opt.count = opt.input.size(); + opt.output = arguments.back(); + } else { + parse_error(arguments[0], "unknown mode: " + arguments[1]); + } + return opt; } - - -}; //namespace +}; // namespace horcrux diff --git a/src/horcrux.h b/src/horcrux.h index 4250d8c..f0c270d 100644 --- a/src/horcrux.h +++ b/src/horcrux.h @@ -10,32 +10,30 @@ namespace horcrux { struct HorcruxOptions { - Mode mode; - int count; - std::vector input; - std::string output; - std::string base64_key; + Mode mode; + int count; + std::vector input; + std::string output; + std::string base64_key; }; class Horcrux { + HorcruxOptions options; + std::unique_ptr cipher; + std::unique_ptr input; + std::unique_ptr output; - HorcruxOptions options; - std::unique_ptr cipher; - std::unique_ptr input; - std::unique_ptr output; - - void init(); + void init(); public: - Horcrux(const HorcruxOptions& opt); - Horcrux(HorcruxOptions&& opt); - void execute(); - const Mode& mode() { return options.mode; } - const std::string& key() { return options.base64_key; } - + explicit Horcrux(const HorcruxOptions& opt); + explicit Horcrux(HorcruxOptions&& opt); + void execute(); + const Mode& mode() { return options.mode; } + const std::string& key() { return options.base64_key; } }; HorcruxOptions parse_arguments(std::vector&& arguments); -}; //namespace -#endif //HORCRUX_HORCRUX_H +}; // namespace horcrux +#endif // HORCRUX_HORCRUX_H diff --git a/src/io.cpp b/src/io.cpp index 31145ad..cf2252b 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -1,98 +1,97 @@ -#include #include "io.h" +#include #include "utils.h" namespace horcrux { namespace fs = std::filesystem; -FsPlainInput::FsPlainInput(const std::string& path) : file_path(fs::absolute(path)) -{ - if (fs::status(path).type() != fs::file_type::regular){ - throw std::invalid_argument("Input is not a regular file"); - } - file_size = fs::file_size(file_path); - if (file_size == 0){ - throw std::invalid_argument("Input is empty, nothing to encrypt"); - } - file_stream = std::ifstream(file_path, std::ios::binary); +FsPlainInput::FsPlainInput(const std::string& path) + : file_path(fs::absolute(path)) { + if (fs::status(path).type() != fs::file_type::regular) { + throw std::invalid_argument("Input is not a regular file"); + } + file_size = fs::file_size(file_path); + if (file_size == 0) { + throw std::invalid_argument("Input is empty, nothing to encrypt"); + } + file_stream = std::ifstream(file_path, std::ios::binary); } -std::vector FsPlainInput::read(){ - std::vector result(file_size); - file_stream.read(reinterpret_cast(result.data()), file_size); - return result; +std::vector FsPlainInput::read() { + std::vector result(file_size); + file_stream.read(reinterpret_cast(result.data()), file_size); + return result; } -FsCryptoInput::FsCryptoInput(const std::vector& filenames) -{ - if (filenames.empty()){ - throw std::invalid_argument("No files to decrypt"); - } - for (auto name : filenames){ - auto path = absolute(fs::absolute({name})); - if (fs::status(path).type() != fs::file_type::regular){ - throw std::invalid_argument("Input is not a regular file"); - } - file_paths.emplace_back(path, fs::file_size(path)); - total_size += file_paths.back().second; - } - if (total_size == 0){ - throw std::invalid_argument("No data to decrypt"); - } +FsCryptoInput::FsCryptoInput(const std::vector& filenames) { + if (filenames.empty()){ + throw std::invalid_argument("No files to decrypt"); + } + for (auto name : filenames) { + auto path = absolute(fs::absolute({name})); + if (fs::status(path).type() != fs::file_type::regular) { + throw std::invalid_argument("Input is not a regular file"); + } + file_paths.emplace_back(path, fs::file_size(path)); + total_size += file_paths.back().second; + } + if (total_size == 0) { + throw std::invalid_argument("No data to decrypt"); + } } -std::vector FsCryptoInput::read(){ - std::vector result(total_size); - size_t data_read{0}; - for (auto& f : file_paths){ - std::ifstream ifstream(f.first); - ifstream.read(reinterpret_cast(result.data()) + data_read, f.second); - data_read += f.second; - } - return result; +std::vector FsCryptoInput::read() { + std::vector result(total_size); + size_t data_read{0}; + for (auto& f : file_paths) { + std::ifstream ifstream(f.first); + ifstream.read(reinterpret_cast(result.data()) + data_read, f.second); + data_read += f.second; + } + return result; } FsPlainOutput::FsPlainOutput(const std::string& filename) - : file_path(fs::absolute(filename)) -{ - if (fs::exists(file_path) && fs::status(file_path).type() != fs::file_type::regular){ - throw std::invalid_argument("Output file is not a regular file"); - } + : file_path(fs::absolute(filename)) { + if (fs::exists(file_path) + && fs::status(file_path).type() != fs::file_type::regular) { + throw std::invalid_argument("Output file is not a regular file"); + } } -size_t FsPlainOutput::write(const std::vector& to_write){ - std::ofstream out(file_path, std::ios::binary); - out.write(reinterpret_cast(to_write.data()), to_write.size()); - return to_write.size(); +size_t FsPlainOutput::write(const std::vector& to_write) { + std::ofstream out(file_path, std::ios::binary); + out.write(reinterpret_cast(to_write.data()), to_write.size()); + return to_write.size(); } FsCryptoOutput::FsCryptoOutput(const std::string& folder, const int horcrux_num, - const std::string& filename, const size_t horcrux_size) - : folder_path(fs::absolute(folder)), base_name(filename), - num(horcrux_num), size(horcrux_size) -{ - if (fs::status(folder_path).type() != fs::file_type::directory){ - throw std::invalid_argument("Output is not a directory"); - } + const std::string& filename, const size_t horcrux_size) + : folder_path(fs::absolute(folder)), base_name(filename), + num(horcrux_num), size(horcrux_size) { + if (fs::status(folder_path).type() != fs::file_type::directory) { + throw std::invalid_argument("Output is not a directory"); + } - if (horcrux_num <= 0){ - throw std::invalid_argument("Invalid horcrux num"); - } + if (horcrux_num <= 0) { + throw std::invalid_argument("Invalid horcrux num"); + } } -size_t FsCryptoOutput::write(const std::vector& to_write){ - size = to_write.size() / num; - std::ofstream f; - created_files.clear(); - size_t data_written{0}; - for (int i = 0; i < num; ++i){ - std::string name = base_name + '.' + std::to_string(i) + ".enc"; - created_files.emplace_back(folder_path / name); - f = std::ofstream(created_files.back(), - std::ios::binary | std::ios::trunc); - auto chunk_size = i + 1 != num ? size : size + to_write.size() % size; - f.write(reinterpret_cast(to_write.data()) + data_written, chunk_size); - data_written += chunk_size; - } - return data_written; +size_t FsCryptoOutput::write(const std::vector& to_write) { + size = to_write.size() / num; + std::ofstream f; + created_files.clear(); + size_t data_written{0}; + for (int i = 0; i < num; ++i) { + std::string name = base_name + '.' + std::to_string(i) + ".enc"; + created_files.emplace_back(folder_path / name); + f = std::ofstream(created_files.back(), + std::ios::binary | std::ios::trunc); + auto chunk_size = i + 1 != num ? size : size + to_write.size() % size; + f.write(reinterpret_cast(to_write.data()) + data_written, + chunk_size); + data_written += chunk_size; + } + return data_written; } }; diff --git a/src/io.h b/src/io.h index 404e61d..d355a58 100644 --- a/src/io.h +++ b/src/io.h @@ -4,16 +4,17 @@ #include #include #include +#include namespace horcrux { class Input { public: - virtual std::vector read() = 0; + virtual std::vector read() = 0; }; class Output { public: - virtual size_t write(const std::vector& data) = 0; + virtual size_t write(const std::vector& data) = 0; }; class PlainInput : public Input {}; class CryptoInput : public Input {}; @@ -21,48 +22,49 @@ 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; + std::filesystem::path file_path; + size_t file_size; + std::ifstream file_stream; + FsPlainInput() = delete; public: - FsPlainInput(const std::string& path); - std::vector read() override; + explicit FsPlainInput(const std::string& path); + std::vector read() override; }; class FsCryptoInput : public Input{ - std::vector> file_paths; - size_t total_size{0}; - FsCryptoInput() = delete; + std::vector> file_paths; + size_t total_size{0}; + FsCryptoInput() = delete; public: - FsCryptoInput(const std::vector& filenames); - std::vector read() override; + explicit FsCryptoInput(const std::vector& filenames); + std::vector read() override; }; class FsPlainOutput : public Output { - std::filesystem::path file_path; - FsPlainOutput() = delete; + std::filesystem::path file_path; + FsPlainOutput() = delete; public: - FsPlainOutput(const std::string& filename); - size_t write(const std::vector& to_write) override; + explicit FsPlainOutput(const std::string& filename); + size_t write(const std::vector& to_write) override; }; class FsCryptoOutput : public Output { - std::filesystem::path folder_path; - std::string base_name; - int num; - size_t size; - FsCryptoOutput() = delete; + std::filesystem::path folder_path; + std::string base_name; + int num; + size_t size; + FsCryptoOutput() = delete; public: - std::vector created_files; + std::vector created_files; - 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; + 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 +}; // namespace horcrux +#endif // HORCRUX_SRC_IO_H diff --git a/src/main.cpp b/src/main.cpp index d705325..da6b2ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,28 +3,28 @@ #include "horcrux.h" int main(const int argc, const char *argv[]) { - int ret = -1; - std::vector arguments; - for (int i = 0; i < argc; ++i){ - arguments.emplace_back(argv[i]); - } + int ret = -1; + std::vector arguments; + for (int i = 0; i < argc; ++i) { + arguments.emplace_back(argv[i]); + } - try { - horcrux::Horcrux h(horcrux::parse_arguments(std::move(arguments))); - h.execute(); - if (h.mode() == horcrux::Mode::kEncrypt){ - //print encryption key to stdout - std::cout << h.key() << std::endl; - } - ret = 0; - } catch (std::invalid_argument& ex){ - std::cerr << "Error: Invalid arguments. " << ex.what() << std::endl; - } catch (std::runtime_error& ex){ - std::cerr << "Runtime error: " << ex.what() << std::endl; - } catch (std::exception& ex){ - std::cerr << "Unknown error: " << ex.what() << std::endl; - } + try { + horcrux::Horcrux h(horcrux::parse_arguments(std::move(arguments))); + h.execute(); + if (h.mode() == horcrux::Mode::kEncrypt) { + // print encryption key to stdout + std::cout << h.key() << std::endl; + } + ret = 0; + } catch (std::invalid_argument& ex) { + std::cerr << "Error: Invalid arguments. " << ex.what() << std::endl; + } catch (std::runtime_error& ex) { + std::cerr << "Runtime error: " << ex.what() << std::endl; + } catch (std::exception& ex) { + std::cerr << "Unknown error: " << ex.what() << std::endl; + } - return ret; + return ret; } diff --git a/src/utils.cpp b/src/utils.cpp index 630e271..f311e50 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,41 +1,39 @@ +#include "utils.h" +#include #include #include #include -#include -#include "utils.h" - -std::string horcrux::to_base64(const std::vector& binary) -{ - using namespace boost::archive::iterators; - using It = base64_from_binary::const_iterator, 6, 8>>; - auto base64 = std::string(It(binary.begin()), It(binary.end())); - // Add padding. - return base64.append((3 - binary.size() % 3) % 3, '='); +std::string horcrux::to_base64(const std::vector& binary) { + using namespace boost::archive::iterators; + using It = base64_from_binary::const_iterator, 6, 8>>; + auto base64 = std::string(It(binary.begin()), It(binary.end())); + // Add padding. + return base64.append((3 - binary.size() % 3) % 3, '='); } -std::vector horcrux::from_base64(const std::string& base64) -{ - using namespace boost::archive::iterators; - using It = transform_width, 8, 6>; - auto binary = std::vector(It(base64.begin()), It(base64.end())); - // Remove padding. - auto length = base64.size(); - if(binary.size() > 2 && base64[length - 1] == '=' && base64[length - 2] == '=') - { - binary.erase(binary.end() - 2, binary.end()); - } - else if(binary.size() > 1 && base64[length - 1] == '=') - { - binary.erase(binary.end() - 1, binary.end()); - } - return binary; +std::vector horcrux::from_base64(const std::string& base64) { + using namespace boost::archive::iterators; + using It = transform_width, + 8, 6>; + auto binary = std::vector(It(base64.begin()), + It(base64.end())); + // Remove padding. + auto length = base64.size(); + if (binary.size() > 2 && base64[length - 1] == '=' + && base64[length - 2] == '=') { + binary.erase(binary.end() - 2, binary.end()); + } else if (binary.size() > 1 && base64[length - 1] == '=') { + binary.erase(binary.end() - 1, binary.end()); + } + return binary; } -std::vector horcrux::generate_random(const size_t size){ - std::vector res(size); - if(!RAND_bytes(res.data(), size)){ - throw std::exception(); - } - return res; +std::vector horcrux::generate_random(const size_t size) { + std::vector res(size); + if (!RAND_bytes(res.data(), size)) { + throw std::exception(); + } + return res; } diff --git a/src/utils.h b/src/utils.h index 709b735..ca752e0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -11,4 +11,4 @@ std::vector from_base64(const std::string& base64); std::vector generate_random(const size_t buf_len); } -#endif //HORCRUX_SRC_UTILS_H +#endif // HORCRUX_SRC_UTILS_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f7df4c4..4498b84 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,11 @@ set(BINARY ${CMAKE_PROJECT_NAME}_test) add_executable(${BINARY} - main.cpp - crypto-test.cpp - io-test.cpp - utils-test.cpp - integration-test.cpp + main.cpp + crypto-test.cpp + io-test.cpp + utils-test.cpp + integration-test.cpp ) add_test(NAME test COMMAND ${BINARY}) @@ -14,5 +14,5 @@ target_link_libraries(${BINARY} PUBLIC ${CMAKE_PROJECT_NAME}_lib gtest) #Copy files needed for unit tests file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_work_dir - DESTINATION ${CMAKE_BINARY_DIR}) + DESTINATION ${CMAKE_BINARY_DIR}) add_definitions(-DTEST_WORK_DIR="${CMAKE_BINARY_DIR}/test_work_dir") diff --git a/test/crypto-test.cpp b/test/crypto-test.cpp index 973888d..05cd05c 100644 --- a/test/crypto-test.cpp +++ b/test/crypto-test.cpp @@ -6,94 +6,94 @@ using namespace horcrux; TEST(CryptoTest, encrypt1){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - size_t output_len = plaintext.size() + 16; - std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + size_t output_len = plaintext.size() + 16; + std::vector output(output_len); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); - AES256_CBC a; - a.encrypt(key, iv, plaintext, output); + AES256_CBC a; + a.encrypt(key, iv, plaintext, output); - EXPECT_EQ(test1_enc, output); + EXPECT_EQ(test1_enc, output); } TEST(CryptoTest, encrypt2){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - //size_t output_len = plaintext.size() + 16; - //std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + //size_t output_len = plaintext.size() + 16; + //std::vector output(output_len); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); - AES256_CBC a; - auto output = a.encrypt(key, iv, plaintext); - auto temp = iv; - temp.insert(temp.end(), test1_enc.begin(), test1_enc.end()); + AES256_CBC a; + auto output = a.encrypt(key, iv, plaintext); + auto temp = iv; + temp.insert(temp.end(), test1_enc.begin(), test1_enc.end()); - EXPECT_EQ(temp.size(), output.size()); - EXPECT_EQ(temp, output); + EXPECT_EQ(temp.size(), output.size()); + EXPECT_EQ(temp, output); } TEST(CryptoTest, encrypt3){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - //size_t output_len = plaintext.size() + 16; - //std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + //size_t output_len = plaintext.size() + 16; + //std::vector output(output_len); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); - AES256_CBC a(key); - auto output = a.encrypt(plaintext); - auto temp = iv; - temp.insert(temp.end(), test1_enc.begin(), test1_enc.end()); + AES256_CBC a(key); + auto output = a.encrypt(plaintext); + auto temp = iv; + temp.insert(temp.end(), test1_enc.begin(), test1_enc.end()); - EXPECT_EQ(temp.size(), output.size()); + EXPECT_EQ(temp.size(), output.size()); } TEST(CryptoTest, decrypt1){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - size_t output_len = test1_enc.size(); - std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + size_t output_len = test1_enc.size(); + std::vector output(output_len); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); - AES256_CBC a; - a.decrypt(key, iv, test1_enc, output); + AES256_CBC a; + a.decrypt(key, iv, test1_enc, output); - EXPECT_EQ(plaintext, output); + EXPECT_EQ(plaintext, output); } TEST(CryptoTest, decrypt2){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - size_t output_len = test1_enc.size(); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); - // constructs encrypted input (iv + encrypted_data) - auto input = std::vector(iv); - input.insert(input.end(),test1_enc.begin(), test1_enc.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + size_t output_len = test1_enc.size(); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); + // constructs encrypted input (iv + encrypted_data) + auto input = std::vector(iv); + input.insert(input.end(),test1_enc.begin(), test1_enc.end()); - AES256_CBC a; - auto output = a.decrypt(key, input); + AES256_CBC a; + auto output = a.decrypt(key, input); - EXPECT_EQ(plaintext, output); + EXPECT_EQ(plaintext, output); } TEST(CryptoTest, decrypt3){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - size_t output_len = test1_enc.size(); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); - // constructs encrypted input (iv + encrypted_data) - auto input = std::vector(iv); - input.insert(input.end(),test1_enc.begin(), test1_enc.end()); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + size_t output_len = test1_enc.size(); + const std::vector key(test1_key.begin(), test1_key.end()); + const std::vector iv(test1_iv.begin(), test1_iv.end()); + // constructs encrypted input (iv + encrypted_data) + auto input = std::vector(iv); + input.insert(input.end(),test1_enc.begin(), test1_enc.end()); - AES256_CBC a(key); - auto output = a.decrypt(input); + AES256_CBC a(key); + auto output = a.decrypt(input); - EXPECT_EQ(plaintext, output); + EXPECT_EQ(plaintext, output); } TEST(CryptoTest, endToEnd){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - AES256_CBC a; - auto crypto = a.encrypt(plaintext); - EXPECT_GT(crypto.size(), plaintext.size()); - auto decrypted = a.decrypt(crypto); - EXPECT_EQ(plaintext, decrypted); + const std::vector plaintext(test1_str.begin(), test1_str.end()); + AES256_CBC a; + auto crypto = a.encrypt(plaintext); + EXPECT_GT(crypto.size(), plaintext.size()); + auto decrypted = a.decrypt(crypto); + EXPECT_EQ(plaintext, decrypted); } diff --git a/test/integration-test.cpp b/test/integration-test.cpp index 585399c..d020571 100644 --- a/test/integration-test.cpp +++ b/test/integration-test.cpp @@ -5,124 +5,124 @@ using namespace horcrux; TEST(IntegrationTest, EndToEndEncryptDecrypt){ - auto buf = generic_read_file(image); + auto buf = generic_read_file(image); - //Prepare encrypt operation - auto in = FsPlainInput(image); - auto cipher = AES256_CBC(); - auto out = FsCryptoOutput(folder, 3); + //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())); + // 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()); + //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); + //check outcome + EXPECT_EQ(buf, buf2); - auto out2 = FsPlainOutput(noexist); - out2.write(cipher.decrypt(in2.read())); + auto out2 = FsPlainOutput(noexist); + out2.write(cipher.decrypt(in2.read())); - auto buf3 = generic_read_file(noexist); - EXPECT_EQ(buf, buf3); + auto buf3 = generic_read_file(noexist); + EXPECT_EQ(buf, buf3); - delete_created_files(out); - std::filesystem::remove(noexist); + 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}); + 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}; + HorcruxOptions options { + .mode = Mode::kEncrypt, + .count = 3, + .input = {image}, + .output = {folder} + }; + Horcrux enc{options}; - enc.execute(); - auto key = enc.key(); + enc.execute(); + auto key = enc.key(); - HorcruxOptions options2 { - .mode = Mode::kDecrypt, - .count = 3, - .input = {get_encrypted_files(folder, image)}, - .output = {noexist}, - .base64_key = key - }; + HorcruxOptions options2 { + .mode = Mode::kDecrypt, + .count = 3, + .input = {get_encrypted_files(folder, image)}, + .output = {noexist}, + .base64_key = key + }; - Horcrux dec{options2}; - dec.execute(); + 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);}); + //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"})); + 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"})); } diff --git a/test/io-test.cpp b/test/io-test.cpp index 55c146f..86195ba 100644 --- a/test/io-test.cpp +++ b/test/io-test.cpp @@ -3,174 +3,174 @@ #include "test.h" TEST(IoTests, FsPlainInput) { - EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument); - EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument); - EXPECT_THROW(horcrux::FsPlainInput input{empty}, std::invalid_argument); - EXPECT_NO_THROW(horcrux::FsPlainInput input{text}); - EXPECT_NO_THROW(horcrux::FsPlainInput input{image}); + EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument); + EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument); + EXPECT_THROW(horcrux::FsPlainInput input{empty}, std::invalid_argument); + EXPECT_NO_THROW(horcrux::FsPlainInput input{text}); + EXPECT_NO_THROW(horcrux::FsPlainInput input{image}); } TEST(IoTests, FsPlainInputReadText) { - auto input = horcrux::FsPlainInput{text}; - auto buf = generic_read_file(text); - EXPECT_EQ(buf, input.read()); + auto input = horcrux::FsPlainInput{text}; + auto buf = generic_read_file(text); + EXPECT_EQ(buf, input.read()); } TEST(IoTests, FsPlainInputReadImg) { - auto input = horcrux::FsPlainInput{image}; - auto buf = generic_read_file(image); - EXPECT_EQ(buf, input.read()); + auto input = horcrux::FsPlainInput{image}; + auto buf = generic_read_file(image); + EXPECT_EQ(buf, input.read()); } TEST(IoTests, FsCryptoOutput) { - EXPECT_THROW(horcrux::FsCryptoOutput output(noexist, 1), std::invalid_argument); - EXPECT_NO_THROW(horcrux::FsCryptoOutput output(folder, 1)); - EXPECT_THROW(horcrux::FsCryptoOutput output(folder, 0), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoOutput output(empty, 1), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoOutput output(text, 1), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoOutput output(image, 1), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoOutput output(noexist, 1), std::invalid_argument); + EXPECT_NO_THROW(horcrux::FsCryptoOutput output(folder, 1)); + EXPECT_THROW(horcrux::FsCryptoOutput output(folder, 0), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoOutput output(empty, 1), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoOutput output(text, 1), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoOutput output(image, 1), std::invalid_argument); } TEST(IoTests, FsCryptoOutputWriteImage){ - auto ifstream = std::ifstream{image, std::ios::binary}; - auto buf = generic_read_file(image); + auto ifstream = std::ifstream{image, std::ios::binary}; + auto buf = generic_read_file(image); - horcrux::FsCryptoOutput out(folder, 10); - auto written = out.write(buf); - EXPECT_EQ(written, buf.size()); + horcrux::FsCryptoOutput out(folder, 10); + auto written = out.write(buf); + EXPECT_EQ(written, buf.size()); - EXPECT_EQ(10, out.created_files.size()); - delete_created_files(out); + EXPECT_EQ(10, out.created_files.size()); + delete_created_files(out); } TEST(IoTests, FsCryptoOutputWriteText){ - auto buf = generic_read_file(text); + auto buf = generic_read_file(text); - horcrux::FsCryptoOutput out(folder, 2); - auto written = out.write(buf); - EXPECT_EQ(written, buf.size()); + horcrux::FsCryptoOutput out(folder, 2); + auto written = out.write(buf); + EXPECT_EQ(written, buf.size()); - EXPECT_EQ(2, out.created_files.size()); - delete_created_files(out); + EXPECT_EQ(2, out.created_files.size()); + delete_created_files(out); } TEST(IoTest, FsCryptoInput){ - EXPECT_THROW(horcrux::FsCryptoInput output({noexist, text, image}), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoInput output({folder, text, image}), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoInput output(std::vector{}), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoInput output({empty}), std::invalid_argument); - EXPECT_THROW(horcrux::FsCryptoInput output({empty, empty}), std::invalid_argument); - EXPECT_NO_THROW(horcrux::FsCryptoInput output({empty, empty, text})); - EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text})); + EXPECT_THROW(horcrux::FsCryptoInput output({noexist, text, image}), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoInput output({folder, text, image}), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoInput output(std::vector{}), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoInput output({empty}), std::invalid_argument); + EXPECT_THROW(horcrux::FsCryptoInput output({empty, empty}), std::invalid_argument); + EXPECT_NO_THROW(horcrux::FsCryptoInput output({empty, empty, text})); + EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text})); } TEST(IoTest, FsCryptoInputImage){ - auto buf = generic_read_file(image); - //split image using FsCryptoOutput - auto out = horcrux::FsCryptoOutput(folder, 7); - auto written = out.write(buf); - EXPECT_EQ(written, buf.size()); + auto buf = generic_read_file(image); + //split image using FsCryptoOutput + auto out = horcrux::FsCryptoOutput(folder, 7); + auto written = out.write(buf); + EXPECT_EQ(written, buf.size()); - auto files = get_created_filenames(out); - auto in = horcrux::FsCryptoInput(files); - auto read = in.read(); - EXPECT_EQ(read, buf); + auto files = get_created_filenames(out); + auto in = horcrux::FsCryptoInput(files); + auto read = in.read(); + EXPECT_EQ(read, buf); - files.push_back(text); - in = horcrux::FsCryptoInput(files); - read = in.read(); - EXPECT_NE(read, buf); + files.push_back(text); + in = horcrux::FsCryptoInput(files); + read = in.read(); + EXPECT_NE(read, buf); - delete_created_files(out); + delete_created_files(out); } TEST(IoTest, FsPlainOuput){ - EXPECT_NO_THROW(horcrux::FsPlainOutput output{noexist}); - EXPECT_THROW(horcrux::FsPlainOutput output{folder}, std::invalid_argument); - EXPECT_NO_THROW(horcrux::FsPlainOutput output{empty}); - EXPECT_NO_THROW(horcrux::FsPlainOutput output{text}); - EXPECT_NO_THROW(horcrux::FsPlainOutput output{image}); + EXPECT_NO_THROW(horcrux::FsPlainOutput output{noexist}); + EXPECT_THROW(horcrux::FsPlainOutput output{folder}, std::invalid_argument); + EXPECT_NO_THROW(horcrux::FsPlainOutput output{empty}); + EXPECT_NO_THROW(horcrux::FsPlainOutput output{text}); + EXPECT_NO_THROW(horcrux::FsPlainOutput output{image}); } TEST(IoTest, FsPlainOutputWrite){ - auto buf = generic_read_file(image); - auto out = horcrux::FsPlainOutput(noexist); - auto written = out.write(buf); - EXPECT_EQ(written, buf.size()); - auto buf2 = generic_read_file(noexist); - EXPECT_EQ(buf, buf2); - std::filesystem::remove(noexist); + auto buf = generic_read_file(image); + auto out = horcrux::FsPlainOutput(noexist); + auto written = out.write(buf); + EXPECT_EQ(written, buf.size()); + auto buf2 = generic_read_file(noexist); + EXPECT_EQ(buf, buf2); + std::filesystem::remove(noexist); } TEST(IoTest, PlainToPlain){ - auto buf = generic_read_file(image); - auto in = horcrux::FsPlainInput(image); - auto out = horcrux::FsPlainOutput(noexist); + auto buf = generic_read_file(image); + auto in = horcrux::FsPlainInput(image); + auto out = horcrux::FsPlainOutput(noexist); - //Execute Plain To Plain - out.write(in.read()); + //Execute Plain To Plain + out.write(in.read()); - //Check outcome - auto buf2 = generic_read_file(noexist); - EXPECT_EQ(buf, buf2); - std::filesystem::remove(noexist); + //Check outcome + auto buf2 = generic_read_file(noexist); + EXPECT_EQ(buf, buf2); + std::filesystem::remove(noexist); } TEST(IoTest, PlainToCrypto){ - auto buf = generic_read_file(image); - auto in = horcrux::FsPlainInput(image); - auto out = horcrux::FsCryptoOutput(folder, 5); + auto buf = generic_read_file(image); + auto in = horcrux::FsPlainInput(image); + auto out = horcrux::FsCryptoOutput(folder, 5); - //Execute Plain to Crypto - out.write(in.read()); + //Execute Plain to Crypto + out.write(in.read()); - //use FsCryptoInput to re-join the files and check the outcome - auto files = get_created_filenames(out); - auto in_test = horcrux::FsCryptoInput(files); - auto buf2 = in_test.read(); - EXPECT_EQ(buf, buf2); - delete_created_files(out); + //use FsCryptoInput to re-join the files and check the outcome + auto files = get_created_filenames(out); + auto in_test = horcrux::FsCryptoInput(files); + auto buf2 = in_test.read(); + EXPECT_EQ(buf, buf2); + delete_created_files(out); } TEST(IoTest, CryptoToCrypto){ - auto buf = generic_read_file(image); - // First use FsCryptoOutput to split the files - auto out_test = horcrux::FsCryptoOutput(folder, 8); - out_test.write(buf); + auto buf = generic_read_file(image); + // First use FsCryptoOutput to split the files + auto out_test = horcrux::FsCryptoOutput(folder, 8); + out_test.write(buf); - //use FsCryptoInput to re-join the files - auto files = get_created_filenames(out_test); - auto in = horcrux::FsCryptoInput(files); - auto out = horcrux::FsCryptoOutput(folder, 5, "test"); - //Execute Crypto to Crypto - out.write(in.read()); + //use FsCryptoInput to re-join the files + auto files = get_created_filenames(out_test); + auto in = horcrux::FsCryptoInput(files); + auto out = horcrux::FsCryptoOutput(folder, 5, "test"); + //Execute Crypto to Crypto + out.write(in.read()); - //use FsCryptoInput to re-join the files and check the outcome - files = get_created_filenames(out); - auto in_test = horcrux::FsCryptoInput(files); - auto buf2 = in_test.read(); - EXPECT_EQ(buf, buf2); + //use FsCryptoInput to re-join the files and check the outcome + files = get_created_filenames(out); + auto in_test = horcrux::FsCryptoInput(files); + auto buf2 = in_test.read(); + EXPECT_EQ(buf, buf2); - delete_created_files(out_test); - delete_created_files(out); + delete_created_files(out_test); + delete_created_files(out); } TEST(IoTest, CryptoToPlain){ - auto buf = generic_read_file(image); - // First use FsCryptoOutput to split the files - auto out_test = horcrux::FsCryptoOutput(folder, 8); - out_test.write(buf); + auto buf = generic_read_file(image); + // First use FsCryptoOutput to split the files + auto out_test = horcrux::FsCryptoOutput(folder, 8); + out_test.write(buf); - //use FsCryptoInput to re-join the files - auto files = get_created_filenames(out_test); - auto in = horcrux::FsCryptoInput(files); - auto out = horcrux::FsPlainOutput(noexist); - //Execute Crypto to Plain - out.write(in.read()); + //use FsCryptoInput to re-join the files + auto files = get_created_filenames(out_test); + auto in = horcrux::FsCryptoInput(files); + auto out = horcrux::FsPlainOutput(noexist); + //Execute Crypto to Plain + out.write(in.read()); - //check the outcome - auto buf2 = generic_read_file(noexist); - EXPECT_EQ(buf, buf2); - delete_created_files(out_test); - std::filesystem::remove(noexist); + //check the outcome + auto buf2 = generic_read_file(noexist); + EXPECT_EQ(buf, buf2); + delete_created_files(out_test); + std::filesystem::remove(noexist); } diff --git a/test/main.cpp b/test/main.cpp index 1eb5aab..c5e8a03 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,40 +1,43 @@ -#include "gtest/gtest.h" #include "test.h" +#include "gtest/gtest.h" /* Utility functions */ -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; +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; } -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; +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; } -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);}); +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); } ); } -std::vector get_encrypted_files(const std::string& folder, const std::string& basename) { - std::vector result; - for (auto& p: std::filesystem::directory_iterator(folder)){ - if (p.path().string().starts_with(basename) && - p.path().string().ends_with(".enc")) - result.push_back(p.path().string()); - } - std::sort(result.begin(), result.end()); - return result; +std::vector get_encrypted_files(const std::string& folder, + const std::string& basename) { + std::vector result; + for (auto& p: std::filesystem::directory_iterator(folder)) { + if (p.path().string().starts_with(basename) + && p.path().string().ends_with(".enc")) { + result.push_back(p.path().string()); + } + } + std::sort(result.begin(), result.end()); + return result; } int main(int argc, char *argv[]) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/test/test.h b/test/test.h index 6a530ee..2aac5b7 100644 --- a/test/test.h +++ b/test/test.h @@ -28,8 +28,8 @@ std::vector get_encrypted_files(const std::string& folder, const st /* test command: echo "ditemi perche' se la mucca fa mu il merlo non fa me" | - scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF | - xxd -i + 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"; diff --git a/test/utils-test.cpp b/test/utils-test.cpp index 6751c8f..37fce1c 100644 --- a/test/utils-test.cpp +++ b/test/utils-test.cpp @@ -16,31 +16,31 @@ const std::string test4_str = "mi e' entrata una bruschetta nell'occhio"; const std::string test4_base64 = "bWkgZScgZW50cmF0YSB1bmEgYnJ1c2NoZXR0YSBuZWxsJ29jY2hpbw=="; TEST(utilTests, to_base64Test){ - EXPECT_EQ(test1_base64, horcrux::to_base64(std::vector(test1_str.begin(), test1_str.end()))); - EXPECT_EQ(test2_base64, horcrux::to_base64(std::vector(test2_str.begin(), test2_str.end()))); - EXPECT_EQ(test3_base64, horcrux::to_base64(std::vector(test3_str.begin(), test3_str.end()))); - EXPECT_EQ(test4_base64, horcrux::to_base64(std::vector(test4_str.begin(), test4_str.end()))); + EXPECT_EQ(test1_base64, horcrux::to_base64(std::vector(test1_str.begin(), test1_str.end()))); + EXPECT_EQ(test2_base64, horcrux::to_base64(std::vector(test2_str.begin(), test2_str.end()))); + EXPECT_EQ(test3_base64, horcrux::to_base64(std::vector(test3_str.begin(), test3_str.end()))); + EXPECT_EQ(test4_base64, horcrux::to_base64(std::vector(test4_str.begin(), test4_str.end()))); } TEST(utilTests, from_base64Test){ - auto result = horcrux::from_base64(test1_base64); - EXPECT_EQ(test1_str, std::string(result.begin(), result.end())); + auto result = horcrux::from_base64(test1_base64); + EXPECT_EQ(test1_str, std::string(result.begin(), result.end())); - result = horcrux::from_base64(test2_base64); - EXPECT_EQ(test2_str, std::string(result.begin(), result.end())); + result = horcrux::from_base64(test2_base64); + EXPECT_EQ(test2_str, std::string(result.begin(), result.end())); - result = horcrux::from_base64(test3_base64); - EXPECT_EQ(test3_str, std::string(result.begin(), result.end())); + result = horcrux::from_base64(test3_base64); + EXPECT_EQ(test3_str, std::string(result.begin(), result.end())); - result = horcrux::from_base64(test4_base64); - EXPECT_EQ(test4_str, std::string(result.begin(), result.end())); + result = horcrux::from_base64(test4_base64); + EXPECT_EQ(test4_str, std::string(result.begin(), result.end())); } TEST(utilTests, generate_random){ - /* It works, but it's not deterministic */ - //EXPECT_NE(horcrux::generate_random(32), horcrux::generate_random(32)); - //EXPECT_NE(horcrux::generate_random(16), horcrux::generate_random(16)); - //EXPECT_NE(horcrux::generate_random(8), horcrux::generate_random(8)); - //EXPECT_NE(horcrux::generate_random(1), horcrux::generate_random(1)); - EXPECT_EQ(horcrux::generate_random(0), horcrux::generate_random(0)); + /* It works, but it's not deterministic */ + //EXPECT_NE(horcrux::generate_random(32), horcrux::generate_random(32)); + //EXPECT_NE(horcrux::generate_random(16), horcrux::generate_random(16)); + //EXPECT_NE(horcrux::generate_random(8), horcrux::generate_random(8)); + //EXPECT_NE(horcrux::generate_random(1), horcrux::generate_random(1)); + EXPECT_EQ(horcrux::generate_random(0), horcrux::generate_random(0)); }