From a3483851a0c835c36543ef37cb4e4c77877a485f Mon Sep 17 00:00:00 2001 From: WangRunji Date: Tue, 26 Mar 2019 17:46:34 +0800 Subject: [PATCH] add test for link, unlink, rename --- test/Makefile | 2 +- test/link/Makefile | 5 +++ test/link/main.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 test/link/Makefile create mode 100644 test/link/main.c diff --git a/test/Makefile b/test/Makefile index 398744d5..37a90fae 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 truncate readdir mkdir +TESTS := empty argv hello_world malloc file getpid spawn pipe time truncate readdir mkdir link # Benchmarks: need to be compiled and run by bench-% target BENCHES := spawn_and_exit_latency pipe_throughput diff --git a/test/link/Makefile b/test/link/Makefile new file mode 100644 index 00000000..9e1b6dec --- /dev/null +++ b/test/link/Makefile @@ -0,0 +1,5 @@ +include ../test_common.mk + +EXTRA_C_FLAGS := +EXTRA_LINK_FLAGS := +BIN_ARGS := diff --git a/test/link/main.c b/test/link/main.c new file mode 100644 index 00000000..a959079a --- /dev/null +++ b/test/link/main.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, const char* argv[]) { + const char* file_name = "tmp.txt"; + const char* link_name = "link.txt"; + const char* write_msg = "Hello World\n"; + char read_buf[128] = {0}; + int ret; + + // create a file and write message + int flags = O_WRONLY | O_CREAT| O_TRUNC; + int mode = 00666; + int fd = open(file_name, flags, mode); + if (fd < 0) { + printf("ERROR: failed to open a file for write\n"); + return -1; + } + int len = write(fd, write_msg, strlen(write_msg)); + if (len <= 0) { + printf("ERROR: failed to write to the file\n"); + return -1; + } + close(fd); + + // link + ret = link(file_name, link_name); + if(ret < 0) { + printf("ERROR: failed to link the file\n"); + return -1; + } + + // read the link file + fd = open(link_name, O_RDONLY, 00666); + if (fd < 0) { + printf("ERROR: failed to open the file for read\n"); + return -1; + } + len = read(fd, read_buf, sizeof(read_buf)); + if (len != strlen(write_msg)) { + printf("ERROR: failed to read to the file\n"); + return -1; + } + ret = strcmp(write_msg, read_buf); + if (ret != 0) { + printf("ERROR: the message read from the file is not as it was written\n"); + return -1; + } + + // unlink + ret = unlink(link_name); + if(ret < 0) { + printf("ERROR: failed to link the file\n"); + return -1; + } + + // stat + struct stat stat_buf; + ret = stat(link_name, &stat_buf); + if(!(ret < 0 && errno == ENOENT)) { + printf("ERROR: stat on \"%s\" should return ENOENT", link_name); + return -1; + } + + // rename + ret = rename(file_name, link_name); + if(ret < 0) { + printf("ERROR: failed to rename the file"); + return -1; + } + + // stat + ret = stat(file_name, &stat_buf); + if(!(ret < 0 && errno == ENOENT)) { + printf("ERROR: stat on \"%s\" should return ENOENT", file_name); + return -1; + } + ret = stat(link_name, &stat_buf); + if(ret < 0) { + printf("ERROR: failed to stat the file"); + return -1; + } + + printf("link, unlink, rename test successful\n"); + return 0; +}