2015-10-23 00:04:03 +02:00
# include <google/gtest/gtest.h>
# include "../../src/config/CryConfigFile.h"
# include <messmer/cpp-utils/tempfile/TempFile.h>
2015-10-24 19:35:37 +02:00
# include <boost/optional/optional_io.hpp>
2015-10-23 00:04:03 +02:00
2015-11-11 10:19:47 +01:00
//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)
2015-11-12 02:10:02 +01:00
//TODO Test it can load a precreated config file
2015-11-11 10:19:47 +01:00
2015-10-23 00:04:03 +02:00
using namespace cryfs ;
using cpputils : : TempFile ;
2015-10-24 19:35:37 +02:00
using std : : string ;
using boost : : optional ;
using boost : : none ;
2015-11-04 05:27:00 +01:00
using cpputils : : SCrypt ;
2015-10-24 19:35:37 +02:00
namespace bf = boost : : filesystem ;
//gtest/boost::optional workaround for working with optional<CryConfigFile>
namespace boost {
inline std : : ostream & operator < < ( std : : ostream & out , const CryConfigFile & file ) {
UNUSED ( file ) ;
out < < " ConfigFile " ;
return out ;
}
}
2015-10-23 00:04:03 +02:00
class CryConfigFileTest : public : : testing : : Test {
public :
CryConfigFileTest ( ) : file ( false ) { }
TempFile file ;
2015-11-11 10:19:47 +01:00
CryConfig Config ( ) {
CryConfig result ;
result . SetCipher ( " aes-256-gcm " ) ;
return result ;
}
2015-10-24 19:35:37 +02:00
CryConfigFile CreateAndLoadEmpty ( const string & password = " mypassword " ) {
2015-11-11 10:19:47 +01:00
Create ( Config ( ) , password ) ;
2015-10-24 19:35:37 +02:00
return Load ( ) . value ( ) ;
}
void Create ( CryConfig cfg , const string & password = " mypassword " ) {
2015-11-04 05:27:00 +01:00
CryConfigFile : : create ( file . path ( ) , std : : move ( cfg ) , password , SCrypt : : TestSettings ) ;
2015-10-23 00:04:03 +02:00
}
2015-10-24 19:35:37 +02:00
optional < CryConfigFile > Load ( const string & password = " mypassword " ) {
return CryConfigFile : : load ( file . path ( ) , password ) ;
2015-10-23 00:04:03 +02:00
}
2015-11-11 10:19:47 +01:00
void CreateWithCipher ( const string & cipher ) {
return CreateWithCipher ( cipher , file ) ;
}
2015-10-24 19:35:37 +02:00
void CreateWithCipher ( const string & cipher , const TempFile & tempFile ) {
CryConfig cfg ;
cfg . SetCipher ( cipher ) ;
2015-11-04 05:27:00 +01:00
CryConfigFile : : create ( tempFile . path ( ) , std : : move ( cfg ) , " mypassword " , SCrypt : : TestSettings ) ;
2015-10-23 00:04:03 +02:00
}
} ;
2015-10-24 19:35:37 +02:00
TEST_F ( CryConfigFileTest , DoesntLoadIfWrongPassword ) {
2015-11-11 10:19:47 +01:00
Create ( Config ( ) , " mypassword " ) ;
2015-10-24 19:35:37 +02:00
auto loaded = Load ( " mypassword2 " ) ;
EXPECT_EQ ( none , loaded ) ;
}
2015-10-23 00:04:03 +02:00
TEST_F ( CryConfigFileTest , RootBlob_Init ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
EXPECT_EQ ( " " , created . config ( ) - > RootBlob ( ) ) ;
}
TEST_F ( CryConfigFileTest , RootBlob_CreateAndLoad ) {
2015-11-11 10:19:47 +01:00
CryConfig cfg = Config ( ) ;
2015-10-23 00:04:03 +02:00
cfg . SetRootBlob ( " rootblobid " ) ;
Create ( std : : move ( cfg ) ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-10-23 00:04:03 +02:00
EXPECT_EQ ( " rootblobid " , loaded . config ( ) - > RootBlob ( ) ) ;
}
TEST_F ( CryConfigFileTest , RootBlob_SaveAndLoad ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
created . config ( ) - > SetRootBlob ( " rootblobid " ) ;
created . save ( ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-10-23 00:04:03 +02:00
EXPECT_EQ ( " rootblobid " , loaded . config ( ) - > RootBlob ( ) ) ;
}
TEST_F ( CryConfigFileTest , EncryptionKey_Init ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
EXPECT_EQ ( " " , created . config ( ) - > EncryptionKey ( ) ) ;
}
TEST_F ( CryConfigFileTest , EncryptionKey_CreateAndLoad ) {
2015-11-11 10:19:47 +01:00
CryConfig cfg = Config ( ) ;
2015-10-23 00:04:03 +02:00
cfg . SetEncryptionKey ( " encryptionkey " ) ;
Create ( std : : move ( cfg ) ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-10-23 00:04:03 +02:00
EXPECT_EQ ( " encryptionkey " , loaded . config ( ) - > EncryptionKey ( ) ) ;
}
TEST_F ( CryConfigFileTest , EncryptionKey_SaveAndLoad ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
created . config ( ) - > SetEncryptionKey ( " encryptionkey " ) ;
created . save ( ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-10-23 00:04:03 +02:00
EXPECT_EQ ( " encryptionkey " , loaded . config ( ) - > EncryptionKey ( ) ) ;
}
TEST_F ( CryConfigFileTest , Cipher_Init ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
2015-11-11 10:19:47 +01:00
EXPECT_EQ ( " aes-256-gcm " , created . config ( ) - > Cipher ( ) ) ;
2015-10-23 00:04:03 +02:00
}
TEST_F ( CryConfigFileTest , Cipher_CreateAndLoad ) {
2015-11-11 10:19:47 +01:00
CryConfig cfg = Config ( ) ;
cfg . SetCipher ( " twofish-128-cfb " ) ;
2015-10-23 00:04:03 +02:00
Create ( std : : move ( cfg ) ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-11-11 10:19:47 +01:00
EXPECT_EQ ( " twofish-128-cfb " , loaded . config ( ) - > Cipher ( ) ) ;
2015-10-23 00:04:03 +02:00
}
TEST_F ( CryConfigFileTest , Cipher_SaveAndLoad ) {
CryConfigFile created = CreateAndLoadEmpty ( ) ;
2015-11-11 10:19:47 +01:00
created . config ( ) - > SetCipher ( " twofish-128-cfb " ) ;
2015-10-23 00:04:03 +02:00
created . save ( ) ;
2015-10-24 19:35:37 +02:00
CryConfigFile loaded = Load ( ) . value ( ) ;
2015-11-11 10:19:47 +01:00
EXPECT_EQ ( " twofish-128-cfb " , loaded . config ( ) - > Cipher ( ) ) ;
2015-10-23 00:04:03 +02:00
}
2015-10-24 19:35:37 +02:00
//Test that the encrypted config file has the same size, no matter how big the plaintext config data.
TEST_F ( CryConfigFileTest , ConfigFileHasFixedSize ) {
TempFile file1 ( false ) ;
TempFile file2 ( false ) ;
2015-11-11 10:19:47 +01:00
//It is important to have different cipher name lengths here, because they're on the outer encryption level.
//So this ensures that there also is a padding happening on the outer encryption level.
CreateWithCipher ( " aes-128-gcm " , file1 ) ; // Short cipher name and short key
CreateWithCipher ( " twofish-256-cfb " , file2 ) ; // Long cipher name and long key
2015-10-24 19:35:37 +02:00
EXPECT_EQ ( bf : : file_size ( file1 . path ( ) ) , bf : : file_size ( file2 . path ( ) ) ) ;
}
2015-11-11 10:19:47 +01:00
TEST_F ( CryConfigFileTest , CanSaveAndLoadModififedCipher ) {
CreateWithCipher ( " aes-256-gcm " ) ;
CryConfigFile created = Load ( ) . value ( ) ;
EXPECT_EQ ( " aes-256-gcm " , created . config ( ) - > Cipher ( ) ) ;
created . config ( ) - > SetCipher ( " twofish-128-cfb " ) ;
created . save ( ) ;
CryConfigFile loaded = Load ( ) . value ( ) ;
EXPECT_EQ ( " twofish-128-cfb " , loaded . config ( ) - > Cipher ( ) ) ;
}