ladybird/Tests/LibC/TestLibCInodeWatcher.cpp
Ben Wiederhake 6b7ce19161 Everywhere: Remove unused includes of LibC/stdlib.h
These instances were detected by searching for files that include
stdlib.h, but don't match the regex:

\\b(_abort|abort|abs|aligned_alloc|arc4random|arc4random_buf|arc4random_
uniform|atexit|atof|atoi|atol|atoll|bsearch|calloc|clearenv|div|div_t|ex
it|_Exit|EXIT_FAILURE|EXIT_SUCCESS|free|getenv|getprogname|grantpt|labs|
ldiv|ldiv_t|llabs|lldiv|lldiv_t|malloc|malloc_good_size|malloc_size|mble
n|mbstowcs|mbtowc|mkdtemp|mkstemp|mkstemps|mktemp|posix_memalign|posix_o
penpt|ptsname|ptsname_r|putenv|qsort|qsort_r|rand|RAND_MAX|random|reallo
c|realpath|secure_getenv|serenity_dump_malloc_stats|serenity_setenv|sete
nv|setprogname|srand|srandom|strtod|strtof|strtol|strtold|strtoll|strtou
l|strtoull|system|unlockpt|unsetenv|wcstombs|wctomb)\\b

(Without the linebreaks.)

This regex is pessimistic, so there might be more files that don't
actually use anything from the stdlib.

In theory, one might use LibCPP to detect things like this
automatically, but let's do this one step after another.
2023-01-02 20:27:20 -05:00

169 lines
4.2 KiB
C++

/*
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/NumericLimits.h>
#include <Kernel/API/InodeWatcherEvent.h>
#include <Kernel/API/InodeWatcherFlags.h>
#include <LibTest/TestCase.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <utime.h>
u8 buffer[MAXIMUM_EVENT_SIZE];
InodeWatcherEvent* event = reinterpret_cast<InodeWatcherEvent*>(buffer);
static int read_event(int fd)
{
int rc = read(fd, &buffer, MAXIMUM_EVENT_SIZE);
return rc;
}
static DeprecatedString get_event_name()
{
if (event->name_length == 0)
return DeprecatedString();
return DeprecatedString { event->name, event->name_length - 1 };
}
TEST_CASE(inode_watcher_metadata_modified_event)
{
int fd = create_inode_watcher(0);
EXPECT_NE(fd, -1);
int test_fd = creat("/tmp/testfile", 0777);
EXPECT_NE(test_fd, -1);
int wd = inode_watcher_add_watch(fd, "/tmp/testfile", 13, static_cast<unsigned>(InodeWatcherEvent::Type::MetadataModified));
EXPECT_NE(wd, -1);
// "touch" the file
int rc = utime("/tmp/testfile", nullptr);
EXPECT_NE(rc, -1);
rc = read_event(fd);
EXPECT_EQ(event->watch_descriptor, wd);
EXPECT_EQ(event->type, InodeWatcherEvent::Type::MetadataModified);
close(fd);
close(test_fd);
unlink("/tmp/testfile");
}
TEST_CASE(inode_watcher_content_modified_event)
{
int fd = create_inode_watcher(0);
EXPECT_NE(fd, -1);
int test_fd = creat("/tmp/testfile", 0777);
EXPECT_NE(test_fd, -1);
int wd = inode_watcher_add_watch(fd, "/tmp/testfile", 13, static_cast<unsigned>(InodeWatcherEvent::Type::ContentModified));
EXPECT_NE(wd, -1);
int rc = write(test_fd, "test", 4);
EXPECT_NE(rc, -1);
rc = read_event(fd);
EXPECT_NE(rc, -1);
EXPECT_EQ(event->watch_descriptor, wd);
EXPECT_EQ(event->type, InodeWatcherEvent::Type::ContentModified);
close(fd);
close(test_fd);
unlink("/tmp/testfile");
}
TEST_CASE(inode_watcher_deleted_event)
{
int fd = create_inode_watcher(0);
EXPECT_NE(fd, -1);
int test_fd = creat("/tmp/testfile", 0777);
EXPECT_NE(test_fd, -1);
int wd = inode_watcher_add_watch(fd, "/tmp/testfile", 13, static_cast<unsigned>(InodeWatcherEvent::Type::Deleted));
EXPECT_NE(wd, -1);
int rc = unlink("/tmp/testfile");
EXPECT_NE(rc, -1);
rc = read_event(fd);
EXPECT_NE(rc, -1);
EXPECT_EQ(event->watch_descriptor, wd);
EXPECT_EQ(event->type, InodeWatcherEvent::Type::Deleted);
close(fd);
close(test_fd);
}
TEST_CASE(inode_watcher_child_events)
{
int fd = create_inode_watcher(0);
EXPECT_NE(fd, -1);
int wd = inode_watcher_add_watch(fd, "/tmp/", 5, static_cast<unsigned>(InodeWatcherEvent::Type::ChildCreated | InodeWatcherEvent::Type::ChildDeleted));
EXPECT_NE(fd, -1);
int rc = creat("/tmp/testfile", 0777);
EXPECT_NE(rc, -1);
rc = read_event(fd);
EXPECT_NE(rc, -1);
EXPECT_EQ(event->watch_descriptor, wd);
EXPECT_EQ(event->type, InodeWatcherEvent::Type::ChildCreated);
VERIFY(event->name_length > 0);
EXPECT_EQ(get_event_name(), "testfile");
rc = unlink("/tmp/testfile");
EXPECT_NE(rc, -1);
rc = read_event(fd);
EXPECT_NE(rc, -1);
EXPECT_EQ(event->watch_descriptor, wd);
EXPECT_EQ(event->type, InodeWatcherEvent::Type::ChildDeleted);
VERIFY(event->name_length > 0);
EXPECT_EQ(get_event_name(), "testfile");
close(fd);
}
TEST_CASE(inode_watcher_closes_children_on_close)
{
int fd = create_inode_watcher(0);
EXPECT_NE(fd, -1);
int test_fd = creat("/tmp/testfile", 0777);
EXPECT_NE(test_fd, -1);
int wd = inode_watcher_add_watch(fd, "/tmp/testfile", 13, static_cast<unsigned>(InodeWatcherEvent::Type::MetadataModified));
EXPECT_NE(wd, -1);
int rc = utime("/tmp/testfile", nullptr);
EXPECT_NE(rc, -1);
close(fd);
rc = read_event(fd);
EXPECT_EQ(rc, -1);
EXPECT_EQ(errno, EBADF);
close(test_fd);
unlink("/tmp/testfile");
}
TEST_CASE(inode_watcher_nonblock)
{
int fd = create_inode_watcher(static_cast<unsigned>(InodeWatcherFlags::Nonblock));
EXPECT_NE(fd, -1);
int rc = read_event(fd);
EXPECT_EQ(rc, -1);
EXPECT_EQ(errno, EAGAIN);
close(fd);
}