From: Martin Wilck <[email protected]>

Add tests for the logic of the code added in the previous commit.

Signed-off-by: Martin Wilck <[email protected]>
Reviewed-by: Benjamin Marzinski <[email protected]>
---
 tests/Makefile        |   5 +-
 tests/cmocka-compat.h |   1 +
 tests/wwid-unmangle.c | 375 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 379 insertions(+), 2 deletions(-)
 create mode 100644 tests/wwid-unmangle.c

diff --git a/tests/Makefile b/tests/Makefile
index 23f7d2b..5c7b276 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -10,7 +10,7 @@ LIBDEPS += -L. -L $(mpathutildir) -L$(mpathcmddir) 
-lmultipath -lmpathutil -lmpa
 
 TESTS := uevent parser util dmevents hwtable blacklist unaligned vpd pgpolicy \
         alias directio valid devt mpathvalid strbuf sysfs features cli mapinfo 
runner \
-        shared_ptr $(if $(MEMFD_SUPPORT),gpt)
+        shared_ptr $(if $(MEMFD_SUPPORT),gpt) wwid-unmangle
 HELPERS := test-lib.o test-log.o
 
 .PRECIOUS: $(TESTS:%=%-test)
@@ -71,7 +71,8 @@ mapinfo-test_LIBDEPS = -lpthread -ldevmapper
 runner-test_LIBDEPS = -lpthread
 shared_ptr-test_LIBDEPS = -lpthread
 gpt-test_OBJDEPS := $(kpartxdir)/gpt.o $(kpartxdir)/crc32.o
-
+wwid-unmangle-test_OBJDEPS := $(multipathdir)/discovery.o
+wwid-unmangle-test_LIBDEPS := -lpthread
 
 %.o: %.c
        @echo building $@ because of $?
diff --git a/tests/cmocka-compat.h b/tests/cmocka-compat.h
index 8ec6ed2..58d73ce 100644
--- a/tests/cmocka-compat.h
+++ b/tests/cmocka-compat.h
@@ -13,4 +13,5 @@
 #define expect_int_value(f, x, y) expect_value(f, x, y)
 #define expect_uint_value(f, x, y) expect_value(f, x, y)
 #define assert_int_in_set(x, vals, count) assert_in_set(x, vals, count)
+#define will_return_int(f, x) will_return(f, x)
 #endif
