#include #include #include #include #include #include #include #include #include using namespace ::llarp; using namespace ::testing; static constexpr auto rcFile = "rc.signed"; static constexpr auto encFile = "encryption.key"; static constexpr auto transportFile = "transport.key"; static constexpr auto identFile = "identity.key"; struct KeyManagerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium > { // paranoid file guards for anything KeyManager might touch test::FileGuard m_rcFileGuard; test::FileGuard m_encFileGuard; test::FileGuard m_transportFileGuard; test::FileGuard m_identFileGuard; KeyManagerTest() : m_rcFileGuard(rcFile) , m_encFileGuard(encFile) , m_transportFileGuard(transportFile) , m_identFileGuard(identFile) { } /// generate a valid "rc.signed" file bool generateRcFile() { RouterContact rc; return rc.Write(rcFile); } }; TEST_F(KeyManagerTest, TestBackupFileByMoving_MovesExistingFiles) { fs::path p = test::randFilename(); ASSERT_FALSE(fs::exists(p)); // touch file std::fstream f; f.open(p.string(), std::ios::out); f.close(); KeyManager::backupFileByMoving(p.string()); ASSERT_FALSE(fs::exists(p)); fs::path moved = p.string() + ".0.bak"; ASSERT_TRUE(fs::exists(moved)); test::FileGuard guard(moved); }; TEST_F(KeyManagerTest, TestBackupFileByMoving_DoesntTouchNonExistentFiles) { fs::path p = test::randFilename(); ASSERT_FALSE(fs::exists(p)); KeyManager::backupFileByMoving(p.string()); ASSERT_FALSE(fs::exists(p)); fs::path moved = p.string() + ".0.bak"; ASSERT_FALSE(fs::exists(moved)); } TEST_F(KeyManagerTest, TestBackupFileByMoving_FailsIfBackupNamesAreExausted) { fs::path base = test::randFilename(); ASSERT_FALSE(fs::exists(base)); // touch file { std::fstream f; f.open(base.string(), std::ios::out); f.close(); } test::FileGuard guard(base); constexpr uint32_t numBackupNames = 9; std::vector guards; guards.reserve(numBackupNames); // generate backup files foo.0.bak through foo.9.bak for (uint32_t i=0; i