Commit c27a6839 authored by Michael Lee's avatar Michael Lee Committed by Facebook Github Bot 4

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
parent 7c05f8af
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
#include <sstream> #include <sstream>
#include <system_error> #include <system_error>
#include <folly/experimental/TestUtil.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h> #include <folly/portability/Sockets.h>
#include <folly/portability/Stdlib.h>
#include <folly/test/SocketAddressTestHelper.h> #include <folly/test/SocketAddressTestHelper.h>
using namespace boost; using namespace boost;
...@@ -31,6 +31,7 @@ using std::cerr; ...@@ -31,6 +31,7 @@ using std::cerr;
using std::endl; using std::endl;
using folly::SocketAddress; using folly::SocketAddress;
using folly::SocketAddressTestHelper; using folly::SocketAddressTestHelper;
using folly::test::TemporaryDirectory;
namespace fsp = folly::portability::sockets; namespace fsp = folly::portability::sockets;
...@@ -803,19 +804,9 @@ TEST(SocketAddress, SetFromSocketUnixAbstract) { ...@@ -803,19 +804,9 @@ TEST(SocketAddress, SetFromSocketUnixAbstract) {
TEST(SocketAddress, SetFromSocketUnixExplicit) { TEST(SocketAddress, SetFromSocketUnixExplicit) {
// Pick two temporary path names. // Pick two temporary path names.
// We use mkstemp() just to avoid warnings about mktemp, TemporaryDirectory tempDirectory("SocketAddressTest");
// but we need to remove the file to let the socket code bind to it. std::string serverPath = (tempDirectory.path() / "server").string();
char serverPath[] = "/tmp/SocketAddressTest.server.XXXXXX"; std::string clientPath = (tempDirectory.path() / "client").string();
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);
SocketAddress serverBindAddr; SocketAddress serverBindAddr;
SocketAddress clientBindAddr; SocketAddress clientBindAddr;
...@@ -826,8 +817,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { ...@@ -826,8 +817,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) {
SocketAddress clientAddr; SocketAddress clientAddr;
SocketAddress clientPeerAddr; SocketAddress clientPeerAddr;
try { try {
serverBindAddr.setFromPath(serverPath); serverBindAddr.setFromPath(serverPath.c_str());
clientBindAddr.setFromPath(clientPath); clientBindAddr.setFromPath(clientPath.c_str());
testSetFromSocket(&serverBindAddr, &clientBindAddr, testSetFromSocket(&serverBindAddr, &clientBindAddr,
&listenAddr, &acceptAddr, &listenAddr, &acceptAddr,
...@@ -835,12 +826,12 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { ...@@ -835,12 +826,12 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) {
&clientAddr, &clientPeerAddr); &clientAddr, &clientPeerAddr);
} catch (...) { } catch (...) {
// Remove the socket files after we are done // Remove the socket files after we are done
unlink(serverPath); unlink(serverPath.c_str());
unlink(clientPath); unlink(clientPath.c_str());
throw; throw;
} }
unlink(serverPath); unlink(serverPath.c_str());
unlink(clientPath); unlink(clientPath.c_str());
// The server socket's local address should be the same as the listen // The server socket's local address should be the same as the listen
// address. // address.
...@@ -857,11 +848,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) { ...@@ -857,11 +848,8 @@ TEST(SocketAddress, SetFromSocketUnixExplicit) {
TEST(SocketAddress, SetFromSocketUnixAnonymous) { TEST(SocketAddress, SetFromSocketUnixAnonymous) {
// Test an anonymous client talking to a fixed-path unix socket. // Test an anonymous client talking to a fixed-path unix socket.
char serverPath[] = "/tmp/SocketAddressTest.server.XXXXXX"; TemporaryDirectory tempDirectory("SocketAddressTest");
int serverPathFd = mkstemp(serverPath); std::string serverPath = (tempDirectory.path() / "server").string();
EXPECT_GE(serverPathFd, 0);
int rc = unlink(serverPath);
EXPECT_EQ(rc, 0);
SocketAddress serverBindAddr; SocketAddress serverBindAddr;
SocketAddress listenAddr; SocketAddress listenAddr;
...@@ -871,7 +859,7 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) { ...@@ -871,7 +859,7 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) {
SocketAddress clientAddr; SocketAddress clientAddr;
SocketAddress clientPeerAddr; SocketAddress clientPeerAddr;
try { try {
serverBindAddr.setFromPath(serverPath); serverBindAddr.setFromPath(serverPath.c_str());
testSetFromSocket(&serverBindAddr, nullptr, testSetFromSocket(&serverBindAddr, nullptr,
&listenAddr, &acceptAddr, &listenAddr, &acceptAddr,
...@@ -879,10 +867,10 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) { ...@@ -879,10 +867,10 @@ TEST(SocketAddress, SetFromSocketUnixAnonymous) {
&clientAddr, &clientPeerAddr); &clientAddr, &clientPeerAddr);
} catch (...) { } catch (...) {
// Remove the socket file after we are done // Remove the socket file after we are done
unlink(serverPath); unlink(serverPath.c_str());
throw; throw;
} }
unlink(serverPath); unlink(serverPath.c_str());
// The server socket's local address should be the same as the listen // The server socket's local address should be the same as the listen
// address. // address.
......
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