From nobody Sat Feb 7 11:31:57 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB8613C464 for ; Fri, 1 Mar 2024 20:27:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709324856; cv=none; b=q6WVp9eMDWQDDzr9blPMKNRfh1jdjLBiomffAmMBPU6I5Dx+M9u69uyQ0iTIihRKcEiuIQKqGb+gnFrgGT5lxbqWfuVj5zWvaUAt7GdCR0WfAU3vySr44+J+CTX2DU98MM8frpFBXx+nVWaM/nHC/oFBUfZa83OCKSYYyaIQ0dA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709324856; c=relaxed/simple; bh=VwPmtfrEpMQZppZlXT0BVh7uJDWDRNLzilybx+4SCQM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jO9k93tqAA8gCOpwaVdAUXd42H7LHP3BcXjpr/uBEFXo4rMIgTWditRneu9gg5SwGRbLEUmxRSx+RqO9aDXZ/DsWgCjobjpW4co/BIzw/F7IFmXF9x0F3cbrZN76AwCxAu3tKVLX9TIe3JLQ0F+BAGpPUTlMmlUsMPEhaO/5GwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=kF5LUqKn; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="kF5LUqKn" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6e5b1c6daa3so1247620b3a.1 for ; Fri, 01 Mar 2024 12:27:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709324854; x=1709929654; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=++ygW/zrbGDkm7OEamsSzy5xkbe+NsVciJd37Dungz8=; b=kF5LUqKnuIVXes1UZfoHYG7RhK+EdqqV0JX655qiHf9Kq5s9vH2jDmvbX7lOcLrXsn aGtnK+1n2g2DbiGhr4lRDKePpH6JqGTbxTa7vfyvBQF4uN9H0rfNmmZG5A7AVRusgGZ5 ef4c9HneZK2KIt6uHofaRUUALf+w/JTiTGMpM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709324854; x=1709929654; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=++ygW/zrbGDkm7OEamsSzy5xkbe+NsVciJd37Dungz8=; b=iflVqFx2kXEsbPIpMg3n/dRxiWlZq8NKaiJQkSlLZtnyHPQptlB6rzjqT8DYABYKNE ZUVsJgAE/MoAoL9CezYBkFqa0hPYJT2XVssuPYdj7IA9RdcsJaxo22ZEZAMnSnIJF+g2 MIHr3qbTno1Y+hZ0wwHOCVm71ORWF1ZOWs1pxJqSfGzuaAcrT/cf2fwDGVzbOvjkRd60 Px9VJyGxkbXTjIr+U9eFjMtdPheVuOIkrfXuY3JgLdtioSwMzTZ8DCDyCTqvDuQ/kiGW 144Zy9FeWGC0QLTEWQBy73tNnu3zIB9By3Se0QoRU4I+3/GaYntuOCfV5cJkxUI+iCax Esfw== X-Forwarded-Encrypted: i=1; AJvYcCUClcdGFwwPMB2lrLV4a2HGBPtaWG94j5oNmy2B2sMPtjlu/HbNZ6oh8NMWDC8cEZw5yp6Eo5rU4ljGJHXWe8XTxOmTgXRGSkM365g8 X-Gm-Message-State: AOJu0YwFQ9JfW4tAO22pIb7BOiJWtMUEraxc0q9sZYJnS09mCbBKow8G BO6Rp2uAVVJVr/E2cDD3L6Dd7CQeMxi6qI7cpgTiaP3xjFF3lps+z+nw4T9RLg== X-Google-Smtp-Source: AGHT+IG3c0BvnqRYp7HHxponKHVesrJ9uywDhykAWTt3SMm/sN4jr8XioGLuQKBfjydM9P3PT60q8Q== X-Received: by 2002:a05:6a00:842:b0:6e4:79a1:c9c with SMTP id q2-20020a056a00084200b006e479a10c9cmr3309881pfk.12.1709324853845; Fri, 01 Mar 2024 12:27:33 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id fn17-20020a056a002fd100b006e2301e702fsm3348707pfb.125.2024.03.01.12.27.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 12:27:33 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v3 1/2] string: Convert selftest to KUnit Date: Fri, 1 Mar 2024 12:27:30 -0800 Message-Id: <20240301202732.2688342-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240301202524.make.800-kees@kernel.org> References: <20240301202524.make.800-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11517; i=keescook@chromium.org; h=from:subject; bh=VwPmtfrEpMQZppZlXT0BVh7uJDWDRNLzilybx+4SCQM=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBl4jozJUd6Mz1T30BxDp7hozu+2IvRChqP3J4eW TlBK7mJFQOJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZeI6MwAKCRCJcvTf3G3A JvfwD/9iGxn5ciz1gJA3FgcC6oguTmCdle9NVS+6b/xS7jRSs3juqi+nl/vAJ4yuw1bv9nLxOPE 0VeBW/x0QLJWEgnpi6xkolPaEIT+kGFfMYWZkNPNhzoaqBJH2uUTdnq4yvLEtC4KCRAbVli0rpb GzQbMHIW2ahuDlavI2Baxn52g5Daf8MJvwMZPpQU47gzPiTBr6abSYz6j7ShNOWLe3Mu/noECWW cnaradEbLvKUJ//vEbSX8tu6+ozt+w31Dc+xlsEFV18AZ1pCTlS8Un+HVVh+eI8IXpMy0wNdXkR ognlZZpI06X7cax8hi1rX4lJjjSlDmMv+VP4IB6+M/f0Y6mxofgofRX7UMZ9SQBkchdLGOKNEWZ 3CiDYm0GVevzIW+HZMVxXfw//U/p2nNZfcs85+v4H6HjJ8Begzo4BvEY5tMQJTTncP4xp/qr8mM SOiKIvNT/P6DRjPUesTYN7ITb68/ww5o2ebFgL9/nxRqIN7i1mHeeTtXRJVMugcte8yUULul2/C IWClruJEuyKhgYrAkTLoGp4UuJ9w6kHqE6h9hRAcX3N8TNaKTdrWFlap+ccXwthIKwHj+F5+0Mv 60cqvxskVmrZP1cpUW4tHNyRd5kpw52TLhSfLULG4OHD0kG8qaRyGb0LITB9doxf04ujDjTJNYW SXcVN4kvBTMbiag== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert test_string.c to KUnit so it can be easily run with everything else. Additional text context is retained for failure reporting. For example, when forcing a bad match, we can see the loop counters reported for the memset() tests: [09:21:52] # test_memset64: ASSERTION FAILED at lib/string_kunit.c:93 [09:21:52] Expected v =3D=3D 0xa2a1a1a1a1a1a1a1ULL, but [09:21:52] v =3D=3D -6799976246779207263 (0xa1a1a1a1a1a1a1a1) [09:21:52] 0xa2a1a1a1a1a1a1a1ULL =3D=3D -6727918652741279327 (0xa2a= 1a1a1a1a1a1a1) [09:21:52] i:0 j:0 k:0 [09:21:52] [FAILED] test_memset64 Currently passes without problems: $ ./tools/testing/kunit/kunit.py run string ... [09:37:40] Starting KUnit Kernel (1/1)... [09:37:40] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [09:37:40] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D string= (6 subtests) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [09:37:40] [PASSED] test_memset16 [09:37:40] [PASSED] test_memset32 [09:37:40] [PASSED] test_memset64 [09:37:40] [PASSED] test_strchr [09:37:40] [PASSED] test_strnchr [09:37:40] [PASSED] test_strspn [09:37:40] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = [PASSED] string =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D [09:37:40] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [09:37:40] Testing complete. Ran 6 tests: passed: 6 [09:37:40] Elapsed time: 6.730s total, 0.001s configuring, 6.562s building,= 0.131s running Signed-off-by: Kees Cook Reviewed-by: Mark Brown --- Cc: Andy Shevchenko --- MAINTAINERS | 2 +- lib/Kconfig.debug | 6 +- lib/Makefile | 2 +- lib/{test_string.c =3D> string_kunit.c} | 200 +++++++++----------------- 4 files changed, 77 insertions(+), 133 deletions(-) rename lib/{test_string.c =3D> string_kunit.c} (46%) diff --git a/MAINTAINERS b/MAINTAINERS index cd651c4df019..9f1f68cccd6a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8976,9 +8976,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/test-string_helpers.c -F: lib/test_string.c F: scripts/coccinelle/api/string_choices.cocci =20 GENERIC UIO DRIVER FOR PCI DEVICES diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 4e2febe3b568..406cdf353488 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2352,8 +2352,10 @@ config ASYNC_RAID6_TEST config TEST_HEXDUMP tristate "Test functions located in the hexdump module at runtime" =20 -config STRING_SELFTEST - tristate "Test string functions at runtime" +config STRING_KUNIT_TEST + tristate "KUnit test string functions at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS =20 config TEST_STRING_HELPERS tristate "Test functions located in the string_helpers module at runtime" diff --git a/lib/Makefile b/lib/Makefile index eae87c41b22b..946277c37831 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -49,7 +49,7 @@ obj-y +=3D 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_SELFTEST) +=3D test_string.o +obj-$(CONFIG_STRING_KUNIT_TEST) +=3D string_kunit.o obj-y +=3D string_helpers.o obj-$(CONFIG_TEST_STRING_HELPERS) +=3D test-string_helpers.o obj-y +=3D hexdump.o diff --git a/lib/test_string.c b/lib/string_kunit.c similarity index 46% rename from lib/test_string.c rename to lib/string_kunit.c index c5cb92fb710e..15551200d858 100644 --- a/lib/test_string.c +++ b/lib/string_kunit.c @@ -1,17 +1,23 @@ // SPDX-License-Identifier: GPL-2.0-only +/* + * Test cases for string functions. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include #include #include #include #include =20 -static __init int memset16_selftest(void) +static void test_memset16(struct kunit *test) { unsigned i, j, k; u16 v, *p; =20 - p =3D kmalloc(256 * 2 * 2, GFP_KERNEL); - if (!p) - return -1; + p =3D kunit_kzalloc(test, 256 * 2 * 2, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); =20 for (i =3D 0; i < 256; i++) { for (j =3D 0; j < 256; j++) { @@ -20,34 +26,27 @@ static __init int memset16_selftest(void) for (k =3D 0; k < 512; k++) { v =3D p[k]; if (k < i) { - if (v !=3D 0xa1a1) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1, + "i:%d j:%d k:%d", i, j, k); } else if (k < i + j) { - if (v !=3D 0xb1b2) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2, + "i:%d j:%d k:%d", i, j, k); } else { - if (v !=3D 0xa1a1) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1, + "i:%d j:%d k:%d", i, j, k); } } } } - -fail: - kfree(p); - if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; } =20 -static __init int memset32_selftest(void) +static void test_memset32(struct kunit *test) { unsigned i, j, k; u32 v, *p; =20 - p =3D kmalloc(256 * 2 * 4, GFP_KERNEL); - if (!p) - return -1; + p =3D kunit_kzalloc(test, 256 * 2 * 4, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); =20 for (i =3D 0; i < 256; i++) { for (j =3D 0; j < 256; j++) { @@ -56,34 +55,27 @@ static __init int memset32_selftest(void) for (k =3D 0; k < 512; k++) { v =3D p[k]; if (k < i) { - if (v !=3D 0xa1a1a1a1) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1, + "i:%d j:%d k:%d", i, j, k); } else if (k < i + j) { - if (v !=3D 0xb1b2b3b4) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4, + "i:%d j:%d k:%d", i, j, k); } else { - if (v !=3D 0xa1a1a1a1) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1, + "i:%d j:%d k:%d", i, j, k); } } } } - -fail: - kfree(p); - if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; } =20 -static __init int memset64_selftest(void) +static void test_memset64(struct kunit *test) { unsigned i, j, k; u64 v, *p; =20 - p =3D kmalloc(256 * 2 * 8, GFP_KERNEL); - if (!p) - return -1; + p =3D kunit_kzalloc(test, 256 * 2 * 8, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p); =20 for (i =3D 0; i < 256; i++) { for (j =3D 0; j < 256; j++) { @@ -92,27 +84,21 @@ static __init int memset64_selftest(void) for (k =3D 0; k < 512; k++) { v =3D p[k]; if (k < i) { - if (v !=3D 0xa1a1a1a1a1a1a1a1ULL) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL, + "i:%d j:%d k:%d", i, j, k); } else if (k < i + j) { - if (v !=3D 0xb1b2b3b4b5b6b7b8ULL) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4b5b6b7b8ULL, + "i:%d j:%d k:%d", i, j, k); } else { - if (v !=3D 0xa1a1a1a1a1a1a1a1ULL) - goto fail; + KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL, + "i:%d j:%d k:%d", i, j, k); } } } } - -fail: - kfree(p); - if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; } =20 -static __init int strchr_selftest(void) +static void test_strchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; const char *empty_string =3D ""; @@ -121,26 +107,21 @@ static __init int strchr_selftest(void) =20 for (i =3D 0; i < strlen(test_string) + 1; i++) { result =3D strchr(test_string, test_string[i]); - if (result - test_string !=3D i) - return i + 'a'; + KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, + "char:%c", 'a' + i); } =20 result =3D strchr(empty_string, '\0'); - if (result !=3D empty_string) - return 0x101; + KUNIT_ASSERT_PTR_EQ(test, result, empty_string); =20 result =3D strchr(empty_string, 'a'); - if (result) - return 0x102; + KUNIT_ASSERT_NULL(test, result); =20 result =3D strchr(test_string, 'z'); - if (result) - return 0x103; - - return 0; + KUNIT_ASSERT_NULL(test, result); } =20 -static __init int strnchr_selftest(void) +static void test_strnchr(struct kunit *test) { const char *test_string =3D "abcdefghijkl"; const char *empty_string =3D ""; @@ -151,35 +132,29 @@ static __init int strnchr_selftest(void) for (j =3D 0; j < strlen(test_string) + 2; j++) { result =3D strnchr(test_string, j, test_string[i]); if (j <=3D i) { - if (!result) - continue; - return ((i + 'a') << 8) | j; + KUNIT_ASSERT_NULL_MSG(test, result, + "char:%c i:%d j:%d", 'a' + i, i, j); + } else { + KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, + "char:%c i:%d j:%d", 'a' + i, i, j); } - if (result - test_string !=3D i) - return ((i + 'a') << 8) | j; } } =20 result =3D strnchr(empty_string, 0, '\0'); - if (result) - return 0x10001; + KUNIT_ASSERT_NULL(test, result); =20 result =3D strnchr(empty_string, 1, '\0'); - if (result !=3D empty_string) - return 0x10002; + KUNIT_ASSERT_PTR_EQ(test, result, empty_string); =20 result =3D strnchr(empty_string, 1, 'a'); - if (result) - return 0x10003; + KUNIT_ASSERT_NULL(test, result); =20 result =3D strnchr(NULL, 0, '\0'); - if (result) - return 0x10004; - - return 0; + KUNIT_ASSERT_NULL(test, result); } =20 -static __init int strspn_selftest(void) +static void test_strspn(struct kunit *test) { static const struct strspn_test { const char str[16]; @@ -187,71 +162,38 @@ static __init int strspn_selftest(void) const char reject[16]; unsigned a; unsigned r; - } tests[] __initconst =3D { + } tests[] =3D { { "foobar", "", "", 0, 6 }, { "abba", "abc", "ABBA", 4, 4 }, { "abba", "a", "b", 1, 1 }, { "", "abc", "abc", 0, 0}, }; const struct strspn_test *s =3D tests; - size_t i, res; + size_t i; =20 for (i =3D 0; i < ARRAY_SIZE(tests); ++i, ++s) { - res =3D strspn(s->str, s->accept); - if (res !=3D s->a) - return 0x100 + 2*i; - res =3D strcspn(s->str, s->reject); - if (res !=3D s->r) - return 0x100 + 2*i + 1; + KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept), + "i:%d", i); + KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject), + "i:%d", i); } - return 0; } =20 -static __exit void string_selftest_remove(void) -{ -} +static struct kunit_case string_test_cases[] =3D { + KUNIT_CASE(test_memset16), + KUNIT_CASE(test_memset32), + KUNIT_CASE(test_memset64), + KUNIT_CASE(test_strchr), + KUNIT_CASE(test_strnchr), + KUNIT_CASE(test_strspn), + {} +}; =20 -static __init int string_selftest_init(void) -{ - int test, subtest; - - test =3D 1; - subtest =3D memset16_selftest(); - if (subtest) - goto fail; - - test =3D 2; - subtest =3D memset32_selftest(); - if (subtest) - goto fail; - - test =3D 3; - subtest =3D memset64_selftest(); - if (subtest) - goto fail; +static struct kunit_suite string_test_suite =3D { + .name =3D "string", + .test_cases =3D string_test_cases, +}; =20 - test =3D 4; - subtest =3D strchr_selftest(); - if (subtest) - goto fail; - - test =3D 5; - subtest =3D strnchr_selftest(); - if (subtest) - goto fail; - - test =3D 6; - subtest =3D strspn_selftest(); - if (subtest) - goto fail; - - pr_info("String selftests succeeded\n"); - return 0; -fail: - pr_crit("String selftest failure %d.%08x\n", test, subtest); - return 0; -} +kunit_test_suites(&string_test_suite); =20 -module_init(string_selftest_init); -module_exit(string_selftest_remove); MODULE_LICENSE("GPL v2"); --=20 2.34.1 From nobody Sat Feb 7 11:31:57 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 363E63C470 for ; Fri, 1 Mar 2024 20:27:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709324857; cv=none; b=ClC5YryhJzXSsYJwh7mptUUEPAkMmDNc0w5h3qASmWaACQ4EMh/Z798HtZaMETDbBlnssNWp/qKn71h2KbQ+pg2ycbHq0WOoJ/cyzEeMRqktEdMEtix78S/h3x//YLrYmwisWDyZN2A6vVqf/CJe748eestVNuxjMQdFywmInCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709324857; c=relaxed/simple; bh=WI2CgIw9rdflWWSy0zWA8s7YyBxlDqey+Uf+AJqyeQs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P+fe5BkFbF1EuSzQ0pR2fwWTlrTuoujXqh/6RW5WcUdSjqZS4AlMAfcDtmRlbJeqfYrNbsH5QmnYpKmc5thNFlqy7p98xNkeYqvphUEoo/N0WF5pjCcubIgnqq3AFZFNFvPyPYx6fFZ/AkAp9gWoguQLmNl9/IBgmSlQA9gPxG0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=iSz/cKB4; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="iSz/cKB4" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1dcad814986so22539925ad.0 for ; Fri, 01 Mar 2024 12:27:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709324854; x=1709929654; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=e43Oy1VJb2rh5tC+uRZesllR18sa6QyvpjGCw0Qb8JA=; b=iSz/cKB44q+Se9EGJBWfXW84wNg24pb8hSWrUXwTGgx+n1LlvdycmTexf+YHFD3CuR bIgSWacCk3YqYy97NpIEvHnAMJH2HNPR96lpV1QaBkC/pDva4HmQo5kQoSff/cdH3Fub I4PMZiRrCVsMgn9sFCf/v/pKfkSPa9dQ/44gg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709324854; x=1709929654; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e43Oy1VJb2rh5tC+uRZesllR18sa6QyvpjGCw0Qb8JA=; b=Tvvud0I7MHirmGR34fQ+hg95qCEIIn1m9n2nkl0y2LJoejvBaKLAxvMYQP057VYJNl rYP4aOKlH4Cu3g26RTjMTOR1JBwtcOv2aoHx0QgLQxDFLo6w4tR0BaAnis/ZDteqyxqz YThuIgmxmoZxeZoIv54GopLs2iktViAs/AukHrALe7dERkapTRp4Z0k7fZwYb3RbiE3v He+KettUrDhEhcdZVOiKZVPHN7hF6F209s73PDFnBDmavvg45RGTxSqxhEmul+uSmLk0 5XyQU8rIqoxrpNjzc3JabGLk/h4/kqvamVS7ydLCo3gk45rKI/Dw+WWN3neMQgyrHqa4 e8yA== X-Forwarded-Encrypted: i=1; AJvYcCW4uZqKLg8xg/CY5psMwMcTkAxhzXw3h+8Wki6gYuXbBdgGM0lxogZGo6MHaFBcaxkLGBMY26cw3Jb1Px7ncd5Skz8ODeQtYV9resGJ X-Gm-Message-State: AOJu0YxMU03L78QQV8dScmkyj6sO2hnnknv42Ib7EmDHko7+DbWEs7Nw RKQ6NpfVhMZxk778C+JEWARzWuo/k58WTO+XviWwgi8kDwfWNKUX3EvNjRpzkpxc8ldOKVe2l2s = X-Google-Smtp-Source: AGHT+IEcmLPHCY7iAOfIyILJFDBxVuX+lcL41ko/Px1/lT2QlVA0ycrl8dSGmRvttQs2d0mK+r4Qgw== X-Received: by 2002:a17:902:e849:b0:1dc:a681:ca08 with SMTP id t9-20020a170902e84900b001dca681ca08mr2730378plg.28.1709324854497; Fri, 01 Mar 2024 12:27:34 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id ju16-20020a170903429000b001db5ecd115bsm3882815plb.276.2024.03.01.12.27.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 12:27:33 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v3 2/2] string: Convert helpers selftest to KUnit Date: Fri, 1 Mar 2024 12:27:31 -0800 Message-Id: <20240301202732.2688342-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240301202524.make.800-kees@kernel.org> References: <20240301202524.make.800-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=17432; i=keescook@chromium.org; h=from:subject; bh=WI2CgIw9rdflWWSy0zWA8s7YyBxlDqey+Uf+AJqyeQs=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBl4jozuLu7YIfN+Fw/yCXN12+wlzlDkxX5fEo5L nXUdMWNu+SJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZeI6MwAKCRCJcvTf3G3A JslrD/9FJwTVshjXuVhWvBF22g+VZ/uAtIQdQdZ7JFx9C0alntuyMm7T6JvRlAVqagQ1ipFdedz O6K2Eg5FnWXQGePYNmwaEMqsPBacR0U2N6MFqeq8QSSc3cuoaGq34qN7lSm8Q1NXN02bhaliUAb 1uLdhNGxuigZiOuj3DC9gjh53jYOyTrgjaPmpC9f2HSdfo3eoqEDi61ripe2zThfd4k2zd16M3q 08RfVQom5dETF04jYaVpBSbs5Y058iPNC/IzdOlkkd3SOVEL/pMP9LoV3NQDxxE5k5vinV3eC4V AOHq62HlPAOLduZ5TI1Fr2SMnL0jvL/9Pptpser0NU8nLkP79cP+30Z0bfqNr+1T8JQRcYQ9don JH3XrVEdUC787+V+AHTEqLeoLZeyjGWcs3aDwcEy0w/UHudoMZeNfxqDgS6qUbJ2OX5Koujfb13 aJQVdqAuFtZIa8YAVnU6ycNSnmji/GZvwMvo7BakdeRO1E+GFVUZyMJNTHxtFDIHeQtHGK/wi3K qmU+2/yDKsaehEggaSw/8xHYrrJQy4wn5XuNNjnraNUiWozRz1DhuyS8tVz0bcFiX/TD4x8nxTy K7xIjNvM044/7SauK8YewyXztsOS8vUnbMPS7bKSOfVm3CdkHkyATVvrL0UJzn7jrV5xwiAlRzR 2jKVkP7KH1vC4RA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert test-string_helpers.c to KUnit so it can be easily run with everything else. Failure reporting doesn't need to be open-coded in most places, for example, forcing a failure in the expected output for upper/lower testing looks like this: [12:18:43] # test_upper_lower: EXPECTATION FAILED at lib/string_helpers_kun= it.c:579 [12:18:43] Expected dst =3D=3D strings_upper[i].out, but [12:18:43] dst =3D=3D "ABCDEFGH1234567890TEST" [12:18:43] strings_upper[i].out =3D=3D "ABCDEFGH1234567890TeST" [12:18:43] [FAILED] test_upper_lower Currently passes without problems: $ ./tools/testing/kunit/kunit.py run string_helpers ... [12:23:55] Starting KUnit Kernel (1/1)... [12:23:55] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [12:23:55] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D string_helpers (3 = subtests) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [12:23:55] [PASSED] test_get_size [12:23:55] [PASSED] test_upper_lower [12:23:55] [PASSED] test_unescape [12:23:55] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [PASSED] str= ing_helpers =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [12:23:55] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [12:23:55] Testing complete. Ran 3 tests: passed: 3 [12:23:55] Elapsed time: 6.709s total, 0.001s configuring, 6.591s building,= 0.066s running Signed-off-by: Kees Cook Reviewed-by: Mark Brown --- Cc: Andy Shevchenko --- MAINTAINERS | 2 +- lib/Kconfig.debug | 6 +- lib/Makefile | 2 +- ...tring_helpers.c =3D> string_helpers_kunit.c} | 216 ++++++++---------- 4 files changed, 101 insertions(+), 125 deletions(-) rename lib/{test-string_helpers.c =3D> string_helpers_kunit.c} (72%) diff --git a/MAINTAINERS b/MAINTAINERS index 9f1f68cccd6a..f3f26d2d4ffb 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8978,7 +8978,7 @@ F: include/linux/string_helpers.h F: lib/string.c F: lib/string_kunit.c F: lib/string_helpers.c -F: lib/test-string_helpers.c +F: lib/string_helpers_kunit.c F: scripts/coccinelle/api/string_choices.cocci =20 GENERIC UIO DRIVER FOR PCI DEVICES diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 406cdf353488..5429e6f170f3 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2357,8 +2357,10 @@ config STRING_KUNIT_TEST depends on KUNIT default KUNIT_ALL_TESTS =20 -config TEST_STRING_HELPERS - tristate "Test functions located in the string_helpers module at runtime" +config STRING_HELPERS_KUNIT_TEST + tristate "KUnit test string helpers at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS =20 config TEST_KSTRTOX tristate "Test kstrto*() family of functions at runtime" diff --git a/lib/Makefile b/lib/Makefile index 946277c37831..97c42e38046f 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -51,7 +51,7 @@ obj-y +=3D bcd.o sort.o parser.o debug_locks.o random32.o= \ generic-radix-tree.o bitmap-str.o obj-$(CONFIG_STRING_KUNIT_TEST) +=3D string_kunit.o obj-y +=3D string_helpers.o -obj-$(CONFIG_TEST_STRING_HELPERS) +=3D test-string_helpers.o +obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) +=3D string_helpers_kunit.o obj-y +=3D hexdump.o obj-$(CONFIG_TEST_HEXDUMP) +=3D test_hexdump.o obj-y +=3D kstrtox.o diff --git a/lib/test-string_helpers.c b/lib/string_helpers_kunit.c similarity index 72% rename from lib/test-string_helpers.c rename to lib/string_helpers_kunit.c index dce67698297b..f88e39fd68d6 100644 --- a/lib/test-string_helpers.c +++ b/lib/string_helpers_kunit.c @@ -1,35 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause /* * Test cases for lib/string_helpers.c module. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include -#include #include -#include -#include #include #include #include =20 -static __init bool test_string_check_buf(const char *name, unsigned int fl= ags, - char *in, size_t p, - char *out_real, size_t q_real, - char *out_test, size_t q_test) +static void test_string_check_buf(struct kunit *test, + const char *name, unsigned int flags, + char *in, size_t p, + char *out_real, size_t q_real, + char *out_test, size_t q_test) { - if (q_real =3D=3D q_test && !memcmp(out_test, out_real, q_test)) - return true; - - pr_warn("Test '%s' failed: flags =3D %#x\n", name, flags); - - print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1, - in, p, true); - print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1, - out_test, q_test, true); - print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1, - out_real, q_real, true); - - return false; + KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name); + KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test, + "name:%s", name); } =20 struct test_string { @@ -38,7 +28,7 @@ struct test_string { unsigned int flags; }; =20 -static const struct test_string strings[] __initconst =3D { +static const struct test_string strings[] =3D { { .in =3D "\\f\\ \\n\\r\\t\\v", .out =3D "\f\\ \n\r\t\v", @@ -61,17 +51,19 @@ static const struct test_string strings[] __initconst = =3D { }, }; =20 -static void __init test_string_unescape(const char *name, unsigned int fla= gs, - bool inplace) +static void test_string_unescape(struct kunit *test, + const char *name, unsigned int flags, + bool inplace) { int q_real =3D 256; - char *in =3D kmalloc(q_real, GFP_KERNEL); - char *out_test =3D kmalloc(q_real, GFP_KERNEL); - char *out_real =3D kmalloc(q_real, GFP_KERNEL); + char *in =3D kunit_kzalloc(test, q_real, GFP_KERNEL); + char *out_test =3D kunit_kzalloc(test, q_real, GFP_KERNEL); + char *out_real =3D kunit_kzalloc(test, q_real, GFP_KERNEL); int i, p =3D 0, q_test =3D 0; =20 - if (!in || !out_test || !out_real) - goto out; + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real); =20 for (i =3D 0; i < ARRAY_SIZE(strings); i++) { const char *s =3D strings[i].in; @@ -104,12 +96,8 @@ static void __init test_string_unescape(const char *nam= e, unsigned int flags, q_real =3D string_unescape(in, out_real, q_real, flags); } =20 - test_string_check_buf(name, flags, in, p - 1, out_real, q_real, + test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real, out_test, q_test); -out: - kfree(out_real); - kfree(out_test); - kfree(in); } =20 struct test_string_1 { @@ -124,7 +112,7 @@ struct test_string_2 { }; =20 #define TEST_STRING_2_DICT_0 NULL -static const struct test_string_2 escape0[] __initconst =3D {{ +static const struct test_string_2 escape0[] =3D {{ .in =3D "\f\\ \n\r\t\v", .s1 =3D {{ .out =3D "\\f\\ \\n\\r\\t\\v", @@ -222,7 +210,7 @@ static const struct test_string_2 escape0[] __initconst= =3D {{ }}; =20 #define TEST_STRING_2_DICT_1 "b\\ \t\r\xCF" -static const struct test_string_2 escape1[] __initconst =3D {{ +static const struct test_string_2 escape1[] =3D {{ .in =3D "\f\\ \n\r\t\v", .s1 =3D {{ .out =3D "\f\\134\\040\n\\015\\011\v", @@ -359,7 +347,7 @@ static const struct test_string_2 escape1[] __initconst= =3D {{ /* terminator */ }}; =20 -static const struct test_string strings_upper[] __initconst =3D { +static const struct test_string strings_upper[] =3D { { .in =3D "abcdefgh1234567890test", .out =3D "ABCDEFGH1234567890TEST", @@ -370,7 +358,7 @@ static const struct test_string strings_upper[] __initc= onst =3D { }, }; =20 -static const struct test_string strings_lower[] __initconst =3D { +static const struct test_string strings_lower[] =3D { { .in =3D "ABCDEFGH1234567890TEST", .out =3D "abcdefgh1234567890test", @@ -381,8 +369,8 @@ static const struct test_string strings_lower[] __initc= onst =3D { }, }; =20 -static __init const char *test_string_find_match(const struct test_string_= 2 *s2, - unsigned int flags) +static const char *test_string_find_match(const struct test_string_2 *s2, + unsigned int flags) { const struct test_string_1 *s1 =3D s2->s1; unsigned int i; @@ -403,31 +391,31 @@ static __init const char *test_string_find_match(cons= t struct test_string_2 *s2, return NULL; } =20 -static __init void -test_string_escape_overflow(const char *in, int p, unsigned int flags, con= st char *esc, +static void +test_string_escape_overflow(struct kunit *test, + const char *in, int p, unsigned int flags, const char *esc, int q_test, const char *name) { int q_real; =20 q_real =3D string_escape_mem(in, p, NULL, 0, flags, esc); - if (q_real !=3D q_test) - pr_warn("Test '%s' failed: flags =3D %#x, osz =3D 0, expected %d, got %d= \n", - name, flags, q_test, q_real); + KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s: flags:%#x", name, fla= gs); } =20 -static __init void test_string_escape(const char *name, - const struct test_string_2 *s2, - unsigned int flags, const char *esc) +static void test_string_escape(struct kunit *test, const char *name, + const struct test_string_2 *s2, + unsigned int flags, const char *esc) { size_t out_size =3D 512; - char *out_test =3D kmalloc(out_size, GFP_KERNEL); - char *out_real =3D kmalloc(out_size, GFP_KERNEL); - char *in =3D kmalloc(256, GFP_KERNEL); + char *out_test =3D kunit_kzalloc(test, out_size, GFP_KERNEL); + char *out_real =3D kunit_kzalloc(test, out_size, GFP_KERNEL); + char *in =3D kunit_kzalloc(test, 256, GFP_KERNEL); int p =3D 0, q_test =3D 0; int q_real; =20 - if (!out_test || !out_real || !in) - goto out; + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in); =20 for (; s2->in; s2++) { const char *out; @@ -463,45 +451,35 @@ static __init void test_string_escape(const char *nam= e, =20 q_real =3D string_escape_mem(in, p, out_real, out_size, flags, esc); =20 - test_string_check_buf(name, flags, in, p, out_real, q_real, out_test, + test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_tes= t, q_test); =20 - test_string_escape_overflow(in, p, flags, esc, q_test, name); - -out: - kfree(in); - kfree(out_real); - kfree(out_test); + test_string_escape_overflow(test, in, p, flags, esc, q_test, name); } =20 #define string_get_size_maxbuf 16 -#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2= ) \ - do { \ - BUILD_BUG_ON(sizeof(exp_result10) >=3D string_get_size_maxbuf); \ - BUILD_BUG_ON(sizeof(exp_result2) >=3D string_get_size_maxbuf); \ - __test_string_get_size((size), (blk_size), (exp_result10), \ - (exp_result2)); \ +#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2= ) \ + do { \ + BUILD_BUG_ON(sizeof(exp_result10) >=3D string_get_size_maxbuf); \ + BUILD_BUG_ON(sizeof(exp_result2) >=3D string_get_size_maxbuf); \ + __test_string_get_size(test, (size), (blk_size), (exp_result10), \ + (exp_result2)); \ } while (0) =20 =20 -static __init void test_string_get_size_check(const char *units, - const char *exp, - char *res, - const u64 size, - const u64 blk_size) +static void test_string_get_size_check(struct kunit *test, + const char *units, + const char *exp, + char *res, + const u64 size, + const u64 blk_size) { - if (!memcmp(res, exp, strlen(exp) + 1)) - return; - - res[string_get_size_maxbuf - 1] =3D '\0'; - - pr_warn("Test 'test_string_get_size' failed!\n"); - pr_warn("string_get_size(size =3D %llu, blk_size =3D %llu, units =3D %s)\= n", + KUNIT_EXPECT_MEMEQ_MSG(test, res, exp, strlen(exp) + 1, + "string_get_size(size =3D %llu, blk_size =3D %llu, units =3D %s)", size, blk_size, units); - pr_warn("expected: '%s', got '%s'\n", exp, res); } =20 -static __init void __strchrcut(char *dst, const char *src, const char *cut) +static void __strchrcut(char *dst, const char *src, const char *cut) { const char *from =3D src; size_t len; @@ -515,11 +493,12 @@ static __init void __strchrcut(char *dst, const char = *src, const char *cut) *dst =3D '\0'; } =20 -static __init void __test_string_get_size_one(const u64 size, const u64 bl= k_size, - const char *exp_result10, - const char *exp_result2, - enum string_size_units units, - const char *cut) +static void __test_string_get_size_one(struct kunit *test, + const u64 size, const u64 blk_size, + const char *exp_result10, + const char *exp_result2, + enum string_size_units units, + const char *cut) { char buf10[string_get_size_maxbuf]; char buf2[string_get_size_maxbuf]; @@ -537,13 +516,14 @@ static __init void __test_string_get_size_one(const u= 64 size, const u64 blk_size string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(bu= f10)); string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2= )); =20 - test_string_get_size_check(prefix10, exp10, buf10, size, blk_size); - test_string_get_size_check(prefix2, exp2, buf2, size, blk_size); + test_string_get_size_check(test, prefix10, exp10, buf10, size, blk_size); + test_string_get_size_check(test, prefix2, exp2, buf2, size, blk_size); } =20 -static __init void __test_string_get_size(const u64 size, const u64 blk_si= ze, - const char *exp_result10, - const char *exp_result2) +static void __test_string_get_size(struct kunit *test, + const u64 size, const u64 blk_size, + const char *exp_result10, + const char *exp_result2) { struct { enum string_size_units units; @@ -557,12 +537,13 @@ static __init void __test_string_get_size(const u64 s= ize, const u64 blk_size, int i; =20 for (i =3D 0; i < ARRAY_SIZE(get_size_test_cases); i++) - __test_string_get_size_one(size, blk_size, exp_result10, exp_result2, + __test_string_get_size_one(test, size, blk_size, + exp_result10, exp_result2, get_size_test_cases[i].units, get_size_test_cases[i].cut); } =20 -static __init void test_string_get_size(void) +static void test_get_size(struct kunit *test) { /* small values */ test_string_get_size_one(0, 512, "0 B", "0 B"); @@ -582,7 +563,7 @@ static __init void test_string_get_size(void) test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB"); } =20 -static void __init test_string_upper_lower(void) +static void test_upper_lower(struct kunit *test) { char *dst; int i; @@ -592,16 +573,10 @@ static void __init test_string_upper_lower(void) int len =3D strlen(strings_upper[i].in) + 1; =20 dst =3D kmalloc(len, GFP_KERNEL); - if (!dst) - return; + KUNIT_ASSERT_NOT_NULL(test, dst); =20 string_upper(dst, s); - if (memcmp(dst, strings_upper[i].out, len)) { - pr_warn("Test 'string_upper' failed : expected %s, got %s!\n", - strings_upper[i].out, dst); - kfree(dst); - return; - } + KUNIT_EXPECT_STREQ(test, dst, strings_upper[i].out); kfree(dst); } =20 @@ -610,45 +585,44 @@ static void __init test_string_upper_lower(void) int len =3D strlen(strings_lower[i].in) + 1; =20 dst =3D kmalloc(len, GFP_KERNEL); - if (!dst) - return; + KUNIT_ASSERT_NOT_NULL(test, dst); =20 string_lower(dst, s); - if (memcmp(dst, strings_lower[i].out, len)) { - pr_warn("Test 'string_lower failed : : expected %s, got %s!\n", - strings_lower[i].out, dst); - kfree(dst); - return; - } + KUNIT_EXPECT_STREQ(test, dst, strings_lower[i].out); kfree(dst); } } =20 -static int __init test_string_helpers_init(void) +static void test_unescape(struct kunit *test) { unsigned int i; =20 - pr_info("Running tests...\n"); for (i =3D 0; i < UNESCAPE_ALL_MASK + 1; i++) - test_string_unescape("unescape", i, false); - test_string_unescape("unescape inplace", + test_string_unescape(test, "unescape", i, false); + test_string_unescape(test, "unescape inplace", get_random_u32_below(UNESCAPE_ALL_MASK + 1), true); =20 /* Without dictionary */ for (i =3D 0; i < ESCAPE_ALL_MASK + 1; i++) - test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0); + test_string_escape(test, "escape 0", escape0, i, TEST_STRING_2_DICT_0); =20 /* With dictionary */ for (i =3D 0; i < ESCAPE_ALL_MASK + 1; i++) - test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1); + test_string_escape(test, "escape 1", escape1, i, TEST_STRING_2_DICT_1); +} + +static struct kunit_case string_helpers_test_cases[] =3D { + KUNIT_CASE(test_get_size), + KUNIT_CASE(test_upper_lower), + KUNIT_CASE(test_unescape), + {} +}; =20 - /* Test string_get_size() */ - test_string_get_size(); +static struct kunit_suite string_helpers_test_suite =3D { + .name =3D "string_helpers", + .test_cases =3D string_helpers_test_cases, +}; =20 - /* Test string upper(), string_lower() */ - test_string_upper_lower(); +kunit_test_suites(&string_helpers_test_suite); =20 - return -EINVAL; -} -module_init(test_string_helpers_init); MODULE_LICENSE("Dual BSD/GPL"); --=20 2.34.1