proxygen
SocketAddressTest.cpp File Reference
#include <folly/SocketAddress.h>
#include <iostream>
#include <sstream>
#include <system_error>
#include <folly/String.h>
#include <folly/container/Array.h>
#include <folly/experimental/TestUtil.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h>
#include <folly/test/SocketAddressTestHelper.h>

Go to the source code of this file.

Macros

#define REQUIRE_ERRNO(cond, msg)
 

Functions

 TEST (SocketAddress, Size)
 
 TEST (SocketAddress, ConstructFromIpv4)
 
 TEST (SocketAddress, StringConversion)
 
 TEST (SocketAddress, IPv4ToStringConversion)
 
 TEST (SocketAddress, SetFromIpAddressPort)
 
 TEST (SocketAddress, SetFromIpv4)
 
 TEST (SocketAddress, ConstructFromInvalidIpv4)
 
 TEST (SocketAddress, SetFromInvalidIpv4)
 
 TEST (SocketAddress, SetFromHostname)
 
 TEST (SocketAddress, SetFromStrings)
 
 TEST (SocketAddress, EqualityAndHash)
 
 TEST (SocketAddress, IsPrivate)
 
 TEST (SocketAddress, IsLoopback)
 
void CheckPrefixMatch (const SocketAddress &first, const SocketAddress &second, unsigned matchingPrefixLen)
 
 TEST (SocketAddress, PrefixMatch)
 
void CheckFirstLessThanSecond (SocketAddress first, SocketAddress second)
 
 TEST (SocketAddress, CheckComparatorBehavior)
 
 TEST (SocketAddress, Unix)
 
 TEST (SocketAddress, AnonymousUnix)
 
void testSetFromSocket (const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
 
 TEST (SocketAddress, SetFromSocketIPv4)
 
 TEST (SocketAddress, SetFromSocketUnixAbstract)
 
 TEST (SocketAddress, SetFromSocketUnixExplicit)
 
 TEST (SocketAddress, SetFromSocketUnixAnonymous)
 
 TEST (SocketAddress, ResetUnixAddress)
 
 TEST (SocketAddress, ResetIPAddress)
 
 TEST (SocketAddress, ValidFamilyInet)
 

Macro Definition Documentation

#define REQUIRE_ERRNO (   cond,
  msg 
)
Value:
if (!(cond)) { \
ADD_FAILURE() << (msg) << ": " << ::folly::errnoStr(errno); \
}
fbstring errnoStr(int err)
Definition: String.cpp:463
#define ADD_FAILURE()
Definition: gtest.h:1808

Definition at line 674 of file SocketAddressTest.cpp.

Referenced by testSetFromSocket().

Function Documentation

void CheckFirstLessThanSecond ( SocketAddress  first,
SocketAddress  second 
)

Definition at line 470 of file SocketAddressTest.cpp.

References EXPECT_TRUE.

Referenced by TEST().

470  {
471  EXPECT_TRUE(!(first < first));
472  EXPECT_TRUE(!(second < second));
473  EXPECT_TRUE(first < second);
474  EXPECT_TRUE(!(first == second));
475  EXPECT_TRUE(!(second < first));
476 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void CheckPrefixMatch ( const SocketAddress first,
const SocketAddress second,
unsigned  matchingPrefixLen 
)

Definition at line 429 of file SocketAddressTest.cpp.

References EXPECT_TRUE, folly::SocketAddress::getFamily(), i, and folly::SocketAddress::prefixMatch().

Referenced by TEST().

432  {
433  unsigned i;
434  for (i = 0; i <= matchingPrefixLen; i++) {
435  EXPECT_TRUE(first.prefixMatch(second, i));
436  }
437  unsigned addrLen = (first.getFamily() == AF_INET6) ? 128 : 32;
438  for (; i <= addrLen; i++) {
439  EXPECT_TRUE(!first.prefixMatch(second, i));
440  }
441 }
bool prefixMatch(const SocketAddress &other, unsigned prefixLength) const
sa_family_t getFamily() const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SocketAddress  ,
Size   
)

Definition at line 40 of file SocketAddressTest.cpp.

References addr, and EXPECT_EQ.

40  {
42  EXPECT_EQ(sizeof(addr), 32);
43 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
ConstructFromIpv4   
)

Definition at line 45 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, folly::SocketAddress::getAddress(), folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), and folly::SocketAddress::getPort().

45  {
46  SocketAddress addr("1.2.3.4", 4321);
47  EXPECT_EQ(addr.getFamily(), AF_INET);
48  EXPECT_EQ(addr.getAddressStr(), "1.2.3.4");
49  EXPECT_EQ(addr.getPort(), 4321);
50  sockaddr_storage addrStorage;
51  addr.getAddress(&addrStorage);
52  const sockaddr_in* inaddr = reinterpret_cast<sockaddr_in*>(&addrStorage);
53  EXPECT_EQ(inaddr->sin_addr.s_addr, htonl(0x01020304));
54  EXPECT_EQ(inaddr->sin_port, htons(4321));
55 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
StringConversion   
)

Definition at line 57 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, EXPECT_STREQ, folly::SocketAddress::getAddressStr(), and folly::SocketAddress::getFamily().

57  {
58  SocketAddress addr("1.2.3.4", 4321);
59  EXPECT_EQ(addr.getFamily(), AF_INET);
60  EXPECT_EQ(addr.getAddressStr(), "1.2.3.4");
61  char buf[30];
62  addr.getAddressStr(buf, 2);
63  EXPECT_STREQ(buf, "1");
64 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
IPv4ToStringConversion   
)

Definition at line 66 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, folly::SocketAddress::getAddressStr(), i, folly::join(), folly::make_array(), and folly::SocketAddress::setFromIpPort().

66  {
67  // testing addresses *.5.5.5, 5.*.5.5, 5.5.*.5, 5.5.5.*
69  for (int pos = 0; pos < 4; ++pos) {
70  for (int i = 0; i < 256; ++i) {
71  auto fragments = folly::make_array(5, 5, 5, 5);
72  fragments[pos] = i;
73  auto ipString = folly::join(".", fragments);
74  addr.setFromIpPort(ipString, 1234);
75  EXPECT_EQ(addr.getAddressStr(), ipString);
76  }
77  }
78 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr array_detail::return_type< D, TList... > make_array(TList &&...t)
Definition: Array.h:56
void setFromIpPort(const char *ip, uint16_t port)
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
ThreadPoolListHook * addr
std::string getAddressStr() const
TEST ( SocketAddress  ,
SetFromIpAddressPort   
)

Definition at line 80 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), folly::SocketAddress::getIPAddress(), folly::SocketAddress::getPort(), and folly::SocketAddress::setFromIpAddrPort().

