From patchwork Mon Oct 12 22:20:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269658 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 82298C433E7 for ; Mon, 12 Oct 2020 22:21:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2065A20878 for ; Mon, 12 Oct 2020 22:21:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NaQP/YeJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388832AbgJLWVT (ORCPT ); Mon, 12 Oct 2020 18:21:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388682AbgJLWVT (ORCPT ); Mon, 12 Oct 2020 18:21: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 EBE5CC0613D0 for ; Mon, 12 Oct 2020 15:21:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id m62so15236193ybb.6 for ; Mon, 12 Oct 2020 15:21: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=m35WjvIMdsTWxP1ssovUOtDU6uHY4X6slQ9fwWQxET4=; b=NaQP/YeJkoi5eV1MeWEAwbMUYB5TURM2+Y6Z+GZflVYGZdW6GZFAyFOFWdURquNvaC s4eBulnfXFK6ABrnQ3sNdHObLnFdfR3+0nEi6aRJjNTKrMAvM/SJiRdO8PGgIsYKlsp5 Rdp7s87VhU9ZZ8im6ZN0X14ESo9Ms1KeYIyVEkVxDYxpGDM7hAxzvmORewzhaFzCxrHy FkK7nNddYd5m1+AICMJL8InDuvlF0nKtaL1XYcb8Yxe33gjOAbAsvsfdfnEoA9SVh+ZH b7SXzJ8TJUGvBRGdAV0pNabOMpboYcHN8JvKWZA699A8bt4BWwVlJzbiCzB0YmKn7iqU 6nOw== 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=m35WjvIMdsTWxP1ssovUOtDU6uHY4X6slQ9fwWQxET4=; b=kmFaRZ1hQ8yOoDautMYbOmDNm4HVDKSVK7sj487yU5pkjVvX7pUvOAag4s/mKQfSSB VlPlogj+gnNOU7fHg0PfREBdvQPBwbcbKysVLyLwi6bT8hnqILiecwBLh/v1pWlraxQC ElgoKPMY8ZHmmMYL1dAmPMayt99xI/kl6cv835OqU6bpCv6llCPpVu1guwePjblCOqx7 mPRF7xcCyC7S5mrugVMQjgmltz99Xv8QCtkVdio0ZmtEaMJoEiGbcNnp2SkZNdFP7+Bd yn248xALldoIJdLU6TXNgalp4TUuhkg1rxasM03JUXISGvmQHNfnSiF4yAp0pyCPH24G WSMg== X-Gm-Message-State: AOAM530Z7ukSdtPwtdGPsI8dAT4crGs9KeYN3oH9PIjQnGJlDYyDHU/K HtTGrs56YtO2mobjUuePVdo9XaZrLh4Sqw== X-Google-Smtp-Source: ABdhPJxW/TlitWaP/RAp/cQMzpuTJpAfvjMr44SwofFBJS5F7J5XIDQObKaQdZInIkHIk1F0/yPBRKks+udEFA== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a25:6585:: with SMTP id z127mr8041680ybb.33.1602541278166; Mon, 12 Oct 2020 15:21:18 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:39 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-2-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 01/12] Revert "kunit: move string-stream.h to lib/kunit" From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This reverts commit 109fb06fdc6f6788df7dfbc235f7636a38e28fd4. string-stream will be used by kunit mocking code to print messages about mock expectations. It makes the code signifcantly simpler if string-stream objects can be part of structs declared in mocking headers. Signed-off-by: Daniel Latypov --- include/kunit/assert.h | 3 +-- {lib => include}/kunit/string-stream.h | 0 lib/kunit/assert.c | 2 -- lib/kunit/string-stream-test.c | 3 +-- lib/kunit/string-stream.c | 3 +-- lib/kunit/test.c | 2 +- 6 files changed, 4 insertions(+), 9 deletions(-) rename {lib => include}/kunit/string-stream.h (100%) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index ad889b539ab3..db6a0fca09b4 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -9,11 +9,10 @@ #ifndef _KUNIT_ASSERT_H #define _KUNIT_ASSERT_H +#include #include -#include struct kunit; -struct string_stream; /** * enum kunit_assert_type - Type of expectation/assertion. diff --git a/lib/kunit/string-stream.h b/include/kunit/string-stream.h similarity index 100% rename from lib/kunit/string-stream.h rename to include/kunit/string-stream.h diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 33acdaa28a7d..9c12e30792ba 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -8,8 +8,6 @@ #include #include -#include "string-stream.h" - void kunit_base_assert_format(const struct kunit_assert *assert, struct string_stream *stream) { diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 110f3a993250..121f9ab11501 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -6,11 +6,10 @@ * Author: Brendan Higgins */ +#include #include #include -#include "string-stream.h" - static void string_stream_test_empty_on_creation(struct kunit *test) { struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 141789ca8949..151a0e7ac349 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -6,12 +6,11 @@ * Author: Brendan Higgins */ +#include #include #include #include -#include "string-stream.h" - struct string_stream_fragment_alloc_context { struct kunit *test; int len; diff --git a/lib/kunit/test.c b/lib/kunit/test.c index c36037200310..670d1cc9c105 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -7,12 +7,12 @@ */ #include +#include #include #include #include #include "debugfs.h" -#include "string-stream.h" #include "try-catch-impl.h" static void kunit_set_failure(struct kunit *test) From patchwork Mon Oct 12 22:20:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269657 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 A0BCCC433E7 for ; Mon, 12 Oct 2020 22:21:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E98D2078E for ; Mon, 12 Oct 2020 22:21:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="d7htklom" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388932AbgJLWV1 (ORCPT ); Mon, 12 Oct 2020 18:21:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388899AbgJLWV0 (ORCPT ); Mon, 12 Oct 2020 18:21:26 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E150C0613D7 for ; Mon, 12 Oct 2020 15:21:25 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id c4so7844172qtx.20 for ; Mon, 12 Oct 2020 15:21:25 -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=m+wXYoLHV4fcV086/vT+cUUJr23s1Z5LPv/l64PtKCI=; b=d7htklom5TFv6EMI3PZltokIliW9RMyvHFmExRX41mok5bQ/YPtG0DS43P6FqJRjkf PKmdYO1HDNseK2x6SodFYw6dpaEdCiuI5m1Xowu7+ylmblXVkhQB14RHtXZ8Lxeh15TV wdlHucQPL0jHwyX8zhj6zaWmPasMJq6NbvknN5OI88ky7Da/3k2/DwBw0pSK6JOyY31D 9Z3rW64klIEZM4EixkH9efW6wuSE7Od9vaeWffaA+u+9aPkiJCBLWk/Czxqu41zCswrA LN98UYdnOwoD2DChAfebVc5dO8jcYNHCyNlTaYB1x54GyTyHXaAUdQW3wfnL86Eo8TmW TiPw== 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=m+wXYoLHV4fcV086/vT+cUUJr23s1Z5LPv/l64PtKCI=; b=UN3UeZVyMmBj9JnuEZ8VE6WoYzAwL87fy5ooxMUGzsDS9rapvnJirT/8A3YzGWvqP2 faXyYCS2QlJYxEyzaTeB5Xg6WyBCt1Ab5k9iyu+e9zr+emc1IF8V9LzxzJP4gVGA/H/s M8mwJT5k9lhnA2avA179ePGmLq/Lt7+Jo7SvpqzSJwoIRQYmonJ1NfkJJMOrgislcwXv 6DY+0KbcRMeaOG0tirm7HlBZgeg3/iAA99GHeNtD0p/QrHMRmHUuuUaijmZ4H1mqp08o BIpi9AhTDwzUYMm6zNqjzR+/jp7zV3reowrT2ujlPGTPg7sKc3yBgQAR4TLFlu63LY1Q 8SWw== X-Gm-Message-State: AOAM532ZXu3WU8+7YqBfZcYOlOFv7TUNGXl4VLAyJO2lJmOTsmvA7FyV iLILfTDf4lfmpLZxZx/7HY3DGtUb1spJ4Q== X-Google-Smtp-Source: ABdhPJz0hdbpme4RcxU2sxTA1uUQP9aATQVz6BRY34uV4A3z60lr9fQwkjBGu5uuB30/E3kCnI0wi7Pwf5Vi6w== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a0c:a085:: with SMTP id c5mr28431576qva.30.1602541284288; Mon, 12 Oct 2020 15:21:24 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:42 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-5-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 04/12] checkpatch: add support for struct MOCK(foo) syntax From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins KUnit will soon add macros for generating mocks from types, the generated mock types are named like `struct MOCK(foo)` (where the base type is struct foo). Add `struct MOCK(foo)` as a NonptrType so that it is recognized correctly in declarations. Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- scripts/checkpatch.pl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 504d2e431c60..b40a68e7bc25 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -796,6 +796,10 @@ sub build_types { (?: (?:typeof|__typeof__)\s*\([^\)]*\)| (?:$typeTypedefs\b)| + # Matching a \b breaks struct MOCK(foo) syntax, + # so we need to have it not lumped in with the + # types in @typeList. + (?:struct\s+MOCK\($Ident\))| (?:${all}\b) ) (?:\s+$Modifier|\s+const)* From patchwork Mon Oct 12 22:20:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269655 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT, USER_IN_DEF_DKIM_WL 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 30E6DC433DF for ; Mon, 12 Oct 2020 22:21:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D8C8C20797 for ; Mon, 12 Oct 2020 22:21:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="szzZCqZ6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388879AbgJLWV2 (ORCPT ); Mon, 12 Oct 2020 18:21:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388947AbgJLWV1 (ORCPT ); Mon, 12 Oct 2020 18:21:27 -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 2CEBBC0613D0 for ; Mon, 12 Oct 2020 15:21:27 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z3so12912607yba.21 for ; Mon, 12 Oct 2020 15:21:27 -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=B6scR+VlLRNFLgmyfaEV2UlB/ec1kTho3dHQ6KxaOc4=; b=szzZCqZ6YnlKRQC4DoN2/uYCR0GP8qVEWZNPD2tMzUE//qWxVozZIuFbNwY2Nra0sA ANDM8gagFPrZaYcN1X6ELrpJ4fIR6boZ6U4Ny9oU2Hal3M3eN/Oi52ggm05Jh99SR+J3 Kw63fM9VtyqQydpbOA/cKEqN5RT/iwTb1gIYaRNDtMSSzcptxU2TnK94WHmiK0V5TPqZ EI7KUOPYymkWfJ3ZCze676CmMl4Pa+BIgsgnLMQjMpQZGjZ7Eqc7cGjDYmC8tXropHNq cr5x6yHnnhVZHM8ON5pwg8LY63z7PmO82D734/CTUSTFZ6M0TkcjLw5JKpsDzzVanKAI 0RgA== 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=B6scR+VlLRNFLgmyfaEV2UlB/ec1kTho3dHQ6KxaOc4=; b=Wm2MnPrjh7B9wiLnq7gNqRzhLWjKrON9oNEOqtfF7oQ3w1K+qzTDy5GitpUB4m++g7 iKtwPi1KFDEx+se+znpMkiWOtwNFJG6jtWlODcdru630WVXcddr+8CpFml2ywpcHeX6L r/0nFCQ/NStP9Ko/TrVGa0fEN8cPJT9x9reGyBVLWCuvHg2PZASfab0V5iCGLocgaoTY rXo2CIxyhJg1mjONVpdm1IwotNI0t/SPbJpyDacavxQ5cQV/x8wViSZpuIo694kBQT+8 wpm/GwZiXEmuYpbl/tknHOXYtt/VebbErH/aPKiP0EfahdvAhC0VHXdHdElDGO0IUEy8 LGHA== X-Gm-Message-State: AOAM532PlWo0CK6WVAAnmGaM19i/KaPJ4P9YgYFu1zNU+H78dcnqLGAv Dz1HZdY0vSCQdb2/uBaE4kje+R7Oz+nopA== X-Google-Smtp-Source: ABdhPJy8zxCkqGgEjBL34sOeVTyTnXjEDr/D9qO3d9EV6+GVzM2APAl5MBgtW4VlYO9AkpRTErZV/BwPzseZdA== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a05:6902:514:: with SMTP id x20mr11824204ybs.111.1602541286356; Mon, 12 Oct 2020 15:21:26 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:43 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-6-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 05/12] kunit: mock: add parameter list manipulation macros From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Add macros for parsing and manipulating parameter lists needed for generating mocks. Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- include/kunit/params.h | 305 ++++++++++++++++++++++++++++++++++++ lib/kunit/Makefile | 3 +- lib/kunit/mock-macro-test.c | 150 ++++++++++++++++++ 3 files changed, 457 insertions(+), 1 deletion(-) create mode 100644 include/kunit/params.h create mode 100644 lib/kunit/mock-macro-test.c diff --git a/include/kunit/params.h b/include/kunit/params.h new file mode 100644 index 000000000000..06efcb58c2c0 --- /dev/null +++ b/include/kunit/params.h @@ -0,0 +1,305 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Macros for parsing and manipulating parameter lists needed for generating + * mocks. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#ifndef _KUNIT_PARAMS_H +#define _KUNIT_PARAMS_H + +#define NUM_VA_ARGS_IMPL(__dummy, \ + __1, \ + __2, \ + __3, \ + __4, \ + __5, \ + __6, \ + __7, \ + __8, \ + __9, \ + __10, \ + __11, \ + __12, \ + __13, \ + __14, \ + __15, \ + __16, \ + __nargs, args...) __nargs + +#define NUM_VA_ARGS(args...) NUM_VA_ARGS_IMPL(__dummy, ##args, \ + 16, \ + 15, \ + 14, \ + 13, \ + 12, \ + 11, \ + 10, \ + 9, \ + 8, \ + 7, \ + 6, \ + 5, \ + 4, \ + 3, \ + 2, \ + 1, \ + 0) + +#define CONCAT_INTERNAL(left, right) left##right +#define CONCAT(left, right) CONCAT_INTERNAL(left, right) + +#define EMPTY() + +/* + * Takes the name of a function style macro such as FOO() and prevents it from + * being evaluated on the current pass. + * + * This is useful when you need to write a "recursive" macro since a macro name + * becomes painted after it is pasted. If a different macro is pasted, this + * different macro won't be pasted; if we then defer the evaluation of the this + * "indirection macro", we can prevent the original definition from getting + * painted. + * + * Example: + * #define EXAMPLE EXPAND(FOO()) // FOO() is evaluated on 1st pass. + * #define EXAMPLE EXPAND(DEFER(FOO)()) // FOO() is evaluated on the second + * // pass. + */ +#define DEFER(macro_id) macro_id EMPTY() + +/* + * Takes the name of a function style macro such as FOO() and prevents it from + * being evaluated on the current or following pass. + * + * This is useful when you need to DEFER inside an operation which causes an + * extra pass, like IF. + * + * Example: + * #define EXAMPLE EXPAND(FOO()) // FOO() is evaluated on 1st pass. + * #define EXAMPLE EXPAND(DEFER(FOO)()) // FOO() is evaluated on the second + * // pass. + * #define EXAMPLE EXPAND(OBSTRUCT(FOO)()) // FOO() is evaluated on the third + * // pass. + */ +#define OBSTRUCT(macro_id) macro_id DEFER(EMPTY)() + +#define EXPAND_1(args...) args +#define EXPAND_2(args...) EXPAND_1(EXPAND_1(args)) +#define EXPAND_4(args...) EXPAND_2(EXPAND_2(args)) +#define EXPAND_8(args...) EXPAND_4(EXPAND_4(args)) +#define EXPAND_16(args...) EXPAND_8(EXPAND_8(args)) + +/* + * Causes multiple evaluation passes of a macro. + * + * CPP is implemented as a push down automaton. It consumes a stream of tokens + * and as it comes across macros, it either evaluates them and pastes the + * result, or if the macro is a function macro, it pushes the macro to a stack, + * it evaluates the input to the function macro, pops the state from the stack + * and continues. + * + * This macro serves the function of making the cursor return to the beginging + * of a macro that requires mulitple passes to evaluate. It is most useful when + * used with DEFER(...) and OBSTRUCT(...). + */ +#define EXPAND(args...) EXPAND_16(args) + +#define INC(id) INC_##id +#define INC_0 1 +#define INC_1 2 +#define INC_2 3 +#define INC_3 4 +#define INC_4 5 +#define INC_5 6 +#define INC_6 7 +#define INC_7 8 +#define INC_8 9 +#define INC_9 10 +#define INC_10 11 +#define INC_11 12 +#define INC_12 13 +#define INC_13 14 +#define INC_14 15 +#define INC_15 16 +#define INC_16 17 + +#define DEC(id) DEC_##id +#define DEC_1 0 +#define DEC_2 1 +#define DEC_3 2 +#define DEC_4 3 +#define DEC_5 4 +#define DEC_6 5 +#define DEC_7 6 +#define DEC_8 7 +#define DEC_9 8 +#define DEC_10 9 +#define DEC_11 10 +#define DEC_12 11 +#define DEC_13 12 +#define DEC_14 13 +#define DEC_15 14 +#define DEC_16 15 + +#define DROP_FIRST_ARG_INTERNAL(dropped, x, args...) x +#define DROP_FIRST_ARG(args...) DROP_FIRST_ARG_INTERNAL(args) + +#define EQUAL(left, right) EQUAL_##left##_##right +#define EQUAL_0_0 dropped, 1 +#define EQUAL_1_1 dropped, 1 +#define EQUAL_2_2 dropped, 1 +#define EQUAL_3_3 dropped, 1 +#define EQUAL_4_4 dropped, 1 +#define EQUAL_5_5 dropped, 1 +#define EQUAL_6_6 dropped, 1 +#define EQUAL_7_7 dropped, 1 +#define EQUAL_8_8 dropped, 1 +#define EQUAL_9_9 dropped, 1 +#define EQUAL_10_10 dropped, 1 +#define EQUAL_11_11 dropped, 1 +#define EQUAL_12_12 dropped, 1 +#define EQUAL_13_13 dropped, 1 +#define EQUAL_14_14 dropped, 1 +#define EQUAL_15_15 dropped, 1 +#define EQUAL_16_16 dropped, 1 + +#define IS_EQUAL(left, right) DROP_FIRST_ARG(EQUAL(left, right), 0) + +#define NOT_INTERNAL(condition) NOT_##condition +#define NOT(condition) NOT_INTERNAL(condition) +#define NOT_0 1 +#define NOT_1 0 + +#define IS_NOT_EQUAL(left, right) NOT(IS_EQUAL(left, right)) + +#define EMPTY_IMPL(tokens) CONCAT(EMPTY_, tokens) +#define IS_EMPTY(tokens) + +#define OR_INTERNAL(left, right) OR_##left##_##right +#define OR(left, right) OR_INTERNAL(left, right) +#define OR_0_0 0 +#define OR_0_1 1 +#define OR_1_0 1 +#define OR_1_1 1 + +#define IF(condition) CONCAT(IF_, condition) +#define IF_0(body) +#define IF_1(body) body + +#define COMMA() , + +#define APPLY_TOKENS_INTERNAL(tokens, yield_token, seen_token) \ + IF(yield_token)(IF(seen_token)(COMMA()) tokens) +#define APPLY_TOKENS(tokens, yield_token, seen_token) \ + APPLY_TOKENS_INTERNAL(tokens, yield_token, seen_token) + +/* + * Provides the indirection to keep the PARAM_LIST_RECURSE_INTERNAL from getting + * pasted, only useful if used with DEFER(...) or OBSTRUCT(...). + */ +#define PARAM_LIST_RECURSE_INDIRECT() PARAM_LIST_RECURSE_INTERNAL + +/* + * Given a starting index, a number of args, a MACRO to apply, and a list of + * types (with at least one element) this will call MACRO with the first type in + * the list and index; it will then call itself again on all remaining types, if + * any, while incrementing index, and decrementing nargs. + * + * Assumes nargs is the number of types in the list. + */ +#define PARAM_LIST_RECURSE_INTERNAL(index, \ + nargs, \ + MACRO, \ + FILTER, \ + context, \ + seen_token, \ + type, \ + args...) \ + APPLY_TOKENS(MACRO(context, type, index), \ + FILTER(context, type, index), \ + seen_token) \ + IF(IS_NOT_EQUAL(nargs, 1)) \ + (OBSTRUCT(PARAM_LIST_RECURSE_INDIRECT)() \ + (INC(index), DEC(nargs), \ + MACRO, FILTER, context, \ + OR(seen_token, FILTER(context, type, index)), \ + args)) + +#define PARAM_LIST_RECURSE(index, nargs, MACRO, FILTER, context, args...) \ + IF(IS_NOT_EQUAL(nargs, 0)) \ + (OBSTRUCT(PARAM_LIST_RECURSE_INTERNAL)(index, \ + nargs, \ + MACRO, \ + FILTER, \ + context, \ + 0, \ + args)) + +#define FILTER_NONE(context, type, index) 1 + +#define FILTER_INDEX_INTERNAL(index_to_drop, type, index) \ + IS_NOT_EQUAL(index, index_to_drop) +#define FILTER_INDEX(index_to_drop, type, index) \ + FILTER_INDEX_INTERNAL(index_to_drop, type, index) + +/* + * Applies a MACRO which takes a type and the index of the type and outputs a + * sequence of tokens to a list of types. + */ +#define FOR_EACH_PARAM(MACRO, FILTER, context, args...) \ + EXPAND(PARAM_LIST_RECURSE(0,\ + NUM_VA_ARGS(args),\ + MACRO,\ + FILTER,\ + context,\ + args)) + +#define PRODUCE_TYPE_AND_ARG(context, type, index) type arg##index +#define PARAM_LIST_FROM_TYPES(args...) \ + FOR_EACH_PARAM(PRODUCE_TYPE_AND_ARG, \ + FILTER_NONE, \ + not_used, \ + args) + +#define PRODUCE_TYPE_NAME(context, type, index) #type +#define TYPE_NAMES_FROM_TYPES(handle_index, args...) \ + FOR_EACH_PARAM(PRODUCE_TYPE_NAME, \ + FILTER_INDEX, \ + handle_index, \ + args) + +#define PRODUCE_PTR_TO_ARG(context, type, index) &arg##index +#define PTR_TO_ARG_FROM_TYPES(handle_index, args...) \ + FOR_EACH_PARAM(PRODUCE_PTR_TO_ARG, \ + FILTER_INDEX, \ + handle_index, \ + args) + +#define PRODUCE_MATCHER_AND_ARG(ctrl_index, type, index) \ + IF(IS_EQUAL(index, ctrl_index))(struct mock *arg##ctrl_index) \ + IF(IS_NOT_EQUAL(index, ctrl_index))( \ + struct mock_param_matcher *arg##index) +#define MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, args...) \ + FOR_EACH_PARAM(PRODUCE_MATCHER_AND_ARG, \ + FILTER_NONE, \ + ctrl_index, \ + args) + +#define PRODUCE_ARG(context, type, index) arg##index +#define ARG_NAMES_FROM_TYPES(ctrl_index, args...) \ + FOR_EACH_PARAM(PRODUCE_ARG, \ + FILTER_INDEX, \ + ctrl_index, \ + args) + +#define PRODUCE_ARRAY_ACCESSOR(context, type, index) *((type *) params[index]) +#define ARRAY_ACCESSORS_FROM_TYPES(args...) \ + FOR_EACH_PARAM(PRODUCE_ARRAY_ACCESSOR, \ + FILTER_NONE, \ + not_used, \ + args) + +#endif /* _KUNIT_PARAMS_H */ diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 523a7b0f9783..1707660c8b1c 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -10,7 +10,8 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y) kunit-objs += debugfs.o endif -obj-$(CONFIG_KUNIT_TEST) += kunit-test.o +obj-$(CONFIG_KUNIT_TEST) += kunit-test.o \ + mock-macro-test.o # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) diff --git a/lib/kunit/mock-macro-test.c b/lib/kunit/mock-macro-test.c new file mode 100644 index 000000000000..6c3dc2193edb --- /dev/null +++ b/lib/kunit/mock-macro-test.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for parameter list parsing macros. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include + +#define TO_STR_INTERNAL(...) #__VA_ARGS__ +#define TO_STR(...) TO_STR_INTERNAL(__VA_ARGS__) + +static void mock_macro_is_equal(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "dropped, 1", TO_STR(EQUAL(1, 1))); + KUNIT_EXPECT_EQ(test, 1, DROP_FIRST_ARG(dropped, 1, 0)); + KUNIT_EXPECT_EQ(test, 0, DROP_FIRST_ARG(1, 0)); + KUNIT_EXPECT_EQ(test, 0, DROP_FIRST_ARG(EQUAL(1, 0), 0)); + KUNIT_EXPECT_EQ(test, 1, IS_EQUAL(1, 1)); + KUNIT_EXPECT_EQ(test, 0, IS_EQUAL(1, 0)); +} + +static void mock_macro_if(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "body", ""IF(1)("body")); + KUNIT_EXPECT_STREQ(test, "", ""IF(0)("body")); + KUNIT_EXPECT_STREQ(test, "body", ""IF(IS_EQUAL(1, 1))("body")); + KUNIT_EXPECT_STREQ(test, "", ""IF(IS_EQUAL(0, 1))("body")); +} + +static void mock_macro_apply_tokens(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "type", TO_STR(APPLY_TOKENS(type, 1, 0))); + KUNIT_EXPECT_STREQ(test, ", type", TO_STR(APPLY_TOKENS(type, 1, 1))); + KUNIT_EXPECT_STREQ(test, "", TO_STR(APPLY_TOKENS(type, 0, 1))); +} + +#define IDENTITY(context, type, index) type + +static void mock_macro_param_list_recurse(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "", TO_STR(PARAM_LIST_RECURSE(0, + 0, + IDENTITY, + FILTER_NONE, + not_used))); + KUNIT_EXPECT_STREQ(test, + "type", + TO_STR(EXPAND(PARAM_LIST_RECURSE(0, + 1, + IDENTITY, + FILTER_NONE, + not_used, + type)))); + KUNIT_EXPECT_STREQ(test, + "type0 , type1 , type2 , type3 , type4 , type5 , " + "type6 , type7 , type8 , type9 , type10 , type11 , " + "type12 , type13 , type14 , type15", + TO_STR(EXPAND(PARAM_LIST_RECURSE(0, 16, + IDENTITY, + FILTER_NONE, + not_used, + type0, type1, type2, + type3, type4, type5, + type6, type7, type8, + type9, type10, + type11, type12, + type13, type14, + type15)))); +} + +static void mock_macro_for_each_param(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, + "type0 , type1", + TO_STR(FOR_EACH_PARAM(IDENTITY, + FILTER_NONE, + not_used, + type0, + type1))); + KUNIT_EXPECT_STREQ(test, + "type1", + TO_STR(FOR_EACH_PARAM(IDENTITY, + FILTER_INDEX, + 0, + type0, + type1))); +} + +static void mock_macro_param_list_from_types_basic(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "", TO_STR(PARAM_LIST_FROM_TYPES())); + KUNIT_EXPECT_STREQ(test, "int arg0", + TO_STR(PARAM_LIST_FROM_TYPES(int))); + KUNIT_EXPECT_STREQ(test, "struct kunit_struct * arg0 , int arg1", + TO_STR(PARAM_LIST_FROM_TYPES(struct kunit_struct *, + int))); + KUNIT_EXPECT_STREQ(test, + "type0 arg0 , type1 arg1 , type2 arg2 , type3 arg3 , " + "type4 arg4 , type5 arg5 , type6 arg6 , type7 arg7 , " + "type8 arg8 , type9 arg9 , type10 arg10 , " + "type11 arg11 , type12 arg12 , type13 arg13 , " + "type14 arg14 , type15 arg15", + TO_STR(PARAM_LIST_FROM_TYPES(type0, type1, type2, + type3, type4, type5, + type6, type7, type8, + type9, type10, type11, + type12, type13, type14, + type15))); +} + +static void mock_macro_arg_names_from_types(struct kunit *test) +{ + KUNIT_EXPECT_STREQ(test, "", TO_STR(ARG_NAMES_FROM_TYPES(0))); + KUNIT_EXPECT_STREQ(test, "", TO_STR(ARG_NAMES_FROM_TYPES(0, int))); + KUNIT_EXPECT_STREQ(test, + "arg1", + TO_STR(ARG_NAMES_FROM_TYPES(0, + struct kunit_struct *, + int))); + KUNIT_EXPECT_STREQ(test, + "arg0 , arg1 , arg3 , arg4 , arg5 , arg6 , arg7 , " + "arg8 , arg9 , arg10 , arg11 , arg12 , arg13 , " + "arg14 , arg15", + TO_STR(ARG_NAMES_FROM_TYPES(2, type0, type1, type2, + type3, type4, type5, + type6, type7, type8, + type9, type10, type11, + type12, type13, type14, + type15))); +} + +static struct kunit_case mock_macro_test_cases[] = { + KUNIT_CASE(mock_macro_is_equal), + KUNIT_CASE(mock_macro_if), + KUNIT_CASE(mock_macro_apply_tokens), + KUNIT_CASE(mock_macro_param_list_recurse), + KUNIT_CASE(mock_macro_for_each_param), + KUNIT_CASE(mock_macro_param_list_from_types_basic), + KUNIT_CASE(mock_macro_arg_names_from_types), + {} +}; + +static struct kunit_suite mock_macro_test_suite = { + .name = "mock-macro-test", + .test_cases = mock_macro_test_cases, +}; +kunit_test_suite(mock_macro_test_suite); From patchwork Mon Oct 12 22:20:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269656 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 E5ECEC433DF for ; Mon, 12 Oct 2020 22:21:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 875192078E for ; Mon, 12 Oct 2020 22:21:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ivs1YHHK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389163AbgJLWVe (ORCPT ); Mon, 12 Oct 2020 18:21:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389144AbgJLWVd (ORCPT ); Mon, 12 Oct 2020 18:21:33 -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 21C8EC0613D1 for ; Mon, 12 Oct 2020 15:21:33 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id w189so13635449qkd.6 for ; Mon, 12 Oct 2020 15:21:33 -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=lhTgqNnBCcFeSSCXT5bxGdckZVGyCK2i4GVzSMfeTvk=; b=ivs1YHHKEJ7o4m2XgvGfAwBC+uufJsmxqDyWz6ad+Yq0J/g3YvoJEAva/40Ri79hsG yx0seI9cPo6ScOXyvx2azpbpLdRQybti3q9lmmYCWEX75SOayCEfz/KxC4N7RxnYDgxJ LI6n3SUW1bPo4Ei9rV/r4p00cv4gqvyCm+jS8VciiIBBuug09vd+p1C5aoULxIt+0Od+ QJ8v2MyFdx5d1NA0lNSI2Z9sRGJb0qSMSA1naJ29oR9EhC2onG1uqKQCFEQRDc7lfYEW onBXxZLTeMcd/1E3V41pRfoUhUi+VCyNTl7gl1OHVejOvhf8PrAdVNOgbSUzBuLfyZNi cmBQ== 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=lhTgqNnBCcFeSSCXT5bxGdckZVGyCK2i4GVzSMfeTvk=; b=BvDDWsCD/cpuLjas00Ke5NGx7nQEktJxuFQiuhytGENPHF+x2HPdkvxd18ivsaX/rT 1jH9/ReGQqui/VNXZcXZ1Vz5JRH91XCRGdjobvmW+v5dArb4aiH6cBc424H3o9iPpXgj Mga4u8XD+VTqejEAA9Ht3odz5vbeEa2VgMJ+mcjMJLB33jFEeyxVY7K945YZOQZkYCU3 ypFIq37U85MabAylt2QYqBbzxDi2lJeUMb03b0YYzGcgasRQ3vkknqg+7nGTAFuQwQJY PynaIV4O6tdkGYXxN2f3myJkC0ukAiJcEtRyTaidC47mTxq9sQbW8B6dLOkFWpSxj/pe VgRQ== X-Gm-Message-State: AOAM532cWHse4fa8E2e+Q3T5ZJxUbeXSym/hNBNTFqsLtSDpSEQQOPoe zYgelTtDe/utrDKtRj5RShFg0/af7nodWQ== X-Google-Smtp-Source: ABdhPJwf67ydbouZPYcZm2TatGXz8aktNbr0InjUXltjTku2nWbVvEqnVzdH9mJfaK2wY1yOuFwmibeSFsXjjQ== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a0c:f1cb:: with SMTP id u11mr26514822qvl.21.1602541292127; Mon, 12 Oct 2020 15:21:32 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:46 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-9-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 08/12] kunit: mock: add basic matchers and actions From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org 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..4d8a3c9d5f0f --- /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) \ + static 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 Mon Oct 12 22:20:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269654 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 1877EC433E7 for ; Mon, 12 Oct 2020 22:21:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AA47520838 for ; Mon, 12 Oct 2020 22:21:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BuFfT35z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389275AbgJLWVy (ORCPT ); Mon, 12 Oct 2020 18:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389253AbgJLWVi (ORCPT ); Mon, 12 Oct 2020 18:21:38 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA35C0613D5 for ; Mon, 12 Oct 2020 15:21:36 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id y7so13587681pff.20 for ; Mon, 12 Oct 2020 15:21:36 -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=KffvDXhEQlg+xfMX19WD6tWJJFOKWMb6Lrz+5Uzyol4=; b=BuFfT35z9H4qdomlMLMhZvb1sjna4IRPRXT+qFeaWCXAzZmtePZ/Fh9Vxv5NQVZx67 KIG2Rktqlc7u8sahaemTFP/Eyk0a1/KcgUhBPzvTV1QxvqIoEDKF6cZw/KN1HIXZx3BH uN5ChhSXgLomD1ucuyaF8+oIwtodtjW1DyKB2M+EyDJoB/AElxVkaBA09fkBhfB6aj77 q+hl42C9hsBcrTcbrqHh42FPrKZHfML1/4t1bSGidmWtXWcccOamOr6XUjO5FEkqKV0r DMOWnnmCEG7ZXg/YGK1NAJr8At+NzLsLiu/UDBvuyZErDStWWbHgSp3AP4iB4gbPdFEq f4xg== 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=KffvDXhEQlg+xfMX19WD6tWJJFOKWMb6Lrz+5Uzyol4=; b=k4hiQX7kolesAA0BLtNwvQKajsTZ7CEb7A6wLGg1C+e1gENTjDsaCYPxJjA1atYfvW v996BqcMzhdXi2pmly+XVR+tuJEPp33am8w+vRaFZFbgBIPbj7djiYgaiJHvQaGtpy6x bJrA3pylT8dVXPLfQNqR6iaqVqf1tlQqZj2PQ/sfvFuKWmBDgmveQqHLp5CNGO0KNLZd Xfa2+W24PCn/lF4MTVR9/krw1wCeXZB63q0f/5EFXURAZpQq1X9jETb9SiVij15JCJE+ ve9zxiPMxhp8kjb3aHG/c4K1bzrX4T7PZwFN/CaQxzeWUaxVMMXtvsRLAzJcpoaBtTcr Fxcw== X-Gm-Message-State: AOAM531C86lq/DrV+fgWjjXLMq0su2sEFNG8UiIutFc6xCtcY3w+KBc9 nedEk+F3obQLFx8MaTQ2rK9CPBLwGJ+mvA== X-Google-Smtp-Source: ABdhPJxqSEGlAC7GH81RuXLpynypIvywGZvOMgZpn0PWKicZlBwAJ9MadCsnQY+sTx3OPXBCTVfpXKAcfT/gPQ== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:a17:902:7d8d:b029:d3:95b9:68ed with SMTP id a13-20020a1709027d8db02900d395b968edmr25412255plm.28.1602541295836; Mon, 12 Oct 2020 15:21:35 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:48 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-11-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 10/12] kunit: mock: add class mocking support From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Introduce basic class mocking, the ability to automatically generate a Linux C-style class implementation whose behavior is controlled by test cases, which can also set expectations on when and how mocks are called. Co-developed-by: Daniel Latypov Signed-off-by: Daniel Latypov Signed-off-by: Brendan Higgins --- include/kunit/mock.h | 433 +++++++++++++++++++++++++++++++++ lib/kunit/Makefile | 5 +- lib/kunit/kunit-example-test.c | 98 ++++++++ lib/kunit/mock-macro-test.c | 91 +++++++ lib/kunit/mock-test.c | 320 ++++++++++++++++++++++++ 5 files changed, 945 insertions(+), 2 deletions(-) create mode 100644 lib/kunit/mock-test.c diff --git a/include/kunit/mock.h b/include/kunit/mock.h index 13fdeb8730b5..9252a0abd295 100644 --- a/include/kunit/mock.h +++ b/include/kunit/mock.h @@ -123,6 +123,439 @@ struct mock_expectation *mock_add_matcher(struct mock *mock, struct mock_param_matcher *matchers[], int len); +#define MOCK(name) name##_mock + +/** + * KUNIT_EXPECT_CALL() - Declares a *call expectation* on a mock function. + * @expectation_call: a mocked method or function with parameters replaced with + * matchers. + * + * Example: + * + * .. code-block:: c + * + * // Class to mock. + * struct example { + * int (*foo)(struct example *, int); + * }; + * + * // Define the mock. + * DECLARE_STRUCT_CLASS_MOCK_PREREQS(example); + * + * DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example), + * RETURNS(int), + * PARAMS(struct example *, int)); + * + * static int example_init(struct MOCK(example) *mock_example) + * { + * struct example *example = mock_get_trgt(mock_example); + * + * example->foo = foo; + * return 0; + * } + * + * DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init); + * + * static void foo_example_test_success(struct kunit *test) + * { + * struct MOCK(example) *mock_example; + * struct example *example = mock_get_trgt(mock_example); + * struct mock_expectation *handle; + * + * mock_example = CONSTRUCT_MOCK(example, test); + * + * handle = KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example), + * kunit_int_eq(test, 5))); + * handle->action = int_return(test, 2); + * + * KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5)); + * } + * + * Return: + * A &struct mock_expectation representing the call expectation. + * allowing additional conditions and actions to be specified. + */ +#define KUNIT_EXPECT_CALL(expectation_call) mock_master_##expectation_call + +#define mock_get_ctrl_internal(mock_object) (&(mock_object)->ctrl) +#define mock_get_ctrl(mock_object) mock_get_ctrl_internal(mock_object) + +#define mock_get_trgt_internal(mock_object) (&(mock_object)->trgt) +#define mock_get_trgt(mock_object) mock_get_trgt_internal(mock_object) + +#define mock_get_test(mock_object) (mock_get_ctrl(mock_object)->test) + +#define CLASS(struct_name) struct_name +#define HANDLE_INDEX(index) index +#define METHOD(method_name) method_name +#define RETURNS(return_type) return_type +/* #define PARAMS(...) __VA_ARGS__ included by linux/tracepoint.h */ + +#define MOCK_INIT_ID(struct_name) struct_name##mock_init +#define REAL_ID(func_name) __real__##func_name +#define INVOKE_ID(func_name) __invoke__##func_name + +#define DECLARE_MOCK_CLIENT(name, return_type, param_types...) \ + return_type name(PARAM_LIST_FROM_TYPES(param_types)) + +#define DECLARE_MOCK_MASTER(name, ctrl_index, param_types...) \ + struct mock_expectation *mock_master_##name( \ + MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, \ + param_types)) + +#define DECLARE_MOCK_COMMON(name, handle_index, return_type, param_types...) \ + DECLARE_MOCK_CLIENT(name, return_type, param_types); \ + DECLARE_MOCK_MASTER(name, handle_index, param_types) + +#define DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name) \ + struct MOCK(struct_name) { \ + struct mock ctrl; \ + struct struct_name trgt; \ + } + +#define DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name) \ + static inline struct mock *from_##struct_name##_to_mock( \ + const struct struct_name *trgt) \ + { \ + return mock_get_ctrl( \ + container_of(trgt, \ + struct MOCK(struct_name), \ + trgt)); \ + } + +/** + * DECLARE_STRUCT_CLASS_MOCK_PREREQS() - Create a mock child class + * @struct_name: name of the class/struct to be mocked + * + * Creates a mock child class of ``struct_name`` named + * ``struct MOCK(struct_name)`` along with supporting internally used methods. + * + * See KUNIT_EXPECT_CALL() for example usages. + */ +#define DECLARE_STRUCT_CLASS_MOCK_PREREQS(struct_name) \ + DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name); \ + DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name) + +#define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types...) \ + DECLARE_MOCK_COMMON(name, \ + handle_index, \ + return_type, \ + param_types) + +#define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types...) \ + DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types) + +/** + * DECLARE_STRUCT_CLASS_MOCK() + * @name: method name + * @struct_name: name of the class/struct + * @return_type: return type of the method + * @param_types: parameters of the method + * + * Same as DEFINE_STRUCT_CLASS_MOCK(), but only makes header compatible + * declarations. + */ +#define DECLARE_STRUCT_CLASS_MOCK(name, \ + struct_name, \ + return_type, \ + param_types...) \ + DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \ + struct_name, \ + 0, \ + return_type, \ + param_types) + +/** + * DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN() + * @name: method name + * @struct_name: name of the class/struct + * @param_types: parameters of the method + * + * Same as DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(), but only makes header + * compatible declarations. + */ +#define DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN(name, \ + struct_name, \ + param_types...) \ + DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \ + struct_name, \ + 0, \ + void, \ + param_types) + +/** + * DECLARE_STRUCT_CLASS_MOCK_INIT() + * @struct_name: name of the class/struct + * + * Same as DEFINE_STRUCT_CLASS_MOCK_INIT(), but only makes header compatible + * declarations. + */ +#define DECLARE_STRUCT_CLASS_MOCK_INIT(struct_name) \ + struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)( \ + struct kunit *test) + +/** + * CONSTRUCT_MOCK() + * @struct_name: name of the class + * @test: associated test + * + * Constructs and allocates a test managed ``struct MOCK(struct_name)`` given + * the name of the class for which the mock is defined and a test object. + * + * See KUNIT_EXPECT_CALL() for example usage. + */ +#define CONSTRUCT_MOCK(struct_name, test) MOCK_INIT_ID(struct_name)(test) + +#define DEFINE_MOCK_CLIENT_COMMON(name, \ + handle_index, \ + MOCK_SOURCE, \ + mock_source_ctx, \ + return_type, \ + RETURN, \ + param_types...) \ + return_type name(PARAM_LIST_FROM_TYPES(param_types)) \ + { \ + struct mock *mock = MOCK_SOURCE(mock_source_ctx, \ + handle_index); \ + static const char * const param_type_names[] = { \ + TYPE_NAMES_FROM_TYPES(handle_index, \ + param_types) \ + }; \ + const void *params[] = { \ + PTR_TO_ARG_FROM_TYPES(handle_index, \ + param_types) \ + }; \ + const void *retval; \ + \ + retval = mock->do_expect(mock, \ + #name, \ + name, \ + param_type_names, \ + params, \ + ARRAY_SIZE(params)); \ + KUNIT_ASSERT_NOT_ERR_OR_NULL(mock->test, retval); \ + if (!retval) { \ + kunit_info(mock->test, \ + "no action installed for "#name"\n");\ + BUG(); \ + } \ + RETURN(return_type, retval); \ + } + +#define CLASS_MOCK_CLIENT_SOURCE(ctx, handle_index) ctx(arg##handle_index) +#define DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \ + handle_index, \ + mock_converter, \ + return_type, \ + RETURN, \ + param_types...) \ + DEFINE_MOCK_CLIENT_COMMON(name, \ + handle_index, \ + CLASS_MOCK_CLIENT_SOURCE, \ + mock_converter, \ + return_type, \ + RETURN, \ + param_types) + +#define CAST_AND_RETURN(return_type, retval) return *((return_type *) retval) +#define NO_RETURN(return_type, retval) + +#define DEFINE_MOCK_METHOD_CLIENT(name, \ + handle_index, \ + mock_converter, \ + return_type, \ + param_types...) \ + DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \ + handle_index, \ + mock_converter, \ + return_type, \ + CAST_AND_RETURN, \ + param_types) + +#define DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name, \ + handle_index, \ + mock_converter, \ + param_types...) \ + DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \ + handle_index, \ + mock_converter, \ + void, \ + NO_RETURN, \ + param_types) + +#define DEFINE_MOCK_MASTER_COMMON_INTERNAL(name, \ + ctrl_index, \ + MOCK_SOURCE, \ + param_types...) \ + struct mock_expectation *mock_master_##name( \ + MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, \ + param_types)) \ + { \ + struct mock_param_matcher *matchers[] = { \ + ARG_NAMES_FROM_TYPES(ctrl_index, param_types) \ + }; \ + \ + return mock_add_matcher(MOCK_SOURCE(ctrl_index), \ + #name, \ + (const void *) name, \ + matchers, \ + ARRAY_SIZE(matchers)); \ + } +#define DEFINE_MOCK_MASTER_COMMON(name, \ + ctrl_index, \ + MOCK_SOURCE, \ + param_types...) \ + DEFINE_MOCK_MASTER_COMMON_INTERNAL(name, \ + ctrl_index, \ + MOCK_SOURCE, \ + param_types) + +#define CLASS_MOCK_MASTER_SOURCE(ctrl_index) arg##ctrl_index +#define DEFINE_MOCK_METHOD_MASTER(name, ctrl_index, param_types...) \ + DEFINE_MOCK_MASTER_COMMON(name, \ + ctrl_index, \ + CLASS_MOCK_MASTER_SOURCE, \ + param_types) + +#define DEFINE_MOCK_COMMON(name, \ + handle_index, \ + mock_converter, \ + return_type, \ + param_types...) \ + DEFINE_MOCK_METHOD_CLIENT(name, \ + handle_index, \ + mock_converter, \ + return_type, \ + param_types); \ + DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types) + +#define DEFINE_MOCK_COMMON_VOID_RETURN(name, \ + handle_index, \ + mock_converter, \ + param_types...) \ + DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name, \ + handle_index, \ + mock_converter, \ + param_types); \ + DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types) + +#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types...) \ + DEFINE_MOCK_COMMON(name, \ + handle_index, \ + from_##struct_name##_to_mock, \ + return_type, param_types) +#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types...) \ + DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \ + struct_name, \ + handle_index, \ + return_type, \ + param_types) + +#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL( \ + name, \ + struct_name, \ + handle_index, \ + param_types...) \ + DEFINE_MOCK_COMMON_VOID_RETURN(name, \ + handle_index, \ + from_##struct_name##_to_mock, \ + param_types) +#define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name, \ + struct_name, \ + handle_index, \ + param_types...) \ + DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL( \ + name, \ + struct_name, \ + handle_index, \ + param_types) + +/** + * DEFINE_STRUCT_CLASS_MOCK() + * @name: name of the method + * @struct_name: name of the class of which the method belongs + * @return_type: return type of the method to be created. **Must not be void.** + * @param_types: parameters to method to be created. + * + * See KUNIT_EXPECT_CALL() for example usage. + */ +#define DEFINE_STRUCT_CLASS_MOCK(name, \ + struct_name, \ + return_type, \ + param_types...) \ + DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \ + struct_name, \ + 0, \ + return_type, \ + param_types) + +/** + * DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN() + * @name: name of the method + * @struct_name: name of the class of which the method belongs + * @param_types: parameters to method to be created. + * + * Same as DEFINE_STRUCT_CLASS_MOCK() except the method has a ``void`` return + * type. + */ +#define DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(name, struct_name, param_types...)\ + DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name, \ + struct_name, \ + 0, \ + param_types) + +/** + * DEFINE_STRUCT_CLASS_MOCK_INIT() + * @struct_name: name of the class + * @init_func: a function of type ``int (*)(struct kunit *, struct MOCK(struct_name) *)``. + * The function is passed a pointer to an allocated, *but not + * initialized*, ``struct MOCK(struct_name)``. The job of this user + * provided function is to perform remaining initialization. Usually + * this entails assigning mock methods to the function pointers in + * the parent struct. + * + * See KUNIT_EXPECT_CALL() for example usage. + */ +#define DEFINE_STRUCT_CLASS_MOCK_INIT(struct_name, init_func) \ + struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)( \ + struct kunit *test) \ + { \ + struct MOCK(struct_name) *mock_obj; \ + \ + mock_obj = kunit_kzalloc(test, \ + sizeof(*mock_obj), \ + GFP_KERNEL); \ + if (!mock_obj) \ + return NULL; \ + \ + mock_init_ctrl(test, mock_get_ctrl(mock_obj)); \ + \ + if (init_func(test, mock_obj)) \ + return NULL; \ + \ + return mock_obj; \ + } + #define CONVERT_TO_ACTUAL_TYPE(type, ptr) (*((type *) ptr)) /** diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index a7a3c5e0a8bf..649e1c1f0d00 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -13,11 +13,12 @@ kunit-objs += debugfs.o endif obj-$(CONFIG_KUNIT_TEST) += kunit-test.o \ - mock-macro-test.o + mock-macro-test.o \ + mock-test.o # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o endif -obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o +obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index be1164ecc476..d6fff5a961ce 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -7,6 +7,7 @@ */ #include +#include /* * This is the most fundamental element of KUnit, the test case. A test case @@ -29,6 +30,92 @@ static void example_simple_test(struct kunit *test) KUNIT_EXPECT_EQ(test, 1 + 1, 2); } +struct example_ops; + +struct example { + struct example_ops *ops; +}; + +/* + * A lot of times, we embed "ops structs", which acts an abstraction over + * hardware, a file system implementation, or some other subsystem that you + * want to reason about in a generic way. + */ +struct example_ops { + int (*foo)(struct example *example, int num); +}; + +static int example_bar(struct example *example, int num) +{ + return example->ops->foo(example, num); +} + +/* + * KUnit allows such a class to be "mocked out" with the following: + */ + +/* + * This macro creates a mock subclass of the specified class. + */ +DECLARE_STRUCT_CLASS_MOCK_PREREQS(example); + +/* + * This macro creates a mock implementation of the specified method of the + * specified class. + */ +DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example), + RETURNS(int), + PARAMS(struct example *, int)); + +/* + * This tells KUnit how to initialize the parts of the mock that come from the + * parent. In this example, all we have to do is populate the member functions + * of the parent class with the mock versions we defined. + */ +static int example_init(struct kunit *test, struct MOCK(example) *mock_example) +{ + /* This is how you get a pointer to the parent class of a mock. */ + struct example *example = mock_get_trgt(mock_example); + + /* + * Here we create an ops struct containing our mock method instead. + */ + example->ops = kunit_kzalloc(test, sizeof(*example->ops), GFP_KERNEL); + example->ops->foo = foo; + + return 0; +} + +/* + * This registers our parent init function above, allowing KUnit to create a + * constructor for the mock. + */ +DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init); + +/* + * This is a test case where we use our mock. + */ +static void example_mock_test(struct kunit *test) +{ + struct MOCK(example) *mock_example = test->priv; + struct example *example = mock_get_trgt(mock_example); + struct mock_expectation *handle; + + /* + * Here we make an expectation that our mock method will be called with + * a parameter equal to 5 passed in. + */ + handle = KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example), + kunit_int_eq(test, 5))); + /* + * We specify that when our mock is called in this way, we want it to + * return 2. + */ + handle->action = kunit_int_return(test, 2); + + KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5)); +} + /* * This is run once before each test case, see the comment on * example_test_suite for more information. @@ -37,6 +124,16 @@ static int example_test_init(struct kunit *test) { kunit_info(test, "initializing\n"); + /* + * Here we construct the mock and store it in test's `priv` field; this + * field is for KUnit users. You can put whatever you want here, but + * most often it is a place that the init function can put stuff to be + * used by test cases. + */ + test->priv = CONSTRUCT_MOCK(example, test); + if (!test->priv) + return -EINVAL; + return 0; } @@ -52,6 +149,7 @@ static struct kunit_case example_test_cases[] = { * test suite. */ KUNIT_CASE(example_simple_test), + KUNIT_CASE(example_mock_test), {} }; diff --git a/lib/kunit/mock-macro-test.c b/lib/kunit/mock-macro-test.c index 6c3dc2193edb..d196dbee2407 100644 --- a/lib/kunit/mock-macro-test.c +++ b/lib/kunit/mock-macro-test.c @@ -8,6 +8,55 @@ #include #include +#include + +struct mock_macro_dummy_struct { + int (*one_param)(struct mock_macro_dummy_struct *test_struct); + int (*two_param)(struct mock_macro_dummy_struct *test_struct, int num); + int (*non_first_slot_param)( + int num, + struct mock_macro_dummy_struct *test_struct); + void *(*void_ptr_return)(struct mock_macro_dummy_struct *test_struct); +}; + +DECLARE_STRUCT_CLASS_MOCK_PREREQS(mock_macro_dummy_struct); + +DEFINE_STRUCT_CLASS_MOCK(METHOD(one_param), CLASS(mock_macro_dummy_struct), + RETURNS(int), + PARAMS(struct mock_macro_dummy_struct *)); + +DEFINE_STRUCT_CLASS_MOCK(METHOD(two_param), CLASS(mock_macro_dummy_struct), + RETURNS(int), + PARAMS(struct mock_macro_dummy_struct *, int)); + +DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(METHOD(non_first_slot_param), + CLASS(mock_macro_dummy_struct), HANDLE_INDEX(1), + RETURNS(int), + PARAMS(int, struct mock_macro_dummy_struct *)); + +DEFINE_STRUCT_CLASS_MOCK(METHOD(void_ptr_return), + CLASS(mock_macro_dummy_struct), + RETURNS(void *), + PARAMS(struct mock_macro_dummy_struct *)); + +static int mock_macro_dummy_struct_init( + struct MOCK(mock_macro_dummy_struct) *mock_test_struct) +{ + struct mock_macro_dummy_struct *test_struct = + mock_get_trgt(mock_test_struct); + + test_struct->one_param = one_param; + test_struct->two_param = two_param; + test_struct->non_first_slot_param = non_first_slot_param; + return 0; +} + +DEFINE_STRUCT_CLASS_MOCK_INIT(mock_macro_dummy_struct, + mock_macro_dummy_struct_init); + +struct mock_macro_context { + struct MOCK(mock_macro_dummy_struct) *mock_test_struct; +}; #define TO_STR_INTERNAL(...) #__VA_ARGS__ #define TO_STR(...) TO_STR_INTERNAL(__VA_ARGS__) @@ -132,6 +181,46 @@ static void mock_macro_arg_names_from_types(struct kunit *test) type15))); } +static void mock_macro_test_generated_method_code_works(struct kunit *test) +{ + struct mock_macro_context *ctx = test->priv; + struct MOCK(mock_macro_dummy_struct) *mock_test_struct = + ctx->mock_test_struct; + struct mock_macro_dummy_struct *test_struct = + mock_get_trgt(mock_test_struct); + struct mock_expectation *handle; + + handle = KUNIT_EXPECT_CALL(one_param(mock_get_ctrl(mock_test_struct))); + handle->action = kunit_int_return(test, 0); + handle = KUNIT_EXPECT_CALL(two_param(mock_get_ctrl(mock_test_struct), + kunit_int_eq(test, 5))); + handle->action = kunit_int_return(test, 1); + handle = KUNIT_EXPECT_CALL(non_first_slot_param( + kunit_int_eq(test, 5), + mock_get_ctrl(mock_test_struct))); + handle->action = kunit_int_return(test, 1); + + test_struct->one_param(test_struct); + test_struct->two_param(test_struct, 5); + test_struct->non_first_slot_param(5, test_struct); +} + +static int mock_macro_test_init(struct kunit *test) +{ + struct mock_macro_context *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->mock_test_struct = CONSTRUCT_MOCK(mock_macro_dummy_struct, test); + if (!ctx->mock_test_struct) + return -EINVAL; + + return 0; +} + static struct kunit_case mock_macro_test_cases[] = { KUNIT_CASE(mock_macro_is_equal), KUNIT_CASE(mock_macro_if), @@ -140,11 +229,13 @@ static struct kunit_case mock_macro_test_cases[] = { KUNIT_CASE(mock_macro_for_each_param), KUNIT_CASE(mock_macro_param_list_from_types_basic), KUNIT_CASE(mock_macro_arg_names_from_types), + KUNIT_CASE(mock_macro_test_generated_method_code_works), {} }; static struct kunit_suite mock_macro_test_suite = { .name = "mock-macro-test", + .init = mock_macro_test_init, .test_cases = mock_macro_test_cases, }; kunit_test_suite(mock_macro_test_suite); diff --git a/lib/kunit/mock-test.c b/lib/kunit/mock-test.c new file mode 100644 index 000000000000..8a0fa33d087c --- /dev/null +++ b/lib/kunit/mock-test.c @@ -0,0 +1,320 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for mock.h. + * + * Copyright (C) 2020, Google LLC. + * Author: Brendan Higgins + */ + +#include +#include + +// A simple class for unit-testing/example purposes. +struct adder { + int (*add)(struct adder *adder, int x, int y); +}; + +static int real_add(struct adder *adder, int x, int y) +{ + return x + y; +} + +static void adder_real_init(struct adder *adder) +{ + adder->add = real_add; +} + +DECLARE_STRUCT_CLASS_MOCK_PREREQS(adder); +DEFINE_STRUCT_CLASS_MOCK(METHOD(mock_add), CLASS(adder), RETURNS(int), + PARAMS(struct adder*, int, int)); +DECLARE_STRUCT_CLASS_MOCK_INIT(adder); + +// This would normally live in the .c file. +static int adder_mock_init(struct MOCK(adder) *mock_adder) +{ + struct adder *real = mock_get_trgt(mock_adder); + + adder_real_init(real); + + real->add = mock_add; + mock_set_default_action(mock_get_ctrl(mock_adder), + "mock_add", + mock_add, + kunit_int_return(mock_get_test(mock_adder), 0)); + return 0; +} +DEFINE_STRUCT_CLASS_MOCK_INIT(adder, adder_mock_init); + + +/* + * Note: we create a new `failing_test` so we can validate that failed mock + * expectations mark tests as failed. + * Marking the real `test` as failed is obviously problematic. + * + * See mock_test_failed_expect_call_fails_test for an example. + */ +struct mock_test_context { + struct kunit *failing_test; + struct mock *mock; +}; + +static void mock_test_do_expect_basic(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct mock *mock = ctx->mock; + int param0 = 5, param1 = -4; + static const char * const two_param_types[] = {"int", "int"}; + const void *two_params[] = {¶m0, ¶m1}; + struct mock_param_matcher *matchers_any_two[] = { + kunit_any(test), kunit_any(test) + }; + struct mock_expectation *expectation; + const void *ret; + + expectation = mock_add_matcher(mock, + "", + NULL, + matchers_any_two, + ARRAY_SIZE(matchers_any_two)); + expectation->action = kunit_int_return(test, 5); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); + + ret = mock->do_expect(mock, + "", + NULL, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret); + KUNIT_EXPECT_EQ(test, 5, *((int *) ret)); + KUNIT_EXPECT_EQ(test, 1, expectation->times_called); +} + +static void mock_test_ptr_eq(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + void *param0 = ctx, *param1 = failing_test; + static const char * const two_param_types[] = {"void *", "void *"}; + const void *two_params[] = {¶m0, ¶m1}; + struct mock_param_matcher *matchers_two_ptrs[] = { + kunit_ptr_eq(test, param0), kunit_ptr_eq(test, param1) + }; + struct mock_expectation *expectation; + const void *ret; + + expectation = mock_add_matcher(mock, + "", + NULL, + matchers_two_ptrs, + ARRAY_SIZE(matchers_two_ptrs)); + expectation->action = kunit_int_return(test, 0); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); + + ret = mock->do_expect(mock, + "", + NULL, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret); + KUNIT_EXPECT_EQ(test, 1, expectation->times_called); +} + +static void mock_test_ptr_eq_not_equal(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + + /* Pick some two pointers, but pass in different values. */ + void *param0 = test, *param1 = failing_test; + static const char * const two_param_types[] = {"void *", "void *"}; + const void *two_params[] = {¶m0, ¶m1}; + struct mock_param_matcher *matchers_two_ptrs[] = { + kunit_ptr_eq(failing_test, param0), + kunit_ptr_eq(failing_test, param1 - 1) + }; + struct mock_expectation *expectation; + const void *ret; + + expectation = mock_add_matcher(mock, + "", + NULL, + matchers_two_ptrs, + ARRAY_SIZE(matchers_two_ptrs)); + expectation->action = kunit_int_return(failing_test, 0); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); + + ret = mock->do_expect(mock, + "", + NULL, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + KUNIT_EXPECT_FALSE(test, ret); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); + + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + +/* + * In order for us to be able to rely on KUNIT_EXPECT_CALL to validate other + * behavior, we need to test that unsatisfied KUNIT_EXPECT_CALL causes a test + * failure. + * + * In order to understand what this test is testing we must first understand how + * KUNIT_EXPECT_CALL() works conceptually. In theory, a test specifies that it + * expects some function to be called some number of times (can be zero), with + * some particular arguments. Hence, KUNIT_EXPECT_CALL() must do two things: + * + * 1) Determine whether a function call matches the expectation. + * + * 2) Fail if there are too many or too few matches. + */ +static void mock_test_failed_expect_call_fails_test(struct kunit *test) +{ + /* + * We do not want to fail the real `test` object used to run this test. + * So we use a separate `failing_test` for KUNIT_EXPECT_CALL(). + */ + struct mock_test_context *ctx = test->priv; + struct kunit *failing_test = ctx->failing_test; + struct mock *mock = ctx->mock; + + /* + * Put an expectation on mock, which we won't satisify. + * + * NOTE: it does not actually matter what function we expect here. + * `mock` does not represent an actual mock on anything; we just need to + * create some expectation, that we won't satisfy. + */ + KUNIT_EXPECT_CALL(mock_add(mock, + kunit_any(failing_test), + kunit_any(failing_test))); + + /* + * Validate the unsatisfied expectation that we just created. This + * should cause `failing_test` to fail. + */ + mock_validate_expectations(mock); + + /* Verify that `failing_test` has actually failed. */ + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + +static void mock_test_do_expect_default_return(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + 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 *matchers[] = { + kunit_int_eq(test, 5), + kunit_int_eq(test, -4) + }; + struct mock_expectation *expectation; + const void *ret; + + expectation = mock_add_matcher(mock, + "add", + mock_add, + matchers, + ARRAY_SIZE(matchers)); + expectation->action = kunit_int_return(test, 5); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); + + KUNIT_EXPECT_FALSE(test, + mock_set_default_action(mock, + "add", + mock_add, + kunit_int_return(test, -4))); + + ret = mock->do_expect(mock, + "add", + mock_add, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret); + KUNIT_EXPECT_EQ(test, -4, *((int *) ret)); + KUNIT_EXPECT_EQ(test, 0, expectation->times_called); +} + +static void mock_test_mock_validate_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) + }; + struct mock_expectation *expectation; + + + 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; + + mock_validate_expectations(mock); + + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + +static int mock_test_init(struct kunit *test) +{ + struct mock_test_context *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return -ENOMEM; + test->priv = ctx; + + ctx->failing_test = kunit_kzalloc(test, sizeof(*ctx->failing_test), + GFP_KERNEL); + if (!ctx->failing_test) + return -EINVAL; + kunit_init_test(ctx->failing_test, NULL, NULL); + + ctx->mock = kunit_kzalloc(test, sizeof(*ctx->mock), GFP_KERNEL); + if (!ctx->mock) + return -ENOMEM; + mock_init_ctrl(ctx->failing_test, ctx->mock); + + return 0; +} + +static void mock_test_exit(struct kunit *test) +{ + struct mock_test_context *ctx = test->priv; + + kunit_cleanup(ctx->failing_test); +} + +static struct kunit_case mock_test_cases[] = { + KUNIT_CASE(mock_test_do_expect_basic), + KUNIT_CASE(mock_test_ptr_eq), + KUNIT_CASE(mock_test_ptr_eq_not_equal), + KUNIT_CASE(mock_test_failed_expect_call_fails_test), + KUNIT_CASE(mock_test_do_expect_default_return), + KUNIT_CASE(mock_test_mock_validate_expectations), + {} +}; + +static struct kunit_suite mock_test_suite = { + .name = "mock-test", + .init = mock_test_init, + .exit = mock_test_exit, + .test_cases = mock_test_cases, +}; + +kunit_test_suite(mock_test_suite); From patchwork Mon Oct 12 22:20:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 269653 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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 1745EC433DF for ; Mon, 12 Oct 2020 22:22:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA81420797 for ; Mon, 12 Oct 2020 22:22:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CHeTDQjD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389401AbgJLWVy (ORCPT ); Mon, 12 Oct 2020 18:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389275AbgJLWVj (ORCPT ); Mon, 12 Oct 2020 18:21:39 -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 CCD55C0613D7 for ; Mon, 12 Oct 2020 15:21:38 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id z22so3231581qtn.15 for ; Mon, 12 Oct 2020 15:21:38 -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=bC8bc00tDP9ynAbL7H/K4risArdoFSp41x3R2A78u04=; b=CHeTDQjDQWk5SugZGUsEbJWXZVoztLD39IQw73+TiXG0em9ZzdG1BuIpf9eRPn8aeB GWSfpQjI9D/ReFvjMd8JGGQYFg5mljDB2bxYZYBlvh69C34PGI5aIjareTWZZWn98pv0 pD5UBgTeGviwXlBuhRisgLGdq1TrDgdN0vvKlJLl0OOZ6ApM7/BqE23f3QKGYcXfpY8d 1ktSmP8kfEW5iDFzA3ft5RcHEiFrUn+ztBnloGP0yYuqDze4W7/wpz9cuxQWjx5rlItO iEQ68vik4Y7PeFGWY3T+QNrLihYxAPnTtd3qwzTo9iJcflq65ORB6N4CFX9oSHH8TBZA n2AA== 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=bC8bc00tDP9ynAbL7H/K4risArdoFSp41x3R2A78u04=; b=j06PtboFZORWyl2513pWXwNM55Ik3VfnBwh6QQXJTSIJJMS9N44NYUaTQlrss41T5B WFyot/ETRX+Nayc98zzHSPOaqT/9DxF/RIdm+rWlQHVJLhxeOw5eoaXedq9a5nzysHwi oW1z/129ZpbA8y9V2A1jYAByoC57NHVxRbzQVC87EJ3z6R9UfRnmwPKV2xNmMp5R4vS/ m06PVNedlbCIBsrz1CIP4SUKQVBOWuAb19YF71oDERCGXyimPVwS+zrBDpJIJa8lKZFU 1+x6d+1t7jEdcLMsmGrrfNIRa3xc1fOf3uQCjp8twy97Gclnode/W2hHevchIvbC0Aos riqg== X-Gm-Message-State: AOAM532UVH0nnsjdcptoJdA1Umsw+jfCLAMBJvHsuNRO7IzZ1VmrfFfX stjqRFqSfWjEeW6v81C0BkR5xiSUBDUR1w== X-Google-Smtp-Source: ABdhPJyiX8+zF0fcLXehSLljEM5NsLHgQTcclOSA1APGR/J55WSVCeeSczEIyl0nWkmhxyfo2oWO7Oaffboong== Sender: "dlatypov via sendgmr" X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:a28c:fdff:fee3:28c6]) (user=dlatypov job=sendgmr) by 2002:ad4:5843:: with SMTP id de3mr27933829qvb.12.1602541297970; Mon, 12 Oct 2020 15:21:37 -0700 (PDT) Date: Mon, 12 Oct 2020 15:20:49 -0700 In-Reply-To: <20201012222050.999431-1-dlatypov@google.com> Message-Id: <20201012222050.999431-12-dlatypov@google.com> Mime-Version: 1.0 References: <20201012222050.999431-1-dlatypov@google.com> X-Mailer: git-send-email 2.28.0.1011.ga647a8990f-goog Subject: [RFC v2 11/12] kunit: mock: add struct param matcher From: Daniel Latypov To: dlatypov@google.com Cc: alan.maguire@oracle.com, brendanhiggins@google.com, davidgow@google.com, keescook@chromium.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, mcgrof@kernel.org, sboyd@kernel.org, skhan@linuxfoundation.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Brendan Higgins Add parameter matcher builder for matching struct values. Signed-off-by: Brendan Higgins Signed-off-by: Daniel Latypov --- include/kunit/mock.h | 58 +++++++++++++++++++ lib/kunit/common-mocks.c | 117 +++++++++++++++++++++++++++++++++++++++ lib/kunit/mock-test.c | 40 +++++++++++++ 3 files changed, 215 insertions(+) diff --git a/include/kunit/mock.h b/include/kunit/mock.h index 9252a0abd295..df99ae5ac721 100644 --- a/include/kunit/mock.h +++ b/include/kunit/mock.h @@ -758,8 +758,18 @@ struct mock_param_matcher *kunit_ptr_ge(struct kunit *test, void *expected); 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_param_matcher *kunit_str_contains(struct kunit *test, + const char *needle); + +/* Matches var-arg arguments. */ +struct mock_param_matcher *kunit_va_format_cmp( + struct kunit *test, + struct mock_param_matcher *fmt_matcher, + struct mock_param_matcher *va_matcher); + 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); @@ -778,4 +788,52 @@ struct mock_action *kunit_ulonglong_return(struct kunit *test, unsigned long long ret); struct mock_action *kunit_ptr_return(struct kunit *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 *kunit_struct_cmp( + struct kunit *test, + const char *struct_name, + struct mock_struct_matcher_entry *entries); + #endif /* _KUNIT_MOCK_H */ diff --git a/lib/kunit/common-mocks.c b/lib/kunit/common-mocks.c index ce8929157ded..6a4cc9c60427 100644 --- a/lib/kunit/common-mocks.c +++ b/lib/kunit/common-mocks.c @@ -228,6 +228,123 @@ struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str) 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 kunit_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) + kunit_stream_add(stream, + "'%s' found in '%s'", + matcher->needle, + haystack); + else + kunit_stream_add(stream, + "'%s' not found in '%s'", + matcher->needle, + haystack); + return matches; +} + +struct mock_param_matcher *kunit_str_contains(struct kunit *test, + const char *str) +{ + struct mock_str_contains_matcher *matcher; + + matcher = kunit_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 *kunit_va_format_cmp( + struct kunit *test, + struct mock_param_matcher *fmt_matcher, + struct mock_param_matcher *va_matcher) +{ + struct mock_struct_matcher_entry *entries; + + entries = kunit_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 kunit_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 kunit_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; + + kunit_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; + kunit_stream_add(stream, ", "); + } + kunit_stream_add(stream, "}"); + + return matches; +} + +struct mock_param_matcher *kunit_struct_cmp( + struct kunit *test, + const char *struct_name, + struct mock_struct_matcher_entry *entries) +{ + struct mock_struct_matcher *matcher; + + matcher = kunit_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/lib/kunit/mock-test.c b/lib/kunit/mock-test.c index 8a0fa33d087c..df0969b43ade 100644 --- a/lib/kunit/mock-test.c +++ b/lib/kunit/mock-test.c @@ -243,6 +243,45 @@ static void mock_test_do_expect_default_return(struct kunit *test) KUNIT_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 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_set_default_action(mock, + "add", + real_add, + kunit_int_return(failing_test, -4)); + + + KUNIT_EXPECT_CALL(mock_add( + mock, + kunit_any(failing_test), + kunit_va_format_cmp(failing_test, + kunit_str_contains(failing_test, + "Method was called with no expectations declared"), + kunit_any(failing_test)))); + + mock->do_expect(mock, + "add", + real_add, + two_param_types, + two_params, + ARRAY_SIZE(two_params)); + mock_validate_expectations(mock); + + KUNIT_EXPECT_FALSE(test, failing_test->success); +} + static void mock_test_mock_validate_expectations(struct kunit *test) { struct mock_test_context *ctx = test->priv; @@ -307,6 +346,7 @@ 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_naggy_no_expectations_no_fail), {} };