Added test cases for outer config file encryption

This commit is contained in:
Sebastian Messmer 2015-11-11 11:58:09 -08:00
parent 923cd40acf
commit f90196826b
5 changed files with 138 additions and 5 deletions

View File

@ -13,7 +13,7 @@
#include "../CryCipher.h"
namespace cryfs {
//TODO Test (whole crypto folder)
//TODO Test
//TODO Test that encrypted config data always has the same size, no matter how big the plaintext config data
//TODO Test that specified inner cipher is used (e.g. can't be decrypted with other cipher)
//TODO Use own exception for cpputils::Serializer/cpputils::Deserializer errors and only catch them

View File

@ -116,7 +116,7 @@ TEST_F(CryConfigLoaderTest, EncryptionKey_Create) {
TEST_F(CryConfigLoaderTest, Cipher_Load) {
CreateWithCipher("twofish-128-cfb");
auto loaded = Load().value();
EXPECT_EQ("ciphername", loaded.config()->Cipher());
EXPECT_EQ("twofish-128-cfb", loaded.config()->Cipher());
}
TEST_F(CryConfigLoaderTest, Cipher_Create) {

View File

@ -17,7 +17,7 @@ using namespace cryfs;
// This is needed for google test
namespace boost {
ostream &operator<<(ostream &stream, const Data &) {
inline ostream &operator<<(ostream &stream, const Data &) {
return stream << "cpputils::Data()";
}
}
@ -78,8 +78,10 @@ TEST_F(ConcreteInnerEncryptorTest, DoesntEncryptWhenTooLarge) {
TEST_F(ConcreteInnerEncryptorTest, EncryptionIsFixedSize) {
auto encryptor = makeInnerEncryptor<AES256_GCM>();
InnerConfig encrypted1 = encryptor->encrypt(DataFixture::generate(200));
InnerConfig encrypted2 = encryptor->encrypt(Data(0));
InnerConfig encrypted1 = encryptor->encrypt(DataFixture::generate(100));
InnerConfig encrypted2 = encryptor->encrypt(DataFixture::generate(200));
InnerConfig encrypted3 = encryptor->encrypt(Data(0));
EXPECT_EQ(encrypted1.encryptedConfig.size(), encrypted2.encryptedConfig.size());
EXPECT_EQ(encrypted1.encryptedConfig.size(), encrypted3.encryptedConfig.size());
}

View File

@ -0,0 +1,61 @@
#include <google/gtest/gtest.h>
#include <messmer/cpp-utils/data/DataFixture.h>
#include <boost/optional/optional_io.hpp>
#include "../../../../src/config/crypto/outer/OuterConfig.h"
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::DerivedKeyConfig;
using boost::none;
using std::ostream;
using namespace cryfs;
// This is needed for google test
namespace boost {
ostream &operator<<(ostream &stream, const OuterConfig &) {
return stream << "OuterConfig()";
}
}
class OuterConfigTest: public ::testing::Test {
public:
Data salt() {
return DataFixture::generate(128, 2);
}
uint64_t N = 1024;
uint8_t r = 1;
uint8_t p = 2;
};
TEST_F(OuterConfigTest, SomeValues) {
Data serialized = OuterConfig{DerivedKeyConfig(salt(), N, r, p), DataFixture::generate(1024)}.serialize();
OuterConfig deserialized = OuterConfig::deserialize(serialized).value();
EXPECT_EQ(DerivedKeyConfig(salt(), N, r, p), deserialized.keyConfig);
EXPECT_EQ(DataFixture::generate(1024), deserialized.encryptedInnerConfig);
}
TEST_F(OuterConfigTest, DataEmpty) {
Data serialized = OuterConfig{DerivedKeyConfig(salt(), N, r, p), Data(0)}.serialize();
OuterConfig deserialized = OuterConfig::deserialize(serialized).value();
EXPECT_EQ(DerivedKeyConfig(salt(), N, r, p), deserialized.keyConfig);
EXPECT_EQ(Data(0), deserialized.encryptedInnerConfig);
}
TEST_F(OuterConfigTest, KeyConfigEmpty) {
Data serialized = OuterConfig{DerivedKeyConfig(Data(0), 0, 0, 0), DataFixture::generate(1024)}.serialize();
OuterConfig deserialized = OuterConfig::deserialize(serialized).value();
EXPECT_EQ(DerivedKeyConfig(Data(0), 0, 0, 0), deserialized.keyConfig);
EXPECT_EQ(DataFixture::generate(1024), deserialized.encryptedInnerConfig);
}
TEST_F(OuterConfigTest, DataAndKeyConfigEmpty) {
Data serialized = OuterConfig{DerivedKeyConfig(Data(0), 0, 0, 0), Data(0)}.serialize();
OuterConfig deserialized = OuterConfig::deserialize(serialized).value();
EXPECT_EQ(DerivedKeyConfig(Data(0), 0, 0, 0), deserialized.keyConfig);
EXPECT_EQ(Data(0), deserialized.encryptedInnerConfig);
}
TEST_F(OuterConfigTest, InvalidSerialization) {
auto deserialized = OuterConfig::deserialize(DataFixture::generate(1024));
EXPECT_EQ(none, deserialized);
}

View File

@ -0,0 +1,70 @@
#include <google/gtest/gtest.h>
#include "../../../../src/config/crypto/outer/OuterEncryptor.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::DerivedKeyConfig;
using namespace cryfs;
// This is needed for google test
namespace boost {
inline ostream &operator<<(ostream &stream, const Data &) {
return stream << "cpputils::Data()";
}
}
class OuterEncryptorTest : public ::testing::Test {
public:
unique_ref<OuterEncryptor> makeOuterEncryptor() {
auto key = DataFixture::generateFixedSize<OuterEncryptor::Cipher::EncryptionKey::BINARY_LENGTH>();
auto salt = DataFixture::generate(128);
return make_unique_ref<OuterEncryptor>(key, DerivedKeyConfig(std::move(salt), 1024, 1, 2));
}
};
TEST_F(OuterEncryptorTest, EncryptAndDecrypt) {
auto encryptor = makeOuterEncryptor();
OuterConfig encrypted = encryptor->encrypt(DataFixture::generate(200));
Data decrypted = encryptor->decrypt(encrypted).value();
EXPECT_EQ(DataFixture::generate(200), decrypted);
}
TEST_F(OuterEncryptorTest, EncryptAndDecrypt_EmptyData) {
auto encryptor = makeOuterEncryptor();
OuterConfig encrypted = encryptor->encrypt(Data(0));
Data decrypted = encryptor->decrypt(encrypted).value();
EXPECT_EQ(Data(0), decrypted);
}
TEST_F(OuterEncryptorTest, InvalidCiphertext) {
auto encryptor = makeOuterEncryptor();
OuterConfig encrypted = encryptor->encrypt(DataFixture::generate(200));
*(char*)encrypted.encryptedInnerConfig.data() = *(char*)encrypted.encryptedInnerConfig.data()+1; //Modify ciphertext
auto decrypted = encryptor->decrypt(encrypted);
EXPECT_EQ(none, decrypted);
}
TEST_F(OuterEncryptorTest, DoesntEncryptWhenTooLarge) {
auto encryptor = makeOuterEncryptor();
EXPECT_THROW(
encryptor->encrypt(DataFixture::generate(2000)),
std::runtime_error
);
}
TEST_F(OuterEncryptorTest, EncryptionIsFixedSize) {
auto encryptor = makeOuterEncryptor();
OuterConfig encrypted1 = encryptor->encrypt(DataFixture::generate(200));
OuterConfig encrypted2 = encryptor->encrypt(DataFixture::generate(700));
OuterConfig encrypted3 = encryptor->encrypt(Data(0));
EXPECT_EQ(encrypted1.encryptedInnerConfig.size(), encrypted2.encryptedInnerConfig.size());
EXPECT_EQ(encrypted1.encryptedInnerConfig.size(), encrypted3.encryptedInnerConfig.size());
}