From a50f2dca670047c73670471b424d1adba599988f Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Mon, 12 Jun 2017 22:02:37 +0200 Subject: [PATCH] tests: provide test_tool_stdin_write() Implement a routine allowing the test code to write a formatted string to the child process' standard input. Signed-off-by: Bartosz Golaszewski --- tests/gpiod-test.c | 22 ++++++++++++++++++++++ tests/gpiod-test.h | 1 + 2 files changed, 23 insertions(+) diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c index e273ab3..6a671e6 100644 --- a/tests/gpiod-test.c +++ b/tests/gpiod-test.c @@ -446,6 +446,28 @@ void test_tool_signal(int signum) die_perr("unable to send signal to process %d", proc->pid); } +void test_tool_stdin_write(const char *fmt, ...) +{ + struct gpiotool_proc *proc = &globals.test_ctx.tool_proc; + ssize_t written; + va_list va; + char *in; + int rv; + + va_start(va, fmt); + rv = vasprintf(&in, fmt, va); + va_end(va); + if (rv < 0) + die_perr("error building string"); + + written = write(proc->stdin_fd, in, rv); + free(in); + if (written < 0) + die_perr("error writing to child process' stdin"); + if (written != rv) + die("unable to write all data to child process' stdin"); +} + void test_tool_run(char *tool, ...) { int in_fds[2], out_fds[2], err_fds[2], status; diff --git a/tests/gpiod-test.h b/tests/gpiod-test.h index 4ba89ed..e342677 100644 --- a/tests/gpiod-test.h +++ b/tests/gpiod-test.h @@ -110,6 +110,7 @@ const char * test_tool_stderr(void); bool test_tool_exited(void); int test_tool_exit_status(void); void test_tool_signal(int signum); +void test_tool_stdin_write(const char *fmt, ...) TEST_PRINTF(1, 2); /* * Every TEST_ASSERT_*() macro expansion can make a test function return, so -- 2.30.2