From 9b52666bdb06b566c3ca36016ce0d8d65fd5daec Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Fri, 10 Nov 2017 07:57:06 +0000 Subject: [PATCH] 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 --- include/PasswordRAMStore.h | 3 ++- src/PasswordRAMStore.cc | 11 +++++++++++ tests/test_PasswordRAMStore.cc | 22 ++++++---------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/PasswordRAMStore.h b/include/PasswordRAMStore.h index c8d67dad..5c771751 100644 --- a/include/PasswordRAMStore.h +++ b/include/PasswordRAMStore.h @@ -37,7 +37,7 @@ namespace GParted class PasswordRAMStore { friend class PasswordRAMStoreTest; // To allow unit testing PasswordRAMStoreTest class - // access to private erase_all() method. + // access to private methods. public: static bool insert( const Glib::ustring & key, const char * password ); @@ -46,6 +46,7 @@ public: private: static void erase_all(); + static const char * get_protected_mem(); }; } //GParted diff --git a/src/PasswordRAMStore.cc b/src/PasswordRAMStore.cc index 8125b15e..e84cf188 100644 --- a/src/PasswordRAMStore.cc +++ b/src/PasswordRAMStore.cc @@ -45,6 +45,7 @@ public: bool erase( const Glib::ustring & key ); const char * lookup( const Glib::ustring & key ); void erase_all(); + const char * get_protected_mem(); private: iterator find_key( const Glib::ustring & key ); @@ -192,6 +193,11 @@ void PWStore::erase_all() memset( protected_mem, '\0', ProtectedMemSize ); } +const char * PWStore::get_protected_mem() +{ + return protected_mem; +} + // The single password RAM store static PWStore single_pwstore; @@ -219,4 +225,9 @@ void PasswordRAMStore::erase_all() single_pwstore.erase_all(); } +const char * PasswordRAMStore::get_protected_mem() +{ + return single_pwstore.get_protected_mem(); +} + } //GParted diff --git a/tests/test_PasswordRAMStore.cc b/tests/test_PasswordRAMStore.cc index 08e54da8..7b8c41b8 100644 --- a/tests/test_PasswordRAMStore.cc +++ b/tests/test_PasswordRAMStore.cc @@ -16,9 +16,9 @@ /* Test PasswordRAMStore * - * WARNING: - * This unit testing calls the public API of PasswordRAMStore and also the private member. - * It also uses knowledge of the implementation to look through the API to the internals + * NOTE: + * As well as calling the public API of PasswordRAMStore this unit testing also accesses + * 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 * 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] -// Common test case initialisation discovering the underlying password store address. +// Common test case initialisation recording the underlying password store address. void PasswordRAMStoreTest::SetUpTestCase() { - const Glib::ustring key = "key-setup"; - bool success = PasswordRAMStore::insert( key, "" ); - 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"; + protected_mem = PasswordRAMStore::get_protected_mem(); + ASSERT_TRUE( protected_mem != NULL ) << __func__ << "(): No locked virtual memory for password RAM store"; } TEST_F( PasswordRAMStoreTest, Initialisation )