proxygen
SocketPeekerTest.cpp File Reference
#include <wangle/acceptor/SocketPeeker.h>
#include <thread>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <folly/io/async/test/MockAsyncSocket.h>

Go to the source code of this file.

Classes

class  MockSocketPeekerCallback
 
class  SocketPeekerTest
 

Functions

 MATCHER_P2 (BufMatches, buf, len,"")
 
 MATCHER_P2 (IOBufMatches, buf, len,"")
 
 TEST_F (SocketPeekerTest, TestPeekSuccess)
 
 TEST_F (SocketPeekerTest, TestEOFDuringPeek)
 
 TEST_F (SocketPeekerTest, TestNotEnoughDataError)
 
 TEST_F (SocketPeekerTest, TestMultiplePeeks)
 
 TEST_F (SocketPeekerTest, TestDestoryWhilePeeking)
 
 TEST_F (SocketPeekerTest, TestNoPeekSuccess)
 

Function Documentation

MATCHER_P2 ( BufMatches  ,
buf  ,
len  ,
""   
)

Definition at line 59 of file SocketPeekerTest.cpp.

59  {
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 }
MATCHER_P2 ( IOBufMatches  ,
buf  ,
len  ,
""   
)

Definition at line 68 of file SocketPeekerTest.cpp.

References folly::IOBuf::copyBuffer().

68  {
69  return folly::IOBufEqualTo()(arg, folly::IOBuf::copyBuffer(buf, len));
70 }
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
TEST_F ( SocketPeekerTest  ,
TestPeekSuccess   
)

Definition at line 72 of file SocketPeekerTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_EQ, and uint8_t.

72  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
TEST_F ( SocketPeekerTest  ,
TestEOFDuringPeek   
)

Definition at line 91 of file SocketPeekerTest.cpp.

References testing::_, and EXPECT_CALL.

91  {
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 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
TEST_F ( SocketPeekerTest  ,
TestNotEnoughDataError   
)

Definition at line 101 of file SocketPeekerTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_EQ, and uint8_t.

101  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
TEST_F ( SocketPeekerTest  ,
TestMultiplePeeks   
)

Definition at line 118 of file SocketPeekerTest.cpp.

References testing::_, EXPECT_CALL, EXPECT_EQ, and uint8_t.

118  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
TEST_F ( SocketPeekerTest  ,
TestDestoryWhilePeeking   
)

Definition at line 140 of file SocketPeekerTest.cpp.

References testing::_, and EXPECT_CALL.

140  {
141  EXPECT_CALL(*sock, setReadCB(_));
142  SocketPeeker::UniquePtr peeker(new SocketPeeker(*sock, &callback, 2));
143  peeker->start();
144  peeker = nullptr;
145 }
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28
TEST_F ( SocketPeekerTest  ,
TestNoPeekSuccess   
)

Definition at line 147 of file SocketPeekerTest.cpp.

References EXPECT_CALL.

147  {
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_CALL(obj, call)
std::unique_ptr< SocketPeeker, folly::DelayedDestruction::Destructor > UniquePtr
Definition: SocketPeeker.h:28