add test for ftruncate

This commit is contained in:
WangRunji 2019-03-13 16:15:08 +08:00 committed by Tate Tian
parent 0ec4ba9305
commit cbeab07352
3 changed files with 62 additions and 1 deletions

@ -4,7 +4,7 @@ PROJECT_DIR := $(realpath $(CUR_DIR)/../)
# Dependencies: need to be compiled but not to run by any Makefile target # Dependencies: need to be compiled but not to run by any Makefile target
TEST_DEPS := dev_null TEST_DEPS := dev_null
# Tests: need to be compiled and run by test-% target # 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 # Benchmarks: need to be compiled and run by bench-% target
BENCHES := spawn_and_exit_latency pipe_throughput BENCHES := spawn_and_exit_latency pipe_throughput

5
test/truncate/Makefile Normal file

@ -0,0 +1,5 @@
include ../test_common.mk
EXTRA_C_FLAGS :=
EXTRA_LINK_FLAGS :=
BIN_ARGS :=

56
test/truncate/main.c Normal file

@ -0,0 +1,56 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
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;
}