2018-06-20 14:34:48 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <llarp/crypto.hpp>
|
|
|
|
#include <llarp/encrypted_frame.hpp>
|
|
|
|
#include <llarp/messages/relay_commit.hpp>
|
|
|
|
|
|
|
|
using EncryptedFrame = llarp::EncryptedFrame;
|
|
|
|
using SecretKey = llarp::SecretKey;
|
|
|
|
using PubKey = llarp::PubKey;
|
2018-06-21 14:55:02 +02:00
|
|
|
using LRCR = llarp::LR_CommitRecord;
|
2018-06-20 14:34:48 +02:00
|
|
|
|
|
|
|
class FrameTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
llarp_crypto crypto;
|
|
|
|
SecretKey alice, bob;
|
|
|
|
|
|
|
|
FrameTest()
|
|
|
|
{
|
|
|
|
llarp_crypto_libsodium_init(&crypto);
|
|
|
|
}
|
|
|
|
|
|
|
|
~FrameTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
|
|
|
crypto.encryption_keygen(alice);
|
|
|
|
crypto.encryption_keygen(bob);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TearDown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(FrameTest, TestFrameCrypto)
|
|
|
|
{
|
|
|
|
EncryptedFrame f(256);
|
|
|
|
f.Fill(0);
|
2018-06-21 14:55:02 +02:00
|
|
|
LRCR record;
|
|
|
|
record.nextHop.Fill(1);
|
|
|
|
record.tunnelNonce.Fill(2);
|
2018-06-22 02:25:30 +02:00
|
|
|
record.rxid.Fill(3);
|
|
|
|
record.txid.Fill(4);
|
2018-06-20 14:34:48 +02:00
|
|
|
|
|
|
|
auto buf = f.Buffer();
|
|
|
|
buf->cur = buf->base + EncryptedFrame::OverheadSize;
|
|
|
|
|
|
|
|
ASSERT_TRUE(record.BEncode(buf));
|
|
|
|
|
2018-06-21 14:55:02 +02:00
|
|
|
// rewind buffer
|
|
|
|
buf->cur = buf->base + EncryptedFrame::OverheadSize;
|
|
|
|
// encrypt to alice
|
2018-06-20 14:34:48 +02:00
|
|
|
ASSERT_TRUE(f.EncryptInPlace(alice, llarp::seckey_topublic(bob), &crypto));
|
2018-06-21 14:55:02 +02:00
|
|
|
// decrypt from alice
|
2018-06-20 14:34:48 +02:00
|
|
|
ASSERT_TRUE(f.DecryptInPlace(bob, &crypto));
|
2018-06-21 14:55:02 +02:00
|
|
|
|
|
|
|
LRCR otherRecord;
|
|
|
|
ASSERT_TRUE(otherRecord.BDecode(buf));
|
|
|
|
ASSERT_TRUE(otherRecord == record);
|
2018-06-20 14:34:48 +02:00
|
|
|
};
|