2015-10-29 19:34:36 +01:00
# include "testutils/CliTest.h"
2015-10-29 10:47:14 +01:00
2018-09-04 00:46:08 +02:00
# include <cpp-utils/system/env.h>
2015-10-29 19:34:36 +01:00
namespace bf = boost : : filesystem ;
2015-10-29 19:55:26 +01:00
using : : testing : : Values ;
using : : testing : : WithParamInterface ;
2016-01-25 15:01:34 +01:00
using : : testing : : Return ;
2016-02-21 01:34:21 +01:00
using : : testing : : _ ;
2015-10-29 19:55:26 +01:00
using std : : vector ;
2015-10-30 18:28:33 +01:00
using cpputils : : TempFile ;
2018-02-02 01:08:01 +01:00
using cryfs : : ErrorCode ;
2015-10-29 19:55:26 +01:00
struct TestConfig {
bool externalConfigfile ;
bool logIsNotStderr ;
2015-10-29 20:00:59 +01:00
bool runningInForeground ;
2015-10-29 19:55:26 +01:00
} ;
2015-10-29 15:52:49 +01:00
2015-10-29 19:34:36 +01:00
//Tests what happens if cryfs is run in the wrong environment, i.e. with a base directory that doesn't exist or similar
2015-10-29 19:55:26 +01:00
class CliTest_WrongEnvironment : public CliTest , public WithParamInterface < TestConfig > {
2015-10-29 19:34:36 +01:00
public :
2015-10-30 18:28:33 +01:00
void SetAllPermissions ( const bf : : path & dir ) {
bf : : permissions ( dir , bf : : owner_write | bf : : owner_read | bf : : owner_exe ) ;
}
void SetNoReadPermission ( const bf : : path & dir ) {
bf : : permissions ( dir , bf : : owner_write | bf : : owner_exe ) ;
}
void SetNoWritePermission ( const bf : : path & dir ) {
bf : : permissions ( dir , bf : : owner_read | bf : : owner_exe ) ;
}
void SetNoExePermission ( const bf : : path & dir ) {
bf : : permissions ( dir , bf : : owner_read | bf : : owner_write ) ;
}
void SetNoPermission ( const bf : : path & dir ) {
bf : : permissions ( dir , bf : : no_perms ) ;
2015-10-29 19:34:36 +01:00
}
void Test_Run_Success ( ) {
2015-10-30 22:24:18 +01:00
EXPECT_RUN_SUCCESS ( args ( ) , mountdir ) ;
2015-10-29 15:52:49 +01:00
}
2018-02-02 01:08:01 +01:00
void Test_Run_Error ( const char * expectedError , cryfs : : ErrorCode errorCode ) {
2015-10-29 15:52:49 +01:00
EXPECT_RUN_ERROR (
2015-10-29 19:55:26 +01:00
args ( ) ,
2018-02-02 01:08:01 +01:00
expectedError ,
errorCode
2015-10-29 15:52:49 +01:00
) ;
}
2018-09-07 07:44:23 +02:00
vector < string > args ( ) {
vector < string > result = { basedir . string ( ) , mountdir . string ( ) } ;
2015-10-29 19:55:26 +01:00
if ( GetParam ( ) . externalConfigfile ) {
result . push_back ( " --config " ) ;
2018-09-07 07:44:23 +02:00
result . push_back ( configfile . path ( ) . string ( ) ) ;
2015-10-29 19:55:26 +01:00
}
if ( GetParam ( ) . logIsNotStderr ) {
result . push_back ( " --logfile " ) ;
2018-09-07 07:44:23 +02:00
result . push_back ( logfile . path ( ) . string ( ) ) ;
2015-10-29 19:55:26 +01:00
}
2015-10-29 20:00:59 +01:00
if ( GetParam ( ) . runningInForeground ) {
result . push_back ( " -f " ) ;
}
2016-02-13 15:06:28 +01:00
// Test case should be non-interactive, so don't ask for cipher.
2015-11-02 21:20:10 +01:00
result . push_back ( " --cipher " ) ;
result . push_back ( " aes-256-gcm " ) ;
2015-10-29 19:55:26 +01:00
return result ;
2015-10-29 15:52:49 +01:00
}
2015-10-29 10:47:14 +01:00
} ;
2015-10-29 20:00:59 +01:00
INSTANTIATE_TEST_CASE_P ( DefaultParams , CliTest_WrongEnvironment , Values ( TestConfig ( { false , false , false } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( ExternalConfigfile , CliTest_WrongEnvironment , Values ( TestConfig ( { true , false , false } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( LogIsNotStderr , CliTest_WrongEnvironment , Values ( TestConfig ( { false , true , false } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( ExternalConfigfile_LogIsNotStderr , CliTest_WrongEnvironment , Values ( TestConfig ( { true , true , false } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( RunningInForeground , CliTest_WrongEnvironment , Values ( TestConfig ( { false , false , true } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( RunningInForeground_ExternalConfigfile , CliTest_WrongEnvironment , Values ( TestConfig ( { true , false , true } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( RunningInForeground_LogIsNotStderr , CliTest_WrongEnvironment , Values ( TestConfig ( { false , true , true } ) ) ) ;
INSTANTIATE_TEST_CASE_P ( RunningInForeground_ExternalConfigfile_LogIsNotStderr , CliTest_WrongEnvironment , Values ( TestConfig ( { true , true , true } ) ) ) ;
2015-10-29 19:55:26 +01:00
//Counter-Test. Test that it doesn't fail if we call it without an error condition.
TEST_P ( CliTest_WrongEnvironment , NoErrorCondition ) {
2015-11-03 22:02:04 +01:00
if ( ! GetParam ( ) . runningInForeground ) { return ; } // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
2015-10-29 19:34:36 +01:00
Test_Run_Success ( ) ;
2015-10-29 15:52:49 +01:00
}
2015-10-29 20:17:52 +01:00
TEST_P ( CliTest_WrongEnvironment , MountDirIsBaseDir ) {
mountdir = basedir ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 18: base directory can't be inside the mount directory " , ErrorCode : : BaseDirInsideMountDir ) ;
2015-10-30 17:23:08 +01:00
}
bf : : path make_relative ( const bf : : path & path ) {
bf : : path result ;
bf : : path cwd = bf : : current_path ( ) ;
for ( auto iter = + + cwd . begin ( ) ; iter ! = cwd . end ( ) ; + + iter ) {
result / = " .. " ;
}
2018-09-07 07:44:38 +02:00
result / = path . relative_path ( ) ;
2015-10-30 17:23:08 +01:00
return result ;
}
TEST_P ( CliTest_WrongEnvironment , MountDirIsBaseDir_MountDirRelative ) {
mountdir = make_relative ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 18: base directory can't be inside the mount directory " , ErrorCode : : BaseDirInsideMountDir ) ;
2015-10-30 17:23:08 +01:00
}
TEST_P ( CliTest_WrongEnvironment , MountDirIsBaseDir_BaseDirRelative ) {
mountdir = basedir ;
basedir = make_relative ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 18: base directory can't be inside the mount directory " , ErrorCode : : BaseDirInsideMountDir ) ;
2015-10-30 17:23:08 +01:00
}
TEST_P ( CliTest_WrongEnvironment , MountDirIsBaseDir_BothRelative ) {
basedir = make_relative ( basedir ) ;
mountdir = basedir ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 18: base directory can't be inside the mount directory " , ErrorCode : : BaseDirInsideMountDir ) ;
2015-10-29 20:17:52 +01:00
}
2015-10-29 19:55:26 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_DoesntExist ) {
2015-10-30 17:23:08 +01:00
_basedir . remove ( ) ;
2016-02-21 01:34:21 +01:00
// ON_CALL and not EXPECT_CALL, because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find base directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( false ) ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: base directory not found " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-29 10:47:14 +01:00
}
2015-10-29 15:52:49 +01:00
2016-02-21 01:34:21 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_DoesntExist_Noninteractive ) {
_basedir . remove ( ) ;
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find base directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( true ) ) ;
2018-09-04 00:46:08 +02:00
cpputils : : setenv ( " CRYFS_FRONTEND " , " noninteractive " ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: base directory not found " , ErrorCode : : InaccessibleBaseDir ) ;
2018-09-04 00:46:08 +02:00
cpputils : : unsetenv ( " CRYFS_FRONTEND " ) ;
2016-02-21 01:34:21 +01:00
}
2016-01-25 15:01:34 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_DoesntExist_Create ) {
if ( ! GetParam ( ) . runningInForeground ) { return ; } // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
_basedir . remove ( ) ;
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find base directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( true ) ) ;
2016-01-25 15:01:34 +01:00
Test_Run_Success ( ) ;
EXPECT_TRUE ( bf : : exists ( _basedir . path ( ) ) & & bf : : is_directory ( _basedir . path ( ) ) ) ;
}
2015-10-30 18:28:33 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_IsNotDirectory ) {
TempFile basedirfile ;
basedir = basedirfile . path ( ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: base directory is not a directory " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-30 18:28:33 +01:00
}
TEST_P ( CliTest_WrongEnvironment , BaseDir_AllPermissions ) {
2015-11-03 22:02:04 +01:00
if ( ! GetParam ( ) . runningInForeground ) { return ; } // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
2015-10-30 18:28:33 +01:00
//Counter-Test. Test it doesn't fail if permissions are there.
SetAllPermissions ( basedir ) ;
Test_Run_Success ( ) ;
}
2018-09-08 13:26:32 +02:00
// boost::filesystem doesn't set permissions on Windows correctly
# if !defined(_MSC_VER)
2015-10-29 19:55:26 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_NoReadPermission ) {
2015-10-30 18:28:33 +01:00
SetNoReadPermission ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: Could not read from base directory " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-29 15:52:49 +01:00
}
2018-09-08 13:26:32 +02:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_NoExePermission ) {
SetNoExePermission ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: Could not write to base directory " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-29 15:52:49 +01:00
}
2018-09-08 13:26:32 +02:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_NoWritePermission ) {
SetNoWritePermission ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: Could not write to base directory " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-29 15:52:49 +01:00
}
2015-10-29 19:55:26 +01:00
TEST_P ( CliTest_WrongEnvironment , BaseDir_NoPermission ) {
2015-10-30 18:28:33 +01:00
SetNoPermission ( basedir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 16: Could not write to base directory " , ErrorCode : : InaccessibleBaseDir ) ;
2015-10-29 15:52:49 +01:00
}
2018-09-08 13:26:32 +02:00
# endif
2015-10-30 22:01:42 +01:00
TEST_P ( CliTest_WrongEnvironment , MountDir_DoesntExist ) {
_mountdir . remove ( ) ;
2016-02-21 01:34:21 +01:00
// ON_CALL and not EXPECT_CALL, because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find mount directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( false ) ) ;
2018-02-02 01:08:01 +01:00
Test_Run_Error ( " mount directory not found " , ErrorCode : : InaccessibleMountDir ) ;
2016-02-21 01:34:21 +01:00
}
TEST_P ( CliTest_WrongEnvironment , MountDir_DoesntExist_Noninteractive ) {
_mountdir . remove ( ) ;
// We can't set an EXPECT_CALL().Times(0), because this is a death test (i.e. it is forked) and gmock EXPECT_CALL in fork children don't report to parents.
// So we set a default answer that shouldn't crash and check it's not called by checking that it crashes.
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find base directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( true ) ) ;
2018-09-04 00:46:08 +02:00
cpputils : : setenv ( " CRYFS_FRONTEND " , " noninteractive " ) ;
2018-02-02 01:08:01 +01:00
Test_Run_Error ( " mount directory not found " , ErrorCode : : InaccessibleMountDir ) ;
2018-09-04 00:46:08 +02:00
cpputils : : unsetenv ( " CRYFS_FRONTEND " ) ;
2015-10-30 22:01:42 +01:00
}
2016-01-25 15:01:34 +01:00
TEST_P ( CliTest_WrongEnvironment , MountDir_DoesntExist_Create ) {
if ( ! GetParam ( ) . runningInForeground ) { return ; } // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
_mountdir . remove ( ) ;
2016-09-25 02:50:28 +02:00
ON_CALL ( * console , askYesNo ( " Could not find mount directory. Do you want to create it? " , _ ) ) . WillByDefault ( Return ( true ) ) ;
2016-01-25 15:01:34 +01:00
Test_Run_Success ( ) ;
EXPECT_TRUE ( bf : : exists ( _mountdir . path ( ) ) & & bf : : is_directory ( _mountdir . path ( ) ) ) ;
}
2015-10-30 22:01:42 +01:00
TEST_P ( CliTest_WrongEnvironment , MountDir_IsNotDirectory ) {
TempFile mountdirfile ;
mountdir = mountdirfile . path ( ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 17: mount directory is not a directory " , ErrorCode : : InaccessibleMountDir ) ;
2015-10-30 22:01:42 +01:00
}
TEST_P ( CliTest_WrongEnvironment , MountDir_AllPermissions ) {
2015-11-03 22:02:04 +01:00
if ( ! GetParam ( ) . runningInForeground ) { return ; } // TODO Make this work also if run in background (see CliTest::EXPECT_RUN_SUCCESS)
2015-10-30 22:01:42 +01:00
//Counter-Test. Test it doesn't fail if permissions are there.
SetAllPermissions ( mountdir ) ;
Test_Run_Success ( ) ;
}
2018-09-08 13:26:32 +02:00
// boost::filesystem doesn't set permissions on Windows correctly
# if !defined(_MSC_VER)
2015-10-30 22:01:42 +01:00
TEST_P ( CliTest_WrongEnvironment , MountDir_NoReadPermission ) {
SetNoReadPermission ( mountdir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 17: Could not read from mount directory " , ErrorCode : : InaccessibleMountDir ) ;
2015-10-30 22:01:42 +01:00
}
2018-09-08 13:26:32 +02:00
TEST_P ( CliTest_WrongEnvironment , MountDir_NoExePermission ) {
SetNoExePermission ( mountdir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 17: Could not write to mount directory " , ErrorCode : : InaccessibleMountDir ) ;
2015-10-30 22:01:42 +01:00
}
2018-09-08 13:26:32 +02:00
TEST_P ( CliTest_WrongEnvironment , MountDir_NoWritePermission ) {
SetNoWritePermission ( mountdir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 17: Could not write to mount directory " , ErrorCode : : InaccessibleMountDir ) ;
2015-10-30 22:01:42 +01:00
}
TEST_P ( CliTest_WrongEnvironment , MountDir_NoPermission ) {
SetNoPermission ( mountdir ) ;
2018-12-19 06:34:20 +01:00
Test_Run_Error ( " Error 17: Could not write to mount directory " , ErrorCode : : InaccessibleMountDir ) ;
2015-10-30 22:01:42 +01:00
}
2018-09-08 13:26:32 +02:00
# endif