Blame SOURCES/0119-multipath-tests-tests-for-adding-and-removing-featur.patch

1595c1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
1595c1
From: Benjamin Marzinski <bmarzins@redhat.com>
1595c1
Date: Fri, 7 Oct 2022 12:35:39 -0500
1595c1
Subject: [PATCH] multipath tests: tests for adding and removing features
1595c1
1595c1
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
1595c1
Reviewed-by: Martin Wilck <mwilck@suse.com>
1595c1
---
1595c1
 tests/Makefile   |   2 +-
1595c1
 tests/features.c | 318 +++++++++++++++++++++++++++++++++++++++++++++++
1595c1
 2 files changed, 319 insertions(+), 1 deletion(-)
1595c1
 create mode 100644 tests/features.c
1595c1
1595c1
diff --git a/tests/Makefile b/tests/Makefile
1595c1
index 77ff3249..914413b8 100644
1595c1
--- a/tests/Makefile
1595c1
+++ b/tests/Makefile
1595c1
@@ -13,7 +13,7 @@ CFLAGS += $(BIN_CFLAGS) -I$(multipathdir) -I$(mpathcmddir) \
1595c1
 LIBDEPS += -L$(multipathdir) -lmultipath -lcmocka
1595c1
 
1595c1
 TESTS := uevent parser util dmevents hwtable blacklist unaligned vpd pgpolicy \
1595c1
-	 alias directio
1595c1
+	 alias directio features
1595c1
 
1595c1
 .SILENT: $(TESTS:%=%.o)
1595c1
 .PRECIOUS: $(TESTS:%=%-test)
