Implement KeyManager tests

pull/956/head
Stephen Shelton 5 years ago
parent 512a350783
commit ef075a53dd

@ -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

@ -0,0 +1,191 @@
#include <config/key_manager.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <functional>
#include <random>
#include <string>
#include <test_util.hpp>
#include <gtest/gtest.h>
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<test::FileGuard> guards;
guards.reserve(numBackupNames);
// generate backup files foo.0.bak through foo.9.bak
for (uint32_t i=0; i<numBackupNames; ++i)
{
fs::path p = base.string() +"."+ std::to_string(i) +".bak";
std::fstream f;
f.open(p.string(), std::ios::out);
f.close();
guards.emplace_back(p);
ASSERT_TRUE(fs::exists(p));
}
ASSERT_FALSE(KeyManager::backupFileByMoving(base.string()));
};
TEST_F(KeyManagerTest, EnsureDefaultConfNames)
{
llarp::Config conf;
// the default config filenames will suffice, this exists as sanity check to
// protect against the assumptions made below
ASSERT_TRUE(conf.router.ourRcFile() == rcFile);
ASSERT_TRUE(conf.router.encryptionKeyfile() == encFile);
ASSERT_TRUE(conf.router.transportKeyfile() == transportFile);
ASSERT_TRUE(conf.router.identKeyfile() == identFile);
}
TEST_F(KeyManagerTest, TestInitialize_MakesKeyfiles)
{
llarp::Config conf;
KeyManager keyManager;
ASSERT_TRUE(keyManager.initialize(conf, true));
// KeyManager doesn't generate RC file, but should generate others
ASSERT_FALSE(fs::exists(rcFile));
ASSERT_TRUE(fs::exists(encFile));
ASSERT_TRUE(fs::exists(transportFile));
ASSERT_TRUE(fs::exists(identFile));
}
TEST_F(KeyManagerTest, TestInitialize_RespectsGenFlag)
{
llarp::Config conf;
KeyManager keyManager;
ASSERT_FALSE(keyManager.initialize(conf, false));
// KeyManager shouldn't have touched any files without (genIfAbsent == true)
ASSERT_FALSE(fs::exists(rcFile));
ASSERT_FALSE(fs::exists(encFile));
ASSERT_FALSE(fs::exists(transportFile));
ASSERT_FALSE(fs::exists(identFile));
}
TEST_F(KeyManagerTest, TestInitialize_DetectsBadRcFile)
{
llarp::Config conf;
std::fstream f;
f.open(rcFile, std::ios::out);
f << "bad_rc_file";
f.close();
KeyManager keyManager;
ASSERT_TRUE(keyManager.initialize(conf, true));
ASSERT_TRUE(keyManager.needBackup());
ASSERT_TRUE(fs::exists(encFile));
ASSERT_TRUE(fs::exists(transportFile));
ASSERT_TRUE(fs::exists(identFile));
// test that keys are sane
SecretKey key;
key.Zero();
ASSERT_TRUE(key.LoadFromFile(encFile));
ASSERT_FALSE(key.IsZero());
key.Zero();
ASSERT_TRUE(key.LoadFromFile(transportFile));
ASSERT_FALSE(key.IsZero());
key.Zero();
ASSERT_TRUE(key.LoadFromFile(identFile));
ASSERT_FALSE(key.IsZero());
}

@ -1,95 +0,0 @@
#include <router/router.hpp>
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <functional>
#include <random>
#include <test_util.hpp>
#include <gtest/gtest.h>
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), );
*/

@ -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)
{
}

Loading…
Cancel
Save