From patchwork Fri Dec 15 15:13:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 755022 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DA903FE30 for ; Fri, 15 Dec 2023 15:13:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IwK5JV2c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702653226; x=1734189226; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2kQV2dId0UrgYhXYi/uAsAuwiFtuS9UJf4+1fL+cAdg=; b=IwK5JV2cxOVYKaKPMeaaEXmSHHPFG6zGUbm5TJYBpXEkbjqiW0zfK661 FTuodfhmaZtNzg76SmRB2F0yyMViJb5nBpIhQBcFH0XziRM+eeqMmg+WF t2e/oRWBUbVGq1W8KU8ny9eIF/CYt5Rhkkg4DAq+k9bKuK60zd5M91eTR baIQWxKhrRrcrH4qwRJh96xJza+ufWDixP0xMjI9Cql6veI1chNBPj+5X SV6IsOXOsVOYbJVgWg50w7EFLGb/LgeZ09RHy9Po2MXn22hLhyainy2Vr RpMaIPV3iOl9+RbuTdZUzU8hCwA4rM8K9YzCjwP3Kf7Ba+KrHucoTYyL/ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="8645421" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="8645421" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:13:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="840692232" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="840692232" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.249.144.211]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:13:42 -0800 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , David Gow , Rae Moar Subject: [PATCH 1/2] kunit: Add example for using test->priv Date: Fri, 15 Dec 2023 16:13:26 +0100 Message-Id: <20231215151327.1835-2-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20231215151327.1835-1-michal.wajdeczko@intel.com> References: <20231215151327.1835-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In a test->priv field the user can store arbitrary data. Add example how to use this feature in the test code. Signed-off-by: Michal Wajdeczko Cc: David Gow Cc: Rae Moar Reviewed-by: David Gow --- lib/kunit/kunit-example-test.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 6bb5c2ef6696..d7dfcf209ece 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -221,6 +221,20 @@ static void example_params_test(struct kunit *test) KUNIT_EXPECT_EQ(test, param->value % param->value, 0); } +/* + * This test shows the use of test->priv. + */ +static void example_priv_test(struct kunit *test) +{ + /* unless setup in suite->init(), test->priv is NULL */ + KUNIT_ASSERT_NULL(test, test->priv); + + /* but can be used to pass arbitrary data to other functions */ + test->priv = kunit_kzalloc(test, 1, GFP_KERNEL); + KUNIT_EXPECT_NOT_NULL(test, test->priv); + KUNIT_ASSERT_PTR_EQ(test, test->priv, kunit_get_current_test()->priv); +} + /* * This test should always pass. Can be used to practice filtering attributes. */ @@ -245,6 +259,7 @@ static struct kunit_case example_test_cases[] = { KUNIT_CASE(example_mark_skipped_test), KUNIT_CASE(example_all_expect_macros_test), KUNIT_CASE(example_static_stub_test), + KUNIT_CASE(example_priv_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), KUNIT_CASE_SLOW(example_slow_test), {} From patchwork Fri Dec 15 15:13:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Wajdeczko X-Patchwork-Id: 754710 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 835733C49E for ; Fri, 15 Dec 2023 15:13:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hZgAHEf4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702653227; x=1734189227; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xXzAs03rS7Wh150F/MXgoEdBkwMgUJfCLapM46KW2Cs=; b=hZgAHEf49yOg18Rm5pLcqpUv8ydGc33TiSONXx4+PKcmXMjAle+Pthyn uOWj81WoEnpqiKVL5NaKZmrHzwazLL1/IblNK/9XPI5xSn2tvCOYeamQV 0qgOACtf57OOkix4ozDdd5OQqDtq5YyZsPs6Nlgerwe5uGg97O8vwqpQA YrRcujAc1zna05yFnDB45LpELPIiK+Q72SKVnS834jNl/SYtmr3xUMVxY SQg44IR732EQG0NGjC3uqRgBD3xYA16xO3zMINE5y0bsPshwkLXerjBmA fr1FheDhJYUdaimpGGN5EjoekjX5lC5Gr8FgPYYBQNMvfOoVjKN/dmBG5 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="8645424" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="8645424" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:13:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="840692235" X-IronPort-AV: E=Sophos;i="6.04,279,1695711600"; d="scan'208";a="840692235" Received: from mwajdecz-mobl.ger.corp.intel.com ([10.249.144.211]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Dec 2023 07:13:44 -0800 From: Michal Wajdeczko To: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Michal Wajdeczko , David Gow , Rae Moar Subject: [PATCH 2/2] kunit: Reset test->priv after each param iteration Date: Fri, 15 Dec 2023 16:13:27 +0100 Message-Id: <20231215151327.1835-3-michal.wajdeczko@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20231215151327.1835-1-michal.wajdeczko@intel.com> References: <20231215151327.1835-1-michal.wajdeczko@intel.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 If we run parameterized test that uses test->priv to prepare some custom data, then value of test->priv will leak to the next param iteration and may be unexpected. This could be easily seen if we promote example_priv_test to parameterized test as then only first test iteration will be successful: $ ./tools/testing/kunit/kunit.py run \ --kunitconfig ./lib/kunit/.kunitconfig *.example_priv* [ ] Starting KUnit Kernel (1/1)... [ ] ============================================================ [ ] =================== example (1 subtest) ==================== [ ] ==================== example_priv_test ==================== [ ] [PASSED] example value 3 [ ] # example_priv_test: initializing [ ] # example_priv_test: ASSERTION FAILED at lib/kunit/kunit-example-test.c:230 [ ] Expected test->priv == ((void *)0), but [ ] test->priv == 0000000060dfe290 [ ] ((void *)0) == 0000000000000000 [ ] # example_priv_test: cleaning up [ ] [FAILED] example value 2 [ ] # example_priv_test: initializing [ ] # example_priv_test: ASSERTION FAILED at lib/kunit/kunit-example-test.c:230 [ ] Expected test->priv == ((void *)0), but [ ] test->priv == 0000000060dfe290 [ ] ((void *)0) == 0000000000000000 [ ] # example_priv_test: cleaning up [ ] [FAILED] example value 1 [ ] # example_priv_test: initializing [ ] # example_priv_test: ASSERTION FAILED at lib/kunit/kunit-example-test.c:230 [ ] Expected test->priv == ((void *)0), but [ ] test->priv == 0000000060dfe290 [ ] ((void *)0) == 0000000000000000 [ ] # example_priv_test: cleaning up [ ] [FAILED] example value 0 [ ] # example_priv_test: initializing [ ] # example_priv_test: cleaning up [ ] # example_priv_test: pass:1 fail:3 skip:0 total:4 [ ] ================ [FAILED] example_priv_test ================ [ ] # example: initializing suite [ ] # module: kunit_example_test [ ] # example: exiting suite [ ] # Totals: pass:1 fail:3 skip:0 total:4 [ ] ===================== [FAILED] example ===================== Fix that by resetting test->priv after each param iteration, in similar way what we did for the test->status. Signed-off-by: Michal Wajdeczko Cc: David Gow Cc: Rae Moar Reviewed-by: David Gow --- lib/kunit/test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 0308865194bb..7255ca297399 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -622,6 +622,7 @@ int kunit_run_tests(struct kunit_suite *suite) test.param_index++; test.status = KUNIT_SUCCESS; test.status_comment[0] = '\0'; + test.priv = NULL; } }