1595c1
diff --git a/tests/features.c b/tests/features.c
1595c1
new file mode 100644
1595c1
index 00000000..1e2e6bff
1595c1
--- /dev/null
1595c1
+++ b/tests/features.c
1595c1
@@ -0,0 +1,318 @@
1595c1
+#include <stddef.h>
1595c1
+#include <stdarg.h>
1595c1
+#include <setjmp.h>
1595c1
+#include <cmocka.h>
1595c1
+
1595c1
+#include "structs.h"
1595c1
+#include "globals.c"
1595c1
+
1595c1
+static void test_af_null_features_ptr(void **state)
1595c1
+{
1595c1
+	assert_int_equal(add_feature(NULL, "test"), 1);
1595c1
+}
1595c1
+
1595c1
+static void af_helper(const char *features_start, const char *addition,
1595c1
+		      const char *features_end, int result)
1595c1
+{
1595c1
+	char *f = NULL, *orig = NULL;
1595c1
+
1595c1
+	if (features_start) {
1595c1
+		f = strdup(features_start);
1595c1
+		assert_non_null(f);
1595c1
+		orig = f;
1595c1
+	}
1595c1
+	assert_int_equal(add_feature(&f, addition), result);
1595c1
+	if (result != 0 || features_end == NULL)
1595c1
+		assert_ptr_equal(orig, f);
1595c1
+	else
1595c1
+		assert_string_equal(f, features_end);
1595c1
+	free(f);
1595c1
+}
1595c1
+
1595c1
+static void test_af_null_addition1(void **state)
1595c1
+{
1595c1
+	af_helper("0", NULL, NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_null_addition2(void **state)
1595c1
+{
1595c1
+	af_helper("1 queue_if_no_path", NULL, NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_empty_addition(void **state)
1595c1
+{
1595c1
+	af_helper("2 pg_init_retries 5", "", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_invalid_addition1(void **state)
1595c1
+{
1595c1
+	af_helper("2 pg_init_retries 5", " ", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_af_invalid_addition2(void **state)
1595c1
+{
1595c1
+	af_helper("2 pg_init_retries 5", "\tbad", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_af_invalid_addition3(void **state)
1595c1
+{
1595c1
+	af_helper("2 pg_init_retries 5", "bad ", NULL, 1);
1595c1
+}
1595c1
+ 
1595c1
+static void test_af_invalid_addition4(void **state)
1595c1
+{
1595c1
+	af_helper("2 pg_init_retries 5", " bad ", NULL, 1);
1595c1
+}
1595c1
+ 
1595c1
+static void test_af_null_features1(void **state)
1595c1
+{
1595c1
+	af_helper(NULL, "test", "1 test", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_null_features2(void **state)
1595c1
+{
1595c1
+	af_helper(NULL, "test\t  more", "2 test\t  more", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_null_features3(void **state)
1595c1
+{
1595c1
+	af_helper(NULL, "test\neven\tmore", "3 test\neven\tmore", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_already_exists1(void **state)
1595c1
+{
1595c1
+	af_helper("4 this is a test", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_already_exists2(void **state)
1595c1
+{
1595c1
+	af_helper("5 contest testy intestine test retest", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_almost_exists(void **state)
1595c1
+{
1595c1
+	af_helper("3 contest testy intestine", "test",
1595c1
+		  "4 contest testy intestine test", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_bad_features1(void **state)
1595c1
+{
1595c1
+	af_helper("bad", "test", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_af_bad_features2(void **state)
1595c1
+{
1595c1
+	af_helper("1bad", "test", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_af_add1(void **state)
1595c1
+{
1595c1
+	af_helper("0", "test", "1 test", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_add2(void **state)
1595c1
+{
1595c1
+	af_helper("0", "this is a test", "4 this is a test", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_add3(void **state)
1595c1
+{
1595c1
+	af_helper("1 features", "more values", "3 features more values", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_af_add4(void **state)
1595c1
+{
1595c1
+	af_helper("2 one\ttwo", "three\t four", "4 one\ttwo three\t four", 0);
1595c1
+}
1595c1
+
1595c1
+static int test_add_features(void)
1595c1
+{
1595c1
+	const struct CMUnitTest tests[] = {
1595c1
+		cmocka_unit_test(test_af_null_features_ptr),
1595c1
+		cmocka_unit_test(test_af_null_addition1),
1595c1
+		cmocka_unit_test(test_af_null_addition2),
1595c1
+		cmocka_unit_test(test_af_empty_addition),
1595c1
+		cmocka_unit_test(test_af_invalid_addition1),
1595c1
+		cmocka_unit_test(test_af_invalid_addition2),
1595c1
+		cmocka_unit_test(test_af_invalid_addition3),
1595c1
+		cmocka_unit_test(test_af_invalid_addition4),
1595c1
+		cmocka_unit_test(test_af_null_features1),
1595c1
+		cmocka_unit_test(test_af_null_features2),
1595c1
+		cmocka_unit_test(test_af_null_features3),
1595c1
+		cmocka_unit_test(test_af_already_exists1),
1595c1
+		cmocka_unit_test(test_af_already_exists2),
1595c1
+		cmocka_unit_test(test_af_almost_exists),
1595c1
+		cmocka_unit_test(test_af_bad_features1),
1595c1
+		cmocka_unit_test(test_af_bad_features2),
1595c1
+		cmocka_unit_test(test_af_add1),
1595c1
+		cmocka_unit_test(test_af_add2),
1595c1
+		cmocka_unit_test(test_af_add3),
1595c1
+		cmocka_unit_test(test_af_add4),
1595c1
+	};
1595c1
+	return cmocka_run_group_tests(tests, NULL, NULL);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_null_features_ptr(void **state)
1595c1
+{
1595c1
+	assert_int_equal(remove_feature(NULL, "test"), 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_null_features(void **state)
1595c1
+{
1595c1
+	char *f = NULL;
1595c1
+	assert_int_equal(remove_feature(&f, "test"), 1);
1595c1
+}
1595c1
+
1595c1
+static void rf_helper(const char *features_start, const char *removal,
1595c1
+		      const char *features_end, int result)
1595c1
+{
1595c1
+	char *f = strdup(features_start);
1595c1
+	char *orig = f;
1595c1
+
1595c1
+	assert_non_null(f);
1595c1
+	assert_int_equal(remove_feature(&f, removal), result);
1595c1
+	if (result != 0 || features_end == NULL)
1595c1
+		assert_ptr_equal(orig, f);
1595c1
+	else
1595c1
+		assert_string_equal(f, features_end);
1595c1
+	free(f);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_null_removal(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", NULL, NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_empty_removal(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", "", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_invalid_removal1(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", " ", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_invalid_removal2(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", " bad", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_invalid_removal3(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", "bad\n", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_invalid_removal4(void **state)
1595c1
+{
1595c1
+	rf_helper("1 feature", "\tbad  \n", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_bad_features1(void **state)
1595c1
+{
1595c1
+	rf_helper("invalid feature test string", "test", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_bad_features2(void **state)
1595c1
+{
1595c1
+	rf_helper("2no space test", "test", NULL, 1);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_missing_removal1(void **state)
1595c1
+{
1595c1
+	rf_helper("0", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_missing_removal2(void **state)
1595c1
+{
1595c1
+	rf_helper("1 detest", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_missing_removal3(void **state)
1595c1
+{
1595c1
+	rf_helper("4 testing one two three", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_missing_removal4(void **state)
1595c1
+{
1595c1
+	rf_helper("1 contestant", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_missing_removal5(void **state)
1595c1
+{
1595c1
+	rf_helper("3 testament protest detestable", "test", NULL, 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove_all_features1(void **state)
1595c1
+{
1595c1
+	rf_helper("1 test", "test", "0", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove_all_features2(void **state)
1595c1
+{
1595c1
+	rf_helper("2 another\t test", "another\t test", "0", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove1(void **state)
1595c1
+{
1595c1
+	rf_helper("2 feature1 feature2", "feature2", "1 feature1", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove2(void **state)
1595c1
+{
1595c1
+	rf_helper("2 feature1 feature2", "feature1", "1 feature2", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove3(void **state)
1595c1
+{
1595c1
+	rf_helper("3 test1 test\ttest2", "test", "2 test1 test2", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove4(void **state)
1595c1
+{
1595c1
+	rf_helper("4 this\t is a test", "is a", "2 this\t test", 0);
1595c1
+}
1595c1
+
1595c1
+static void test_rf_remove5(void **state)
1595c1
+{
1595c1
+	rf_helper("3 one more test", "more test", "1 one", 0);
1595c1
+}
1595c1
+
1595c1
+static int test_remove_features(void)
1595c1
+{
1595c1
+	const struct CMUnitTest tests[] = {
1595c1
+		cmocka_unit_test(test_rf_null_features_ptr),
1595c1
+		cmocka_unit_test(test_rf_null_features),
1595c1
+		cmocka_unit_test(test_rf_null_removal),
1595c1
+		cmocka_unit_test(test_rf_empty_removal),
1595c1
+		cmocka_unit_test(test_rf_invalid_removal1),
1595c1
+		cmocka_unit_test(test_rf_invalid_removal2),
1595c1
+		cmocka_unit_test(test_rf_invalid_removal3),
1595c1
+		cmocka_unit_test(test_rf_invalid_removal4),
1595c1
+		cmocka_unit_test(test_rf_bad_features1),
1595c1
+		cmocka_unit_test(test_rf_bad_features2),
1595c1
+		cmocka_unit_test(test_rf_missing_removal1),
1595c1
+		cmocka_unit_test(test_rf_missing_removal2),
1595c1
+		cmocka_unit_test(test_rf_missing_removal3),
1595c1
+		cmocka_unit_test(test_rf_missing_removal4),
1595c1
+		cmocka_unit_test(test_rf_missing_removal5),
1595c1
+		cmocka_unit_test(test_rf_remove_all_features1),
1595c1
+		cmocka_unit_test(test_rf_remove_all_features2),
1595c1
+		cmocka_unit_test(test_rf_remove1),
1595c1
+		cmocka_unit_test(test_rf_remove2),
1595c1
+		cmocka_unit_test(test_rf_remove3),
1595c1
+		cmocka_unit_test(test_rf_remove4),
1595c1
+		cmocka_unit_test(test_rf_remove5),
1595c1
+	};
1595c1
+	return cmocka_run_group_tests(tests, NULL, NULL);
1595c1
+}
1595c1
+
1595c1
+int main(void)
1595c1
+{
1595c1
+	int ret = 0;
1595c1
+
1595c1
+	ret += test_add_features();
1595c1
+	ret += test_remove_features();
1595c1
+
1595c1
+	return ret;
1595c1
+}