proxygen
SocketFileDescriptorMapTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-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 
18 
19 #include <folly/Portability.h>
24 
25 namespace folly {
26 namespace netops {
27 
30 
31 TEST(SocketFileDescriptorMap, fd_to_socket_consistent) {
32  auto fd = fsp::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
33  auto sockA = SocketFileDescriptorMap::fdToSocket(fd);
34  auto sockB = SocketFileDescriptorMap::fdToSocket(fd);
35  EXPECT_EQ(sockA, sockB);
36 
37  int fd2 = SocketFileDescriptorMap::socketToFd(sockA);
38  EXPECT_EQ(fd, fd2);
39 
41 }
42 
43 TEST(SocketFileDescriptorMap, no_socket_reuse) {
44  auto sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
47  EXPECT_EQ(fdA, fdB);
48 
49  auto sock2 = SocketFileDescriptorMap::fdToSocket(fdA);
50  EXPECT_EQ(sock, sock2);
51 
53 
54  // We're on Windows, so let's do some additional testing to ensure
55  // that we aren't using stale entries in the map.
56  // This is guarded to only on Windows because we need to assert on
57  // the specifics of how file descriptors & sockets are allocated,
58  // which varies between platforms.
59  if (kIsWindows) {
60  auto sock3 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
61  // Nothing else is running in this context, so we should,
62  // in theory, have re-allocated the same socket/file descriptor.
63  EXPECT_EQ(sock, sock3);
64 
65  // Now we mess up the order by creating a new FD.
66  int devNull = ::open("/dev/null", O_RDONLY);
67  EXPECT_EQ(devNull, fdA);
68 
70  EXPECT_NE(fdA, fdC);
71 
73  ::close(devNull);
74  }
75 }
76 
77 TEST(SocketFileDescriptorMap, close_non_mapped_native_socket) {
78  auto sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
80 }
81 
82 } // namespace netops
83 } // namespace folly
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
TEST(SocketFileDescriptorMap, fd_to_socket_consistent)
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
constexpr auto kIsWindows
Definition: Portability.h:367
int close(NetworkSocket s)
Definition: NetOps.cpp:90