From f09b04110904f3585da1fc9e0a77f0672dab1c6b Mon Sep 17 00:00:00 2001 From: Valeriy Khromov Date: Fri, 6 Oct 2017 04:38:06 -0700 Subject: [PATCH] add static makeFromPath to construct SocketAddress from a unix domain socket path Summary: Add `SocketAddress::makeFromPath(StringPiece)` static member function to constructor `SocketAddress` from a path to Unix domain socket. Reviewed By: yfeldblum Differential Revision: D5974523 fbshipit-source-id: b5c1537e67d07d1ef401fea75e35753392eeaf6b --- folly/SocketAddress.h | 13 +++++++++++++ folly/test/SocketAddressTest.cpp | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/folly/SocketAddress.h b/folly/SocketAddress.h index 31d441f7..929fb694 100644 --- a/folly/SocketAddress.h +++ b/folly/SocketAddress.h @@ -306,6 +306,19 @@ class SocketAddress { setFromPath(StringPiece{path, length}); } + /** + * Construct a SocketAddress from a local unix socket path. + * + * Raises std::invalid_argument on error. + * + * @param path The Unix domain socket path. + */ + static SocketAddress makeFromPath(StringPiece path) { + SocketAddress addr; + addr.setFromPath(path); + return addr; + } + /** * Initialize this SocketAddress from a socket's peer address. * diff --git a/folly/test/SocketAddressTest.cpp b/folly/test/SocketAddressTest.cpp index 7fe8b791..e9e706b4 100644 --- a/folly/test/SocketAddressTest.cpp +++ b/folly/test/SocketAddressTest.cpp @@ -268,16 +268,26 @@ TEST(SocketAddress, EqualityAndHash) { unix3.setFromPath("/bar"); SocketAddress unixAnon; unixAnon.setFromPath(""); + auto unix5 = SocketAddress::makeFromPath("/foo"); + auto unixAnon2 = SocketAddress::makeFromPath(""); EXPECT_EQ(unix1, unix2); + EXPECT_EQ(unix1, unix5); EXPECT_EQ(unix1.hash(), unix2.hash()); + EXPECT_EQ(unix1.hash(), unix5.hash()); EXPECT_NE(unix1, unix3); EXPECT_NE(unix1, unixAnon); + EXPECT_NE(unix1, unixAnon2); EXPECT_NE(unix2, unix3); + EXPECT_NE(unix5, unix3); EXPECT_NE(unix2, unixAnon); + EXPECT_NE(unix2, unixAnon2); + EXPECT_NE(unix5, unixAnon); + EXPECT_NE(unix5, unixAnon2); // anonymous addresses aren't equal to any other address, // including themselves EXPECT_NE(unixAnon, unixAnon); + EXPECT_NE(unixAnon2, unixAnon2); // It isn't strictly required that hashes for different addresses be // different, but we should have very few collisions. It generally indicates @@ -285,6 +295,8 @@ TEST(SocketAddress, EqualityAndHash) { EXPECT_NE(unix1.hash(), unix3.hash()); EXPECT_NE(unix1.hash(), unixAnon.hash()); EXPECT_NE(unix3.hash(), unixAnon.hash()); + EXPECT_NE(unix1.hash(), unixAnon2.hash()); + EXPECT_NE(unix3.hash(), unixAnon2.hash()); } TEST(SocketAddress, IsPrivate) { -- 2.34.1