diff --git a/test/Makefile b/test/Makefile index 5de43a36..c35b0630 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../) # Dependencies: need to be compiled but not to run by any Makefile target TEST_DEPS := dev_null # Tests: need to be compiled and run by test-% target -TESTS := empty argv hello_world malloc file getpid spawn pipe time +TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate # Benchmarks: need to be compiled and run by bench-% target BENCHES := spawn_and_exit_latency pipe_throughput diff --git a/test/truncate/Makefile b/test/truncate/Makefile new file mode 100644 index 00000000..9e1b6dec --- /dev/null +++ b/test/truncate/Makefile @@ -0,0 +1,5 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := +BIN_ARGS := diff --git a/test/truncate/main.c b/test/truncate/main.c new file mode 100644 index 00000000..2fdcec23 --- /dev/null +++ b/test/truncate/main.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, const char* argv[]) { + const char* file_name = "tmp.txt"; + const int TRUNC_LEN = 256; + const int MODE_MASK = 0777; + + int ret; + + int flags = O_WRONLY | O_CREAT| O_TRUNC; + int mode = 00666; + int fd = open(file_name, flags, mode); + if (fd < 0) { + printf("failed to open a file for write\n"); + return fd; + } + + ret = ftruncate(fd, TRUNC_LEN); + if (ret < 0) { + printf("failed to truncate the file\n"); + return ret; + } + + struct stat stat_buf; + ret = fstat(fd, &stat_buf); + if (ret < 0) { + printf("failed to fstat the file\n"); + return ret; + } + + int file_size = stat_buf.st_size; + if (file_size != TRUNC_LEN) { + printf("Incorrect file size %d. Expected %d\n", file_size, TRUNC_LEN); + return -1; + } + int file_mode = stat_buf.st_mode & MODE_MASK; + if (file_mode != mode) { + printf("Incorrect file mode %o. Expected %o\n", file_mode, mode); + return -1; + } + int file_type = stat_buf.st_mode & S_IFMT; + if (file_type != S_IFREG) { + printf("Incorrect file type %o. Expected %o\n", file_type, S_IFREG); + return -1; + } + + close(fd); + + printf("Truncate & fstat test succesful\n"); + return 0; +}