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