From ef075a53dd25ac61a8055bd9a5946bfe0f7072b2 Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Mon, 9 Dec 2019 12:29:33 -0700 Subject: [PATCH] Implement KeyManager tests --- test/CMakeLists.txt | 1 + test/crypto/test_llarp_key_manager.cpp | 191 +++++++++++++++++++++++++ test/test_llarp_router.cpp | 95 ------------ test/test_util.hpp | 4 +- 4 files changed, 194 insertions(+), 97 deletions(-) create mode 100644 test/crypto/test_llarp_key_manager.cpp delete mode 100644 test/test_llarp_router.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 37ebcec9f..3ba964c1f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,6 +5,7 @@ list(APPEND TEST_SRC config/test_llarp_config_ini.cpp crypto/test_llarp_crypto_types.cpp crypto/test_llarp_crypto.cpp + crypto/test_llarp_key_manager.cpp dht/test_llarp_dht_bucket.cpp dht/test_llarp_dht_explorenetworkjob.cpp dht/test_llarp_dht_kademlia.cpp diff --git a/test/crypto/test_llarp_key_manager.cpp b/test/crypto/test_llarp_key_manager.cpp new file mode 100644 index 000000000..450e2e430 --- /dev/null +++ b/test/crypto/test_llarp_key_manager.cpp @@ -0,0 +1,191 @@ +#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 - -#include -#include -#include - -#include -#include - -#include -#include - -using namespace ::llarp; -using namespace ::testing; - -/* - * TODO: reimplement - * -using FindOrCreateFunc = std::function< bool(const fs::path &, SecretKey &) >; - -struct FindOrCreate : public test::LlarpTest<>, - public WithParamInterface< FindOrCreateFunc > -{ -}; - -// Concerns -// - file missing -// - file empty -// - happy path - -TEST_P(FindOrCreate, find_file_missing) -{ - // File missing. Should create a new file - SecretKey key; - fs::path p = test::randFilename(); - ASSERT_FALSE(fs::exists(fs::status(p))); - - test::FileGuard guard(p); - - EXPECT_CALL(m_crypto, encryption_keygen(_)) - .Times(AtMost(1)) - .WillRepeatedly(Invoke(&test::keygen< SecretKey >)); - - EXPECT_CALL(m_crypto, identity_keygen(_)) - .Times(AtMost(1)) - .WillRepeatedly(Invoke(&test::keygen< SecretKey >)); - - ASSERT_TRUE(GetParam()(p, key)); - ASSERT_TRUE(fs::exists(fs::status(p))); - ASSERT_FALSE(key.IsZero()); -} - -TEST_P(FindOrCreate, find_file_empty) -{ - // File empty. - SecretKey key; - fs::path p = test::randFilename(); - ASSERT_FALSE(fs::exists(fs::status(p))); - - std::fstream f; - f.open(p.string(), std::ios::out); - f.close(); - - test::FileGuard guard(p); - - ASSERT_FALSE(GetParam()(p, key)); - // Verify we didn't delete an invalid file - ASSERT_TRUE(fs::exists(fs::status(p))); -} - -TEST_P(FindOrCreate, happy_path) -{ - // happy path. - SecretKey key; - fs::path p = test::randFilename(); - ASSERT_FALSE(fs::exists(fs::status(p))); - - std::ofstream f; - f.open(p.string(), std::ios::out); - std::fill_n(std::ostream_iterator< byte_t >(f), key.size(), 0x20); - f.close(); - - test::FileGuard guard(p); - - ASSERT_TRUE(GetParam()(p, key)); - // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(p))); -} - -FindOrCreateFunc findOrCreateFunc[] = {llarp_findOrCreateEncryption, - llarp_findOrCreateIdentity}; - -INSTANTIATE_TEST_CASE_P(TestRouter, FindOrCreate, - ::testing::ValuesIn(findOrCreateFunc), ); -*/ diff --git a/test/test_util.hpp b/test/test_util.hpp index c084514d6..4433789ab 100644 --- a/test/test_util.hpp +++ b/test/test_util.hpp @@ -25,9 +25,9 @@ namespace llarp struct FileGuard { - const fs::path &p; + const fs::path p; - explicit FileGuard(const fs::path &_p) : p(_p) + FileGuard(const fs::path &_p) : p(_p) { }