From patchwork Tue Feb 11 00:31:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968423 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1DF23175D53; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=sBF0OeWcRKaP86ZP0DfjT9FY4fVWFR80Yi95W5yLh6N0IfIv94Jzjho9uXWVIpn+6Zuzz9nIS9Y+TREc1hDvWaJr+P0o4jiaxeR/V5Zma7n0lRgzZ6OR2mElKr0YpD2JXuUu3wzyKbw9S+2XGeEOx0edGKn0YPBT5jPj+WGZrbs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=H/CNbRKKIExSOBBo689bE82GEti0qTANuIwBEnwrJ3A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=p5xxV7umoCdq3ou2OR8+VB+z5k5ftQBeBgEYIg61ZnD7hVD0TUDUb1H9DfMXN8hdxSdzauwNbxd371p2GiB9Am1of9s5DoSqBeSR2P9HMrukoHTLwl0DgPyNHOhXuzWzeJ9KnqiDC/0uzI3Fnq9l3NFAh7REhCprKpf3TdegRo4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W0eCgAY3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="W0eCgAY3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95852C4CEDF; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233896; bh=H/CNbRKKIExSOBBo689bE82GEti0qTANuIwBEnwrJ3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W0eCgAY3KGcWv/sIaJ5aD8ymcs5tIBCJfdGXry2l9mysodb2P0mdEs16JvVqpzb5O NGW085IyD6myvhHD/j5Q4UPL7tEuVg6xNcrrBdh4vJWbEwHvlwnVsq84zrD0tk0ZSz CmSU/36mU0b19CkUxvx3cTC9ah4T9S5U6LskcACo/h6LbxAiE+E6XqRvTie/PsciUC TbxdCInDiuTCs/Xog5T3KAvigGSEkO2EtiZ4QPU0AJbdgd5fA6nS0tzGjdFwg+ozjV hL8Y9hqwVDn9yn9/d7k/fKTKhZDHSub4s/7psBkoaekK8Eu4qGknFGufKJDOQaOE35 45w1l+8q99ZDA== From: Kees Cook To: David Gow Cc: Kees Cook , Luis Felipe Hernandez , Nicolas Pitre , Rae Moar , Pedro Orlando , Danilo Pereira , Gabriela Bittencourt , Gabriel Krisman Bertazi , Shuah Khan , Diego Vieira , "Steven Rostedt (Google)" , Jakub Kicinski , "Masami Hiramatsu (Google)" , Vlastimil Babka , =?utf-8?q?Bruno_Sobreira_Fran=C3=A7a?= , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 1/6] lib: math: Move KUnit tests into tests/ subdir Date: Mon, 10 Feb 2025 16:31:29 -0800 Message-Id: <20250211003136.2860503-1-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2869; i=kees@kernel.org; h=from:subject; bh=B+Kg5uu/+lAyTwgFo1Ezlkz74SLAmioONnDEVzAUZxQ=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqVIF2kz37v4n/OKV6zPodcJnEvkFhle2XbwjfjzR yuzJO7v7ChlYRDjYpAVU2QJsnOPc/F42x7uPlcRZg4rE8gQBi5OAZhIVQwjwy+ffRHHt5o1Jemn 12cenxxfJnutSUdaU3nx6bc1vxTeMTEyzDKdVJnEPuHImx+WmfJb9wtObs++HnEjX2Q269tDoQ/ XMAIA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 From: Luis Felipe Hernandez This patch is a follow-up task from a discussion stemming from point 3 in a recent patch introducing the int_pow kunit test [1] and documentation regarding kunit test style and nomenclature [2]. Colocate all kunit test suites in lib/math/tests/ and follow recommended naming convention for files _kunit.c and kconfig entries CONFIG__KUNIT_TEST. Link: https://lore.kernel.org/all/CABVgOS=-vh5TqHFCq_jo=ffq8v_nGgr6JsPnOZag3e6+19ysxQ@mail.gmail.com/ [1] Link: https://docs.kernel.org/dev-tools/kunit/style.html [2] Signed-off-by: Luis Felipe Hernandez Acked-by: Nicolas Pitre Reviewed-by: David Gow Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-2-davidgow@google.com Signed-off-by: Kees Cook --- lib/Kconfig.debug | 2 +- lib/math/Makefile | 5 ++--- lib/math/tests/Makefile | 3 ++- lib/math/{rational-test.c => tests/rational_kunit.c} | 0 4 files changed, 5 insertions(+), 5 deletions(-) rename lib/math/{rational-test.c => tests/rational_kunit.c} (100%) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 1af972a92d06..b18dbfc53ca4 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3166,7 +3166,7 @@ config TEST_OBJPOOL If unsure, say N. -config INT_POW_TEST +config INT_POW_KUNIT_TEST tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS diff --git a/lib/math/Makefile b/lib/math/Makefile index 853f023ae537..d1caba23baa0 100644 --- a/lib/math/Makefile +++ b/lib/math/Makefile @@ -5,8 +5,7 @@ obj-$(CONFIG_CORDIC) += cordic.o obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o obj-$(CONFIG_RATIONAL) += rational.o -obj-$(CONFIG_INT_POW_TEST) += tests/int_pow_kunit.o obj-$(CONFIG_TEST_DIV64) += test_div64.o obj-$(CONFIG_TEST_MULDIV64) += test_mul_u64_u64_div_u64.o -obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o -obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += tests/int_sqrt_kunit.o \ No newline at end of file + +obj-y += tests/ diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile index e1a79f093b2d..66b10a7f5a84 100644 --- a/lib/math/tests/Makefile +++ b/lib/math/tests/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_INT_POW_TEST) += int_pow_kunit.o +obj-$(CONFIG_INT_POW_KUNIT_TEST) += int_pow_kunit.o +obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational_kunit.o obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o diff --git a/lib/math/rational-test.c b/lib/math/tests/rational_kunit.c similarity index 100% rename from lib/math/rational-test.c rename to lib/math/tests/rational_kunit.c From patchwork Tue Feb 11 00:31:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968422 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1DDF7524F; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=B/2puNQ91cjUPlpJKxihYWXCNN5AhHfk85Ia8KOxaBh5q3oB9O6F/aPw9r69hzHY4ff9Zuks//hn+7JIpwdeRFQuLLyPnVd49O5p0MqsCgJ2f2ZQc/DAc/anW5jGSX7JyDsaywBg/+O6csG2+NuX6wVKU0wGYKUhbsYF20T/pHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=BmsEnez1b0zRvTS3dQOIzftkHPfoxZVnxcnBemZHVVA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=Ce8IR1v14N60YHSchPWUyVwSnZjkh6yyb4ibfl0AmXv25U0pqsBUwIi41FF2etZc42AqxmN4wsKVcAVf8bUdHm6nEPdkkbp6re2BouxpOVCGffi8yxY5bOj6wPIQC2Ib4Q+3TJccwjrz4LnffVOGJqcjFKln56UHE1G6X9Tufnk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=B3YlhPnV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="B3YlhPnV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98D28C4CEEA; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233896; bh=BmsEnez1b0zRvTS3dQOIzftkHPfoxZVnxcnBemZHVVA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B3YlhPnVdOIT4e81h2O1aWlE3y2MmeG1thD9FvvPckVivtVpBOKCDtPqFfyfDiuHK BQ1rhHhZbiiSzUEn9p2FLpkfexdgq0ZhNZPaG8JDJakGSep8mnZ/5LRf6MCjlPrCA6 HxQleRDT52ioXdy0zySngNKlpkkXqsATiJk7WZ28frEuN3E+U/2hSWcvKwhdX5/2wK 7KhoERUBmq0ifrKX4dFUwFKWoGduvWAG5uoKDz1G5V1VlY9JIkmva2BMOPhoyTf6cE xARr5h+ZIjMCwM/3++8KBYgqjdn1xxVIqXNTedKqkfp6Is6ocePkl2n0Gh6hexJWQF 3pAYprgudHv2g== From: Kees Cook To: David Gow Cc: Kees Cook , =?utf-8?q?Bruno_Sobreira_Fran=C3=A7a?= , Rae Moar , Luis Felipe Hernandez , Nicolas Pitre , Pedro Orlando , Danilo Pereira , Gabriela Bittencourt , Gabriel Krisman Bertazi , Shuah Khan , Diego Vieira , "Steven Rostedt (Google)" , Jakub Kicinski , "Masami Hiramatsu (Google)" , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 2/6] lib/math: Add int_log test suite Date: Mon, 10 Feb 2025 16:31:30 -0800 Message-Id: <20250211003136.2860503-2-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4361; i=kees@kernel.org; h=from:subject; bh=mkouk3rvbOl7z7ij3vA30BSZx3TjW2NkPOI4lLL6SNM=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqWecnqy2iG+/peNzdr9jwNaime/2Lpvho6MxWEx0 /Klr17d6yhlYRDjYpAVU2QJsnOPc/F42x7uPlcRZg4rE8gQBi5OAZiIugzDP02Fiz3bTZRWbBbO r389oVFQ5uTcFSk51jMfFnt+/3SkOoWR4ZC7F9PtpLtl3pHhz+dteL3xwol87xtznqVZXNEUdLO o5wIA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 From: Bruno Sobreira França This commit introduces KUnit tests for the intlog2 and intlog10 functions, which compute logarithms in base 2 and base 10, respectively. The tests cover a range of inputs to ensure the correctness of these functions across common and edge cases. Signed-off-by: Bruno Sobreira França Reviewed-by: David Gow Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-3-davidgow@google.com Signed-off-by: Kees Cook --- lib/Kconfig.debug | 11 +++++ lib/math/tests/Makefile | 1 + lib/math/tests/int_log_kunit.c | 74 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/math/tests/int_log_kunit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index b18dbfc53ca4..f30929ed1a84 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3197,6 +3197,17 @@ config INT_SQRT_KUNIT_TEST If unsure, say N +config INT_LOG_KUNIT_TEST + tristate "Integer log (int_log) test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option enables the KUnit test suite for the int_log library, which + provides two functions to compute the integer logarithm in base 2 and + base 10, called respectively as intlog2 and intlog10. + + If unsure, say N + endif # RUNTIME_TESTING_MENU config ARCH_USE_MEMTEST diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile index 66b10a7f5a84..f139b73c65cb 100644 --- a/lib/math/tests/Makefile +++ b/lib/math/tests/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_INT_POW_KUNIT_TEST) += int_pow_kunit.o +obj-$(CONFIG_INT_LOG_KUNIT_TEST) += int_log_kunit.o obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational_kunit.o obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o diff --git a/lib/math/tests/int_log_kunit.c b/lib/math/tests/int_log_kunit.c new file mode 100644 index 000000000000..14e854146cb4 --- /dev/null +++ b/lib/math/tests/int_log_kunit.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +struct test_case_params { + u32 value; + unsigned int expected_result; + const char *name; +}; + + +/* The expected result takes into account the log error */ +static const struct test_case_params intlog2_params[] = { + {0, 0, "Log base 2 of 0"}, + {1, 0, "Log base 2 of 1"}, + {2, 16777216, "Log base 2 of 2"}, + {3, 26591232, "Log base 2 of 3"}, + {4, 33554432, "Log base 2 of 4"}, + {8, 50331648, "Log base 2 of 8"}, + {16, 67108864, "Log base 2 of 16"}, + {32, 83886080, "Log base 2 of 32"}, + {U32_MAX, 536870911, "Log base 2 of MAX"}, +}; + +static const struct test_case_params intlog10_params[] = { + {0, 0, "Log base 10 of 0"}, + {1, 0, "Log base 10 of 1"}, + {6, 13055203, "Log base 10 of 6"}, + {10, 16777225, "Log base 10 of 10"}, + {100, 33554450, "Log base 10 of 100"}, + {1000, 50331675, "Log base 10 of 1000"}, + {10000, 67108862, "Log base 10 of 10000"}, + {U32_MAX, 161614247, "Log base 10 of MAX"} +}; + +static void get_desc(const struct test_case_params *tc, char *desc) +{ + strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE); +} + + +KUNIT_ARRAY_PARAM(intlog2, intlog2_params, get_desc); + +static void intlog2_test(struct kunit *test) +{ + const struct test_case_params *tc = (const struct test_case_params *)test->param_value; + + KUNIT_EXPECT_EQ(test, tc->expected_result, intlog2(tc->value)); +} + +KUNIT_ARRAY_PARAM(intlog10, intlog10_params, get_desc); + +static void intlog10_test(struct kunit *test) +{ + const struct test_case_params *tc = (const struct test_case_params *)test->param_value; + + KUNIT_EXPECT_EQ(test, tc->expected_result, intlog10(tc->value)); +} + +static struct kunit_case math_int_log_test_cases[] = { + KUNIT_CASE_PARAM(intlog2_test, intlog2_gen_params), + KUNIT_CASE_PARAM(intlog10_test, intlog10_gen_params), + {} +}; + +static struct kunit_suite int_log_test_suite = { + .name = "math-int_log", + .test_cases = math_int_log_test_cases, +}; + +kunit_test_suites(&int_log_test_suite); + +MODULE_DESCRIPTION("math.int_log KUnit test suite"); +MODULE_LICENSE("GPL"); From patchwork Tue Feb 11 00:31:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968425 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1DE45165F13; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=fi1Ujhsu2E/Enwv1mjYZ3FajsAZp7UqC+CYifARoSEuWFaAwnW3qObc1k7W0+qzrRBVcKPxuHzPT1L8usbpOdYge/2wEgWv5tbJvuGz3YsaSUgvn7Nol89tTGAXS/4maKuLYVStH9/suh3U/USeBmQ5qXfD9ImD447Yylwsp9es= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=2aBcGYde6FqKkGz9SbdnoeL7YJGk9ss6Wg4DyxvPqHs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SqLTCAJ8m0XTNMqPEnOYvwYuYA6akHcdQZtsIrdMhWzBCVDG2CLphay1VEjDrrCEEHLmuevJuE2mnK83UG1OvhbNMA9g2SmeizgSV1ULG4iUj4cqPQuUjaMRrxwwNQwQ9YjsXViVhEpXhz6cPp7cbGxeKmRueS/9oWix46DzEzg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ax9sibUj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ax9sibUj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E85DC4CEE7; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233896; bh=2aBcGYde6FqKkGz9SbdnoeL7YJGk9ss6Wg4DyxvPqHs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ax9sibUjQwEcsIGBlYJjiUPyBGuyF29oZsfqj1TOtTNWh8H3rcbVmzM4eBySOQS1F 1+Fb+QqVHViQWzw0fJYKw4CHDyoCelcEybUtQjoUUfpSfrJMj4EOlqve6uOg+nOKxD eM0w8kU37EvIXUZCUTANqTMGM2mwSIej33il51RnJ+EMlM0eIOhelSyiXyyDPQUuii Rg7tPkWyR9urmYMw86u8+tkNcwu8FnLD3Wt8jZf4nITcIMjUzYm9F3aLeedfefUkSQ fF7bgzUogZeDiFf4NIGVXiodB1wcuhvmm0byHBl5kGzh6iA08iwEk5kzrGbixGmGTY kIdKZAyZhHMbw== From: Kees Cook To: David Gow Cc: Kees Cook , Steven Rostedt , Jakub Kicinski , Masami Hiramatsu , Vlastimil Babka , Rae Moar , Luis Felipe Hernandez , Nicolas Pitre , Pedro Orlando , Danilo Pereira , Gabriela Bittencourt , Gabriel Krisman Bertazi , Shuah Khan , Diego Vieira , =?utf-8?q?Bruno_Sobreir?= =?utf-8?q?a_Fran=C3=A7a?= , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 3/6] lib: Move KUnit tests into tests/ subdirectory Date: Mon, 10 Feb 2025 16:31:31 -0800 Message-Id: <20250211003136.2860503-3-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=16536; i=kees@kernel.org; h=from:subject; bh=2aBcGYde6FqKkGz9SbdnoeL7YJGk9ss6Wg4DyxvPqHs=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqVqL/+ysfRDUWC2/j2B1rPlh/eIbI1LiBVddd/zd tn2ffcmdJSyMIhxMciKKbIE2bnHuXi8bQ93n6sIM4eVCWQIAxenAEzE+jfDHy4OvT1TfHdt9XZe 9Z7Bc8KyFW3yRlYZH3v4fU8tiK4vy2P4Z87wIJX7KmOi+/kWfWunayZvJusW2/YIiSbdeLC9Kii QFQA= X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Following from the recent KUnit file naming discussion[1], move all KUnit tests in lib/ into lib/tests/. Link: https://lore.kernel.org/lkml/20240720165441.it.320-kees@kernel.org/ [1] Acked-by: Steven Rostedt (Google) Acked-by: Jakub Kicinski Acked-by: Masami Hiramatsu (Google) Reviewed-by: David Gow Acked-by: Vlastimil Babka Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-4-davidgow@google.com Signed-off-by: Kees Cook --- MAINTAINERS | 19 ++++++------ lib/Makefile | 39 +------------------------ lib/tests/Makefile | 40 ++++++++++++++++++++++++++ lib/{ => tests}/bitfield_kunit.c | 0 lib/{ => tests}/checksum_kunit.c | 0 lib/{ => tests}/cmdline_kunit.c | 0 lib/{ => tests}/cpumask_kunit.c | 0 lib/{ => tests}/crc_kunit.c | 0 lib/{ => tests}/fortify_kunit.c | 0 lib/{ => tests}/hashtable_test.c | 0 lib/{ => tests}/is_signed_type_kunit.c | 0 lib/{ => tests}/kunit_iov_iter.c | 0 lib/{ => tests}/list-test.c | 0 lib/{ => tests}/memcpy_kunit.c | 0 lib/{ => tests}/overflow_kunit.c | 0 lib/{ => tests}/siphash_kunit.c | 0 lib/{ => tests}/slub_kunit.c | 0 lib/{ => tests}/stackinit_kunit.c | 0 lib/{ => tests}/string_helpers_kunit.c | 0 lib/{ => tests}/string_kunit.c | 0 lib/{ => tests}/test_bits.c | 0 lib/{ => tests}/test_fprobe.c | 0 lib/{ => tests}/test_hash.c | 0 lib/{ => tests}/test_kprobes.c | 0 lib/{ => tests}/test_linear_ranges.c | 0 lib/{ => tests}/test_list_sort.c | 0 lib/{ => tests}/test_sort.c | 0 lib/{ => tests}/usercopy_kunit.c | 0 lib/{ => tests}/util_macros_kunit.c | 0 29 files changed, 51 insertions(+), 47 deletions(-) rename lib/{ => tests}/bitfield_kunit.c (100%) rename lib/{ => tests}/checksum_kunit.c (100%) rename lib/{ => tests}/cmdline_kunit.c (100%) rename lib/{ => tests}/cpumask_kunit.c (100%) rename lib/{ => tests}/crc_kunit.c (100%) rename lib/{ => tests}/fortify_kunit.c (100%) rename lib/{ => tests}/hashtable_test.c (100%) rename lib/{ => tests}/is_signed_type_kunit.c (100%) rename lib/{ => tests}/kunit_iov_iter.c (100%) rename lib/{ => tests}/list-test.c (100%) rename lib/{ => tests}/memcpy_kunit.c (100%) rename lib/{ => tests}/overflow_kunit.c (100%) rename lib/{ => tests}/siphash_kunit.c (100%) rename lib/{ => tests}/slub_kunit.c (100%) rename lib/{ => tests}/stackinit_kunit.c (100%) rename lib/{ => tests}/string_helpers_kunit.c (100%) rename lib/{ => tests}/string_kunit.c (100%) rename lib/{ => tests}/test_bits.c (100%) rename lib/{ => tests}/test_fprobe.c (100%) rename lib/{ => tests}/test_hash.c (100%) rename lib/{ => tests}/test_kprobes.c (100%) rename lib/{ => tests}/test_linear_ranges.c (100%) rename lib/{ => tests}/test_list_sort.c (100%) rename lib/{ => tests}/test_sort.c (100%) rename lib/{ => tests}/usercopy_kunit.c (100%) rename lib/{ => tests}/util_macros_kunit.c (100%) diff --git a/MAINTAINERS b/MAINTAINERS index 25c86f47353d..78be36fb1a74 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4017,10 +4017,10 @@ F: include/vdso/bits.h F: lib/bitmap-str.c F: lib/bitmap.c F: lib/cpumask.c -F: lib/cpumask_kunit.c F: lib/find_bit.c F: lib/find_bit_benchmark.c F: lib/test_bitmap.c +F: lib/tests/cpumask_kunit.c F: tools/include/linux/bitfield.h F: tools/include/linux/bitmap.h F: tools/include/linux/bits.h @@ -9065,9 +9065,10 @@ L: linux-hardening@vger.kernel.org S: Supported T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening F: include/linux/fortify-string.h -F: lib/fortify_kunit.c -F: lib/memcpy_kunit.c F: lib/test_fortify/* +F: lib/tests/fortify_kunit.c +F: lib/tests/memcpy_kunit.c +F: scripts/test_fortify.sh K: \bunsafe_memcpy\b K: \b__NO_FORTIFY\b @@ -9755,9 +9756,9 @@ F: include/linux/string.h F: include/linux/string_choices.h F: include/linux/string_helpers.h F: lib/string.c -F: lib/string_kunit.c F: lib/string_helpers.c -F: lib/string_helpers_kunit.c +F: lib/tests/string_helpers_kunit.c +F: lib/tests/string_kunit.c F: scripts/coccinelle/api/string_choices.cocci GENERIC UIO DRIVER FOR PCI DEVICES @@ -12985,7 +12986,7 @@ F: Documentation/trace/kprobes.rst F: include/asm-generic/kprobes.h F: include/linux/kprobes.h F: kernel/kprobes.c -F: lib/test_kprobes.c +F: lib/tests/test_kprobes.c F: samples/kprobes KS0108 LCD CONTROLLER DRIVER @@ -13315,7 +13316,7 @@ M: Mark Brown R: Matti Vaittinen F: include/linux/linear_range.h F: lib/linear_ranges.c -F: lib/test_linear_ranges.c +F: lib/tests/test_linear_ranges.c LINUX FOR POWER MACINTOSH L: linuxppc-dev@lists.ozlabs.org @@ -13443,7 +13444,7 @@ M: David Gow L: linux-kselftest@vger.kernel.org L: kunit-dev@googlegroups.com S: Maintained -F: lib/list-test.c +F: lib/tests/list-test.c LITEX PLATFORM M: Karol Gugala @@ -21732,7 +21733,7 @@ M: Jason A. Donenfeld S: Maintained F: include/linux/siphash.h F: lib/siphash.c -F: lib/siphash_kunit.c +F: lib/tests/siphash_kunit.c SIS 190 ETHERNET DRIVER M: Francois Romieu diff --git a/lib/Makefile b/lib/Makefile index d5cfc7afbbb8..1e886482a6a3 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -52,9 +52,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \ percpu-refcount.o rhashtable.o base64.o \ once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \ generic-radix-tree.o bitmap-str.o -obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o obj-y += string_helpers.o -obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o obj-y += hexdump.o obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o obj-y += kstrtox.o @@ -65,22 +63,17 @@ obj-$(CONFIG_TEST_DHRY) += test_dhry.o obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o obj-$(CONFIG_TEST_BITOPS) += test_bitops.o CFLAGS_test_bitops.o += -Werror -obj-$(CONFIG_CPUMASK_KUNIT_TEST) += cpumask_kunit.o obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o -obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o -obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o obj-$(CONFIG_TEST_IDA) += test_ida.o obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla) CFLAGS_test_ubsan.o += $(call cc-disable-warning, unused-but-set-variable) UBSAN_SANITIZE_test_ubsan.o := y obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o -obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o obj-$(CONFIG_TEST_LKM) += test_module.o obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o -obj-$(CONFIG_TEST_SORT) += test_sort.o obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o obj-$(CONFIG_TEST_DYNAMIC_DEBUG) += test_dynamic_debug.o @@ -98,7 +91,6 @@ obj-$(CONFIG_TEST_XARRAY) += test_xarray.o obj-$(CONFIG_TEST_MAPLE_TREE) += test_maple_tree.o obj-$(CONFIG_TEST_PARMAN) += test_parman.o obj-$(CONFIG_TEST_KMOD) += test_kmod.o -obj-$(CONFIG_TEST_RUNTIME) += tests/ obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o @@ -107,10 +99,7 @@ obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o obj-$(CONFIG_TEST_HMM) += test_hmm.o obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o -obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o -CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE) -obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o obj-$(CONFIG_TEST_OBJPOOL) += test_objpool.o obj-$(CONFIG_TEST_FPU) += test_fpu.o @@ -132,7 +121,7 @@ endif obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any) -obj-y += math/ crypto/ +obj-y += math/ crypto/ tests/ obj-$(CONFIG_GENERIC_IOMAP) += iomap.o obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o @@ -368,32 +357,6 @@ obj-$(CONFIG_OBJAGG) += objagg.o # pldmfw library obj-$(CONFIG_PLDMFW) += pldmfw/ -# KUnit tests -CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) -obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o -obj-$(CONFIG_CHECKSUM_KUNIT) += checksum_kunit.o -obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o -obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o -obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o -obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o -obj-$(CONFIG_BITS_TEST) += test_bits.o -obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o -obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o -obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o -obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o -CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare) -obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o -CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) -obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o -CFLAGS_fortify_kunit.o += $(call cc-disable-warning, unsequenced) -CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-overread) -CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-truncation) -CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN) -obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o -obj-$(CONFIG_CRC_KUNIT_TEST) += crc_kunit.o -obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o -obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o - obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o obj-$(CONFIG_FIRMWARE_TABLE) += fw_table.o diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 8e4f42cb9c54..c7b5170359c1 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -1 +1,41 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for tests of kernel library functions. + +# KUnit tests +CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) +obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o +obj-$(CONFIG_BITS_TEST) += test_bits.o +obj-$(CONFIG_CHECKSUM_KUNIT) += checksum_kunit.o +obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o +obj-$(CONFIG_CPUMASK_KUNIT_TEST) += cpumask_kunit.o +obj-$(CONFIG_CRC_KUNIT_TEST) += crc_kunit.o +CFLAGS_fortify_kunit.o += $(call cc-disable-warning, unsequenced) +CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-overread) +CFLAGS_fortify_kunit.o += $(call cc-disable-warning, stringop-truncation) +CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN) +obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o +CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE) +obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o +obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o +obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o +obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o +obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o +obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o +obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o +obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o +obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o +obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o +CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare) +obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o +obj-$(CONFIG_SIPHASH_KUNIT_TEST) += siphash_kunit.o +obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o +obj-$(CONFIG_TEST_SORT) += test_sort.o +CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) +obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o +obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o +obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o +obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o +obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o + obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/ diff --git a/lib/bitfield_kunit.c b/lib/tests/bitfield_kunit.c similarity index 100% rename from lib/bitfield_kunit.c rename to lib/tests/bitfield_kunit.c diff --git a/lib/checksum_kunit.c b/lib/tests/checksum_kunit.c similarity index 100% rename from lib/checksum_kunit.c rename to lib/tests/checksum_kunit.c diff --git a/lib/cmdline_kunit.c b/lib/tests/cmdline_kunit.c similarity index 100% rename from lib/cmdline_kunit.c rename to lib/tests/cmdline_kunit.c diff --git a/lib/cpumask_kunit.c b/lib/tests/cpumask_kunit.c similarity index 100% rename from lib/cpumask_kunit.c rename to lib/tests/cpumask_kunit.c diff --git a/lib/crc_kunit.c b/lib/tests/crc_kunit.c similarity index 100% rename from lib/crc_kunit.c rename to lib/tests/crc_kunit.c diff --git a/lib/fortify_kunit.c b/lib/tests/fortify_kunit.c similarity index 100% rename from lib/fortify_kunit.c rename to lib/tests/fortify_kunit.c diff --git a/lib/hashtable_test.c b/lib/tests/hashtable_test.c similarity index 100% rename from lib/hashtable_test.c rename to lib/tests/hashtable_test.c diff --git a/lib/is_signed_type_kunit.c b/lib/tests/is_signed_type_kunit.c similarity index 100% rename from lib/is_signed_type_kunit.c rename to lib/tests/is_signed_type_kunit.c diff --git a/lib/kunit_iov_iter.c b/lib/tests/kunit_iov_iter.c similarity index 100% rename from lib/kunit_iov_iter.c rename to lib/tests/kunit_iov_iter.c diff --git a/lib/list-test.c b/lib/tests/list-test.c similarity index 100% rename from lib/list-test.c rename to lib/tests/list-test.c diff --git a/lib/memcpy_kunit.c b/lib/tests/memcpy_kunit.c similarity index 100% rename from lib/memcpy_kunit.c rename to lib/tests/memcpy_kunit.c diff --git a/lib/overflow_kunit.c b/lib/tests/overflow_kunit.c similarity index 100% rename from lib/overflow_kunit.c rename to lib/tests/overflow_kunit.c diff --git a/lib/siphash_kunit.c b/lib/tests/siphash_kunit.c similarity index 100% rename from lib/siphash_kunit.c rename to lib/tests/siphash_kunit.c diff --git a/lib/slub_kunit.c b/lib/tests/slub_kunit.c similarity index 100% rename from lib/slub_kunit.c rename to lib/tests/slub_kunit.c diff --git a/lib/stackinit_kunit.c b/lib/tests/stackinit_kunit.c similarity index 100% rename from lib/stackinit_kunit.c rename to lib/tests/stackinit_kunit.c diff --git a/lib/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c similarity index 100% rename from lib/string_helpers_kunit.c rename to lib/tests/string_helpers_kunit.c diff --git a/lib/string_kunit.c b/lib/tests/string_kunit.c similarity index 100% rename from lib/string_kunit.c rename to lib/tests/string_kunit.c diff --git a/lib/test_bits.c b/lib/tests/test_bits.c similarity index 100% rename from lib/test_bits.c rename to lib/tests/test_bits.c diff --git a/lib/test_fprobe.c b/lib/tests/test_fprobe.c similarity index 100% rename from lib/test_fprobe.c rename to lib/tests/test_fprobe.c diff --git a/lib/test_hash.c b/lib/tests/test_hash.c similarity index 100% rename from lib/test_hash.c rename to lib/tests/test_hash.c diff --git a/lib/test_kprobes.c b/lib/tests/test_kprobes.c similarity index 100% rename from lib/test_kprobes.c rename to lib/tests/test_kprobes.c diff --git a/lib/test_linear_ranges.c b/lib/tests/test_linear_ranges.c similarity index 100% rename from lib/test_linear_ranges.c rename to lib/tests/test_linear_ranges.c diff --git a/lib/test_list_sort.c b/lib/tests/test_list_sort.c similarity index 100% rename from lib/test_list_sort.c rename to lib/tests/test_list_sort.c diff --git a/lib/test_sort.c b/lib/tests/test_sort.c similarity index 100% rename from lib/test_sort.c rename to lib/tests/test_sort.c diff --git a/lib/usercopy_kunit.c b/lib/tests/usercopy_kunit.c similarity index 100% rename from lib/usercopy_kunit.c rename to lib/tests/usercopy_kunit.c diff --git a/lib/util_macros_kunit.c b/lib/tests/util_macros_kunit.c similarity index 100% rename from lib/util_macros_kunit.c rename to lib/tests/util_macros_kunit.c From patchwork Tue Feb 11 00:31:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968426 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 61C7A1ADFFE; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=PfZ/4O5jk80cHmUqEfdSTkwUOl/hCBS0oChHYrF1PL9HI3rG+uV59mBigojeOV45C5YVRtJ7CF+wzgNX/ny5T5mSXBYF60QW3VfwbRWaKevQ5Uj9Rrl4az6PgDp+u1lpA8pFV7v4pqlcn3uzp8VmpTpU+zZVQdlFkgv4ULkDIEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=WGZuTbxYOAFluXZ4IC0Bm7g2+8EWJsIWVjl3lQjchYM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sU0M0662VZO43JFIiJB2r38RUC0OyAEMGOrwoMz5ojnSyCBC9lJD9tK5h9kLYKPEoSNrZuMuQ3/HoSVWbIgTcvzRUJoPMnCl9pby6Jnr0kZmRijsgASrbTJuqW1AMskJ6k3w1rt/XZYD44T7SJ065DXD0/cpHtqZXiaLE3DlIVk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bgx2obp/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bgx2obp/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BAFB8C4CEEC; Tue, 11 Feb 2025 00:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233896; bh=WGZuTbxYOAFluXZ4IC0Bm7g2+8EWJsIWVjl3lQjchYM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bgx2obp/te7UMJbnT3tAu/ZaVwiLtV6yIBM30P89oSArwDs14BbZalhufZZS8ShtU 9zYXnbw8ZZDX81147gLPNl1KylUF/s6rUtyZ3JmrLyaXtmq12olkRKOSdKC+zcbF3k 1WzbpH+H5HW+64Y4+ngpBWlh8LGsHHB9K+CLWkzWv5aJlHEAFNamtFI1NdflcVZRJq rKoqQhghzjbfeEuKvK6H8z1Rn9rIbvg27hKVVw7wfPs04EX3D8lZMeyA/4vRjoxH7o TMoaffYlR+T4wNvFXS/aPygSp7SE14tXm635vzCBq2S7poTaRfVTEY9U7JlN2gCeyG 4fp37g50pooRg== From: Kees Cook To: David Gow Cc: Kees Cook , Diego Vieira , Rae Moar , Luis Felipe Hernandez , Nicolas Pitre , Pedro Orlando , Danilo Pereira , Gabriela Bittencourt , Gabriel Krisman Bertazi , Shuah Khan , "Steven Rostedt (Google)" , Jakub Kicinski , "Masami Hiramatsu (Google)" , Vlastimil Babka , =?utf-8?q?Bruno_Sobreira_Fran=C3=A7a?= , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 4/6] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Date: Mon, 10 Feb 2025 16:31:32 -0800 Message-Id: <20250211003136.2860503-4-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=9560; i=kees@kernel.org; h=from:subject; bh=DaWH7cZmxPkhcrCJEElhv1Dp00L14m9GtO5NAvLi20g=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqVavs2cfi4rsP6m0k+GjasyVXZecZcMi+G0Ps+3L Ds8gudDRykLgxgXg6yYIkuQnXuci8fb9nD3uYowc1iZQIYwcHEKwESmXWf4p5OltIBvVm/50p/V i7edcPi5hMPXjEMinFPDf/f9jOwvXgx/OIPKItZfcza9dPhXTU8D+7NHLpNXFPuqRL023etw438 hKwA= X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 From: Diego Vieira Add KUnit tests for the kfifo data structure. They test the vast majority of macros defined in the kfifo header (include/linux/kfifo.h). These are inspired by the existing tests for the doubly linked list in lib/tests/list-test.c (previously at lib/list-test.c) [1]. Note that this patch depends on the patch that moves the KUnit tests on lib/ into lib/tests/ [2]. [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/list-test.c?h=v6.11-rc6 [2] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/ Signed-off-by: Diego Vieira Reviewed-by: David Gow Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-5-davidgow@google.com Signed-off-by: Kees Cook --- lib/Kconfig.debug | 14 +++ lib/tests/Makefile | 1 + lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100644 lib/tests/kfifo_kunit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index f30929ed1a84..86ddf568cbca 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2691,6 +2691,20 @@ config SYSCTL_KUNIT_TEST If unsure, say N. +config KFIFO_KUNIT_TEST + tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the generic FIFO implementation KUnit test suite. + It tests that the API and basic functionality of the kfifo type + and associated macros. + + For more information on KUnit and unit tests in general please refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + config LIST_KUNIT_TEST tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/Makefile b/lib/tests/Makefile index c7b5170359c1..8696d778d92f 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -23,6 +23,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o +obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c new file mode 100644 index 000000000000..a85eedc3195a --- /dev/null +++ b/lib/tests/kfifo_kunit.c @@ -0,0 +1,224 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the generic kernel FIFO implementation. + * + * Copyright (C) 2024 Diego Vieira + */ +#include + +#include + +#define KFIFO_SIZE 32 +#define N_ELEMENTS 5 + +static void kfifo_test_reset_should_clear_the_fifo(struct kunit *test) +{ + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + kfifo_put(&my_fifo, 1); + kfifo_put(&my_fifo, 2); + kfifo_put(&my_fifo, 3); + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3); + + kfifo_reset(&my_fifo); + + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0); + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo)); +} + +static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test) +{ + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo)); + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo)); + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0); +} + +static void kfifo_test_len_should_ret_n_of_stored_elements(struct kunit *test) +{ + u8 buffer1[N_ELEMENTS]; + + for (int i = 0; i < N_ELEMENTS; i++) + buffer1[i] = i + 1; + + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0); + + kfifo_in(&my_fifo, buffer1, N_ELEMENTS); + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS); + + kfifo_in(&my_fifo, buffer1, N_ELEMENTS); + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS * 2); + + kfifo_reset(&my_fifo); + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0); +} + +static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test) +{ + u8 out_data = 0; + int processed_elements; + u8 elements[] = { 3, 5, 11 }; + + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + // If the fifo is empty, get returns 0 + processed_elements = kfifo_get(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 0); + KUNIT_EXPECT_EQ(test, out_data, 0); + + for (int i = 0; i < 3; i++) + kfifo_put(&my_fifo, elements[i]); + + for (int i = 0; i < 3; i++) { + processed_elements = kfifo_get(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 1); + KUNIT_EXPECT_EQ(test, out_data, elements[i]); + } +} + +static void kfifo_test_in_should_insert_multiple_elements(struct kunit *test) +{ + u8 in_buffer[] = { 11, 25, 65 }; + u8 out_data; + int processed_elements; + + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + kfifo_in(&my_fifo, in_buffer, 3); + + for (int i = 0; i < 3; i++) { + processed_elements = kfifo_get(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 1); + KUNIT_EXPECT_EQ(test, out_data, in_buffer[i]); + } +} + +static void kfifo_test_out_should_pop_multiple_elements(struct kunit *test) +{ + u8 in_buffer[] = { 11, 25, 65 }; + u8 out_buffer[3]; + int copied_elements; + + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + for (int i = 0; i < 3; i++) + kfifo_put(&my_fifo, in_buffer[i]); + + copied_elements = kfifo_out(&my_fifo, out_buffer, 3); + KUNIT_EXPECT_EQ(test, copied_elements, 3); + + for (int i = 0; i < 3; i++) + KUNIT_EXPECT_EQ(test, out_buffer[i], in_buffer[i]); + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo)); +} + +static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test) +{ + DECLARE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + INIT_KFIFO(my_fifo); + + // my_fifo is a struct with an inplace buffer + KUNIT_EXPECT_FALSE(test, __is_kfifo_ptr(&my_fifo)); + + KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo)); +} + +static void kfifo_test_define_should_equal_declare_init(struct kunit *test) +{ + // declare a variable my_fifo of type struct kfifo of u8 + DECLARE_KFIFO(my_fifo1, u8, KFIFO_SIZE); + // initialize the my_fifo variable + INIT_KFIFO(my_fifo1); + + // DEFINE_KFIFO declares the variable with the initial value + // essentially the same as calling DECLARE_KFIFO and INIT_KFIFO + DEFINE_KFIFO(my_fifo2, u8, KFIFO_SIZE); + + // my_fifo1 and my_fifo2 have the same size + KUNIT_EXPECT_EQ(test, sizeof(my_fifo1), sizeof(my_fifo2)); + KUNIT_EXPECT_EQ(test, kfifo_initialized(&my_fifo1), + kfifo_initialized(&my_fifo2)); + KUNIT_EXPECT_EQ(test, kfifo_is_empty(&my_fifo1), + kfifo_is_empty(&my_fifo2)); +} + +static void kfifo_test_alloc_should_initiliaze_a_ptr_fifo(struct kunit *test) +{ + int ret; + DECLARE_KFIFO_PTR(my_fifo, u8); + + INIT_KFIFO(my_fifo); + + // kfifo_initialized returns false signaling the buffer pointer is NULL + KUNIT_EXPECT_FALSE(test, kfifo_initialized(&my_fifo)); + + // kfifo_alloc allocates the buffer + ret = kfifo_alloc(&my_fifo, KFIFO_SIZE, GFP_KERNEL); + KUNIT_EXPECT_EQ_MSG(test, ret, 0, "Memory allocation should succeed"); + KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo)); + + // kfifo_free frees the buffer + kfifo_free(&my_fifo); +} + +static void kfifo_test_peek_should_not_remove_elements(struct kunit *test) +{ + u8 out_data; + int processed_elements; + + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE); + + // If the fifo is empty, peek returns 0 + processed_elements = kfifo_peek(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 0); + + kfifo_put(&my_fifo, 3); + kfifo_put(&my_fifo, 5); + kfifo_put(&my_fifo, 11); + + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3); + + processed_elements = kfifo_peek(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 1); + KUNIT_EXPECT_EQ(test, out_data, 3); + + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3); + + // Using peek doesn't remove the element + // so the read element and the fifo length + // remains the same + processed_elements = kfifo_peek(&my_fifo, &out_data); + KUNIT_EXPECT_EQ(test, processed_elements, 1); + KUNIT_EXPECT_EQ(test, out_data, 3); + + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3); +} + +static struct kunit_case kfifo_test_cases[] = { + KUNIT_CASE(kfifo_test_reset_should_clear_the_fifo), + KUNIT_CASE(kfifo_test_define_should_define_an_empty_fifo), + KUNIT_CASE(kfifo_test_len_should_ret_n_of_stored_elements), + KUNIT_CASE(kfifo_test_put_should_insert_and_get_should_pop), + KUNIT_CASE(kfifo_test_in_should_insert_multiple_elements), + KUNIT_CASE(kfifo_test_out_should_pop_multiple_elements), + KUNIT_CASE(kfifo_test_dec_init_should_define_an_empty_fifo), + KUNIT_CASE(kfifo_test_define_should_equal_declare_init), + KUNIT_CASE(kfifo_test_alloc_should_initiliaze_a_ptr_fifo), + KUNIT_CASE(kfifo_test_peek_should_not_remove_elements), + {}, +}; + +static struct kunit_suite kfifo_test_module = { + .name = "kfifo", + .test_cases = kfifo_test_cases, +}; + +kunit_test_suites(&kfifo_test_module); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Diego Vieira "); +MODULE_DESCRIPTION("KUnit test for the kernel FIFO"); From patchwork Tue Feb 11 00:31:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968428 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A03431C3F36; Tue, 11 Feb 2025 00:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=Fw5HRUv/QqgGiBSdZhyRsmSituRXbjqMyswXhKuuSrPaHmHM9UpnCM/71tsUQlPPLPJJ4CmLXThRHTta2C4o0Q+rWTteluHXiZd4HhDF7Aa4EK701CpFQ5zHSKvMVSVulh5BrMTrxMCUQrb6nR+UI4S3psm/JrLt/e7cDAgsb7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=/PR9AB+QpWzqEeX3zFKe7QITS4cHv803/0AieWg4tIY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=g1SUg/D9TPAm0L6yVFyQPDy0OuFnCnbdEk4hDvkxKDA8/YwHMcMMzqbDi646HctKWnrBfz+XrRjJ5IDNucIqQZbd77aOuyqDMk82qMozi2YC9ew9nhPnEzmB/Za6Y1nh2VXFfR+wh2N9OkxxjoGek2bohmUIalM4UUwyeaEzrJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qimTdQHk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qimTdQHk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 067CBC4CEF5; Tue, 11 Feb 2025 00:31:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233897; bh=/PR9AB+QpWzqEeX3zFKe7QITS4cHv803/0AieWg4tIY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qimTdQHkHrV7M7T/TX+bmLorH+5rf7VHKUGtE5rIQj3LRSvKlkoojVxxG0xEEkhBR jkSF7aT2xcjR1fiTCftT0OV9FG2TsS9HjYnCyVpVQ3gbn0jSL52wI7pvONxjcEIHfD 69fQtpKqbWbNVyoqflAt5ub96jO7FCjcvGhuqNrUMaCGGqt1ekisPidbuKFhar5bds WqhHqBciwHvQMsIp3pIugIrZSsyEZZF8e1V+xNJo0AuOYID+0xQC0slx+k9sD1Iw/O P4uZV9iN7CgfobCGkdnbK2DVBzFBfXm8F39oE0JWNhGKzjg4UMsr9ggOg+q51xdj7x hmCmdiQCTzZFA== From: Kees Cook To: David Gow Cc: Kees Cook , Gabriela Bittencourt , Pedro Orlando , Danilo Pereira , Gabriel Krisman Bertazi , Rae Moar , Luis Felipe Hernandez , Nicolas Pitre , Shuah Khan , Diego Vieira , "Steven Rostedt (Google)" , Jakub Kicinski , "Masami Hiramatsu (Google)" , Vlastimil Babka , =?utf-8?q?Bruno_Sobreira_Fran=C3=A7a?= , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 5/6] unicode: kunit: refactor selftest to kunit tests Date: Mon, 10 Feb 2025 16:31:33 -0800 Message-Id: <20250211003136.2860503-5-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11462; i=kees@kernel.org; h=from:subject; bh=qneY3+E6WQflVJ8FUJLVj8OKpCtnR1WKhTjP+Ezmn4M=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqUda+t6s7pcz37vjKLlXT7aYXt3L7Q9Yndx0bL/5 jde//qQ0FHKwiDGxSArpsgSZOce5+Lxtj3cfa4izBxWJpAhDFycAjCRa7cY/lk0sv9unah/gOvV w5A1l2u0qkpfL5giGSiyWMPVsftuBTMjw5mXO/wmKN258no1+36DhckChiFBDLE7bHf4J93gFtB 4xAoA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 From: Gabriela Bittencourt Refactoring 'test' functions into kunit tests, to test utf-8 support in unicode subsystem. This allows the utf8 tests to be run alongside the KUnit test suite using kunit-tool, quickly compiling and running all desired tests as part of the KUnit test suite, instead of compiling the selftest module and loading it. The refactoring kept the original testing logic intact, while adopting a testing pattern across different kernel modules and leveraging KUnit's benefits. Co-developed-by: Pedro Orlando Signed-off-by: Pedro Orlando Co-developed-by: Danilo Pereira Signed-off-by: Danilo Pereira Signed-off-by: Gabriela Bittencourt Reviewed-by: David Gow Acked-by: Gabriel Krisman Bertazi Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-6-davidgow@google.com Signed-off-by: Kees Cook --- fs/unicode/.kunitconfig | 3 + fs/unicode/Kconfig | 5 +- fs/unicode/Makefile | 2 +- fs/unicode/utf8-norm.c | 2 +- fs/unicode/utf8-selftest.c | 149 +++++++++++++++++-------------------- 5 files changed, 77 insertions(+), 84 deletions(-) create mode 100644 fs/unicode/.kunitconfig diff --git a/fs/unicode/.kunitconfig b/fs/unicode/.kunitconfig new file mode 100644 index 000000000000..62dd5c171f9c --- /dev/null +++ b/fs/unicode/.kunitconfig @@ -0,0 +1,3 @@ +CONFIG_KUNIT=y +CONFIG_UNICODE=y +CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST=y diff --git a/fs/unicode/Kconfig b/fs/unicode/Kconfig index da786a687fdc..4ad2c36550f1 100644 --- a/fs/unicode/Kconfig +++ b/fs/unicode/Kconfig @@ -10,6 +10,7 @@ config UNICODE be a separate loadable module that gets requested only when a file system actually use it. -config UNICODE_NORMALIZATION_SELFTEST +config UNICODE_NORMALIZATION_KUNIT_TEST tristate "Test UTF-8 normalization support" - depends on UNICODE + depends on UNICODE && KUNIT + default KUNIT_ALL_TESTS diff --git a/fs/unicode/Makefile b/fs/unicode/Makefile index e309afe2b2bb..37bbcbc628a1 100644 --- a/fs/unicode/Makefile +++ b/fs/unicode/Makefile @@ -4,7 +4,7 @@ ifneq ($(CONFIG_UNICODE),) obj-y += unicode.o endif obj-$(CONFIG_UNICODE) += utf8data.o -obj-$(CONFIG_UNICODE_NORMALIZATION_SELFTEST) += utf8-selftest.o +obj-$(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) += utf8-selftest.o unicode-y := utf8-norm.o utf8-core.o diff --git a/fs/unicode/utf8-norm.c b/fs/unicode/utf8-norm.c index 768f8ab448b8..7b998c99c88d 100644 --- a/fs/unicode/utf8-norm.c +++ b/fs/unicode/utf8-norm.c @@ -586,7 +586,7 @@ int utf8byte(struct utf8cursor *u8c) } } -#ifdef CONFIG_UNICODE_NORMALIZATION_SELFTEST_MODULE +#if IS_MODULE(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) EXPORT_SYMBOL_GPL(utf8version_is_supported); EXPORT_SYMBOL_GPL(utf8nlen); EXPORT_SYMBOL_GPL(utf8ncursor); diff --git a/fs/unicode/utf8-selftest.c b/fs/unicode/utf8-selftest.c index 5ddaf27b21a6..9476ab012baa 100644 --- a/fs/unicode/utf8-selftest.c +++ b/fs/unicode/utf8-selftest.c @@ -1,35 +1,15 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * Kernel module for testing utf-8 support. + * KUnit tests for utf-8 support. * * Copyright 2017 Collabora Ltd. */ -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - -#include -#include #include -#include +#include #include "utf8n.h" -static unsigned int failed_tests; -static unsigned int total_tests; - -#define _test(cond, func, line, fmt, ...) do { \ - total_tests++; \ - if (!cond) { \ - failed_tests++; \ - pr_err("test %s:%d Failed: %s%s", \ - func, line, #cond, (fmt?":":".")); \ - if (fmt) \ - pr_err(fmt, ##__VA_ARGS__); \ - } \ - } while (0) -#define test_f(cond, fmt, ...) _test(cond, __func__, __LINE__, fmt, ##__VA_ARGS__) -#define test(cond) _test(cond, __func__, __LINE__, "") - static const struct { /* UTF-8 strings in this vector _must_ be NULL-terminated. */ unsigned char str[10]; @@ -167,69 +147,74 @@ static int utf8cursor(struct utf8cursor *u8c, const struct unicode_map *um, return utf8ncursor(u8c, um, n, s, (unsigned int)-1); } -static void check_utf8_nfdi(struct unicode_map *um) +static void check_utf8_nfdi(struct kunit *test) { int i; struct utf8cursor u8c; + struct unicode_map *um = test->priv; for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { int len = strlen(nfdi_test_data[i].str); int nlen = strlen(nfdi_test_data[i].dec); int j = 0; unsigned char c; + int ret; + + KUNIT_EXPECT_EQ(test, utf8len(um, UTF8_NFDI, nfdi_test_data[i].str), nlen); + KUNIT_EXPECT_EQ(test, utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len), + nlen); - test((utf8len(um, UTF8_NFDI, nfdi_test_data[i].str) == nlen)); - test((utf8nlen(um, UTF8_NFDI, nfdi_test_data[i].str, len) == - nlen)); - if (utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str) < 0) - pr_err("can't create cursor\n"); + ret = utf8cursor(&u8c, um, UTF8_NFDI, nfdi_test_data[i].str); + KUNIT_EXPECT_TRUE_MSG(test, ret >= 0, "Can't create cursor\n"); while ((c = utf8byte(&u8c)) > 0) { - test_f((c == nfdi_test_data[i].dec[j]), - "Unexpected byte 0x%x should be 0x%x\n", - c, nfdi_test_data[i].dec[j]); + KUNIT_EXPECT_EQ_MSG(test, c, nfdi_test_data[i].dec[j], + "Unexpected byte 0x%x should be 0x%x\n", + c, nfdi_test_data[i].dec[j]); j++; } - test((j == nlen)); + KUNIT_EXPECT_EQ(test, j, nlen); } } -static void check_utf8_nfdicf(struct unicode_map *um) +static void check_utf8_nfdicf(struct kunit *test) { int i; struct utf8cursor u8c; + struct unicode_map *um = test->priv; for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { int len = strlen(nfdicf_test_data[i].str); int nlen = strlen(nfdicf_test_data[i].ncf); int j = 0; + int ret; unsigned char c; - test((utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str) == - nlen)); - test((utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len) == - nlen)); + KUNIT_EXPECT_EQ(test, utf8len(um, UTF8_NFDICF, nfdicf_test_data[i].str), + nlen); + KUNIT_EXPECT_EQ(test, utf8nlen(um, UTF8_NFDICF, nfdicf_test_data[i].str, len), + nlen); - if (utf8cursor(&u8c, um, UTF8_NFDICF, - nfdicf_test_data[i].str) < 0) - pr_err("can't create cursor\n"); + ret = utf8cursor(&u8c, um, UTF8_NFDICF, nfdicf_test_data[i].str); + KUNIT_EXPECT_TRUE_MSG(test, ret >= 0, "Can't create cursor\n"); while ((c = utf8byte(&u8c)) > 0) { - test_f((c == nfdicf_test_data[i].ncf[j]), - "Unexpected byte 0x%x should be 0x%x\n", - c, nfdicf_test_data[i].ncf[j]); + KUNIT_EXPECT_EQ_MSG(test, c, nfdicf_test_data[i].ncf[j], + "Unexpected byte 0x%x should be 0x%x\n", + c, nfdicf_test_data[i].ncf[j]); j++; } - test((j == nlen)); + KUNIT_EXPECT_EQ(test, j, nlen); } } -static void check_utf8_comparisons(struct unicode_map *table) +static void check_utf8_comparisons(struct kunit *test) { int i; + struct unicode_map *um = test->priv; for (i = 0; i < ARRAY_SIZE(nfdi_test_data); i++) { const struct qstr s1 = {.name = nfdi_test_data[i].str, @@ -237,8 +222,9 @@ static void check_utf8_comparisons(struct unicode_map *table) const struct qstr s2 = {.name = nfdi_test_data[i].dec, .len = sizeof(nfdi_test_data[i].dec)}; - test_f(!utf8_strncmp(table, &s1, &s2), - "%s %s comparison mismatch\n", s1.name, s2.name); + /* strncmp returns 0 when strings are equal */ + KUNIT_EXPECT_TRUE_MSG(test, utf8_strncmp(um, &s1, &s2) == 0, + "%s %s comparison mismatch\n", s1.name, s2.name); } for (i = 0; i < ARRAY_SIZE(nfdicf_test_data); i++) { @@ -247,62 +233,65 @@ static void check_utf8_comparisons(struct unicode_map *table) const struct qstr s2 = {.name = nfdicf_test_data[i].ncf, .len = sizeof(nfdicf_test_data[i].ncf)}; - test_f(!utf8_strncasecmp(table, &s1, &s2), - "%s %s comparison mismatch\n", s1.name, s2.name); + /* strncasecmp returns 0 when strings are equal */ + KUNIT_EXPECT_TRUE_MSG(test, utf8_strncasecmp(um, &s1, &s2) == 0, + "%s %s comparison mismatch\n", s1.name, s2.name); } } -static void check_supported_versions(struct unicode_map *um) +static void check_supported_versions(struct kunit *test) { + struct unicode_map *um = test->priv; /* Unicode 7.0.0 should be supported. */ - test(utf8version_is_supported(um, UNICODE_AGE(7, 0, 0))); + KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UNICODE_AGE(7, 0, 0))); /* Unicode 9.0.0 should be supported. */ - test(utf8version_is_supported(um, UNICODE_AGE(9, 0, 0))); + KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UNICODE_AGE(9, 0, 0))); /* Unicode 1x.0.0 (the latest version) should be supported. */ - test(utf8version_is_supported(um, UTF8_LATEST)); + KUNIT_EXPECT_TRUE(test, utf8version_is_supported(um, UTF8_LATEST)); /* Next versions don't exist. */ - test(!utf8version_is_supported(um, UNICODE_AGE(13, 0, 0))); - test(!utf8version_is_supported(um, UNICODE_AGE(0, 0, 0))); - test(!utf8version_is_supported(um, UNICODE_AGE(-1, -1, -1))); + KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(13, 0, 0))); + KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(0, 0, 0))); + KUNIT_EXPECT_FALSE(test, utf8version_is_supported(um, UNICODE_AGE(-1, -1, -1))); } -static int __init init_test_ucd(void) +static struct kunit_case unicode_normalization_test_cases[] = { + KUNIT_CASE(check_supported_versions), + KUNIT_CASE(check_utf8_comparisons), + KUNIT_CASE(check_utf8_nfdicf), + KUNIT_CASE(check_utf8_nfdi), + {} +}; + +static int init_test_ucd(struct kunit *test) { - struct unicode_map *um; + struct unicode_map *um = utf8_load(UTF8_LATEST); - failed_tests = 0; - total_tests = 0; + test->priv = um; - um = utf8_load(UTF8_LATEST); - if (IS_ERR(um)) { - pr_err("%s: Unable to load utf8 table.\n", __func__); - return PTR_ERR(um); - } + KUNIT_EXPECT_EQ_MSG(test, IS_ERR(um), 0, + "%s: Unable to load utf8 table.\n", __func__); - check_supported_versions(um); - check_utf8_nfdi(um); - check_utf8_nfdicf(um); - check_utf8_comparisons(um); - - if (!failed_tests) - pr_info("All %u tests passed\n", total_tests); - else - pr_err("%u out of %u tests failed\n", failed_tests, - total_tests); - utf8_unload(um); return 0; } -static void __exit exit_test_ucd(void) +static void exit_test_ucd(struct kunit *test) { + utf8_unload(test->priv); } -module_init(init_test_ucd); -module_exit(exit_test_ucd); +static struct kunit_suite unicode_normalization_test_suite = { + .name = "unicode_normalization", + .test_cases = unicode_normalization_test_cases, + .init = init_test_ucd, + .exit = exit_test_ucd, +}; + +kunit_test_suite(unicode_normalization_test_suite); + MODULE_AUTHOR("Gabriel Krisman Bertazi "); -MODULE_DESCRIPTION("Kernel module for testing utf-8 support"); +MODULE_DESCRIPTION("KUnit tests for utf-8 support."); MODULE_LICENSE("GPL"); From patchwork Tue Feb 11 00:31:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13968427 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A02951C3C1C; Tue, 11 Feb 2025 00:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; cv=none; b=OnE1J0TCF3UzXXqJDv7rge6HhByUGF4FTp9kbrcecYUI+IYPhPYudwS3k693T0kJp/nPsk0xSvz1VCXuZqXmIW2Z1kw12n1h+XAC0K2sCDvVtS4LPC0qKxkmqsIj738n8DuOVNxzY7g8aC5rdx4FTebSJ3v5ydRtYDwI/gHx9qQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739233897; c=relaxed/simple; bh=gAfzP3Akbt3rrJhscbWO+FbDpwpEfOValH1tmXtVu/Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=csriO/+ftKgqUzW7FUI/aRA6I8Y1u7yNPEGDgJhe9x5ZABPc4mw9vHC+NUpPAm0lsuWaLnw45PhRkhL87qmVLw34+SPBROImZnZt2yHjy2ulMjRCf6yLrwk7M8ya1oASt9Bw8RT0WIkcLwqGO/w8Nf5Bl/CtA5HnyEsUpTSVuy4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NQOSxTn+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NQOSxTn+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 043F1C4CEED; Tue, 11 Feb 2025 00:31:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739233897; bh=gAfzP3Akbt3rrJhscbWO+FbDpwpEfOValH1tmXtVu/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NQOSxTn+X5kq43kLaMi4OMg/Y89AeggPpgswLSdj7SFgIEDaeYf8pqY2ias7XpBqm kw3v1ykE9UDnQ+0T4gvyySd6pbK0X7HzG3L9Jog8KvS/Rtn8SvYlZ/HLUwycAyx6Yo qXHbaiwBZOp2EaBaeJhQcZ8nut8IL3g070zKFUzQIEX/OOWpu2FvwJKMnBu+csnVaR tatG15/yCJ9L6PlGE1kWmL7K7yfxk/zKixpdT4RiJj8ygpso7Qx34/Xx4IGqvDIDOI c00dGBgtPlL31P/iunYTXMaf3PIBIwvGKseoDasWV75oy8GVR/bvyg5/Sdag9PfG8z pCRlKVAEql7hg== From: Kees Cook To: David Gow Cc: Kees Cook , Gabriela Bittencourt , Pedro Orlando , Danilo Pereira , Gabriel Krisman Bertazi , Shuah Khan , Rae Moar , Luis Felipe Hernandez , Nicolas Pitre , Diego Vieira , "Steven Rostedt (Google)" , Jakub Kicinski , "Masami Hiramatsu (Google)" , Vlastimil Babka , =?utf-8?q?Bruno_Sobreira_Fran=C3=A7a?= , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Subject: [PATCH v2 6/6] unicode: kunit: change tests filename and path Date: Mon, 10 Feb 2025 16:31:34 -0800 Message-Id: <20250211003136.2860503-6-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250211002600.it.339-kees@kernel.org> References: <20250211002600.it.339-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1870; i=kees@kernel.org; h=from:subject; bh=HLI6DJk21kGeUIWW0nIc0fj9BIw5YtsNKNhJ+tB6yHU=; b=owGbwMvMwCVmps19z/KJym7G02pJDOmrZqUlJ39dzn/OrKxD6MfTp8fZrL9athwKmZNRyZUqv e/T4qLPHaUsDGJcDLJiiixBdu5xLh5v28Pd5yrCzGFlAhnCwMUpABOpnsnwm/XY1/ZCD6Nt5/Ze vrItSkj7yMYtqyX/+UzaHjL7qPYdvipGhsWLmSY7rVQs7oq4tWtXZNTiRp/TU7zf+KywCLM4p7N CixsA X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 From: Gabriela Bittencourt Change utf8 kunit test filename and path to follow the style convention on Documentation/dev-tools/kunit/style.rst Co-developed-by: Pedro Orlando Signed-off-by: Pedro Orlando Co-developed-by: Danilo Pereira Signed-off-by: Danilo Pereira Signed-off-by: Gabriela Bittencourt Reviewed-by: David Gow Acked-by: Gabriel Krisman Bertazi Reviewed-by: Shuah Khan Reviewed-by: Rae Moar Link: https://lore.kernel.org/r/20241202075545.3648096-7-davidgow@google.com Signed-off-by: Kees Cook --- fs/unicode/Makefile | 2 +- fs/unicode/{ => tests}/.kunitconfig | 0 fs/unicode/{utf8-selftest.c => tests/utf8_kunit.c} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename fs/unicode/{ => tests}/.kunitconfig (100%) rename fs/unicode/{utf8-selftest.c => tests/utf8_kunit.c} (100%) diff --git a/fs/unicode/Makefile b/fs/unicode/Makefile index 37bbcbc628a1..d95be7fb9f6b 100644 --- a/fs/unicode/Makefile +++ b/fs/unicode/Makefile @@ -4,7 +4,7 @@ ifneq ($(CONFIG_UNICODE),) obj-y += unicode.o endif obj-$(CONFIG_UNICODE) += utf8data.o -obj-$(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) += utf8-selftest.o +obj-$(CONFIG_UNICODE_NORMALIZATION_KUNIT_TEST) += tests/utf8_kunit.o unicode-y := utf8-norm.o utf8-core.o diff --git a/fs/unicode/.kunitconfig b/fs/unicode/tests/.kunitconfig similarity index 100% rename from fs/unicode/.kunitconfig rename to fs/unicode/tests/.kunitconfig diff --git a/fs/unicode/utf8-selftest.c b/fs/unicode/tests/utf8_kunit.c similarity index 100% rename from fs/unicode/utf8-selftest.c rename to fs/unicode/tests/utf8_kunit.c