[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH libnbd v2 3/5] tests: Add a new test for nbd_aio_connect.



This was not tested before.
---
 .gitignore          |   1 +
 tests/Makefile.am   |   7 +++
 tests/aio-connect.c | 106 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)

diff --git a/.gitignore b/.gitignore
index ffe6261..29ff60b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -151,6 +151,7 @@ Makefile.in
 /sh/nbdsh.1
 /stamp-h1
 /test-driver
+/tests/aio-connect
 /tests/aio-parallel
 /tests/aio-parallel-load
 /tests/aio-parallel-load-tls
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 1fad236..396fd7e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -179,6 +179,7 @@ check_PROGRAMS += \
 	opt-list-meta \
 	connect-unix \
 	connect-tcp \
+	aio-connect \
 	aio-parallel \
 	aio-parallel-load \
 	synch-parallel \
@@ -219,6 +220,7 @@ TESTS += \
 	opt-list-meta \
 	connect-unix \
 	connect-tcp \
+	aio-connect \
 	aio-parallel.sh \
 	aio-parallel-load.sh \
 	synch-parallel.sh \
@@ -435,6 +437,11 @@ connect_tcp_CPPFLAGS = -I$(top_srcdir)/include
 connect_tcp_CFLAGS = $(WARNINGS_CFLAGS)
 connect_tcp_LDADD = $(top_builddir)/lib/libnbd.la
 
+aio_connect_SOURCES = aio-connect.c
+aio_connect_CPPFLAGS = -I$(top_srcdir)/include
+aio_connect_CFLAGS = $(WARNINGS_CFLAGS)
+aio_connect_LDADD = $(top_builddir)/lib/libnbd.la
+
 aio_parallel_SOURCES = aio-parallel.c
 aio_parallel_CPPFLAGS = \
 	-I$(top_srcdir)/include \
diff --git a/tests/aio-connect.c b/tests/aio-connect.c
new file mode 100644
index 0000000..50e9055
--- /dev/null
+++ b/tests/aio-connect.c
@@ -0,0 +1,106 @@
+/* NBD client library in userspace
+ * Copyright (C) 2013-2021 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/* Test connecting to an IPv4 TCP port using nbd_aio_connect. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <libnbd.h>
+
+#define PIDFILE "aio-connect.pid"
+
+int
+main (int argc, char *argv[])
+{
+  struct nbd_handle *nbd;
+  int port;
+  char port_str[16];
+  pid_t pid;
+  size_t i;
+  struct sockaddr_in addr;
+
+  unlink (PIDFILE);
+
+  /* Pick a port at random, hope it's free. */
+  srand (time (NULL) + getpid ());
+  port = 32768 + (rand () & 32767);
+
+  snprintf (port_str, sizeof port_str, "%d", port);
+
+  pid = fork ();
+  if (pid == -1) {
+    perror ("fork");
+    exit (EXIT_FAILURE);
+  }
+  if (pid == 0) {
+    execlp ("nbdkit",
+            "nbdkit", "-f", "-p", port_str, "-P", PIDFILE,
+            "--exit-with-parent", "null", NULL);
+    perror ("nbdkit");
+    _exit (EXIT_FAILURE);
+  }
+
+  /* Wait for nbdkit to start listening. */
+  for (i = 0; i < 60; ++i) {
+    if (access (PIDFILE, F_OK) == 0)
+      break;
+    sleep (1);
+  }
+  unlink (PIDFILE);
+
+  nbd = nbd_create ();
+  if (nbd == NULL) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
+
+  addr.sin_family = AF_INET;
+  addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+  addr.sin_port = htons (port);
+
+  if (nbd_aio_connect (nbd, (struct sockaddr *) &addr, sizeof addr) == -1) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
+
+  /* Wait until we have connected. */
+  while (!nbd_aio_is_ready (nbd)) {
+    if (nbd_poll (nbd, -1) == -1) {
+      fprintf (stderr, "%s\n", nbd_get_error ());
+      exit (EXIT_FAILURE);
+    }
+  }
+
+  if (nbd_shutdown (nbd, 0) == -1) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
+
+  nbd_close (nbd);
+  exit (EXIT_SUCCESS);
+}
-- 
2.29.0.rc2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]