aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-18 19:03:08 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-18 19:03:08 +0100
commitd1a75019ef48ce1b6993bf634e6a321bbe9a2f2e (patch)
tree942d500a4ae3afc18ab790ef05658bce6f4c15f5
parente38fc08b2fc253a8e19450cbe3f4e8dcba654da9 (diff)
Add a test case for xxhash32
Technically the code was imported by a third party library, but some modifications have been made. This commit adds a simple test case with some test vectors and expected results that have to match. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--tests/Makemodule.am7
-rw-r--r--tests/xxhash.c68
2 files changed, 74 insertions, 1 deletions
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
index 8016c83..0d0f26b 100644
--- a/tests/Makemodule.am
+++ b/tests/Makemodule.am
@@ -10,11 +10,16 @@ test_rbtree_SOURCES = tests/rbtree.c
test_rbtree_LDADD = libutil.a libcompat.a
test_rbtree_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/sqfs
+test_xxhash_SOURCES = tests/xxhash.c
+test_xxhash_LDADD = libutil.a libcompat.a
+test_xxhash_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/lib/sqfs
+
test_abi_SOURCES = tests/abi.c
test_abi_LDADD = libsquashfs.la
check_PROGRAMS += test_canonicalize_name test_str_table test_abi test_rbtree
-TESTS += test_canonicalize_name test_str_table test_abi test_rbtree
+check_PROGRAMS += test_xxhash
+TESTS += test_canonicalize_name test_str_table test_abi test_rbtree test_xxhash
if BUILD_TOOLS
test_mknode_simple_SOURCES = tests/mknode_simple.c
diff --git a/tests/xxhash.c b/tests/xxhash.c
new file mode 100644
index 0000000..f586481
--- /dev/null
+++ b/tests/xxhash.c
@@ -0,0 +1,68 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * xxhash.c
+ *
+ * Copyright (C) 2020 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "util.h"
+
+static const struct {
+ const char *plaintext;
+ size_t psize;
+ sqfs_u32 digest;
+} test_vectors[] = {
+ {
+ .plaintext = "\x9e",
+ .psize = 1,
+ .digest = 0xB85CBEE5,
+ },
+ {
+ .plaintext = "\x9e\xff\x1f\x4b\x5e\x53\x2f\xdd"
+ "\xb5\x54\x4d\x2a\x95\x2b",
+ .psize = 14,
+ .digest = 0xE5AA0AB4,
+ },
+ {
+ .plaintext = "\x9e\xff\x1f\x4b\x5e\x53\x2f\xdd"
+ "\xb5\x54\x4d\x2a\x95\x2b\x57\xae"
+ "\x5d\xba\x74\xe9\xd3\xa6\x4c\x98"
+ "\x30\x60\xc0\x80\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00",
+ .psize = 101,
+ .digest = 0x018F52BC,
+ },
+};
+
+int main(void)
+{
+ sqfs_u32 hash;
+ size_t i;
+
+ for (i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); ++i) {
+ hash = xxh32(test_vectors[i].plaintext, test_vectors[i].psize);
+
+ if (hash != test_vectors[i].digest) {
+ fprintf(stderr, "Test case " PRI_SZ " failed!\n", i);
+ fprintf(stderr, "Expected result: 0x%08X\n",
+ test_vectors[i].digest);
+ fprintf(stderr, "Actual result: 0x%08X\n", hash);
+ return EXIT_FAILURE;
+ }
+ }
+
+ return EXIT_SUCCESS;
+}