Add file I/O test

This commit is contained in:
Tate, Hongliang Tian 2019-01-07 13:11:13 +08:00
parent 2b4c8255c6
commit a2b62891cc
7 changed files with 65 additions and 17 deletions

@ -11,7 +11,7 @@ src:
@$(MAKE) --no-print-directory -C src
test:
@$(MAKE) --no-print-directory -C test run
@$(MAKE) --no-print-directory -C test test
clean:
@$(MAKE) --no-print-directory -C src clean

@ -49,7 +49,7 @@ pub fn do_open(path: &str, flags: u32, mode: u32) -> Result<FileDesc, Error> {
Arc::new(SgxMutex::new(sgx_file))
};
let is_readable = (flags & O_RDONLY != 0) || (flags & O_RDWR != 0);
let is_readable = (flags & O_WRONLY) == 0;
let is_writable = (flags & O_WRONLY != 0) || (flags & O_RDWR != 0);
let is_append = (flags & O_APPEND != 0);
let file_ref : Arc<Box<File>> = Arc::new(Box::new(

@ -1,9 +1,9 @@
CUR_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
PROJECT_DIR := $(realpath $(CUR_DIR)/../)
TEST_SUITES := empty hello_world malloc getpid spawn
TEST_SUITES := empty hello_world malloc file getpid spawn
BUILD_TEST_SUITES := $(TEST_SUITES:%=%)
RUN_TEST_SUITES := $(TEST_SUITES:%=run-%)
RUN_TEST_SUITES := $(TEST_SUITES:%=test-%)
CLEAN_TEST_SUITES := $(TEST_SUITES:%=clean-%)
CYAN := \033[1;36m
@ -30,7 +30,7 @@ $(BUILD_TEST_SUITES): %:
# Run tests
#############################################################################
run: build $(RUN_TEST_SUITES)
test: build $(RUN_TEST_SUITES)
pal: $(PROJECT_DIR)/src/pal/pal
@cp $< pal
@ -38,9 +38,9 @@ pal: $(PROJECT_DIR)/src/pal/pal
libocclum.signed.so: $(PROJECT_DIR)/src/libos/libocclum.signed.so
@cp $< libocclum.signed.so
$(RUN_TEST_SUITES): run-%: % pal libocclum.signed.so
@echo "$(CYAN)TEST => $<$(NO_COLOR)"
@$(MAKE) --no-print-directory -C $< run ; \
$(RUN_TEST_SUITES): test-%: % pal libocclum.signed.so
@echo "$(CYAN)RUN TEST => $<$(NO_COLOR)"
@$(MAKE) --no-print-directory -C $< test ; \
if [ $$? -eq 0 ] ; then \
echo "$(GREEN)PASS$(NO_COLOR)" ; \
else \

4
test/file/Makefile Normal file

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

44
test/file/main.c Normal file

@ -0,0 +1,44 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main() {
const char* file_name = "tmp.txt";
int fd, flags, mode, len;
const char* write_msg = "Hello World\n";
char read_buf[128] = {0};
flags = O_WRONLY | O_CREAT| O_TRUNC;
mode = 00666;
if ((fd = open(file_name, flags, mode)) < 0) {
printf("ERROR: failed to open a file for write\n");
return -1;
}
if ((len = write(fd, write_msg, strlen(write_msg))) <= 0) {
printf("ERROR: failed to write to the file\n");
return -1;
}
close(fd);
flags = O_RDONLY;
if ((fd = open(file_name, flags)) < 0) {
printf("ERROR: failed to open a file for read\n");
return -1;
}
if ((len = read(fd, read_buf, sizeof(read_buf) - 1)) <= 0) {
printf("ERROR: failed to read from the file\n");
return -1;
}
close(fd);
if (strcmp(write_msg, read_buf) != 0) {
printf("ERROR: the message read from the file is not as it was written\n");
return -1;
}
printf("File write and read succesfully\n");
return 0;
}

@ -4,7 +4,7 @@
static const char* msg = "Hello World\n";
int main(int argc) {
int main() {
printf("%s", msg);
return 0;
}

@ -16,7 +16,7 @@ C_FLAGS = -Wall -O0 $(EXTRA_C_FLAGS)
C_FLAGS += -Xclang -load -Xclang $(LLVM_PATH)/lib/LLVMBoundchecker.so -mllvm -check-store-only=true
LINK_FLAGS = $(C_FLAGS) $(EXTRA_LINK_FLAGS)
.PHONY: all run debug clean
.PHONY: all test debug clean
#############################################################################
# Build
@ -44,18 +44,18 @@ $(READELF_FILE): $(BIN_NAME)
@echo "READELF => $@"
$(BIN_NAME): $(C_OBJS)
@$(CC) $^ $(LINK_FLAGS) -o $(BIN_NAME) 2> /dev/null
@$(CC) $^ $(LINK_FLAGS) -o $(BIN_NAME)
@echo "LINK => $@"
$(C_OBJS): %.o: %.c
@$(CC) $(C_FLAGS) -c $< -o $@ 2> /dev/null
@$(CC) $(C_FLAGS) -c $< -o $@
@echo "CC <= $@"
#############################################################################
# Test
#############################################################################
run: $(BIN_ENC_NAME)
test: $(BIN_ENC_NAME)
@cd ../ && RUST_BACKTRACE=1 ./pal $(CUR_DIR)/$(BIN_ENC_NAME)
#############################################################################