diff --git a/programs/tests/CMakeLists.txt b/programs/tests/CMakeLists.txt index 3b366ed..d1dc66b 100644 --- a/programs/tests/CMakeLists.txt +++ b/programs/tests/CMakeLists.txt @@ -1,5 +1,6 @@ # List of programs. set(TEST_LIST + t_write_read.c t_gid.c t_alarm.c t_periodic3.c diff --git a/programs/tests/t_write_read.c b/programs/tests/t_write_read.c new file mode 100644 index 0000000..1cd9603 --- /dev/null +++ b/programs/tests/t_write_read.c @@ -0,0 +1,58 @@ +/// @file t_write_read.c +/// @brief Test consecutive writes +/// @copyright (c) 2024 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + char *file = "t_write_read_file"; + mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; + + int fd = creat(file, mode); + if (fd < 0) { + printf("Failed to open file %s: %s\n", file, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (write(fd, "foo", 3) != 3) { + printf("First write to %s failed: %s\n", file, strerror(errno)); + exit(1); + } + + if (write(fd, "bar", 3) != 3) { + printf("Second write to %s failed: %s\n", file, strerror(errno)); + exit(1); + } + + close(fd); + + fd = open(file, O_RDONLY, 0); + if (fd < 0) { + printf("Failed to open file %s: %s\n", file, strerror(errno)); + exit(1); + } + + ssize_t bytes_read; + char buf[7]; + buf[6] = 0; + if ((bytes_read = read(fd, &buf, 6)) < 0) { + printf("Reading from file %s failed: %s\n", file, strerror(errno)); + exit(1); + } + + if (strcmp(buf, "foobar") != 0) { + printf("Unexpected file content: %s\n", buf); + exit(1); + } + + unlink(file); + return 0; +}