From 89be7ea160b48624c7092905fcc0d6fd804bd40b Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 15 Jan 2018 09:10:10 +0000 Subject: [PATCH] Implement mock functionality for some system calls in bp_sys. --- src/tests/Makefile | 10 +++---- src/tests/proctests.cc | 4 +++ src/tests/test-bpsys.cc | 40 ++++++++++++++++++++++++++ src/tests/test-includes/baseproc-sys.h | 16 ++++------- src/tests/test-includes/dinit.h | 2 +- 5 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 src/tests/test-bpsys.cc diff --git a/src/tests/Makefile b/src/tests/Makefile index b4b1153..dd82c0e 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,6 +1,6 @@ -include ../../mconfig -objects = tests.o test-dinit.o proctests.o test-run-child-proc.o +objects = tests.o test-dinit.o proctests.o test-run-child-proc.o test-bpsys.o parent_objs = service.o proc-service.o dinit-log.o load_service.o baseproc-service.o check: build-tests @@ -16,11 +16,11 @@ prepare-incdir: cd includes; ln -sf ../../includes/*.h . cd includes; ln -sf ../test-includes/*.h . -tests: prepare-incdir $(parent_objs) tests.o test-dinit.o - $(CXX) $(SANITIZEOPTS) -o tests $(parent_objs) tests.o test-dinit.o $(EXTRA_LIBS) +tests: prepare-incdir $(parent_objs) tests.o test-dinit.o test-bpsys.o + $(CXX) $(SANITIZEOPTS) -o tests $(parent_objs) tests.o test-dinit.o test-bpsys.o $(EXTRA_LIBS) -proctests: prepare-incdir $(parent_objs) proctests.o test-dinit.o - $(CXX) $(SANITIZEOPTS) -o proctests $(parent_objs) proctests.o test-dinit.o $(EXTRA_LIBS) +proctests: prepare-incdir $(parent_objs) proctests.o test-dinit.o test-bpsys.o + $(CXX) $(SANITIZEOPTS) -o proctests $(parent_objs) proctests.o test-dinit.o test-bpsys.o $(EXTRA_LIBS) $(objects): %.o: %.cc $(CXX) $(CXXOPTS) $(SANITIZEOPTS) -Iincludes -I../dasynq -c $< -o $@ diff --git a/src/tests/proctests.cc b/src/tests/proctests.cc index 9d71560..eb45389 100644 --- a/src/tests/proctests.cc +++ b/src/tests/proctests.cc @@ -41,8 +41,12 @@ void test1() process_service p = process_service(&sset, "testproc", std::move(command), command_offsets, depends); p.start(true); + sset.process_queues(); + + assert(p.get_state() == service_state_t::STARTING); base_process_service_test::exec_succeeded(&p); + sset.process_queues(); assert(p.get_state() == service_state_t::STARTED); } diff --git a/src/tests/test-bpsys.cc b/src/tests/test-bpsys.cc new file mode 100644 index 0000000..a244f8c --- /dev/null +++ b/src/tests/test-bpsys.cc @@ -0,0 +1,40 @@ +#include +#include +#include + +#include "baseproc-sys.h" + +std::vector usedfds = {true, true, true}; + +// Allocate a file descriptor +static int allocfd() +{ + auto f = std::find(usedfds.begin(), usedfds.end(), false); + if (f == usedfds.end()) { + int r = usedfds.size(); + usedfds.push_back(true); + return r; + } + + *f = true; + return f - usedfds.begin(); +} + +namespace bp_sys { + +int pipe2(int fds[2], int flags) +{ + fds[0] = allocfd(); + fds[1] = allocfd(); + return 0; +} + +int close(int fd) +{ + if (fd >= usedfds.size()) abort(); + + usedfds[fd] = false; + return 0; +} + +} diff --git a/src/tests/test-includes/baseproc-sys.h b/src/tests/test-includes/baseproc-sys.h index b12c495..fbe4a61 100644 --- a/src/tests/test-includes/baseproc-sys.h +++ b/src/tests/test-includes/baseproc-sys.h @@ -1,14 +1,12 @@ #include +#include // Mock system functions for testing. namespace bp_sys { -inline int pipe2(int pipefd[2], int flags) -{ - abort(); - return 0; -} +int pipe2(int pipefd[2], int flags); +int close(int fd); inline int fcntl(int fd, int cmd, ...) { @@ -16,14 +14,10 @@ inline int fcntl(int fd, int cmd, ...) return 0; } -inline int close(int fd) -{ - abort(); - return 0; -} - inline int kill(pid_t pid, int sig) { + // No proper mock implemented yet: + std::cout << "(kill; aborting)" << std::endl; abort(); return 0; } diff --git a/src/tests/test-includes/dinit.h b/src/tests/test-includes/dinit.h index 8582bed..521f68f 100644 --- a/src/tests/test-includes/dinit.h +++ b/src/tests/test-includes/dinit.h @@ -22,7 +22,7 @@ class eventloop_t public: pid_t fork(eventloop_t &loop, bool reserved_child_watcher, int priority = dasynq::DEFAULT_PRIORITY) { - return -1; + return 2; // doesn't matter much what we return here, but it should be a potentially valid pid } void add_reserved(eventloop_t &eloop, pid_t child, int prio = dasynq::DEFAULT_PRIORITY) noexcept -- 2.25.1