diff --git a/src/libLocalDescriptorCache/DescDbProxy.cpp b/src/libLocalDescriptorCache/DescDbProxy.cpp index e274398..4afbfbb 100644 --- a/src/libLocalDescriptorCache/DescDbProxy.cpp +++ b/src/libLocalDescriptorCache/DescDbProxy.cpp @@ -301,10 +301,9 @@ std::string DescDbProxy::prepareCacheFile() return dbFilePath; } -void DescDbProxy::getDescriptors(const std::string &f_hostAddress) -{ - std::string cacheFilePath = prepareCacheFile(); - +/// 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 std::fstream input; input.open(cacheFilePath, std::ios::in | std::ios::binary); @@ -317,10 +316,33 @@ 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 { - std::cerr << "Failed to parse local Descriptor Cache." << std::endl; - exit(EXIT_FAILURE); + 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 ) + { + // 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 ) + { + 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; 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 ##############################################################################