kunit: mock: added struct param matcher

Added parameter matcher builder for matching struct values.

Change-Id: I6aa4ca9706528b30c1cf23fbf0a6c66ac6245d58
Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
diff --git a/include/test/mock.h b/include/test/mock.h
index e0090142..060cf83 100644
--- a/include/test/mock.h
+++ b/include/test/mock.h
@@ -720,6 +720,13 @@
 				 size_t size);
 struct mock_param_matcher *streq(struct test *test, const char *str);
 
+struct mock_param_matcher *str_contains(struct test *test, const char *needle);
+
+/* Matches var-arg arguments. */
+struct mock_param_matcher *va_format_cmp(struct test *test,
+					 struct mock_param_matcher *fmt_matcher,
+					 struct mock_param_matcher *va_matcher);
+
 struct mock_action *u8_return(struct test *test, u8 ret);
 struct mock_action *u16_return(struct test *test, u16 ret);
 struct mock_action *u32_return(struct test *test, u32 ret);
@@ -737,4 +744,52 @@
 struct mock_action *ulonglong_return(struct test *test, unsigned long long ret);
 struct mock_action *ptr_return(struct test *test, void *ret);
 
+/**
+ * struct mock_struct_matcher_entry - composed with other &struct
+ *                                    mock_struct_matcher_entry to make a
+ *                                    &struct struct_matcher
+ * @member_offset: offset of this member
+ * @matcher: matcher for this particular member
+ *
+ * This is used for struct_cmp() matchers.
+ */
+struct mock_struct_matcher_entry {
+	size_t member_offset;
+	struct mock_param_matcher *matcher;
+};
+
+static inline void init_mock_struct_matcher_entry_internal(
+		struct mock_struct_matcher_entry *entry,
+		size_t offset,
+		struct mock_param_matcher *matcher)
+{
+	entry->member_offset = offset;
+	entry->matcher = matcher;
+}
+
+/**
+ * INIT_MOCK_STRUCT_MATCHER_ENTRY()
+ * @entry: the &struct mock_struct_matcher_entry to initialize
+ * @type: the struct being matched
+ * @member: the member of the struct being matched, used to calculate the offset
+ * @matcher: matcher to match that member
+ *
+ * Initializes ``entry`` to match ``type->member`` with ``matcher``.
+ */
+#define INIT_MOCK_STRUCT_MATCHER_ENTRY(entry, type, member, matcher)	       \
+		init_mock_struct_matcher_entry_internal(entry,		       \
+							offsetof(type, member),\
+							matcher)
+
+static inline void INIT_MOCK_STRUCT_MATCHER_ENTRY_LAST(
+		struct mock_struct_matcher_entry *entry)
+{
+	entry->matcher = NULL;
+}
+
+struct mock_param_matcher *struct_cmp(
+		struct test *test,
+		const char *struct_name,
+		struct mock_struct_matcher_entry *entries);
+
 #endif /* _TEST_MOCK_H */
diff --git a/test/Makefile b/test/Makefile
index 6fe9ab4..e27ea5a 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,5 +1,6 @@
 obj-$(CONFIG_TEST)		+= test.o mock.o common-mocks.o string-stream.o \
   test-stream.o
 obj-$(CONFIG_TEST_TEST)		+= \
-  test-test.o test-mock.o mock-macro-test.o mock-test.o string-stream-test.o
+  test-test.o test-mock.o mock-macro-test.o mock-test.o string-stream-test.o \
+  test-stream-test.o
 obj-$(CONFIG_EXAMPLE_TEST)	+= example-test.o
diff --git a/test/common-mocks.c b/test/common-mocks.c
index 9206115..4ea87b9 100644
--- a/test/common-mocks.c
+++ b/test/common-mocks.c
@@ -199,6 +199,120 @@
 	return &matcher->matcher;
 }
 
