libcryfs/test/config/crypto/CryConfigEncryptorFactoryTest.cpp
2015-11-11 17:10:02 -08:00

64 lines
2.6 KiB
C++

#include <google/gtest/gtest.h>
#include "../../../src/config/crypto/CryConfigEncryptorFactory.h"
#include <messmer/cpp-utils/crypto/symmetric/ciphers.h>
#include <messmer/cpp-utils/data/DataFixture.h>
#include <messmer/cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
using cpputils::SCrypt;
using cpputils::AES256_GCM;
using cpputils::Data;
using cpputils::DataFixture;
using boost::none;
using std::ostream;
using namespace cryfs;
// This is needed for google test
namespace boost {
inline ostream &operator<<(ostream &stream, const CryConfigEncryptor::Decrypted &) {
return stream << "CryConfigEncryptor::Decrypted()";
}
}
class CryConfigEncryptorFactoryTest: public ::testing::Test {
public:
};
TEST_F(CryConfigEncryptorFactoryTest, EncryptAndDecrypt_SameEncryptor) {
auto encryptor = CryConfigEncryptorFactory::deriveKey("mypassword", SCrypt::TestSettings);
Data encrypted = encryptor->encrypt(DataFixture::generate(400), AES256_GCM::NAME);
auto decrypted = encryptor->decrypt(encrypted).value();
EXPECT_EQ(DataFixture::generate(400), decrypted.data);
}
TEST_F(CryConfigEncryptorFactoryTest, EncryptAndDecrypt_NewEncryptor) {
auto encryptor = CryConfigEncryptorFactory::deriveKey("mypassword", SCrypt::TestSettings);
Data encrypted = encryptor->encrypt(DataFixture::generate(400), AES256_GCM::NAME);
auto loadedEncryptor = CryConfigEncryptorFactory::loadKey(encrypted, "mypassword").value();
auto decrypted = loadedEncryptor->decrypt(encrypted).value();
EXPECT_EQ(DataFixture::generate(400), decrypted.data);
}
TEST_F(CryConfigEncryptorFactoryTest, DoesntDecryptWithWrongPassword) {
auto encryptor = CryConfigEncryptorFactory::deriveKey("mypassword", SCrypt::TestSettings);
Data encrypted = encryptor->encrypt(DataFixture::generate(400), AES256_GCM::NAME);
auto loadedEncryptor = CryConfigEncryptorFactory::loadKey(encrypted, "wrongpassword").value();
auto decrypted = loadedEncryptor->decrypt(encrypted);
EXPECT_EQ(none, decrypted);
}
TEST_F(CryConfigEncryptorFactoryTest, DoesntDecryptWithWrongPassword_EmptyData) {
auto encryptor = CryConfigEncryptorFactory::deriveKey("mypassword", SCrypt::TestSettings);
Data encrypted = encryptor->encrypt(Data(0), AES256_GCM::NAME);
auto loadedEncryptor = CryConfigEncryptorFactory::loadKey(encrypted, "wrongpassword").value();
auto decrypted = loadedEncryptor->decrypt(encrypted);
EXPECT_EQ(none, decrypted);
}
TEST_F(CryConfigEncryptorFactoryTest, DoesntDecryptInvalidData) {
auto loadedEncryptor = CryConfigEncryptorFactory::loadKey(Data(0), "mypassword");
EXPECT_EQ(none, loadedEncryptor);
}