Skip to content

Commit

Permalink
Merge pull request #148 from abb3r/fixCacheFileParsingError
Browse files Browse the repository at this point in the history
retry parsing after removing the cache file.
  • Loading branch information
rainerschoe authored Nov 20, 2023
2 parents 9d7a9d6 + 664c3b5 commit e3d3f6c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/libLocalDescriptorCache/DescDbProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tests/functionTests/cacheFunctionTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
##############################################################################
Expand Down

0 comments on commit e3d3f6c

Please sign in to comment.