+struct mock_str_contains_matcher {
+	struct mock_param_matcher matcher;
+	const char *needle;
+};
+
+static bool match_str_contains(struct mock_param_matcher *pmatcher,
+			       struct test_stream *stream,
+			       const void *phaystack)
+{
+	struct mock_str_contains_matcher *matcher =
+		container_of(pmatcher,
+			     struct mock_str_contains_matcher,
+			     matcher);
+	const char *haystack = CONVERT_TO_ACTUAL_TYPE(const char *, phaystack);
+	bool matches = strstr(haystack, matcher->needle);
+
+	if (matches)
+		stream->add(stream,
+			    "'%s' found in '%s'",
+			    matcher->needle,
+			    haystack);
+	else
+		stream->add(stream,
+			    "'%s' not found in '%s'",
+			    matcher->needle,
+			    haystack);
+	return matches;
+}
+
+struct mock_param_matcher *str_contains(struct test *test, const char *str)
+{
+	struct mock_str_contains_matcher *matcher;
+
+	matcher = test_kzalloc(test, sizeof(*matcher), GFP_KERNEL);
+	if (!matcher)
+		return NULL;
+
+	matcher->matcher.match = match_str_contains;
+	matcher->needle = str;
+
+	return &matcher->matcher;
+}
+
+struct mock_param_matcher *va_format_cmp(struct test *test,
+					 struct mock_param_matcher *fmt_matcher,
+					 struct mock_param_matcher *va_matcher)
+{
+	struct mock_struct_matcher_entry *entries;
+
+	entries = test_kzalloc(test, sizeof(*entries) * 3, GFP_KERNEL);
+	if (!entries)
+		return NULL;
+
+	INIT_MOCK_STRUCT_MATCHER_ENTRY(&entries[0],
+				       struct va_format,
+				       fmt,
+				       fmt_matcher);
+	INIT_MOCK_STRUCT_MATCHER_ENTRY(&entries[1],
+				       struct va_format,
+				       va,
+				       va_matcher);
+	INIT_MOCK_STRUCT_MATCHER_ENTRY_LAST(&entries[2]);
+
+	return struct_cmp(test, "va_format", entries);
+}
+
+struct mock_struct_matcher {
+	struct mock_param_matcher matcher;
+	const char *struct_name;
+	struct mock_struct_matcher_entry *entries;
+};
+
+static bool match_struct(struct mock_param_matcher *pmatcher,
+			 struct test_stream *stream,
+			 const void *pactual)
+{
+	struct mock_struct_matcher *matcher =
+			container_of(pmatcher,
+				     struct mock_struct_matcher,
+				     matcher);
+	struct mock_struct_matcher_entry *entry;
+	const char *actual = CONVERT_TO_ACTUAL_TYPE(const char *, pactual);
+	const char *member_ptr;
+	bool matches = true, tmp;
+
+	stream->add(stream, "struct %s {", matcher->struct_name);
+	for (entry = matcher->entries; entry->matcher; entry++) {
+		member_ptr = actual + entry->member_offset;
+		tmp = entry->matcher->match(entry->matcher, stream, member_ptr);
+		matches = matches && tmp;
+		stream->add(stream, ", ");
+	}
+	stream->add(stream, "}");
+
+	return matches;
+}
+
+struct mock_param_matcher *struct_cmp(struct test *test,
+				      const char *struct_name,
+				      struct mock_struct_matcher_entry *entries)
+{
+	struct mock_struct_matcher *matcher;
+
+	matcher = test_kzalloc(test, sizeof(*matcher), GFP_KERNEL);
+	if (!matcher)
+		return NULL;
+
+	matcher->matcher.match = match_struct;
+	matcher->struct_name = struct_name;
+	matcher->entries = entries;
+
+	return &matcher->matcher;
+}
+
 #define DEFINE_RETURN_ACTION_STRUCT(type_name, type)			       \
 		struct mock_##type_name##_action {			       \
 			struct mock_action action;			       \
diff --git a/test/mock-test.c b/test/mock-test.c
index bae6bbf..cf34e1f7 100644
--- a/test/mock-test.c
+++ b/test/mock-test.c
@@ -176,6 +176,42 @@
 	EXPECT_EQ(test, 0, expectation->times_called);
 }
 
+/*
+ * Method called on naggy mock with no expectations will not fail, but will show
+ * a warning message
+ */
+static void mock_test_naggy_no_expectations_no_fail(struct test *test)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test *trgt = mock_get_trgt(mock_test);
+	struct mock *mock = ctx->mock;
+	int param0 = 5, param1 = -5;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+	struct mock_expectation *expectation;
+
+	mock_set_default_action(mock, "test_printk", test_printk,
+		int_return(trgt, -4));
+
+	expectation = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
+	expectation->min_calls_expected = 0;
+	expectation->max_calls_expected = 0;
+
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test), any(test),
+		va_format_cmp(test, str_contains(test,
+			"Method was called with no expectations declared"),
+		any(test))));
+
+	mock->do_expect(mock,
+			"test_printk",
+			test_printk,
+			two_param_types,
+			two_params,
+			ARRAY_SIZE(two_params));
+	mock_validate_expectations(mock);
+}
+
 static void mock_test_mock_validate_expectations(struct test *test)
 {
 	struct mock_test_context *ctx = test->priv;
@@ -253,6 +289,7 @@
 	TEST_CASE(mock_test_failed_expect_call_fails_test),
 	TEST_CASE(mock_test_do_expect_default_return),
 	TEST_CASE(mock_test_mock_validate_expectations),
+	TEST_CASE(mock_test_naggy_no_expectations_no_fail),
 	{},
 };
 
