Skip to content

Truly independent web browser

Tests/LibCore: Give each test run its own temp paths and ports

Shannon Booth84103bee3f43
diff --git a/Tests/LibCore/TestLibCoreFileWatcher.cpp b/Tests/LibCore/TestLibCoreFileWatcher.cpp
index caef72647d2..ab0c72a9c6a 100644
--- a/Tests/LibCore/TestLibCoreFileWatcher.cpp
+++ b/Tests/LibCore/TestLibCoreFileWatcher.cpp
@@ -10,7 +10,7 @@
#include <LibCore/FileWatcher.h>
#include <LibCore/System.h>
#include <LibCore/Timer.h>
-#include <LibFileSystem/FileSystem.h>
+#include <LibTest/TemporaryDirectory.h>
#include <LibTest/TestCase.h>
#include <fcntl.h>
#include <unistd.h>
@@ -27,27 +27,25 @@ TEST_CASE(file_watcher_child_events)
auto maybe_file_watcher = Core::FileWatcher::create();
EXPECT_NE(maybe_file_watcher.is_error(), true);
- // Ensure the testfile does not already exist.
- (void)Core::System::unlink("/tmp/testfile"sv);
+ Test::TemporaryDirectory directory { "TestLibCoreFileWatcher"sv };
+ auto test_path = directory.path().append("testfile"sv).string();
auto file_watcher = maybe_file_watcher.release_value();
- auto watch_result = file_watcher->add_watch("/tmp/",
+ auto watch_result = file_watcher->add_watch(directory.path().string(),
Core::FileWatcherEvent::Type::ChildCreated
| Core::FileWatcherEvent::Type::ChildDeleted);
EXPECT_NE(watch_result.is_error(), true);
int event_count = 0;
file_watcher->on_change = [&](Core::FileWatcherEvent const& event) {
- // Ignore path events under /tmp that can occur for anything else the OS is
- // doing to create/delete files there.
- if (event.event_path != "/tmp/testfile"sv)
+ if (event.event_path != test_path)
return;
if (event_count == 0) {
EXPECT(has_flag(event.type, Core::FileWatcherEvent::Type::ChildCreated));
} else if (event_count == 1) {
EXPECT(has_flag(event.type, Core::FileWatcherEvent::Type::ChildDeleted));
- EXPECT(MUST(file_watcher->remove_watch("/tmp/"sv)));
+ EXPECT(MUST(file_watcher->remove_watch(directory.path().string())));
event_loop.quit(0);
}
@@ -56,13 +54,13 @@ TEST_CASE(file_watcher_child_events)
};
auto timer1 = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(1 * TIMEOUT_PER_STEP_IN_MS), [&] {
- int rc = creat("/tmp/testfile", 0777);
+ int rc = creat(test_path.characters(), 0777);
EXPECT_NE(rc, -1);
});
timer1->start();
auto timer2 = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(2 * TIMEOUT_PER_STEP_IN_MS), [&] {
- MUST(Core::System::unlink("/tmp/testfile"sv));
+ MUST(Core::System::unlink(test_path));
});
timer2->start();
@@ -78,8 +76,8 @@ TEST_CASE(contents_changed)
{
auto event_loop = Core::EventLoop();
- auto temp_path = MUST(FileSystem::real_path("/tmp"sv));
- auto test_path = LexicalPath::join(temp_path, "testfile"sv);
+ Test::TemporaryDirectory directory { "TestLibCoreFileWatcher"sv };
+ auto test_path = directory.path().append("testfile"sv);
auto write_file = [&](auto contents) {
auto file = MUST(Core::File::open(test_path.string(), Core::File::OpenMode::Write));
@@ -120,10 +118,10 @@ TEST_CASE(symbolic_link)
{
auto event_loop = Core::EventLoop();
- auto temp_path = MUST(FileSystem::real_path("/tmp"sv));
- auto test_file = LexicalPath::join(temp_path, "testfile"sv);
- auto test_link1 = LexicalPath::join(temp_path, "testlink1"sv);
- auto test_link2 = LexicalPath::join(temp_path, "testlink2"sv);
+ Test::TemporaryDirectory directory { "TestLibCoreFileWatcher"sv };
+ auto test_file = directory.path().append("testfile"sv);
+ auto test_link1 = directory.path().append("testlink1"sv);
+ auto test_link2 = directory.path().append("testlink2"sv);
(void)MUST(Core::File::open(test_link1.string(), Core::File::OpenMode::ReadWrite));
(void)MUST(Core::File::open(test_link2.string(), Core::File::OpenMode::ReadWrite));
diff --git a/Tests/LibCore/TestLibCoreMappedFile.cpp b/Tests/LibCore/TestLibCoreMappedFile.cpp
index c940a6028fb..0d56438f7b2 100644
--- a/Tests/LibCore/TestLibCoreMappedFile.cpp
+++ b/Tests/LibCore/TestLibCoreMappedFile.cpp
@@ -11,15 +11,18 @@
#include <LibCore/File.h>
#include <LibCore/ImmutableBytes.h>
#include <LibCore/MappedFile.h>
-#include <LibCore/StandardPaths.h>
+#include <LibTest/TemporaryDirectory.h>
#include <LibTest/TestCase.h>
TEST_CASE(mapped_file_open)
{
+ Test::TemporaryDirectory directory { "TestLibCoreMappedFile"sv };
+ auto path = directory.path().append("file-open-test.txt"sv).string();
+
// Fill the file with a little content so we can write to it.
constexpr auto text = "Here's some text to be mmapped."sv;
{
- auto maybe_file = Core::File::open(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-open-test.txt"sv), Core::File::OpenMode::Write);
+ auto maybe_file = Core::File::open(path, Core::File::OpenMode::Write);
if (maybe_file.is_error()) {
warnln("Failed to open the file: {}", strerror(maybe_file.error().code()));
VERIFY_NOT_REACHED();
@@ -27,7 +30,7 @@ TEST_CASE(mapped_file_open)
TRY_OR_FAIL(maybe_file.value()->write_until_depleted(text.bytes()));
}
- auto maybe_file = Core::MappedFile::map(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-open-test.txt"sv), Core::MappedFile::Mode::ReadWrite);
+ auto maybe_file = Core::MappedFile::map(path, Core::MappedFile::Mode::ReadWrite);
if (maybe_file.is_error()) {
warnln("Failed to open the file: {}", strerror(maybe_file.error().code()));
VERIFY_NOT_REACHED();
diff --git a/Tests/LibCore/TestLibCoreStream.cpp b/Tests/LibCore/TestLibCoreStream.cpp
index 87862176d8a..2a1405104e9 100644
--- a/Tests/LibCore/TestLibCoreStream.cpp
+++ b/Tests/LibCore/TestLibCoreStream.cpp
@@ -11,22 +11,28 @@
#include <LibCore/File.h>
#include <LibCore/LocalServer.h>
#include <LibCore/Socket.h>
-#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
#include <LibCore/TCPServer.h>
#include <LibCore/Timer.h>
#include <LibCore/UDPServer.h>
#include <LibFileSystem/FileSystem.h>
+#include <LibTest/TemporaryDirectory.h>
#include <LibTest/TestCase.h>
#include <LibThreading/Thread.h>
#include <AK/Windows.h>
+static ByteString temporary_path(StringView name)
+{
+ static Test::TemporaryDirectory const directory { "TestLibCoreStream"sv };
+ return directory.path().append(name).string();
+}
+
// File tests
TEST_CASE(file_open)
{
- auto maybe_file = Core::File::open(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-open-test.txt"sv), Core::File::OpenMode::Write);
+ auto maybe_file = Core::File::open(temporary_path("file-open-test.txt"sv), Core::File::OpenMode::Write);
if (maybe_file.is_error()) {
warnln("Failed to open the file: {}", strerror(maybe_file.error().code()));
VERIFY_NOT_REACHED();
@@ -49,7 +55,7 @@ TEST_CASE(file_open_rejects_embedded_nul)
TEST_CASE(file_write_bytes)
{
- auto file = TRY_OR_FAIL(Core::File::open(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-write-bytes-test.txt"sv), Core::File::OpenMode::Write));
+ auto file = TRY_OR_FAIL(Core::File::open(temporary_path("file-write-bytes-test.txt"sv), Core::File::OpenMode::Write));
constexpr auto some_words = "These are some words"sv;
ReadonlyBytes buffer { some_words.characters_without_null_termination(), some_words.length() };
@@ -122,7 +128,7 @@ BENCHMARK_CASE(file_tell)
TEST_CASE(file_buffered_write_and_seek)
{
- auto file = TRY_OR_FAIL(Core::OutputBufferedFile::create(TRY_OR_FAIL(Core::File::open(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-buffered-write-test.txt"sv), Core::File::OpenMode::Truncate | Core::File::OpenMode::ReadWrite))));
+ auto file = TRY_OR_FAIL(Core::OutputBufferedFile::create(TRY_OR_FAIL(Core::File::open(temporary_path("file-buffered-write-test.txt"sv), Core::File::OpenMode::Truncate | Core::File::OpenMode::ReadWrite))));
TRY_OR_FAIL(file->write_some("0123456789"sv.bytes()));
EXPECT_EQ(file->tell().release_value(), 10ul);
@@ -167,7 +173,7 @@ TEST_CASE(file_adopt_invalid_fd)
TEST_CASE(file_truncate)
{
- auto file = TRY_OR_FAIL(Core::File::open(ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-truncate-test.txt"sv), Core::File::OpenMode::Write));
+ auto file = TRY_OR_FAIL(Core::File::open(temporary_path("file-truncate-test.txt"sv), Core::File::OpenMode::Write));
TRY_OR_FAIL(file->truncate(999));
EXPECT_EQ(file->size().release_value(), 999ul);
@@ -204,12 +210,12 @@ TEST_CASE(tcp_socket_read)
Core::EventLoop event_loop;
auto tcp_server = TRY_OR_FAIL(Core::TCPServer::try_create());
- TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 9090, Core::TCPServer::AllowAddressReuse::Yes));
+ TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 0, Core::TCPServer::AllowAddressReuse::Yes));
#if !defined(AK_OS_WINDOWS)
TRY_OR_FAIL(tcp_server->set_blocking(true));
#endif
- auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 }));
+ auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, tcp_server->local_port().value() }));
EXPECT(client_socket->is_open());
@@ -235,10 +241,10 @@ TEST_CASE(tcp_socket_write)
Core::EventLoop event_loop;
auto tcp_server = TRY_OR_FAIL(Core::TCPServer::try_create());
- TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 9090, Core::TCPServer::AllowAddressReuse::Yes));
+ TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 0, Core::TCPServer::AllowAddressReuse::Yes));
TRY_OR_FAIL(tcp_server->set_blocking(true));
- auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 }));
+ auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, tcp_server->local_port().value() }));
auto server_socket = TRY_OR_FAIL(tcp_server->accept());
TRY_OR_FAIL(server_socket->set_blocking(true));
@@ -258,10 +264,10 @@ TEST_CASE(tcp_socket_eof)
Core::EventLoop event_loop;
auto tcp_server = TRY_OR_FAIL(Core::TCPServer::try_create());
- TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 9090, Core::TCPServer::AllowAddressReuse::Yes));
+ TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 0, Core::TCPServer::AllowAddressReuse::Yes));
TRY_OR_FAIL(tcp_server->set_blocking(true));
- auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 }));
+ auto client_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, tcp_server->local_port().value() }));
EXPECT(client_socket->is_open());
@@ -293,9 +299,9 @@ TEST_CASE(udp_socket_read_write)
Core::EventLoop event_loop;
auto udp_server = Core::UDPServer::construct();
- EXPECT(udp_server->bind({ 127, 0, 0, 1 }, 9090));
+ EXPECT(udp_server->bind({ 127, 0, 0, 1 }, 0));
- auto client_socket = TRY_OR_FAIL(Core::UDPSocket::connect({ { 127, 0, 0, 1 }, 9090 }));
+ auto client_socket = TRY_OR_FAIL(Core::UDPSocket::connect({ { 127, 0, 0, 1 }, udp_server->local_port().value() }));
EXPECT(client_socket->is_open());
TRY_OR_FAIL(client_socket->write_until_depleted({ sent_data.characters_without_null_termination(), sent_data.length() }));
@@ -347,7 +353,7 @@ TEST_CASE(local_socket_read)
{
Core::EventLoop event_loop;
- auto socket_path = ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "test-socket"sv);
+ auto socket_path = temporary_path("test-socket"sv);
if (FileSystem::exists(socket_path))
TRY_OR_FAIL(FileSystem::remove(socket_path, FileSystem::RecursionMode::Disallowed));
@@ -399,7 +405,7 @@ TEST_CASE(local_socket_write)
{
Core::EventLoop event_loop;
- auto socket_path = ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "test-socket"sv);
+ auto socket_path = temporary_path("test-socket"sv);
if (FileSystem::exists(socket_path))
TRY_OR_FAIL(FileSystem::remove(socket_path, FileSystem::RecursionMode::Disallowed));
@@ -577,7 +583,7 @@ constexpr auto new_newlines_message = "Hi, look, no newlines"sv;
TEST_CASE(buffered_file_without_newlines)
{
- auto const filename = ByteString::formatted("{}/{}", Core::StandardPaths::tempfile_directory(), "file-without-newlines"sv);
+ auto const filename = temporary_path("file-without-newlines"sv);
auto file_wo_newlines = Core::File::open(filename, Core::File::OpenMode::Write).release_value();
TRY_OR_FAIL(file_wo_newlines->write_until_depleted(new_newlines_message.bytes()));
file_wo_newlines->close();
@@ -601,10 +607,10 @@ TEST_CASE(buffered_tcp_socket_read)
Core::EventLoop event_loop;
auto tcp_server = TRY_OR_FAIL(Core::TCPServer::try_create());
- TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 9090, Core::TCPServer::AllowAddressReuse::Yes));
+ TRY_OR_FAIL(tcp_server->listen({ 127, 0, 0, 1 }, 0, Core::TCPServer::AllowAddressReuse::Yes));
TRY_OR_FAIL(tcp_server->set_blocking(true));
- auto unbuffered_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 }));
+ auto unbuffered_socket = TRY_OR_FAIL(Core::TCPSocket::connect({ { 127, 0, 0, 1 }, tcp_server->local_port().value() }));
auto client_socket = TRY_OR_FAIL(Core::BufferedTCPSocket::create(move(unbuffered_socket)));
EXPECT(client_socket->is_open());