Commit f09b0411 authored by Valeriy Khromov's avatar Valeriy Khromov Committed by Facebook Github Bot

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
parent 71d0dd7f
......@@ -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.
*
......
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment