From 80b922b6b6eaa504f98184ac816cc0ede15365bc Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 3 Aug 2019 10:30:39 +0100 Subject: [PATCH 03/11] tests: Add some basic compile tests, inspired by nbdkit. * Compile a program which only includes * Compile a program which only opens and closes the handle. * Compile a program using ANSI C settings. * Test the debug flag can be set and cleared, and the effect of the LIBNBD_DEBUG environment variable. * Test that the export name can be set and read. * Test the package name and version APIs. --- .gitignore | 7 ++ tests/Makefile.am | 135 +++++++++++++++++++++++++++--------- tests/compile-ansi-c.c | 43 ++++++++++++ tests/compile-header-only.c | 27 ++++++++ tests/compile.c | 41 +++++++++++ tests/debug-environment.c | 68 ++++++++++++++++++ tests/debug.c | 70 +++++++++++++++++++ tests/export-name.c | 67 ++++++++++++++++++ tests/version.c | 58 ++++++++++++++++ 9 files changed, 482 insertions(+), 34 deletions(-) create mode 100644 tests/compile-ansi-c.c create mode 100644 tests/compile-header-only.c create mode 100644 tests/compile.c create mode 100644 tests/debug-environment.c create mode 100644 tests/debug.c create mode 100644 tests/export-name.c create mode 100644 tests/version.c diff --git a/.gitignore b/.gitignore index ed1e03e..e69d243 100644 --- a/.gitignore +++ b/.gitignore @@ -109,13 +109,19 @@ Makefile.in /tests/can-not-trim-flag /tests/can-zero-flag /tests/closure-lifetimes +/tests/compile +/tests/compile-ansi-c +/tests/compile-header-only /tests/connect-tcp /tests/connect-tls-certs /tests/connect-tls-psk /tests/connect-unix /tests/connect-uri-tcp /tests/connect-uri-unix +/tests/debug +/tests/debug-environment /tests/errors +/tests/export-name /tests/functions.sh /tests/get-size /tests/is-rotational-flag @@ -123,6 +129,7 @@ Makefile.in /tests/keys.psk /tests/meta-base-allocation /tests/oldstyle +/tests/version /tests/pki/ /tests/read-only-flag /tests/read-write-flag diff --git a/tests/Makefile.am b/tests/Makefile.am index 3b8448e..59318b4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -39,40 +39,9 @@ EXTRA_DIST = \ synch-parallel-tls.sh \ $(NULL) -if HAVE_NBDKIT - check_DATA = - -check_PROGRAMS = \ - errors \ - server-death \ - get-size \ - read-only-flag \ - read-write-flag \ - can-flush-flag \ - can-not-flush-flag \ - can-fua-flag \ - can-not-fua-flag \ - is-rotational-flag \ - is-not-rotational-flag \ - can-trim-flag \ - can-not-trim-flag \ - can-zero-flag \ - can-multi-conn-flag \ - can-not-multi-conn-flag \ - oldstyle \ - connect-unix \ - connect-tcp \ - connect-uri-unix \ - connect-uri-tcp \ - aio-parallel \ - aio-parallel-load \ - synch-parallel \ - meta-base-allocation \ - closure-lifetimes \ - $(NULL) -# can-cache-flag -# can-not-cache-flag +check_PROGRAMS = +TESTS = # Make sure that $srcdir is available to tests. # Enable debug in all tests. @@ -80,7 +49,103 @@ TESTS_ENVIRONMENT = srcdir=$(srcdir) LIBNBD_DEBUG=1 # Use the ./run script so we're always using the local library and tools. LOG_COMPILER = $(top_builddir)/run -TESTS = \ +#---------------------------------------------------------------------- +# The following tests do not need an NBD server. + +check_PROGRAMS += \ + compile-header-only \ + compile \ + compile-ansi-c \ + debug \ + debug-environment \ + version \ + export-name \ + $(NULL) + +TESTS += \ + compile-header-only \ + compile \ + compile-ansi-c \ + debug \ + debug-environment \ + version \ + export-name \ + $(NULL) + +compile_header_only_SOURCES = compile-header-only.c +compile_header_only_CPPFLAGS = -I$(top_srcdir)/include +compile_header_only_CFLAGS = $(WARNINGS_CFLAGS) +compile_header_only_LDADD = $(top_builddir)/lib/libnbd.la + +compile_SOURCES = compile.c +compile_CPPFLAGS = -I$(top_srcdir)/include +compile_CFLAGS = $(WARNINGS_CFLAGS) +compile_LDADD = $(top_builddir)/lib/libnbd.la + +compile_ansi_c_SOURCES = compile-ansi-c.c +compile_ansi_c_CPPFLAGS = \ + -I$(top_srcdir)/include \ + -std=c90 -pedantic +compile_ansi_c_CFLAGS = $(WARNINGS_CFLAGS) +compile_ansi_c_LDADD = $(top_builddir)/lib/libnbd.la + +debug_SOURCES = debug.c +debug_CPPFLAGS = -I$(top_srcdir)/include +debug_CFLAGS = $(WARNINGS_CFLAGS) +debug_LDADD = $(top_builddir)/lib/libnbd.la + +debug_environment_SOURCES = debug-environment.c +debug_environment_CPPFLAGS = -I$(top_srcdir)/include +debug_environment_CFLAGS = $(WARNINGS_CFLAGS) +debug_environment_LDADD = $(top_builddir)/lib/libnbd.la + +version_SOURCES = version.c +version_CPPFLAGS = -I$(top_srcdir)/include +version_CFLAGS = $(WARNINGS_CFLAGS) +version_LDADD = $(top_builddir)/lib/libnbd.la + +export_name_SOURCES = export-name.c +export_name_CPPFLAGS = -I$(top_srcdir)/include +export_name_CFLAGS = $(WARNINGS_CFLAGS) +export_name_LDADD = $(top_builddir)/lib/libnbd.la + +#---------------------------------------------------------------------- +# The following tests require nbdkit as an NBD server to test against. + +if HAVE_NBDKIT + +check_PROGRAMS += \ + errors \ + server-death \ + get-size \ + read-only-flag \ + read-write-flag \ + can-flush-flag \ + can-not-flush-flag \ + can-fua-flag \ + can-not-fua-flag \ + is-rotational-flag \ + is-not-rotational-flag \ + can-trim-flag \ + can-not-trim-flag \ + can-zero-flag \ + can-multi-conn-flag \ + can-not-multi-conn-flag \ + oldstyle \ + connect-unix \ + connect-tcp \ + connect-uri-unix \ + connect-uri-tcp \ + aio-parallel \ + aio-parallel-load \ + synch-parallel \ + meta-base-allocation \ + closure-lifetimes \ + $(NULL) +# can-cache-flag +# can-not-cache-flag + +TESTS += \ errors \ server-death \ get-size \ @@ -286,7 +351,9 @@ closure_lifetimes_CPPFLAGS = -I$(top_srcdir)/include closure_lifetimes_CFLAGS = $(WARNINGS_CFLAGS) closure_lifetimes_LDADD = $(top_builddir)/lib/libnbd.la +#---------------------------------------------------------------------- # Testing TLS support. + if HAVE_GNUTLS if HAVE_CERTTOOL diff --git a/tests/compile-ansi-c.c b/tests/compile-ansi-c.c new file mode 100644 index 0000000..0d48618 --- /dev/null +++ b/tests/compile-ansi-c.c @@ -0,0 +1,43 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 that (only simple) programs can be compiled by an ANSI C + * compiler. + */ + +#include + +#include +#include + +#include + +int +main (int argc, char *argv[]) +{ + struct nbd_handle *nbd; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} diff --git a/tests/compile-header-only.c b/tests/compile-header-only.c new file mode 100644 index 0000000..52b93d7 --- /dev/null +++ b/tests/compile-header-only.c @@ -0,0 +1,27 @@ +/* NBD client library in userspace + * Copyright (C) 2019 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 + */ + +/* Check that does not require any other headers. */ + +#include + +int +main (int argc, char *argv[]) +{ + return 0; +} diff --git a/tests/compile.c b/tests/compile.c new file mode 100644 index 0000000..d7ef98c --- /dev/null +++ b/tests/compile.c @@ -0,0 +1,41 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 + */ + +/* Compile, open and close a handle. */ + +#include + +#include +#include + +#include + +int +main (int argc, char *argv[]) +{ + struct nbd_handle *nbd; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} diff --git a/tests/debug-environment.c b/tests/debug-environment.c new file mode 100644 index 0000000..e11708a --- /dev/null +++ b/tests/debug-environment.c @@ -0,0 +1,68 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 + */ + +/* Check that LIBNBD_DEBUG=0|1 affects the debug flag. */ + +#include + +#include +#include +#include + +#include + +static int +get_debug_flag (void) +{ + struct nbd_handle *nbd; + int r; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + r = nbd_get_debug (nbd); + if (r == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + nbd_close (nbd); + + return r; +} + +int +main (int argc, char *argv[]) +{ + setenv ("LIBNBD_DEBUG", "1", 1); + assert (get_debug_flag () == 1); + + setenv ("LIBNBD_DEBUG", "0", 1); + assert (get_debug_flag () == 0); + + setenv ("LIBNBD_DEBUG", "", 1); + assert (get_debug_flag () == 0); + + unsetenv ("LIBNBD_DEBUG"); + assert (get_debug_flag () == 0); + + exit (EXIT_SUCCESS); +} diff --git a/tests/debug.c b/tests/debug.c new file mode 100644 index 0000000..af2e80f --- /dev/null +++ b/tests/debug.c @@ -0,0 +1,70 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 setting and reading the debug flag. */ + +#include + +#include +#include +#include +#include + +#include + +int +main (int argc, char *argv[]) +{ + struct nbd_handle *nbd; + int r; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + r = nbd_set_debug (nbd, true); + if (r == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + r = nbd_get_debug (nbd); + if (r == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + assert (r == 1); + + r = nbd_set_debug (nbd, false); + if (r == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + r = nbd_get_debug (nbd); + if (r == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + assert (r == 0); + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} diff --git a/tests/export-name.c b/tests/export-name.c new file mode 100644 index 0000000..7cbc3cd --- /dev/null +++ b/tests/export-name.c @@ -0,0 +1,67 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 setting and reading the export name. */ + +#include + +#include +#include +#include +#include + +#include + +static void +check_export_name (struct nbd_handle *nbd, const char *str) +{ + char *r; + + if (nbd_set_export_name (nbd, str) == -1) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + r = nbd_get_export_name (nbd); + if (r == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + assert (strcmp (str, r) == 0); + free (r); +} + +int +main (int argc, char *argv[]) +{ + struct nbd_handle *nbd; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + check_export_name (nbd, "/"); + check_export_name (nbd, "123"); + check_export_name (nbd, ""); + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} diff --git a/tests/version.c b/tests/version.c new file mode 100644 index 0000000..1ba3f58 --- /dev/null +++ b/tests/version.c @@ -0,0 +1,58 @@ +/* NBD client library in userspace + * Copyright (C) 2013-2019 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 + */ + +/* Read the package and version from the handle. */ + +#include + +#include +#include +#include +#include + +#include + +int +main (int argc, char *argv[]) +{ + struct nbd_handle *nbd; + const char *s; + + nbd = nbd_create (); + if (nbd == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + + s = nbd_get_package_name (nbd); + if (s == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + assert (strcmp (s, PACKAGE_NAME) == 0); + + s = nbd_get_version (nbd); + if (s == NULL) { + fprintf (stderr, "%s\n", nbd_get_error ()); + exit (EXIT_FAILURE); + } + assert (strcmp (s, PACKAGE_VERSION) == 0); + + nbd_close (nbd); + exit (EXIT_SUCCESS); +} -- 2.22.0