mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/tests: add string_get_size() boundary tests
@ 2026-04-21 20:19 Shuvam Pandey
  2026-04-21 20:32 ` Josh Law
  2026-04-22  7:32 ` Andy Shevchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Shuvam Pandey @ 2026-04-21 20:19 UTC (permalink / raw)
  To: Kees Cook, Andy Shevchenko
  Cc: James Bottomley, Andrew Morton, linux-hardening, linux-kernel

string_get_size() has several non-obvious outputs around decimal and
binary unit boundaries because the unit is chosen before the final
rounding step. Decimal values just below 1 MB can print as "1000 kB";
binary values below 1 KiB stay in bytes; and rounded binary values can
print as "1000 KiB" or "1024 KiB" before the input reaches the next
binary unit.

Add KUnit coverage for these current outputs and nearby threshold values
so the existing behavior is explicit.

Link: https://lore.kernel.org/r/CAHp75VfB0Y7K=chk5wpKtZcgVfCN5X7zhNEq6QSrirdgHs5_bQ@mail.gmail.com
Link: https://lore.kernel.org/r/696d25567aadbb8ba324d4a980c15eb5cf4ccef6.camel@HansenPartnership.com
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
---
 lib/tests/string_helpers_kunit.c | 33 ++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c
index c853046183d24..92454837f3ddd 100644
--- a/lib/tests/string_helpers_kunit.c
+++ b/lib/tests/string_helpers_kunit.c
@@ -558,6 +558,39 @@ static void test_get_size(struct kunit *test)
 	/* weird block sizes */
 	test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
 
+	/* decimal boundary around 1 MB */
+	test_string_get_size_one(999499, 1, "999 kB", "976 KiB");
+	test_string_get_size_one(999500, 1, "1000 kB", "976 KiB");
+	test_string_get_size_one(999999, 1, "1000 kB", "977 KiB");
+	test_string_get_size_one(1000000, 1, "1.00 MB", "977 KiB");
+	test_string_get_size_one(1000001, 1, "1.00 MB", "977 KiB");
+
+	/* binary values stay in bytes until the 1 KiB boundary */
+	test_string_get_size_one(1000, 1, "1.00 kB", "1000 B");
+	test_string_get_size_one(1018, 1, "1.02 kB", "1018 B");
+	test_string_get_size_one(1023, 1, "1.02 kB", "1023 B");
+	test_string_get_size_one(1024, 1, "1.02 kB", "1.00 KiB");
+	test_string_get_size_one(1025, 1, "1.03 kB", "1.00 KiB");
+
+	/* binary rounding around 1000 KiB */
+	test_string_get_size_one(1023487, 1, "1.02 MB", "999 KiB");
+	test_string_get_size_one(1023488, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024000, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024511, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024512, 1, "1.02 MB", "1001 KiB");
+
+	/* binary boundary around 1 MiB */
+	test_string_get_size_one(1048063, 1, "1.05 MB", "1023 KiB");
+	test_string_get_size_one(1048064, 1, "1.05 MB", "1024 KiB");
+	test_string_get_size_one(1048575, 1, "1.05 MB", "1024 KiB");
+	test_string_get_size_one(1048576, 1, "1.05 MB", "1.00 MiB");
+	test_string_get_size_one(1048577, 1, "1.05 MB", "1.00 MiB");
+
+	/* binary boundary around 2 MiB */
+	test_string_get_size_one(2097151, 1, "2.10 MB", "2.00 MiB");
+	test_string_get_size_one(2097152, 1, "2.10 MB", "2.00 MiB");
+	test_string_get_size_one(2097153, 1, "2.10 MB", "2.00 MiB");
+
 	/* huge values */
 	test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
 	test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
-- 
2.50.1 (Apple Git-155)



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-22  7:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-21 20:19 [PATCH] lib/tests: add string_get_size() boundary tests Shuvam Pandey
2026-04-21 20:32 ` Josh Law
2026-04-22  7:32 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®