Simplify obtaining address of password memory for unit tests (#795617)

Use private access into the PasswordRAMStore class to directly obtain
the address of the locked memory, rather than inferring it from the
address of the first stored password.  This simplifies
PasswordRAMStoreTest::SetUpTestCase() and avoids encoding most of the
implementation knowledge that the first password will be stored at the
start of the protected memory.

Bug 795617 - Implement opening and closing of LUKS mappings
This commit is contained in:
Mike Fleetwood 2017-11-10 07:57:06 +00:00 committed by Curtis Gedak
parent d2a2ebe4a1
commit 9b52666bdb
3 changed files with 19 additions and 17 deletions

View File

@ -37,7 +37,7 @@ namespace GParted
class PasswordRAMStore class PasswordRAMStore
{ {
friend class PasswordRAMStoreTest; // To allow unit testing PasswordRAMStoreTest class friend class PasswordRAMStoreTest; // To allow unit testing PasswordRAMStoreTest class
// access to private erase_all() method. // access to private methods.
public: public:
static bool insert( const Glib::ustring & key, const char * password ); static bool insert( const Glib::ustring & key, const char * password );
@ -46,6 +46,7 @@ public:
private: private:
static void erase_all(); static void erase_all();
static const char * get_protected_mem();
}; };
} //GParted } //GParted

View File

@ -45,6 +45,7 @@ public:
bool erase( const Glib::ustring & key ); bool erase( const Glib::ustring & key );
const char * lookup( const Glib::ustring & key ); const char * lookup( const Glib::ustring & key );
void erase_all(); void erase_all();
const char * get_protected_mem();
private: private:
iterator find_key( const Glib::ustring & key ); iterator find_key( const Glib::ustring & key );
@ -192,6 +193,11 @@ void PWStore::erase_all()
memset( protected_mem, '\0', ProtectedMemSize ); memset( protected_mem, '\0', ProtectedMemSize );
} }
const char * PWStore::get_protected_mem()
{
return protected_mem;
}
// The single password RAM store // The single password RAM store
static PWStore single_pwstore; static PWStore single_pwstore;
@ -219,4 +225,9 @@ void PasswordRAMStore::erase_all()
single_pwstore.erase_all(); single_pwstore.erase_all();
} }
const char * PasswordRAMStore::get_protected_mem()
{
return single_pwstore.get_protected_mem();
}
} //GParted } //GParted

View File

@ -16,9 +16,9 @@
/* Test PasswordRAMStore /* Test PasswordRAMStore
* *
* WARNING: * NOTE:
* This unit testing calls the public API of PasswordRAMStore and also the private member. * As well as calling the public API of PasswordRAMStore this unit testing also accesses
* It also uses knowledge of the implementation to look through the API to the internals * the private members of PasswordRAMStore and uses knowledge of the implementation,
* making this white box testing. This is so that the hidden behaviour of zeroing * making this white box testing. This is so that the hidden behaviour of zeroing
* password storing memory before and after use can be tested. * password storing memory before and after use can be tested.
* *
@ -97,21 +97,11 @@ const char * PasswordRAMStoreTest::protected_mem = NULL;
const size_t ProtectedMemSize = 4096; // [Implementation knowledge: size] const size_t ProtectedMemSize = 4096; // [Implementation knowledge: size]
// Common test case initialisation discovering the underlying password store address. // Common test case initialisation recording the underlying password store address.
void PasswordRAMStoreTest::SetUpTestCase() void PasswordRAMStoreTest::SetUpTestCase()
{ {
const Glib::ustring key = "key-setup"; protected_mem = PasswordRAMStore::get_protected_mem();
bool success = PasswordRAMStore::insert( key, "" ); ASSERT_TRUE( protected_mem != NULL ) << __func__ << "(): No locked virtual memory for password RAM store";
ASSERT_TRUE( success ) << __func__ << "(): Insert \"" << key << "\" password failed";
// First password is stored at the start of the locked memory.
// [Implementation knowledge: locked memory layout]
protected_mem = PasswordRAMStore::lookup( key );
ASSERT_TRUE( protected_mem != NULL ) << __func__
<< "(): Find \"" << key << "\" password failed";
success = PasswordRAMStore::erase( key );
ASSERT_TRUE( success ) << __func__ << "(): Erase \"" << key << "\" password failed";
} }
TEST_F( PasswordRAMStoreTest, Initialisation ) TEST_F( PasswordRAMStoreTest, Initialisation )