From patchwork Tue Jun 4 12:32:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13685156 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 58EAC146A9B; Tue, 4 Jun 2024 12:32:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504371; cv=none; b=dB5YXVyWkJ79Yjj1YBJgPYkcepkZaA8df9t2HSlBhdJ0KgHPrtWV9Pm2IQbN4unafQ5Le0GzJHHg18LNDgXFop/jXgWdGdH8AU6IapFbx2IHrSkdv4mOY4hoGkknQx6xb0l/5YzI8IrPwWR2emy7IesvIeZJvnwh+YyUOj3szSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504371; c=relaxed/simple; bh=WwmjeOQu72FaUg1dfsnXq/tC9I4b3h2WVybb3KIPNKs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KrEDFfFUJFc/6Y3cm0UpgbhYVzm0rmCOYUsR7sHWx9t8P+6BfVBBni5zWgJo+snGztgWffXztmxfB6IYdf5vi0BL9JL0ZBZElsVoRpK1ueSpacCekcieo6t7jE4etopgsHQzeZAB8hB85DRwLY3Ec5teFaj3iYg3TE2e0/f5T8Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Xd+73NHF; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Xd+73NHF" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-42111da672eso6641635e9.0; Tue, 04 Jun 2024 05:32:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717504367; x=1718109167; 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=3hm8JZD/Uu53KILH4Z+apiOCSWKZ89tmhz9RtA/wLq8=; b=Xd+73NHFMjM8sSXy0Ss2BEK6Q7Ux4B6MUsNgNfmEbTK1ZX0xAzEsAx8sfYWIv01k6Y NjBrKJtdvWJffkDF43EEWvi0BA8RA2s75rUeT5Gd4RbEXe2isbHszW614C4W2MlcEGA1 6y1+T1YCh61l+tVKzubDtBDutGm8Mmo4l5UybwC83ncTYyaGxDCYVnWugm6g7RXVsvxf A5VVGp1sR5zUKAcNt01XFBjlsft9O9vKpSuqPt0/gCPY7Q+4m17ifoBF33rPeq6rCTmG 2h6aMHMuXT1cbv0eC/i8DnC04fK2q4OpqD1D2HowW5sMfD4lxdiCkl2uubJi8VcZIwif s5Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717504367; x=1718109167; 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=3hm8JZD/Uu53KILH4Z+apiOCSWKZ89tmhz9RtA/wLq8=; b=Z3VSAb03/eDnvkLQMsY9J4ywwwzXWrXTaW10qy4TEAN/yTq6MkSgNApEnIfsYtyhCh yaqe5dVFL9endE7FnNAN8fbJirVTG4+CWM86rFQ66//lQXVH6Fq/vsoFG8mI1+it4ykP ZRv5F/ZyXQRLDXkZpCpVmxbizNbDa20Wacj9przA0JZG1KbVZ+Dry99sBtKDSaR/ECl3 DZP3eHSe0YLsyQFaCuKBOZfeZLhX8C1Plz/397h3ya6uhmgj2sFAiMXk0fzC/S1FCOa/ Km3GBi1i3qCsVidjhKnnKYfMtKxgWtTsuFR42e1OCXuFAnDvw68iKkcTkNa+CABA20n0 6SeQ== X-Forwarded-Encrypted: i=1; AJvYcCXA4ar67y2fCCf4Mn3BM+HgtdGpLp+Q5kw7lrMXflHVRiECYnf/3v4JlcxbHKNrhbrkoOb5/NK5WjWjzwEPViJFtLU2paL9dJkTgjryYeH/W4Zq2T9+/Zei3XkJae49e8jhXD2CnDOS7LFMXHHF X-Gm-Message-State: AOJu0Yw/tpAnqW+bwuwg7UDkdrOQBUMp9UwmxiWmg0HHvqlJ+26Kwix/ gTA0iL/Q2Nng8jTlLlD3KPQQixmFQ4PA0I6n8oFbm3vqvomZRURX X-Google-Smtp-Source: AGHT+IFY+sD7IjToSbM9mBDxRiXhglvCWpFcKPGlj2cppS5xmG904hULLwsLKMYEg4ipIaZCyhJnyQ== X-Received: by 2002:adf:e289:0:b0:359:79f1:bab4 with SMTP id ffacd0b85a97d-35e0f30c9c3mr7466982f8f.3.1717504367544; Tue, 04 Jun 2024 05:32:47 -0700 (PDT) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e535567e9sm7209179f8f.21.2024.06.04.05.32.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:32:47 -0700 (PDT) From: Ivan Orlov To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH 1/5] kunit: string-stream: export non-static functions Date: Tue, 4 Jun 2024 13:32:00 +0100 Message-Id: <20240604123204.10412-2-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240604123204.10412-1-ivan.orlov0322@gmail.com> References: <20240604123204.10412-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Export non-static functions from the string-stream.c file into the KUnit namespace in order to be able to access them from the KUnit core tests (when they are loaded as modules). Signed-off-by: Ivan Orlov Reviewed-by: David Gow --- lib/kunit/string-stream.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 54f4fdcbfac8..a5e3339854da 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -10,7 +10,7 @@ #include #include #include - +#include #include "string-stream.h" @@ -86,6 +86,7 @@ int string_stream_vadd(struct string_stream *stream, return 0; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_vadd); int string_stream_add(struct string_stream *stream, const char *fmt, ...) { @@ -98,6 +99,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...) return result; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_add); void string_stream_clear(struct string_stream *stream) { @@ -113,6 +115,7 @@ void string_stream_clear(struct string_stream *stream) stream->length = 0; spin_unlock(&stream->lock); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_clear); char *string_stream_get_string(struct string_stream *stream) { @@ -131,6 +134,7 @@ char *string_stream_get_string(struct string_stream *stream) return buf; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_get_string); int string_stream_append(struct string_stream *stream, struct string_stream *other) @@ -148,11 +152,13 @@ int string_stream_append(struct string_stream *stream, return ret; } +EXPORT_SYMBOL_IF_KUNIT(string_stream_append); bool string_stream_is_empty(struct string_stream *stream) { return list_empty(&stream->fragments); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_is_empty); struct string_stream *alloc_string_stream(gfp_t gfp) { @@ -168,6 +174,7 @@ struct string_stream *alloc_string_stream(gfp_t gfp) return stream; } +EXPORT_SYMBOL_IF_KUNIT(alloc_string_stream); void string_stream_destroy(struct string_stream *stream) { @@ -179,6 +186,7 @@ void string_stream_destroy(struct string_stream *stream) string_stream_clear(stream); kfree(stream); } +EXPORT_SYMBOL_IF_KUNIT(string_stream_destroy); static void resource_free_string_stream(void *p) { @@ -200,8 +208,10 @@ struct string_stream *kunit_alloc_string_stream(struct kunit *test, gfp_t gfp) return stream; } +EXPORT_SYMBOL_IF_KUNIT(kunit_alloc_string_stream); void kunit_free_string_stream(struct kunit *test, struct string_stream *stream) { kunit_release_action(test, resource_free_string_stream, (void *)stream); } +EXPORT_SYMBOL_IF_KUNIT(kunit_free_string_stream); From patchwork Tue Jun 4 12:32:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13685157 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (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 0432C146D4D; Tue, 4 Jun 2024 12:32:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504373; cv=none; b=sEilIh24SMWeOxORzk4C3XpI07xfnfhlX7aR/dQOmyjEhImk9u85Qhyz7MhTl1nFRNCETf5/8bkDaYYjei/h1W7WYNbRn15GIJZhi1aKMr8SACKXfdS2cVQZB+4R4KDKMYhzjaiRr5t68nSek6MMYJu1XFzz5kjyr/E+5jmsheg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504373; c=relaxed/simple; bh=s5QTEl8avTLCywD+yMrBHIhLbHpxN43v91JE2hPsTQ4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MIbUzf3wdt0Vdis2MioDOGOkgn+yw8nNV5qgq+4KYwfN98H7+Nlh758MZH7rvUbzxLj83B1hXZyToZzb9I6mN9EKzP8NFL+vpHv/jimOia6GMmqg/s6U63dWaqVb5ei9vgfZK7zJENrixkaj59qhjuAvq91U2pms32tcbCyrvQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KorNNrwt; arc=none smtp.client-ip=209.85.167.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KorNNrwt" Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-52b9a7fb034so177510e87.1; Tue, 04 Jun 2024 05:32:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717504370; x=1718109170; 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=8sMeT1jEzulHyaa1CBMKL3RJvYNB57BjMb6V3pNuGo0=; b=KorNNrwtaYwleKlF7EArYCPxAm5z9V33Fiy/j9cp/2RYKFhfuJz2sVFsodsmOdFXPB KBC89Sw73smFNeEXdNnGCn/44xZLvJKT/vSCKX7hdgFP7Yc6A5NccXl5yr7/tuGmTAPs FNQ9M2DP/9Iuus6eHGWlJ/hCYKwYPSVjG9nh5vdXWRAGHwwm1sgJecjOsb3AHCdX23cD j4ZPlZ0O3KGSRbl8ayx0/UVqi38nTrdKl5NZXh62MzCZkyLQosEXpn+wSiZMo8qSOlzw 6h3U6tpZPbEhsuMleX0ktP/oHEU0b9RUdHq3X0U0vhthpELO7K1K3iYFddeqftzvq/1K Rv7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717504370; x=1718109170; 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=8sMeT1jEzulHyaa1CBMKL3RJvYNB57BjMb6V3pNuGo0=; b=WHy8Nns2st8UN6Jnt1bfPVS9jYtHBhwLb6f4wEuaFruYvZE1mtVR5B5XihB+jmltrx a0+q4zb2GNCyyoaKaeUa460GnXsIBl0r09M0HNjwQvuvlzw23OG3ZN+j3kxNsSSJb/Wl ok+Omm3x2qLmcuTiejHsUaNDtcf5olMC1A6fMGv6mcgFM7e+3x6xadCq67yc1SC8m4Xy geW2vxflPQRGuPjXPwGnXflLTREL7PZW50Nf9z1vUFpx1Dk/rDIQlshX2qp1fKevJCio U55WtYY0gk8Fw6Z/O5VZaExULQ7hveuNy2tyGyIDTPZVeZnliXyPYzPiW2UfG306r1gt jigg== X-Forwarded-Encrypted: i=1; AJvYcCWtZmeNwgZMlUYRHKp7wTVyww1ZZP8Ax8A1yP2ZNU02YrynvKBLXxKUk1pw6OdAaJmbGRR8Bik4gn2lPY16VG99rF+Hq5zv+QCsIKahCuEZ9xPrSFUi5vnJpVHxnExqr/vGfD1c+Rc3TDMGUZ+/ X-Gm-Message-State: AOJu0Yz5D24PF1Lgnd0hKkRLn3OyHtp/YyIDf6g2ITxZCYFgBDgqH9mb 45VptoWk4xCjHuJn42g7YttP9xg1AOdL8Ve7I28Z26Fupqhp/xdF X-Google-Smtp-Source: AGHT+IH4MqbZlrE8NuAF05L8iSWfy4SesXmj/SAsMMm0WfWl7qxM76jujmDju423FBfp7/kHx/nb/g== X-Received: by 2002:a05:6512:3b25:b0:52b:8909:58b1 with SMTP id 2adb3069b0e04-52b89702d1emr10752838e87.3.1717504369804; Tue, 04 Jun 2024 05:32:49 -0700 (PDT) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e535567e9sm7209179f8f.21.2024.06.04.05.32.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:32:49 -0700 (PDT) From: Ivan Orlov To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH 2/5] kunit: kunit-test: Remove stub for log tests Date: Tue, 4 Jun 2024 13:32:01 +0100 Message-Id: <20240604123204.10412-3-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240604123204.10412-1-ivan.orlov0322@gmail.com> References: <20240604123204.10412-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since now we are exporting string-stream functions into the KUnit namespace, we can safely use them in kunit-test when it is compiled as a module as well. So, remove the stubs used when kunit-test is compiled as a module. Import the KUnit namespace in the test. Signed-off-by: Ivan Orlov Reviewed-by: David Gow --- lib/kunit/kunit-test.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index e3412e0ca399..42178d5a97d1 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -577,12 +577,6 @@ static struct kunit_suite kunit_resource_test_suite = { .test_cases = kunit_resource_test_cases, }; -/* - * Log tests call string_stream functions, which aren't exported. So only - * build this code if this test is built-in. - */ -#if IS_BUILTIN(CONFIG_KUNIT_TEST) - /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */ KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); @@ -637,17 +631,6 @@ static void kunit_log_newline_test(struct kunit *test) kunit_skip(test, "only useful when debugfs is enabled"); } } -#else -static void kunit_log_test(struct kunit *test) -{ - kunit_skip(test, "Log tests only run when built-in"); -} - -static void kunit_log_newline_test(struct kunit *test) -{ - kunit_skip(test, "Log tests only run when built-in"); -} -#endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */ static struct kunit_case kunit_log_test_cases[] = { KUNIT_CASE(kunit_log_test), @@ -871,4 +854,5 @@ kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_current_test_suite, &kunit_device_test_suite, &kunit_fault_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); MODULE_LICENSE("GPL v2"); From patchwork Tue Jun 4 12:32:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13685158 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 3A1111474C4; Tue, 4 Jun 2024 12:32:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504375; cv=none; b=N5xhFut6FBObf35JxZ62ORvBGfBtA+d6LuaEnGdNDn/KrtF9JW9Svmq+xM80hqJerCHXPZqfSFufMb4csxl1LOrlB+nNFuphPb0U+c5k2Pa8W0eLWmKpIUjIHftq03elf6fhFkJEfD5E2PxipEEpKbF0PitN0wXef0AT1Ypc0V4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504375; c=relaxed/simple; bh=SummDxouOGlU7IgU6AXvmDabJn0ASZr5+1Pu30VLmLg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dx7Ku8GjbDeAwquofX6e0bcLdTGbF4ufXGkEoIZgdjfL0LN59FwGWmzdjxHWSgi4+pycbyeHLCIy7+E7sTO7PEzpVQH8j/ZWOIZ3MHclUjOqKSUYZVI3lLjt9OBlS4F6DV2yePHOx5tMaJPpmGvUiv1vBZE4rZP1g7q5+UgD3yE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kbokmere; arc=none smtp.client-ip=209.85.208.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kbokmere" Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2eaaa3be101so3842131fa.0; Tue, 04 Jun 2024 05:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717504372; x=1718109172; 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=pj+nK1mCKwm8YE53nt8me8RE7m7ZJlrzUOfVRauTT1Y=; b=kbokmereWHltRD4E/iPdVncHIlXPoZNZ6EH2Th4TKLUbNW5PJ+xVPODWrjROjmFpXA 3G5Z0RsPOAkD8pjiuxlzac7NS8sDs4CadlRiHNixRR0W1FUJLoEgp4a2AFTsYVCAWrbK RHfMNCC3ZMMsQFLtmOQvx101tjJ0z9+TmAcvq+ibjcX1wGeImhqNjr4DOw24oGPq+iWi Xn9o6rxhjnhdyxZuCz1F6m2GC8MxIckDg/SWoBXgc9Uw72ERfB9nPMV/ZUg2iZO7qQmc wXm8waU2DrYkh6ZIR4NqrO2ZY61HzBPB+PzBAY/18IaJOJZpn08awq9FOhk6MHNjaVhQ KAUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717504372; x=1718109172; 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=pj+nK1mCKwm8YE53nt8me8RE7m7ZJlrzUOfVRauTT1Y=; b=WSPiBLFQwJYiYQLT0EESSJVqHFv1Cf6fN+Xwv9khO4b1rfXx2PHw+xRzvLPi32Xk4k dHRGakykLpv4AtkUAXnpc9BV0Xo4GBeCnlgkTLi+QACx3/Zbt7W6EY0bVZ+/XK29avi5 c8m1c3MEnC6tgxD5W7ZAjBq6Zxrc35fy0ZLTF66Gi+3yBUI84JU1e8FE1H5ySl5p6X3a R086k8Fzv46XhDzUgDe4g5NTSbFGNtw5fvxW6QfR69Qb5NRK7mwvZiztI7uO50E3afR+ 95CVwwNbnf0XvvcAAaf8skB2j0L/0UMcZaTbKDQvL/pfJpJjlrXQx971PVTAkoUyz1L7 02eQ== X-Forwarded-Encrypted: i=1; AJvYcCUDD/TRLfhQUHisgBXywj1T0RBJon9bXTuXUogAKn5VL0St48x0CW0ylXM0/pDOSLADw1b6fXcfBP59lIQQE0zgLSIgDdwlTtGVmCQoSwetiyHujxZqmNdRpUh4z12LjUm5es40w/x4bQUNa2nD X-Gm-Message-State: AOJu0Yw+29/ZW+ybSSSYe3iKMSKrdXBKGIHTMnp06FOxxIOZDEUY6DLY mEEVhmIlailUbISPp6ZG4QwjtjLYhXvAazoALUC0sXyJfhqrs+gr X-Google-Smtp-Source: AGHT+IHcSXdDIh8lx41srPQud1VgcG+ztrz0/EsKzZCNwbU83kojtuotoQECws9hfEfQMcAclKpz1A== X-Received: by 2002:ac2:58ed:0:b0:52a:b1d7:222b with SMTP id 2adb3069b0e04-52b89834f33mr6602472e87.5.1717504371787; Tue, 04 Jun 2024 05:32:51 -0700 (PDT) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e535567e9sm7209179f8f.21.2024.06.04.05.32.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:32:51 -0700 (PDT) From: Ivan Orlov To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH 3/5] kunit: string-stream-test: Make it a separate module Date: Tue, 4 Jun 2024 13:32:02 +0100 Message-Id: <20240604123204.10412-4-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240604123204.10412-1-ivan.orlov0322@gmail.com> References: <20240604123204.10412-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently, the only way to build string-stream-test is by setting CONFIG_KUNIT_TEST=y. However, CONFIG_KUNIT_TEST is a config option for a different test (`kunit-test.c`). Introduce a new Kconfig entry in order to be able to build the string-stream-test test as a separate module. Import the KUnit namespace in the test so we could have string-stream functions accessible. Signed-off-by: Ivan Orlov Reviewed-by: David Gow --- lib/kunit/Kconfig | 8 ++++++++ lib/kunit/Makefile | 2 +- lib/kunit/string-stream-test.c | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig index 34d7242d526d..b0713c0f9265 100644 --- a/lib/kunit/Kconfig +++ b/lib/kunit/Kconfig @@ -45,6 +45,14 @@ config KUNIT_TEST purposes by developers interested in testing that KUnit works as expected. +config KUNIT_STRING_STREAM_TEST + tristate "KUnit test for string-stream" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + help + Enables the KUnit test for the string-stream (C++ stream style string + builder used in KUnit for building messages). For the string-stream + implementation, see lib/kunit/string-stream.c. + config KUNIT_EXAMPLE_TEST tristate "Example test for KUnit" if !KUNIT_ALL_TESTS default KUNIT_ALL_TESTS diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 900e6447c8e8..4793a3960f07 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -18,10 +18,10 @@ endif obj-y += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o +obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o # string-stream-test compiles built-in only. ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o obj-$(CONFIG_KUNIT_TEST) += assert_test.o endif diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c index 7511442ea98f..d03cac934e04 100644 --- a/lib/kunit/string-stream-test.c +++ b/lib/kunit/string-stream-test.c @@ -534,3 +534,5 @@ static struct kunit_suite string_stream_test_suite = { .init = string_stream_test_init, }; kunit_test_suites(&string_stream_test_suite); +MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); +MODULE_LICENSE("GPL"); From patchwork Tue Jun 4 12:32:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13685159 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 117EC147C76; Tue, 4 Jun 2024 12:32:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504376; cv=none; b=l+vdAt9KZBzIWUOvysIy09Ek5u8IavodRjxxS0suNwT7J9l8XpgBicRs3CU/d2y02MuQguBiK4u7Cw2C4JBCeq7pftLrOXq1xKN6iGZjkDUHwFIBzkyd8VCPDpR0bBzwA8LSNlqf3ME7TtyZ983w1xqj+q6UXMEA7gJ2o8C+R/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504376; c=relaxed/simple; bh=8l84IiSkdT4NCQTONJ+krCv3ALHT7sKoSDUEmuB3hQY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CScMClvlGOKj1IsSphiZvfvltR7RCZlP6c8yj8iOYAZZt0A8/QM/lA6arzEccZWcWJSB/6IgnUPD5m6Z2IYHsQQmpDPtMWgO9go6GeEnD9Iv4LiJwsiT3ipBM3oA15AivzTKJxcqbUZasw4+1SNd9abk0jsMPt97y5kwmZzomoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OANPKk/6; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OANPKk/6" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42152e2d384so364825e9.3; Tue, 04 Jun 2024 05:32:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717504373; x=1718109173; 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=tI5LoNcYQivAnNze7yHQ7N0aSL5De3/uw+ZctyPJStE=; b=OANPKk/6700BegFV3r5CIG169rv7/tu4tXoYproWS1+ydXvXp8+o//FkPTU6wBavTF K7UnsijaRJC1vGdt9uJe5MOHIBYY0kIc9onpBpBJ4nGV3XIpYJgFIp8KUXE6MBcKY9Dc kXLxxYlqBckmwALewB8293+pIr7rcm+ZTNiAYo0V9XjrJXJ41SaSM0U2iWHCEReM8bnE A6fPokKTcyRphAvrrpGwPgxCZyzOD9qRk7pGH9TL5abO3xn/SHclzBCaio0lk5cX7HmJ FeGDnqAHwOJioazQ1pe2RB8QM++Vp3yPjUvkrcCTF8UqQ2x0/B1u+K5zf2iiE+KhEWaY e8gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717504373; x=1718109173; 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=tI5LoNcYQivAnNze7yHQ7N0aSL5De3/uw+ZctyPJStE=; b=VwmxFlIw4szCpp1snI4UO4dqISfulSJXNaTsvM2bRuKBzUTlEdlSwDnvmThos67fp3 LdkDt2l95kriyrSqESPrTEFnIH7F6y3vLZYHRmLBBOEF7GnaXBWaD0OFBErsB5VCLPnm KFVVhTHisabDrL882tTMziZJ8lczhaLB5m3GRqwYC9Dxt8ZquEfCFY009nFmcwmgThSD j1BprALl9XNKKr42tjFbhQLn3OBhGSWQtX/nTeXzAvptJD/E8B9ojL50a/hpEX9A3EL/ H0pA+MJYzQ9hJPgZTXVdBn3TX92hXB97RPnpU5Cx89853jk9Zi/4z6XUfOYZJN9icHZL /v2g== X-Forwarded-Encrypted: i=1; AJvYcCWy4yKjZtQVl4UInglQAC6eUxVObrag/EwpuyNFpog6rAhPolssR4qIkJId5bzKqH/2a6BQvgTy4jBZ+EGItTKHNjrFRCJk47txdLDSyTAD2lNphIqhSuhBTqdHdP/1M23btfnUvotU6qsGVFCr X-Gm-Message-State: AOJu0YwQT9dqIeu2sU8sdIoUB7/iGHHyV+88i/CBPiah1BouNDt/dNxf nJyzULVToCLCpKJRYUBK5/7LSJmTMj9P/NvmVCG3SNE0QXkQO8hw X-Google-Smtp-Source: AGHT+IF7e+otMbfOkeziWjPXugKhtYVwF5NfTIElBb0knNAgix9rgln0wV1tQXGx+/BEaO3dTldEgQ== X-Received: by 2002:adf:e38a:0:b0:354:fa0d:1422 with SMTP id ffacd0b85a97d-35e0f2590damr8384064f8f.1.1717504373344; Tue, 04 Jun 2024 05:32:53 -0700 (PDT) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e535567e9sm7209179f8f.21.2024.06.04.05.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:32:53 -0700 (PDT) From: Ivan Orlov To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] kunit: assert: export non-static functions Date: Tue, 4 Jun 2024 13:32:03 +0100 Message-Id: <20240604123204.10412-5-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240604123204.10412-1-ivan.orlov0322@gmail.com> References: <20240604123204.10412-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Export non-static functions from the assert.c file into the KUnit namespace in order to be able to access them from the tests if they are compiled as modules. Signed-off-by: Ivan Orlov --- lib/kunit/assert.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c index 867aa5c4bccf..f394e4b8482f 100644 --- a/lib/kunit/assert.c +++ b/lib/kunit/assert.c @@ -38,6 +38,7 @@ void kunit_assert_print_msg(const struct va_format *message, if (message->fmt) string_stream_add(stream, "\n%pV", message); } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_print_msg); void kunit_fail_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -112,6 +113,7 @@ VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value) return ret; } +EXPORT_SYMBOL_IF_KUNIT(is_literal); void kunit_binary_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -180,6 +182,7 @@ VISIBLE_IF_KUNIT bool is_str_literal(const char *text, const char *value) return strncmp(text + 1, value, len - 2) == 0; } +EXPORT_SYMBOL_IF_KUNIT(is_str_literal); void kunit_binary_str_assert_format(const struct kunit_assert *assert, const struct va_format *message, @@ -232,6 +235,7 @@ void kunit_assert_hexdump(struct string_stream *stream, string_stream_add(stream, " %02x ", buf1[i]); } } +EXPORT_SYMBOL_IF_KUNIT(kunit_assert_hexdump); void kunit_mem_assert_format(const struct kunit_assert *assert, const struct va_format *message, From patchwork Tue Jun 4 12:32:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13685160 X-Patchwork-Delegate: brendanhiggins@google.com Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 1B080147C98; Tue, 4 Jun 2024 12:32:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504378; cv=none; b=bnENGIeQtCFQJjaaJEXJhmQagp2jWvKaqvmn/B91oNGurckQ9JguEPggjJIGiFhE36W8lg+RyfplZYin1913y9JPcY0RyWzEsbpRd2q6reFEqSh/mKA7aaE/nfzkPgCh85vprkFc0uGYiSlWRJ+flSUA9MIPFtfmyaoTB0E7yhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717504378; c=relaxed/simple; bh=K7PTBAzXAoBgSJ+xKfvJW7X0sYaTeuzUtC1J50VAFJ8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QnzXs14cshRvqzu8gSCfBdWMQYc+j12avlj6PzGbp5UnJasoR27puGOOJRWMvgJxdbyQBJJu/5A5gZWbBpgXgiWEjiNeMdoTCrU3GmAVjnUDZE3zaPrpvjBXjyW5ZEt6GSuivXLmHj4nUPAj6/6me9VWrVivGiGZTmqHqD+L5RQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ahdNyXNa; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ahdNyXNa" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-42111c2892bso6499455e9.1; Tue, 04 Jun 2024 05:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717504374; x=1718109174; 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=lSP18lcHx1ChZ83uy+ELGIcQeD+03tu22F2jT4KbnYI=; b=ahdNyXNa2+MATWHbFKzpaXg+podc+i8s0dbiByFwIowXDzyoVDMgDGEnsQ+u8tU+yT WawxM6VvRqeVCPhMGS6TK07bDswRlX+EWiAKeB9sMX8p/6WAIbICyWs2DeoKxMmDm97Y 7AxHBkkBCCXdh/qDatvlVcL8nF64MHPOLw77IWZsUZCvqJIcZKB4eOvFs4Gjgbk4C7N/ HXxLsQDYVnpKkBOt5nwXH7IdcjG9caGawvt7yr7/3bGSsjTj0qGB4rQd3lCYm5LqI94F v3wyLzJZUKBqLv6qDkZXEvqPwi4hX2JZoHWggFFqfh594zout5Jr/AkQv2KDyvpz9J2k WGUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717504374; x=1718109174; 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=lSP18lcHx1ChZ83uy+ELGIcQeD+03tu22F2jT4KbnYI=; b=XOdomwIJczgAuqEfrrn9qcYWGT8IlQH91Ts3z9IuR2l4+RD0t/H3DPNM7RCOcup1ea hxFxkRSjvxwGKhhKEByPRgJ2NuvfCEErrkXhPcw6RvvSkeC441OFcKOuNz7zFsaB+7xs zDNy1MoEn5ndWMuLZXB9pWP9zpE/QqH2AFrErX4j8TW4KodfFJES4hItFcbA47NthF1r VC+mMrwlLlEE7FyaymjgYIfgmTTh7cBw/pmLHN0RU0p/EgkfHJC/oMjcl47VEuDVL6U1 AtKX7q5nrXnJzsKDt+esmaxmtbQw6nFk8yca5BdyinkX0RGemyeWLDc5jnENXcMP3cHv Th5g== X-Forwarded-Encrypted: i=1; AJvYcCVWj+uFaviy3tH69BSErXxXva3iredevU63rkPlkrlUtTam6doYJpWJKFW1FmfXfIFOUFWEqi5LoRXMrHGIbEStx5TQ+v1O+YZ08hYoxGfO8cXeb4u2jvaRTGocbLNmpt+kBtA2sYE2Gr2R6DZC X-Gm-Message-State: AOJu0YzOX0bd+MLIuKWXLSAsf9NeFW7eY90r9Z82lehdeKBw4ixWs7KG tQpqMm5o1WjNLJHqwM4beNay4xBG9MMD37gsycAf2upcdmKQjeZKHc8pWw== X-Google-Smtp-Source: AGHT+IET0TS86z2KqiYXtuk/kiig8sl/pspB76KANZzBCBdXrzbcFE49iLmENZHc8lCCnPrJkCAL4Q== X-Received: by 2002:a05:600c:3ca6:b0:41a:408c:579c with SMTP id 5b1f17b1804b1-4212e046157mr95442955e9.1.1717504374171; Tue, 04 Jun 2024 05:32:54 -0700 (PDT) Received: from ivan-HLYL-WXX9.guest.codethink.co.uk ([167.98.27.226]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35e535567e9sm7209179f8f.21.2024.06.04.05.32.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 05:32:53 -0700 (PDT) From: Ivan Orlov To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] kunit: Merge assertion test into kunit-test.c Date: Tue, 4 Jun 2024 13:32:04 +0100 Message-Id: <20240604123204.10412-6-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240604123204.10412-1-ivan.orlov0322@gmail.com> References: <20240604123204.10412-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since assert_test covers the part of the KUnit core (the assertion formatting functions), I believe it would be better to have it merged into kunit-test (as it is done for other tests for the KUnit core). Signed-off-by: Ivan Orlov --- lib/kunit/Makefile | 5 - lib/kunit/assert_test.c | 388 ---------------------------------------- lib/kunit/kunit-test.c | 379 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 378 insertions(+), 394 deletions(-) delete mode 100644 lib/kunit/assert_test.c diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile index 4793a3960f07..f41d2eab1f8d 100644 --- a/lib/kunit/Makefile +++ b/lib/kunit/Makefile @@ -20,9 +20,4 @@ obj-y += hooks.o obj-$(CONFIG_KUNIT_TEST) += kunit-test.o obj-$(CONFIG_KUNIT_STRING_STREAM_TEST) += string-stream-test.o -# string-stream-test compiles built-in only. -ifeq ($(CONFIG_KUNIT_TEST),y) -obj-$(CONFIG_KUNIT_TEST) += assert_test.o -endif - obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o diff --git a/lib/kunit/assert_test.c b/lib/kunit/assert_test.c deleted file mode 100644 index 4a5967712186..000000000000 --- a/lib/kunit/assert_test.c +++ /dev/null @@ -1,388 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* - * KUnit test for the assertion formatting functions. - * Author: Ivan Orlov - */ -#include -#include "string-stream.h" - -#define TEST_PTR_EXPECTED_BUF_SIZE 32 -#define HEXDUMP_TEST_BUF_LEN 5 -#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) -#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr)) - -static void kunit_test_is_literal(struct kunit *test) -{ - KUNIT_EXPECT_TRUE(test, is_literal("5", 5)); - KUNIT_EXPECT_TRUE(test, is_literal("0", 0)); - KUNIT_EXPECT_TRUE(test, is_literal("1234567890", 1234567890)); - KUNIT_EXPECT_TRUE(test, is_literal("-1234567890", -1234567890)); - KUNIT_EXPECT_FALSE(test, is_literal("05", 5)); - KUNIT_EXPECT_FALSE(test, is_literal("", 0)); - KUNIT_EXPECT_FALSE(test, is_literal("-0", 0)); - KUNIT_EXPECT_FALSE(test, is_literal("12#45", 1245)); -} - -static void kunit_test_is_str_literal(struct kunit *test) -{ - KUNIT_EXPECT_TRUE(test, is_str_literal("\"Hello, World!\"", "Hello, World!")); - KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"", "")); - KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"\"", "\"")); - KUNIT_EXPECT_FALSE(test, is_str_literal("", "")); - KUNIT_EXPECT_FALSE(test, is_str_literal("\"", "\"")); - KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba", "Abacaba")); - KUNIT_EXPECT_FALSE(test, is_str_literal("Abacaba\"", "Abacaba")); - KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba\"", "\"Abacaba\"")); -} - -KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *); - -/* this function is used to get a "char *" string from the string stream and defer its cleanup */ -static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) -{ - char *str = string_stream_get_string(stream); - - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); - kunit_add_action(test, kfree_wrapper, (void *)str); - - return str; -} - -static void kunit_test_assert_prologue(struct kunit *test) -{ - struct string_stream *stream; - char *str; - const struct kunit_loc location = { - .file = "testfile.c", - .line = 1337, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* Test an expectation fail prologue */ - kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); - - /* Test an assertion fail prologue */ - string_stream_clear(stream); - kunit_assert_prologue(&location, KUNIT_ASSERTION, stream); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); - ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); -} - -/* - * This function accepts an arbitrary count of parameters and generates a va_format struct, - * which can be used to validate kunit_assert_print_msg function - */ -static void verify_assert_print_msg(struct kunit *test, - struct string_stream *stream, - char *expected, const char *format, ...) -{ - va_list list; - const struct va_format vformat = { - .fmt = format, - .va = &list, - }; - - va_start(list, format); - string_stream_clear(stream); - kunit_assert_print_msg(&vformat, stream); - KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected); -} - -static void kunit_test_assert_print_msg(struct kunit *test) -{ - struct string_stream *stream; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - verify_assert_print_msg(test, stream, "\nTest", "Test"); - verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u", - "Abacaba", -123, 234U); - verify_assert_print_msg(test, stream, "", NULL); -} - -/* - * Further code contains the tests for different assert format functions. - * This helper function accepts the assert format function, executes it and - * validates the result string from the stream by checking that all of the - * substrings exist in the output. - */ -static void validate_assert(assert_format_t format_func, struct kunit *test, - const struct kunit_assert *assert, - struct string_stream *stream, int num_checks, ...) -{ - size_t i; - va_list checks; - char *cur_substr_exp; - struct va_format message = { NULL, NULL }; - - va_start(checks, num_checks); - string_stream_clear(stream); - format_func(assert, &message, stream); - - for (i = 0; i < num_checks; i++) { - cur_substr_exp = va_arg(checks, char *); - ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp); - } -} - -static void kunit_test_unary_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_unary_assert un_assert = { - .assert = assert, - .condition = "expr", - .expected_true = true, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - validate_assert(kunit_unary_assert_format, test, &un_assert.assert, - stream, 2, "true", "is false"); - - un_assert.expected_true = false; - validate_assert(kunit_unary_assert_format, test, &un_assert.assert, - stream, 2, "false", "is true"); -} - -static void kunit_test_ptr_not_err_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_ptr_not_err_assert not_err_assert = { - .assert = assert, - .text = "expr", - .value = NULL, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* Value is NULL. The corresponding message should be printed out */ - validate_assert(kunit_ptr_not_err_assert_format, test, - ¬_err_assert.assert, - stream, 1, "null"); - - /* Value is not NULL, but looks like an error pointer. Error should be printed out */ - not_err_assert.value = (void *)-12; - validate_assert(kunit_ptr_not_err_assert_format, test, - ¬_err_assert.assert, stream, 2, - "error", "-12"); -} - -static void kunit_test_binary_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - struct kunit_binary_assert_text text = { - .left_text = "1 + 2", - .operation = "==", - .right_text = "2", - }; - const struct kunit_binary_assert binary_assert = { - .assert = assert, - .text = &text, - .left_value = 3, - .right_value = 2, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* - * Printed values should depend on the input we provide: the left text, right text, left - * value and the right value. - */ - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 4, "1 + 2", "2", "3", "=="); - - text.right_text = "4 - 2"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 3, "==", "1 + 2", "4 - 2"); - - text.left_text = "3"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 4, "3", "4 - 2", "2", "=="); - - text.right_text = "2"; - validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, - stream, 3, "3", "2", "=="); -} - -static void kunit_test_binary_ptr_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - char *addr_var_a, *addr_var_b; - static const void *var_a = (void *)0xDEADBEEF; - static const void *var_b = (void *)0xBADDCAFE; - struct kunit_binary_assert_text text = { - .left_text = "var_a", - .operation = "==", - .right_text = "var_b", - }; - struct kunit_binary_ptr_assert binary_ptr_assert = { - .assert = assert, - .text = &text, - .left_value = var_a, - .right_value = var_b, - }; - - addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a); - addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b); - /* - * Print the addresses to the buffers first. - * This is necessary as we may have different count of leading zeros in the pointer - * on different architectures. - */ - snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a); - snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b); - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert, - stream, 3, addr_var_a, addr_var_b, "=="); -} - -static void kunit_test_binary_str_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct kunit_assert assert = {}; - static const char *var_a = "abacaba"; - static const char *var_b = "kernel"; - struct kunit_binary_assert_text text = { - .left_text = "var_a", - .operation = "==", - .right_text = "var_b", - }; - struct kunit_binary_str_assert binary_str_assert = { - .assert = assert, - .text = &text, - .left_value = var_a, - .right_value = var_b, - }; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - validate_assert(kunit_binary_str_assert_format, test, - &binary_str_assert.assert, - stream, 5, "var_a", "var_b", "\"abacaba\"", - "\"kernel\"", "=="); - - text.left_text = "\"abacaba\""; - validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, - stream, 4, "\"abacaba\"", "var_b", "\"kernel\"", "=="); - - text.right_text = "\"kernel\""; - validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, - stream, 3, "\"abacaba\"", "\"kernel\"", "=="); -} - -static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, - 0x45, 0x9d, 0x47, 0xd6, 0x47, - 0x2, 0x89, 0x8c, 0x81, 0x94, - 0x12, 0xfe, 0x01 }; -static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, - 0x45, 0x9d, 0x47, 0x21, 0x47, - 0xcd, 0x89, 0x24, 0x50, 0x94, - 0x12, 0xba, 0x01 }; -static void kunit_test_assert_hexdump(struct kunit *test) -{ - struct string_stream *stream; - char *str; - size_t i; - char buf[HEXDUMP_TEST_BUF_LEN]; - - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - /* Check that we are getting output like for non-matching numbers. */ - kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1)); - str = get_str_from_stream(test, stream); - for (i = 0; i < sizeof(hex_testbuf1); i++) { - snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]); - if (hex_testbuf1[i] != hex_testbuf2[i]) - ASSERT_TEST_EXPECT_CONTAIN(test, str, buf); - } - /* We shouldn't get any numbers when comparing the buffer with itself. */ - string_stream_clear(stream); - kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1)); - str = get_str_from_stream(test, stream); - ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<"); - ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">"); -} - -static void kunit_test_mem_assert_format(struct kunit *test) -{ - struct string_stream *stream; - struct string_stream *expected_stream; - struct kunit_assert assert = {}; - static const struct kunit_binary_assert_text text = { - .left_text = "hex_testbuf1", - .operation = "==", - .right_text = "hex_testbuf2", - }; - struct kunit_mem_assert mem_assert = { - .assert = assert, - .text = &text, - .left_value = NULL, - .right_value = hex_testbuf2, - .size = sizeof(hex_testbuf1), - }; - - expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream); - stream = kunit_alloc_string_stream(test, GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); - - /* The left value is NULL */ - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 2, "hex_testbuf1", "is not null"); - - /* The right value is NULL, the left value is not NULL */ - mem_assert.left_value = hex_testbuf1; - mem_assert.right_value = NULL; - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 2, "hex_testbuf2", "is not null"); - - /* Both arguments are not null */ - mem_assert.left_value = hex_testbuf1; - mem_assert.right_value = hex_testbuf2; - - validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, - stream, 3, "hex_testbuf1", "hex_testbuf2", "=="); -} - -static struct kunit_case assert_test_cases[] = { - KUNIT_CASE(kunit_test_is_literal), - KUNIT_CASE(kunit_test_is_str_literal), - KUNIT_CASE(kunit_test_assert_prologue), - KUNIT_CASE(kunit_test_assert_print_msg), - KUNIT_CASE(kunit_test_unary_assert_format), - KUNIT_CASE(kunit_test_ptr_not_err_assert_format), - KUNIT_CASE(kunit_test_binary_assert_format), - KUNIT_CASE(kunit_test_binary_ptr_assert_format), - KUNIT_CASE(kunit_test_binary_str_assert_format), - KUNIT_CASE(kunit_test_assert_hexdump), - KUNIT_CASE(kunit_test_mem_assert_format), - {} -}; - -static struct kunit_suite assert_test_suite = { - .name = "kunit-assert", - .test_cases = assert_test_cases, -}; - -kunit_test_suites(&assert_test_suite); diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index 42178d5a97d1..efba189442c2 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -849,10 +849,387 @@ static struct kunit_suite kunit_current_test_suite = { .test_cases = kunit_current_test_cases, }; +#define TEST_PTR_EXPECTED_BUF_SIZE 32 +#define HEXDUMP_TEST_BUF_LEN 5 +#define ASSERT_TEST_EXPECT_CONTAIN(test, str, substr) KUNIT_EXPECT_TRUE(test, strstr(str, substr)) +#define ASSERT_TEST_EXPECT_NCONTAIN(test, str, substr) KUNIT_EXPECT_FALSE(test, strstr(str, substr)) + +static void kunit_test_is_literal(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, is_literal("5", 5)); + KUNIT_EXPECT_TRUE(test, is_literal("0", 0)); + KUNIT_EXPECT_TRUE(test, is_literal("1234567890", 1234567890)); + KUNIT_EXPECT_TRUE(test, is_literal("-1234567890", -1234567890)); + KUNIT_EXPECT_FALSE(test, is_literal("05", 5)); + KUNIT_EXPECT_FALSE(test, is_literal("", 0)); + KUNIT_EXPECT_FALSE(test, is_literal("-0", 0)); + KUNIT_EXPECT_FALSE(test, is_literal("12#45", 1245)); +} + +static void kunit_test_is_str_literal(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, is_str_literal("\"Hello, World!\"", "Hello, World!")); + KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"", "")); + KUNIT_EXPECT_TRUE(test, is_str_literal("\"\"\"", "\"")); + KUNIT_EXPECT_FALSE(test, is_str_literal("", "")); + KUNIT_EXPECT_FALSE(test, is_str_literal("\"", "\"")); + KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba", "Abacaba")); + KUNIT_EXPECT_FALSE(test, is_str_literal("Abacaba\"", "Abacaba")); + KUNIT_EXPECT_FALSE(test, is_str_literal("\"Abacaba\"", "\"Abacaba\"")); +} + +/* this function is used to get a "char *" string from the string stream and defer its cleanup */ +static char *get_str_from_stream(struct kunit *test, struct string_stream *stream) +{ + char *str = string_stream_get_string(stream); + + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); + kunit_add_action(test, kfree_wrapper, (void *)str); + + return str; +} + +static void kunit_test_assert_prologue(struct kunit *test) +{ + struct string_stream *stream; + char *str; + const struct kunit_loc location = { + .file = "testfile.c", + .line = 1337, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* Test an expectation fail prologue */ + kunit_assert_prologue(&location, KUNIT_EXPECTATION, stream); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "EXPECTATION"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); + + /* Test an assertion fail prologue */ + string_stream_clear(stream); + kunit_assert_prologue(&location, KUNIT_ASSERTION, stream); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "ASSERTION"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "testfile.c"); + ASSERT_TEST_EXPECT_CONTAIN(test, str, "1337"); +} + +/* + * This function accepts an arbitrary count of parameters and generates a va_format struct, + * which can be used to validate kunit_assert_print_msg function + */ +static void verify_assert_print_msg(struct kunit *test, + struct string_stream *stream, + char *expected, const char *format, ...) +{ + va_list list; + const struct va_format vformat = { + .fmt = format, + .va = &list, + }; + + va_start(list, format); + string_stream_clear(stream); + kunit_assert_print_msg(&vformat, stream); + KUNIT_EXPECT_STREQ(test, get_str_from_stream(test, stream), expected); +} + +static void kunit_test_assert_print_msg(struct kunit *test) +{ + struct string_stream *stream; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + verify_assert_print_msg(test, stream, "\nTest", "Test"); + verify_assert_print_msg(test, stream, "\nAbacaba -123 234", "%s %d %u", + "Abacaba", -123, 234U); + verify_assert_print_msg(test, stream, "", NULL); +} + +/* + * Further code contains the tests for different assert format functions. + * This helper function accepts the assert format function, executes it and + * validates the result string from the stream by checking that all of the + * substrings exist in the output. + */ +static void validate_assert(assert_format_t format_func, struct kunit *test, + const struct kunit_assert *assert, + struct string_stream *stream, int num_checks, ...) +{ + size_t i; + va_list checks; + char *cur_substr_exp; + struct va_format message = { NULL, NULL }; + + va_start(checks, num_checks); + string_stream_clear(stream); + format_func(assert, &message, stream); + + for (i = 0; i < num_checks; i++) { + cur_substr_exp = va_arg(checks, char *); + ASSERT_TEST_EXPECT_CONTAIN(test, get_str_from_stream(test, stream), cur_substr_exp); + } +} + +static void kunit_test_unary_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_unary_assert un_assert = { + .assert = assert, + .condition = "expr", + .expected_true = true, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + validate_assert(kunit_unary_assert_format, test, &un_assert.assert, + stream, 2, "true", "is false"); + + un_assert.expected_true = false; + validate_assert(kunit_unary_assert_format, test, &un_assert.assert, + stream, 2, "false", "is true"); +} + +static void kunit_test_ptr_not_err_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_ptr_not_err_assert not_err_assert = { + .assert = assert, + .text = "expr", + .value = NULL, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* Value is NULL. The corresponding message should be printed out */ + validate_assert(kunit_ptr_not_err_assert_format, test, + ¬_err_assert.assert, + stream, 1, "null"); + + /* Value is not NULL, but looks like an error pointer. Error should be printed out */ + not_err_assert.value = (void *)-12; + validate_assert(kunit_ptr_not_err_assert_format, test, + ¬_err_assert.assert, stream, 2, + "error", "-12"); +} + +static void kunit_test_binary_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + struct kunit_binary_assert_text text = { + .left_text = "1 + 2", + .operation = "==", + .right_text = "2", + }; + const struct kunit_binary_assert binary_assert = { + .assert = assert, + .text = &text, + .left_value = 3, + .right_value = 2, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* + * Printed values should depend on the input we provide: the left text, right text, left + * value and the right value. + */ + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 4, "1 + 2", "2", "3", "=="); + + text.right_text = "4 - 2"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 3, "==", "1 + 2", "4 - 2"); + + text.left_text = "3"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 4, "3", "4 - 2", "2", "=="); + + text.right_text = "2"; + validate_assert(kunit_binary_assert_format, test, &binary_assert.assert, + stream, 3, "3", "2", "=="); +} + +static void kunit_test_binary_ptr_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + char *addr_var_a, *addr_var_b; + static const void *var_a = (void *)0xDEADBEEF; + static const void *var_b = (void *)0xBADDCAFE; + struct kunit_binary_assert_text text = { + .left_text = "var_a", + .operation = "==", + .right_text = "var_b", + }; + struct kunit_binary_ptr_assert binary_ptr_assert = { + .assert = assert, + .text = &text, + .left_value = var_a, + .right_value = var_b, + }; + + addr_var_a = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_a); + addr_var_b = kunit_kzalloc(test, TEST_PTR_EXPECTED_BUF_SIZE, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, addr_var_b); + /* + * Print the addresses to the buffers first. + * This is necessary as we may have different count of leading zeros in the pointer + * on different architectures. + */ + snprintf(addr_var_a, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_a); + snprintf(addr_var_b, TEST_PTR_EXPECTED_BUF_SIZE, "%px", var_b); + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + validate_assert(kunit_binary_ptr_assert_format, test, &binary_ptr_assert.assert, + stream, 3, addr_var_a, addr_var_b, "=="); +} + +static void kunit_test_binary_str_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct kunit_assert assert = {}; + static const char *var_a = "abacaba"; + static const char *var_b = "kernel"; + struct kunit_binary_assert_text text = { + .left_text = "var_a", + .operation = "==", + .right_text = "var_b", + }; + struct kunit_binary_str_assert binary_str_assert = { + .assert = assert, + .text = &text, + .left_value = var_a, + .right_value = var_b, + }; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + validate_assert(kunit_binary_str_assert_format, test, + &binary_str_assert.assert, + stream, 5, "var_a", "var_b", "\"abacaba\"", + "\"kernel\"", "=="); + + text.left_text = "\"abacaba\""; + validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, + stream, 4, "\"abacaba\"", "var_b", "\"kernel\"", "=="); + + text.right_text = "\"kernel\""; + validate_assert(kunit_binary_str_assert_format, test, &binary_str_assert.assert, + stream, 3, "\"abacaba\"", "\"kernel\"", "=="); +} + +static const u8 hex_testbuf1[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, + 0x45, 0x9d, 0x47, 0xd6, 0x47, + 0x2, 0x89, 0x8c, 0x81, 0x94, + 0x12, 0xfe, 0x01 }; +static const u8 hex_testbuf2[] = { 0x26, 0x74, 0x6b, 0x9c, 0x55, + 0x45, 0x9d, 0x47, 0x21, 0x47, + 0xcd, 0x89, 0x24, 0x50, 0x94, + 0x12, 0xba, 0x01 }; +static void kunit_test_assert_hexdump(struct kunit *test) +{ + struct string_stream *stream; + char *str; + size_t i; + char buf[HEXDUMP_TEST_BUF_LEN]; + + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + /* Check that we are getting output like for non-matching numbers. */ + kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf2, sizeof(hex_testbuf1)); + str = get_str_from_stream(test, stream); + for (i = 0; i < sizeof(hex_testbuf1); i++) { + snprintf(buf, HEXDUMP_TEST_BUF_LEN, "<%02x>", hex_testbuf1[i]); + if (hex_testbuf1[i] != hex_testbuf2[i]) + ASSERT_TEST_EXPECT_CONTAIN(test, str, buf); + } + /* We shouldn't get any numbers when comparing the buffer with itself. */ + string_stream_clear(stream); + kunit_assert_hexdump(stream, hex_testbuf1, hex_testbuf1, sizeof(hex_testbuf1)); + str = get_str_from_stream(test, stream); + ASSERT_TEST_EXPECT_NCONTAIN(test, str, "<"); + ASSERT_TEST_EXPECT_NCONTAIN(test, str, ">"); +} + +static void kunit_test_mem_assert_format(struct kunit *test) +{ + struct string_stream *stream; + struct string_stream *expected_stream; + struct kunit_assert assert = {}; + static const struct kunit_binary_assert_text text = { + .left_text = "hex_testbuf1", + .operation = "==", + .right_text = "hex_testbuf2", + }; + struct kunit_mem_assert mem_assert = { + .assert = assert, + .text = &text, + .left_value = NULL, + .right_value = hex_testbuf2, + .size = sizeof(hex_testbuf1), + }; + + expected_stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_stream); + stream = kunit_alloc_string_stream(test, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); + + /* The left value is NULL */ + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 2, "hex_testbuf1", "is not null"); + + /* The right value is NULL, the left value is not NULL */ + mem_assert.left_value = hex_testbuf1; + mem_assert.right_value = NULL; + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 2, "hex_testbuf2", "is not null"); + + /* Both arguments are not null */ + mem_assert.left_value = hex_testbuf1; + mem_assert.right_value = hex_testbuf2; + + validate_assert(kunit_mem_assert_format, test, &mem_assert.assert, + stream, 3, "hex_testbuf1", "hex_testbuf2", "=="); +} + +static struct kunit_case kunit_assert_test_cases[] = { + KUNIT_CASE(kunit_test_is_literal), + KUNIT_CASE(kunit_test_is_str_literal), + KUNIT_CASE(kunit_test_assert_prologue), + KUNIT_CASE(kunit_test_assert_print_msg), + KUNIT_CASE(kunit_test_unary_assert_format), + KUNIT_CASE(kunit_test_ptr_not_err_assert_format), + KUNIT_CASE(kunit_test_binary_assert_format), + KUNIT_CASE(kunit_test_binary_ptr_assert_format), + KUNIT_CASE(kunit_test_binary_str_assert_format), + KUNIT_CASE(kunit_test_assert_hexdump), + KUNIT_CASE(kunit_test_mem_assert_format), + {} +}; + +static struct kunit_suite kunit_assert_test_suite = { + .name = "kunit-assert", + .test_cases = kunit_assert_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, &kunit_log_test_suite, &kunit_status_test_suite, &kunit_current_test_suite, &kunit_device_test_suite, - &kunit_fault_test_suite); + &kunit_fault_test_suite, &kunit_assert_test_suite); MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING); MODULE_LICENSE("GPL v2");