proxygen
SocketPeekerTest.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 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
23 
24 using namespace folly;
25 using namespace folly::test;
26 using namespace wangle;
27 using namespace testing;
28 
30  public:
31  ~MockSocketPeekerCallback() override = default;
32 
33  MOCK_METHOD1(peekSuccess_, void(typename std::vector<uint8_t>));
34  void peekSuccess(std::vector<uint8_t> peekBytes) noexcept override {
35  peekSuccess_(peekBytes);
36  }
37 
38  MOCK_METHOD1(peekError_, void(const folly::AsyncSocketException&));
39  void peekError(const folly::AsyncSocketException& ex) noexcept override {
40  peekError_(ex);
41  }
42 };
43 
44 class SocketPeekerTest : public Test {
45  public:
46  void SetUp() override {
47  sock = new MockAsyncSocket(&base);
48  }
49 
50  void TearDown() override {
51  sock->destroy();
52  }
53 
57 };
58 
59 MATCHER_P2(BufMatches, buf, len, "") {
60  if (arg.size() != size_t(len)) {
61  return false;
62  } else if (len == 0) {
63  return true;
64  }
65  return memcmp(buf, arg.data(), len) == 0;
66 }
67 
68 MATCHER_P2(IOBufMatches, buf, len, "") {
69  return folly::IOBufEqualTo()(arg, folly::IOBuf::copyBuffer(buf, len));
70 }
71 
72 TEST_F(SocketPeekerTest, TestPeekSuccess) {
73  EXPECT_CALL(*sock, setReadCB(_));
74  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
75  peeker->start();
76 
77  uint8_t* buf = nullptr;
78  size_t len = 0;
79  peeker->getReadBuffer(reinterpret_cast<void**>(&buf), &len);
80  EXPECT_EQ(2, len);
81  // first 2 bytes of SSL3+.
82  buf[0] = 0x16;
83  buf[1] = 0x03;
84  EXPECT_CALL(*sock, _setPreReceivedData(IOBufMatches(buf, 2)));
85  EXPECT_CALL(callback, peekSuccess_(BufMatches(buf, 2)));
86  // once after peeking, and once during destruction.
87  EXPECT_CALL(*sock, setReadCB(nullptr));
88  peeker->readDataAvailable(2);
89 }
90 
91 TEST_F(SocketPeekerTest, TestEOFDuringPeek) {
92  EXPECT_CALL(*sock, setReadCB(_));
93  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
94  peeker->start();
95 
96  EXPECT_CALL(callback, peekError_(_));
97  EXPECT_CALL(*sock, setReadCB(nullptr));
98  peeker->readEOF();
99 }
100 
101 TEST_F(SocketPeekerTest, TestNotEnoughDataError) {
102  EXPECT_CALL(*sock, setReadCB(_));
103  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
104  peeker->start();
105 
106  uint8_t* buf = nullptr;
107  size_t len = 0;
108  peeker->getReadBuffer(reinterpret_cast<void**>(&buf), &len);
109  EXPECT_EQ(2, len);
110  buf[0] = 0x16;
111  peeker->readDataAvailable(1);
112 
113  EXPECT_CALL(callback, peekError_(_));
114  EXPECT_CALL(*sock, setReadCB(nullptr));
115  peeker->readEOF();
116 }
117 
118 TEST_F(SocketPeekerTest, TestMultiplePeeks) {
119  EXPECT_CALL(*sock, setReadCB(_));
120  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
121  peeker->start();
122 
123  uint8_t* buf = nullptr;
124  size_t len = 0;
125  peeker->getReadBuffer(reinterpret_cast<void**>(&buf), &len);
126  EXPECT_EQ(2, len);
127  buf[0] = 0x16;
128  peeker->readDataAvailable(1);
129 
130  peeker->getReadBuffer(reinterpret_cast<void**>(&buf), &len);
131  EXPECT_EQ(1, len);
132  buf[0] = 0x03;
133 
134  EXPECT_CALL(*sock, _setPreReceivedData(IOBufMatches("\x16\x03", 2)));
135  EXPECT_CALL(callback, peekSuccess_(BufMatches("\x16\x03", 2)));
136  EXPECT_CALL(*sock, setReadCB(nullptr));
137  peeker->readDataAvailable(1);
138 }
139 
140 TEST_F(SocketPeekerTest, TestDestoryWhilePeeking) {
141  EXPECT_CALL(*sock, setReadCB(_));
142  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
143  peeker->start();
144  peeker = nullptr;
145 }
146 
147 TEST_F(SocketPeekerTest, TestNoPeekSuccess) {
148  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 0));
149 
150  char buf = '\0';
151  EXPECT_CALL(callback, peekSuccess_(BufMatches(&buf, 0)));
152  peeker->start();
153 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
MockSocketPeekerCallback callback
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
MockAsyncSocket * sock
void TearDown() override
void SetUp() override
void peekSuccess(std::vector< uint8_t > peekBytes) noexceptoverride
MATCHER_P2(BufMatches, buf, len,"")
#define MOCK_METHOD1(m,...)
void peekError(const folly::AsyncSocketException &ex) noexceptoverride
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
TEST_F(FileUtilTest, read)
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::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28