lokinet/test/regress/2020-06-08-key-backup-bug.cpp

78 lines
2.7 KiB
C++
Raw Normal View History

2020-06-08 12:42:10 +00:00
#include <llarp.hpp>
#include <config/config.hpp>
#include <router/abstractrouter.hpp>
#include <service/context.hpp>
#include <catch2/catch.hpp>
llarp::RuntimeOptions opts = {false, false, false};
/// make a context with 1 endpoint that specifies a keyfile
2020-06-29 19:55:59 +00:00
static std::shared_ptr<llarp::Context>
make_context(std::optional<fs::path> keyfile)
2020-06-08 12:42:10 +00:00
{
auto conf = std::make_shared<llarp::Config>(fs::current_path());
conf->Load(std::nullopt, opts.isSNode);
conf->network.m_endpointType = "null";
conf->network.m_keyfile = keyfile;
conf->bootstrap.seednode = true;
conf->api.m_enableRPCServer = false;
2020-06-29 19:55:59 +00:00
auto context = std::make_shared<llarp::Context>();
REQUIRE_NOTHROW(context->Configure(std::move(conf)));
2020-06-29 19:55:59 +00:00
return context;
2020-06-08 12:42:10 +00:00
}
/// test that we dont back up all keys when self.signed is missing or invalid as client
2020-06-08 12:42:10 +00:00
TEST_CASE("key backup bug regression test", "[regress]")
{
// kill logging, this code is noisy
// test 2 explicitly provided keyfiles, empty keyfile and no keyfile
for (std::optional<fs::path> path : {std::optional<fs::path>{"regress-1.private"},
std::optional<fs::path>{"regress-2.private"},
std::optional<fs::path>{""},
{std::nullopt}})
{
llarp::service::Address endpointAddress{};
// try 10 start up and shut downs and see if our key changes or not
for (size_t index = 0; index < 10; index++)
2020-06-08 13:07:49 +00:00
{
2020-06-29 19:55:59 +00:00
auto ctx = make_context(path);
REQUIRE_NOTHROW(ctx->Setup(opts));
ctx->CallSafe([ctx, index, &endpointAddress, &path]() {
auto ep = ctx->router->hiddenServiceContext().GetDefault();
REQUIRE(ep != nullptr);
if (index == 0)
{
REQUIRE(endpointAddress.IsZero());
// first iteration, we are getting our identity that we start with
endpointAddress = ep->GetIdentity().pub.Addr();
REQUIRE(not endpointAddress.IsZero());
}
else
{
REQUIRE(not endpointAddress.IsZero());
if (path.has_value() and not path->empty())
2020-06-08 13:07:49 +00:00
{
// we have a keyfile provided
// after the first iteration we expect the keys to stay the same
REQUIRE(endpointAddress == ep->GetIdentity().pub.Addr());
2020-06-08 13:07:49 +00:00
}
else
{
// we want the keys to shift because no keyfile was provided
REQUIRE(endpointAddress != ep->GetIdentity().pub.Addr());
2020-06-08 13:07:49 +00:00
}
}
// close the router right away
ctx->router->Die();
});
2020-06-29 19:55:59 +00:00
REQUIRE(ctx->Run({}) == 0);
ctx.reset();
2020-06-08 13:07:49 +00:00
}
// remove keys if provied
if (path.has_value() and not path->empty())
fs::remove(*path);
}
2020-06-08 12:42:10 +00:00
}