Added test cases for inner config file encryption
This commit is contained in:
parent
6d4ae65660
commit
d003b7f0c5
@ -9,7 +9,6 @@
|
||||
#include "InnerConfig.h"
|
||||
|
||||
namespace cryfs {
|
||||
//TODO Test
|
||||
template<class Cipher>
|
||||
class ConcreteInnerEncryptor: public InnerEncryptor {
|
||||
public:
|
||||
@ -38,6 +37,7 @@ namespace cryfs {
|
||||
}
|
||||
auto decrypted = Cipher::decrypt(static_cast<const uint8_t*>(innerConfig.encryptedConfig.data()), innerConfig.encryptedConfig.size(), _key);
|
||||
if (decrypted == boost::none) {
|
||||
cpputils::logging::LOG(cpputils::logging::ERROR) << "Failed decrypting configuration file";
|
||||
return boost::none;
|
||||
}
|
||||
auto configData = cpputils::RandomPadding::remove(*decrypted);
|
||||
|
85
test/config/crypto/inner/ConcreteInnerEncryptorTest.cpp
Normal file
85
test/config/crypto/inner/ConcreteInnerEncryptorTest.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
#include <google/gtest/gtest.h>
|
||||
#include "../../../../src/config/crypto/inner/ConcreteInnerEncryptor.h"
|
||||
#include <messmer/cpp-utils/crypto/symmetric/ciphers.h>
|
||||
#include <messmer/cpp-utils/data/DataFixture.h>
|
||||
#include <boost/optional/optional_io.hpp>
|
||||
|
||||
using std::ostream;
|
||||
using boost::none;
|
||||
using cpputils::Data;
|
||||
using cpputils::DataFixture;
|
||||
using cpputils::unique_ref;
|
||||
using cpputils::make_unique_ref;
|
||||
using cpputils::AES256_GCM;
|
||||
using cpputils::AES256_CFB;
|
||||
using cpputils::Twofish128_CFB;
|
||||
using namespace cryfs;
|
||||
|
||||
// This is needed for google test
|
||||
namespace boost {
|
||||
ostream &operator<<(ostream &stream, const Data &) {
|
||||
return stream << "cpputils::Data()";
|
||||
}
|
||||
}
|
||||
|
||||
class ConcreteInnerEncryptorTest : public ::testing::Test {
|
||||
public:
|
||||
template<class Cipher>
|
||||
unique_ref<InnerEncryptor> makeInnerEncryptor() {
|
||||
auto key = DataFixture::generateFixedSize<Cipher::EncryptionKey::BINARY_LENGTH>();
|
||||
return make_unique_ref<ConcreteInnerEncryptor<Cipher>>(key);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, EncryptAndDecrypt_AES) {
|
||||
auto encryptor = makeInnerEncryptor<AES256_GCM>();
|
||||
InnerConfig encrypted = encryptor->encrypt(DataFixture::generate(200));
|
||||
Data decrypted = encryptor->decrypt(encrypted).value();
|
||||
EXPECT_EQ(DataFixture::generate(200), decrypted);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, EncryptAndDecrypt_Twofish) {
|
||||
auto encryptor = makeInnerEncryptor<Twofish128_CFB>();
|
||||
InnerConfig encrypted = encryptor->encrypt(DataFixture::generate(200));
|
||||
Data decrypted = encryptor->decrypt(encrypted).value();
|
||||
EXPECT_EQ(DataFixture::generate(200), decrypted);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, EncryptAndDecrypt_EmptyData) {
|
||||
auto encryptor = makeInnerEncryptor<AES256_GCM>();
|
||||
InnerConfig encrypted = encryptor->encrypt(Data(0));
|
||||
Data decrypted = encryptor->decrypt(encrypted).value();
|
||||
EXPECT_EQ(Data(0), decrypted);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, DoesntDecryptWithWrongCipherName) {
|
||||
auto encryptor = makeInnerEncryptor<Twofish128_CFB>();
|
||||
InnerConfig encrypted = encryptor->encrypt(Data(0));
|
||||
encrypted.cipherName = AES256_CFB::NAME;
|
||||
auto decrypted = encryptor->decrypt(encrypted);
|
||||
EXPECT_EQ(none, decrypted);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, InvalidCiphertext) {
|
||||
auto encryptor = makeInnerEncryptor<AES256_GCM>();
|
||||
InnerConfig encrypted = encryptor->encrypt(DataFixture::generate(200));
|
||||
*(char*)encrypted.encryptedConfig.data() = *(char*)encrypted.encryptedConfig.data()+1; //Modify ciphertext
|
||||
auto decrypted = encryptor->decrypt(encrypted);
|
||||
EXPECT_EQ(none, decrypted);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, DoesntEncryptWhenTooLarge) {
|
||||
auto encryptor = makeInnerEncryptor<AES256_GCM>();
|
||||
EXPECT_THROW(
|
||||
encryptor->encrypt(DataFixture::generate(2000)),
|
||||
std::runtime_error
|
||||
);
|
||||
}
|
||||
|
||||
TEST_F(ConcreteInnerEncryptorTest, EncryptionIsFixedSize) {
|
||||
auto encryptor = makeInnerEncryptor<AES256_GCM>();
|
||||
InnerConfig encrypted1 = encryptor->encrypt(DataFixture::generate(200));
|
||||
InnerConfig encrypted2 = encryptor->encrypt(Data(0));
|
||||
|
||||
EXPECT_EQ(encrypted1.encryptedConfig.size(), encrypted2.encryptedConfig.size());
|
||||
}
|
50
test/config/crypto/inner/InnerConfigTest.cpp
Normal file
50
test/config/crypto/inner/InnerConfigTest.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <google/gtest/gtest.h>
|
||||
#include <messmer/cpp-utils/data/DataFixture.h>
|
||||
#include <boost/optional/optional_io.hpp>
|
||||
#include "../../../../src/config/crypto/inner/InnerConfig.h"
|
||||
|
||||
using cpputils::Data;
|
||||
using cpputils::DataFixture;
|
||||
using boost::none;
|
||||
using std::ostream;
|
||||
using namespace cryfs;
|
||||
|
||||
// This is needed for google test
|
||||
namespace boost {
|
||||
ostream &operator<<(ostream &stream, const InnerConfig &config) {
|
||||
return stream << "InnerConfig(" << config.cipherName << ", [data])";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(InnerConfigTest, SomeValues) {
|
||||
Data serialized = InnerConfig{"myciphername", DataFixture::generate(1024)}.serialize();
|
||||
InnerConfig deserialized = InnerConfig::deserialize(serialized).value();
|
||||
EXPECT_EQ("myciphername", deserialized.cipherName);
|
||||
EXPECT_EQ(DataFixture::generate(1024), deserialized.encryptedConfig);
|
||||
}
|
||||
|
||||
TEST(InnerConfigTest, DataEmpty) {
|
||||
Data serialized = InnerConfig{"myciphername", Data(0)}.serialize();
|
||||
InnerConfig deserialized = InnerConfig::deserialize(serialized).value();
|
||||
EXPECT_EQ("myciphername", deserialized.cipherName);
|
||||
EXPECT_EQ(Data(0), deserialized.encryptedConfig);
|
||||
}
|
||||
|
||||
TEST(InnerConfigTest, CipherNameEmpty) {
|
||||
Data serialized = InnerConfig{"", DataFixture::generate(1024)}.serialize();
|
||||
InnerConfig deserialized = InnerConfig::deserialize(serialized).value();
|
||||
EXPECT_EQ("", deserialized.cipherName);
|
||||
EXPECT_EQ(DataFixture::generate(1024), deserialized.encryptedConfig);
|
||||
}
|
||||
|
||||
TEST(InnerConfigTest, DataAndCipherNameEmpty) {
|
||||
Data serialized = InnerConfig{"", Data(0)}.serialize();
|
||||
InnerConfig deserialized = InnerConfig::deserialize(serialized).value();
|
||||
EXPECT_EQ("", deserialized.cipherName);
|
||||
EXPECT_EQ(Data(0), deserialized.encryptedConfig);
|
||||
}
|
||||
|
||||
TEST(InnerConfigTest, InvalidSerialization) {
|
||||
auto deserialized = InnerConfig::deserialize(DataFixture::generate(1024));
|
||||
EXPECT_EQ(none, deserialized);
|
||||
}
|
Loading…
Reference in New Issue
Block a user