proxygen
AsyncSocketTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-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  */
16 #include <iostream>
17 
22 
23 namespace folly {
24 
25 #ifndef TCP_SAVE_SYN
26 #define TCP_SAVE_SYN 27
27 #endif
28 
29 TEST(AsyncSocketTest, getSockOpt) {
30  EventBase evb;
31  std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb, 0);
32 
33  int val;
34  socklen_t len;
35 
36  int expectedRc =
37  getsockopt(socket->getFd(), SOL_SOCKET, SO_REUSEADDR, &val, &len);
38  int actualRc = socket->getSockOpt(SOL_SOCKET, SO_REUSEADDR, &val, &len);
39 
40  EXPECT_EQ(expectedRc, actualRc);
41 }
42 
43 TEST(AsyncSocketTest, REUSEPORT) {
44  EventBase base;
45  auto serverSocket = AsyncServerSocket::newSocket(&base);
46  serverSocket->bind(0);
47  serverSocket->listen(0);
48  serverSocket->startAccepting();
49 
50  try {
51  serverSocket->setReusePortEnabled(true);
52  } catch (...) {
53  LOG(INFO) << "Reuse port probably not supported";
54  return;
55  }
56 
57  SocketAddress address;
58  serverSocket->getAddress(&address);
59  int port = address.getPort();
60 
61  auto serverSocket2 = AsyncServerSocket::newSocket(&base);
62  serverSocket2->setReusePortEnabled(true);
63  serverSocket2->bind(port);
64  serverSocket2->listen(0);
65  serverSocket2->startAccepting();
66 }
67 
68 TEST(AsyncSocketTest, v4v6samePort) {
69  EventBase base;
70  auto serverSocket = AsyncServerSocket::newSocket(&base);
71  serverSocket->bind(0);
72  auto addrs = serverSocket->getAddresses();
73  ASSERT_GT(addrs.size(), 0);
74  uint16_t port = addrs[0].getPort();
75  for (const auto& addr : addrs) {
76  EXPECT_EQ(port, addr.getPort());
77  }
78 }
79 
80 TEST(AsyncSocketTest, duplicateBind) {
81  EventBase base;
82  auto server1 = AsyncServerSocket::newSocket(&base);
83  server1->bind(0);
84  server1->listen(10);
85 
86  SocketAddress address;
87  server1->getAddress(std::addressof(address));
88 
89  auto server2 = AsyncServerSocket::newSocket(&base);
90  EXPECT_THROW(server2->bind(address.getPort()), std::exception);
91 }
92 
93 TEST(AsyncSocketTest, tosReflect) {
94  EventBase base;
95  auto server1 = AsyncServerSocket::newSocket(&base);
96  server1->bind(0);
97  server1->listen(10);
98  int fd = server1->getSocket();
99 
100  // Verify if tos reflect is disabled by default
101  // and the TCP_SAVE_SYN setting is not enabled
102  EXPECT_FALSE(server1->getTosReflect());
103  int value;
104  socklen_t valueLength = sizeof(value);
105  int rc = getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength);
106  ASSERT_EQ(rc, 0);
107  ASSERT_EQ(value, 0);
108 
109  // Enable TOS reflect on the server socket
110  server1->setTosReflect(true);
111 
112  // Verify if tos reflect is enabled now
113  // and the TCP_SAVE_SYN setting is also enabled
114  EXPECT_TRUE(server1->getTosReflect());
115  rc = getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength);
116  ASSERT_EQ(rc, 0);
117  ASSERT_EQ(value, 1);
118 }
119 
120 } // namespace folly
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define TCP_SAVE_SYN
double val
Definition: String.cpp:273
uint16_t getPort() const
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
int getsockopt(NetworkSocket s, int level, int optname, void *optval, socklen_t *optlen)
Definition: NetOps.cpp:112
static std::shared_ptr< AsyncServerSocket > newSocket(EventBase *evb=nullptr)
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
socklen_t getAddress(sockaddr_storage *addr) const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static std::shared_ptr< AsyncSocket > newSocket(EventBase *evb)
Definition: AsyncSocket.h:281
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
ThreadPoolListHook * addr
TEST(SequencedExecutor, CPUThreadPoolExecutor)