diff --git a/.vscode/launch.json b/.vscode/launch.json index 6ee6b31..1ab4a22 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,7 +6,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/fstrace-debug", - "args": ["bash", "${workspaceFolder}/test2/1.sh"], + "args": ["${workspaceFolder}/build/tests_syscalls_openat2.c"], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], diff --git a/CMakeLists.txt b/CMakeLists.txt index b231c84..ba6e4f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,9 @@ add_compile_options(-Wall -Wextra -Wpedantic -Wimplicit-fallthrough) # Build fstrace add_executable(fstrace src/fstrace.cc) +# target_compile_options(fstrace PRIVATE --coverage) +# target_link_options(fstrace PRIVATE --coverage) + set(DEBUG_FD 1) target_compile_definitions(fstrace PRIVATE DEBUGFD=${DEBUG_FD}) @@ -77,7 +80,7 @@ include(GoogleTest) gtest_discover_tests(approval-tests) set(SOURCE_DIR "tests/syscalls") -file(GLOB SOURCES "${SOURCE_DIR}/**/*.c" "${SOURCE_DIR}/**/*.cpp") +file(GLOB SOURCES "${SOURCE_DIR}/*.c" "${SOURCE_DIR}/*.cpp") foreach(SOURCE_FILE ${SOURCES}) file(RELATIVE_PATH REL_PATH "${CMAKE_SOURCE_DIR}" "${SOURCE_FILE}") string(REPLACE "/" "_" TARGET_NAME ${REL_PATH}) diff --git a/tests/approval-tests/approval-tests.Syscalls.Open.approved.txt b/tests/approval-tests/approval-tests.SyscallTests.Open.approved.txt similarity index 76% rename from tests/approval-tests/approval-tests.Syscalls.Open.approved.txt rename to tests/approval-tests/approval-tests.SyscallTests.Open.approved.txt index c90470c..8bc64f8 100644 --- a/tests/approval-tests/approval-tests.Syscalls.Open.approved.txt +++ b/tests/approval-tests/approval-tests.SyscallTests.Open.approved.txt @@ -1,4 +1,4 @@ -RF ./tests_syscalls_open_basic.c +RF ./tests_syscalls_open.c RX /etc/ld.so.preload RF /etc/ld.so.cache RF /etc/ld.so.cache diff --git a/tests/approval-tests/approval-tests.Syscalls.OpenAt2_FDCWD.approved.txt b/tests/approval-tests/approval-tests.SyscallTests.OpenAt2.approved.txt similarity index 73% rename from tests/approval-tests/approval-tests.Syscalls.OpenAt2_FDCWD.approved.txt rename to tests/approval-tests/approval-tests.SyscallTests.OpenAt2.approved.txt index 89aecd7..7c9d92c 100644 --- a/tests/approval-tests/approval-tests.Syscalls.OpenAt2_FDCWD.approved.txt +++ b/tests/approval-tests/approval-tests.SyscallTests.OpenAt2.approved.txt @@ -1,7 +1,8 @@ -RF ./tests_syscalls_openat2_fdcwd.c +RF ./tests_syscalls_openat2.c RX /etc/ld.so.preload RF /etc/ld.so.cache RF /etc/ld.so.cache RF /lib/x86_64-linux-gnu/libc.so.6 RF /tmp +RD /tmp/ diff --git a/tests/approval-tests/approval-tests.cc b/tests/approval-tests/approval-tests.cc index 7749ba6..813678e 100644 --- a/tests/approval-tests/approval-tests.cc +++ b/tests/approval-tests/approval-tests.cc @@ -56,15 +56,14 @@ std::string getFsTraceOutput(const char *cmd) return result; } -TEST(Syscalls, OpenAt2_FDCWD) +TEST(SyscallTests, OpenAt2) { - std::string output = getFsTraceOutput("./tests_syscalls_openat2_fdcwd.c"); + std::string output = getFsTraceOutput("./tests_syscalls_openat2.c"); ApprovalTests::Approvals::verify(output); } - -TEST(Syscalls, Open) +TEST(SyscallTests, Open) { - std::string output = getFsTraceOutput("./tests_syscalls_open_basic.c"); + std::string output = getFsTraceOutput("./tests_syscalls_open.c"); ApprovalTests::Approvals::verify(output); } diff --git a/tests/syscalls/open/basic.c b/tests/syscalls/open.c similarity index 100% rename from tests/syscalls/open/basic.c rename to tests/syscalls/open.c diff --git a/tests/syscalls/openat2.c b/tests/syscalls/openat2.c new file mode 100644 index 0000000..21f9c2a --- /dev/null +++ b/tests/syscalls/openat2.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include + +// int openat2(int dirfd, const char *pathname, const struct open_how *how, size_t size); + +int dirfd1() +{ + int dirfd = open("/tmp", O_RDONLY); + + const char *pathname = "/tmp"; + struct open_how how = {.flags = O_PATH, .mode = 0}; + int size = sizeof(struct open_how); + syscall(SYS_openat2, dirfd, pathname, &how, size); +} + +int dirfd2() +{ + int dirfd = open("/tmp", O_RDONLY); + const char *pathname = "./"; + struct open_how how = {.flags = O_PATH, .mode = 0}; + int size = sizeof(struct open_how); + syscall(SYS_openat2, dirfd, pathname, &how, size); +} + +int atfdcwd1() +{ + int dirfd = AT_FDCWD; + const char *pathname = "/tmp"; + struct open_how how = {.flags = O_PATH, .mode = 0}; + int size = sizeof(struct open_how); + syscall(SYS_openat2, dirfd, pathname, &how, size); +} + +int atfdcwd2() +{ + int dirfd = AT_FDCWD; + chdir("/tmp"); + const char *pathname = "./"; + struct open_how how = {.flags = O_PATH, .mode = 0}; + int size = sizeof(struct open_how); + syscall(SYS_openat2, dirfd, pathname, &how, size); +} + +int main() +{ + // dirfd1(); + dirfd2(); + // atfdcwd1(); + // atfdcwd2(); + return 0; +} diff --git a/tests/syscalls/openat2/fdcwd.c b/tests/syscalls/openat2/fdcwd.c deleted file mode 100644 index 59c7139..0000000 --- a/tests/syscalls/openat2/fdcwd.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include -#include -#include - -// int openat2(int dirfd, const char *pathname, const struct open_how *how, size_t size); - -int main() -{ - int dirfd = AT_FDCWD; - const char *pathname = "/tmp"; - struct open_how how = {.flags = O_PATH, .mode = 0}; - int size = sizeof(struct open_how); - syscall(SYS_openat2, dirfd, pathname, &how, size); - - return 0; -}