80  {
82  folly::IPAddress ipAddr("123.234.0.23");
83  addr.setFromIpAddrPort(ipAddr, 8888);
84  EXPECT_EQ(addr.getFamily(), AF_INET);
85  EXPECT_EQ(addr.getAddressStr(), "123.234.0.23");
86  EXPECT_EQ(addr.getIPAddress(), ipAddr);
87  EXPECT_EQ(addr.getPort(), 8888);
88 
89  folly::IPAddress ip6Addr("2620:0:1cfe:face:b00c::3");
90  SocketAddress addr6(ip6Addr, 8888);
91  EXPECT_EQ(addr6.getFamily(), AF_INET6);
92  EXPECT_EQ(addr6.getAddressStr(), "2620:0:1cfe:face:b00c::3");
93  EXPECT_EQ(addr6.getIPAddress(), ip6Addr);
94  EXPECT_EQ(addr6.getPort(), 8888);
95 }
void setFromIpAddrPort(const IPAddress &ip, uint16_t port)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint16_t getPort() const
sa_family_t getFamily() const
const folly::IPAddress & getIPAddress() const
ThreadPoolListHook * addr
std::string getAddressStr() const
TEST ( SocketAddress  ,
SetFromIpv4   
)

Definition at line 97 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, folly::SocketAddress::getAddress(), folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), folly::SocketAddress::getPort(), and folly::SocketAddress::setFromIpPort().

97  {
99  addr.setFromIpPort("255.254.253.252", 8888);
100  EXPECT_EQ(addr.getFamily(), AF_INET);
101  EXPECT_EQ(addr.getAddressStr(), "255.254.253.252");
102  EXPECT_EQ(addr.getPort(), 8888);
103  sockaddr_storage addrStorage;
104  addr.getAddress(&addrStorage);
105  const sockaddr_in* inaddr = reinterpret_cast<sockaddr_in*>(&addrStorage);
106  EXPECT_EQ(inaddr->sin_addr.s_addr, htonl(0xfffefdfc));
107  EXPECT_EQ(inaddr->sin_port, htons(8888));
108 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint16_t getPort() const
sa_family_t getFamily() const
socklen_t getAddress(sockaddr_storage *addr) const
void setFromIpPort(const char *ip, uint16_t port)
ThreadPoolListHook * addr
std::string getAddressStr() const
TEST ( SocketAddress  ,
ConstructFromInvalidIpv4   
)

Definition at line 110 of file SocketAddressTest.cpp.

References EXPECT_THROW.

110  {
111  EXPECT_THROW(SocketAddress("1.2.3.256", 1234), std::runtime_error);
112 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
TEST ( SocketAddress  ,
SetFromInvalidIpv4   
)

Definition at line 114 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, EXPECT_THROW, folly::SocketAddress::getAddress(), folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), folly::SocketAddress::getPort(), and folly::SocketAddress::setFromIpPort().

114  {
115  SocketAddress addr("12.34.56.78", 80);
116 
117  // Try setting to an invalid value
118  // Since setFromIpPort() shouldn't allow hostname lookups, setting to
119  // "localhost" should fail, even if localhost is resolvable
120  EXPECT_THROW(addr.setFromIpPort("localhost", 1234), std::runtime_error);
121 
122  // Make sure the address still has the old contents
123  EXPECT_EQ(addr.getFamily(), AF_INET);
124  EXPECT_EQ(addr.getAddressStr(), "12.34.56.78");
125  EXPECT_EQ(addr.getPort(), 80);
126  sockaddr_storage addrStorage;
127  addr.getAddress(&addrStorage);
128  const sockaddr_in* inaddr = reinterpret_cast<sockaddr_in*>(&addrStorage);
129  EXPECT_EQ(inaddr->sin_addr.s_addr, htonl(0x0c22384e));
130 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
SetFromHostname   
)

Definition at line 132 of file SocketAddressTest.cpp.

References addr, EXPECT_THROW, folly::SocketAddress::setFromHostPort(), and folly::SocketAddress::setFromIpPort().

132  {
133  // hopefully "localhost" is resolvable on any system that will run the tests
134  EXPECT_THROW(SocketAddress("localhost", 80), std::runtime_error);
135  SocketAddress addr("localhost", 80, true);
136 
137  SocketAddress addr2;
138  EXPECT_THROW(addr2.setFromIpPort("localhost", 0), std::runtime_error);
139  addr2.setFromHostPort("localhost", 0);
140 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void setFromHostPort(const char *host, uint16_t port)
void setFromIpPort(const char *ip, uint16_t port)
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
SetFromStrings   
)

Definition at line 142 of file SocketAddressTest.cpp.

References addr, EXPECT_EQ, EXPECT_THROW, folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), folly::SocketAddress::getPort(), folly::SocketAddress::setFromIpPort(), folly::SocketAddress::setFromLocalIpPort(), and folly::SocketAddress::setFromLocalPort().

142  {
144 
145  // Set from a numeric port string
146  addr.setFromLocalPort("1234");
147  EXPECT_EQ(addr.getPort(), 1234);
148 
149  // setFromLocalPort() should not accept service names
150  EXPECT_THROW(addr.setFromLocalPort("http"), std::runtime_error);
151 
152  // Call setFromLocalIpPort() with just a port, no IP
153  addr.setFromLocalIpPort("80");
154  EXPECT_EQ(addr.getPort(), 80);
155 
156  // Call setFromLocalIpPort() with an IP and port.
157  if (SocketAddressTestHelper::isIPv4Enabled()) {
158  addr.setFromLocalIpPort("127.0.0.1:4321");
159  EXPECT_EQ(addr.getAddressStr(), "127.0.0.1");
160  EXPECT_EQ(addr.getPort(), 4321);
161  }
162  if (SocketAddressTestHelper::isIPv6Enabled()) {
163  addr.setFromLocalIpPort("::1:4321");
164  EXPECT_EQ(addr.getAddressStr(), "::1");
165  EXPECT_EQ(addr.getPort(), 4321);
166  }
167 
168  // setFromIpPort() without an address should fail
169  EXPECT_THROW(addr.setFromIpPort("4321"), std::invalid_argument);
170 
171  // Call setFromIpPort() with an IPv6 address and port
172  addr.setFromIpPort("2620:0:1cfe:face:b00c::3:65535");
173  EXPECT_EQ(addr.getFamily(), AF_INET6);
174  EXPECT_EQ(addr.getAddressStr(), "2620:0:1cfe:face:b00c::3");
175  EXPECT_EQ(addr.getPort(), 65535);
176 
177  // Call setFromIpPort() with an IPv4 address and port
178  addr.setFromIpPort("1.2.3.4:9999");
179  EXPECT_EQ(addr.getFamily(), AF_INET);
180  EXPECT_EQ(addr.getAddressStr(), "1.2.3.4");
181  EXPECT_EQ(addr.getPort(), 9999);
182 
183  // Call setFromIpPort() with a bracketed IPv6
184  addr.setFromIpPort("[::]:1234");
185  EXPECT_EQ(addr.getFamily(), AF_INET6);
186  EXPECT_EQ(addr.getAddressStr(), "::");
187  EXPECT_EQ(addr.getPort(), 1234);
188 
189  // Call setFromIpPort() with a bracketed IPv6
190  addr.setFromIpPort("[9:8::2]:1234");
191  EXPECT_EQ(addr.getFamily(), AF_INET6);
192  EXPECT_EQ(addr.getAddressStr(), "9:8::2");
193  EXPECT_EQ(addr.getPort(), 1234);
194 
195  // Call setFromIpPort() with a bracketed IPv6 and no port
196  EXPECT_THROW(addr.setFromIpPort("[::]"), std::system_error);
197 }
void setFromLocalIpPort(const char *addressAndPort)
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void setFromLocalPort(uint16_t port)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint16_t getPort() const
sa_family_t getFamily() const
void setFromIpPort(const char *ip, uint16_t port)
ThreadPoolListHook * addr
std::string getAddressStr() const
TEST ( SocketAddress  ,
EqualityAndHash   
)

