From c27a6839ff172d033e631ef1dd87e7195a1d26ef Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 22 Sep 2016 11:09:57 -0700 Subject: [PATCH] Switch SocketAddressTest to use TemporaryFile instead of mkstemp Summary: Switch from mkstemp to the slightly more portable `folly::test::TemporaryFile` Reviewed By: jsedgwick Differential Revision: D3890411 fbshipit-source-id: e98d1e3a5adae92af1bb36f6213b194f633fab0f --- folly/test/SocketAddressTest.cpp | 44 ++++++++++++-------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/folly/test/SocketAddressTest.cpp b/folly/test/SocketAddressTest.cpp index 200f7258..6a95db98 100644 --- a/folly/test/SocketAddressTest.cpp +++ b/folly/test/SocketAddressTest.cpp @@ -20,9 +20,9 @@ #include #include +#include #include #include -#include #include using namespace boost; @@ -31,6 +31,7 @@ using std::cerr; using std::endl; using folly::SocketAddress; using folly::SocketAddressTestHelper; +using folly::test::TemporaryDirectory; namespace fsp = folly::portability::sockets; @@ -803,19 +804,9 @@ TEST(SocketAddress, SetFromSocketUnixAbstract) { TEST(SocketAddress, SetFromSocketUnixExplicit) { // Pick two temporary path names. - // We use mkstemp() just to avoid warnings about mktemp, - // but we need to remove the file to let the socket code bind to it. - char serverPath[] = "/tmp/SocketAddressTest.server.XXXXXX"; - int serverPathFd = mkstemp(serverPath); - EXPECT_GE(serverPathFd, 0); - char clientPath[] = "/tmp/SocketAddressTest.client.XXXXXX"; - int clientPathFd = mkstemp(clientPath); - EXPECT_GE(clientPathFd, 0); - - int rc = unlink(serverPath); - EXPECT_EQ(rc, 0); - rc = unlink(clientPath); - EXPECT_EQ(rc, 0); + TemporaryDirectory tempDirectory("SocketAddressTest"); + std::string serverPath = (tempDirectory.path() / "server").string(); + std::string clientPath = (tempDirectory.path() / "client").string(); SocketAddress serverBindAddr; SocketAddress clientBindAddr; @@ -826,8 +817,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { SocketAddress clientAddr; SocketAddress clientPeerAddr; try { - serverBindAddr.setFromPath(serverPath); - clientBindAddr.setFromPath(clientPath); + serverBindAddr.setFromPath(serverPath.c_str()); + clientBindAddr.setFromPath(clientPath.c_str()); testSetFromSocket(&serverBindAddr, &clientBindAddr, &listenAddr, &acceptAddr, @@ -835,12 +826,12 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { &clientAddr, &clientPeerAddr); } catch (...) { // Remove the socket files after we are done - unlink(serverPath); - unlink(clientPath); + unlink(serverPath.c_str()); + unlink(clientPath.c_str()); throw; } - unlink(serverPath); - unlink(clientPath); + unlink(serverPath.c_str()); + unlink(clientPath.c_str()); // The server socket's local address should be the same as the listen // address. @@ -857,11 +848,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { TEST(SocketAddress, SetFromSocketUnixAnonymous) { // Test an anonymous client talking to a fixed-path unix socket. - char serverPath[] = "/tmp/SocketAddressTest.server.XXXXXX"; - int serverPathFd = mkstemp(serverPath); - EXPECT_GE(serverPathFd, 0); - int rc = unlink(serverPath); - EXPECT_EQ(rc, 0); + TemporaryDirectory tempDirectory("SocketAddressTest"); + std::string serverPath = (tempDirectory.path() / "server").string(); SocketAddress serverBindAddr; SocketAddress listenAddr; @@ -871,7 +859,7 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) { SocketAddress clientAddr; SocketAddress clientPeerAddr; try { - serverBindAddr.setFromPath(serverPath); + serverBindAddr.setFromPath(serverPath.c_str()); testSetFromSocket(&serverBindAddr, nullptr, &listenAddr, &acceptAddr, @@ -879,10 +867,10 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) { &clientAddr, &clientPeerAddr); } catch (...) { // Remove the socket file after we are done - unlink(serverPath); + unlink(serverPath.c_str()); throw; } - unlink(serverPath); + unlink(serverPath.c_str()); // The server socket's local address should be the same as the listen // address. -- 2.34.1