proxygen
PlaintextRecordTest.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 
14 #include <folly/String.h>
15 
16 using namespace folly;
17 using namespace folly::io;
18 
19 using testing::_;
20 using namespace testing;
21 
22 namespace fizz {
23 namespace test {
24 
26  protected:
29 
31 
33 
34  Buf getBuf(const std::string& hex) {
35  auto data = unhexlify(hex);
36  return IOBuf::copyBuffer(data.data(), data.size());
37  }
38 
39  void addToQueue(const std::string& hex) {
40  queue_.append(getBuf(hex));
41  }
42 
43  void expectSame(const Buf& buf, const std::string& hex) {
44  auto str = buf->moveToFbString().toStdString();
45  EXPECT_EQ(hexlify(str), hex);
46  }
47 };
48 
49 TEST_F(PlaintextRecordTest, TestReadEmpty) {
50  EXPECT_FALSE(read_.read(queue_).hasValue());
51 }
52 
53 TEST_F(PlaintextRecordTest, TestReadHandshake) {
54  addToQueue("16030100050123456789");
55  auto msg = read_.read(queue_);
56  EXPECT_EQ(msg->type, ContentType::handshake);
57  expectSame(msg->fragment, "0123456789");
58  EXPECT_TRUE(queue_.empty());
59 }
60 
61 TEST_F(PlaintextRecordTest, TestReadAlert) {
62  addToQueue("15030100050123456789");
63  auto msg = read_.read(queue_);
64  EXPECT_EQ(msg->type, ContentType::alert);
65  expectSame(msg->fragment, "0123456789");
66  EXPECT_TRUE(queue_.empty());
67 }
68 
69 TEST_F(PlaintextRecordTest, TestReadAppData) {
70  addToQueue("17030100050123456789");
71  EXPECT_ANY_THROW(read_.read(queue_));
72 }
73 
74 TEST_F(PlaintextRecordTest, TestWaitForData) {
75  addToQueue("160301000512345678");
76  EXPECT_FALSE(read_.read(queue_).hasValue());
77  EXPECT_EQ(queue_.chainLength(), 9);
78 }
79 
80 TEST_F(PlaintextRecordTest, TestWaitForHeader) {
81  addToQueue("16030102");
82  EXPECT_FALSE(read_.read(queue_).hasValue());
83  EXPECT_EQ(queue_.chainLength(), 4);
84 }
85 
86 TEST_F(PlaintextRecordTest, TestMaxSize) {
87  addToQueue("1603014000");
88  EXPECT_FALSE(read_.read(queue_).hasValue());
89  EXPECT_EQ(queue_.chainLength(), 5);
90 }
91 
92 TEST_F(PlaintextRecordTest, TestOverSize) {
93  addToQueue("1603014001");
94  EXPECT_ANY_THROW(read_.read(queue_));
95 }
96 
98  addToQueue("1603010000aa");
99  EXPECT_ANY_THROW(read_.read(queue_));
100 }
101 
102 TEST_F(PlaintextRecordTest, TestDataRemaining) {
103  addToQueue("16030100050123456789160301");
104  auto msg = read_.read(queue_);
105  EXPECT_EQ(msg->type, ContentType::handshake);
106  expectSame(msg->fragment, "0123456789");
107  EXPECT_EQ(queue_.chainLength(), 3);
108  expectSame(queue_.move(), "160301");
109 }
110 
111 TEST_F(PlaintextRecordTest, TestSkipAndWait) {
112  read_.setSkipEncryptedRecords(true);
113  addToQueue("17030100050123456789");
114  EXPECT_FALSE(read_.read(queue_).hasValue());
115  EXPECT_TRUE(queue_.empty());
116 }
117 
118 TEST_F(PlaintextRecordTest, TestWaitBeforeSkip) {
119  read_.setSkipEncryptedRecords(true);
120  addToQueue("170301000501234567");
121  EXPECT_FALSE(read_.read(queue_).hasValue());
122  expectSame(queue_.move(), "170301000501234567");
123 }
124 
125 TEST_F(PlaintextRecordTest, TestSkipAndRead) {
126  read_.setSkipEncryptedRecords(true);
127  addToQueue("170301000501234567891703010005012345678916030100050123456789");
128  auto msg = read_.read(queue_);
129  EXPECT_EQ(msg->type, ContentType::handshake);
130  expectSame(msg->fragment, "0123456789");
131  EXPECT_TRUE(queue_.empty());
132 }
133 
134 TEST_F(PlaintextRecordTest, TestWriteHandshake) {
135  TLSMessage msg{ContentType::handshake, getBuf("1234567890")};
136  auto buf = write_.write(std::move(msg));
137  expectSame(buf.data, "16030300051234567890");
138 }
139 
140 TEST_F(PlaintextRecordTest, TestWriteClientHello) {
141  auto buf = write_.writeInitialClientHello(getBuf("1234567890"));
142  expectSame(buf.data, "16030100051234567890");
143 }
144 
145 TEST_F(PlaintextRecordTest, TestWriteAppData) {
146  TLSMessage msg{ContentType::application_data};
147  EXPECT_ANY_THROW(write_.write(std::move(msg)));
148 }
149 
150 TEST_F(PlaintextRecordTest, TestFragmentedWrite) {
151  TLSMessage msg{ContentType::handshake, IOBuf::create(0)};
152  auto buf = IOBuf::create(0x4010);
153  buf->append(0x4010);
154  memset(buf->writableData(), 0x1, buf->length());
155  msg.fragment->prependChain(std::move(buf));
156  auto write = write_.write(std::move(msg));
157 
158  TLSMessage msg1{ContentType::handshake, IOBuf::create(0)};
159  buf = IOBuf::create(0x4000);
160  buf->append(0x4000);
161  memset(buf->writableData(), 0x1, buf->length());
162  msg1.fragment->prependChain(std::move(buf));
163  auto write1 = write_.write(std::move(msg1));
164 
165  TLSMessage msg2{ContentType::handshake, IOBuf::create(0)};
166  buf = IOBuf::create(0x10);
167  buf->append(0x10);
168  memset(buf->writableData(), 0x1, buf->length());
169  msg2.fragment->prependChain(std::move(buf));
170  auto write2 = write_.write(std::move(msg2));
171 
172  write1.data->prependChain(std::move(write2.data));
174  EXPECT_TRUE(eq(write.data, write1.data));
175 }
176 } // namespace test
177 } // namespace fizz
#define EXPECT_ANY_THROW(statement)
Definition: gtest.h:1847
Buf getBuf(const std::string &hex)
bool unhexlify(const InputString &input, OutputString &output)
Definition: String-inl.h:616
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static Options cacheChainLength()
Definition: IOBufQueue.h:83
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
Definition: Actions.h:16
void addToQueue(const std::string &hex)
void expectSame(const Buf &buf, const std::string &hex)
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
const internal::AnythingMatcher _
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool hexlify(const InputString &input, OutputString &output, bool append_output)
Definition: String-inl.h:596
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
PlaintextWriteRecordLayer write_