From patchwork Thu Feb 29 23:35:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 208563 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp745694dyb; Thu, 29 Feb 2024 15:36:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVbVu9F3tgJOvCi2VbJOmsjeRS9eWkWVx+WKeHXl7p+t02kZQUimINUwNO29exW7ICH4Uhu0HVErLD1O7eWvNjBamcMQQ== X-Google-Smtp-Source: AGHT+IHWeWU0ZE9K6a5dNcWg6WEsy4g3kTz6DWgikJeFg/mUwyOb+GaoWPA657pLbZA7d/wRacHX X-Received: by 2002:a17:902:da8b:b0:1dc:b063:34ac with SMTP id j11-20020a170902da8b00b001dcb06334acmr33706plx.21.1709249786368; Thu, 29 Feb 2024 15:36:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709249786; cv=pass; d=google.com; s=arc-20160816; b=FTFd2yF8AKJMfSmwE/kGXzM4rrBMiWhsYxKlWXbE7eWHelPItbxUIk2ZPHm8yBS7bE Xo+EpTre0caSUkRxuYXUgpm8IdjzPi4J28zJUYQX9HGfKTqtOM/3E92A06iCMyBhR35p ugdLkIHbZIemYRcFsz7oIntabIA7CinuSXXAsf4z9Q8bPCFnp9txZ6b4DDoQm9QIUnnh Y11vElmSG+PkCNMQxnIOU+MP5Ia93PMtguJHvUucGjQxu4/zTe262a7CgCwBOyeXHkpS BJZSlqvkhwTJ8Wml6MmVCBtXVSi7/rlJxYlAQQJO3rCaYqhrZf7j3hi+tjgBN10oOmYg 9lJQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=hhQN8lEo47C06uFL3UK+82WnUqWmwbabHP0lvyxYyyI=; fh=6q1iHn3YZWJWIYSkD7KUWLUfjVtwCExKnaVF1lsGG+U=; b=AyCNQSMkzRy6kbGh9FNPCyjuT+2/HQzW1CxqJWpXXiM5WtaJ2jrvSaD3RpyDEhj8U6 mq+fpK3cATFHYEiCn+ZBISe17RPGx4xahydY0oWiSGpQum3Upc6V/7ugUozD9h6hpZos 69yXi3Mrjtf1tNbrL5mddoAxsvcu+ziY81uObaz50F9BvQlwMFD7bBOzVWPr9szEWAm3 5J7TapBMHDp2OfvF6XGowtMgA6MYBeVsbG8FCYE5wIOXli5pP8jy4cTA1ze+9y0NZMiP 4lqys9nw0jdkxhRLjZNsiryVvxAdOCfI7GBAXu270ggn6fiWRiWhB5XeBRMxEFcfIE3l H6Ug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MWUeH5bq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-87697-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87697-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l18-20020a170903245200b001db4f0f3698si2295273pls.458.2024.02.29.15.36.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 15:36:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87697-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MWUeH5bq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-87697-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87697-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C610228554F for ; Thu, 29 Feb 2024 23:36:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77E2C13E7E4; Thu, 29 Feb 2024 23:35:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MWUeH5bq" Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 7F470200A6 for ; Thu, 29 Feb 2024 23:35:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709249733; cv=none; b=LwRfeumlEZ7Cx1ng5SaB1ziccFx3UYNnzMEspvEF6TvSjJl6o30vBzFfdLthc8dHoaxzSEJ0zXwdWU4L+MM6Y96/jbNqVaFq7hiUDSXyKcZ6YsVXXZE+uqD1eeFXZil7flKKowo7HivQ1tmZwFPPCt7X93sP249ZVTbBjH2hdF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709249733; c=relaxed/simple; bh=55xyn8zKO4nNH9Rvcx8r5OPu2ble/KrvXCx28GmYr8o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oNijQQnLf5Kc8PEdfz/RKP2eVS24SC5FlBTIRuHwVmd9W3hwdUU3HHJCDj1A/+pnUF6keM7IWHU95Qm+o53/lqxucRiKgnyiuyGM2Dfln2bxIuklLNT/wa39QUCPjoHBg1QGusj23saBhzg1Ng3xQcDIrWq/Q4isqEbmkEOa5J0= 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=MWUeH5bq; arc=none smtp.client-ip=209.85.210.173 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 Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6e55731af5cso1210770b3a.0 for ; Thu, 29 Feb 2024 15:35:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709249730; x=1709854530; 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=hhQN8lEo47C06uFL3UK+82WnUqWmwbabHP0lvyxYyyI=; b=MWUeH5bqL9/Ul2XrQYRTSjP/kmPVZs8DroOonYBj/G65jwn2+YYTnCDRyFGJTIqfv/ +pB9N43bSbG49py5ovHsgTrUlSdNpxL3kKvQR9AK2MSc657leOkDD97cTIfhlSzd22vp 92ygbaUcaz6642iFmP11kcdYhnO0Dlyckhhzo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709249730; x=1709854530; 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=hhQN8lEo47C06uFL3UK+82WnUqWmwbabHP0lvyxYyyI=; b=AeP8xyDbEjUVGvxYfLJIsvf4+9KvaLHr8CQjKTkFX95yH/WUh1TdB6FeCX314i+nQZ R98/bPY/B74g9pzvTz+V/iZko3+uc5JtuVJPLIq3B26FW2QEi3mEWZi2qJu2o+Jlv0nw cPRqBEs+UAY07UX6k5UhXdJcxZFbG2nGERifptZW5w2zFL4bIEHdRBUJoEzFnrrw35lK GSAvCwmx46zRF/Izyd5sml6bUlloKXrShURUbU6rTzuKtqiF56gpM81pfmIHmwyLj5k8 fmWc4sS5Ogj3Ka+55U4IYMa4PMFnLYkjZ7DaU+KLieeDGXucKmD+ks+hu3h2cb2QGP52 7nQg== X-Forwarded-Encrypted: i=1; AJvYcCVp9E4umJqvw6f1N/kqZ7rH6wIS4c+azoxRg0zhoVScuYgyWlyEJMjhrMUj1E0cHQq+lwDEfd2iDVSasCCMsdu39t5EZPt+4KKwHFqL X-Gm-Message-State: AOJu0YyO3IrSm3EGb4wyd0SecXppYRrn8hpOSF5HKfskenchomuN94WP 7SqXAah8Wb0gA9or1fWD4PmrX2pqmlQF+4FPgYLv6n4JnxUvdGflvkqG6LYRzA== X-Received: by 2002:a05:6a21:9214:b0:1a0:e811:f521 with SMTP id tl20-20020a056a21921400b001a0e811f521mr4436711pzb.41.1709249730383; Thu, 29 Feb 2024 15:35:30 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id y33-20020a17090a53a400b0029ab5abcbe1sm4525268pjh.20.2024.02.29.15.35.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 15:35:29 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] string: Convert selftest to KUnit Date: Thu, 29 Feb 2024 15:35:23 -0800 Message-Id: <20240229233528.1538803-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229233432.work.675-kees@kernel.org> References: <20240229233432.work.675-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=9530; i=keescook@chromium.org; h=from:subject; bh=55xyn8zKO4nNH9Rvcx8r5OPu2ble/KrvXCx28GmYr8o=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBl4RS8k3eOcod2ISKCbrONdo52S/cd/eKMgfWDU 72HZYnkRyCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZeEUvAAKCRCJcvTf3G3A JmefD/4jXek21dq0rQfcpU5sZMPfGkZ2PvtC9C0Uy0KOh6pmovBev0Ivjks2Z6SpW47h3c09z34 DgkYNmGDRkIE9VCM/CHRvzvav7uYL6zfqLHyeNPBapvyffYdJfspW3pB6qcmeal3zbjxCFcWSek kis86eJ673hvtdswv3svQDbK4hzgxrXNX1OmwiWN6S6W35ATi0SxhYLIX9N9KhEv3bx/AM2RBxI pBjl98YVpsiVeMYi59vo5nfCGVzFkfCEtikmlQF9hIYP3i7YnJnqSASEClXy6KOgGh6/xMC7fud i8c8EuUxWSslw8LsDku7LjcnNipkZ/uZq1qp5Ck0BLg1A+JBxigXKMHxX/fsuCYdvWkEkO6iFzd xD++6EZH4KIbGNellQt1Us4MpGkpfTPf20mLCmJ310kI6pUCMDuAVaTtYO/Lh3nrH3KuiuE3Rtg Fd03eNITa0TDD+x9uRbHss3qfKxSZLYI2cWadQldWdtqqg/91G6zCWZDsiLQVyQxyOkrT8zwLRK IALTapgqbhFN+dmplRXW2bnCgUIGYq2twgHH+swXzqtwqcQKkn7/YpgWbqNXNCmsiNkISMA5a+Z 30Z5eMplrALcW/tREZY7/9an5/nTNQ69j2bJnBS9qAbUifQMP8AixAtfy1KiCYHnjipBK4JoIma JE5sTgvPNR4jcjw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792278303908649421 X-GMAIL-MSGID: 1792278303908649421 Convert test_string.c to KUnit so it can be easily run with everything else. Signed-off-by: Kees Cook --- Cc: Andy Shevchenko Cc: linux-hardening@vger.kernel.org --- MAINTAINERS | 2 +- lib/Kconfig.debug | 6 +- lib/Makefile | 2 +- lib/{test_string.c => string_kunit.c} | 166 +++++++++----------------- 4 files changed, 61 insertions(+), 115 deletions(-) rename lib/{test_string.c => string_kunit.c} (54%) 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 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" -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 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 += 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) += test_string.o +obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o obj-y += string_helpers.o obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o obj-y += hexdump.o diff --git a/lib/test_string.c b/lib/string_kunit.c similarity index 54% rename from lib/test_string.c rename to lib/string_kunit.c index c5cb92fb710e..bbb54ac11f7b 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 -static __init int memset16_selftest(void) +static void test_memset16(struct kunit *test) { unsigned i, j, k; u16 v, *p; p = kmalloc(256 * 2 * 2, GFP_KERNEL); - if (!p) - return -1; + KUNIT_ASSERT_NOT_NULL(test, p); for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { @@ -20,34 +26,28 @@ static __init int memset16_selftest(void) for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { - if (v != 0xa1a1) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1); } else if (k < i + j) { - if (v != 0xb1b2) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xb1b2); } else { - if (v != 0xa1a1) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1); } } } } -fail: kfree(p); if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; + KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000); } -static __init int memset32_selftest(void) +static void test_memset32(struct kunit *test) { unsigned i, j, k; u32 v, *p; p = kmalloc(256 * 2 * 4, GFP_KERNEL); - if (!p) - return -1; + KUNIT_ASSERT_NOT_NULL(test, p); for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { @@ -56,34 +56,28 @@ static __init int memset32_selftest(void) for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { - if (v != 0xa1a1a1a1) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1a1a1); } else if (k < i + j) { - if (v != 0xb1b2b3b4) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xb1b2b3b4); } else { - if (v != 0xa1a1a1a1) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1a1a1); } } } } -fail: kfree(p); if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; + KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000); } -static __init int memset64_selftest(void) +static void test_memset64(struct kunit *test) { unsigned i, j, k; u64 v, *p; p = kmalloc(256 * 2 * 8, GFP_KERNEL); - if (!p) - return -1; + KUNIT_ASSERT_NOT_NULL(test, p); for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { @@ -92,27 +86,22 @@ static __init int memset64_selftest(void) for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { - if (v != 0xa1a1a1a1a1a1a1a1ULL) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1a1a1a1a1a1a1ULL); } else if (k < i + j) { - if (v != 0xb1b2b3b4b5b6b7b8ULL) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xb1b2b3b4b5b6b7b8ULL); } else { - if (v != 0xa1a1a1a1a1a1a1a1ULL) - goto fail; + KUNIT_EXPECT_EQ(test, v, 0xa1a1a1a1a1a1a1a1ULL); } } } } -fail: kfree(p); if (i < 256) - return (i << 24) | (j << 16) | k | 0x8000; - return 0; + KUNIT_EXPECT_EQ(test, 0, (i << 24) | (j << 16) | k | 0x8000); } -static __init int strchr_selftest(void) +static void test_strchr(struct kunit *test) { const char *test_string = "abcdefghijkl"; const char *empty_string = ""; @@ -121,26 +110,20 @@ static __init int strchr_selftest(void) for (i = 0; i < strlen(test_string) + 1; i++) { result = strchr(test_string, test_string[i]); - if (result - test_string != i) - return i + 'a'; + KUNIT_ASSERT_EQ(test, result - test_string, i); } result = strchr(empty_string, '\0'); - if (result != empty_string) - return 0x101; + KUNIT_ASSERT_PTR_EQ(test, result, empty_string); result = strchr(empty_string, 'a'); - if (result) - return 0x102; + KUNIT_ASSERT_NULL(test, result); result = strchr(test_string, 'z'); - if (result) - return 0x103; - - return 0; + KUNIT_ASSERT_NULL(test, result); } -static __init int strnchr_selftest(void) +static void test_strnchr(struct kunit *test) { const char *test_string = "abcdefghijkl"; const char *empty_string = ""; @@ -153,33 +136,27 @@ static __init int strnchr_selftest(void) if (j <= i) { if (!result) continue; - return ((i + 'a') << 8) | j; + KUNIT_ASSERT_EQ(test, 0, 1); } if (result - test_string != i) - return ((i + 'a') << 8) | j; + KUNIT_ASSERT_EQ(test, 0, 1); } } result = strnchr(empty_string, 0, '\0'); - if (result) - return 0x10001; + KUNIT_ASSERT_NULL(test, result); result = strnchr(empty_string, 1, '\0'); - if (result != empty_string) - return 0x10002; + KUNIT_ASSERT_PTR_EQ(test, result, empty_string); result = strnchr(empty_string, 1, 'a'); - if (result) - return 0x10003; + KUNIT_ASSERT_NULL(test, result); result = strnchr(NULL, 0, '\0'); - if (result) - return 0x10004; - - return 0; + KUNIT_ASSERT_NULL(test, result); } -static __init int strspn_selftest(void) +static void test_strspn(struct kunit *test) { static const struct strspn_test { const char str[16]; @@ -187,7 +164,7 @@ static __init int strspn_selftest(void) const char reject[16]; unsigned a; unsigned r; - } tests[] __initconst = { + } tests[] = { { "foobar", "", "", 0, 6 }, { "abba", "abc", "ABBA", 4, 4 }, { "abba", "a", "b", 1, 1 }, @@ -198,60 +175,27 @@ static __init int strspn_selftest(void) for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) { res = strspn(s->str, s->accept); - if (res != s->a) - return 0x100 + 2*i; + KUNIT_ASSERT_EQ(test, res, s->a); res = strcspn(s->str, s->reject); - if (res != s->r) - return 0x100 + 2*i + 1; + KUNIT_ASSERT_EQ(test, res, s->r); } - return 0; -} - -static __exit void string_selftest_remove(void) -{ } -static __init int string_selftest_init(void) -{ - int test, subtest; - - test = 1; - subtest = memset16_selftest(); - if (subtest) - goto fail; - - test = 2; - subtest = memset32_selftest(); - if (subtest) - goto fail; - - test = 3; - subtest = memset64_selftest(); - if (subtest) - goto fail; +static struct kunit_case string_test_cases[] = { + KUNIT_CASE(test_memset16), + KUNIT_CASE(test_memset32), + KUNIT_CASE(test_memset64), + KUNIT_CASE(test_strchr), + KUNIT_CASE(test_strnchr), + KUNIT_CASE(test_strspn), + {} +}; - test = 4; - subtest = strchr_selftest(); - if (subtest) - goto fail; +static struct kunit_suite string_test_suite = { + .name = "string", + .test_cases = string_test_cases, +}; - test = 5; - subtest = strnchr_selftest(); - if (subtest) - goto fail; - - test = 6; - subtest = 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); -module_init(string_selftest_init); -module_exit(string_selftest_remove); MODULE_LICENSE("GPL v2"); From patchwork Thu Feb 29 23:35:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 208562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp745636dyb; Thu, 29 Feb 2024 15:36:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVz1RGE8ROQbusW9lTOdx8ETYJbwfKTOW+29+CKvtV17DaJlQnJWokc3XjSG8s8NbcntiX+lV5dRV/TU8GNj+Fohud9oA== X-Google-Smtp-Source: AGHT+IEBdKdXIlKzXNq6krRJ8tNlQiIoMST0XidkceCP6uoNZyKeVo3DVxyboJlCSYsaqjY/ZNtp X-Received: by 2002:a17:906:a409:b0:a44:4e66:e5ce with SMTP id l9-20020a170906a40900b00a444e66e5cemr108802ejz.12.1709249776311; Thu, 29 Feb 2024 15:36:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709249776; cv=pass; d=google.com; s=arc-20160816; b=x/ZSO3ELnVyBBeU0HL5Jq6CBnTUxlOz5oIpliIS3rYPRDivc5nsojgJtUWGCDh4kXf BLBk467yJLyaLXmb8zmS3bgcHuAE5hZM33ZAEfFXirPgtHjSQd07zug/nVb8N7VmLQ2/ hZLl4f3VQp2x3gEq3jIbb/0s2fAOlVwQSdWf4psFARcSwEmgCWAy98u1sMLlE8LGyEiU WFVRd9MXZxs+v1/R/0zfYnDoFB9sIKTd/d99eqoi5bfm8EeEHWUp4ndcssFU5WiUtBB7 QG0CRonho0Zbi6vEcM38kWWqH8CjtB8eCzCRwQKzK5iShujTZLAc6jUB8LRiTM567KHf iAmQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=xaSgUgfOZn9fgH6QcdlrLYMK12GQmmivaE6Pb6d2BPw=; fh=vRXIt4bFswcLvmo2+tw/Xy1b6cGBqpefb969Ee8hgUU=; b=X3mbQkt7Vs2NStqsuy2U3p4JLUW5PDE2B0ke92c+QD9136rg5UVgBN+H/Jh4r6M8QU P0WFWfrF5TfpP5jTUWFSNRpxCPMZFv4qNbOKuu2VJKzDTZy1jgcSg/7sNttjLzPAJhVQ 76LqpV7aEeZpYfbfhI2fjArk9N+CpTSWPnMDou5XguWRod8B/aB2J9qKMeSdNt6/ALEN aS0Oy2QGD1BA5NQ7MkVD/FU15rFfcT/qE0czI5Ruv5zQf4jsPSKl9/zZnDvZ6XD2pHSn pXSVlEEYy+tJVnTqt4pmsAy1wdJ9De3oaGMdfx/Zu/WjUOemQoY85yxJS9YcLJ4cNO4z CuXA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SJF1zktF; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-87699-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87699-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id br26-20020a170906d15a00b00a446beecf12si346519ejb.910.2024.02.29.15.36.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 15:36:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87699-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SJF1zktF; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-87699-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87699-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C06CB1F23F14 for ; Thu, 29 Feb 2024 23:36:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3FF274C03; Thu, 29 Feb 2024 23:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="SJF1zktF" Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 B772E17545 for ; Thu, 29 Feb 2024 23:35:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709249734; cv=none; b=o5X2sbsXNME3DfWsNu2tzEY0wFgewiNveWQ8CW2WHztEjfoKfZq09XusZd1LZk8F3q0FzW6s4NuSeQHe2i05vmH1nitE9A4sd1IWAmPTE+mewONSATM1XHqWJWjT47awhJDhmsZRmyN6eoAsIjtjTnLifIbonVFiCUn0lWAudcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709249734; c=relaxed/simple; bh=8AW6vpLoBsvSGGVQr6eadfqDgRiZzNtT+j+25yacYoY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r5Lq4FDZpq4tXyHT2ft1nmwr2xARqpRItvN1rItL9bh5wlupwNqsDWBRDkieJhyGYZCxhZav9BBwz4ZlIUUhoI9vAYZ7otJmULPS2cYTnRkIdvnKrBtzbQtR3dqq6bM6nHB9p3T/KPTh1qwbafSDbpNxD//+3NSMDVj5omvIh4s= 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=SJF1zktF; arc=none smtp.client-ip=209.85.210.181 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 Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6da202aa138so1085716b3a.2 for ; Thu, 29 Feb 2024 15:35:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709249732; x=1709854532; 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=xaSgUgfOZn9fgH6QcdlrLYMK12GQmmivaE6Pb6d2BPw=; b=SJF1zktFcDiqAD0ChmcC/e9AcZl2KgveVIA+u2XzhO/yqnsd2upDJKAzFO9TkGx0YB YOx1kMOfeHdS16x6asO8J38O9+qyrgpq8DsOPIvr/fH0qzXiMZbClw4vKJ/odSDHg7q9 kjll02vL8c1yxFXZEt7GH+MZBPTijYwXvU6XQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709249732; x=1709854532; 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=xaSgUgfOZn9fgH6QcdlrLYMK12GQmmivaE6Pb6d2BPw=; b=E2FMoFZWtzj4K7iEb+MZXltVMEkBKzBqFog3IZdPgkq+kj4qLuYO9JGQkn+lwGM0ay AoAz38pTaA6ketqhydh0mdvK+GketdcF+4sg1kWaI847hA/ngyOCTrwmDtx/L4GT5zlV 6H/plhqFwloz3B7KY4LFbUj4qHknIRsJ7j5v164/zYukoXkNhGw3WLi0V/7Nbf1CcmBT AbsYdKW5lHH053KJYIqgq6w2ehHupExfqbeiOTNSQMRN0LHkhqxhCgYdz5meuCobTmOi EVtuwOA9wB4P9dUX86i+u3t75WDZpgLInQOAnoI34jGOW8LTDaV551NwXn5fOy6zhB9g K19g== X-Forwarded-Encrypted: i=1; AJvYcCVc91UVj9YkPZ9UJ6vq55hj5TCluz/hOHxs7Wg/QH7s6QN9FxBrvyr4Ch8cr/R8GOkqBHZy8/tvSf/ti/irj2WwQuQdZsrf1FYYdvSE X-Gm-Message-State: AOJu0YyCsI3CSAICEtPDPOmmuYVMJ75R7vHXma4tVXpHIVzmCBCMJPST AZdUEDscMKs7GQC/mocuOSuV3GjhbKBOFmHIdOP+I3hZcykQwOCDAHUFYXRB1hABfR15MxSS7mw = X-Received: by 2002:a05:6a00:929d:b0:6e5:8cc1:f75b with SMTP id jw29-20020a056a00929d00b006e58cc1f75bmr247230pfb.4.1709249732019; Thu, 29 Feb 2024 15:35:32 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id y133-20020a62ce8b000000b006e45a0101basm1926356pfg.99.2024.02.29.15.35.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 15:35:29 -0800 (PST) From: Kees Cook To: Andy Shevchenko Cc: Kees Cook , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] string: Convert helpers selftest to KUnit Date: Thu, 29 Feb 2024 15:35:24 -0800 Message-Id: <20240229233528.1538803-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229233432.work.675-kees@kernel.org> References: <20240229233432.work.675-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=13861; i=keescook@chromium.org; h=from:subject; bh=8AW6vpLoBsvSGGVQr6eadfqDgRiZzNtT+j+25yacYoY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBl4RS8pY37KRfK0m6BW6mkRvOBGJyKj4b3fBznP DPbMv/BUQuJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZeEUvAAKCRCJcvTf3G3A JoheD/4ysCqHzF0OnapGBM5miH8aDv6O141tgCMfC0/53/u3YAtpJ9qyC23hmr1Q1D5rfy2Cw2P WJlBS29/i7mXfEZmdxQH8lNWHcbblmTZdj1aIbdbwiGPwrzW5GzK0i4NL/NPehKeYFT+YKSBhVU ACa2W4FloX21fDjlT5PSLOVL2JIBClNfxFg/L4Y0KuT1Nkia4rLmQRPJtD0QN3K42F4A5nWs2me FNYQiaDbAwmGEpbHc4LP5WBJcKsnAvg8UsOocRvXocqJQAv0AEPR21P6fXrQQNvwAV58jlKG4Ig CZMYkFbfs+f89b+leZpYs82cHx2eoQddQkq9dBgbSkH4QL7feUU1Lf9pNl++9C/8k2TUs6oAAtK yjJkSYC25Y0xf8nG8W1RI50hlGewF11i9VenOB2yREdNwvuFdLoIJggPvFsot4mvAIM0OupNsp9 BglWt9plPqHmvE77rr45g4Cc6e0Gy8VAm/qaaYuLHZCFTQJze7xXq7Y0SOf8MQk5d4lONcMOGKv bVYYxZGbcAVElDIVWWBH2zUEamotjaYHyRFKUokCienmCJEbqxoBKOeHCG8GFBw8k8pVBDSqH1y eM25HkrIrqY7mJ9xSfxw0bNRzcnzFQjnLDVRc1gB0eW8y+4HFdyWv6FmcagLSZvpZx2bmTH9aUl UVlhf+lZvaLbLvA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792278293684404633 X-GMAIL-MSGID: 1792278293684404633 Convert test-string_helpers.c to KUnit so it can be easily run with everything else. Signed-off-by: Kees Cook --- Cc: Andy Shevchenko Cc: linux-hardening@vger.kernel.org --- MAINTAINERS | 2 +- lib/Kconfig.debug | 6 +- lib/Makefile | 2 +- ...tring_helpers.c => string_helpers_kunit.c} | 160 ++++++++++-------- 4 files changed, 93 insertions(+), 77 deletions(-) rename lib/{test-string_helpers.c => string_helpers_kunit.c} (79%) 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 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 -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 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 += bcd.o sort.o parser.o debug_locks.o random32.o \ generic-radix-tree.o bitmap-str.o obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o obj-y += string_helpers.o -obj-$(CONFIG_TEST_STRING_HELPERS) += test-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 diff --git a/lib/test-string_helpers.c b/lib/string_helpers_kunit.c similarity index 79% rename from lib/test-string_helpers.c rename to lib/string_helpers_kunit.c index 9a68849a5d55..67d6703146c0 100644 --- a/lib/test-string_helpers.c +++ b/lib/string_helpers_kunit.c @@ -3,7 +3,7 @@ */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt -#include +#include #include #include #include @@ -11,13 +11,21 @@ #include #include -static __init bool test_string_check_buf(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) +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 == q_test && !memcmp(out_test, out_real, q_test)) - return true; + int result; + + KUNIT_EXPECT_EQ(test, q_real, q_test); + + result = memcmp(out_test, out_real, q_test); + KUNIT_EXPECT_EQ(test, 0, result); + + if (q_real == q_test && result == 0) + return; pr_warn("Test '%s' failed: flags = %#x\n", name, flags); @@ -27,8 +35,6 @@ static __init bool test_string_check_buf(const char *name, unsigned int flags, out_test, q_test, true); print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1, out_real, q_real, true); - - return false; } struct test_string { @@ -37,7 +43,7 @@ struct test_string { unsigned int flags; }; -static const struct test_string strings[] __initconst = { +static const struct test_string strings[] = { { .in = "\\f\\ \\n\\r\\t\\v", .out = "\f\\ \n\r\t\v", @@ -60,8 +66,9 @@ static const struct test_string strings[] __initconst = { }, }; -static void __init test_string_unescape(const char *name, unsigned int flags, - bool inplace) +static void test_string_unescape(struct kunit *test, + const char *name, unsigned int flags, + bool inplace) { int q_real = 256; char *in = kmalloc(q_real, GFP_KERNEL); @@ -103,7 +110,7 @@ static void __init test_string_unescape(const char *name, unsigned int flags, q_real = string_unescape(in, out_real, q_real, flags); } - 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); @@ -123,7 +130,7 @@ struct test_string_2 { }; #define TEST_STRING_2_DICT_0 NULL -static const struct test_string_2 escape0[] __initconst = {{ +static const struct test_string_2 escape0[] = {{ .in = "\f\\ \n\r\t\v", .s1 = {{ .out = "\\f\\ \\n\\r\\t\\v", @@ -221,7 +228,7 @@ static const struct test_string_2 escape0[] __initconst = {{ }}; #define TEST_STRING_2_DICT_1 "b\\ \t\r\xCF" -static const struct test_string_2 escape1[] __initconst = {{ +static const struct test_string_2 escape1[] = {{ .in = "\f\\ \n\r\t\v", .s1 = {{ .out = "\f\\134\\040\n\\015\\011\v", @@ -358,7 +365,7 @@ static const struct test_string_2 escape1[] __initconst = {{ /* terminator */ }}; -static const struct test_string strings_upper[] __initconst = { +static const struct test_string strings_upper[] = { { .in = "abcdefgh1234567890test", .out = "ABCDEFGH1234567890TEST", @@ -369,7 +376,7 @@ static const struct test_string strings_upper[] __initconst = { }, }; -static const struct test_string strings_lower[] __initconst = { +static const struct test_string strings_lower[] = { { .in = "ABCDEFGH1234567890TEST", .out = "abcdefgh1234567890test", @@ -380,8 +387,8 @@ static const struct test_string strings_lower[] __initconst = { }, }; -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 = s2->s1; unsigned int i; @@ -402,21 +409,20 @@ static __init const char *test_string_find_match(const struct test_string_2 *s2, return NULL; } -static __init void -test_string_escape_overflow(const char *in, int p, unsigned int flags, const 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; q_real = string_escape_mem(in, p, NULL, 0, flags, esc); - if (q_real != q_test) - pr_warn("Test '%s' failed: flags = %#x, osz = 0, expected %d, got %d\n", - name, flags, q_test, q_real); + KUNIT_EXPECT_EQ(test, q_real, q_test); } -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 = 512; char *out_test = kmalloc(out_size, GFP_KERNEL); @@ -462,10 +468,10 @@ static __init void test_string_escape(const char *name, q_real = string_escape_mem(in, p, out_real, out_size, flags, esc); - 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_test, q_test); - test_string_escape_overflow(in, p, flags, esc, q_test, name); + test_string_escape_overflow(test, in, p, flags, esc, q_test, name); out: kfree(in); @@ -474,22 +480,26 @@ static __init void test_string_escape(const char *name, } #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) >= string_get_size_maxbuf); \ - BUILD_BUG_ON(sizeof(exp_result2) >= 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) >= string_get_size_maxbuf); \ + BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \ + __test_string_get_size(test, (size), (blk_size), (exp_result10), \ + (exp_result2)); \ } while (0) -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)) + int result = memcmp(res, exp, strlen(exp) + 1); + + KUNIT_EXPECT_EQ(test, 0, result); + if (!result) return; res[string_get_size_maxbuf - 1] = '\0'; @@ -500,9 +510,10 @@ static __init void test_string_get_size_check(const char *units, pr_warn("expected: '%s', got '%s'\n", exp, res); } -static __init void __test_string_get_size(const u64 size, const u64 blk_size, - 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) { char buf10[string_get_size_maxbuf]; char buf2[string_get_size_maxbuf]; @@ -510,14 +521,14 @@ static __init void __test_string_get_size(const u64 size, const u64 blk_size, string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10)); string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2)); - test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10, + test_string_get_size_check(test, "STRING_UNITS_10", exp_result10, buf10, size, blk_size); - test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2, + test_string_get_size_check(test, "STRING_UNITS_2", exp_result2, buf2, size, blk_size); } -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"); @@ -537,7 +548,7 @@ static __init void test_string_get_size(void) test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB"); } -static void __init test_string_upper_lower(void) +static void test_upper_lower(struct kunit *test) { char *dst; int i; @@ -545,65 +556,68 @@ static void __init test_string_upper_lower(void) for (i = 0; i < ARRAY_SIZE(strings_upper); i++) { const char *s = strings_upper[i].in; int len = strlen(strings_upper[i].in) + 1; + int result; dst = kmalloc(len, GFP_KERNEL); - if (!dst) - return; + KUNIT_ASSERT_NOT_NULL(test, dst); string_upper(dst, s); - if (memcmp(dst, strings_upper[i].out, len)) { + result = memcmp(dst, strings_upper[i].out, len); + KUNIT_EXPECT_EQ(test, 0, result); + if (result) pr_warn("Test 'string_upper' failed : expected %s, got %s!\n", strings_upper[i].out, dst); - kfree(dst); - return; - } kfree(dst); } for (i = 0; i < ARRAY_SIZE(strings_lower); i++) { const char *s = strings_lower[i].in; int len = strlen(strings_lower[i].in) + 1; + int result; dst = kmalloc(len, GFP_KERNEL); - if (!dst) - return; + KUNIT_ASSERT_NOT_NULL(test, dst); string_lower(dst, s); - if (memcmp(dst, strings_lower[i].out, len)) { + result = memcmp(dst, strings_lower[i].out, len); + KUNIT_EXPECT_EQ(test, 0, result); + if (result) pr_warn("Test 'string_lower failed : : expected %s, got %s!\n", strings_lower[i].out, dst); - kfree(dst); - return; - } kfree(dst); } } -static int __init test_string_helpers_init(void) +static void test_unescape(struct kunit *test) { unsigned int i; - pr_info("Running tests...\n"); for (i = 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); /* Without dictionary */ for (i = 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); /* With dictionary */ for (i = 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[] = { + KUNIT_CASE(test_get_size), + KUNIT_CASE(test_upper_lower), + KUNIT_CASE(test_unescape), + {} +}; - /* Test string_get_size() */ - test_string_get_size(); +static struct kunit_suite string_helpers_test_suite = { + .name = "string_helpers", + .test_cases = string_helpers_test_cases, +}; - /* Test string upper(), string_lower() */ - test_string_upper_lower(); +kunit_test_suites(&string_helpers_test_suite); - return -EINVAL; -} -module_init(test_string_helpers_init); MODULE_LICENSE("Dual BSD/GPL");