1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/test/test_llarp_encrypted_frame.cpp

64 lines
1.5 KiB
C++
Raw Normal View History

#include <crypto/encrypted_frame.hpp>
2019-01-13 15:00:50 +01:00
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
2019-05-28 21:45:09 +02:00
#include <llarp_test.hpp>
#include <messages/relay_commit.hpp>
2018-06-20 14:34:48 +02:00
2019-05-28 21:45:09 +02:00
#include <test_util.hpp>
2019-01-13 15:00:50 +01:00
#include <gtest/gtest.h>
2019-05-28 21:45:09 +02:00
using namespace ::llarp;
using namespace ::testing;
using EncryptedFrame = EncryptedFrame;
using SecretKey = SecretKey;
using PubKey = PubKey;
using LRCR = LR_CommitRecord;
2018-06-20 14:34:48 +02:00
2019-05-28 21:45:09 +02:00
class FrameTest : public test::LlarpTest<>
2018-06-20 14:34:48 +02:00
{
public:
SecretKey alice, bob;
};
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();
2019-05-28 21:45:09 +02:00
buf->cur = buf->base + EncryptedFrameOverheadSize;
2018-06-20 14:34:48 +02:00
ASSERT_TRUE(record.BEncode(buf));
2019-05-28 21:45:09 +02:00
EXPECT_CALL(m_crypto, randbytes(_, _))
.WillOnce(Invoke(&test::randbytes_impl));
EXPECT_CALL(m_crypto, dh_client(_, _, alice, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, xchacha20(_, _, _))
.Times(2)
.WillRepeatedly(Return(true));
EXPECT_CALL(m_crypto, hmac(_, _, _)).Times(2).WillRepeatedly(Return(true));
2018-06-21 14:55:02 +02:00
// rewind buffer
2019-05-28 21:45:09 +02:00
buf->cur = buf->base + EncryptedFrameOverheadSize;
2018-06-21 14:55:02 +02:00
// encrypt to alice
ASSERT_TRUE(f.EncryptInPlace(alice, bob.toPublic()));
2019-05-28 21:45:09 +02:00
EXPECT_CALL(m_crypto, dh_server(_, _, _, _)).WillOnce(Return(true));
2018-06-21 14:55:02 +02:00
// decrypt from alice
ASSERT_TRUE(f.DecryptInPlace(bob));
2018-06-21 14:55:02 +02:00
LRCR otherRecord;
ASSERT_TRUE(otherRecord.BDecode(buf));
ASSERT_TRUE(otherRecord == record);
2019-04-26 01:21:19 +02:00
}