2019-01-13 14:00:50 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2019-05-28 19:45:09 +00:00
|
|
|
#include <llarp_test.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <path/path.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <service/address.hpp>
|
2019-04-22 18:35:19 +00:00
|
|
|
#include <service/identity.hpp>
|
|
|
|
#include <service/intro_set.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/time.hpp>
|
2018-07-06 16:08:30 +00:00
|
|
|
|
2019-05-18 17:34:07 +00:00
|
|
|
#include <crypto/mock_crypto.hpp>
|
|
|
|
#include <test_util.hpp>
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <gtest/gtest.h>
|
2019-05-18 17:34:07 +00:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
|
|
|
using namespace llarp;
|
|
|
|
using namespace testing;
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
struct HiddenServiceTest : public test::LlarpTest<>
|
2018-07-06 16:08:30 +00:00
|
|
|
{
|
2019-05-18 17:34:07 +00:00
|
|
|
service::Identity ident;
|
2018-07-06 16:08:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(HiddenServiceTest, TestGenerateIntroSet)
|
|
|
|
{
|
2019-05-18 17:34:07 +00:00
|
|
|
service::Address addr;
|
2019-01-02 01:03:53 +00:00
|
|
|
ASSERT_TRUE(ident.pub.CalculateAddress(addr.as_array()));
|
2019-05-18 17:34:07 +00:00
|
|
|
service::IntroSet I;
|
|
|
|
auto now = time_now_ms();
|
2018-10-29 16:49:09 +00:00
|
|
|
I.T = now;
|
2018-07-06 16:08:30 +00:00
|
|
|
while(I.I.size() < 10)
|
|
|
|
{
|
2019-05-18 17:34:07 +00:00
|
|
|
service::Introduction intro;
|
|
|
|
intro.expiresAt = now + (path::default_lifetime / 2);
|
2018-07-06 16:08:30 +00:00
|
|
|
intro.router.Randomize();
|
|
|
|
intro.pathID.Randomize();
|
2019-04-05 14:58:22 +00:00
|
|
|
I.I.emplace_back(std::move(intro));
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
2019-05-28 19:45:09 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(m_crypto, sign(I.Z, _, _)).WillOnce(Return(true));
|
|
|
|
EXPECT_CALL(m_crypto, verify(_, _, I.Z)).WillOnce(Return(true));
|
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
ASSERT_TRUE(ident.SignIntroSet(I, now));
|
|
|
|
ASSERT_TRUE(I.Verify(now));
|
2019-04-25 23:21:19 +00:00
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
|
|
|
|
TEST_F(HiddenServiceTest, TestAddressToFromString)
|
|
|
|
{
|
|
|
|
auto str = ident.pub.Addr().ToString();
|
2019-05-18 17:34:07 +00:00
|
|
|
service::Address addr;
|
2018-08-02 00:48:43 +00:00
|
|
|
ASSERT_TRUE(addr.FromString(str));
|
|
|
|
ASSERT_TRUE(addr == ident.pub.Addr());
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
2019-05-18 17:34:07 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
struct ServiceIdentityTest : public test::LlarpTest<>
|
2019-05-18 17:34:07 +00:00
|
|
|
{
|
2019-05-28 19:45:09 +00:00
|
|
|
ServiceIdentityTest()
|
2019-05-28 19:45:08 +00:00
|
|
|
{
|
|
|
|
}
|
2019-05-18 17:34:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template < typename Arg >
|
|
|
|
std::function< void(Arg&) >
|
|
|
|
FillArg(byte_t val)
|
|
|
|
{
|
|
|
|
return [=](Arg& arg) { arg.Fill(val); };
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ServiceIdentityTest, EnsureKeys)
|
|
|
|
{
|
|
|
|
fs::path p = test::randFilename();
|
|
|
|
ASSERT_FALSE(fs::exists(fs::status(p)));
|
|
|
|
|
|
|
|
test::FileGuard guard(p);
|
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
EXPECT_CALL(m_crypto, encryption_keygen(_))
|
2019-05-18 17:34:07 +00:00
|
|
|
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x01)));
|
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
EXPECT_CALL(m_crypto, identity_keygen(_))
|
2019-05-18 17:34:07 +00:00
|
|
|
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x02)));
|
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
EXPECT_CALL(m_crypto, pqe_keygen(_))
|
2019-05-18 17:34:07 +00:00
|
|
|
.WillOnce(WithArg< 0 >(FillArg< PQKeyPair >(0x03)));
|
|
|
|
|
|
|
|
service::Identity identity;
|
2019-05-28 19:45:08 +00:00
|
|
|
ASSERT_TRUE(identity.EnsureKeys(p.string()));
|
2019-05-18 17:34:07 +00:00
|
|
|
ASSERT_TRUE(fs::exists(fs::status(p)));
|
|
|
|
|
|
|
|
// Verify what is on disk is what is what was generated
|
|
|
|
service::Identity other;
|
|
|
|
// No need to set more mocks, as we shouldn't need to re-keygen
|
2019-05-28 19:45:08 +00:00
|
|
|
ASSERT_TRUE(other.EnsureKeys(p.string()));
|
2019-05-18 17:34:07 +00:00
|
|
|
ASSERT_EQ(identity, other);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ServiceIdentityTest, EnsureKeysDir)
|
|
|
|
{
|
|
|
|
fs::path p = test::randFilename();
|
|
|
|
ASSERT_FALSE(fs::exists(fs::status(p)));
|
|
|
|
|
|
|
|
test::FileGuard guard(p);
|
|
|
|
std::error_code code;
|
|
|
|
ASSERT_TRUE(fs::create_directory(p, code)) << code;
|
|
|
|
|
|
|
|
service::Identity identity;
|
2019-05-28 19:45:08 +00:00
|
|
|
ASSERT_FALSE(identity.EnsureKeys(p.string()));
|
2019-05-18 17:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ServiceIdentityTest, EnsureKeysBrokenFile)
|
|
|
|
{
|
|
|
|
fs::path p = test::randFilename();
|
|
|
|
ASSERT_FALSE(fs::exists(fs::status(p)));
|
|
|
|
|
|
|
|
test::FileGuard guard(p);
|
|
|
|
std::error_code code;
|
|
|
|
|
|
|
|
std::fstream file;
|
|
|
|
file.open(p.string(), std::ios::out);
|
|
|
|
ASSERT_TRUE(file.is_open()) << p;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
service::Identity identity;
|
2019-05-28 19:45:08 +00:00
|
|
|
ASSERT_FALSE(identity.EnsureKeys(p.string()));
|
2019-05-18 17:34:07 +00:00
|
|
|
}
|