proxygen
AsyncFizzBaseTest.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 namespace fizz {
18 namespace test {
19 
20 using namespace folly;
21 using namespace folly::test;
22 using namespace testing;
23 
29  public:
31  : testing::Test(),
34  socket_ = getUnderlyingTransport<MockAsyncTransport>();
35  ON_CALL(*this, good()).WillByDefault(Return(true));
36  }
37 
38  void TearDown() override {
39  EXPECT_CALL(*socket_, setReadCB(nullptr));
40  }
41 
42  MOCK_CONST_METHOD0(good, bool());
43  MOCK_CONST_METHOD0(readable, bool());
44  MOCK_CONST_METHOD0(connecting, bool());
45  MOCK_CONST_METHOD0(error, bool());
47  MOCK_CONST_METHOD0(getSelfCert, const X509*());
48  MOCK_CONST_METHOD0(isReplaySafe, bool());
50  setReplaySafetyCallback,
52  MOCK_CONST_METHOD0(getSelfCertificate, const Cert*());
53  MOCK_CONST_METHOD0(getPeerCertificate, const Cert*());
54  MOCK_CONST_METHOD0(getApplicationProtocol_, std::string());
55 
57  return getApplicationProtocol_();
58  }
59 
61  MOCK_CONST_METHOD0(getSupportedSigSchemes, std::vector<SignatureScheme>());
63 
65  writeAppDataInternal,
66  void(
68  std::shared_ptr<folly::IOBuf>,
70 
73  std::unique_ptr<folly::IOBuf>&& buf,
75  writeAppDataInternal(
76  callback, std::shared_ptr<folly::IOBuf>(buf.release()), flags);
77  }
78 
79  MOCK_METHOD1(transportError, void(const folly::AsyncSocketException&));
80  MOCK_METHOD0(transportDataAvailable, void());
81 
82  protected:
83  void expectReadBufRequest(size_t sizeToGive) {
84  readBuf_.resize(sizeToGive);
85  EXPECT_CALL(readCallback_, getReadBuffer(_, _))
86  .InSequence(readBufSeq_)
87  .WillOnce(DoAll(
88  SetArgPointee<0>(readBuf_.data()),
89  SetArgPointee<1>(readBuf_.size())));
90  }
91 
93  EXPECT_CALL(readCallback_, readDataAvailable_(data.size()))
94  .InSequence(readBufSeq_)
95  .WillOnce(Invoke([this, data](size_t len) {
96  EXPECT_TRUE(std::memcmp(readBuf_.data(), data.data(), len) == 0);
97  }));
98  }
99 
101  EXPECT_CALL(*socket_, setReadCB(_))
102  .WillOnce(SaveArg<0>(&transportReadCallback_));
103  }
104 
110  Sequence readBufSeq_;
111  std::vector<uint8_t> readBuf_;
112 };
113 
114 MATCHER_P(BufMatches, expected, "") {
116  return eq(*arg, *expected);
117 }
118 
119 TEST_F(AsyncFizzBaseTest, TestIsFizz) {
120  EXPECT_EQ(getSecurityProtocol(), "Fizz");
121 }
122 
123 TEST_F(AsyncFizzBaseTest, TestAppBytesWritten) {
124  EXPECT_EQ(getAppBytesWritten(), 0);
125 
126  auto four = IOBuf::copyBuffer("4444");
127  writeChain(nullptr, std::move(four));
128  EXPECT_EQ(getAppBytesWritten(), 4);
129 
130  auto eight = IOBuf::copyBuffer("88888888");
131  auto two = IOBuf::copyBuffer("22");
132  eight->prependChain(std::move(two));
133  writeChain(nullptr, std::move(eight));
134  EXPECT_EQ(getAppBytesWritten(), 14);
135 }
136 
137 TEST_F(AsyncFizzBaseTest, TestAppBytesReceived) {
138  EXPECT_EQ(getAppBytesReceived(), 0);
139 
140  auto four = IOBuf::copyBuffer("4444");
141  deliverAppData(std::move(four));
142  EXPECT_EQ(getAppBytesReceived(), 4);
143 
144  auto eight = IOBuf::copyBuffer("88888888");
145  auto two = IOBuf::copyBuffer("22");
146  eight->prependChain(std::move(two));
147  deliverAppData(std::move(eight));
148  EXPECT_EQ(getAppBytesReceived(), 14);
149 }
150 
152  auto buf = IOBuf::copyBuffer("buf");
153 
154  EXPECT_CALL(*this, writeAppDataInternal(_, _, _));
155  writeChain(nullptr, std::move(buf));
156 }
157 
158 TEST_F(AsyncFizzBaseTest, TestReadErr) {
159  setReadCB(&readCallback_);
160 
161  EXPECT_CALL(readCallback_, readErr_(_));
163  deliverError(ase_);
164  EXPECT_EQ(getReadCallback(), nullptr);
165 }
166 
167 TEST_F(AsyncFizzBaseTest, TestReadErrNoCallback) {
169  deliverError(ase_);
170 }
171 
172 TEST_F(AsyncFizzBaseTest, TestReadErrAsync) {
173  ON_CALL(*this, good()).WillByDefault(Return(false));
174  deliverError(ase_);
175 
176  EXPECT_CALL(readCallback_, readErr_(_));
177  setReadCB(&readCallback_);
178  EXPECT_EQ(getReadCallback(), nullptr);
179 }
180 
181 TEST_F(AsyncFizzBaseTest, TestReadEOF) {
182  setReadCB(&readCallback_);
183 
184  EXPECT_CALL(readCallback_, readEOF_());
185  deliverError(eof_);
186  EXPECT_EQ(getReadCallback(), nullptr);
187 }
188 
189 TEST_F(AsyncFizzBaseTest, TestReadEOFNoCallback) {
190  deliverError(eof_);
191 }
192 
193 TEST_F(AsyncFizzBaseTest, TestMovableBuffer) {
194  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(true));
195 
196  setReadCB(&readCallback_);
197 
198  auto buf = IOBuf::copyBuffer("buf");
199  EXPECT_CALL(readCallback_, readBufferAvailable_(BufMatches(buf.get())));
200  deliverAppData(buf->clone());
201 
202  auto buf2 = IOBuf::copyBuffer("buf2");
203  EXPECT_CALL(readCallback_, readBufferAvailable_(BufMatches(buf2.get())));
204  deliverAppData(buf2->clone());
205 }
206 
207 TEST_F(AsyncFizzBaseTest, TestMovableBufferAsyncCallback) {
208  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(true));
209 
210  auto buf = IOBuf::copyBuffer("buf");
211  deliverAppData(std::move(buf));
212 
213  auto buf2 = IOBuf::copyBuffer("buf2");
214  deliverAppData(std::move(buf2));
215 
216  auto expected = IOBuf::copyBuffer("bufbuf2");
217  EXPECT_CALL(readCallback_, readBufferAvailable_(BufMatches(expected.get())));
218  setReadCB(&readCallback_);
219 
220  auto buf3 = IOBuf::copyBuffer("buf3");
221  EXPECT_CALL(readCallback_, readBufferAvailable_(BufMatches(buf3.get())));
222  deliverAppData(buf3->clone());
223 }
224 
225 TEST_F(AsyncFizzBaseTest, TestReadBufferLarger) {
226  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
227 
228  setReadCB(&readCallback_);
229 
230  auto buf = IOBuf::copyBuffer("sup");
231  expectReadBufRequest(20);
232  expectReadData("sup");
233  deliverAppData(std::move(buf));
234 }
235 
236 TEST_F(AsyncFizzBaseTest, TestReadBufferExact) {
237  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
238 
239  setReadCB(&readCallback_);
240 
241  auto buf = IOBuf::copyBuffer("sup");
242  expectReadBufRequest(3);
243  expectReadData("sup");
244  deliverAppData(std::move(buf));
245 }
246 
247 TEST_F(AsyncFizzBaseTest, TestReadBufferSmaller) {
248  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
249 
250  setReadCB(&readCallback_);
251 
252  auto buf = IOBuf::copyBuffer("hello");
253  expectReadBufRequest(3);
254  expectReadData("hel");
255  expectReadBufRequest(3);
256  expectReadData("lo");
257  deliverAppData(std::move(buf));
258 }
259 
260 TEST_F(AsyncFizzBaseTest, TestReadBufferAsync) {
261  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
262 
263  auto buf1 = IOBuf::copyBuffer("buf1");
264  deliverAppData(std::move(buf1));
265  auto buf2 = IOBuf::copyBuffer("buf2");
266  deliverAppData(std::move(buf2));
267 
268  expectReadBufRequest(20);
269  expectReadData("buf1buf2");
270  setReadCB(&readCallback_);
271 
272  auto buf3 = IOBuf::copyBuffer("buf3");
273  expectReadBufRequest(20);
274  expectReadData("buf3");
275  deliverAppData(std::move(buf3));
276 }
277 
278 TEST_F(AsyncFizzBaseTest, TestReadBufferZero) {
279  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
280 
281  setReadCB(&readCallback_);
282 
283  auto buf = IOBuf::copyBuffer("hello");
284  expectReadBufRequest(0);
285  EXPECT_CALL(readCallback_, readErr_(_));
287  deliverAppData(std::move(buf));
288 }
289 
290 TEST_F(AsyncFizzBaseTest, TestReadBufferPause) {
291  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(false));
292 
293  setReadCB(&readCallback_);
294 
295  auto buf = IOBuf::copyBuffer("hello");
296  expectReadBufRequest(3);
297  EXPECT_CALL(readCallback_, readDataAvailable_(3))
298  .InSequence(readBufSeq_)
299  .WillOnce(Invoke([this](size_t len) {
300  EXPECT_TRUE(std::memcmp(readBuf_.data(), "hel", len) == 0);
301  this->setReadCB(nullptr);
302  }));
303  deliverAppData(std::move(buf));
304 
305  expectReadBufRequest(20);
306  expectReadData("lo");
307  setReadCB(&readCallback_);
308 }
309 
310 TEST_F(AsyncFizzBaseTest, TestTransportReadBufMovable) {
311  expectTransportReadCallback();
312  startTransportReads();
313  EXPECT_TRUE(transportReadCallback_->isBufferMovable());
314 }
315 
316 TEST_F(AsyncFizzBaseTest, TestTransportReadBufMove) {
318  expectTransportReadCallback();
319  startTransportReads();
320 
321  auto buf = IOBuf::copyBuffer("hello");
322  EXPECT_CALL(*this, transportDataAvailable());
323  transportReadCallback_->readBufferAvailable(buf->clone());
324  EXPECT_TRUE(eq(*buf, *transportReadBuf_.front()));
325 
326  EXPECT_CALL(*this, transportDataAvailable());
327  transportReadCallback_->readBufferAvailable(IOBuf::copyBuffer("world"));
328  EXPECT_TRUE(eq(*IOBuf::copyBuffer("helloworld"), *transportReadBuf_.front()));
329 }
330 
331 TEST_F(AsyncFizzBaseTest, TestTransportReadBufAvail) {
332  void* buf;
333  size_t len;
335  expectTransportReadCallback();
336  startTransportReads();
337 
338  EXPECT_CALL(*this, transportDataAvailable());
339  transportReadCallback_->getReadBuffer(&buf, &len);
340  // Make sure the buffer is a reasonable size.
341  EXPECT_GE(len, 128);
342  EXPECT_LE(len, 1024 * 64);
343  std::memcpy(buf, "hello", 5);
344  transportReadCallback_->readDataAvailable(5);
345  EXPECT_TRUE(eq(*IOBuf::copyBuffer("hello"), *transportReadBuf_.front()));
346 
347  EXPECT_CALL(*this, transportDataAvailable());
348  transportReadCallback_->getReadBuffer(&buf, &len);
349  std::memcpy(buf, "goodbye", 7);
350  transportReadCallback_->readDataAvailable(7);
351  EXPECT_TRUE(
352  eq(*IOBuf::copyBuffer("hellogoodbye"), *transportReadBuf_.front()));
353 }
354 
355 TEST_F(AsyncFizzBaseTest, TestTransportReadError) {
356  expectTransportReadCallback();
357  startTransportReads();
358 
359  EXPECT_CALL(*this, transportError(_));
360  transportReadCallback_->readErr(ase_);
361 }
362 
363 TEST_F(AsyncFizzBaseTest, TestTransportReadEOF) {
364  expectTransportReadCallback();
365  startTransportReads();
366 
367  EXPECT_CALL(*this, transportError(_))
368  .WillOnce(Invoke([](const AsyncSocketException& ex) {
370  }));
371  transportReadCallback_->readEOF();
372 }
373 
374 TEST_F(AsyncFizzBaseTest, TestTransportReadBufPause) {
375  expectTransportReadCallback();
376  startTransportReads();
377 
378  auto bigBuf = IOBuf::create(1024 * 1024);
379  bigBuf->append(1024 * 1024);
380  expectTransportReadCallback();
381  EXPECT_CALL(*this, transportDataAvailable());
382  transportReadCallback_->readBufferAvailable(std::move(bigBuf));
383  EXPECT_EQ(transportReadCallback_, nullptr);
384 
385  expectTransportReadCallback();
386  setReadCB(&readCallback_);
387  EXPECT_NE(transportReadCallback_, nullptr);
388 }
389 
390 TEST_F(AsyncFizzBaseTest, TestAppReadBufPause) {
391  EXPECT_CALL(readCallback_, isBufferMovable_()).WillRepeatedly(Return(true));
392  expectTransportReadCallback();
393  startTransportReads();
394 
395  auto bigBuf = IOBuf::create(1024 * 1024);
396  bigBuf->append(1024 * 1024);
397  expectTransportReadCallback();
398  deliverAppData(std::move(bigBuf));
399  EXPECT_EQ(transportReadCallback_, nullptr);
400 
401  expectTransportReadCallback();
402  EXPECT_CALL(readCallback_, readBufferAvailable_(_));
403  setReadCB(&readCallback_);
404  EXPECT_NE(transportReadCallback_, nullptr);
405 }
406 
407 TEST_F(AsyncFizzBaseTest, TestWriteSuccess) {
408  AsyncTransportWrapper::WriteCallback* writeCallback = this;
409  writeCallback->writeSuccess();
410 }
411 
412 TEST_F(AsyncFizzBaseTest, TestWriteError) {
413  AsyncTransportWrapper::WriteCallback* writeCallback = this;
414  EXPECT_CALL(*this, transportError(_));
415  writeCallback->writeErr(0, ase_);
416 }
417 
418 TEST_F(AsyncFizzBaseTest, TestHandshakeTimeout) {
419  MockTimeoutManager manager;
420  ON_CALL(manager, isInTimeoutManagerThread()).WillByDefault(Return(true));
421  attachTimeoutManager(&manager);
423 
424  EXPECT_CALL(manager, scheduleTimeout(_, std::chrono::milliseconds(2)))
425  .WillOnce(DoAll(SaveArg<0>(&timeout), Return(true)));
426  startHandshakeTimeout(std::chrono::milliseconds(2));
427 
428  EXPECT_CALL(*this, transportError(_))
429  .WillOnce(Invoke([](const AsyncSocketException& ex) {
431  }));
432  timeout->timeoutExpired();
433 }
434 
435 TEST_F(AsyncFizzBaseTest, TestAttachEventBase) {
436  EventBase evb;
437  expectTransportReadCallback();
438  startTransportReads();
439  ON_CALL(*socket_, good()).WillByDefault(Return(true));
440  Sequence s;
441 
442  EXPECT_CALL(*socket_, setReadCB(nullptr)).InSequence(s);
443  EXPECT_CALL(*socket_, detachEventBase()).InSequence(s);
444  detachEventBase();
445 
446  EXPECT_CALL(*socket_, attachEventBase(&evb)).InSequence(s);
447  EXPECT_CALL(*socket_, setReadCB(transportReadCallback_)).InSequence(s);
448  attachEventBase(&evb);
449 }
450 
451 TEST_F(AsyncFizzBaseTest, TestAttachEventBaseWithReadCb) {
452  EventBase evb;
453  expectTransportReadCallback();
454  startTransportReads();
455  ON_CALL(*socket_, good()).WillByDefault(Return(false));
456  Sequence s;
457 
458  EXPECT_CALL(*socket_, setReadCB(nullptr)).InSequence(s);
459  EXPECT_CALL(*socket_, detachEventBase()).InSequence(s);
460  detachEventBase();
461 
462  expectTransportReadCallback();
463  setReadCB(&readCallback_);
464  EXPECT_CALL(*socket_, attachEventBase(&evb)).InSequence(s);
465  EXPECT_CALL(*socket_, setReadCB(transportReadCallback_)).InSequence(s);
466  attachEventBase(&evb);
467 }
468 } // namespace test
469 } // namespace fizz
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
flags
Definition: http_parser.h:127
void writeAppData(folly::AsyncTransportWrapper::WriteCallback *callback, std::unique_ptr< folly::IOBuf > &&buf, folly::WriteFlags flags=folly::WriteFlags::NONE) override
std::vector< uint8_t > readBuf_
MATCHER_P(WriteNewSessionTicketMatches, expected,"")
std::unique_ptr< X509, X509Deleter > X509UniquePtr
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::array< char, 8192 > readBuf_
StrictMock< folly::test::MockReadCallback > readCallback_
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void expectReadData(const std::string &data)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
void expectReadBufRequest(size_t sizeToGive)
#define MOCK_METHOD3(m,...)
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
AsyncServerSocket::UniquePtr socket_
Definition: Actions.h:16
#define MOCK_CONST_METHOD3(m,...)
virtual void timeoutExpired() noexcept=0
std::shared_ptr< PeerCert > getPeerCert(const CertAndKey &cert)
Definition: Utilities.h:122
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define ON_CALL(obj, call)
std::unique_ptr< AsyncFizzBase, folly::DelayedDestruction::Destructor > UniquePtr
Definition: AsyncFizzBase.h:30
std::unique_ptr< Aead > getCipher(const CipherParams &params)
TEST_F(RSAPSSTest, TestSignVerify)
#define MOCK_METHOD1(m,...)
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
static set< string > s
const
Definition: upload.py:398
#define MOCK_CONST_METHOD0(m,...)
internal::DoBothAction< Action1, Action2 > DoAll(Action1 a1, Action2 a2)
std::string getApplicationProtocol() const noexceptoverride
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
virtual void writeSuccess() noexcept=0
virtual void writeErr(size_t bytesWritten, const AsyncSocketException &ex) noexcept=0
AsyncSocketExceptionType getType() const noexcept
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
int close(NetworkSocket s)
Definition: NetOps.cpp:90
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
internal::ReturnAction< R > Return(R value)
#define MOCK_METHOD0(m,...)