[PATCH v2 2/2] selftests/nolibc: add testcase for pipe

From: Yuan Tan
Date: Mon Jul 31 2023 - 01:51:33 EST


Add a testcase of pipe that child process sends message to parent
process.

Here we use memcmp() to avoid the output buffer issue.

Suggested-by: Thomas Weißschuh <thomas@xxxxxxxx>
Suggested-by: Willy Tarreau <w@xxxxxx>
Link: https://lore.kernel.org/all/c5de2d13-3752-4e1b-90d9-f58cca99c702@xxxxxxxx/
Signed-off-by: Yuan Tan <tanyuan@xxxxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 35 ++++++++++++++++++++
1 file changed, 35 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 03b1d30f5507..2653ab8d5124 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -767,6 +767,41 @@ int test_mmap_munmap(void)
return ret;
}

+int test_pipe(void)
+{
+ const char *const msg = "hello, nolibc";
+ int pipefd[2];
+ char buf[32];
+ pid_t pid;
+ ssize_t len;
+
+ if (pipe(pipefd) == -1)
+ return 1;
+
+ pid = fork();
+
+ switch (pid) {
+ case -1:
+ return 1;
+
+ case 0:
+ close(pipefd[0]);
+ write(pipefd[1], msg, strlen(msg));
+ close(pipefd[1]);
+ exit(EXIT_SUCCESS);
+
+ default:
+ close(pipefd[1]);
+ len = read(pipefd[0], buf, sizeof(buf));
+ close(pipefd[0]);
+ waitpid(pid, NULL, 0);
+
+ if (len != strlen(msg))
+ return 1;
+ return !!memcmp(buf, msg, len);
+ }
+}
+

/* Run syscall tests between IDs <min> and <max>.
* Return 0 on success, non-zero on failure.
--
2.34.1