diff --git a/Tests/Kernel/CMakeLists.txt b/Tests/Kernel/CMakeLists.txt index e05be25829..9e49b0487c 100644 --- a/Tests/Kernel/CMakeLists.txt +++ b/Tests/Kernel/CMakeLists.txt @@ -41,6 +41,7 @@ set(LIBTEST_BASED_SOURCES TestEmptyPrivateInodeVMObject.cpp TestEmptySharedInodeVMObject.cpp TestExt2FS.cpp + TestFileSystemDirentTypes.cpp TestInvalidUIDSet.cpp TestSharedInodeVMObject.cpp TestPosixFallocate.cpp diff --git a/Tests/Kernel/TestFileSystemDirentTypes.cpp b/Tests/Kernel/TestFileSystemDirentTypes.cpp new file mode 100644 index 0000000000..963f2bf0b3 --- /dev/null +++ b/Tests/Kernel/TestFileSystemDirentTypes.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2024, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +TEST_CASE(test_sysfs_root_directory) +{ + auto dirfd = open("/sys/", O_RDONLY | O_DIRECTORY); + auto cleanup_guard = ScopeGuard([&] { + close(dirfd); + }); + + DIR* dir = fdopendir(dirfd); + EXPECT(dir != nullptr); + + auto cleanup_dir_guard = ScopeGuard([&] { + closedir(dir); + }); + + auto* _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '.' + EXPECT_EQ(_dirent->d_type, DT_DIR); + + _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '..' + EXPECT_EQ(_dirent->d_type, DT_DIR); + + while (true) { + _dirent = readdir(dir); + if (_dirent == nullptr) + break; + // NOTE: We should only see a directory entry in the /sys directory + EXPECT_EQ(_dirent->d_type, DT_DIR); + } +} + +TEST_CASE(test_devpts_root_directory) +{ + auto dirfd = open("/dev/pts/", O_RDONLY | O_DIRECTORY); + auto cleanup_guard = ScopeGuard([&] { + close(dirfd); + }); + + DIR* dir = fdopendir(dirfd); + EXPECT(dir != nullptr); + + auto cleanup_dir_guard = ScopeGuard([&] { + closedir(dir); + }); + + auto* _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '.' + EXPECT_EQ(_dirent->d_type, DT_DIR); + + _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '..' + EXPECT_EQ(_dirent->d_type, DT_DIR); +} + +TEST_CASE(test_procfs_root_directory) +{ + auto dirfd = open("/proc/", O_RDONLY | O_DIRECTORY); + auto cleanup_guard = ScopeGuard([&] { + close(dirfd); + }); + + DIR* dir = fdopendir(dirfd); + EXPECT(dir != nullptr); + + auto cleanup_dir_guard = ScopeGuard([&] { + closedir(dir); + }); + + auto* _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '.' now + EXPECT_EQ(_dirent->d_type, DT_DIR); + + _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see '..' now + EXPECT_EQ(_dirent->d_type, DT_DIR); + + _dirent = readdir(dir); + EXPECT(_dirent != nullptr); + // NOTE: We should see 'self' now + EXPECT_EQ(_dirent->d_type, DT_LNK); +}