From 908d1eef0b6e35421bc9bee34603c73e97d0fc7c Mon Sep 17 00:00:00 2001
From: Mariusz Zaborski <oshogbo@FreeBSD.org>
Date: Mon, 15 Apr 2019 03:32:01 +0000
Subject: [PATCH] libnv: extend the tests

Add cases for sending file descriptors.

Submitted by:	Mindaugas Rasiukevicius <rmind@noxt.eu>
MFC after:	2 weeks
---
 lib/libnv/tests/nvlist_send_recv_test.c | 31 +++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/lib/libnv/tests/nvlist_send_recv_test.c b/lib/libnv/tests/nvlist_send_recv_test.c
index fbc918102b50..449a6df1da0d 100644
--- a/lib/libnv/tests/nvlist_send_recv_test.c
+++ b/lib/libnv/tests/nvlist_send_recv_test.c
@@ -34,6 +34,7 @@
 #include <sys/wait.h>
 #include <sys/nv.h>
 
+#include <stdlib.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -58,6 +59,7 @@ child(int sock)
 {
 	nvlist_t *nvl;
 	nvlist_t *empty;
+	int pfd[2];
 
 	nvl = nvlist_create(0);
 	empty = nvlist_create(0);
@@ -73,7 +75,16 @@ child(int sock)
 	nvlist_add_string(nvl, "nvlist/string/", "");
 	nvlist_add_string(nvl, "nvlist/string/x", "x");
 	nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
+
 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
+	if (pipe(pfd) == -1)
+		err(EXIT_FAILURE, "pipe");
+	if (write(pfd[1], "test", 4) != 4)
+		err(EXIT_FAILURE, "write");
+	close(pfd[1]);
+	nvlist_add_descriptor(nvl, "nvlist/descriptor/pipe_rd", pfd[0]);
+	close(pfd[0]);
+
 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
 	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
 	nvlist_move_nvlist(nvl, "nvlist/nvlist/empty", empty);
@@ -91,8 +102,9 @@ parent(int sock)
 	const nvlist_t *cnvl, *empty;
 	const char *name, *cname;
 	void *cookie, *ccookie;
-	int type, ctype;
+	int type, ctype, fd;
 	size_t size;
+	char buf[4];
 
 	nvl = nvlist_recv(sock, 0);
 	CHECK(nvlist_error(nvl) == 0);
@@ -173,6 +185,15 @@ parent(int sock)
 	CHECK(strcmp(name, "nvlist/descriptor/STDERR_FILENO") == 0);
 	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, name)));
 
+	name = nvlist_next(nvl, &type, &cookie);
+	CHECK(name != NULL);
+	CHECK(type == NV_TYPE_DESCRIPTOR);
+	CHECK(strcmp(name, "nvlist/descriptor/pipe_rd") == 0);
+	fd = nvlist_get_descriptor(nvl, name);
+	CHECK(fd_is_valid(fd));
+	CHECK(read(fd, buf, sizeof(buf)) == 4);
+	CHECK(strncmp(buf, "test", sizeof(buf)) == 0);
+
 	name = nvlist_next(nvl, &type, &cookie);
 	CHECK(name != NULL);
 	CHECK(type == NV_TYPE_BINARY);
@@ -276,6 +297,12 @@ parent(int sock)
 	CHECK(strcmp(cname, "nvlist/descriptor/STDERR_FILENO") == 0);
 	CHECK(fd_is_valid(nvlist_get_descriptor(cnvl, cname)));
 
+	cname = nvlist_next(cnvl, &ctype, &ccookie);
+	CHECK(cname != NULL);
+	CHECK(ctype == NV_TYPE_DESCRIPTOR);
+	CHECK(strcmp(cname, "nvlist/descriptor/pipe_rd") == 0);
+	CHECK(fd_is_valid(nvlist_get_descriptor(cnvl, cname)));
+
 	cname = nvlist_next(cnvl, &ctype, &ccookie);
 	CHECK(cname != NULL);
 	CHECK(ctype == NV_TYPE_BINARY);
@@ -359,7 +386,7 @@ int
 main(void)
 {
 
-	printf("1..136\n");
+	printf("1..146\n");
 	fflush(stdout);
 
 	send_nvlist();