proxygen
CookieCipherTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 
13 
16 
17 using namespace fizz::test;
18 using namespace folly;
19 using namespace testing;
20 
21 static constexpr StringPiece statelessHrr{
22  "020000380303cf21ad74e59a6111be1d8c021e65b891c2a211167abb8c5e079e09e2c8a8339c001301000010002b00020304002c0006000474657374"};
23 
24 static constexpr StringPiece statelessHrrGroup{
25  "0200003e0303cf21ad74e59a6111be1d8c021e65b891c2a211167abb8c5e079e09e2c8a8339c001301000016002b0002030400330002001d002c0006000474657374"};
26 
27 namespace fizz {
28 namespace server {
29 namespace test {
30 
31 TEST(GetStatelessHrrTest, NoGroup) {
35  none,
36  IOBuf::copyBuffer("test"));
37  EXPECT_EQ(hexlify(hrr->coalesce()), statelessHrr);
38 }
39 
40 TEST(GetStatelessHrrTest, Group) {
45  IOBuf::copyBuffer("test"));
46  EXPECT_EQ(hexlify(hrr->coalesce()), statelessHrrGroup);
47 }
48 
49 class GetCookieStateTest : public Test {
50  public:
51  void SetUp() override {
52  factory_.setDefaults();
53  }
54 
55  protected:
57 };
58 
60  auto mockHandshakeContext = new MockHandshakeContext();
61  Sequence seq;
63  factory_, makeHandshakeContext(CipherSuite::TLS_AES_128_GCM_SHA256))
64  .InSequence(seq)
65  .WillOnce(InvokeWithoutArgs([=]() {
66  return std::unique_ptr<HandshakeContext>(mockHandshakeContext);
67  }));
69  *mockHandshakeContext,
70  appendToTranscript(BufMatches("clienthelloencoding")))
71  .InSequence(seq);
72  EXPECT_CALL(*mockHandshakeContext, getHandshakeContext())
73  .WillOnce(Invoke([]() { return IOBuf::copyBuffer("context"); }));
74  auto state = getCookieState(
75  factory_,
80  IOBuf::copyBuffer("token"));
83  EXPECT_FALSE(state.group.hasValue());
84  EXPECT_TRUE(IOBufEqualTo()(state.chloHash, IOBuf::copyBuffer("context")));
85  EXPECT_TRUE(IOBufEqualTo()(state.appToken, IOBuf::copyBuffer("token")));
86 }
87 
88 TEST_F(GetCookieStateTest, TestNoVersion) {
93  factory_,
97  chlo,
98  IOBuf::copyBuffer("token")),
99  std::runtime_error);
100 }
101 
102 TEST_F(GetCookieStateTest, TestVersionMismatch) {
103  EXPECT_THROW(
105  factory_,
110  IOBuf::copyBuffer("token")),
111  std::runtime_error);
112 }
113 
114 TEST_F(GetCookieStateTest, TestCipherNegotiate) {
115  EXPECT_CALL(
116  factory_, makeHandshakeContext(CipherSuite::TLS_AES_256_GCM_SHA384))
117  .WillOnce(InvokeWithoutArgs([=]() {
118  auto ret = std::make_unique<MockHandshakeContext>();
119  ret->setDefaults();
120  return ret;
121  }));
122  auto state = getCookieState(
123  factory_,
129  IOBuf::copyBuffer("token"));
131 }
132 
133 TEST_F(GetCookieStateTest, TestCipherNegotiateTie) {
134  EXPECT_CALL(
135  factory_, makeHandshakeContext(CipherSuite::TLS_AES_128_GCM_SHA256))
136  .WillOnce(InvokeWithoutArgs([=]() {
137  auto ret = std::make_unique<MockHandshakeContext>();
138  ret->setDefaults();
139  return ret;
140  }));
141  auto state = getCookieState(
142  factory_,
148  IOBuf::copyBuffer("token"));
149  EXPECT_EQ(state.cipher, CipherSuite::TLS_AES_128_GCM_SHA256);
150 }
151 
152 TEST_F(GetCookieStateTest, TestCipherMismatch) {
153  EXPECT_THROW(
155  factory_,
157  {{}},
160  IOBuf::copyBuffer("token")),
161  std::runtime_error);
162 }
163 
164 TEST_F(GetCookieStateTest, TestGroupNotSent) {
165  auto state = getCookieState(
166  factory_,
171  IOBuf::copyBuffer("token"));
173 }
174 
175 TEST_F(GetCookieStateTest, TestNoGroups) {
179  auto state = getCookieState(
180  factory_,
184  chlo,
185  IOBuf::copyBuffer("token"));
186  EXPECT_FALSE(state.group.hasValue());
187 }
188 
189 TEST_F(GetCookieStateTest, TestNoKeyShare) {
192  EXPECT_THROW(
194  factory_,
198  chlo,
199  IOBuf::copyBuffer("token")),
200  std::runtime_error);
201 }
202 
203 TEST_F(GetCookieStateTest, TestGroupMismatch) {
204  auto state = getCookieState(
205  factory_,
208  {},
210  IOBuf::copyBuffer("token"));
211  EXPECT_FALSE(state.group.hasValue());
212 }
213 } // namespace test
214 } // namespace server
215 } // namespace fizz
Buf getStatelessHelloRetryRequest(ProtocolVersion version, CipherSuite cipher, folly::Optional< NamedGroup > group, Buf cookie)
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
static const std::string chlo
CookieState getCookieState(const Factory &factory, const std::vector< ProtocolVersion > &supportedVersions, const std::vector< std::vector< CipherSuite >> &supportedCiphers, const std::vector< NamedGroup > &supportedGroups, const ClientHello &chlo, Buf appToken)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static constexpr StringPiece statelessHrrGroup
Gen seq(Value first, Value last)
Definition: Base.h:484
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
TEST(IOBufUtilTest, TrimBytes)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
Definition: Actions.h:16
static void removeExtension(T &msg, ExtensionType ext)
Definition: TestMessages.h:21
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST_F(RSAPSSTest, TestSignVerify)
#define EXPECT_CALL(obj, call)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool hexlify(const InputString &input, OutputString &output, bool append_output)
Definition: String-inl.h:596
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
static ClientHello clientHello()
Definition: TestMessages.h:26
constexpr ProtocolVersion TestProtocolVersion
Definition: ProtocolTest.h:22
state
Definition: http_parser.c:272
constexpr None none
Definition: Optional.h:87
static constexpr StringPiece statelessHrr