From: "Richard W.M. Jones" <rjones(a)redhat.com>
Partially port nbdkit commit 58e7b0e104d5 ("common/include: Add unit
tests.", 2018-12-29) to libnbd. In libnbd, we already use the headers
"byte-swapping.h", "isaligned.h" and "iszero.h", but we
don't have the
matching test cases for them.
Porting notes:
- nbdkit commit 58e7b0e104d5 also introduces "test-current-dir-name". In
libnbd, we don't have "get-current-dir-name.h" for the time being, so
the test case is not ported either.
- In libnbd, we have "ispowerof2" (both header and test case) from
earlier, so porting that part of nbdkit commit 58e7b0e104d5 is not
necessary.
- In the three test programs being ported, update the (C) notice. Extend
the year range to 2023, plus remove the "All rights reserved" line (to
be consistent with the other test cases in libnbd).
Signed-off-by: Laszlo Ersek <lersek(a)redhat.com>
(cherry picked from nbdkit commit 58e7b0e104d57d767d11aacab7d7ab82f0d4a98b)
---
common/include/Makefile.am | 15 ++++
common/include/test-byte-swapping.c | 89 ++++++++++++++++++++
common/include/test-isaligned.c | 61 ++++++++++++++
common/include/test-iszero.c | 65 ++++++++++++++
.gitignore | 3 +
5 files changed, 233 insertions(+)
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index 8d6b2a88be13..1d7720d9b353 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -36,8 +36,11 @@ EXTRA_DIST = \
TESTS = \
test-array-size \
+ test-byte-swapping \
test-checked-overflow \
+ test-isaligned \
test-ispowerof2 \
+ test-iszero \
$(NULL)
check_PROGRAMS = $(TESTS)
@@ -45,10 +48,22 @@ test_array_size_SOURCES = test-array-size.c array-size.h
test_array_size_CPPFLAGS = -I$(srcdir)
test_array_size_CFLAGS = $(WARNINGS_CFLAGS)
+test_byte_swapping_SOURCES = test-byte-swapping.c byte-swapping.h
+test_byte_swapping_CPPFLAGS = -I$(srcdir)
+test_byte_swapping_CFLAGS = $(WARNINGS_CFLAGS)
+
test_checked_overflow_SOURCES = test-checked-overflow.c checked-overflow.h
test_checked_overflow_CPPFLAGS = -I$(srcdir)
test_checked_overflow_CFLAGS = $(WARNINGS_CFLAGS)
+test_isaligned_SOURCES = test-isaligned.c isaligned.h
+test_isaligned_CPPFLAGS = -I$(srcdir)
+test_isaligned_CFLAGS = $(WARNINGS_CFLAGS)
+
test_ispowerof2_SOURCES = test-ispowerof2.c ispowerof2.h
test_ispowerof2_CPPFLAGS = -I$(srcdir)
test_ispowerof2_CFLAGS = $(WARNINGS_CFLAGS)
+
+test_iszero_SOURCES = test-iszero.c iszero.h
+test_iszero_CPPFLAGS = -I$(srcdir)
+test_iszero_CFLAGS = $(WARNINGS_CFLAGS)
diff --git a/common/include/test-byte-swapping.c b/common/include/test-byte-swapping.c
new file mode 100644
index 000000000000..fcb83c574bfc
--- /dev/null
+++ b/common/include/test-byte-swapping.c
@@ -0,0 +1,89 @@
+/* nbdkit
+ * Copyright (C) 2018-2023 Red Hat Inc.
+ *
+ * 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 <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "byte-swapping.h"
+
+/* Little-endian test strings. */
+static uint8_t le16[] = { 0x34, 0x12 };
+static uint8_t le32[] = { 0x78, 0x56, 0x34, 0x12 };
+static uint8_t le64[] = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 };
+
+/* Big-endian test strings. */
+static uint8_t be16[] = { 0x12, 0x34 };
+static uint8_t be32[] = { 0x12, 0x34, 0x56, 0x78 };
+static uint8_t be64[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
+
+int
+main (void)
+{
+ uint16_t i16;
+ uint32_t i32;
+ uint64_t i64;
+
+ memcpy (&i16, le16, 2);
+ assert (le16toh (i16) == 0x1234);
+ memcpy (&i32, le32, 4);
+ assert (le32toh (i32) == 0x12345678);
+ memcpy (&i64, le64, 8);
+ assert (le64toh (i64) == 0x123456789abcdef0);
+
+ memcpy (&i16, be16, 2);
+ assert (be16toh (i16) == 0x1234);
+ memcpy (&i32, be32, 4);
+ assert (be32toh (i32) == 0x12345678);
+ memcpy (&i64, be64, 8);
+ assert (be64toh (i64) == 0x123456789abcdef0);
+
+ i16 = htole16 (0x1234);
+ assert (memcmp (&i16, le16, 2) == 0);
+ i32 = htole32 (0x12345678);
+ assert (memcmp (&i32, le32, 4) == 0);
+ i64 = htole64 (0x123456789abcdef0);
+ assert (memcmp (&i64, le64, 8) == 0);
+
+ i16 = htobe16 (0x1234);
+ assert (memcmp (&i16, be16, 2) == 0);
+ i32 = htobe32 (0x12345678);
+ assert (memcmp (&i32, be32, 4) == 0);
+ i64 = htobe64 (0x123456789abcdef0);
+ assert (memcmp (&i64, be64, 8) == 0);
+
+ exit (EXIT_SUCCESS);
+}
diff --git a/common/include/test-isaligned.c b/common/include/test-isaligned.c
new file mode 100644
index 000000000000..e3709a8f05bd
--- /dev/null
+++ b/common/include/test-isaligned.c
@@ -0,0 +1,61 @@
+/* nbdkit
+ * Copyright (C) 2018-2023 Red Hat Inc.
+ *
+ * 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 <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "isaligned.h"
+
+int
+main (void)
+{
+ uint64_t i;
+
+ for (i = 1; i <= 0x80000000; i <<= 1) {
+ assert (IS_ALIGNED (0, i));
+ assert (IS_ALIGNED (i, i));
+ assert (IS_ALIGNED (i*2, i));
+ assert (IS_ALIGNED (i*3, i));
+ assert (IS_ALIGNED (i*4, i));
+ if (i > 1) {
+ assert (! IS_ALIGNED (i-1, i));
+ assert (! IS_ALIGNED (i/2, i));
+ }
+ }
+
+ exit (EXIT_SUCCESS);
+}
diff --git a/common/include/test-iszero.c b/common/include/test-iszero.c
new file mode 100644
index 000000000000..6c2836678155
--- /dev/null
+++ b/common/include/test-iszero.c
@@ -0,0 +1,65 @@
+/* nbdkit
+ * Copyright (C) 2018-2023 Red Hat Inc.
+ *
+ * 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 <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "iszero.h"
+
+int
+main (void)
+{
+ char *buf;
+ size_t i, j;
+
+ buf = malloc (256);
+ if (buf == NULL) {
+ perror ("malloc");
+ exit (EXIT_FAILURE);
+ }
+ memset (buf, 0, 256);
+
+ for (j = 0; j <= 16; ++j) {
+ for (i = 0; i <= 16; ++i)
+ assert (is_zero (&buf[j], i));
+ for (i = 0; i <= 16; ++i)
+ assert (is_zero (&buf[j], 256-j-i));
+ }
+
+ free (buf);
+ exit (EXIT_SUCCESS);
+}
diff --git a/.gitignore b/.gitignore
index f42737131d8c..1225917034f3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,8 +34,11 @@ Makefile.in
/bash-completion/nbdinfo
/bash-completion/nbdublk
/common/include/test-array-size
+/common/include/test-byte-swapping
/common/include/test-checked-overflow
+/common/include/test-isaligned
/common/include/test-ispowerof2
+/common/include/test-iszero
/common/utils/test-human-size
/common/utils/test-vector
/compile