Definition at line 199 of file SocketAddressTest.cpp.

References folly::SocketAddress::convertToIPv4(), EXPECT_EQ, EXPECT_NE, EXPECT_TRUE, folly::SocketAddress::hash(), folly::SocketAddress::isIPv4Mapped(), and folly::SocketAddress::setFromPath().

199  {
200  // IPv4
201  SocketAddress local1("127.0.0.1", 1234);
202  EXPECT_EQ(local1, local1);
203  EXPECT_EQ(local1.hash(), local1.hash());
204 
205  SocketAddress local2("127.0.0.1", 1234);
206  EXPECT_EQ(local1, local2);
207  EXPECT_EQ(local1.hash(), local2.hash());
208 
209  SocketAddress local3("127.0.0.1", 4321);
210  EXPECT_NE(local1, local3);
211  EXPECT_NE(local1.hash(), local3.hash());
212 
213  SocketAddress other1("1.2.3.4", 1234);
214  EXPECT_EQ(other1, other1);
215  EXPECT_EQ(other1.hash(), other1.hash());
216  EXPECT_NE(local1, other1);
217  EXPECT_NE(local1.hash(), other1.hash());
218 
219  SocketAddress other2("4.3.2.1", 1234);
220  EXPECT_NE(other1.hash(), other2.hash());
221  EXPECT_NE(other1.hash(), other2.hash());
222 
223  other2.setFromIpPort("1.2.3.4", 0);
224  EXPECT_NE(other1.hash(), other2.hash());
225  EXPECT_NE(other1.hash(), other2.hash());
226  other2.setPort(1234);
227  EXPECT_EQ(other1.hash(), other2.hash());
228  EXPECT_EQ(other1.hash(), other2.hash());
229 
230  // IPv6
231  SocketAddress v6_1("2620:0:1c00:face:b00c:0:0:abcd", 1234);
232  SocketAddress v6_2("2620:0:1c00:face:b00c::abcd", 1234);
233  SocketAddress v6_3("2620:0:1c00:face:b00c::bcda", 1234);
234  EXPECT_EQ(v6_1, v6_2);
235  EXPECT_EQ(v6_1.hash(), v6_2.hash());
236  EXPECT_NE(v6_1, v6_3);
237  EXPECT_NE(v6_1.hash(), v6_3.hash());
238 
239  // IPv4 versus IPv6 comparison
240  SocketAddress localIPv6("::1", 1234);
241  // Even though these both refer to localhost,
242  // IPv4 and IPv6 addresses are never treated as the same address
243  EXPECT_NE(local1, localIPv6);
244  EXPECT_NE(local1.hash(), localIPv6.hash());
245 
246  // IPv4-mapped IPv6 addresses are not treated as equal
247  // to the equivalent IPv4 address
248  SocketAddress v4("10.0.0.3", 99);
249  SocketAddress v6_mapped1("::ffff:10.0.0.3", 99);
250  SocketAddress v6_mapped2("::ffff:0a00:0003", 99);
251  EXPECT_NE(v4, v6_mapped1);
252  EXPECT_NE(v4, v6_mapped2);
253  EXPECT_EQ(v6_mapped1, v6_mapped2);
254 
255  // However, after calling convertToIPv4(), the mapped address should now be
256  // equal to the v4 version.
257  EXPECT_TRUE(v6_mapped1.isIPv4Mapped());
258  v6_mapped1.convertToIPv4();
259  EXPECT_EQ(v6_mapped1, v4);
260  EXPECT_NE(v6_mapped1, v6_mapped2);
261 
262  // Unix
263  SocketAddress unix1;
264  unix1.setFromPath("/foo");
265  SocketAddress unix2;
266  unix2.setFromPath("/foo");
267  SocketAddress unix3;
268  unix3.setFromPath("/bar");
269  SocketAddress unixAnon;
270  unixAnon.setFromPath("");
271  auto unix5 = SocketAddress::makeFromPath("/foo");
272  auto unixAnon2 = SocketAddress::makeFromPath("");
273 
274  EXPECT_EQ(unix1, unix2);
275  EXPECT_EQ(unix1, unix5);
276  EXPECT_EQ(unix1.hash(), unix2.hash());
277  EXPECT_EQ(unix1.hash(), unix5.hash());
278  EXPECT_NE(unix1, unix3);
279  EXPECT_NE(unix1, unixAnon);
280  EXPECT_NE(unix1, unixAnon2);
281  EXPECT_NE(unix2, unix3);
282  EXPECT_NE(unix5, unix3);
283  EXPECT_NE(unix2, unixAnon);
284  EXPECT_NE(unix2, unixAnon2);
285  EXPECT_NE(unix5, unixAnon);
286  EXPECT_NE(unix5, unixAnon2);
287  // anonymous addresses aren't equal to any other address,
288  // including themselves
289  EXPECT_NE(unixAnon, unixAnon);
290  EXPECT_NE(unixAnon2, unixAnon2);
291 
292  // It isn't strictly required that hashes for different addresses be
293  // different, but we should have very few collisions. It generally indicates
294  // a problem if these collide
295  EXPECT_NE(unix1.hash(), unix3.hash());
296  EXPECT_NE(unix1.hash(), unixAnon.hash());
297  EXPECT_NE(unix3.hash(), unixAnon.hash());
298  EXPECT_NE(unix1.hash(), unixAnon2.hash());
299  EXPECT_NE(unix3.hash(), unixAnon2.hash());
300 }
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
size_t hash() const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( SocketAddress  ,
IsPrivate   
)

