From patchwork Fri Sep 18 18:31:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255192 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93C74C43463 for ; Fri, 18 Sep 2020 18:32:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 384E922208 for ; Fri, 18 Sep 2020 18:32:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="gTLzQBFj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726312AbgIRScA (ORCPT ); Fri, 18 Sep 2020 14:32:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726311AbgIRSb6 (ORCPT ); Fri, 18 Sep 2020 14:31:58 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C78B6C0613CE for ; Fri, 18 Sep 2020 11:31:57 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id t56so5820579qtt.19 for ; Fri, 18 Sep 2020 11:31:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=W9nO+Dl/jBcjFa1cyCl0d5sknSzAnqBMzzXnQytzWAY=; b=gTLzQBFjeHmySZXZjlH+Cpfv55BRdOabJbPLGKahTIe7bepc1Low+V/3dKSqLDOCsT lgqWpKjsOxNFgHRE6gw08dxfTby0JuVh6khTIkeGGz8pGgzPcuugQ7618iYVpcMRdlst IyelpqWKY9LlRbF/IYSdXC167xVhwfJSyXlViuV7rO/XpEOpDUpTiPV0jI+LNcbXeT+V 2qeQ2dlsRckey/TTIFMffFwe/Z1n7wpB0KeXxakDntDYMpmtWXsEeRqWQwv8CCw7Rzja TozN98aYkHuuW0Otq1jP2eiLyYZ+jaNSF+v1yAPA425b6s2p5chYk4Y4NhaMcLhyAkO+ CdBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=W9nO+Dl/jBcjFa1cyCl0d5sknSzAnqBMzzXnQytzWAY=; b=oXx2mNbYs5i5Sa4LPLpyjqovIWsZgrD+UVLJCq6WqNRWUPBYf25M16xeh4quJZV/xM Fat7262HJjEsq7Hsuxo89cdrp17iyXKM9mLxxk0MIEFuj9iB+Dg77FsZYxCge9ECqdkx VjmLj7Jp4gjbvd12ZT+azfp7HNasLh9vXHxK8M3chM/aWebHKp12hniIxOl8fkcTSuRt IObJEyK3hZ21ifivGum19CWe+33JpJypGLqQPrwBAl6M6Pe2SgBs4dS8hMJ69kh9Vtqk zF1qZk0fSWReelui1jlt7fH+weeSnRhJvDuzXj8mxZXUkQZn4nu6BxpnNc8w48n8XjeJ dPLQ== X-Gm-Message-State: AOAM531abZjFghpIPtjROz1W8pmizwdcJ0tCHBX0PM0IOipsQfRpxTr5 YzMrACtzBVg12d2LCpRFbOAu5b3smNyupg== X-Google-Smtp-Source: ABdhPJzluVgkElxn7+JunF/vO3905KAumFakcgRKA4/EGPvQkha+k80OrrxB5QCulfgZYxdfO/mFIO9+/at9ZQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:ad4:55ec:: with SMTP id bu12mr35168527qvb.0.1600453916926; Fri, 18 Sep 2020 11:31:56 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:04 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-3-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 02/12] kunit: test: add kunit_stream a std::stream like logger From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins A lot of the expectation and assertion infrastructure prints out fairly complicated test failure messages, so add a C++ style log library for for logging test results called `struct kunit_stream`. kunit_stream allows us to construct a message before we know whether we want to print it out; this can be extremely handy if there is information you might need for a failure message that is easiest to collect in the steps leading up to the actual check. Note that kunit_stream is distinct from string_stream: whereas string_stream is really just a string builder, kunit_stream is specifically used for constructing expectation/assertion (introduced later in this series) messages to a user of KUnit. Patch was previously reviewed at https://lore.kernel.org/linux-fsdevel/20190712081744.87097-5-brendanhiggins@google.com/ Primary differences are * kunit/ => lib/kunit/ * re-expose string_stream_clear(), which was made static since then Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov Cc: Stephen Boyd --- include/kunit/kunit-stream.h | 94 +++++++++++++++++++++++++++++ include/kunit/string-stream.h | 2 + include/kunit/test.h | 1 + lib/kunit/Makefile | 1 + lib/kunit/kunit-stream.c | 110 ++++++++++++++++++++++++++++++++++ lib/kunit/string-stream.c | 2 +- 6 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 include/kunit/kunit-stream.h create mode 100644 lib/kunit/kunit-stream.c diff --git a/include/kunit/kunit-stream.h b/include/kunit/kunit-stream.h new file mode 100644 index 000000000000..9a768732a04b --- /dev/null +++ b/include/kunit/kunit-stream.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#ifndef _KUNIT_KUNIT_STREAM_H +#define _KUNIT_KUNIT_STREAM_H + +#include +#include + +struct kunit; + +/** + * struct kunit_stream - a std::stream style string builder. + * + * A std::stream style string builder. Allows messages to be built up and + * printed all at once. Note that the intention is to only use + * &struct kunit_stream to communicate with a user of KUnit, most often to + * communicate something about an expectation or an assertion to the user. If + * you want a similar interface, but aren't sure if this is the right class for + * you to use, you probably want to use the related string_stream class, which + * is allowed for generic string construction in a similar manner. This class is + * really only for the KUnit library to communicate certain kinds of information + * to KUnit users and should not be used by anyone else. + * + * A note on &struct kunit_stream's usage: a kunit_stream will generally + * accompany *one* expectation or assertion. Multiple expectations/assertions + * may be validated concurrently at any given time, even within a single test + * case, so sharing a kunit_stream between expectations/assertions may result in + * unintended consequences. + */ +struct kunit_stream { + /* private: internal use only. */ + struct kunit *test; + struct string_stream *internal_stream; +}; + +/** + * alloc_kunit_stream() - constructs a new &struct kunit_stream. + * @test: The test context object. + * @gfp: The GFP flags to use for internal allocations. + * + * Constructs a new test managed &struct kunit_stream. + */ +struct kunit_stream *alloc_kunit_stream(struct kunit *test, + gfp_t gfp); + +/** + * kunit_stream_add(): adds the formatted input to the internal buffer. + * @kstream: the stream being operated on. + * @fmt: printf style format string to append to stream. + * + * Appends the formatted string, @fmt, to the internal buffer. + */ +void __printf(2, 3) kunit_stream_add(struct kunit_stream *kstream, + const char *fmt, ...); + +/** + * kunit_stream_append(): appends the contents of @other to @kstream. + * @kstream: the stream to which @other is appended. + * @other: the stream whose contents are appended to @kstream. + * + * Appends the contents of @other to @kstream. + */ +void kunit_stream_append(struct kunit_stream *kstream, + struct kunit_stream *other); + +/** + * kunit_stream_commit(): prints out the internal buffer to the user. + * @kstream: the stream being operated on. + * + * Outputs the contents of the internal buffer as a kunit_printk formatted + * output. KUNIT_STREAM ONLY OUTPUTS ITS BUFFER TO THE USER IF COMMIT IS + * CALLED!!! The reason for this is that it allows us to construct a message + * before we know whether we want to print it out; this can be extremely handy + * if there is information you might need for a failure message that is easiest + * to collect in the steps leading up to the actual check. + */ +void kunit_stream_commit(struct kunit_stream *kstream); + +/** + * kunit_stream_clear(): clears the internal buffer. + * @kstream: the stream being operated on. + * + * Clears the contents of the internal buffer. + */ +void kunit_stream_clear(struct kunit_stream *kstream); + +#endif /* _KUNIT_KUNIT_STREAM_H */ diff --git a/include/kunit/string-stream.h b/include/kunit/string-stream.h index fe98a00b75a9..44677c386890 100644 --- a/include/kunit/string-stream.h +++ b/include/kunit/string-stream.h @@ -46,6 +46,8 @@ int string_stream_append(struct string_stream *stream, bool string_stream_is_empty(struct string_stream *stream); +void string_stream_clear(struct string_stream *stream); + int string_stream_destroy(struct string_stream *stream); #endif /* _KUNIT_STRING_STREAM_H */ diff --git a/include/kunit/test.h b/include/kunit/test.h index 59f3144f009a..687782fa44d9 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -10,6 +10,7 @@ #define _KUNIT_TEST_H #include +#include #include #include #include diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 724b94311ca3..523a7b0f9783 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_KUNIT) += kunit.o kunit-objs += test.o \ string-stream.o \ assert.o \ + kunit-stream.o \ try-catch.o ifeq ($(CONFIG_KUNIT_DEBUGFS),y) diff --git a/lib/kunit/kunit-stream.c b/lib/kunit/kunit-stream.c new file mode 100644 index 000000000000..744798693f39 --- /dev/null +++ b/lib/kunit/kunit-stream.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * C++ stream style string formatter and printer used in KUnit for outputting + * KUnit messages. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include +#include + +void kunit_stream_add(struct kunit_stream *kstream, const char *fmt, ...) +{ + va_list args; + struct string_stream *stream = kstream->internal_stream; + + va_start(args, fmt); + + if (string_stream_vadd(stream, fmt, args)) + kunit_err(kstream->test, + "Failed to allocate fragment: %s\n", + fmt); + + va_end(args); +} + +void kunit_stream_append(struct kunit_stream *kstream, + struct kunit_stream *other) +{ + int ret; + + ret = string_stream_append(kstream->internal_stream, + other->internal_stream); + + if (ret) + kunit_err(kstream->test, + "Failed to append other stream: %d\n", ret); +} + +void kunit_stream_clear(struct kunit_stream *kstream) +{ + string_stream_clear(kstream->internal_stream); +} + +void kunit_stream_commit(struct kunit_stream *kstream) +{ + struct string_stream *stream = kstream->internal_stream; + struct string_stream_fragment *fragment; + struct kunit *test = kstream->test; + char *buf; + + buf = string_stream_get_string(stream); + if (!buf) { + kunit_err(test, + "Could not allocate buffer, dumping stream:\n"); + list_for_each_entry(fragment, &stream->fragments, node) { + kunit_err(test, "%s", fragment->fragment); + } + kunit_err(test, "\n"); + } else { + kunit_err(test, "%s", buf); + } + + kunit_stream_clear(kstream); +} + +struct kunit_stream_alloc_context { + struct kunit *test; + gfp_t gfp; +}; + +static int kunit_stream_init(struct kunit_resource *res, void *context) +{ + struct kunit_stream_alloc_context *ctx = context; + struct kunit_stream *stream; + + stream = kunit_kzalloc(ctx->test, sizeof(*stream), ctx->gfp); + if (!stream) + return -ENOMEM; + + stream->test = ctx->test; + stream->internal_stream = alloc_string_stream(ctx->test, ctx->gfp); + if (!stream->internal_stream) + return -ENOMEM; + + res->data = stream; + return 0; +} + +static void kunit_stream_free(struct kunit_resource *res) +{ + /* Do nothing because cleanup is handled by KUnit managed resources */ +} + +struct kunit_stream *alloc_kunit_stream(struct kunit *test, + gfp_t gfp) +{ + struct kunit_stream_alloc_context ctx = { + .test = test, + .gfp = gfp + }; + + return kunit_alloc_resource(test, + kunit_stream_init, + kunit_stream_free, + gfp, + &ctx); +} diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 151a0e7ac349..a94004134928 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -112,7 +112,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...) return result; } -static void string_stream_clear(struct string_stream *stream) +void string_stream_clear(struct string_stream *stream) { struct string_stream_fragment *frag_container, *frag_container_safe; From patchwork Fri Sep 18 18:31:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01022C43463 for ; Fri, 18 Sep 2020 18:32:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9F0AE21D7F for ; Fri, 18 Sep 2020 18:32:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="jDjmNJ3K" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726327AbgIRScB (ORCPT ); Fri, 18 Sep 2020 14:32:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbgIRSb7 (ORCPT ); Fri, 18 Sep 2020 14:31:59 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0ECDC0613D0 for ; Fri, 18 Sep 2020 11:31:59 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 193so6400762ybi.8 for ; Fri, 18 Sep 2020 11:31:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=6j9u1FoP2f0YImnHuabzWtMZNqpaUUCvlMhNCAR76JU=; b=jDjmNJ3KSjUg3aa2pfGH6CUYyYGAFArBEZZ1q/Tv+a+dfnBIYuV1XXDlylS5aE4y8U WO1UYuqBLmyj+k5wRntFsQ3bEH1WCZspbUxL/burLyyD4Dn6XJPB4d8VpnCEySdG7IXY ZYa6j5yEnprmhDU1vuXUvpDL1zB2Z/1tE1BKrfSynNYOXnQa4+0HBNBTSQ/5XIehuaUf ad7PyGX8YJF27WDgEWiVnDYzUHW5HmB5swPoT58rXdtoEdH24gFbi3eFxGQkBE8oWQxw wW5YFTniST+xPuOrdLrBU8QCD5dGYujxkpwLXxwfA/loh9zuRC2JJw8vFZoRGRbJalCB rfyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=6j9u1FoP2f0YImnHuabzWtMZNqpaUUCvlMhNCAR76JU=; b=rMKxXwR3w0qnTm1iNxNvGp0OH2fzrpxl/dmwomuF+vpAGgAPQuwuTnWo8FvrjnyblC z1pQd5Cm6npu4bC+do0Pmn4AqNzE+4XEqfEnBHvfQf0+vVIpy03A0cdlgkmnp5BOTlI+ 4M7Y7Z6ydBZ1iB/QhX2vILa0f4yhssPgWPRSHoVvSRLJqOjjdA/LoP4L3Y3Q1KpsQlLj 0UdMyaKua2k/8la8Ky79kSTqp4Ip+LLJKJJ5WyY5XaWBGwYSvn/33e6Qs5pyqXOxyzgc 5MvArG8iH7ypACLDdtKfWQ+m5INjLPrGl2B+699QFG31b9+M+AgUmZzJIr0UUf8iH+Lm +oZQ== X-Gm-Message-State: AOAM533knRevZpWdnJtTw2Pz3u7MUZrpq3SeylNTWWZ4KZQbM6i9mhin DEtvzwiuetJjqO1MfbfqPbr4q0ZlMmH7eg== X-Google-Smtp-Source: ABdhPJwSKO0Xmi7ZdVdGxwSqjplGIFXXa2v0SvgpaWFgjL3kkLAAaO0/pZnF0v65UTjDjem62JeMBrGHZTNwNw== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a25:6a06:: with SMTP id f6mr12076185ybc.334.1600453918948; Fri, 18 Sep 2020 11:31:58 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:05 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-4-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 03/12] kunit: test: add concept of post conditions From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Add a way to specify that certain conditions must be met at the end of a test case. Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- include/kunit/test.h | 6 ++++++ lib/kunit/test.c | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 687782fa44d9..0eb3abb00da4 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -190,6 +190,11 @@ struct kunit_suite { char *log; }; +struct kunit_post_condition { + struct list_head node; + void (*validate)(struct kunit_post_condition *condition); +}; + /** * struct kunit - represents a running instance of a test. * @@ -223,6 +228,7 @@ struct kunit { * protect it with some type of lock. */ struct list_head resources; /* Protected by lock. */ + struct list_head post_conditions; }; void kunit_init_test(struct kunit *test, const char *name, char *log); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 670d1cc9c105..4e8c74c89073 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -228,6 +228,7 @@ void kunit_init_test(struct kunit *test, const char *name, char *log) { spin_lock_init(&test->lock); INIT_LIST_HEAD(&test->resources); + INIT_LIST_HEAD(&test->post_conditions); test->name = name; test->log = log; if (test->log) @@ -269,6 +270,16 @@ static void kunit_case_internal_cleanup(struct kunit *test) static void kunit_run_case_cleanup(struct kunit *test, struct kunit_suite *suite) { + struct kunit_post_condition *condition, *condition_safe; + + list_for_each_entry_safe(condition, + condition_safe, + &test->post_conditions, + node) { + condition->validate(condition); + list_del(&condition->node); + } + if (suite->exit) suite->exit(test); From patchwork Fri Sep 18 18:31:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255188 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A04BC43465 for ; Fri, 18 Sep 2020 18:32:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB20B22211 for ; Fri, 18 Sep 2020 18:32:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="bUtWKfIL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726360AbgIRScJ (ORCPT ); Fri, 18 Sep 2020 14:32:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726382AbgIRScG (ORCPT ); Fri, 18 Sep 2020 14:32:06 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52795C0613D1 for ; Fri, 18 Sep 2020 11:32:05 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id w69so3986261pgw.7 for ; Fri, 18 Sep 2020 11:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=gJao4X6+KTMHEows7N2UhZIG5yfBzHyRTcMMo4PkBvg=; b=bUtWKfIL4GW4/m+BQNZHjOvLG9OLF6Pflcmhg4nwTBIY7SmZyZ5E08RvdQOWuxM8/z CUUO40mBIm9Rxb5NWOWGndOTyL2ANa/40lymPISHyAX9GZg4SoqNqJQJOIP5IRiMACKS 0vsklPoVUCUX4kgNvHBwQEXssHqqd8lX/TRmQ3wOmP6akOSrQcClZ8GsWlpO59D6+wmD f/J1gwNkt3DMxNrt1rQ3GM2xRxORb2iSg9854Ub5+umHzs2HJYp9ZntmqUpJ80WBGvPP FMzIDbB0raDqCjleJ3AwtblhBw1aH2ei3oippBsg7KoCyASeYATk8tMZ5z23BD0pgNyL k1CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=gJao4X6+KTMHEows7N2UhZIG5yfBzHyRTcMMo4PkBvg=; b=W6byRFFYDKoYRG3kdYoTjxq8xCZeD9w05kNmG3s8FkTlcm2GkaU95PhkF5ObYdva90 bAJO5Uu6yj9lIE5ZFoo0TJecPnlU15Y0Yk74ARC4ZRpReSMbZow65wtQYo9309p/Qce1 KMU1zeqEqEEYepPrj4u6GMq57PuV13Vw2G3TLRzNx99f4lgn2aBt4H9ym8+QGXDvxYOo 0pWPafqqGfjA/bK0nJTR+3PSVqmbdGCdQ0Mk7O75yqR6umnEWTbS2r26zrrF+UHljtxV f5wlem//GlXWV+bgZ/0bfeNf8e+VB6jWBXjJahYkP6dUW8ZfsgX6AnkrZsIwcpjtX48S YL7A== X-Gm-Message-State: AOAM530SdFRcHYWNYKPQBhtWntiqejojUq14ypdji6zE61Sk8NCHOz9h khcoryBfHNNLjjPByJGK+knECG7gOFBF2w== X-Google-Smtp-Source: ABdhPJz/rWUW/Gc65pMGhH0XQKJ0DeOELYpZc4eku1aD31lT5XDBFBKecK+/VkMTyhsPDgDiMyllt7OFvyj1YQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a17:902:aa0a:b029:d0:89f4:6224 with SMTP id be10-20020a170902aa0ab02900d089f46224mr34591239plb.12.1600453924807; Fri, 18 Sep 2020 11:32:04 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:08 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-7-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 06/12] kunit: expose kunit_set_failure() for use by mocking From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Being able to fail the test outside of expectations and assertions is a requirement for new features, e.g. mocking, dynamic analysis, etc. Signed-off-by: Daniel Latypov --- include/kunit/test.h | 2 ++ lib/kunit/test.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 0eb3abb00da4..05330593243d 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -233,6 +233,8 @@ struct kunit { void kunit_init_test(struct kunit *test, const char *name, char *log); +void kunit_set_failure(struct kunit *test); + int kunit_run_tests(struct kunit_suite *suite); size_t kunit_suite_num_test_cases(struct kunit_suite *suite); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 4e8c74c89073..1ccf6dbecd73 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -15,7 +15,7 @@ #include "debugfs.h" #include "try-catch-impl.h" -static void kunit_set_failure(struct kunit *test) +void kunit_set_failure(struct kunit *test) { WRITE_ONCE(test->success, false); } From patchwork Fri Sep 18 18:31:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D6E6C43463 for ; Fri, 18 Sep 2020 18:32:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E835123119 for ; Fri, 18 Sep 2020 18:32:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="GsTN0Fqq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726394AbgIRScJ (ORCPT ); Fri, 18 Sep 2020 14:32:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726392AbgIRScH (ORCPT ); Fri, 18 Sep 2020 14:32:07 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B985C0613D2 for ; Fri, 18 Sep 2020 11:32:07 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id q131so5265682qke.22 for ; Fri, 18 Sep 2020 11:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=y15UkrpVILsyPpJzSCvvGnSuGfIrAVdnneb1O3jNsec=; b=GsTN0Fqqhr374zpwIsk8tbfwFUnxs21Z1dtpKfsPyeb7jpd9rHIdP8GYAg8FnOXys1 OIELCjgsdH3BIVjw86Wfbp18sFXjglTYNmRe6WyWgnHdOnmLf5OHztZ1z1ZsqesIbc/F ESNH2BQqgu6t3B+r0/gAd+vKO/SpgK/Pf81UF3UtC1s7TjsIsWzzw46LzqRHl/I2DYbi rWiuctWHFJSS6NqpRtIJ/yInOZp/06u+5rFNOk48qJN7KI+sTVNFrZxcQAqy2Rwyaf8u GKZXXZ6VfYpG0agd95n5l1D9JPG7YS586nJzRMU4OZfPEhJa+ScjBra5/BaqPPynH/yV jtkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=y15UkrpVILsyPpJzSCvvGnSuGfIrAVdnneb1O3jNsec=; b=a+3q7BafcOcJoq/72lxtIXybpLYfvXe0LOdkjQcFqa1ivkYysIB8ErGqd1tuMrH5Bw BQymVlH/gsRDY5rZZKe/PtKoZ/g1UlPWMrR7r7fCymX9dOPZ8ERS1kHrLmuyWIAUCL/v uqVJ/YANvu+yhAwnzHgeX4K2zDj3PLaER9XV1uUZk2U33GLv4soWrmOFP570MnpNx/16 8a1mW+I9rlBb99BE+tVo23aH9gvwTF+IXYDXLvi9QP1pwhT512LJFzPFro7cDvSn8EcJ tPh8y/QMsGK8SJAxV6e2vUYV109insGOx2RrX4yYPJXmR9BHeDt2WHvtqHlu8AWjBDzt fwFg== X-Gm-Message-State: AOAM531fNndCWel8jNGp7uTE4TsOaMTc9sAR0IcLkohlh4w0dS+UcylA 0YubvgauLsF+sEByA81YwMxmkMfiUDl3iA== X-Google-Smtp-Source: ABdhPJzjKGUVA/MU3if7G/38erCuSrsG3UTt9u//MEEn/WGslt9fIIYhmXXIsbH6L0FlpdMCZ1OrFueu6Rarmw== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:ad4:58e7:: with SMTP id di7mr18236843qvb.36.1600453926645; Fri, 18 Sep 2020 11:32:06 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:09 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-8-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 07/12] kunit: mock: add internal mock infrastructure From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Add the core internal mechanisms that mocks are implemented with; in particular, this adds the mechanisms by which expectation on mocks are validated and by which actions may be supplied and then executed when mocks are called. Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- include/kunit/mock.h | 126 +++++++++++++++ lib/kunit/Makefile | 1 + lib/kunit/mock.c | 364 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 491 insertions(+) create mode 100644 include/kunit/mock.h create mode 100644 lib/kunit/mock.c diff --git a/include/kunit/mock.h b/include/kunit/mock.h new file mode 100644 index 000000000000..299b423fdd51 --- /dev/null +++ b/include/kunit/mock.h @@ -0,0 +1,126 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Mocking API for KUnit. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#ifndef _KUNIT_MOCK_H +#define _KUNIT_MOCK_H + +#include +#include /* For PARAMS(...) */ +#include +#include +#include + +/** + * struct mock_param_matcher - represents a matcher used in a *call expectation* + * @match: the function that performs the matching + * + * The matching function takes a couple of parameters: + * + * - ``this``: refers to the parent struct + * - ``stream``: a &kunit_stream to which a detailed message should be added as + * to why the parameter matches or not + * - ``param``: a pointer to the parameter to check for a match + * + * The matching function should return whether or not the passed parameter + * matches. + */ +struct mock_param_matcher { + bool (*match)(struct mock_param_matcher *this, + struct kunit_stream *stream, + const void *param); +}; + +#define MOCK_MAX_PARAMS 255 + +struct mock_matcher { + struct mock_param_matcher *matchers[MOCK_MAX_PARAMS]; + int num; +}; + +/** + * struct mock_action - Represents an action that a mock performs when + * expectation is matched + * @do_action: the action to perform + * + * The action function is given some parameters: + * + * - ``this``: refers to the parent struct + * - ``params``: an array of pointers to the params passed into the mocked + * method or function. **The class argument is excluded for a mocked class + * method.** + * - ``len``: size of ``params`` + * + * The action function returns a pointer to the value that the mocked method + * or function should be returning. + */ +struct mock_action { + void *(*do_action)(struct mock_action *this, + const void **params, + int len); +}; + +/** + * struct mock_expectation - represents a *call expectation* on a function. + * @action: A &struct mock_action to perform when the function is called. + * @max_calls_expected: maximum number of times an expectation may be called. + * @min_calls_expected: minimum number of times an expectation may be called. + * @retire_on_saturation: no longer match once ``max_calls_expected`` is + * reached. + * + * Represents a *call expectation* on a function created with + * KUNIT_EXPECT_CALL(). + */ +struct mock_expectation { + struct mock_action *action; + int max_calls_expected; + int min_calls_expected; + bool retire_on_saturation; + /* private: internal use only. */ + const char *expectation_name; + struct list_head node; + struct mock_matcher *matcher; + int times_called; +}; + +struct mock_method { + struct list_head node; + const char *method_name; + const void *method_ptr; + struct mock_action *default_action; + struct list_head expectations; +}; + +struct mock { + struct kunit_post_condition parent; + struct kunit *test; + struct list_head methods; + /* TODO(brendanhiggins@google.com): add locking to do_expect. */ + const void *(*do_expect)(struct mock *mock, + const char *method_name, + const void *method_ptr, + const char * const *param_types, + const void **params, + int len); +}; + +void mock_init_ctrl(struct kunit *test, struct mock *mock); + +void mock_validate_expectations(struct mock *mock); + +int mock_set_default_action(struct mock *mock, + const char *method_name, + const void *method_ptr, + struct mock_action *action); + +struct mock_expectation *mock_add_matcher(struct mock *mock, + const char *method_name, + const void *method_ptr, + struct mock_param_matcher *matchers[], + int len); + +#endif /* _KUNIT_MOCK_H */ diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 1707660c8b1c..a51157970502 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -1,6 +1,7 @@ obj-$(CONFIG_KUNIT) += kunit.o kunit-objs += test.o \ + mock.o \ string-stream.o \ assert.o \ kunit-stream.o \ diff --git a/lib/kunit/mock.c b/lib/kunit/mock.c new file mode 100644 index 000000000000..12fb88899451 --- /dev/null +++ b/lib/kunit/mock.c @@ -0,0 +1,364 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Mocking API for KUnit. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#include + +static bool mock_match_params(struct mock_matcher *matcher, + struct kunit_stream *stream, + const void **params, + int len) +{ + struct mock_param_matcher *param_matcher; + bool ret = true, tmp; + int i; + + BUG_ON(matcher->num != len); + + for (i = 0; i < matcher->num; i++) { + param_matcher = matcher->matchers[i]; + kunit_stream_add(stream, "\t"); + tmp = param_matcher->match(param_matcher, stream, params[i]); + ret = ret && tmp; + kunit_stream_add(stream, "\n"); + } + + return ret; +} + +static const void *mock_do_expect(struct mock *mock, + const char *method_name, + const void *method_ptr, + const char * const *type_names, + const void **params, + int len); + +static void fail_and_flush(struct kunit *test, struct kunit_stream *stream) +{ + kunit_set_failure(test); + kunit_stream_commit(stream); +} + +void mock_validate_expectations(struct mock *mock) +{ + struct mock_expectation *expectation, *expectation_safe; + struct kunit_stream *stream; + struct mock_method *method; + int times_called; + + stream = alloc_kunit_stream(mock->test, GFP_KERNEL); + list_for_each_entry(method, &mock->methods, node) { + list_for_each_entry_safe(expectation, expectation_safe, + &method->expectations, node) { + times_called = expectation->times_called; + if (!(expectation->min_calls_expected <= times_called && + times_called <= expectation->max_calls_expected) + ) { + kunit_stream_add(stream, + "Expectation was not called the specified number of times:\n\t"); + kunit_stream_add(stream, + "Function: %s, min calls: %d, max calls: %d, actual calls: %d\n", + method->method_name, + expectation->min_calls_expected, + expectation->max_calls_expected, + times_called); + fail_and_flush(mock->test, stream); + } + list_del(&expectation->node); + } + } +} + +static void mock_validate_wrapper(struct kunit_post_condition *condition) +{ + struct mock *mock = container_of(condition, struct mock, parent); + + mock_validate_expectations(mock); +} + +void mock_init_ctrl(struct kunit *test, struct mock *mock) +{ + mock->test = test; + INIT_LIST_HEAD(&mock->methods); + mock->do_expect = mock_do_expect; + mock->parent.validate = mock_validate_wrapper; + list_add_tail(&mock->parent.node, &test->post_conditions); +} + +static struct mock_method *mock_lookup_method(struct mock *mock, + const void *method_ptr) +{ + struct mock_method *ret; + + list_for_each_entry(ret, &mock->methods, node) { + if (ret->method_ptr == method_ptr) + return ret; + } + + return NULL; +} + +static struct mock_method *mock_add_method(struct mock *mock, + const char *method_name, + const void *method_ptr) +{ + struct mock_method *method; + + method = kunit_kzalloc(mock->test, sizeof(*method), GFP_KERNEL); + if (!method) + return NULL; + + INIT_LIST_HEAD(&method->expectations); + method->method_name = method_name; + method->method_ptr = method_ptr; + list_add_tail(&method->node, &mock->methods); + + return method; +} + +static int mock_add_expectation(struct mock *mock, + const char *method_name, + const void *method_ptr, + struct mock_expectation *expectation) +{ + struct mock_method *method; + + method = mock_lookup_method(mock, method_ptr); + if (!method) { + method = mock_add_method(mock, method_name, method_ptr); + if (!method) + return -ENOMEM; + } + + list_add_tail(&expectation->node, &method->expectations); + + return 0; +} + +struct mock_expectation *mock_add_matcher(struct mock *mock, + const char *method_name, + const void *method_ptr, + struct mock_param_matcher *matchers[], + int len) +{ + struct mock_expectation *expectation; + struct mock_matcher *matcher; + int ret; + + expectation = kunit_kzalloc(mock->test, + sizeof(*expectation), + GFP_KERNEL); + if (!expectation) + return NULL; + + matcher = kunit_kmalloc(mock->test, sizeof(*matcher), GFP_KERNEL); + if (!matcher) + return NULL; + + memcpy(&matcher->matchers, matchers, sizeof(*matchers) * len); + matcher->num = len; + + expectation->matcher = matcher; + expectation->max_calls_expected = 1; + expectation->min_calls_expected = 1; + + ret = mock_add_expectation(mock, method_name, method_ptr, expectation); + if (ret < 0) + return NULL; + + return expectation; +} + +int mock_set_default_action(struct mock *mock, + const char *method_name, + const void *method_ptr, + struct mock_action *action) +{ + struct mock_method *method; + + method = mock_lookup_method(mock, method_ptr); + if (!method) { + method = mock_add_method(mock, method_name, method_ptr); + if (!method) + return -ENOMEM; + } + + method->default_action = action; + + return 0; +} + +static void mock_format_param(struct kunit_stream *stream, + const char *type_name, + const void *param) +{ + /* + * Cannot find formatter, so just print the pointer of the + * symbol. + */ + kunit_stream_add(stream, "<%pS>", param); +} + +static void mock_add_method_declaration_to_stream( + struct kunit_stream *stream, + const char *function_name, + const char * const *type_names, + const void **params, + int len) +{ + int i; + + kunit_stream_add(stream, "%s(", function_name); + for (i = 0; i < len; i++) { + mock_format_param(stream, type_names[i], params[i]); + if (i < len - 1) + kunit_stream_add(stream, ", "); + } + kunit_stream_add(stream, ")\n"); +} + +static struct kunit_stream *mock_initialize_failure_message( + struct kunit *test, + const char *function_name, + const char * const *type_names, + const void **params, + int len) +{ + struct kunit_stream *stream; + + stream = alloc_kunit_stream(test, GFP_KERNEL); + if (!stream) + return NULL; + + kunit_stream_add(stream, + "EXPECTATION FAILED: no expectation for call: "); + mock_add_method_declaration_to_stream(stream, + function_name, + type_names, + params, + len); + return stream; +} + +static bool mock_is_expectation_retired(struct mock_expectation *expectation) +{ + return expectation->retire_on_saturation && + expectation->times_called == + expectation->max_calls_expected; +} + +static void mock_add_method_expectation_error(struct kunit *test, + struct kunit_stream *stream, + char *message, + struct mock *mock, + struct mock_method *method, + const char * const *type_names, + const void **params, + int len) +{ + kunit_stream_clear(stream); + kunit_stream_add(stream, message); + mock_add_method_declaration_to_stream(stream, + method->method_name, type_names, params, len); +} + +static struct mock_expectation *mock_apply_expectations( + struct mock *mock, + struct mock_method *method, + const char * const *type_names, + const void **params, + int len) +{ + struct kunit_stream *attempted_matching_stream; + bool expectations_all_saturated = true; + struct kunit *test = mock->test; + struct kunit_stream *stream = alloc_kunit_stream(test, GFP_KERNEL); + struct mock_expectation *ret; + + if (list_empty(&method->expectations)) { + mock_add_method_expectation_error(test, stream, + "Method was called with no expectations declared: ", + mock, method, type_names, params, len); + kunit_stream_commit(stream); + return NULL; + } + + attempted_matching_stream = mock_initialize_failure_message( + test, + method->method_name, + type_names, + params, + len); + + list_for_each_entry(ret, &method->expectations, node) { + if (mock_is_expectation_retired(ret)) + continue; + expectations_all_saturated = false; + + kunit_stream_add(attempted_matching_stream, + "Tried expectation: %s, but\n", + ret->expectation_name); + if (mock_match_params(ret->matcher, + attempted_matching_stream, params, len)) { + /* + * Matcher was found; we won't print, so clean up the + * log. + */ + kunit_stream_clear(attempted_matching_stream); + return ret; + } + } + + if (expectations_all_saturated) { + mock_add_method_expectation_error(test, stream, + "Method was called with fully saturated expectations: ", + mock, method, type_names, params, len); + } else { + mock_add_method_expectation_error(test, stream, + "Method called that did not match any expectations: ", + mock, method, type_names, params, len); + kunit_stream_append(stream, attempted_matching_stream); + } + fail_and_flush(test, stream); + kunit_stream_clear(attempted_matching_stream); + return NULL; +} + +static const void *mock_do_expect(struct mock *mock, + const char *method_name, + const void *method_ptr, + const char * const *param_types, + const void **params, + int len) +{ + struct mock_expectation *expectation; + struct mock_method *method; + struct mock_action *action; + + method = mock_lookup_method(mock, method_ptr); + if (!method) + return NULL; + + expectation = mock_apply_expectations(mock, + method, + param_types, + params, + len); + if (!expectation) { + action = method->default_action; + } else { + expectation->times_called++; + if (expectation->action) + action = expectation->action; + else + action = method->default_action; + } + if (!action) + return NULL; + + return action->do_action(action, params, len); +} From patchwork Fri Sep 18 18:31:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E24AAC43463 for ; Fri, 18 Sep 2020 18:32:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8BAF521D7F for ; Fri, 18 Sep 2020 18:32:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="HE7e9clC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726415AbgIRScM (ORCPT ); Fri, 18 Sep 2020 14:32:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726379AbgIRScK (ORCPT ); Fri, 18 Sep 2020 14:32:10 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDE76C0613CE for ; Fri, 18 Sep 2020 11:32:09 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id u11so3851340pls.16 for ; Fri, 18 Sep 2020 11:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=qypWKHXedF8/KF4Xvr3+aRHP7U3dz5Tf83jt6mtBndg=; b=HE7e9clCmg0oUbqIj6monaOTb6MIK+LUI55ECVMtIlloc5B8VzAkzFnU0gyPsDHJgH iIIye2eWrKZnyoGgt2TEuNE9+qL/jWfafuKj6NGlsfpHAqfXcn1xcfoG+DvA31ITA7nu Li/yxBaMGDYA9V6rZOuB22hcPsst3Ff9CaVYyskZz5EuAvS7w3ElSQtEkpQ1woVf04nq ROK2YC2LzNGcgbwX9SF6lePqMy4bUbGphxrPnGyuHn1u2I+ihRE/boFfgY6lyiXP6WGf YEWbHJUTsCPfutKMDALCrCGKcGSKgQM6UMK+LhuKRzWoXPdV6fMve59gDerYVbpEx7CM l3LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=qypWKHXedF8/KF4Xvr3+aRHP7U3dz5Tf83jt6mtBndg=; b=fMvhOd051VAhIkEQPrqlSIM2H1FUeb+1KEqF7a5S3C49ItaQfTOPaERYrFGcxU+I3w Fx7Q8xlRsm5I64AW3w6kW1gHnDrZ0F3SBXlQzf4Z/0BqOWxAZc6oE74IYrtReYf8Gl4q Dvo6wJRb2lkVvEXkvATOLyMaPEP7r+naQrd48r+NYnqF+uEPZAiAJvpAaZWG4d1iY/fJ F89vET/yQqnH96S866t0Aw8rZW/GicitSH10sHXAbHqiOySrmyTv71FTDJrhiyJYmghD UDjixd/a97yiVW3ufZYGYDSHW7KQEIObwzCsqrQ09vugy5mV90QUfuwSagX4Dbi2Mblw RT6g== X-Gm-Message-State: AOAM530Ag+jKWxAexhnc1TjSZVvngKqmv8cZWP1JhstJ0jG7OvWtiQA9 +vGMIDGtFTApIszTIRjmv/OXOMcoe806NA== X-Google-Smtp-Source: ABdhPJwE60WXp8KeBIxu4dUHV/wIrnmb9FtigordbJCGRWafmNbNCRY4un4uphCWJLcNigZuC037SafvKmapEQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a63:f34b:: with SMTP id t11mr28170252pgj.111.1600453929259; Fri, 18 Sep 2020 11:32:09 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:10 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-9-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 08/12] kunit: mock: add basic matchers and actions From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Add basic matchers and actions needed for any kind of mocking to be useful; these matchers and actions are how expectations for mocks are described: what calls the mocks are expected to receive, and what the mock should do under those circumstances. Co-developed-by: Daniel Latypov Signed-off-by: Daniel Latypov Signed-off-by: Brendan Higgins --- include/kunit/mock.h | 222 ++++++++++++++++++++++++++++++++ lib/kunit/Makefile | 1 + lib/kunit/common-mocks.c | 271 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 494 insertions(+) create mode 100644 lib/kunit/common-mocks.c diff --git a/include/kunit/mock.h b/include/kunit/mock.h index 299b423fdd51..13fdeb8730b5 100644 --- a/include/kunit/mock.h +++ b/include/kunit/mock.h @@ -123,4 +123,226 @@ struct mock_expectation *mock_add_matcher(struct mock *mock, struct mock_param_matcher *matchers[], int len); +#define CONVERT_TO_ACTUAL_TYPE(type, ptr) (*((type *) ptr)) + +/** + * DOC: Built In Matchers + * + * These are the matchers that can be used when matching arguments in + * :c:func:`KUNIT_EXPECT_CALL` (more can be defined manually). + * + * For example, there's a matcher that matches any arguments: + * + * .. code-block:: c + * + * struct mock_param_matcher *any(struct kunit *test); + * + * There are matchers for integers based on the binary condition: + * + * * eq: equals to + * * ne: not equal to + * * lt: less than + * * le: less than or equal to + * * gt: greater than + * * ge: greater than or equal to + * + * .. code-block:: c + * + * struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected); + * struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected); + * struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected); + * struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected); + * struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected); + * struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected); + * + * For a detailed list, please see + * ``include/linux/mock.h``. + */ + +/* Matches any argument */ +struct mock_param_matcher *kunit_any(struct kunit *test); + +/* + * Matches different types of integers, the argument is compared to the + * `expected` field, based on the comparison defined. + */ +struct mock_param_matcher *kunit_u8_eq(struct kunit *test, u8 expected); +struct mock_param_matcher *kunit_u8_ne(struct kunit *test, u8 expected); +struct mock_param_matcher *kunit_u8_le(struct kunit *test, u8 expected); +struct mock_param_matcher *kunit_u8_lt(struct kunit *test, u8 expected); +struct mock_param_matcher *kunit_u8_ge(struct kunit *test, u8 expected); +struct mock_param_matcher *kunit_u8_gt(struct kunit *test, u8 expected); + +struct mock_param_matcher *kunit_u16_eq(struct kunit *test, u16 expected); +struct mock_param_matcher *kunit_u16_ne(struct kunit *test, u16 expected); +struct mock_param_matcher *kunit_u16_le(struct kunit *test, u16 expected); +struct mock_param_matcher *kunit_u16_lt(struct kunit *test, u16 expected); +struct mock_param_matcher *kunit_u16_ge(struct kunit *test, u16 expected); +struct mock_param_matcher *kunit_u16_gt(struct kunit *test, u16 expected); + +struct mock_param_matcher *kunit_u32_eq(struct kunit *test, u32 expected); +struct mock_param_matcher *kunit_u32_ne(struct kunit *test, u32 expected); +struct mock_param_matcher *kunit_u32_le(struct kunit *test, u32 expected); +struct mock_param_matcher *kunit_u32_lt(struct kunit *test, u32 expected); +struct mock_param_matcher *kunit_u32_ge(struct kunit *test, u32 expected); +struct mock_param_matcher *kunit_u32_gt(struct kunit *test, u32 expected); + +struct mock_param_matcher *kunit_u64_eq(struct kunit *test, u64 expected); +struct mock_param_matcher *kunit_u64_ne(struct kunit *test, u64 expected); +struct mock_param_matcher *kunit_u64_le(struct kunit *test, u64 expected); +struct mock_param_matcher *kunit_u64_lt(struct kunit *test, u64 expected); +struct mock_param_matcher *kunit_u64_ge(struct kunit *test, u64 expected); +struct mock_param_matcher *kunit_u64_gt(struct kunit *test, u64 expected); + +struct mock_param_matcher *kunit_char_eq(struct kunit *test, char expected); +struct mock_param_matcher *kunit_char_ne(struct kunit *test, char expected); +struct mock_param_matcher *kunit_char_le(struct kunit *test, char expected); +struct mock_param_matcher *kunit_char_lt(struct kunit *test, char expected); +struct mock_param_matcher *kunit_char_ge(struct kunit *test, char expected); +struct mock_param_matcher *kunit_char_gt(struct kunit *test, char expected); + +struct mock_param_matcher *kunit_uchar_eq(struct kunit *test, + unsigned char expected); +struct mock_param_matcher *kunit_uchar_ne(struct kunit *test, + unsigned char expected); +struct mock_param_matcher *kunit_uchar_le(struct kunit *test, + unsigned char expected); +struct mock_param_matcher *kunit_uchar_lt(struct kunit *test, + unsigned char expected); +struct mock_param_matcher *kunit_uchar_ge(struct kunit *test, + unsigned char expected); +struct mock_param_matcher *kunit_uchar_gt(struct kunit *test, + unsigned char expected); + +struct mock_param_matcher *kunit_schar_eq(struct kunit *test, + signed char expected); +struct mock_param_matcher *kunit_schar_ne(struct kunit *test, + signed char expected); +struct mock_param_matcher *kunit_schar_le(struct kunit *test, + signed char expected); +struct mock_param_matcher *kunit_schar_lt(struct kunit *test, + signed char expected); +struct mock_param_matcher *kunit_schar_ge(struct kunit *test, + signed char expected); +struct mock_param_matcher *kunit_schar_gt(struct kunit *test, + signed char expected); + +struct mock_param_matcher *kunit_short_eq(struct kunit *test, short expected); +struct mock_param_matcher *kunit_short_ne(struct kunit *test, short expected); +struct mock_param_matcher *kunit_short_le(struct kunit *test, short expected); +struct mock_param_matcher *kunit_short_lt(struct kunit *test, short expected); +struct mock_param_matcher *kunit_short_ge(struct kunit *test, short expected); +struct mock_param_matcher *kunit_short_gt(struct kunit *test, short expected); + +struct mock_param_matcher *kunit_ushort_eq(struct kunit *test, + unsigned short expected); +struct mock_param_matcher *kunit_ushort_ne(struct kunit *test, + unsigned short expected); +struct mock_param_matcher *kunit_ushort_le(struct kunit *test, + unsigned short expected); +struct mock_param_matcher *kunit_ushort_lt(struct kunit *test, + unsigned short expected); +struct mock_param_matcher *kunit_ushort_ge(struct kunit *test, + unsigned short expected); +struct mock_param_matcher *kunit_ushort_gt(struct kunit *test, + unsigned short expected); + +struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected); +struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected); +struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected); +struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected); +struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected); +struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected); + +struct mock_param_matcher *kunit_uint_eq(struct kunit *test, + unsigned int expected); +struct mock_param_matcher *kunit_uint_ne(struct kunit *test, + unsigned int expected); +struct mock_param_matcher *kunit_uint_lt(struct kunit *test, + unsigned int expected); +struct mock_param_matcher *kunit_uint_le(struct kunit *test, + unsigned int expected); +struct mock_param_matcher *kunit_uint_gt(struct kunit *test, + unsigned int expected); +struct mock_param_matcher *kunit_uint_ge(struct kunit *test, + unsigned int expected); + +struct mock_param_matcher *kunit_long_eq(struct kunit *test, long expected); +struct mock_param_matcher *kunit_long_ne(struct kunit *test, long expected); +struct mock_param_matcher *kunit_long_le(struct kunit *test, long expected); +struct mock_param_matcher *kunit_long_lt(struct kunit *test, long expected); +struct mock_param_matcher *kunit_long_ge(struct kunit *test, long expected); +struct mock_param_matcher *kunit_long_gt(struct kunit *test, long expected); + +struct mock_param_matcher *kunit_ulong_eq(struct kunit *test, + unsigned long expected); +struct mock_param_matcher *kunit_ulong_ne(struct kunit *test, + unsigned long expected); +struct mock_param_matcher *kunit_ulong_le(struct kunit *test, + unsigned long expected); +struct mock_param_matcher *kunit_ulong_lt(struct kunit *test, + unsigned long expected); +struct mock_param_matcher *kunit_ulong_ge(struct kunit *test, + unsigned long expected); +struct mock_param_matcher *kunit_ulong_gt(struct kunit *test, + unsigned long expected); + +struct mock_param_matcher *kunit_longlong_eq(struct kunit *test, + long long expected); +struct mock_param_matcher *kunit_longlong_ne(struct kunit *test, + long long expected); +struct mock_param_matcher *kunit_longlong_le(struct kunit *test, + long long expected); +struct mock_param_matcher *kunit_longlong_lt(struct kunit *test, + long long expected); +struct mock_param_matcher *kunit_longlong_ge(struct kunit *test, + long long expected); +struct mock_param_matcher *kunit_longlong_gt(struct kunit *test, + long long expected); + +struct mock_param_matcher *kunit_ulonglong_eq(struct kunit *test, + unsigned long long expected); +struct mock_param_matcher *kunit_ulonglong_ne(struct kunit *test, + unsigned long long expected); +struct mock_param_matcher *kunit_ulonglong_le(struct kunit *test, + unsigned long long expected); +struct mock_param_matcher *kunit_ulonglong_lt(struct kunit *test, + unsigned long long expected); +struct mock_param_matcher *kunit_ulonglong_ge(struct kunit *test, + unsigned long long expected); +struct mock_param_matcher *kunit_ulonglong_gt(struct kunit *test, + unsigned long long expected); + +/* Matches pointers. */ +struct mock_param_matcher *kunit_ptr_eq(struct kunit *test, void *expected); +struct mock_param_matcher *kunit_ptr_ne(struct kunit *test, void *expected); +struct mock_param_matcher *kunit_ptr_lt(struct kunit *test, void *expected); +struct mock_param_matcher *kunit_ptr_le(struct kunit *test, void *expected); +struct mock_param_matcher *kunit_ptr_gt(struct kunit *test, void *expected); +struct mock_param_matcher *kunit_ptr_ge(struct kunit *test, void *expected); + +/* Matches memory sections and strings. */ +struct mock_param_matcher *kunit_memeq(struct kunit *test, + const void *buf, + size_t size); +struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str); + +struct mock_action *kunit_u8_return(struct kunit *test, u8 ret); +struct mock_action *kunit_u16_return(struct kunit *test, u16 ret); +struct mock_action *kunit_u32_return(struct kunit *test, u32 ret); +struct mock_action *kunit_u64_return(struct kunit *test, u64 ret); +struct mock_action *kunit_char_return(struct kunit *test, char ret); +struct mock_action *kunit_uchar_return(struct kunit *test, unsigned char ret); +struct mock_action *kunit_schar_return(struct kunit *test, signed char ret); +struct mock_action *kunit_short_return(struct kunit *test, short ret); +struct mock_action *kunit_ushort_return(struct kunit *test, unsigned short ret); +struct mock_action *kunit_int_return(struct kunit *test, int ret); +struct mock_action *kunit_uint_return(struct kunit *test, unsigned int ret); +struct mock_action *kunit_long_return(struct kunit *test, long ret); +struct mock_action *kunit_ulong_return(struct kunit *test, unsigned long ret); +struct mock_action *kunit_longlong_return(struct kunit *test, long long ret); +struct mock_action *kunit_ulonglong_return(struct kunit *test, + unsigned long long ret); +struct mock_action *kunit_ptr_return(struct kunit *test, void *ret); + #endif /* _KUNIT_MOCK_H */ diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index a51157970502..a7a3c5e0a8bf 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_KUNIT) += kunit.o kunit-objs += test.o \ mock.o \ + common-mocks.o \ string-stream.o \ assert.o \ kunit-stream.o \ diff --git a/lib/kunit/common-mocks.c b/lib/kunit/common-mocks.c new file mode 100644 index 000000000000..b3a410fda63a --- /dev/null +++ b/lib/kunit/common-mocks.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Common KUnit mock call arg matchers and formatters. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include + +static bool match_any(struct mock_param_matcher *pmatcher, + struct kunit_stream *stream, + const void *actual) +{ + kunit_stream_add(stream, "don't care"); + return true; +} + +static struct mock_param_matcher any_matcher = { + .match = match_any, +}; + +struct mock_param_matcher *kunit_any(struct kunit *test) +{ + return &any_matcher; +} + +#define DEFINE_MATCHER_STRUCT(type_name, type) \ + struct mock_##type_name##_matcher { \ + struct mock_param_matcher matcher; \ + type expected; \ + } + +#define DEFINE_TO_MATCHER_STRUCT(type_name) \ + struct mock_##type_name##_matcher * \ + to_mock_##type_name##_matcher( \ + struct mock_param_matcher *matcher) \ + { \ + return container_of(matcher, \ + struct mock_##type_name##_matcher, \ + matcher); \ + } + +#define DEFINE_MATCH_FUNC(type_name, type, op_name, op) \ + bool match_##type_name##_##op_name( \ + struct mock_param_matcher *pmatcher, \ + struct kunit_stream *stream, \ + const void *pactual) \ + { \ + struct mock_##type_name##_matcher *matcher = \ + to_mock_##type_name##_matcher(pmatcher); \ + type actual = *((type *) pactual); \ + bool matches = actual op matcher->expected; \ + \ + if (matches) \ + kunit_stream_add(stream, \ + "%d "#op" %d", \ + actual, \ + matcher->expected); \ + else \ + kunit_stream_add(stream, \ + "%d not "#op" %d", \ + actual, \ + matcher->expected); \ + \ + return matches; \ + } + +#define DEFINE_MATCH_FACTORY(type_name, type, op_name) \ + struct mock_param_matcher *kunit_##type_name##_##op_name( \ + struct kunit *test, type expected) \ + { \ + struct mock_##type_name##_matcher *matcher; \ + \ + matcher = kunit_kmalloc(test, \ + sizeof(*matcher), \ + GFP_KERNEL); \ + if (!matcher) \ + return NULL; \ + \ + matcher->matcher.match = match_##type_name##_##op_name;\ + matcher->expected = expected; \ + return &matcher->matcher; \ + } + +#define DEFINE_MATCHER_WITH_TYPENAME(type_name, type) \ + DEFINE_MATCHER_STRUCT(type_name, type); \ + DEFINE_TO_MATCHER_STRUCT(type_name) \ + DEFINE_MATCH_FUNC(type_name, type, eq, ==) \ + DEFINE_MATCH_FACTORY(type_name, type, eq) \ + DEFINE_MATCH_FUNC(type_name, type, ne, !=) \ + DEFINE_MATCH_FACTORY(type_name, type, ne) \ + DEFINE_MATCH_FUNC(type_name, type, le, <=) \ + DEFINE_MATCH_FACTORY(type_name, type, le) \ + DEFINE_MATCH_FUNC(type_name, type, lt, <) \ + DEFINE_MATCH_FACTORY(type_name, type, lt) \ + DEFINE_MATCH_FUNC(type_name, type, ge, >=) \ + DEFINE_MATCH_FACTORY(type_name, type, ge) \ + DEFINE_MATCH_FUNC(type_name, type, gt, >) \ + DEFINE_MATCH_FACTORY(type_name, type, gt) + +#define DEFINE_MATCHER(type) DEFINE_MATCHER_WITH_TYPENAME(type, type) + +DEFINE_MATCHER(u8); +DEFINE_MATCHER(u16); +DEFINE_MATCHER(u32); +DEFINE_MATCHER(u64); +DEFINE_MATCHER(char); +DEFINE_MATCHER_WITH_TYPENAME(uchar, unsigned char); +DEFINE_MATCHER_WITH_TYPENAME(schar, signed char); +DEFINE_MATCHER(short); +DEFINE_MATCHER_WITH_TYPENAME(ushort, unsigned short); +DEFINE_MATCHER(int); +DEFINE_MATCHER_WITH_TYPENAME(uint, unsigned int); +DEFINE_MATCHER(long); +DEFINE_MATCHER_WITH_TYPENAME(ulong, unsigned long); +DEFINE_MATCHER_WITH_TYPENAME(longlong, long long); +DEFINE_MATCHER_WITH_TYPENAME(ulonglong, unsigned long long); + +DEFINE_MATCHER_WITH_TYPENAME(ptr, void *); + +struct mock_memeq_matcher { + struct mock_param_matcher matcher; + const void *expected; + size_t size; +}; + +static bool match_memeq(struct mock_param_matcher *pmatcher, + struct kunit_stream *stream, + const void *pactual) +{ + struct mock_memeq_matcher *matcher = + container_of(pmatcher, + struct mock_memeq_matcher, + matcher); + const void *actual = CONVERT_TO_ACTUAL_TYPE(const void *, pactual); + bool matches = !memcmp(actual, matcher->expected, matcher->size); + int i; + + for (i = 0; i < matcher->size; i++) + kunit_stream_add(stream, "%02x, ", ((const char *) actual)[i]); + if (matches) + kunit_stream_add(stream, "== "); + else + kunit_stream_add(stream, "!= "); + for (i = 0; i < matcher->size; i++) + kunit_stream_add(stream, + "%02x, ", + ((const char *) matcher->expected)[i]); + + return matches; +} + +struct mock_param_matcher *kunit_memeq(struct kunit *test, + const void *buf, + size_t size) +{ + struct mock_memeq_matcher *matcher; + + matcher = kunit_kzalloc(test, sizeof(*matcher), GFP_KERNEL); + if (!matcher) + return NULL; + + matcher->matcher.match = match_memeq; + matcher->expected = buf; + matcher->size = size; + + return &matcher->matcher; +} + +struct mock_streq_matcher { + struct mock_param_matcher matcher; + const char *expected; +}; + +static bool match_streq(struct mock_param_matcher *pmatcher, + struct kunit_stream *stream, + const void *pactual) +{ + struct mock_streq_matcher *matcher = + container_of(pmatcher, + struct mock_streq_matcher, + matcher); + const char *actual = CONVERT_TO_ACTUAL_TYPE(const char *, pactual); + bool matches = !strcmp(actual, matcher->expected); + + if (matches) + kunit_stream_add(stream, "%s == %s", actual, matcher->expected); + else + kunit_stream_add(stream, "%s != %s", actual, matcher->expected); + + return matches; +} + +struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str) +{ + struct mock_streq_matcher *matcher; + + matcher = kunit_kzalloc(test, sizeof(*matcher), GFP_KERNEL); + if (!matcher) + return NULL; + + matcher->matcher.match = match_streq; + matcher->expected = str; + + return &matcher->matcher; +} + +#define DEFINE_RETURN_ACTION_STRUCT(type_name, type) \ + struct mock_##type_name##_action { \ + struct mock_action action; \ + type ret; \ + } + +#define DEFINE_RETURN_ACTION_FUNC(type_name, type) \ + void *do_##type_name##_return(struct mock_action *paction, \ + const void **params, \ + int len) \ + { \ + struct mock_##type_name##_action *action = \ + container_of(paction, \ + struct mock_##type_name##_action,\ + action); \ + \ + return (void *) &action->ret; \ + } + +#define DEFINE_RETURN_ACTION_FACTORY(type_name, type) \ + struct mock_action *kunit_##type_name##_return( \ + struct kunit *test, \ + type ret) \ + { \ + struct mock_##type_name##_action *action; \ + \ + action = kunit_kmalloc(test, \ + sizeof(*action), \ + GFP_KERNEL); \ + if (!action) \ + return NULL; \ + \ + action->action.do_action = do_##type_name##_return; \ + action->ret = ret; \ + \ + return &action->action; \ + } + +#define DEFINE_RETURN_ACTION_WITH_TYPENAME(type_name, type) \ + DEFINE_RETURN_ACTION_STRUCT(type_name, type); \ + DEFINE_RETURN_ACTION_FUNC(type_name, type); \ + DEFINE_RETURN_ACTION_FACTORY(type_name, type) + +#define DEFINE_RETURN_ACTION(type) \ + DEFINE_RETURN_ACTION_WITH_TYPENAME(type, type) + +DEFINE_RETURN_ACTION(u8); +DEFINE_RETURN_ACTION(u16); +DEFINE_RETURN_ACTION(u32); +DEFINE_RETURN_ACTION(u64); +DEFINE_RETURN_ACTION(char); +DEFINE_RETURN_ACTION_WITH_TYPENAME(uchar, unsigned char); +DEFINE_RETURN_ACTION_WITH_TYPENAME(schar, signed char); +DEFINE_RETURN_ACTION(short); +DEFINE_RETURN_ACTION_WITH_TYPENAME(ushort, unsigned short); +DEFINE_RETURN_ACTION(int); +DEFINE_RETURN_ACTION_WITH_TYPENAME(uint, unsigned int); +DEFINE_RETURN_ACTION(long); +DEFINE_RETURN_ACTION_WITH_TYPENAME(ulong, unsigned long); +DEFINE_RETURN_ACTION_WITH_TYPENAME(longlong, long long); +DEFINE_RETURN_ACTION_WITH_TYPENAME(ulonglong, unsigned long long); +DEFINE_RETURN_ACTION_WITH_TYPENAME(ptr, void *); From patchwork Fri Sep 18 18:31:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 255190 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF36AC43465 for ; Fri, 18 Sep 2020 18:32:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 70DAD21D7F for ; Fri, 18 Sep 2020 18:32:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="Hfm78dE1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726463AbgIRScV (ORCPT ); Fri, 18 Sep 2020 14:32:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726455AbgIRScT (ORCPT ); Fri, 18 Sep 2020 14:32:19 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9AB6C0613CF for ; Fri, 18 Sep 2020 11:32:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e190so6361190ybf.18 for ; Fri, 18 Sep 2020 11:32:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=F8bCD50iXV9Z9O4p1imguTXbT98l8nZxhgs6nNCkXG8=; b=Hfm78dE172Bx2rF63Uymcxe4biCYv59pvXc023t2uHFLXXMAgvFNHMVRENNWWCMJW/ MKjbSKtWM1jwd5I671X7QHa0DzzlUl0PuaA0lByuA3q2Jrxmz0o47pjjWDcl3FWy5EVS RYxrsjHT8ZvUUxYRPqslr5/3Ijwq8XMao1QB0jijeMzY2sfWOo4i3mWylwhfowC5AjKN PfWJilbs6WSPnBhVNzZIMZrcajkHObXDJ7yWO02Ck9hINznoRTtph1g/02qYHE4gZ640 eG+12TDdDn+9yv48/2XuFhF6kb+KZAOdmPI3pVYu8IBGByB8vsGgVrAsGM5Uq+E79cBb UQ5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=F8bCD50iXV9Z9O4p1imguTXbT98l8nZxhgs6nNCkXG8=; b=USnOcT/YWB+Q2endy3MaKTA4SUDWRIY3nkaWA6TZZ7EnLsYA0LPjJ5KtDu7tMcZEoH 4a/11egaode54Tt8AKfCSVzZJ2QmzqbMU4CYQ7NHNeQ2k4rcRFr6m4hy+DVnYfcdH8d2 EjEmdbHq9ranxvPdtPHQEVmJpLlUoYM1yRSRHex8lzllIVDljbn+ceG7FHcDbLxuHzXC D2DoD2YO9qb87W5OULVZuVaZy7bfA+ImQ1Avxov56QhIpTOsRR0X09YgJdfybfe9vEaI nr85L4YVlw15Z4dsiPMT6R2I1QRExa80m8xWYCtNK2AX0Zt6tjIDj6G8qImK+faJkwFj JZYw== X-Gm-Message-State: AOAM532RJ08Odf9MFYjnOukQdrQJO3+WcO+T8wdBDZBB+1YjdjZQwXVk F0D2Z1SwR+NczqXkMwv9Aq3Uyt3iewAP3w== X-Google-Smtp-Source: ABdhPJzerFWj9XJaQ8C3lUgEGIFO/KfPfdNhnInTinPShmgihj+GaD+6/hG7gTdYeNYLVeZz/6kl0kHZm3XKvA== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a25:cb88:: with SMTP id b130mr43488480ybg.521.1600453937975; Fri, 18 Sep 2020 11:32:17 -0700 (PDT) Date: Fri, 18 Sep 2020 11:31:14 -0700 In-Reply-To: <20200918183114.2571146-1-dlatypov@google.com> Message-Id: <20200918183114.2571146-13-dlatypov@google.com> Mime-Version: 1.0 References: <20200918183114.2571146-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [RFC v1 12/12] kunit: mock: implement nice, strict and naggy mock distinctions From: Daniel Latypov To: Brendan Higgins , David Gow , Shuah Khan Cc: kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Alan Maguire , Stephen Boyd , Felix Guo , Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Nice mocks only fail when there is an expectation on a method, but none match a given call. Strict mocks only pass when there is a matching expectation for every call. Naggy mocks have the same pass/fail behavior as nice, but report a warning in any case a strict mock would fail. Signed-off-by: Felix Guo Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- include/kunit/mock.h | 63 ++++++++++++++++ lib/kunit/mock-test.c | 171 ++++++++++++++++++++++++++++++++++++++++++ lib/kunit/mock.c | 10 ++- 3 files changed, 242 insertions(+), 2 deletions(-) diff --git a/include/kunit/mock.h b/include/kunit/mock.h index 4a03f0627afa..955c4267d726 100644 --- a/include/kunit/mock.h +++ b/include/kunit/mock.h @@ -95,10 +95,17 @@ struct mock_method { struct list_head expectations; }; +enum mock_type { + MOCK_TYPE_NICE, + MOCK_TYPE_NAGGY, + MOCK_TYPE_STRICT +}; + struct mock { struct kunit_post_condition parent; struct kunit *test; struct list_head methods; + enum mock_type type; /* TODO(brendanhiggins@google.com): add locking to do_expect. */ const void *(*do_expect)(struct mock *mock, const char *method_name, @@ -108,6 +115,8 @@ struct mock { int len); }; +#define DEFAULT_MOCK_TYPE MOCK_TYPE_NAGGY + void mock_init_ctrl(struct kunit *test, struct mock *mock); void mock_validate_expectations(struct mock *mock); @@ -125,6 +134,60 @@ struct mock_expectation *mock_add_matcher(struct mock *mock, #define MOCK(name) name##_mock +/** + * STRICT_MOCK() - sets the mock to be strict and returns the mock + * @mock: the mock + * + * For an example, see ``The Nice, the Strict, and the Naggy`` under + * ``Using KUnit``. + */ +#define STRICT_MOCK(mock) \ +({ \ + mock_get_ctrl(mock)->type = MOCK_TYPE_STRICT; \ + mock; \ +}) + +static inline bool is_strict_mock(struct mock *mock) +{ + return mock->type == MOCK_TYPE_STRICT; +} + +/** + * NICE_MOCK() - sets the mock to be nice and returns the mock + * @mock: the mock + * + * For an example, see ``The Nice, the Strict, and the Naggy`` under + * ``Using KUnit``. + */ +#define NICE_MOCK(mock) \ +({ \ + mock_get_ctrl(mock)->type = MOCK_TYPE_NICE; \ + mock; \ +}) + +static inline bool is_nice_mock(struct mock *mock) +{ + return mock->type == MOCK_TYPE_NICE; +} + +/** + * NAGGY_MOCK() - sets the mock to be naggy and returns the mock + * @mock: the mock + * + * For an example, see ``The Nice, the Strict, and the Naggy`` under + * ``Using KUnit``. + */ +#define NAGGY_MOCK(mock) \ +({ \ + mock_get_ctrl(mock)->type = MOCK_TYPE_NAGGY; \ + mock; \ +}) + +static inline bool is_naggy_mock(struct mock *mock) +{ + return mock->type == MOCK_TYPE_NAGGY; +} + /** * KUNIT_EXPECT_CALL() - Declares a *call expectation* on a mock function. * @expectation_call: a mocked method or function with parameters replaced with diff --git a/lib/kunit/mock-test.c b/lib/kunit/mock-test.c index df0969b43ade..1c2aa2aa9c1b 100644 --- a/lib/kunit/mock-test.c +++ b/lib/kunit/mock-test.c @@ -243,6 +243,50 @@ static void mock_test_do_expect_default_return(struct kunit *test) KUNIT_EXPECT_EQ(test, 0, expectation->times_called); } +/** + * DOC: Testing the failure condition of different mock types. + * + * The following tests will test the behaviour of expectations under different + * conditions. For example, what happens when an expectation: + * - is not satisfied at the end of the test + * - is fulfilled but the expected function is called again + * - a function is called without expectations set on it + * + * For each of these conditions, there may be variations between the different + * types of mocks: nice mocks, naggy mocks (the default) and strict mocks. + * + * More information about these mocks can be found in the kernel documentation + * under Documentation/test/api/class-and-function-mocking + */ + +/* Method called on strict mock with no expectations will fail */ +static void mock_test_strict_no_expectations_will_fail(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + int param0 = 5, param1 = -5; + static const char * const two_param_types[] = {"int", "int"}; + const void *two_params[] = {¶m0, ¶m1}; + + mock->type = MOCK_TYPE_STRICT; + + mock_set_default_action(mock, + "add", + mock_add, + kunit_int_return(failing_test, -4)); + + mock->do_expect(mock, + "add", + mock_add, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + mock_validate_expectations(mock); + + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + /* * Method called on naggy mock with no expectations will not fail, but will show * a warning message @@ -257,6 +301,8 @@ static void mock_test_naggy_no_expectations_no_fail(struct kunit *test) static const char * const two_param_types[] = {"int", "int"}; const void *two_params[] = {¶m0, ¶m1}; + mock->type = MOCK_TYPE_NAGGY; + mock_set_default_action(mock, "add", real_add, @@ -282,6 +328,77 @@ static void mock_test_naggy_no_expectations_no_fail(struct kunit *test) KUNIT_EXPECT_FALSE(test, failing_test->success); } +/* Method called on nice mock with no expectations will do nothing. */ +static void mock_test_nice_no_expectations_do_nothing(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + int param0 = 5, param1 = -5; + static const char * const two_param_types[] = {"int", "int"}; + const void *two_params[] = {¶m0, ¶m1}; + + mock->type = MOCK_TYPE_NICE; + + mock->do_expect(mock, + "add", + mock_add, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + mock_validate_expectations(mock); + + KUNIT_EXPECT_TRUE(test, failing_test->success); +} + +/* Test that method called on a mock (of any type) with no matching expectations + * will fail test and print all the tried expectations. + */ +static void +run_method_called_but_no_matching_expectation_test(struct kunit *test, + enum mock_type mock_type) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + int param0 = 5, param1 = -5; + static const char * const two_param_types[] = {"int", "int"}; + const void *two_params[] = {¶m0, ¶m1}; + struct mock_param_matcher *two_matchers[] = { + kunit_int_eq(failing_test, 100), + kunit_int_eq(failing_test, 100) + }; + + mock_add_matcher(mock, "add", mock_add, two_matchers, + ARRAY_SIZE(two_matchers)); + + mock->type = mock_type; + + mock->do_expect(mock, "add", mock_add, two_param_types, two_params, + ARRAY_SIZE(two_params)); + + /* Even nice mocks should fail if there's an unmet expectation. */ + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + +static void mock_test_naggy_no_matching_expectations_fail(struct kunit *test) +{ + run_method_called_but_no_matching_expectation_test(test, + MOCK_TYPE_NAGGY); +} + +static void mock_test_strict_no_matching_expectations_fail(struct kunit *test) +{ + run_method_called_but_no_matching_expectation_test(test, + MOCK_TYPE_STRICT); +} + +static void mock_test_nice_no_matching_expectations_fail(struct kunit *test) +{ + run_method_called_but_no_matching_expectation_test(test, + MOCK_TYPE_NICE); +} + static void mock_test_mock_validate_expectations(struct kunit *test) { struct mock_test_context *ctx = test->priv; @@ -309,6 +426,54 @@ static void mock_test_mock_validate_expectations(struct kunit *test) KUNIT_EXPECT_FALSE(test, failing_test->success); } +static void mock_test_validate_clears_expectations(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + struct mock_param_matcher *matchers[] = { + kunit_int_eq(failing_test, 5), + kunit_int_eq(failing_test, -4) + }; + int param0 = 5, param1 = -4; + static const char * const two_param_types[] = {"int", "int"}; + const void *two_params[] = {¶m0, ¶m1}; + + struct mock_expectation *expectation; + + mock->type = MOCK_TYPE_STRICT; + + /* Add an arbitrary matcher for 0 calls */ + expectation = mock_add_matcher(mock, "add", mock_add, + matchers, ARRAY_SIZE(matchers)); + expectation->times_called = 0; + expectation->min_calls_expected = 0; + expectation->max_calls_expected = 0; + + /* Should have 0 calls and should clear the previous expectation */ + mock_validate_expectations(mock); + + /* Add a new matcher for 1 call */ + expectation = mock_add_matcher(mock, "add", mock_add, + matchers, ARRAY_SIZE(matchers)); + expectation->times_called = 0; + expectation->min_calls_expected = 1; + expectation->max_calls_expected = 1; + + /* Satisfy previous matcher */ + mock->do_expect(mock, "add", mock_add, two_param_types, two_params, + ARRAY_SIZE(two_params)); + + /* + * Validate previous satisfy; if we didn't clear the previous + * expectation, it would fail the mock_test. + */ + mock_validate_expectations(mock); + + /* If all goes well, shouldn't fail the test. */ + KUNIT_EXPECT_TRUE(test, failing_test->success); +} + static int mock_test_init(struct kunit *test) { struct mock_test_context *ctx; @@ -346,7 +511,13 @@ static struct kunit_case mock_test_cases[] = { KUNIT_CASE(mock_test_failed_expect_call_fails_test), KUNIT_CASE(mock_test_do_expect_default_return), KUNIT_CASE(mock_test_mock_validate_expectations), + KUNIT_CASE(mock_test_strict_no_expectations_will_fail), KUNIT_CASE(mock_test_naggy_no_expectations_no_fail), + KUNIT_CASE(mock_test_nice_no_expectations_do_nothing), + KUNIT_CASE(mock_test_strict_no_matching_expectations_fail), + KUNIT_CASE(mock_test_naggy_no_matching_expectations_fail), + KUNIT_CASE(mock_test_nice_no_matching_expectations_fail), + KUNIT_CASE(mock_test_validate_clears_expectations), {} }; diff --git a/lib/kunit/mock.c b/lib/kunit/mock.c index 12fb88899451..f1fa7a5b9dd4 100644 --- a/lib/kunit/mock.c +++ b/lib/kunit/mock.c @@ -85,6 +85,7 @@ void mock_init_ctrl(struct kunit *test, struct mock *mock) mock->test = test; INIT_LIST_HEAD(&mock->methods); mock->do_expect = mock_do_expect; + mock->type = DEFAULT_MOCK_TYPE; mock->parent.validate = mock_validate_wrapper; list_add_tail(&mock->parent.node, &test->post_conditions); } @@ -283,7 +284,12 @@ static struct mock_expectation *mock_apply_expectations( mock_add_method_expectation_error(test, stream, "Method was called with no expectations declared: ", mock, method, type_names, params, len); - kunit_stream_commit(stream); + if (is_strict_mock(mock)) + fail_and_flush(test, stream); + else if (is_naggy_mock(mock)) + kunit_stream_commit(stream); + else + kunit_stream_clear(stream); return NULL; } @@ -313,7 +319,7 @@ static struct mock_expectation *mock_apply_expectations( } } - if (expectations_all_saturated) { + if (expectations_all_saturated && !is_nice_mock(mock)) { mock_add_method_expectation_error(test, stream, "Method was called with fully saturated expectations: ", mock, method, type_names, params, len);