From: Davin McCall Date: Tue, 14 Apr 2020 22:56:21 +0000 (+1000) Subject: Add mock 'open' system call to bpsys X-Git-Tag: v0.8.2~8 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=13ecb45a5c5a0dba34e264f06816ab3d229c5a36;p=oweals%2Fdinit.git Add mock 'open' system call to bpsys This allows a test harness to supply file content for particular paths; opening a "file" will then allow reading the supplied content. --- diff --git a/src/includes/baseproc-sys.h b/src/includes/baseproc-sys.h index fcf5056..6c4f7a9 100644 --- a/src/includes/baseproc-sys.h +++ b/src/includes/baseproc-sys.h @@ -19,6 +19,7 @@ namespace bp_sys { using dasynq::pipe2; using ::fcntl; +using ::open; using ::close; using ::kill; using ::getpgid; diff --git a/src/includes/dinit-util.h b/src/includes/dinit-util.h index bd28c2e..caa2cdd 100644 --- a/src/includes/dinit-util.h +++ b/src/includes/dinit-util.h @@ -9,6 +9,8 @@ #include #include +#include "baseproc-sys.h" + // Complete read - read the specified size until end-of-file or error; continue read if // interrupted by signal. inline ssize_t complete_read(int fd, void * buf, size_t n) @@ -16,7 +18,7 @@ inline ssize_t complete_read(int fd, void * buf, size_t n) char * cbuf = static_cast(buf); ssize_t r = 0; while ((size_t)r < n) { - ssize_t res = read(fd, cbuf + r, n - r); + ssize_t res = bp_sys::read(fd, cbuf + r, n - r); if (res == 0) { return r; } diff --git a/src/tests/test-bpsys.cc b/src/tests/test-bpsys.cc index fc0a08d..737231b 100644 --- a/src/tests/test-bpsys.cc +++ b/src/tests/test-bpsys.cc @@ -39,6 +39,9 @@ std::map read_data; // map of fd to the handler for writes to that fd std::map> write_hndlr_map; +// map of path to file content +std::map> file_content_map; + } // anon namespace namespace bp_sys { @@ -102,9 +105,32 @@ void extract_written_data(int fd, std::vector &data) data = std::move(dwhndlr->data); } +// Supply a file content +void supply_file_content(const std::string &path, std::vector &data) +{ + file_content_map[path] = data; +} + +void supply_file_content(const std::string &path, std::vector &&data) +{ + file_content_map[path] = std::move(data); +} // Mock implementations of system calls: +int open(const char *pathname, int flags) +{ + auto i = file_content_map.find(pathname); + if (i == file_content_map.end()) { + errno = ENOENT; + return -1; + } + + int nfd = allocfd(); + supply_read_data(nfd, i->second); + return nfd; +} + int pipe2(int fds[2], int flags) { fds[0] = allocfd(); diff --git a/src/tests/test-includes/baseproc-sys.h b/src/tests/test-includes/baseproc-sys.h index 2d29807..e0c1d98 100644 --- a/src/tests/test-includes/baseproc-sys.h +++ b/src/tests/test-includes/baseproc-sys.h @@ -43,10 +43,13 @@ void supply_read_data(int fd, std::vector &data); void supply_read_data(int fd, std::vector &&data); void set_blocking(int fd); void extract_written_data(int fd, std::vector &data); +void supply_file_content(const std::string &path, const std::vector &data); +void supply_file_content(const std::string &path, std::vector &&data); // Mock system calls: // implementations elsewhere: +int open(const char *pathname, int flags); int pipe2(int pipefd[2], int flags); int close(int fd); int kill(pid_t pid, int sig); @@ -126,7 +129,8 @@ class exit_status inline pid_t waitpid(pid_t p, exit_status *statusp, int flags) { - throw std::string("not implemented"); + // throw std::string("not implemented"); + return 0; // TODO complete mock } ssize_t read(int fd, void *buf, size_t count);