2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-07-07 05:13:58 +00:00
|
|
|
* Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-01-03 02:16:47 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Types.h>
|
2023-03-27 00:37:51 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-07-07 05:13:58 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2020-01-03 02:16:47 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2020-01-26 09:10:15 +00:00
|
|
|
#include <string.h>
|
2020-01-03 02:16:47 +00:00
|
|
|
#include <sys/mman.h>
|
2020-01-15 20:58:03 +00:00
|
|
|
#include <sys/stat.h>
|
2020-01-26 09:10:15 +00:00
|
|
|
#include <sys/uio.h>
|
2022-12-19 18:32:31 +00:00
|
|
|
#include <sys/wait.h>
|
2020-01-03 02:16:47 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-06-30 13:29:13 +00:00
|
|
|
#define EXPECT_ERROR_2(err, syscall, arg1, arg2) \
|
|
|
|
do { \
|
|
|
|
rc = syscall(arg1, arg2); \
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(rc < 0); \
|
|
|
|
EXPECT_EQ(errno, err); \
|
2021-06-30 13:29:13 +00:00
|
|
|
if (rc >= 0 || errno != err) { \
|
|
|
|
warnln(__FILE__ ":{}: Expected " #err ": " #syscall "({}, {}), got rc={}, errno={}", __LINE__, arg1, arg2, rc, errno); \
|
|
|
|
} \
|
2020-01-03 02:16:47 +00:00
|
|
|
} while (0)
|
|
|
|
|
2021-06-30 13:29:13 +00:00
|
|
|
#define EXPECT_ERROR_3(err, syscall, arg1, arg2, arg3) \
|
|
|
|
do { \
|
|
|
|
rc = syscall(arg1, arg2, arg3); \
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(rc < 0); \
|
|
|
|
EXPECT_EQ(errno, err); \
|
2021-06-30 13:29:13 +00:00
|
|
|
if (rc >= 0 || errno != err) { \
|
|
|
|
warnln(__FILE__ ":{}: Expected " #err ": " #syscall "({}, {}, {}), got rc={}, errno={}", __LINE__, arg1, arg2, arg3, rc, errno); \
|
|
|
|
} \
|
2020-01-03 02:16:47 +00:00
|
|
|
} while (0)
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(read_from_directory)
|
2020-01-03 02:16:47 +00:00
|
|
|
{
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
int fd = open("/", O_DIRECTORY | O_RDONLY);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-03 02:16:47 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_3(EISDIR, read, fd, buffer, sizeof(buffer));
|
|
|
|
rc = close(fd);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-03 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(write_to_directory)
|
2020-01-03 02:16:47 +00:00
|
|
|
{
|
|
|
|
char str[] = "oh frick";
|
|
|
|
int fd = open("/", O_DIRECTORY | O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
perror("open");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-03 02:16:47 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_3(EBADF, write, fd, str, sizeof(str));
|
|
|
|
rc = close(fd);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-03 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(read_from_writeonly)
|
2020-01-03 02:33:34 +00:00
|
|
|
{
|
|
|
|
char buffer[BUFSIZ];
|
|
|
|
int fd = open("/tmp/xxxx123", O_CREAT | O_WRONLY);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-03 02:33:34 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_3(EBADF, read, fd, buffer, sizeof(buffer));
|
|
|
|
rc = close(fd);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = unlink("/tmp/xxxx123");
|
|
|
|
VERIFY(rc == 0);
|
2020-01-03 02:33:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(write_to_readonly)
|
2020-01-03 02:36:17 +00:00
|
|
|
{
|
|
|
|
char str[] = "hello";
|
|
|
|
int fd = open("/tmp/abcd123", O_CREAT | O_RDONLY);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-03 02:36:17 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_3(EBADF, write, fd, str, sizeof(str));
|
|
|
|
rc = close(fd);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = unlink("/tmp/abcd123");
|
|
|
|
VERIFY(rc == 0);
|
2020-01-03 02:36:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(read_past_eof)
|
2020-01-03 03:03:52 +00:00
|
|
|
{
|
|
|
|
char buffer[BUFSIZ];
|
2021-07-07 05:13:58 +00:00
|
|
|
int fd = open("/home/anon/README.md", O_RDONLY);
|
2020-01-03 03:03:52 +00:00
|
|
|
if (fd < 0)
|
|
|
|
perror("open");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-03 03:03:52 +00:00
|
|
|
int rc;
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = lseek(fd, 99999, SEEK_SET);
|
2020-01-03 03:03:52 +00:00
|
|
|
if (rc < 0)
|
|
|
|
perror("lseek");
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (rc < 0)
|
|
|
|
perror("read");
|
|
|
|
if (rc > 0)
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("read {} bytes past EOF", rc);
|
2020-01-03 03:03:52 +00:00
|
|
|
rc = close(fd);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-03 03:03:52 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(ftruncate_readonly)
|
2020-01-07 13:42:56 +00:00
|
|
|
{
|
|
|
|
int fd = open("/tmp/trunctest", O_RDONLY | O_CREAT, 0666);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-07 13:42:56 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_2(EBADF, ftruncate, fd, 0);
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = close(fd);
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
rc = unlink("/tmp/trunctest");
|
|
|
|
VERIFY(rc == 0);
|
2020-01-07 13:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(ftruncate_negative)
|
2020-01-07 13:42:56 +00:00
|
|
|
{
|
|
|
|
int fd = open("/tmp/trunctest", O_RDWR | O_CREAT, 0666);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-07 13:42:56 +00:00
|
|
|
int rc;
|
|
|
|
EXPECT_ERROR_2(EINVAL, ftruncate, fd, -1);
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = close(fd);
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
rc = unlink("/tmp/trunctest");
|
|
|
|
VERIFY(rc == 0);
|
2020-01-07 13:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(mmap_directory)
|
2020-01-08 11:33:36 +00:00
|
|
|
{
|
|
|
|
int fd = open("/tmp", O_RDONLY | O_DIRECTORY);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2023-09-01 07:53:12 +00:00
|
|
|
auto* ptr = mmap(nullptr, 4096, PROT_READ, MAP_SHARED, fd, 0);
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(ptr, MAP_FAILED);
|
2020-01-08 11:33:36 +00:00
|
|
|
if (ptr != MAP_FAILED) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Boo! mmap() of a directory succeeded!");
|
2020-01-08 11:33:36 +00:00
|
|
|
}
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(errno, ENODEV);
|
2020-01-08 11:33:36 +00:00
|
|
|
if (errno != ENODEV) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Boo! mmap() of a directory gave errno={} instead of ENODEV!", errno);
|
2020-01-08 11:33:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(tmpfs_read_past_end)
|
2020-01-08 11:45:39 +00:00
|
|
|
{
|
|
|
|
int fd = open("/tmp/x", O_RDWR | O_CREAT | O_TRUNC, 0600);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-08 11:45:39 +00:00
|
|
|
|
|
|
|
int rc = ftruncate(fd, 1);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-08 11:45:39 +00:00
|
|
|
|
|
|
|
rc = lseek(fd, 4096, SEEK_SET);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 4096);
|
2020-01-08 11:45:39 +00:00
|
|
|
|
|
|
|
char buffer[16];
|
|
|
|
int nread = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (nread != 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Expected 0-length read past end of file in /tmp");
|
2020-01-08 11:45:39 +00:00
|
|
|
}
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = close(fd);
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
rc = unlink("/tmp/x");
|
|
|
|
VERIFY(rc == 0);
|
2020-01-08 11:45:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 18:53:53 +00:00
|
|
|
TEST_CASE(sysfs_read_past_uptime_end)
|
2020-01-08 11:49:36 +00:00
|
|
|
{
|
2022-10-14 18:53:53 +00:00
|
|
|
int fd = open("/sys/kernel/uptime", O_RDONLY);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-08 11:49:36 +00:00
|
|
|
|
|
|
|
int rc = lseek(fd, 4096, SEEK_SET);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 4096);
|
2020-01-08 11:49:36 +00:00
|
|
|
|
|
|
|
char buffer[16];
|
|
|
|
int nread = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (nread != 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Expected 0-length read past end of file in /proc");
|
2020-01-08 11:49:36 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(open_create_device)
|
2020-01-08 12:59:22 +00:00
|
|
|
{
|
|
|
|
int fd = open("/tmp/fakedevice", (O_RDWR | O_CREAT), (S_IFCHR | 0600));
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(fd >= 0);
|
2020-01-08 12:59:22 +00:00
|
|
|
|
|
|
|
struct stat st;
|
2021-07-07 05:13:58 +00:00
|
|
|
int rc = fstat(fd, &st);
|
|
|
|
EXPECT(rc >= 0);
|
|
|
|
if (rc < 0) {
|
2020-01-08 12:59:22 +00:00
|
|
|
perror("stat");
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(st.st_mode, 0100600);
|
2020-01-08 12:59:22 +00:00
|
|
|
if (st.st_mode != 0100600) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Expected mode 0100600 after attempt to create a device node with open(O_CREAT), mode={:o}", st.st_mode);
|
2020-01-08 12:59:22 +00:00
|
|
|
}
|
2021-07-07 05:13:58 +00:00
|
|
|
rc = unlink("/tmp/fakedevice");
|
|
|
|
EXPECT_EQ(rc, 0);
|
2020-01-08 12:59:22 +00:00
|
|
|
close(fd);
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(rc, 0);
|
2020-01-08 12:59:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(unlink_symlink)
|
2020-01-10 11:51:05 +00:00
|
|
|
{
|
|
|
|
int rc = symlink("/proc/2/foo", "/tmp/linky");
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(rc >= 0);
|
2020-01-10 11:51:05 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("symlink");
|
|
|
|
}
|
2020-01-27 20:47:30 +00:00
|
|
|
|
2023-05-07 18:14:06 +00:00
|
|
|
auto target = TRY_OR_FAIL(FileSystem::read_link("/tmp/linky"sv));
|
2023-03-27 00:37:51 +00:00
|
|
|
EXPECT_EQ(target.bytes_as_string_view(), "/proc/2/foo"sv);
|
2020-01-27 20:47:30 +00:00
|
|
|
|
2020-01-10 11:51:05 +00:00
|
|
|
rc = unlink("/tmp/linky");
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(rc >= 0);
|
2020-01-10 11:51:05 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("unlink");
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Expected unlink() of a symlink into an unreadable directory to succeed!");
|
2020-01-10 11:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(tmpfs_eoverflow)
|
2020-01-12 19:15:53 +00:00
|
|
|
{
|
2021-07-07 05:13:58 +00:00
|
|
|
int fd = open("/tmp/x", O_RDWR | O_CREAT);
|
|
|
|
EXPECT(fd >= 0);
|
2020-01-12 19:15:53 +00:00
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
off_t rc = lseek(fd, INT64_MAX, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, INT64_MAX);
|
|
|
|
|
|
|
|
char buffer[16] {};
|
|
|
|
char empty_buffer[16] {};
|
2020-01-12 19:15:53 +00:00
|
|
|
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(rc, -1);
|
|
|
|
EXPECT_EQ(errno, EOVERFLOW);
|
|
|
|
|
|
|
|
[[maybe_unused]] auto ignored = strlcpy(buffer, "abcdefghijklmno", sizeof(buffer) - 1);
|
|
|
|
|
|
|
|
rc = write(fd, buffer, sizeof(buffer));
|
|
|
|
EXPECT_EQ(rc, -1);
|
|
|
|
EXPECT_EQ(errno, EOVERFLOW);
|
2020-01-12 19:15:53 +00:00
|
|
|
if (rc >= 0 || errno != EOVERFLOW) {
|
2021-07-07 05:13:58 +00:00
|
|
|
warnln("Expected EOVERFLOW when trying to write past INT64_MAX");
|
2020-01-12 19:15:53 +00:00
|
|
|
}
|
2021-07-07 05:13:58 +00:00
|
|
|
|
|
|
|
// ok now, write something to it, and try again
|
|
|
|
rc = lseek(fd, 0, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
2020-01-12 19:15:53 +00:00
|
|
|
rc = write(fd, buffer, sizeof(buffer));
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(rc, 16);
|
|
|
|
|
|
|
|
rc = lseek(fd, INT64_MAX, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, INT64_MAX);
|
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
|
|
|
EXPECT_EQ(rc, -1);
|
|
|
|
EXPECT_EQ(errno, EOVERFLOW);
|
2020-01-12 19:15:53 +00:00
|
|
|
if (rc >= 0 || errno != EOVERFLOW) {
|
2021-07-07 05:13:58 +00:00
|
|
|
warnln("Expected EOVERFLOW when trying to read past INT64_MAX");
|
2020-01-12 19:15:53 +00:00
|
|
|
}
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(0, memcmp(buffer, empty_buffer, sizeof(buffer)));
|
|
|
|
|
|
|
|
rc = close(fd);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
rc = unlink("/tmp/x");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(tmpfs_massive_file)
|
|
|
|
{
|
|
|
|
int fd = open("/tmp/x", O_RDWR | O_CREAT);
|
|
|
|
EXPECT(fd >= 0);
|
|
|
|
|
|
|
|
off_t rc = lseek(fd, INT32_MAX, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, INT32_MAX);
|
|
|
|
|
|
|
|
char buffer[16] {};
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
[[maybe_unused]] auto ignored = strlcpy(buffer, "abcdefghijklmno", sizeof(buffer) - 1);
|
|
|
|
|
|
|
|
rc = write(fd, buffer, sizeof(buffer));
|
2022-09-09 03:58:44 +00:00
|
|
|
EXPECT_EQ(rc, 16);
|
2021-07-07 05:13:58 +00:00
|
|
|
|
|
|
|
// ok now, write something to it, and try again
|
|
|
|
rc = lseek(fd, 0, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = write(fd, buffer, sizeof(buffer));
|
|
|
|
EXPECT_EQ(rc, 16);
|
|
|
|
|
|
|
|
rc = lseek(fd, INT32_MAX, SEEK_SET);
|
|
|
|
EXPECT_EQ(rc, INT32_MAX);
|
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
2022-09-09 03:58:44 +00:00
|
|
|
EXPECT_EQ(rc, 16);
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(buffer != "abcdefghijklmno"sv);
|
|
|
|
|
|
|
|
rc = close(fd);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
rc = unlink("/tmp/x");
|
|
|
|
EXPECT_EQ(rc, 0);
|
2020-01-12 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 17:47:35 +00:00
|
|
|
TEST_CASE(rmdir_dot)
|
|
|
|
{
|
|
|
|
int rc = mkdir("/home/anon/rmdir-test-1", 0700);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = rmdir("/home/anon/rmdir-test-1/.");
|
|
|
|
EXPECT_NE(rc, 0);
|
|
|
|
EXPECT_EQ(errno, EINVAL);
|
|
|
|
|
|
|
|
rc = chdir("/home/anon/rmdir-test-1");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = rmdir(".");
|
|
|
|
VERIFY(rc != 0);
|
|
|
|
EXPECT_EQ(errno, EINVAL);
|
|
|
|
|
|
|
|
rc = rmdir("/home/anon/rmdir-test-1");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
}
|
|
|
|
|
2022-12-19 17:53:20 +00:00
|
|
|
TEST_CASE(rmdir_dot_dot)
|
|
|
|
{
|
|
|
|
int rc = mkdir("/home/anon/rmdir-test-2", 0700);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = mkdir("/home/anon/rmdir-test-2/foo", 0700);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = rmdir("/home/anon/rmdir-test-2/foo/..");
|
|
|
|
EXPECT_NE(rc, 0);
|
|
|
|
EXPECT_EQ(errno, ENOTEMPTY);
|
|
|
|
|
|
|
|
rc = rmdir("/home/anon/rmdir-test-2/foo");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = rmdir("/home/anon/rmdir-test-2");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:32:31 +00:00
|
|
|
TEST_CASE(rmdir_someone_elses_directory_in_my_sticky_directory)
|
|
|
|
{
|
|
|
|
// NOTE: This test only works when run as root, since it has to chown a directory to someone else.
|
|
|
|
if (getuid() != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create /tmp/sticky-dir a sticky directory owned by 12345:12345
|
|
|
|
// Then, create /tmp/sticky-dir/notmine, a normal directory owned by 23456:23456
|
|
|
|
// Then, fork and seteuid to 12345, and try to rmdir the "notmine" directory. This should succeed.
|
|
|
|
// In the parent, waitpid on the child, and finally rmdir /tmp/sticky-dir
|
|
|
|
|
|
|
|
int rc = mkdir("/tmp/sticky-dir", 01777);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = chown("/tmp/sticky-dir", 12345, 12345);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = mkdir("/tmp/sticky-dir/notmine", 0700);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = chown("/tmp/sticky-dir/notmine", 23456, 23456);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
EXPECT(pid >= 0);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// We are in the child.
|
|
|
|
rc = seteuid(12345);
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
|
|
|
|
rc = rmdir("/tmp/sticky-dir/notmine");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
|
|
|
|
rc = rmdir("/tmp/sticky-dir");
|
|
|
|
EXPECT_EQ(rc, 0);
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(rmdir_while_inside_dir)
|
2020-01-15 20:58:03 +00:00
|
|
|
{
|
|
|
|
int rc = mkdir("/home/anon/testdir", 0700);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-15 20:58:03 +00:00
|
|
|
|
|
|
|
rc = chdir("/home/anon/testdir");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-15 20:58:03 +00:00
|
|
|
|
|
|
|
rc = rmdir("/home/anon/testdir");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-15 20:58:03 +00:00
|
|
|
|
|
|
|
int fd = open("x", O_CREAT | O_RDWR, 0600);
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT(fd < 0);
|
|
|
|
EXPECT_EQ(errno, ENOENT);
|
2020-01-15 20:58:03 +00:00
|
|
|
if (fd >= 0 || errno != ENOENT) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Expected ENOENT when trying to create a file inside a deleted directory. Got {} with errno={}", fd, errno);
|
2020-01-15 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = chdir("/home/anon");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2020-01-15 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(writev)
|
2020-01-26 09:10:15 +00:00
|
|
|
{
|
|
|
|
int pipefds[2];
|
2021-07-07 05:13:58 +00:00
|
|
|
int rc = pipe(pipefds);
|
|
|
|
EXPECT(rc == 0);
|
2020-01-26 09:10:15 +00:00
|
|
|
|
|
|
|
iovec iov[2];
|
2022-04-01 17:58:27 +00:00
|
|
|
iov[0].iov_base = const_cast<void*>((void const*)"Hello");
|
2020-01-26 09:10:15 +00:00
|
|
|
iov[0].iov_len = 5;
|
2022-04-01 17:58:27 +00:00
|
|
|
iov[1].iov_base = const_cast<void*>((void const*)"Friends");
|
2020-01-26 09:10:15 +00:00
|
|
|
iov[1].iov_len = 7;
|
|
|
|
int nwritten = writev(pipefds[1], iov, 2);
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(nwritten, 12);
|
2020-01-26 09:10:15 +00:00
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("writev");
|
|
|
|
}
|
|
|
|
if (nwritten != 12) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Didn't write 12 bytes to pipe with writev");
|
2020-01-26 09:10:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
char buffer[32] {};
|
2020-01-26 09:10:15 +00:00
|
|
|
int nread = read(pipefds[0], buffer, sizeof(buffer));
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(nread, 12);
|
|
|
|
EXPECT_EQ(buffer, "HelloFriends"sv);
|
2020-01-26 09:10:15 +00:00
|
|
|
if (nread != 12 || memcmp(buffer, "HelloFriends", 12)) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Didn't read the expected data from pipe after writev");
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-01-26 09:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(pipefds[0]);
|
|
|
|
close(pipefds[1]);
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(rmdir_root)
|
2020-04-19 16:07:16 +00:00
|
|
|
{
|
|
|
|
int rc = rmdir("/");
|
2021-07-07 05:13:58 +00:00
|
|
|
EXPECT_EQ(rc, -1);
|
|
|
|
EXPECT_EQ(errno, EBUSY);
|
2020-04-19 16:07:16 +00:00
|
|
|
if (rc != -1 || errno != EBUSY) {
|
2020-10-25 16:46:16 +00:00
|
|
|
warnln("rmdir(/) didn't fail with EBUSY");
|
2020-04-19 16:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 05:13:58 +00:00
|
|
|
TEST_CASE(open_silly_things)
|
2020-01-03 02:16:47 +00:00
|
|
|
{
|
2021-07-07 05:13:58 +00:00
|
|
|
int rc = -1;
|
2020-01-03 02:16:47 +00:00
|
|
|
EXPECT_ERROR_2(ENOTDIR, open, "/dev/zero", (O_DIRECTORY | O_RDONLY));
|
|
|
|
EXPECT_ERROR_2(EINVAL, open, "/dev/zero", (O_DIRECTORY | O_CREAT | O_RDWR));
|
|
|
|
EXPECT_ERROR_2(EEXIST, open, "/dev/zero", (O_CREAT | O_EXCL | O_RDWR));
|
|
|
|
EXPECT_ERROR_2(EINVAL, open, "/tmp/abcdef", (O_DIRECTORY | O_CREAT | O_RDWR));
|
2022-10-14 18:53:53 +00:00
|
|
|
EXPECT_ERROR_2(EACCES, open, "/sys/kernel/processes", (O_RDWR));
|
2020-01-03 02:16:47 +00:00
|
|
|
EXPECT_ERROR_2(ENOENT, open, "/boof/baaf/nonexistent", (O_CREAT | O_RDWR));
|
|
|
|
EXPECT_ERROR_2(EISDIR, open, "/tmp", (O_DIRECTORY | O_RDWR));
|
2020-01-15 21:10:38 +00:00
|
|
|
EXPECT_ERROR_2(EPERM, link, "/", "/home/anon/lolroot");
|
2020-01-03 02:16:47 +00:00
|
|
|
}
|