lokinet/test/crypto/test_llarp_key_manager.cpp

177 lines
4.2 KiB
C++
Raw Normal View History

2023-01-09 18:47:41 +01:00
#include "llarp_test.hpp"
#include "test_util.hpp"
2019-12-09 20:29:33 +01:00
2023-01-09 18:47:41 +01:00
#include <llarp/config/key_manager.hpp>
#include <llarp/crypto/crypto.hpp>
#include <llarp/crypto/crypto_libsodium.hpp>
2019-12-09 20:29:33 +01:00
#include <functional>
#include <random>
#include <string>
2021-03-01 22:07:32 +01:00
#include <catch2/catch.hpp>
2019-12-09 20:29:33 +01:00
using namespace ::llarp;
2021-03-01 22:07:32 +01:00
struct KeyManagerTest : public test::LlarpTest<llarp::sodium::CryptoLibSodium>
2019-12-09 20:29:33 +01:00
{
// 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(our_rc_filename)
, m_encFileGuard(our_enc_key_filename)
, m_transportFileGuard(our_transport_key_filename)
, m_identFileGuard(our_identity_filename)
2021-03-01 22:07:32 +01:00
{}
2019-12-09 20:29:33 +01:00
/// generate a valid "rc.signed" file
bool
generateRcFile()
{
RouterContact rc;
return rc.Write(our_rc_filename);
2019-12-09 20:29:33 +01:00
}
};
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving moves existing files")
2019-12-09 20:29:33 +01:00
{
fs::path p = test::randFilename();
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(p));
2019-12-09 20:29:33 +01:00
// touch file
std::fstream f;
f.open(p.string(), std::ios::out);
f.close();
KeyManager::backupFileByMoving(p.string());
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(p));
2019-12-09 20:29:33 +01:00
fs::path moved = p.string() + ".0.bak";
2021-03-01 22:07:32 +01:00
REQUIRE(fs::exists(moved));
2019-12-09 20:29:33 +01:00
test::FileGuard guard(moved);
};
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving doesnt touch non existent files")
2019-12-09 20:29:33 +01:00
{
fs::path p = test::randFilename();
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(p));
2019-12-09 20:29:33 +01:00
KeyManager::backupFileByMoving(p.string());
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(p));
2019-12-09 20:29:33 +01:00
fs::path moved = p.string() + ".0.bak";
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(moved));
2019-12-09 20:29:33 +01:00
}
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving fails if backup names are exausted")
2019-12-09 20:29:33 +01:00
{
fs::path base = test::randFilename();
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(base));
2019-12-09 20:29:33 +01:00
// 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
2021-03-01 22:07:32 +01:00
for (uint32_t i = 0; i < numBackupNames; ++i)
2019-12-09 20:29:33 +01:00
{
2021-03-01 22:07:32 +01:00
fs::path p = base.string() + "." + std::to_string(i) + ".bak";
2019-12-09 20:29:33 +01:00
std::fstream f;
f.open(p.string(), std::ios::out);
f.close();
guards.emplace_back(p);
2021-03-01 22:07:32 +01:00
REQUIRE(fs::exists(p));
2019-12-09 20:29:33 +01:00
}
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(KeyManager::backupFileByMoving(base.string()));
2019-12-09 20:29:33 +01:00
};
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Initialize makes keyfiles")
2019-12-09 20:29:33 +01:00
{
llarp::Config conf{fs::current_path()};
conf.Load();
2019-12-09 20:29:33 +01:00
KeyManager keyManager;
2021-03-01 22:07:32 +01:00
REQUIRE(keyManager.initialize(conf, true, true));
2019-12-09 20:29:33 +01:00
// KeyManager doesn't generate RC file, but should generate others
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(our_rc_filename));
2019-12-09 20:29:33 +01:00
2021-03-01 22:07:32 +01:00
REQUIRE(fs::exists(our_enc_key_filename));
REQUIRE(fs::exists(our_transport_key_filename));
REQUIRE(fs::exists(our_identity_filename));
2019-12-09 20:29:33 +01:00
}
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Initialize respects gen flag")
2019-12-09 20:29:33 +01:00
{
llarp::Config conf{fs::current_path()};
conf.Load();
2021-03-01 22:07:32 +01:00
2019-12-09 20:29:33 +01:00
KeyManager keyManager;
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(keyManager.initialize(conf, false, true));
2019-12-09 20:29:33 +01:00
// KeyManager shouldn't have touched any files without (genIfAbsent == true)
2021-03-01 22:07:32 +01:00
REQUIRE_FALSE(fs::exists(our_rc_filename));
REQUIRE_FALSE(fs::exists(our_enc_key_filename));
REQUIRE_FALSE(fs::exists(our_transport_key_filename));
REQUIRE_FALSE(fs::exists(our_identity_filename));
2019-12-09 20:29:33 +01:00
}
2021-03-01 22:07:32 +01:00
TEST_CASE_METHOD(KeyManagerTest, "Initialize detects bad rc file")
2019-12-09 20:29:33 +01:00
{
llarp::Config conf{fs::current_path()};
conf.Load();
2021-03-01 22:07:32 +01:00
conf.lokid.whitelistRouters = false;
2019-12-09 20:29:33 +01:00
std::fstream f;
f.open(our_rc_filename, std::ios::out);
2019-12-09 20:29:33 +01:00
f << "bad_rc_file";
f.close();
KeyManager keyManager;
2021-03-01 22:07:32 +01:00
REQUIRE(keyManager.initialize(conf, true, true));
REQUIRE(keyManager.needBackup());
2019-12-09 20:29:33 +01:00
2021-03-01 22:07:32 +01:00
REQUIRE(fs::exists(our_enc_key_filename));
REQUIRE(fs::exists(our_transport_key_filename));
REQUIRE(fs::exists(our_identity_filename));
2019-12-09 20:29:33 +01:00
// test that keys are sane
SecretKey key;
key.Zero();
2021-03-01 22:07:32 +01:00
REQUIRE(key.LoadFromFile(our_enc_key_filename));
REQUIRE_FALSE(key.IsZero());
2019-12-09 20:29:33 +01:00
key.Zero();
2021-03-01 22:07:32 +01:00
REQUIRE(key.LoadFromFile(our_transport_key_filename));
REQUIRE_FALSE(key.IsZero());
2019-12-09 20:29:33 +01:00
key.Zero();
2021-03-01 22:07:32 +01:00
REQUIRE(key.LoadFromFile(our_identity_filename));
REQUIRE_FALSE(key.IsZero());
2019-12-09 20:29:33 +01:00
}