#include #include "../../../../src/config/crypto/inner/ConcreteInnerEncryptor.h" #include #include #include 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 { inline ostream &operator<<(ostream &stream, const Data &) { return stream << "cpputils::Data()"; } } class ConcreteInnerEncryptorTest : public ::testing::Test { public: template unique_ref makeInnerEncryptor() { auto key = DataFixture::generateFixedSize(); return make_unique_ref>(key); } }; TEST_F(ConcreteInnerEncryptorTest, EncryptAndDecrypt_AES) { auto encryptor = makeInnerEncryptor(); 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(); 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(); InnerConfig encrypted = encryptor->encrypt(Data(0)); Data decrypted = encryptor->decrypt(encrypted).value(); EXPECT_EQ(Data(0), decrypted); } TEST_F(ConcreteInnerEncryptorTest, DoesntDecryptWithWrongCipherName) { auto encryptor = makeInnerEncryptor(); 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(); 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(); EXPECT_THROW( encryptor->encrypt(DataFixture::generate(2000)), std::runtime_error ); } TEST_F(ConcreteInnerEncryptorTest, EncryptionIsFixedSize) { auto encryptor = makeInnerEncryptor(); 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()); }