[Libguestfs] [libnbd PATCH v3 02/18] tests: Add coverage of unusual nbd_connect_command argv

Eric Blake eblake at redhat.com
Mon Sep 26 22:05:44 UTC 2022


Catch the library crashes patched in the previous commit.
---
 tests/Makefile.am           |  5 +++
 tests/errors-connect-null.c | 88 +++++++++++++++++++++++++++++++++++++
 .gitignore                  |  1 +
 3 files changed, 94 insertions(+)
 create mode 100644 tests/errors-connect-null.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7fc7dec3..f6a2c110 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -168,6 +168,7 @@ check_PROGRAMS += \
 	errors-not-connected \
 	errors-name-too-long \
 	errors-poll-no-fd \
+	errors-connect-null \
 	errors-connect-twice \
 	errors-not-negotiating \
 	errors-notify-not-blocked \
@@ -233,6 +234,7 @@ TESTS += \
 	errors-not-connected \
 	errors-name-too-long \
 	errors-poll-no-fd \
+	errors-connect-null \
 	errors-connect-twice \
 	errors-not-negotiating \
 	errors-notify-not-blocked \
@@ -309,6 +311,9 @@ errors_name_too_long_LDADD = $(top_builddir)/lib/libnbd.la
 errors_poll_no_fd_SOURCES = errors-poll-no-fd.c
 errors_poll_no_fd_LDADD = $(top_builddir)/lib/libnbd.la

+errors_connect_null_SOURCES = errors-connect-null.c
+errors_connect_null_LDADD = $(top_builddir)/lib/libnbd.la
+
 errors_connect_twice_SOURCES = errors-connect-twice.c
 errors_connect_twice_LDADD = $(top_builddir)/lib/libnbd.la

diff --git a/tests/errors-connect-null.c b/tests/errors-connect-null.c
new file mode 100644
index 00000000..9cda242e
--- /dev/null
+++ b/tests/errors-connect-null.c
@@ -0,0 +1,88 @@
+/* NBD client library in userspace
+ * Copyright (C) 2013-2022 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
+ */
+
+/* Deliberately provoke some errors and check the error messages from
+ * nbd_get_error etc look reasonable.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <libnbd.h>
+
+static char *progname;
+
+static void
+check (int experr, const char *prefix)
+{
+  const char *msg = nbd_get_error ();
+  int errnum = nbd_get_errno ();
+
+  fprintf (stderr, "error: \"%s\"\n", msg);
+  fprintf (stderr, "errno: %d (%s)\n", errnum, strerror (errnum));
+  if (strncmp (msg, prefix, strlen (prefix)) != 0) {
+    fprintf (stderr, "%s: test failed: missing context prefix: %s\n",
+             progname, msg);
+    exit (EXIT_FAILURE);
+  }
+  if (errnum != experr) {
+    fprintf (stderr, "%s: test failed: "
+             "expected errno = %d (%s), but got %d\n",
+             progname, experr, strerror (experr), errnum);
+    exit (EXIT_FAILURE);
+  }
+}
+
+int
+main (int argc, char *argv[])
+{
+  struct nbd_handle *nbd;
+  const char *cmd[] = { NULL };
+
+  progname = argv[0];
+
+  nbd = nbd_create ();
+  if (nbd == NULL) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
+
+  /* Connect to an invalid command. */
+  if (nbd_connect_command (nbd, NULL) != -1) {
+    fprintf (stderr, "%s: test failed: "
+             "nbd_connect_command did not reject NULL argv\n",
+             argv[0]);
+    exit (EXIT_FAILURE);
+  }
+  check (EFAULT, "nbd_connect_command: ");
+
+  if (nbd_connect_command (nbd, (char **) cmd) != -1) {
+    fprintf (stderr, "%s: test failed: "
+             "nbd_connect_command did not reject empty argv\n",
+             argv[0]);
+    exit (EXIT_FAILURE);
+  }
+  check (EINVAL, "nbd_connect_command: ");
+
+  nbd_close (nbd);
+  exit (EXIT_SUCCESS);
+}
diff --git a/.gitignore b/.gitignore
index 071393fb..bc49860d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -204,6 +204,7 @@ Makefile.in
 /tests/errors-client-unaligned
 /tests/errors-client-unknown-flags
 /tests/errors-client-zerosize
+/tests/errors-connect-null
 /tests/errors-connect-twice
 /tests/errors-enum
 /tests/errors-multiple-disconnects
-- 
2.37.3



More information about the Libguestfs mailing list