diff options
author | Andreas Kling <awesomekling@gmail.com> | 2020-01-03 03:16:47 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2020-01-03 03:18:49 +0100 |
commit | 2da3edb3d0d1faacc3c05f998eb68365cd5bdbc7 (patch) | |
tree | b81c5628765b5d6221ce8aa8160128dd2c6f74df /Userland/test_io.cpp | |
parent | 064e46e581e682ca8e99e2771494c634bffcedb4 (diff) | |
download | serenity-2da3edb3d0d1faacc3c05f998eb68365cd5bdbc7.zip |
test_io: Add a simple test program that abuses some I/O syscalls
This exposes some very bad behaviors that will need fixing.
Diffstat (limited to 'Userland/test_io.cpp')
-rw-r--r-- | Userland/test_io.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Userland/test_io.cpp b/Userland/test_io.cpp new file mode 100644 index 0000000000..2ee3fdf936 --- /dev/null +++ b/Userland/test_io.cpp @@ -0,0 +1,71 @@ +#include <AK/Assertions.h> +#include <AK/Types.h> +#include <fcntl.h> +#include <stdio.h> +#include <sys/mman.h> +#include <unistd.h> + +#define EXPECT_OK(syscall, address, size) \ + do { \ + rc = syscall(fd, (void*)(address)); \ + if (rc < 0) { \ + fprintf(stderr, "Expected success: " #syscall "(%p, %zu), got rc=%d, errno=%d\n", (void*)(address), (size_t)(size), rc, errno); \ + } \ + } while (0) + +#define EXPECT_ERROR_2(err, syscall, arg1, arg2) \ + do { \ + rc = syscall(arg1, arg2); \ + if (rc >= 0 || errno != err) { \ + fprintf(stderr, __FILE__ ":%d: Expected " #err ": " #syscall "(%p, %p), got rc=%d, errno=%d\n", __LINE__, (const void*)(arg1), (const void*)arg2, rc, errno); \ + } \ + } while (0) + +#define EXPECT_ERROR_3(err, syscall, arg1, arg2, arg3) \ + do { \ + rc = syscall(arg1, arg2, arg3); \ + if (rc >= 0 || errno != err) { \ + fprintf(stderr, __FILE__ ":%d: Expected " #err ": " #syscall "(%p, %p, %p), got rc=%d, errno=%d\n", __LINE__, (const void*)(arg1), (const void*)(arg2), (const void*)(arg3), rc, errno); \ + } \ + } while (0) + +void test_read_from_directory() +{ + char buffer[BUFSIZ]; + int fd = open("/", O_DIRECTORY | O_RDONLY); + ASSERT(fd >= 0); + int rc; + EXPECT_ERROR_3(EISDIR, read, fd, buffer, sizeof(buffer)); + rc = close(fd); + ASSERT(rc == 0); +} + +void test_write_to_directory() +{ + char str[] = "oh frick"; + int fd = open("/", O_DIRECTORY | O_RDONLY); + if (fd < 0) + perror("open"); + ASSERT(fd >= 0); + int rc; + EXPECT_ERROR_3(EBADF, write, fd, str, sizeof(str)); + rc = close(fd); + ASSERT(rc == 0); +} + +int main(int, char**) +{ + int rc; + 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)); + EXPECT_ERROR_2(EACCES, open, "/proc/all", (O_RDWR)); + EXPECT_ERROR_2(ENOENT, open, "/boof/baaf/nonexistent", (O_CREAT | O_RDWR)); + EXPECT_ERROR_2(EISDIR, open, "/tmp", (O_DIRECTORY | O_RDWR)); + + test_read_from_directory(); + test_write_to_directory(); + + return 0; +} |