proxygen
PeekingAcceptorHandshakeHelperTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <thread>
19 
24 
25 using namespace folly;
26 using namespace folly::test;
27 using namespace wangle;
28 using namespace testing;
29 
32  public:
33  using PeekingAcceptorHandshakeHelper::PeekCallback::PeekCallback;
34 
36  getHelperInternal,
38  const std::vector<uint8_t>&,
39  const folly::SocketAddress&,
40  std::chrono::steady_clock::time_point,
41  TransportInfo&));
42 
44  const std::vector<uint8_t>& peekedBytes,
45  const folly::SocketAddress& clientAddr,
46  std::chrono::steady_clock::time_point acceptTime,
47  TransportInfo& tinfo) override {
49  getHelperInternal(peekedBytes, clientAddr, acceptTime, tinfo));
50  }
51 };
52 
54  protected:
55  void SetUp() override {
56  sslSock_ = new MockAsyncSSLSocket(
58  &base_,
59  true /* defer security negotiation */);
60  sockPtr_ = AsyncSSLSocket::UniquePtr(sslSock_);
61 
62  peekCallbacks_.push_back(&mockPeekCallback1_);
63  peekCallbacks_.push_back(&mockPeekCallback2_);
64 
65  helper_ = new PeekingAcceptorHandshakeHelper(
66  sa_,
68  tinfo_,
69  peekCallbacks_,
70  2);
71 
72  innerHelper_ = new MockHandshakeHelper<>();
73  helperPtr_ = AcceptorHandshakeHelper::UniquePtr(innerHelper_);
74  }
75 
76  void TearDown() override {
77  // Normally this would be destoryed by the AcceptorHandshakeManager.
78  helper_->destroy();
79  }
80 
85  MockPeekingCallback mockPeekCallback1_{2};
86  MockPeekingCallback mockPeekCallback2_{1};
87  std::vector<PeekingCallbackPtr> peekCallbacks_;
93 };
94 
96  helper_->start(std::move(sockPtr_), &callback_);
97  // first 2 bytes of SSL3+.
98  std::vector<uint8_t> buf(2);
99  buf[0] = 0x16;
100  buf[1] = 0x03;
101  EXPECT_CALL(mockPeekCallback1_, getHelperInternal(_, _, _, _))
102  .WillOnce(Return(helperPtr_.release()));
103  EXPECT_CALL(*innerHelper_, startInternal(_, _));
104  helper_->peekSuccess(buf);
105 }
106 
108  helper_->start(std::move(sockPtr_), &callback_);
109  // first 2 bytes of SSL3+.
110  std::vector<uint8_t> buf(2);
111  buf[0] = 0x16;
112  buf[1] = 0x03;
113  EXPECT_CALL(mockPeekCallback1_, getHelperInternal(_, _, _, _))
114  .WillOnce(Return(nullptr));
115  EXPECT_CALL(mockPeekCallback2_, getHelperInternal(_, _, _, _))
116  .WillOnce(Return(nullptr));
117  EXPECT_CALL(callback_, connectionError_(_, _, _));
118  helper_->peekSuccess(buf);
119 }
120 
122  helper_->start(std::move(sockPtr_), &callback_);
123  // first 2 bytes of SSL3+.
124  std::vector<uint8_t> buf(2);
125  buf[0] = 0x16;
126  buf[1] = 0x03;
127  EXPECT_CALL(mockPeekCallback1_, getHelperInternal(_, _, _, _))
128  .WillOnce(Return(nullptr));
129  EXPECT_CALL(mockPeekCallback2_, getHelperInternal(_, _, _, _))
130  .WillOnce(Return(helperPtr_.release()));
131  EXPECT_CALL(*innerHelper_, startInternal(_, _));
132  helper_->peekSuccess(buf);
133 }
134 
135 
138  EXPECT_CALL(*sslSock_, setReadCB(_))
139  .WillOnce(SaveArg<0>(&rcb));
140  EXPECT_CALL(*sslSock_, setReadCB(nullptr));
141  EXPECT_CALL(callback_, connectionError_(_, _, _));
142  helper_->start(std::move(sockPtr_), &callback_);
143  ASSERT_TRUE(rcb);
144  rcb->readEOF();
145 }
146 
148  helper_->start(std::move(sockPtr_), &callback_);
149  EXPECT_CALL(callback_, connectionError_(_, _, _));
150  helper_->peekError(AsyncSocketException(
151  AsyncSocketException::AsyncSocketExceptionType::END_OF_FILE,
152  "Unit test"));
153 }
154 
156  helper_->start(std::move(sockPtr_), &callback_);
157 
158  EXPECT_CALL(*sslSock_, closeNow()).Times(AtLeast(1)).WillOnce(Invoke([&] {
159  helper_->peekError(AsyncSocketException(
161  }));
162  EXPECT_CALL(callback_, connectionError_(_, _, _));
163  helper_->dropConnection();
164 }
165 
167  helper_->start(std::move(sockPtr_), &callback_);
168  std::vector<uint8_t> buf(2);
169  // first 2 bytes of SSL3+.
170  buf[0] = 0x16;
171  buf[1] = 0x03;
172 
173  EXPECT_CALL(mockPeekCallback1_, getHelperInternal(_, _, _, _))
174  .WillOnce(Return(helperPtr_.release()));
175  EXPECT_CALL(*innerHelper_, startInternal(_, _));
176  helper_->peekSuccess(buf);
177 
178  EXPECT_CALL(*innerHelper_, dropConnection(_));
179  helper_->dropConnection();
180 }
#define MOCK_METHOD4_T(m,...)
GTEST_API_ Cardinality AtLeast(int n)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
MockHandshakeHelper< UseSharedPtrPolicy > * innerHelper_
wangle::AcceptorHandshakeHelper::UniquePtr getHelper(const std::vector< uint8_t > &peekedBytes, const folly::SocketAddress &clientAddr, std::chrono::steady_clock::time_point acceptTime, TransportInfo &tinfo) override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
string UNKNOWN
Definition: tokenize.py:53
std::unique_ptr< AsyncSSLSocket, Destructor > UniquePtr
folly::Synchronized< EventBase * > base_
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
std::shared_ptr< SSLContext > SSLContextPtr
Definition: SSLContext.h:628
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
StrictMock< MockHandshakeHelperCallback<> > callback_
folly::Function< void()> callback_
TEST_F(FileUtilTest, read)
std::unique_ptr< AcceptorHandshakeHelper, folly::DelayedDestruction::Destructor > UniquePtr
virtual void readEOF() noexcept=0
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
internal::ReturnAction< R > Return(R value)