Everything in tests/ is useful for testing our public interface:
nbdkit as a whole, as well as plugins and filters. But we are
lacking some unit testing of internal functionality, where it
is better to build a standalone binary rather than a full-blown
plugin. Unit testing belongs best in src/, in part because
cross-directory linking (which would be required if the unit
test lived in tests/) makes automake unhappy.
The first such unit test is of the nbdkit_parse_size() helper
function that is exposed to both plugins and filters. The test
intentionally has several lines filtered out due to current
weaknesses in the code, which will be fixed later.
Having tests in two different directories means that you now have
to use 'make check -C tests/ TESTS=...' rather than just
'make check TESTS=...' when limiting (re-)testing to just a single
test, but that's not too bad (projects that use gnulib tests are
already familiar with that restriction).
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
src/test-utils.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
.gitignore | 1 +
src/Makefile.am | 15 ++++++-
3 files changed, 143 insertions(+), 1 deletion(-)
create mode 100644 src/test-utils.c
diff --git a/src/test-utils.c b/src/test-utils.c
new file mode 100644
index 0000000..e26a6f3
--- /dev/null
+++ b/src/test-utils.c
@@ -0,0 +1,128 @@
+/* nbdkit
+ * Copyright (C) 2018 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdbool.h>
+
+#include "internal.h"
+
+static bool error_flagged;
+
+/* Stub for linking against minimal source files, and for proving that
+ * an error message is issued when expected. */
+void
+nbdkit_error (const char *fs, ...)
+{
+ error_flagged = true;
+}
+
+static bool
+test_nbdkit_parse_size (void)
+{
+ bool pass = true;
+ struct pair {
+ const char *str;
+ int64_t res;
+ } pairs[] = {
+ /* Bogus strings */
+ { "", -1 },
+ { "0x0", -1 },
+ { "garbage", -1 },
+ /* { "0garbage", -1 }, */
+ /* { "-1", -1 }, */
+ /* { "-2", -1 }, */
+ /* { "9223372036854775808", -1 }, */
+ /* { "-9223372036854775808", -1 }, */
+ /* { "8E", -1 }, */
+ /* { "8192P", -1 }, */
+ /* { "999999999999999999999999", -1 }, */
+
+ /* Strings we may want to support in the future */
+ { "M", -1 },
+ /* { "1MB", -1 }, */
+ /* { "1MiB", -1 }, */
+ { "1.5M", -1 },
+
+ /* Valid strings */
+ { "0", 0 },
+ { " 08", 8 },
+ { "9223372036854775807", INT64_MAX },
+ { "1s", 512 },
+ { "2S", 1024 },
+ { "1b", 1 },
+ { "1B", 1 },
+ { "1k", 1024 },
+ { "1K", 1024 },
+ { "1m", 1024 * 1024 },
+ { "1M", 1024 * 1024 },
+ { "1g", 1024 * 1024 * 1024 },
+ { "1G", 1024 * 1024 * 1024 },
+ { "1t", 1024LL * 1024 * 1024 * 1024 },
+ { "1T", 1024LL * 1024 * 1024 * 1024 },
+ { "1p", 1024LL * 1024 * 1024 * 1024 * 1024 },
+ { "1P", 1024LL * 1024 * 1024 * 1024 * 1024 },
+ { "1e", 1024LL * 1024 * 1024 * 1024 * 1024 * 1024 },
+ { "1E", 1024LL * 1024 * 1024 * 1024 * 1024 * 1024 },
+ };
+
+ for (size_t i = 0; i < sizeof pairs / sizeof pairs[0]; i++) {
+ int64_t r;
+
+ error_flagged = false;
+ r = nbdkit_parse_size (pairs[i].str);
+ if (r != pairs[i].res) {
+ fprintf (stderr,
+ "Wrong parse for %s, got %" PRId64 ", expected %"
PRId64 "\n",
+ pairs[i].str, r, pairs[i].res);
+ pass = false;
+ }
+ if ((r == -1) != error_flagged) {
+ fprintf (stderr, "Wrong error message handling for %s\n", pairs[i].str);
+ pass = false;
+ }
+ }
+
+ return pass;
+}
+
+int
+main (int argc, char *argv[])
+{
+ bool pass = true;
+ pass &= test_nbdkit_parse_size ();
+ /* XXX tests nbdkit_absolute_path, nbdkit_read_password */
+ return pass ? 0 : 1;
+}
diff --git a/.gitignore b/.gitignore
index 58a0fdd..3b7365f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,6 +42,7 @@ Makefile.in
/plugins/tar/nbdkit-tar-plugin
/src/nbdkit
/src/nbdkit.pc
+/src/test-utils
/stamp-h1
/tests/disk
/tests/disk.gz
diff --git a/src/Makefile.am b/src/Makefile.am
index ae16fde..d11a2b3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
# nbdkit
-# Copyright (C) 2013 Red Hat Inc.
+# Copyright (C) 2013-2018 Red Hat Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@@ -78,3 +78,16 @@ CLEANFILES = *~
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = nbdkit.pc
+
+# Unit testing
+
+TESTS = test-utils
+
+check_PROGRAMS = test-utils
+
+test_utils_SOURCES = \
+ test-utils.c \
+ utils.c \
+ cleanup.c
+test_utils_CPPFLAGS = -I$(top_srcdir)/include
+test_utils_CFLAGS = $(WARNINGS_CFLAGS)
--
2.14.3