Definition at line 302 of file SocketAddressTest.cpp.

References addr, EXPECT_TRUE, folly::SocketAddress::isPrivateAddress(), folly::SocketAddress::setFromIpPort(), and folly::SocketAddress::setFromPath().

302  {
303  // IPv4
304  SocketAddress addr("9.255.255.255", 0);
305  EXPECT_TRUE(!addr.isPrivateAddress());
306  addr.setFromIpPort("10.0.0.0", 0);
307  EXPECT_TRUE(addr.isPrivateAddress());
308  addr.setFromIpPort("10.255.255.255", 0);
309  EXPECT_TRUE(addr.isPrivateAddress());
310  addr.setFromIpPort("11.0.0.0", 0);
311  EXPECT_TRUE(!addr.isPrivateAddress());
312 
313  addr.setFromIpPort("172.15.255.255", 0);
314  EXPECT_TRUE(!addr.isPrivateAddress());
315  addr.setFromIpPort("172.16.0.0", 0);
316  EXPECT_TRUE(addr.isPrivateAddress());
317  addr.setFromIpPort("172.31.255.255", 0);
318  EXPECT_TRUE(addr.isPrivateAddress());
319  addr.setFromIpPort("172.32.0.0", 0);
320  EXPECT_TRUE(!addr.isPrivateAddress());
321 
322  addr.setFromIpPort("192.167.255.255", 0);
323  EXPECT_TRUE(!addr.isPrivateAddress());
324  addr.setFromIpPort("192.168.0.0", 0);
325  EXPECT_TRUE(addr.isPrivateAddress());
326  addr.setFromIpPort("192.168.255.255", 0);
327  EXPECT_TRUE(addr.isPrivateAddress());
328  addr.setFromIpPort("192.169.0.0", 0);
329  EXPECT_TRUE(!addr.isPrivateAddress());
330 
331  addr.setFromIpPort("126.255.255.255", 0);
332  EXPECT_TRUE(!addr.isPrivateAddress());
333  addr.setFromIpPort("127.0.0.0", 0);
334  EXPECT_TRUE(addr.isPrivateAddress());
335  addr.setFromIpPort("127.0.0.1", 0);
336  EXPECT_TRUE(addr.isPrivateAddress());
337  addr.setFromIpPort("127.255.255.255", 0);
338  EXPECT_TRUE(addr.isPrivateAddress());
339  addr.setFromIpPort("128.0.0.0", 0);
340  EXPECT_TRUE(!addr.isPrivateAddress());
341 
342  addr.setFromIpPort("1.2.3.4", 0);
343  EXPECT_TRUE(!addr.isPrivateAddress());
344  addr.setFromIpPort("69.171.239.10", 0);
345  EXPECT_TRUE(!addr.isPrivateAddress());
346 
347  // IPv6
348  addr.setFromIpPort("fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
349  EXPECT_TRUE(!addr.isPrivateAddress());
350  addr.setFromIpPort("fc00::", 0);
351  EXPECT_TRUE(addr.isPrivateAddress());
352  addr.setFromIpPort("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
353  EXPECT_TRUE(addr.isPrivateAddress());
354  addr.setFromIpPort("fe00::", 0);
355  EXPECT_TRUE(!addr.isPrivateAddress());
356 
357  addr.setFromIpPort("fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
358  EXPECT_TRUE(!addr.isPrivateAddress());
359  addr.setFromIpPort("fe80::", 0);
360  EXPECT_TRUE(addr.isPrivateAddress());
361  addr.setFromIpPort("fe80::ffff:ffff:ffff:ffff", 0);
362  EXPECT_TRUE(addr.isPrivateAddress());
363  addr.setFromIpPort("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
364  EXPECT_TRUE(addr.isPrivateAddress());
365  addr.setFromIpPort("fec0::", 0);
366  EXPECT_TRUE(!addr.isPrivateAddress());
367 
368  addr.setFromIpPort("::0", 0);
369  EXPECT_TRUE(!addr.isPrivateAddress());
370  addr.setFromIpPort("2620:0:1c00:face:b00c:0:0:abcd", 0);
371  EXPECT_TRUE(!addr.isPrivateAddress());
372 
373  // IPv4-mapped IPv6
374  addr.setFromIpPort("::ffff:127.0.0.1", 0);
375  EXPECT_TRUE(addr.isPrivateAddress());
376  addr.setFromIpPort("::ffff:10.1.2.3", 0);
377  EXPECT_TRUE(addr.isPrivateAddress());
378  addr.setFromIpPort("::ffff:172.24.0.115", 0);
379  EXPECT_TRUE(addr.isPrivateAddress());
380  addr.setFromIpPort("::ffff:192.168.0.1", 0);
381  EXPECT_TRUE(addr.isPrivateAddress());
382  addr.setFromIpPort("::ffff:69.171.239.10", 0);
383  EXPECT_TRUE(!addr.isPrivateAddress());
384 
385  // Unix sockets are considered private addresses
386  addr.setFromPath("/tmp/mysock");
387  EXPECT_TRUE(addr.isPrivateAddress());
388 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
IsLoopback   
)

Definition at line 390 of file SocketAddressTest.cpp.

References addr, EXPECT_TRUE, folly::SocketAddress::isLoopbackAddress(), folly::SocketAddress::setFromIpPort(), and folly::SocketAddress::setFromPath().

390  {
391  // IPv4
392  SocketAddress addr("127.0.0.1", 0);
393  EXPECT_TRUE(addr.isLoopbackAddress());
394  addr.setFromIpPort("127.0.0.0", 0xffff);
395  EXPECT_TRUE(addr.isLoopbackAddress());
396  addr.setFromIpPort("127.1.1.1", 0xffff);
397  EXPECT_TRUE(addr.isLoopbackAddress());
398  addr.setFromIpPort("127.255.255.255", 80);
399  EXPECT_TRUE(addr.isLoopbackAddress());
400 
401  addr.setFromIpPort("128.0.0.0", 0);
402  EXPECT_TRUE(!addr.isLoopbackAddress());
403  addr.setFromIpPort("126.255.255.255", 0);
404  EXPECT_TRUE(!addr.isLoopbackAddress());
405  addr.setFromIpPort("10.1.2.3", 0);
406  EXPECT_TRUE(!addr.isLoopbackAddress());
407 
408  // IPv6
409  addr.setFromIpPort("::1", 0);
410  EXPECT_TRUE(addr.isLoopbackAddress());
411  addr.setFromIpPort("::0", 0);
412  EXPECT_TRUE(!addr.isLoopbackAddress());
413  addr.setFromIpPort("::2", 0);
414  EXPECT_TRUE(!addr.isLoopbackAddress());
415 
416  // IPv4-mapped IPv6
417  addr.setFromIpPort("::ffff:127.0.0.1", 0);
418  EXPECT_TRUE(addr.isLoopbackAddress());
419  addr.setFromIpPort("::ffff:7f0a:141e", 0);
420  EXPECT_TRUE(addr.isLoopbackAddress());
421  addr.setFromIpPort("::ffff:169.254.0.13", 0);
422  EXPECT_TRUE(!addr.isLoopbackAddress());
423 
424  // Unix sockets are considered loopback addresses
425  addr.setFromPath("/tmp/mysock");
426  EXPECT_TRUE(addr.isLoopbackAddress());
427 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
PrefixMatch   
)

Definition at line 443 of file SocketAddressTest.cpp.

References CheckPrefixMatch(), EXPECT_TRUE, folly::SocketAddress::prefixMatch(), and folly::SocketAddress::setFromIpPort().

443  {
444  // IPv4
445  SocketAddress addr1("127.0.0.1", 0);
446  SocketAddress addr2("127.0.0.1", 0);
447  CheckPrefixMatch(addr1, addr2, 32);
448 
449  addr2.setFromIpPort("127.0.1.1", 0);
450  CheckPrefixMatch(addr1, addr2, 23);
451 
452  addr2.setFromIpPort("1.1.0.127", 0);
453  CheckPrefixMatch(addr1, addr2, 1);
454 
455  // Address family mismatch
456  addr2.setFromIpPort("::ffff:127.0.0.1", 0);
457  EXPECT_TRUE(!addr1.prefixMatch(addr2, 1));
458 
459  // IPv6
460  addr1.setFromIpPort("2a03:2880:10:8f02:face:b00c:0:25", 0);
461  CheckPrefixMatch(addr1, addr2, 2);
462 
463  addr2.setFromIpPort("2a03:2880:10:8f02:face:b00c:0:25", 0);
464  CheckPrefixMatch(addr1, addr2, 128);
465 
466  addr2.setFromIpPort("2a03:2880:30:8f02:face:b00c:0:25", 0);
467  CheckPrefixMatch(addr1, addr2, 42);
468 }
void CheckPrefixMatch(const SocketAddress &first, const SocketAddress &second, unsigned matchingPrefixLen)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( SocketAddress  ,
CheckComparatorBehavior   
)

Definition at line 478 of file SocketAddressTest.cpp.

References CheckFirstLessThanSecond(), folly::gen::first, folly::SocketAddress::setFromIpPort(), and folly::SocketAddress::setFromPath().

478  {
479  SocketAddress first, second;
480  // The following comparison are strict (so if first and second were
481  // inverted that is ok.
482 
483  // IP V4
484 
485  // port comparisions
486  first.setFromIpPort("128.0.0.0", 0);
487  second.setFromIpPort("128.0.0.0", 0xFFFF);
488  CheckFirstLessThanSecond(first, second);
489  first.setFromIpPort("128.0.0.100", 0);
490  second.setFromIpPort("128.0.0.0", 0xFFFF);
491  CheckFirstLessThanSecond(first, second);
492 
493  // Address comparisons
494  first.setFromIpPort("128.0.0.0", 10);
495  second.setFromIpPort("128.0.0.100", 10);
496  CheckFirstLessThanSecond(first, second);
497 
498  // Comaprision between IPV4 and IPV6
499  first.setFromIpPort("128.0.0.0", 0);
500  second.setFromIpPort("::ffff:127.0.0.1", 0);
501  CheckFirstLessThanSecond(first, second);
502  first.setFromIpPort("128.0.0.0", 100);
503  second.setFromIpPort("::ffff:127.0.0.1", 0);
504  CheckFirstLessThanSecond(first, second);
505 
506  // IPV6 comparisons
507 
508  // port comparisions
509  first.setFromIpPort("::0", 0);
510  second.setFromIpPort("::0", 0xFFFF);
511  CheckFirstLessThanSecond(first, second);
512  first.setFromIpPort("::0", 0);
513  second.setFromIpPort("::1", 0xFFFF);
514  CheckFirstLessThanSecond(first, second);
515 
516  // Address comparisons
517  first.setFromIpPort("::0", 10);
518  second.setFromIpPort("::1", 10);
519  CheckFirstLessThanSecond(first, second);
520 
521  // Unix
522  first.setFromPath("/foo");
523  second.setFromPath("/1234");
524  // The exact comparison order doesn't really matter, as long as
525  // (a < b), (b < a), and (a == b) are consistent.
526  // This checks our current comparison rules, which checks the path length
527  // before the path contents.
528  CheckFirstLessThanSecond(first, second);
529  first.setFromPath("/1234");
530  second.setFromPath("/5678");
531  CheckFirstLessThanSecond(first, second);
532 
533  // IPv4 vs Unix.
534  // We currently compare the address family values, and AF_UNIX < AF_INET
535  first.setFromPath("/foo");
536  second.setFromIpPort("127.0.0.1", 80);
537  CheckFirstLessThanSecond(first, second);
538 }
void setFromPath(StringPiece path)
void setFromIpPort(const char *ip, uint16_t port)
constexpr detail::First first
Definition: Base-inl.h:2553
void CheckFirstLessThanSecond(SocketAddress first, SocketAddress second)
TEST ( SocketAddress  ,
Unix   
)

Definition at line 540 of file SocketAddressTest.cpp.

References addr, folly::copy(), folly::SocketAddress::describe(), EXPECT_EQ, EXPECT_NE, EXPECT_THROW, EXPECT_TRUE, folly::SocketAddress::getAddressStr(), folly::SocketAddress::getFamily(), folly::SocketAddress::getPath(), folly::SocketAddress::getPort(), folly::SocketAddress::isLoopbackAddress(), folly::SocketAddress::isPrivateAddress(), folly::gen::move, folly::SocketAddress::setFromHostPort(), folly::SocketAddress::setFromIpPort(), and folly::SocketAddress::setFromPath().

540  {
542 
543  // Test a small path
544  addr.setFromPath("foo");
545  EXPECT_EQ(addr.getFamily(), AF_UNIX);
546  EXPECT_EQ(addr.describe(), "foo");
547  EXPECT_THROW(addr.getAddressStr(), std::invalid_argument);
548  EXPECT_THROW(addr.getPort(), std::invalid_argument);
551 
552  // Test a path that is too large
553  const char longPath[] =
554  "abcdefghijklmnopqrstuvwxyz0123456789"
555  "abcdefghijklmnopqrstuvwxyz0123456789"
556  "abcdefghijklmnopqrstuvwxyz0123456789"
557  "abcdefghijklmnopqrstuvwxyz0123456789";
558  EXPECT_THROW(addr.setFromPath(longPath), std::invalid_argument);
559  // The original address should still be the same
560  EXPECT_EQ(addr.getFamily(), AF_UNIX);
561  EXPECT_EQ(addr.describe(), "foo");
562 
563  // Test a path that exactly fits in sockaddr_un
564  // (not including the NUL terminator)
565  const char exactLengthPath[] =
566  "abcdefghijklmnopqrstuvwxyz0123456789"
567  "abcdefghijklmnopqrstuvwxyz0123456789"
568  "abcdefghijklmnopqrstuvwxyz0123456789";
569  addr.setFromPath(exactLengthPath);
570  EXPECT_EQ(addr.describe(), exactLengthPath);
571 
572  // Test converting a unix socket address to an IPv4 one, then back
573  addr.setFromHostPort("127.0.0.1", 1234);
574  EXPECT_EQ(addr.getFamily(), AF_INET);
575  EXPECT_EQ(addr.describe(), "127.0.0.1:1234");
576  addr.setFromPath("/i/am/a/unix/address");
577  EXPECT_EQ(addr.getFamily(), AF_UNIX);
578  EXPECT_EQ(addr.describe(), "/i/am/a/unix/address");
579 
580  // Test copy constructor and assignment operator
581  {
582  SocketAddress copy(addr);
583  EXPECT_EQ(copy, addr);
584  copy.setFromPath("/abc");
585  EXPECT_NE(copy, addr);
586  copy = addr;
587  EXPECT_EQ(copy, addr);
588  copy.setFromIpPort("127.0.0.1", 80);
589  EXPECT_NE(copy, addr);
590  copy = addr;
591  EXPECT_EQ(copy, addr);
592  }
593 
594  {
595  SocketAddress copy(addr);
596  EXPECT_EQ(copy, addr);
597  EXPECT_EQ(copy.describe(), "/i/am/a/unix/address");
598  EXPECT_EQ(copy.getPath(), "/i/am/a/unix/address");
599 
600  SocketAddress other("127.0.0.1", 80);
601  EXPECT_NE(other, addr);
602  other = copy;
603  EXPECT_EQ(other, copy);
604  EXPECT_EQ(other, addr);
605  EXPECT_EQ(copy, addr);
606  }
607 
608  {
610  {
611  // move a unix address into a non-unix address
612  SocketAddress tmpCopy(addr);
613  copy = std::move(tmpCopy);
614  }
615  EXPECT_EQ(copy, addr);
616 
617  copy.setFromPath("/another/path");
618  {
619  // move a unix address into a unix address
620  SocketAddress tmpCopy(addr);
621  copy = std::move(tmpCopy);
622  }
623  EXPECT_EQ(copy, addr);
624 
625  {
626  // move a non-unix address into a unix address
627  SocketAddress tmp("127.0.0.1", 80);
628  copy = std::move(tmp);
629  }
630  EXPECT_EQ(copy.getAddressStr(), "127.0.0.1");
631  EXPECT_EQ(copy.getPort(), 80);
632 
633  copy = addr;
634  // move construct a unix address
635  SocketAddress other(std::move(copy));
636  EXPECT_EQ(other, addr);
637  EXPECT_EQ(other.getPath(), addr.getPath());
638  }
639 }
void setFromPath(StringPiece path)
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void setFromHostPort(const char *host, uint16_t port)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::string getPath() const
uint16_t getPort() const
std::string describe() const
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
sa_family_t getFamily() const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool isPrivateAddress() const
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
ThreadPoolListHook * addr
bool isLoopbackAddress() const
std::string getAddressStr() const
TEST ( SocketAddress  ,
AnonymousUnix   
)

Definition at line 641 of file SocketAddressTest.cpp.

References folly::netops::close(), folly::SocketAddress::describe(), EXPECT_EQ, EXPECT_NE, folly::SocketAddress::setFromLocalAddress(), folly::SocketAddress::setFromPeerAddress(), and folly::netops::socketpair().

641  {
642  // Create a unix socket pair, and get the addresses.
643  NetworkSocket fds[2];
644  int rc = netops::socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
645  EXPECT_EQ(rc, 0);
646 
647  SocketAddress addr0;
648  SocketAddress peer0;
649  SocketAddress addr1;
650  SocketAddress peer1;
651  addr0.setFromLocalAddress(fds[0]);
652  peer0.setFromPeerAddress(fds[0]);
653  addr1.setFromLocalAddress(fds[1]);
654  peer1.setFromPeerAddress(fds[1]);
655  netops::close(fds[0]);
656  netops::close(fds[1]);
657 
658  EXPECT_EQ(addr0.describe(), "<anonymous unix address>");
659  EXPECT_EQ(addr1.describe(), "<anonymous unix address>");
660  EXPECT_EQ(peer0.describe(), "<anonymous unix address>");
661  EXPECT_EQ(peer1.describe(), "<anonymous unix address>");
662 
663  // Anonymous addresses should never compare equal
664  EXPECT_NE(addr0, addr1);
665  EXPECT_NE(peer0, peer1);
666 
667  // Note that logically addr0 and peer1 are the same,
668  // but since they are both anonymous we have no way to determine this
669  EXPECT_NE(addr0, peer1);
670  // We can't even tell if an anonymous address is equal to itself
671  EXPECT_NE(addr0, addr0);
672 }
void setFromPeerAddress(int socket)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string describe() const
void setFromLocalAddress(int socket)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
int close(NetworkSocket s)
Definition: NetOps.cpp:90
int socketpair(int domain, int type, int protocol, NetworkSocket sv[2])
Definition: NetOps.cpp:416
TEST ( SocketAddress  ,
SetFromSocketIPv4   
)

Definition at line 748 of file SocketAddressTest.cpp.

References EXPECT_EQ, folly::SocketAddress::getPort(), and testSetFromSocket().

748  {
749  SocketAddress serverBindAddr("0.0.0.0", 0);
750  SocketAddress clientBindAddr("0.0.0.0", 0);
751  SocketAddress listenAddr;
752  SocketAddress acceptAddr;
753  SocketAddress serverAddr;
754  SocketAddress serverPeerAddr;
755  SocketAddress clientAddr;
756  SocketAddress clientPeerAddr;
757 
759  &serverBindAddr,
760  &clientBindAddr,
761  &listenAddr,
762  &acceptAddr,
763  &serverAddr,
764  &serverPeerAddr,
765  &clientAddr,
766  &clientPeerAddr);
767 
768  // The server socket's local address should have the same port as the listen
769  // address. The IP will be different, since the listening socket is
770  // listening on INADDR_ANY, but the server socket will have a concrete IP
771  // address assigned to it.
772  EXPECT_EQ(serverAddr.getPort(), listenAddr.getPort());
773 
774  // The client's peer address should always be the same as the server
775  // socket's address.
776  EXPECT_EQ(clientPeerAddr, serverAddr);
777  // The address returned by getpeername() on the server socket should
778  // be the same as the address returned by accept()
779  EXPECT_EQ(serverPeerAddr, acceptAddr);
780  EXPECT_EQ(serverPeerAddr, clientAddr);
781  EXPECT_EQ(acceptAddr, clientAddr);
782 }
void testSetFromSocket(const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
uint16_t getPort() const
TEST ( SocketAddress  ,
SetFromSocketUnixAbstract   
)

Definition at line 787 of file SocketAddressTest.cpp.

References EXPECT_EQ, folly::SocketAddress::setFromPath(), and testSetFromSocket().

787  {
788  // Explicitly binding to an empty path results in an abstract socket
789  // name being picked for us automatically.
790  SocketAddress serverBindAddr;
791  string path(1, 0);
792  path.append("test address");
793  serverBindAddr.setFromPath(path);
794  SocketAddress clientBindAddr;
795  clientBindAddr.setFromPath("");
796 
797  SocketAddress listenAddr;
798  SocketAddress acceptAddr;
799  SocketAddress serverAddr;
800  SocketAddress serverPeerAddr;
801  SocketAddress clientAddr;
802  SocketAddress clientPeerAddr;
803 
805  &serverBindAddr,
806  &clientBindAddr,
807  &listenAddr,
808  &acceptAddr,
809  &serverAddr,
810  &serverPeerAddr,
811  &clientAddr,
812  &clientPeerAddr);
813 
814  // The server socket's local address should be the same as the listen
815  // address.
816  EXPECT_EQ(serverAddr, listenAddr);
817 
818  // The client's peer address should always be the same as the server
819  // socket's address.
820  EXPECT_EQ(clientPeerAddr, serverAddr);
821 
822  EXPECT_EQ(serverPeerAddr, clientAddr);
823  // Oddly, the address returned by accept() does not seem to match the address
824  // returned by getpeername() on the server socket or getsockname() on the
825  // client socket.
826  // EXPECT_EQ(serverPeerAddr, acceptAddr);
827  // EXPECT_EQ(acceptAddr, clientAddr);
828 }
void testSetFromSocket(const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( SocketAddress  ,
SetFromSocketUnixExplicit   
)

Definition at line 830 of file SocketAddressTest.cpp.

References EXPECT_EQ, folly::test::TemporaryDirectory::path(), folly::SocketAddress::setFromPath(), string, and testSetFromSocket().

830  {
831  // Pick two temporary path names.
832  TemporaryDirectory tempDirectory("SocketAddressTest");
833  std::string serverPath = (tempDirectory.path() / "server").string();
834  std::string clientPath = (tempDirectory.path() / "client").string();
835 
836  SocketAddress serverBindAddr;
837  SocketAddress clientBindAddr;
838  SocketAddress listenAddr;
839  SocketAddress acceptAddr;
840  SocketAddress serverAddr;
841  SocketAddress serverPeerAddr;
842  SocketAddress clientAddr;
843  SocketAddress clientPeerAddr;
844  try {
845  serverBindAddr.setFromPath(serverPath.c_str());
846  clientBindAddr.setFromPath(clientPath.c_str());
847 
849  &serverBindAddr,
850  &clientBindAddr,
851  &listenAddr,
852  &acceptAddr,
853  &serverAddr,
854  &serverPeerAddr,
855  &clientAddr,
856  &clientPeerAddr);
857  } catch (...) {
858  // Remove the socket files after we are done
859  unlink(serverPath.c_str());
860  unlink(clientPath.c_str());
861  throw;
862  }
863  unlink(serverPath.c_str());
864  unlink(clientPath.c_str());
865 
866  // The server socket's local address should be the same as the listen
867  // address.
868  EXPECT_EQ(serverAddr, listenAddr);
869 
870  // The client's peer address should always be the same as the server
871  // socket's address.
872  EXPECT_EQ(clientPeerAddr, serverAddr);
873 
874  EXPECT_EQ(serverPeerAddr, clientAddr);
875  EXPECT_EQ(serverPeerAddr, acceptAddr);
876  EXPECT_EQ(acceptAddr, clientAddr);
877 }
void testSetFromSocket(const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * string
Definition: Conv.cpp:212
TEST ( SocketAddress  ,
SetFromSocketUnixAnonymous   
)

Definition at line 879 of file SocketAddressTest.cpp.

References EXPECT_EQ, folly::SocketAddress::getPath(), folly::test::TemporaryDirectory::path(), folly::SocketAddress::setFromPath(), string, and testSetFromSocket().

879  {
880  // Test an anonymous client talking to a fixed-path unix socket.
881  TemporaryDirectory tempDirectory("SocketAddressTest");
882  std::string serverPath = (tempDirectory.path() / "server").string();
883 
884  SocketAddress serverBindAddr;
885  SocketAddress listenAddr;
886  SocketAddress acceptAddr;
887  SocketAddress serverAddr;
888  SocketAddress serverPeerAddr;
889  SocketAddress clientAddr;
890  SocketAddress clientPeerAddr;
891  try {
892  serverBindAddr.setFromPath(serverPath.c_str());
893 
895  &serverBindAddr,
896  nullptr,
897  &listenAddr,
898  &acceptAddr,
899  &serverAddr,
900  &serverPeerAddr,
901  &clientAddr,
902  &clientPeerAddr);
903  } catch (...) {
904  // Remove the socket file after we are done
905  unlink(serverPath.c_str());
906  throw;
907  }
908  unlink(serverPath.c_str());
909 
910  // The server socket's local address should be the same as the listen
911  // address.
912  EXPECT_EQ(serverAddr, listenAddr);
913 
914  // The client's peer address should always be the same as the server
915  // socket's address.
916  EXPECT_EQ(clientPeerAddr, serverAddr);
917 
918  // Since the client is using an anonymous address, it won't compare equal to
919  // any other anonymous addresses. Make sure the addresses are anonymous.
920  EXPECT_EQ(serverPeerAddr.getPath(), "");
921  EXPECT_EQ(clientAddr.getPath(), "");
922  EXPECT_EQ(acceptAddr.getPath(), "");
923 }
void testSetFromSocket(const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::string getPath() const
const char * string
Definition: Conv.cpp:212
TEST ( SocketAddress  ,
ResetUnixAddress   
)

Definition at line 925 of file SocketAddressTest.cpp.

References EXPECT_EQ, folly::SocketAddress::getFamily(), folly::SocketAddress::reset(), and folly::SocketAddress::setFromPath().

925  {
926  SocketAddress addy;
927  addy.setFromPath("/foo");
928 
929  addy.reset();
930  EXPECT_EQ(addy.getFamily(), AF_UNSPEC);
931 }
void setFromPath(StringPiece path)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
sa_family_t getFamily() const
TEST ( SocketAddress  ,
ResetIPAddress   
)

Definition at line 933 of file SocketAddressTest.cpp.

References addr, folly::SocketAddress::empty(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::SocketAddress::getFamily(), folly::SocketAddress::isInitialized(), folly::SocketAddress::reset(), and folly::SocketAddress::setFromIpPort().

933  {
935  addr.setFromIpPort("127.0.0.1", 80);
936  addr.reset();
937  EXPECT_EQ(addr.getFamily(), AF_UNSPEC);
938  EXPECT_FALSE(addr.isInitialized());
939  EXPECT_TRUE(addr.empty());
940 
941  addr.setFromIpPort("2620:0:1cfe:face:b00c::3:65535");
942  addr.reset();
943  EXPECT_EQ(addr.getFamily(), AF_UNSPEC);
944  EXPECT_FALSE(addr.isInitialized());
945  EXPECT_TRUE(addr.empty());
946 }
bool empty() const
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
sa_family_t getFamily() const
bool isInitialized() const
void setFromIpPort(const char *ip, uint16_t port)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
ThreadPoolListHook * addr
TEST ( SocketAddress  ,
ValidFamilyInet   
)

Definition at line 948 of file SocketAddressTest.cpp.

References addr, EXPECT_FALSE, EXPECT_TRUE, folly::SocketAddress::isFamilyInet(), and folly::SocketAddress::setFromIpAddrPort().

948  {
950  EXPECT_FALSE(addr.isFamilyInet());
951  folly::IPAddress ipAddr("123.234.0.23");
952  addr.setFromIpAddrPort(ipAddr, 8888);
953  EXPECT_TRUE(addr.isFamilyInet());
954 
955  folly::IPAddress ip6Addr("2620:0:1cfe:face:b00c::3");
956  SocketAddress addr6(ip6Addr, 8888);
957  EXPECT_TRUE(addr6.isFamilyInet());
958 }
void setFromIpAddrPort(const IPAddress &ip, uint16_t port)
bool isFamilyInet() const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
ThreadPoolListHook * addr
void testSetFromSocket ( const SocketAddress serverBindAddr,
const SocketAddress clientBindAddr,
SocketAddress listenAddrRet,
SocketAddress acceptAddrRet,
SocketAddress serverAddrRet,
SocketAddress serverPeerAddrRet,
SocketAddress clientAddrRet,
SocketAddress clientPeerAddrRet 
)

Definition at line 679 of file SocketAddressTest.cpp.

References folly::netops::accept(), folly::netops::bind(), folly::netops::close(), folly::netops::connect(), EXPECT_THROW, folly::SocketAddress::getActualSize(), folly::SocketAddress::getAddress(), folly::SocketAddress::getFamily(), folly::netops::listen(), REQUIRE_ERRNO, folly::SocketAddress::setFromLocalAddress(), folly::SocketAddress::setFromPeerAddress(), folly::SocketAddress::setFromSockaddr(), and folly::netops::socket().

Referenced by TEST().

687  {
688  auto listenSock = netops::socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
690  listenSock != NetworkSocket(), "failed to create listen socket");
691  sockaddr_storage laddr;
692  serverBindAddr->getAddress(&laddr);
693  socklen_t laddrLen = serverBindAddr->getActualSize();
694  int rc =
695  netops::bind(listenSock, reinterpret_cast<sockaddr*>(&laddr), laddrLen);
696  REQUIRE_ERRNO(rc == 0, "failed to bind to server socket");
697  rc = netops::listen(listenSock, 10);
698  REQUIRE_ERRNO(rc == 0, "failed to listen");
699 
700  listenAddrRet->setFromLocalAddress(listenSock);
701 
702  SocketAddress listenPeerAddr;
703  EXPECT_THROW(
704  listenPeerAddr.setFromPeerAddress(listenSock), std::runtime_error);
705 
706  // Note that we use the family from serverBindAddr here, since we allow
707  // clientBindAddr to be nullptr.
708  auto clientSock = netops::socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
710  clientSock != NetworkSocket(), "failed to create client socket");
711  if (clientBindAddr != nullptr) {
712  sockaddr_storage clientAddr;
713  clientBindAddr->getAddress(&clientAddr);
714 
715  rc = netops::bind(
716  clientSock,
717  reinterpret_cast<sockaddr*>(&clientAddr),
718  clientBindAddr->getActualSize());
719  REQUIRE_ERRNO(rc == 0, "failed to bind to client socket");
720  }
721 
722  sockaddr_storage listenAddr;
723  listenAddrRet->getAddress(&listenAddr);
724  rc = netops::connect(
725  clientSock,
726  reinterpret_cast<sockaddr*>(&listenAddr),
727  listenAddrRet->getActualSize());
728  REQUIRE_ERRNO(rc == 0, "failed to connect");
729 
730  sockaddr_storage acceptAddr;
731  socklen_t acceptAddrLen = sizeof(acceptAddr);
732  auto serverSock = netops::accept(
733  listenSock, reinterpret_cast<sockaddr*>(&acceptAddr), &acceptAddrLen);
734  REQUIRE_ERRNO(serverSock != NetworkSocket(), "failed to accept");
735  acceptAddrRet->setFromSockaddr(
736  reinterpret_cast<sockaddr*>(&acceptAddr), acceptAddrLen);
737 
738  serverAddrRet->setFromLocalAddress(serverSock);
739  serverPeerAddrRet->setFromPeerAddress(serverSock);
740  clientAddrRet->setFromLocalAddress(clientSock);
741  clientPeerAddrRet->setFromPeerAddress(clientSock);
742 
743  netops::close(clientSock);
744  netops::close(serverSock);
745  netops::close(listenSock);
746 }
#define REQUIRE_ERRNO(cond, msg)
void setFromPeerAddress(int socket)
int connect(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:94
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
socklen_t getActualSize() const
void setFromSockaddr(const struct sockaddr *address)
sa_family_t getFamily() const
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
socklen_t getAddress(sockaddr_storage *addr) const
int listen(NetworkSocket s, int backlog)
Definition: NetOps.cpp:137
void setFromLocalAddress(int socket)
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
int close(NetworkSocket s)
Definition: NetOps.cpp:90
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71