mirror of
https://github.com/facebookincubator/mvfst.git
synced 2025-11-10 21:22:20 +03:00
Summary: After https://github.com/facebookincubator/mvfst/issues/35 , the facility to mock fizz::Aead is not required anymore. Either the test depends on fizz's behavior, in which case a mock was never appropriate, or it doesn, in which case a quic::Aead is more appropriate. Pull Request resolved: https://github.com/facebookincubator/mvfst/pull/39 Reviewed By: mjoras Differential Revision: D16785265 Pulled By: yangchi fbshipit-source-id: ba74155ab7e8ed1c261c58569db0690781d85fe1
92 lines
3.1 KiB
C++
92 lines
3.1 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
*/
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <fizz/crypto/aead/test/Mocks.h>
|
|
#include <quic/codec/QuicConnectionId.h>
|
|
#include <quic/common/test/TestUtils.h>
|
|
#include <quic/handshake/HandshakeLayer.h>
|
|
#include <quic/handshake/QuicFizzFactory.h>
|
|
#include <quic/handshake/test/Mocks.h>
|
|
#include <vector>
|
|
|
|
using namespace folly;
|
|
using namespace testing;
|
|
|
|
namespace quic {
|
|
namespace test {
|
|
|
|
class QuicTestFizzFactory : public QuicFizzFactory {
|
|
public:
|
|
~QuicTestFizzFactory() override = default;
|
|
|
|
std::unique_ptr<PacketNumberCipher> makePacketNumberCipher(
|
|
fizz::CipherSuite) const override {
|
|
return std::move(packetNumberCipher_);
|
|
}
|
|
|
|
void setMockPacketNumberCipher(
|
|
std::unique_ptr<MockPacketNumberCipher> packetNumberCipher) {
|
|
packetNumberCipher_ = std::move(packetNumberCipher);
|
|
}
|
|
|
|
mutable std::unique_ptr<MockPacketNumberCipher> packetNumberCipher_;
|
|
};
|
|
|
|
class HandshakeLayerTest : public Test {
|
|
public:
|
|
std::unique_ptr<MockPacketNumberCipher> createMockPacketNumberCipher() {
|
|
auto mockPacketNumberCipher = std::make_unique<MockPacketNumberCipher>();
|
|
EXPECT_CALL(*mockPacketNumberCipher, setKey(_))
|
|
.WillOnce(Invoke([&](folly::ByteRange key) {
|
|
packetCipherKey_ = folly::IOBuf::copyBuffer(key);
|
|
}));
|
|
EXPECT_CALL(*mockPacketNumberCipher, keyLength())
|
|
.WillRepeatedly(Return(fizz::AESGCM128::kKeyLength));
|
|
return mockPacketNumberCipher;
|
|
}
|
|
|
|
folly::Optional<std::unique_ptr<folly::IOBuf>> packetCipherKey_;
|
|
};
|
|
|
|
TEST_F(HandshakeLayerTest, TestPacketEncryptionKey) {
|
|
QuicTestFizzFactory factory;
|
|
factory.setMockPacketNumberCipher(createMockPacketNumberCipher());
|
|
auto clientKey = std::vector<uint8_t>(
|
|
{0x0c, 0x74, 0xbb, 0x95, 0xa1, 0x04, 0x8e, 0x52, 0xef, 0x3b, 0x72,
|
|
0xe1, 0x28, 0x89, 0x35, 0x1c, 0xd7, 0x3a, 0x55, 0x0f, 0xb6, 0x2c,
|
|
0x4b, 0xb0, 0x87, 0xe9, 0x15, 0xcc, 0xe9, 0x6c, 0xe3, 0xa0});
|
|
auto expectedHex = "cd253a36ff93937c469384a823af6c56";
|
|
auto packetCipher = makePacketNumberCipher(
|
|
&factory,
|
|
folly::range(clientKey),
|
|
fizz::CipherSuite::TLS_AES_128_GCM_SHA256);
|
|
auto secretHex = folly::hexlify(packetCipherKey_.value()->coalesce());
|
|
EXPECT_EQ(secretHex, expectedHex);
|
|
|
|
// reset the cipher
|
|
factory.setMockPacketNumberCipher(createMockPacketNumberCipher());
|
|
auto serverKey = std::vector<uint8_t>(
|
|
{0x4c, 0x9e, 0xdf, 0x24, 0xb0, 0xe5, 0xe5, 0x06, 0xdd, 0x3b, 0xfa,
|
|
0x4e, 0x0a, 0x03, 0x11, 0xe8, 0xc4, 0x1f, 0x35, 0x42, 0x73, 0xd8,
|
|
0xcb, 0x49, 0xdd, 0xd8, 0x46, 0x41, 0x38, 0xd4, 0x7e, 0xc6});
|
|
|
|
auto expectedKey2 = "2579d8696f85eda68d3502b65596586b";
|
|
|
|
auto packetCipher2 = makePacketNumberCipher(
|
|
&factory,
|
|
folly::range(serverKey),
|
|
fizz::CipherSuite::TLS_AES_128_GCM_SHA256);
|
|
auto secretHex2 = folly::hexlify(packetCipherKey_.value()->coalesce());
|
|
EXPECT_EQ(secretHex2, expectedKey2);
|
|
}
|
|
} // namespace test
|
|
} // namespace quic
|