diff --git a/tests/wwid-unmangle.c b/tests/wwid-unmangle.c
new file mode 100644
index 0000000..5188706
--- /dev/null
+++ b/tests/wwid-unmangle.c
@@ -0,0 +1,375 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 SUSE Linux GmbH
+ */
+
+#include <stdbool.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdlib.h>
+#include "cmocka-compat.h"
+#include "structs.h"
+#include "discovery.h"
+#include "util.h"
+
+#include "globals.c"
+
+// #include "globals.c"
+static char wwid_buf[WWID_SIZE];
+
+int __wrap_check_wwids_file(const char *wwid, int write)
+{
+       return mock_type(int);
+}
+
+static void test_wwid_null(void **state)
+{
+       size_t res = maybe_unmangle_wwid(wwid_buf, NULL);
+
+       assert_int_equal(res, 0);
+       assert_string_equal(wwid_buf, "");
+}
+
+static void test_wwid_zero(void **state)
+{
+       size_t res = maybe_unmangle_wwid(wwid_buf, "");
+
+       assert_int_equal(res, 0);
+       assert_string_equal(wwid_buf, "");
+}
+
+/* contains no mangled chars */
+static void test_wwid_no_unmangle_01(void **state)
+{
+       const char *const wwid = "simple";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid character, not unmangled */
+static void test_wwid_no_unmangle_02(void **state)
+{
+       const char *const wwid = "simple wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains unicode character, not unmangled */
+static void test_wwid_no_unmangle_03(void **state)
+{
+       const char *const wwid = "utf8→wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid character, not unmangled */
+static void test_wwid_no_unmangle_04(void **state)
+{
+       const char *const wwid = "simple;wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid character, not unmangled */
+static void test_wwid_no_unmangle_05(void **state)
+{
+       const char *const wwid = "simple\\wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains unicode character, not unmangled */
+static void test_wwid_no_unmangle_06(void **state)
+{
+       const char *const wwid = "utf8→wwid\x20space";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid character, not unmangled */
+static void test_wwid_no_unmangle_07(void **state)
+{
+       const char *const wwid = "invalid\\x20;wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid character, not unmangled */
+static void test_wwid_no_unmangle_08(void **state)
+{
+       const char *const wwid = "invalid,wwid\x20space";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains unicode character, not unmangled */
+static void test_wwid_no_unmangle_09(void **state)
+{
+       const char *const wwid = "utf8\\x20→wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* contains invalid mangled char */
+static void test_wwid_no_unmangle_10(void **state)
+{
+       const char *const wwid = "invalid\\x1wwid";
+       size_t res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* test that WWID_SIZE doesn't overflow */
+static void test_wwid_overlong_01(void **state)
+{
+       char wwid[WWID_SIZE + 2];
+       size_t res;
+       memset(wwid, 'A', sizeof(wwid) - 1);
+
+       wwid[sizeof(wwid) - 1] = '\0';
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+       assert_int_equal(res, strlen(wwid));
+       wwid[WWID_SIZE - 1] = '\0';
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* mangle sequence at the end, fits after mangling */
+static void test_wwid_overlong_02(void **state)
+{
+       char wwid[WWID_SIZE];
+       size_t res;
+       memset(wwid, 'A', sizeof(wwid) - 1);
+
+       wwid[sizeof(wwid) - 1] = '\0';
+       strlcpy(wwid + sizeof(wwid) - 5, "\\x20", 5);
+       memcpy(wwid, "\\x20", 4);
+
+       will_return_int(__wrap_check_wwids_file, 0);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       wwid[3] = ' ';
+       strlcpy(wwid + sizeof(wwid) - 5, " ", 5);
+       assert_int_equal(res, strlen(wwid + 3));
+       assert_string_equal(wwid_buf, wwid + 3);
+}
+
+/* truncated mangle sequence at the end, will no be unmangled */
+static void test_wwid_overlong_03(void **state)
+{
+       char wwid[WWID_SIZE + 5];
+       size_t res;
+       memset(wwid, 'A', sizeof(wwid) - 1);
+
+       wwid[sizeof(wwid) - 1] = '\0';
+       strlcpy(wwid + sizeof(wwid) - 4, "\\x20", 4);
+       memcpy(wwid, "\\x20", 4);
+
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+       assert_int_equal(res, strlen(wwid));
+       wwid[WWID_SIZE - 1] = '\0';
+       assert_string_equal(wwid_buf, wwid);
+}
+
+/* overlong string fits after unmangling */
+static void test_wwid_overlong_04(void **state)
+{
+       char wwid[WWID_SIZE + 3];
+       size_t res;
+       memset(wwid, 'A', sizeof(wwid) - 1);
+
+       wwid[sizeof(wwid) - 1] = '\0';
+       strlcpy(wwid, "\\x20", sizeof(wwid));
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       wwid[3] = ' ';
+       assert_int_equal(res, strlen(wwid + 3));
+       assert_string_equal(wwid_buf, wwid + 3);
+}
+
+static void test_wwid_unmangle_01(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x20spaces";
+       const char *const expected = "wwid with spaces";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_02(void **state)
+{
+       const char *const wwid = "wwid\\x2cwith\\x2ccommas";
+       const char *const expected = "wwid,with,commas";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_03(void **state)
+{
+       const char *const wwid = 
"\\x20\\x20wwid\\x20\\x20\\x20with\\x20spaces\\x20";
+       const char *const expected = "  wwid   with spaces ";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_04(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x5cbackslash";
+       const char *const expected = "wwid with\\backslash";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_05(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x5cx20mangled\\x20space";
+       const char *const expected = "wwid with\\x20mangled space";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_06(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x5cx5cmangled\\x20backslash";
+       const char *const expected = "wwid with\\x5cmangled backslash";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangle_07(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x09unprintable";
+       const char *const expected = "wwid with_unprintable";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 1);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_unmangled_exists(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x20spaces";
+       const char *const expected = "wwid with spaces";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 0);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(expected));
+       assert_string_equal(wwid_buf, expected);
+}
+
+static void test_wwid_orig_exists(void **state)
+{
+       const char *const wwid = "wwid\\x20with\\x20spaces";
+       size_t res;
+
+       will_return_int(__wrap_check_wwids_file, 1);
+       will_return_int(__wrap_check_wwids_file, 0);
+       res = maybe_unmangle_wwid(wwid_buf, wwid);
+
+       assert_int_equal(res, strlen(wwid));
+       assert_string_equal(wwid_buf, wwid);
+}
+
+static int test_uevent_wwid_mangle(void)
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_wwid_null),
+               cmocka_unit_test(test_wwid_zero),
+               cmocka_unit_test(test_wwid_no_unmangle_01),
+               cmocka_unit_test(test_wwid_no_unmangle_02),
+               cmocka_unit_test(test_wwid_no_unmangle_03),
+               cmocka_unit_test(test_wwid_no_unmangle_04),
+               cmocka_unit_test(test_wwid_no_unmangle_05),
+               cmocka_unit_test(test_wwid_no_unmangle_06),
+               cmocka_unit_test(test_wwid_no_unmangle_07),
+               cmocka_unit_test(test_wwid_no_unmangle_08),
+               cmocka_unit_test(test_wwid_no_unmangle_09),
+               cmocka_unit_test(test_wwid_no_unmangle_10),
+               cmocka_unit_test(test_wwid_overlong_01),
+               cmocka_unit_test(test_wwid_overlong_02),
+               cmocka_unit_test(test_wwid_overlong_03),
+               cmocka_unit_test(test_wwid_overlong_04),
+               cmocka_unit_test(test_wwid_unmangle_01),
+               cmocka_unit_test(test_wwid_unmangle_02),
+               cmocka_unit_test(test_wwid_unmangle_03),
+               cmocka_unit_test(test_wwid_unmangle_04),
+               cmocka_unit_test(test_wwid_unmangle_05),
+               cmocka_unit_test(test_wwid_unmangle_06),
+               cmocka_unit_test(test_wwid_unmangle_07),
+               cmocka_unit_test(test_wwid_unmangled_exists),
+               cmocka_unit_test(test_wwid_orig_exists),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
+int main(void)
+{
+       int ret = 0;
+
+       init_test_verbosity(-1);
+       ret += test_uevent_wwid_mangle();
+       return ret;
+}
-- 
2.55.0


Reply via email to