Skip to content

Instantly share code, notes, and snippets.

@cosmologistPiyush
Created March 3, 2025 04:57
Show Gist options
  • Save cosmologistPiyush/421b6e73d0465973785d1454308a0042 to your computer and use it in GitHub Desktop.
Save cosmologistPiyush/421b6e73d0465973785d1454308a0042 to your computer and use it in GitHub Desktop.
ATF testcase
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/errno.h>
#include <atf-c.h>
extern int errno;
ATF_TC(read_test);
ATF_TC_HEAD(read_test, tc) {
atf_tc_set_md_var(tc, "descr", "Testing read");
}
ATF_TC_BODY(read_test, tc) {
ssize_t ret;
char *file = "readtester.txt";
int fd = open(file, O_WRONLY);
if (fd == -1) {
printf("open failed: %i\n", errno);
exit(1);
}
char data[] = "The quick brown fox jumped over the wall";
char buf[sizeof(data)];
ret = write(fd, data, sizeof(data));
printf("bytes written: %zu\n", ret);
fsync(fd);
fd = close(fd);
if (fd == -1) {
printf("close failed\n");
exit(1);
}
fd = open(file, O_RDONLY);
if (fd == -1) {
printf("close failed\n");
exit(1);
}
ret = read(fd, buf, sizeof(buf));
printf("bytes read: %zu\n", ret);
close(fd);
printf("buffer: %s\n", buf);
}
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, read_test);
return atf_no_error();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment