From patchwork Mon Sep 4 13:21:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 720065 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1253AC83F33 for ; Mon, 4 Sep 2023 13:22:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239681AbjIDNWK (ORCPT ); Mon, 4 Sep 2023 09:22:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238005AbjIDNWJ (ORCPT ); Mon, 4 Sep 2023 09:22:09 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:242:246e::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D168CCD0 for ; Mon, 4 Sep 2023 06:22:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-To:Resent-Cc: Resent-Message-ID:In-Reply-To:References; bh=PfQ10RDcuF6bzVfXDVOWYd7nyL7h/wSUO25g2XCuP84=; t=1693833722; x=1695043322; b=Y8ASWMegN9nEzgCPhMSD3bhB7d7PReyazIcQq1EJx2M/mfP5gutwdhOI+D4aV6rwt+USCPRC2mj CbznJEWgm3OX7CuH2bNczxlEUzPw1oQjNuWf5lEFNwEEt1fnTiNFuI4cy1kaGo4EhMnN79Yp9WGOw ntHpyjWS1y3ek0BQo/JL8dDrpNm9iwDq7dC1XqsmI8ovvhIeNqvKWDLH7DOMirc2SmVoVvtt23XWM VyBC0i4eqz1WJnajtN+lmvyoXGt3IQIvxNQLH77DJCGmmUjR3o2iYxqH2vdbiQT7bYe024xupOBGI ACRJBzQCc8tXAolQPajiFiqTEdAbMtreqBnA==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1qd9Wh-00D8YX-1f; Mon, 04 Sep 2023 15:21:59 +0200 From: benjamin@sipsolutions.net To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Benjamin Berg Subject: [PATCH 1/2] kunit: add parameter generation macro using description from array Date: Mon, 4 Sep 2023 15:21:38 +0200 Message-ID: <20230904132139.103140-1-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Benjamin Berg The existing KUNIT_ARRAY_PARAM macro requires a separate function to get the description. However, in a lot of cases the description can just be copied directly from the array. Add a second macro that avoids having to write a static function just for a single strscpy. Signed-off-by: Benjamin Berg Reviewed-by: David Gow --- Documentation/dev-tools/kunit/usage.rst | 7 ++++--- include/kunit/test.h | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index c27e1646ecd9..fe8c28d66dfe 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -571,8 +571,9 @@ By reusing the same ``cases`` array from above, we can write the test as a { strcpy(desc, t->str); } - // Creates `sha1_gen_params()` to iterate over `cases`. - KUNIT_ARRAY_PARAM(sha1, cases, case_to_desc); + // Creates `sha1_gen_params()` to iterate over `cases` while using + // the struct member `str` for the case description. + KUNIT_ARRAY_PARAM_DESC(sha1, cases, str); // Looks no different from a normal test. static void sha1_test(struct kunit *test) @@ -588,7 +589,7 @@ By reusing the same ``cases`` array from above, we can write the test as a } // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the - // function declared by KUNIT_ARRAY_PARAM. + // function declared by KUNIT_ARRAY_PARAM or KUNIT_ARRAY_PARAM_DESC. static struct kunit_case sha1_test_cases[] = { KUNIT_CASE_PARAM(sha1_test, sha1_gen_params), {} diff --git a/include/kunit/test.h b/include/kunit/test.h index 68ff01aee244..f60d11e41855 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1516,6 +1516,25 @@ do { \ return NULL; \ } +/** + * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array. + * @name: prefix for the test parameter generator function. + * @array: array of test parameters. + * @desc_member: structure member from array element to use as description + * + * Define function @name_gen_params which uses @array to generate parameters. + */ +#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \ + static const void *name##_gen_params(const void *prev, char *desc) \ + { \ + typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ + if (__next - (array) < ARRAY_SIZE((array))) { \ + strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \ + return __next; \ + } \ + return NULL; \ + } + // TODO(dlatypov@google.com): consider eventually migrating users to explicitly // include resource.h themselves if they need it. #include From patchwork Mon Sep 4 13:21:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Berg X-Patchwork-Id: 720284 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A3EEC83F3F for ; Mon, 4 Sep 2023 13:22:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238005AbjIDNWL (ORCPT ); Mon, 4 Sep 2023 09:22:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239193AbjIDNWK (ORCPT ); Mon, 4 Sep 2023 09:22:10 -0400 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:242:246e::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 706F9CD1 for ; Mon, 4 Sep 2023 06:22:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-To: Resent-Cc:Resent-Message-ID; bh=ZwLfUM/cdMYzDf00Kwl/BwjmCoVZQCyb2gPuJnz8cPg=; t=1693833723; x=1695043323; b=Njzmdk0yvPIlbzqYHdJCxunv2byqo4Bls8QacSONcxO2PtE Jr1U7IVLWELlt3VCtZpXvgp3OXRvu+nxWFr71getH9HgetZQA7lUZrVWnXzEqPiZJNnSKKvrGdNdt QYSoF3bDm/cfdmLBfoTeh6eQiQYhJGuAEDsLXcGVdNRtSAJgf+dInHOuftQrJD2RWQtFyPXdlWbwl CUIo7E0nSgamjdPNp+6xE8I2e/w5ImE3n1VfrenX2vfMUvAMq79hIy+hTRzNbVTv/UNXk5wjPE4us 3/Rz3SKbkNZOoCzNkWV6EdaDkZtUCn0H+OynYbFULuY3YyKuvPSum1EWWCe2t0oA==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.96) (envelope-from ) id 1qd9Wi-00D8YX-1m; Mon, 04 Sep 2023 15:22:00 +0200 From: benjamin@sipsolutions.net To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Benjamin Berg Subject: [PATCH 2/2] kunit: add a convenience allocation wrapper for SKBs Date: Mon, 4 Sep 2023 15:21:39 +0200 Message-ID: <20230904132139.103140-2-benjamin@sipsolutions.net> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230904132139.103140-1-benjamin@sipsolutions.net> References: <20230904132139.103140-1-benjamin@sipsolutions.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Benjamin Berg Add a simple convenience helper to allocate and zero fill an SKB for the use by a kunit test. Also provide a way to free it again in case that may be desirable. This simply mirrors the kunit_kmalloc API. Signed-off-by: Benjamin Berg --- include/kunit/skbuff.h | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 include/kunit/skbuff.h diff --git a/include/kunit/skbuff.h b/include/kunit/skbuff.h new file mode 100644 index 000000000000..947fc8b5b48f --- /dev/null +++ b/include/kunit/skbuff.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Base unit test (KUnit) API. + * + * Copyright (C) 2023 Intel Corporation + */ + +#ifndef _KUNIT_SKBUFF_H +#define _KUNIT_SKBUFF_H + +#include +#include + +/** + * kunit_zalloc_skb() - Allocate and initialize a resource managed skb. + * @test: The test case to which the skb belongs + * @len: size to allocate + * + * Allocate a new struct sk_buff with GFP_KERNEL, zero fill the give length + * and add it as a resource to the kunit test for automatic cleanup. + * + * Returns: newly allocated SKB, or %NULL on error + */ +static inline struct sk_buff *kunit_zalloc_skb(struct kunit *test, int len, + gfp_t gfp) +{ + struct sk_buff *res = alloc_skb(len, GFP_KERNEL); + + if (!res || skb_pad(res, len)) + return NULL; + + if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree_skb, res)) + return NULL; + + return res; +} + +/** + * kunit_kfree_skb() - Like kfree_skb except for allocations managed by KUnit. + * @test: The test case to which the resource belongs. + * @skb: The SKB to free. + */ +static inline void kunit_kfree_skb(struct kunit *test, struct sk_buff *skb) +{ + if (!skb) + return; + + kunit_release_action(test, (kunit_action_t *)kfree_skb, (void *)skb); +} + +#endif /* _KUNIT_SKBUFF_H */