proxygen
SocketAddressTest.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 
17 #include <folly/SocketAddress.h>
18 
19 #include <iostream>
20 #include <sstream>
21 #include <system_error>
22 
23 #include <folly/String.h>
24 #include <folly/container/Array.h>
29 
34 using std::cerr;
35 using std::endl;
36 using std::string;
37 
38 namespace netops = folly::netops;
39 
42  EXPECT_EQ(sizeof(addr), 32);
43 }
44 
45 TEST(SocketAddress, ConstructFromIpv4) {
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 }
56 
57 TEST(SocketAddress, StringConversion) {
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 }
65 
66 TEST(SocketAddress, IPv4ToStringConversion) {
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 }
79 
80 TEST(SocketAddress, SetFromIpAddressPort) {
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 }
96 
97 TEST(SocketAddress, SetFromIpv4) {
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 }
109 
110 TEST(SocketAddress, ConstructFromInvalidIpv4) {
111  EXPECT_THROW(SocketAddress("1.2.3.256", 1234), std::runtime_error);
112 }
113 
114 TEST(SocketAddress, SetFromInvalidIpv4) {
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 }
131 
132 TEST(SocketAddress, SetFromHostname) {
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 }
141 
142 TEST(SocketAddress, SetFromStrings) {
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 }
198 
199 TEST(SocketAddress, EqualityAndHash) {
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 }
301 
302 TEST(SocketAddress, IsPrivate) {
303  // IPv4
304  SocketAddress addr("9.255.255.255", 0);
305  EXPECT_TRUE(!addr.isPrivateAddress());
306  addr.setFromIpPort("10.0.0.0", 0);
308  addr.setFromIpPort("10.255.255.255", 0);
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);
317  addr.setFromIpPort("172.31.255.255", 0);
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);
326  addr.setFromIpPort("192.168.255.255", 0);
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);
335  addr.setFromIpPort("127.0.0.1", 0);
337  addr.setFromIpPort("127.255.255.255", 0);
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);
352  addr.setFromIpPort("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
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);
361  addr.setFromIpPort("fe80::ffff:ffff:ffff:ffff", 0);
363  addr.setFromIpPort("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0);
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);
376  addr.setFromIpPort("::ffff:10.1.2.3", 0);
378  addr.setFromIpPort("::ffff:172.24.0.115", 0);
380  addr.setFromIpPort("::ffff:192.168.0.1", 0);
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");
388 }
389 
390 TEST(SocketAddress, IsLoopback) {
391  // IPv4
392  SocketAddress addr("127.0.0.1", 0);
394  addr.setFromIpPort("127.0.0.0", 0xffff);
396  addr.setFromIpPort("127.1.1.1", 0xffff);
398  addr.setFromIpPort("127.255.255.255", 80);
400 
401  addr.setFromIpPort("128.0.0.0", 0);
403  addr.setFromIpPort("126.255.255.255", 0);
405  addr.setFromIpPort("10.1.2.3", 0);
407 
408  // IPv6
409  addr.setFromIpPort("::1", 0);
411  addr.setFromIpPort("::0", 0);
413  addr.setFromIpPort("::2", 0);
415 
416  // IPv4-mapped IPv6
417  addr.setFromIpPort("::ffff:127.0.0.1", 0);
419  addr.setFromIpPort("::ffff:7f0a:141e", 0);
421  addr.setFromIpPort("::ffff:169.254.0.13", 0);
423 
424  // Unix sockets are considered loopback addresses
425  addr.setFromPath("/tmp/mysock");
427 }
428 
430  const SocketAddress& first,
431  const SocketAddress& second,
432  unsigned matchingPrefixLen) {
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 }
442 
443 TEST(SocketAddress, PrefixMatch) {
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 }
469 
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 }
477 
478 TEST(SocketAddress, CheckComparatorBehavior) {
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 }
539 
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 }
640 
641 TEST(SocketAddress, AnonymousUnix) {
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 }
673 
674 #define REQUIRE_ERRNO(cond, msg) \
675  if (!(cond)) { \
676  ADD_FAILURE() << (msg) << ": " << ::folly::errnoStr(errno); \
677  }
678 
680  const SocketAddress* serverBindAddr,
681  const SocketAddress* clientBindAddr,
682  SocketAddress* listenAddrRet,
683  SocketAddress* acceptAddrRet,
684  SocketAddress* serverAddrRet,
685  SocketAddress* serverPeerAddrRet,
686  SocketAddress* clientAddrRet,
687  SocketAddress* clientPeerAddrRet) {
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 }
747 
748 TEST(SocketAddress, SetFromSocketIPv4) {
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 }
783 
784 /*
785  * Note this test exercises Linux-specific Unix socket behavior
786  */
787 TEST(SocketAddress, SetFromSocketUnixAbstract) {
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 }
829 
830 TEST(SocketAddress, SetFromSocketUnixExplicit) {
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 }
878 
879 TEST(SocketAddress, SetFromSocketUnixAnonymous) {
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 }
924 
925 TEST(SocketAddress, ResetUnixAddress) {
926  SocketAddress addy;
927  addy.setFromPath("/foo");
928 
929  addy.reset();
930  EXPECT_EQ(addy.getFamily(), AF_UNSPEC);
931 }
932 
933 TEST(SocketAddress, ResetIPAddress) {
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 }
947 
948 TEST(SocketAddress, ValidFamilyInet) {
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 testSetFromSocket(const SocketAddress *serverBindAddr, const SocketAddress *clientBindAddr, SocketAddress *listenAddrRet, SocketAddress *acceptAddrRet, SocketAddress *serverAddrRet, SocketAddress *serverPeerAddrRet, SocketAddress *clientAddrRet, SocketAddress *clientPeerAddrRet)
#define REQUIRE_ERRNO(cond, msg)
void setFromLocalIpPort(const char *addressAndPort)
void setFromPath(StringPiece path)
void setFromIpAddrPort(const IPAddress &ip, uint16_t port)
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
bool prefixMatch(const SocketAddress &other, unsigned prefixLength) const
void setFromHostPort(const char *host, uint16_t port)
bool empty() const
void setFromLocalPort(uint16_t port)
bool isIPv4Mapped() const
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr array_detail::return_type< D, TList... > make_array(TList &&...t)
Definition: Array.h:56
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
socklen_t getActualSize() const
void setFromSockaddr(const struct sockaddr *address)
std::string getPath() const
uint16_t getPort() const
std::string describe() const
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
TEST(SocketAddress, Size)
bool isFamilyInet() const
sa_family_t getFamily() const
bool isInitialized() const
void CheckPrefixMatch(const SocketAddress &first, const SocketAddress &second, unsigned matchingPrefixLen)
const folly::IPAddress & getIPAddress() 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 setFromIpPort(const char *ip, uint16_t port)
size_t hash() const
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void setFromLocalAddress(int socket)
const char * string
Definition: Conv.cpp:212
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
bool isPrivateAddress() const
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void join(const Delim &delimiter, Iterator begin, Iterator end, String &output)
Definition: String-inl.h:498
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
int close(NetworkSocket s)
Definition: NetOps.cpp:90
ThreadPoolListHook * addr
const fs::path & path() const
Definition: TestUtil.h:116
bool isLoopbackAddress() const
std::string getAddressStr() const
constexpr detail::First first
Definition: Base-inl.h:2553
void CheckFirstLessThanSecond(SocketAddress first, SocketAddress second)
int socketpair(int domain, int type, int protocol, NetworkSocket sv[2])
Definition: NetOps.cpp:416
NetworkSocket accept(NetworkSocket s, sockaddr *addr, socklen_t *addrlen)
Definition: NetOps.cpp:71