proxygen
FizzServerTest.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 
12 #include <fizz/server/FizzServer.h>
13 #include <fizz/server/test/Mocks.h>
14 
15 using namespace folly;
16 using namespace testing;
17 
18 namespace fizz {
19 namespace server {
20 namespace test {
21 
22 class MockServerStateMachineInstance : public MockServerStateMachine {
23  public:
25  instance = this;
26  }
27  static MockServerStateMachineInstance* instance;
28 };
29 MockServerStateMachineInstance* MockServerStateMachineInstance::instance;
30 
31 class ActionMoveVisitor : public boost::static_visitor<> {
32  public:
33  MOCK_METHOD0(fallback, void());
34 
35  template <typename T>
36  void operator()(T&) {}
37 
39  fallback();
40  }
41 };
42 
44  public:
45  TestFizzServer() : fizzServer_(state_, queue_, visitor_, this) {}
46 
51 };
52 
53 class FizzServerTest : public Test {
54  public:
55  void SetUp() override {
56  context_ = std::make_shared<FizzServerContext>();
57  fizzServer_.reset(new TestFizzServer());
58  }
59 
60  protected:
61  void accept() {
63  *MockServerStateMachineInstance::instance, _processAccept(_, _, _, _))
64  .WillOnce(InvokeWithoutArgs([] { return AsyncActions(Actions()); }));
65  fizzServer_->fizzServer_.accept(&evb_, context_);
66  }
67 
68  std::shared_ptr<FizzServerContext> context_;
70  std::unique_ptr<TestFizzServer, DelayedDestruction::Destructor> fizzServer_;
71 };
72 
73 TEST_F(FizzServerTest, TestAccept) {
74  accept();
75 }
76 
77 static std::unique_ptr<IOBuf> getV2ClientHello() {
78  // This client hello is truncated but is sufficient to trigger the fallback.
79  static constexpr StringPiece v2ClientHello =
80  "808c0103010063000000200000390000380000350000";
81  return IOBuf::copyBuffer(folly::unhexlify(v2ClientHello));
82 }
83 
84 TEST_F(FizzServerTest, TestSSLV2) {
85  context_->setVersionFallbackEnabled(true);
86  accept();
87  fizzServer_->queue_.append(getV2ClientHello());
88  EXPECT_CALL(fizzServer_->visitor_, fallback());
89  fizzServer_->fizzServer_.newTransportData();
90 }
91 
92 TEST_F(FizzServerTest, TestSSLV2NoVersionFallback) {
93  context_->setVersionFallbackEnabled(false);
94  accept();
95  fizzServer_->queue_.append(getV2ClientHello());
97  *MockServerStateMachineInstance::instance, _processSocketData(_, _))
98  .WillOnce(InvokeWithoutArgs([this]() {
99  fizzServer_->fizzServer_.waitForData();
100  return AsyncActions(Actions());
101  }));
102  fizzServer_->fizzServer_.newTransportData();
103 }
104 
105 TEST_F(FizzServerTest, TestNotSSLV2) {
106  context_->setVersionFallbackEnabled(true);
107  accept();
108  fizzServer_->queue_.append(IOBuf::copyBuffer("ClientHello"));
109  EXPECT_CALL(
110  *MockServerStateMachineInstance::instance, _processSocketData(_, _))
111  .WillOnce(InvokeWithoutArgs([this]() {
112  fizzServer_->fizzServer_.waitForData();
113  return AsyncActions(Actions());
114  }));
115  fizzServer_->fizzServer_.newTransportData();
116 }
117 
118 TEST_F(FizzServerTest, TestSSLV2AfterData) {
119  context_->setVersionFallbackEnabled(true);
120  accept();
121  fizzServer_->queue_.append(IOBuf::copyBuffer("ClientHello"));
122  EXPECT_CALL(
123  *MockServerStateMachineInstance::instance, _processSocketData(_, _))
124  .WillOnce(InvokeWithoutArgs([this]() {
125  fizzServer_->fizzServer_.waitForData();
126  return AsyncActions(Actions());
127  }));
128  fizzServer_->fizzServer_.newTransportData();
129  fizzServer_->queue_.clear();
130  fizzServer_->queue_.append(getV2ClientHello());
131  EXPECT_CALL(
132  *MockServerStateMachineInstance::instance, _processSocketData(_, _))
133  .WillOnce(InvokeWithoutArgs([this]() {
134  fizzServer_->fizzServer_.waitForData();
135  return AsyncActions(Actions());
136  }));
137  fizzServer_->fizzServer_.newTransportData();
138 }
139 } // namespace test
140 } // namespace server
141 } // namespace fizz
bool unhexlify(const InputString &input, OutputString &output)
Definition: String-inl.h:616
boost::variant< Actions, folly::Future< Actions >> AsyncActions
Definition: Actions.h:52
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
std::unique_ptr< TestFizzServer, DelayedDestruction::Destructor > fizzServer_
FizzServer< ActionMoveVisitor, MockServerStateMachineInstance > fizzServer_
void operator()(AttemptVersionFallback &)
EventBase * evb_
std::vector< Action > Actions
Definition: Actions.h:81
Definition: Actions.h:16
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
static std::unique_ptr< IOBuf > getV2ClientHello()
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
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
std::shared_ptr< FizzServerContext > context_
#define MOCK_METHOD0(m,...)
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71