tests: add simple test for small consecutive writes

This test was written for: 4d4504486d.
This commit is contained in:
Florian Fischer
2024-02-13 21:39:53 +01:00
parent 88b1846a43
commit f124be1cf7
2 changed files with 59 additions and 0 deletions
+1
View File
@@ -1,5 +1,6 @@
# List of programs.
set(TEST_LIST
t_write_read.c
t_gid.c
t_alarm.c
t_periodic3.c
+58
View File
@@ -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 <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strerror.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/unistd.h>
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;
}