2021-01-14 08:53:28 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-04-29 05:59:19 +03:00
|
|
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
2021-01-14 08:53:28 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-14 08:53:28 +03:00
|
|
|
*/
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-03-27 03:37:51 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-04-29 05:59:19 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2021-01-14 08:53:28 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-05-14 18:38:33 +03:00
|
|
|
#include <sys/mman.h>
|
2021-01-14 08:53:28 +03:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-04-29 05:59:19 +03:00
|
|
|
TEST_CASE(test_mktemp_unique_filename)
|
2021-01-14 08:53:28 +03:00
|
|
|
{
|
|
|
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(ptr != MAP_FAILED);
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
if (fork() == 0) {
|
|
|
|
char path[] = "/tmp/test.mktemp.XXXXXX";
|
2022-12-04 21:02:33 +03:00
|
|
|
auto temp_path = DeprecatedString::formatted("{}", mktemp(path));
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(temp_path.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
unlink(path);
|
|
|
|
|
|
|
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
wait(NULL);
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path1 = DeprecatedString::formatted("{}", reinterpret_cast<char const*>(ptr));
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
char path[] = "/tmp/test.mktemp.XXXXXX";
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path2 = DeprecatedString::formatted("{}", mktemp(path));
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(path2.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
unlink(path);
|
|
|
|
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(path1, path2);
|
2021-01-14 08:53:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
munmap(ptr, sizeof(*ptr));
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:59:19 +03:00
|
|
|
TEST_CASE(test_mkdtemp_unique_filename)
|
2021-01-14 08:53:28 +03:00
|
|
|
{
|
|
|
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(ptr, MAP_FAILED);
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
if (fork() == 0) {
|
|
|
|
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
2022-12-04 21:02:33 +03:00
|
|
|
auto temp_path = DeprecatedString::formatted("{}", mkdtemp(path));
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(temp_path.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
rmdir(path);
|
|
|
|
|
|
|
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
wait(NULL);
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path1 = DeprecatedString::formatted("{}", reinterpret_cast<char const*>(ptr));
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path2 = DeprecatedString::formatted("{}", mkdtemp(path));
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(path2.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
rmdir(path);
|
|
|
|
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(path1, path2);
|
2021-01-14 08:53:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
munmap(ptr, sizeof(*ptr));
|
|
|
|
}
|
2021-04-29 05:59:19 +03:00
|
|
|
|
|
|
|
TEST_CASE(test_mkstemp_unique_filename)
|
2021-01-14 08:53:28 +03:00
|
|
|
{
|
|
|
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(ptr, MAP_FAILED);
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
if (fork() == 0) {
|
|
|
|
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
|
|
|
auto fd = mkstemp(path);
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(fd, -1);
|
2021-01-14 08:53:28 +03:00
|
|
|
|
2023-05-07 21:14:06 +03:00
|
|
|
auto temp_path_string = TRY_OR_FAIL(FileSystem::read_link(DeprecatedString::formatted("/proc/{}/fd/{}", getpid(), fd)));
|
|
|
|
auto temp_path = temp_path_string.to_deprecated_string();
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(temp_path.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
|
|
|
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
wait(NULL);
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path1 = DeprecatedString::formatted("{}", reinterpret_cast<char const*>(ptr));
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
|
|
|
auto fd = mkstemp(path);
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(fd != -1);
|
2021-01-14 08:53:28 +03:00
|
|
|
|
2023-05-07 21:14:06 +03:00
|
|
|
auto path2_string = TRY_OR_FAIL(FileSystem::read_link(DeprecatedString::formatted("/proc/{}/fd/{}", getpid(), fd)));
|
|
|
|
auto path2 = path2_string.to_deprecated_string();
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT(path2.characters());
|
2021-01-14 08:53:28 +03:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
|
2021-04-29 05:59:19 +03:00
|
|
|
EXPECT_NE(path1, path2);
|
2021-01-14 08:53:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
munmap(ptr, sizeof(*ptr));
|
|
|
|
}
|
2022-09-03 20:35:47 +03:00
|
|
|
|
|
|
|
TEST_CASE(test_mkstemps_unique_filename)
|
|
|
|
{
|
|
|
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
|
|
|
EXPECT_NE(ptr, MAP_FAILED);
|
|
|
|
|
|
|
|
if (fork() == 0) {
|
|
|
|
char path[] = "/tmp/test.mkstemps.prefixXXXXXXsuffix";
|
|
|
|
auto fd = mkstemps(path, 6);
|
|
|
|
EXPECT_NE(fd, -1);
|
|
|
|
|
2023-05-07 21:14:06 +03:00
|
|
|
auto temp_path_string = TRY_OR_FAIL(FileSystem::read_link(DeprecatedString::formatted("/proc/{}/fd/{}", getpid(), fd)));
|
|
|
|
auto temp_path = temp_path_string.to_deprecated_string();
|
2022-09-03 20:35:47 +03:00
|
|
|
EXPECT(temp_path.characters());
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
|
|
|
|
EXPECT(temp_path.starts_with("/tmp/test.mkstemps.prefix"sv));
|
|
|
|
EXPECT(temp_path.ends_with("suffix"sv));
|
|
|
|
EXPECT_EQ(strlen(path), temp_path.length());
|
|
|
|
|
|
|
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
|
|
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
|
|
|
wait(NULL);
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
auto path1 = DeprecatedString::formatted("{}", reinterpret_cast<char const*>(ptr));
|
2022-09-03 20:35:47 +03:00
|
|
|
|
|
|
|
char path[] = "/tmp/test.mkstemps.prefixXXXXXXsuffix";
|
|
|
|
auto fd = mkstemps(path, 6);
|
|
|
|
EXPECT(fd != -1);
|
|
|
|
|
2023-05-07 21:14:06 +03:00
|
|
|
auto path2_string = TRY_OR_FAIL(FileSystem::read_link(DeprecatedString::formatted("/proc/{}/fd/{}", getpid(), fd)));
|
|
|
|
auto path2 = path2_string.to_deprecated_string();
|
2022-09-03 20:35:47 +03:00
|
|
|
EXPECT(path2.characters());
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
unlink(path);
|
|
|
|
|
|
|
|
EXPECT(path2.starts_with("/tmp/test.mkstemps.prefix"sv));
|
|
|
|
EXPECT(path2.ends_with("suffix"sv));
|
|
|
|
EXPECT_EQ(strlen(path), path2.length());
|
|
|
|
|
|
|
|
EXPECT_NE(path1, path2);
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(ptr, sizeof(*ptr));
|
|
|
|
}
|