2019-01-14 21:46:07 +00:00
|
|
|
#include <crypto/encrypted_frame.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-26 15:40:58 +00:00
|
|
|
#include <crypto/crypto_libsodium.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/relay_commit.hpp>
|
2018-06-20 12:34:48 +00:00
|
|
|
|
2019-01-13 14:00:50 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-06-20 12:34:48 +00:00
|
|
|
using EncryptedFrame = llarp::EncryptedFrame;
|
|
|
|
using SecretKey = llarp::SecretKey;
|
|
|
|
using PubKey = llarp::PubKey;
|
2018-06-21 12:55:02 +00:00
|
|
|
using LRCR = llarp::LR_CommitRecord;
|
2018-06-20 12:34:48 +00:00
|
|
|
|
|
|
|
class FrameTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
public:
|
2019-01-26 15:40:58 +00:00
|
|
|
llarp::sodium::CryptoLibSodium crypto;
|
2018-06-20 12:34:48 +00:00
|
|
|
SecretKey alice, bob;
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
FrameTest()
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~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 12:55:02 +00:00
|
|
|
LRCR record;
|
|
|
|
record.nextHop.Fill(1);
|
|
|
|
record.tunnelNonce.Fill(2);
|
2018-06-22 00:25:30 +00:00
|
|
|
record.rxid.Fill(3);
|
|
|
|
record.txid.Fill(4);
|
2018-06-20 12:34:48 +00:00
|
|
|
|
|
|
|
auto buf = f.Buffer();
|
2018-12-20 16:49:05 +00:00
|
|
|
buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
|
2018-06-20 12:34:48 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(record.BEncode(buf));
|
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
// rewind buffer
|
2018-12-20 16:49:05 +00:00
|
|
|
buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
|
2018-06-21 12:55:02 +00:00
|
|
|
// encrypt to alice
|
2019-01-02 01:04:08 +00:00
|
|
|
ASSERT_TRUE(f.EncryptInPlace(alice, bob.toPublic(), &crypto));
|
2018-06-21 12:55:02 +00:00
|
|
|
// decrypt from alice
|
2018-06-20 12:34:48 +00:00
|
|
|
ASSERT_TRUE(f.DecryptInPlace(bob, &crypto));
|
2018-06-21 12:55:02 +00:00
|
|
|
|
|
|
|
LRCR otherRecord;
|
|
|
|
ASSERT_TRUE(otherRecord.BDecode(buf));
|
|
|
|
ASSERT_TRUE(otherRecord == record);
|
2018-09-02 18:25:42 +00:00
|
|
|
};
|