add static makeFromPath to construct SocketAddress from a unix domain socket path
authorValeriy Khromov <vkhromov@fb.com>
Fri, 6 Oct 2017 11:38:06 +0000 (04:38 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 6 Oct 2017 11:39:02 +0000 (04:39 -0700)
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
folly/test/SocketAddressTest.cpp

index 31d441f7ed26e471b45efd7cc03d77f64dfb07df..929fb6946ac68d9d665a39347ba99c9a535c0f8d 100644 (file)
@@ -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.
    *
index 7fe8b7910c6d0afd352887ee4ea4afbebb209c76..e9e706b4c34ca2da670d32902111a117a42c5c29 100644 (file)
@@ -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) {