#include <sstream>
#include <system_error>
+#include <folly/experimental/TestUtil.h>
#include <folly/portability/GTest.h>
#include <folly/portability/Sockets.h>
-#include <folly/portability/Stdlib.h>
#include <folly/test/SocketAddressTestHelper.h>
using namespace boost;
using std::endl;
using folly::SocketAddress;
using folly::SocketAddressTestHelper;
+using folly::test::TemporaryDirectory;
namespace fsp = folly::portability::sockets;
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;
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,
&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.
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;
SocketAddress clientAddr;
SocketAddress clientPeerAddr;
try {
- serverBindAddr.setFromPath(serverPath);
+ serverBindAddr.setFromPath(serverPath.c_str());
testSetFromSocket(&serverBindAddr, nullptr,
&listenAddr, &acceptAddr,
&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.