From 1c30140838dc6ccc60c2b3a2d2a62971387a5542 Mon Sep 17 00:00:00 2001 From: Rahman Abber Tahir Date: Fri, 17 Nov 2023 10:46:01 +0100 Subject: [PATCH 1/4] retry parsing after removing the cache file. Signed-off-by: Rahman Abber Tahir --- src/libLocalDescriptorCache/DescDbProxy.cpp | 31 ++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/libLocalDescriptorCache/DescDbProxy.cpp b/src/libLocalDescriptorCache/DescDbProxy.cpp index a8e25ed..7a5b4a3 100644 --- a/src/libLocalDescriptorCache/DescDbProxy.cpp +++ b/src/libLocalDescriptorCache/DescDbProxy.cpp @@ -301,10 +301,8 @@ std::string DescDbProxy::prepareCacheFile() return dbFilePath; } -void DescDbProxy::getDescriptors(const std::string &f_hostAddress) -{ - std::string cacheFilePath = prepareCacheFile(); - +static bool getDbFileFromCacheFile(const std::string& cacheFilePath, localDescDb::DescriptorDb& file) +{ // Import .prot file std::fstream input; input.open(cacheFilePath, std::ios::in | std::ios::binary); @@ -317,10 +315,29 @@ void DescDbProxy::getDescriptors(const std::string &f_hostAddress) } //std::cerr << "Cannot open file, file does not exist. Creating new file.." << std::endl; } - else if (!dbFile.ParseFromIstream(&input)) + else + { + return file.ParseFromIstream(&input); + } + return true; +} + +void DescDbProxy::getDescriptors(const std::string &f_hostAddress) +{ + localDescDb::DescriptorDb dbFile; + std::string cacheFilePath = prepareCacheFile(); + if( getDbFileFromCacheFile(cacheFilePath, dbFile) == false ) { - std::cerr << "Failed to parse local Descriptor Cache." << std::endl; - exit(EXIT_FAILURE); + localDescDb::DescriptorDb newDbFile; //new file to ensure there are no side-effects from last parsing failure. + std::filesystem::remove(cacheFilePath); //remove cache file if failed and try again. + + if( getDbFileFromCacheFile(cacheFilePath, dbFile) == false ) + { + std::cerr << "Failed to parse local Descriptor Cache." << std::endl; + exit(EXIT_FAILURE); + } + + dbFile = newDbFile; //overwrite the main dbFile. } std::string gwhisperBuildVersion = GWHISPER_BUILD_VERSION; From 76a5f0d4ae0f46f85ac8a94824980b2e2e8b4217 Mon Sep 17 00:00:00 2001 From: Rahman Abber Tahir Date: Fri, 17 Nov 2023 13:16:35 +0100 Subject: [PATCH 2/4] add functional test for scenario: invalid descriptor cache. Signed-off-by: Rahman Abber Tahir --- tests/functionTests/cacheFunctionTests.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/functionTests/cacheFunctionTests.txt b/tests/functionTests/cacheFunctionTests.txt index 02047bc..1093f5b 100644 --- a/tests/functionTests/cacheFunctionTests.txt +++ b/tests/functionTests/cacheFunctionTests.txt @@ -42,6 +42,28 @@ RPC succeeded :D #END_CMD #END_TEST +#START_TEST RPC with invalid cache +#EXEC_CMD +rm ~/.cache/gwhisper/DescriptorCache.bin +#END_CMD +#EXEC_CMD +touch ~/.cache/gwhisper/DescriptorCache.bin +#END_CMD +#EXEC_CMD +echo "212123132yx1c56y465wad6sa4dwq7e98wq7w98e7qw89jdsakjdksal" >> ~/.cache/gwhisper/DescriptorCache.bin +#END_CMD +#EXEC_CMD +cat ~/.cache/gwhisper/DescriptorCache.bin +212123132yx1c56y465wad6sa4dwq7e98wq7w98e7qw89jdsakjdksal +#END_CMD +#EXEC_CMD +@@CMD@@ --rpcTimeoutMilliseconds=125000 localhost:50051 examples.ScalarTypeRpcs negateBool m_bool=0 +/.* Received message: +| m_bool = true +RPC succeeded :D +#END_CMD +#END_TEST + ############################################################################## # secure RPC with cache ############################################################################## From cdd2f9fe77d3a509dc94ac9896ca5d678b54d223 Mon Sep 17 00:00:00 2001 From: Rahman Abber Tahir Date: Fri, 17 Nov 2023 16:59:02 +0100 Subject: [PATCH 3/4] use new db file in next try. Signed-off-by: Rahman Abber Tahir --- src/libLocalDescriptorCache/DescDbProxy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libLocalDescriptorCache/DescDbProxy.cpp b/src/libLocalDescriptorCache/DescDbProxy.cpp index 7a5b4a3..9cb55e2 100644 --- a/src/libLocalDescriptorCache/DescDbProxy.cpp +++ b/src/libLocalDescriptorCache/DescDbProxy.cpp @@ -331,7 +331,7 @@ void DescDbProxy::getDescriptors(const std::string &f_hostAddress) localDescDb::DescriptorDb newDbFile; //new file to ensure there are no side-effects from last parsing failure. std::filesystem::remove(cacheFilePath); //remove cache file if failed and try again. - if( getDbFileFromCacheFile(cacheFilePath, dbFile) == false ) + if( getDbFileFromCacheFile(cacheFilePath, newDbFile) == false ) { std::cerr << "Failed to parse local Descriptor Cache." << std::endl; exit(EXIT_FAILURE); From 664c3b563a6887dcd2ff788ad8fba5530a19e14c Mon Sep 17 00:00:00 2001 From: Rahman Abber Tahir Date: Mon, 20 Nov 2023 18:35:18 +0100 Subject: [PATCH 4/4] added comments and function description. Signed-off-by: Rahman Abber Tahir --- src/libLocalDescriptorCache/DescDbProxy.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libLocalDescriptorCache/DescDbProxy.cpp b/src/libLocalDescriptorCache/DescDbProxy.cpp index 9cb55e2..417928f 100644 --- a/src/libLocalDescriptorCache/DescDbProxy.cpp +++ b/src/libLocalDescriptorCache/DescDbProxy.cpp @@ -301,6 +301,7 @@ std::string DescDbProxy::prepareCacheFile() return dbFilePath; } +/// returns false if file was found but could not be parsed. true otherwise. static bool getDbFileFromCacheFile(const std::string& cacheFilePath, localDescDb::DescriptorDb& file) { // Import .prot file @@ -328,8 +329,12 @@ void DescDbProxy::getDescriptors(const std::string &f_hostAddress) std::string cacheFilePath = prepareCacheFile(); if( getDbFileFromCacheFile(cacheFilePath, dbFile) == false ) { - localDescDb::DescriptorDb newDbFile; //new file to ensure there are no side-effects from last parsing failure. - std::filesystem::remove(cacheFilePath); //remove cache file if failed and try again. + // Parsing the cache file failed. re-instantiating the cache: + // remove cache file if failed and try again. + std::filesystem::remove(cacheFilePath); + + // new file to ensure there are no side-effects from last parsing failure. + localDescDb::DescriptorDb newDbFile; if( getDbFileFromCacheFile(cacheFilePath, newDbFile) == false ) {