Added test cases for CryConfigEncryptorFactory
This commit is contained in:
parent
10cbb06a4b
commit
9793b0e870
@ -5,6 +5,7 @@
|
||||
#include <boost/optional/optional_io.hpp>
|
||||
|
||||
//TODO Test that config file is still loadable after changing the cipher and then storing it (i.e. it uses a different inner cipher but is still the same CryConfigFile instance)
|
||||
//TODO Test it can load a precreated config file
|
||||
|
||||
using namespace cryfs;
|
||||
using cpputils::TempFile;
|
||||
|
64
test/config/crypto/CryConfigEncryptorFactoryTest.cpp
Normal file
64
test/config/crypto/CryConfigEncryptorFactoryTest.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user