diff --git a/test/test-stream-test.c b/test/test-stream-test.c
new file mode 100644
index 0000000..eb7d4cf
--- /dev/null
+++ b/test/test-stream-test.c
@@ -0,0 +1,135 @@
+#include <test/test.h>
+#include <test/mock.h>
+#include <test/test-stream.h>
+
+#include "test-mock.h"
+
+struct test_stream_test_context {
+	struct MOCK(test)	*mock_test;
+	struct test_stream	*stream;
+};
+
+static void test_stream_test_add(struct test *test)
+{
+	struct test_stream_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test_stream *stream = ctx->stream;
+
+	stream->add(stream, "Foo");
+	stream->add(stream, " %s", "bar");
+	stream->set_level(stream, KERN_INFO);
+
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+					  any(test),
+					  va_format_cmp(test,
+							streq(test, "Foo bar"),
+							any(test))));
+
+	stream->commit(stream);
+}
+
+static void test_stream_test_append(struct test *test)
+{
+	struct test_stream_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test_stream *stream = ctx->stream;
+	struct test_stream *other_stream;
+
+	stream->add(stream, "Foo");
+	stream->set_level(stream, KERN_INFO);
+	other_stream = test_new_stream(mock_get_trgt(mock_test));
+	other_stream->add(other_stream, " %s", "bar");
+
+	stream->append(stream, other_stream);
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+					  any(test),
+					  va_format_cmp(test,
+							streq(test, "Foo bar"),
+							any(test))));
+
+	stream->commit(stream);
+}
+
+static void test_stream_error_message_when_no_level_set(struct test *test)
+{
+	struct test_stream_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test_stream *stream = ctx->stream;
+	struct test_stream *other_stream;
+
+	stream->add(stream, "Foo bar");
+	other_stream = test_new_stream(mock_get_trgt(mock_test));
+
+	stream->append(stream, other_stream);
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+				 any(test),
+				 va_format_cmp(test,
+					       streq(test,
+						     "Stream was committed without a specified log level."),
+					       any(test))));
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+				any(test),
+				va_format_cmp(test,
+					streq(test, "Foo bar"),
+					any(test))));
+	stream->commit(stream);
+}
+
+static int test_stream_test_init(struct test *test)
+{
+	struct test_stream_test_context *ctx;
+
+	ctx = test_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	test->priv = ctx;
+
+	ctx->mock_test = CONSTRUCT_MOCK(test, test);
+	if (!ctx->mock_test)
+		return -EINVAL;
+
+	ctx->stream = test_new_stream(mock_get_trgt(ctx->mock_test));
+	if (!ctx->stream)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void test_stream_test_commits_any_uncommitted_when_cleanup(
+		struct test *test)
+{
+	struct test_stream_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test_stream *stream = ctx->stream;
+
+	stream->add(stream, "Hello World");
+	stream->set_level(stream, KERN_WARNING);
+
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+				 any(test),
+				 va_format_cmp(test,
+					       streq(test,
+						     "End of test case reached with uncommitted stream entries."),
+					       any(test))));
+	EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+			any(test),
+			va_format_cmp(test,
+				streq(test, "Hello World"),
+				any(test))));
+	test_cleanup(mock_get_trgt(mock_test));
+}
+
+static struct test_case test_stream_test_cases[] = {
+	TEST_CASE(test_stream_test_add),
+	TEST_CASE(test_stream_test_append),
+	TEST_CASE(test_stream_test_commits_any_uncommitted_when_cleanup),
+	TEST_CASE(test_stream_error_message_when_no_level_set),
+	{},
+};
+
+static struct test_module test_stream_test_module = {
+	.name = "test-stream-test",
+	.init = test_stream_test_init,
+	.test_cases = test_stream_test_cases,
+};
+module_test(test_stream_test_module);