From nobody Fri Dec 19 17:49:37 2025 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (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 AF5EB4D8CE for ; Fri, 5 Dec 2025 04:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764909082; cv=none; b=fWaW5gZuaCrDez8il5xtReiVirVCMS9BosJzr7PpYOESxg0Ty8hhOPO7A+7b3HQ1DTRyL9OGrnm2t/D3XTxzDNNGqsewkJY0Qae2KM9Doy2F4apmQLkkb283ESCcER+daqqMI8/FVrouteOF7v42KKEuiFNopz4C5InNTc6v6M4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764909082; c=relaxed/simple; bh=4+703upttbLBXrs3HCR/osHe0MWHMFb29STOXf/zQlw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PUnHRKSzVwI6oZs9CgJXf56R1+2UNg2S/bl1gpurA2xFx2uDHIdpbNKOJ2jRxv9QyF1x5+R5yqcl24+tu4eYWWbRYSHYm9NGFTLoZHiQgL1VBWqmt4uN5ncMDJr6trtvlO8nyxglQQg9Kqj+8Y2pI2TWXT4T7EkU5pmyhSTNBLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=TKM9D1Ho; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="TKM9D1Ho" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1764909079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/IbInYvILPYcoixt21Z2SWUU0ENgDuclZexoYjmKwdA=; b=TKM9D1HoXxt9Y5NphY20zpbQ6r9CFJNwnoLhfjUAeUCi8hl4ZAA9TASk4eGqXSP7NM12cs +eugVup//dbEbol8cFusnZTGmBA8cBXs4xvBsC8sQgKJwB59Ug8RVMJ202IbF9Ax8zCIX1 HzEuhj0tDkjKhuQVexyh1xZJr0OFl4c= From: chenxiaosong.chenxiaosong@linux.dev To: sfrench@samba.org, smfrench@gmail.com, linkinjeon@kernel.org, linkinjeon@samba.org Cc: linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org, chenxiaosong@chenxiaosong.com, ChenXiaoSong Subject: [PATCH v2 8/9] smb/client: introduce smb2maperror KUnit tests Date: Fri, 5 Dec 2025 12:29:56 +0800 Message-ID: <20251205042958.2658496-9-chenxiaosong.chenxiaosong@linux.dev> In-Reply-To: <20251205042958.2658496-1-chenxiaosong.chenxiaosong@linux.dev> References: <20251205042958.2658496-1-chenxiaosong.chenxiaosong@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: ChenXiaoSong The KUnit tests are executed when cifs.ko is loaded. The maperror_test_check_sort() checks whether the array is properly sorted. The maperror_test_get_err_map() checks whether the expected element can be correctly searched for in the smb2_error_map_table array. Signed-off-by: ChenXiaoSong --- fs/smb/Kconfig | 13 ++++++ fs/smb/client/smb2maperror.c | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/fs/smb/Kconfig b/fs/smb/Kconfig index ef425789fa6a..95b29d089e60 100644 --- a/fs/smb/Kconfig +++ b/fs/smb/Kconfig @@ -9,3 +9,16 @@ config SMBFS tristate default y if CIFS=3Dy || SMB_SERVER=3Dy default m if CIFS=3Dm || SMB_SERVER=3Dm + +config SMB_KUNIT_TEST + bool "SMB KUnit tests" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Only useful for kernel devs running KUnit test harness and are not + for inclusion into a production build. + + 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. diff --git a/fs/smb/client/smb2maperror.c b/fs/smb/client/smb2maperror.c index f5d999f3b569..95e4a41ecc5a 100644 --- a/fs/smb/client/smb2maperror.c +++ b/fs/smb/client/smb2maperror.c @@ -2497,3 +2497,80 @@ void smb2_init_maperror(void) sizeof(struct status_to_posix_error), cmp_smb2_status, NULL); } + +#if IS_ENABLED(CONFIG_SMB_KUNIT_TEST) +#include + +static void maperror_test_check_sort(struct kunit *test) +{ + bool is_sorted =3D true; + unsigned int i; + + for (i =3D 1; i < err_map_num; i++) { + if (smb2_error_map_table[i].smb2_status >=3D + smb2_error_map_table[i - 1].smb2_status) + continue; + + pr_err("smb2_error_map_table array order is incorrect\n"); + is_sorted =3D false; + break; + } + + KUNIT_EXPECT_EQ(test, true, is_sorted); +} + +static void +get_and_cmp_err_map(struct kunit *test, struct status_to_posix_error *expe= ct) +{ + struct status_to_posix_error *result; + + result =3D smb2_get_err_map(expect->smb2_status); + KUNIT_EXPECT_PTR_NE(test, NULL, result); + KUNIT_EXPECT_EQ(test, expect->posix_error, result->posix_error); + KUNIT_EXPECT_STREQ(test, expect->status_string, result->status_string); +} + +static void maperror_test_get_err_map(struct kunit *test) +{ + struct status_to_posix_error expect; + + /* first element */ + expect =3D smb2_error_map_table[0]; + get_and_cmp_err_map(test, &expect); + + /* last element */ + expect =3D smb2_error_map_table[err_map_num - 1]; + get_and_cmp_err_map(test, &expect); + + expect =3D (struct status_to_posix_error) { + .smb2_status =3D STATUS_SERIAL_COUNTER_TIMEOUT, + .posix_error =3D -ETIMEDOUT, + .status_string =3D "STATUS_SERIAL_COUNTER_TIMEOUT", + }; + get_and_cmp_err_map(test, &expect); + + expect =3D (struct status_to_posix_error) { + .smb2_status =3D STATUS_IO_REPARSE_TAG_NOT_HANDLED, + .posix_error =3D -EOPNOTSUPP, + .status_string =3D "STATUS_REPARSE_NOT_HANDLED", + }; + get_and_cmp_err_map(test, &expect); +} + +/* + * Before running these test cases, the smb2_init_maperror() + * function is called first. + */ +static struct kunit_case maperror_test_cases[] =3D { + KUNIT_CASE(maperror_test_check_sort), + KUNIT_CASE(maperror_test_get_err_map), + {} +}; + +static struct kunit_suite maperror_suite =3D { + .name =3D "smb2-maperror", + .test_cases =3D maperror_test_cases, +}; + +kunit_test_suite(maperror_suite); +#endif /* CONFIG_SMB_KUNIT_TEST */ --=20 2.43.0