printf: add tests for generic FourCCs
authorAditya Garg <gargaditya08@live.com>
Tue, 8 Apr 2025 06:48:32 +0000 (12:18 +0530)
committerAlyssa Rosenzweig <alyssa@rosenzweig.io>
Mon, 21 Apr 2025 14:18:30 +0000 (10:18 -0400)
This patch adds support for kunit tests of generic 32-bit FourCCs added to
vsprintf.

Acked-by: Tamir Duberstein <tamird@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Aditya Garg <gargaditya08@live.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Link: https://lore.kernel.org/r/PN3PR01MB95973AF4F6262B2D1996FB25B8B52@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
lib/tests/printf_kunit.c

index 2c9f6170bacdbf21dda0ac6abcae4b58d5f6ebd2..b1fa0dcea52fe9d40c229bab0faa2ce5f21fb46a 100644 (file)
@@ -701,21 +701,46 @@ static void fwnode_pointer(struct kunit *kunittest)
        software_node_unregister_node_group(group);
 }
 
+struct fourcc_struct {
+       u32 code;
+       const char *str;
+};
+
+static void fourcc_pointer_test(struct kunit *kunittest, const struct fourcc_struct *fc,
+                               size_t n, const char *fmt)
+{
+       size_t i;
+
+       for (i = 0; i < n; i++)
+               test(fc[i].str, fmt, &fc[i].code);
+}
+
 static void fourcc_pointer(struct kunit *kunittest)
 {
-       struct {
-               u32 code;
-               char *str;
-       } const try[] = {
+       static const struct fourcc_struct try_cc[] = {
                { 0x3231564e, "NV12 little-endian (0x3231564e)", },
                { 0xb231564e, "NV12 big-endian (0xb231564e)", },
                { 0x10111213, ".... little-endian (0x10111213)", },
                { 0x20303159, "Y10  little-endian (0x20303159)", },
        };
-       unsigned int i;
+       static const struct fourcc_struct try_ch[] = {
+               { 0x41424344, "ABCD (0x41424344)", },
+       };
+       static const struct fourcc_struct try_cn[] = {
+               { 0x41424344, "DCBA (0x44434241)", },
+       };
+       static const struct fourcc_struct try_cl[] = {
+               { (__force u32)cpu_to_le32(0x41424344), "ABCD (0x41424344)", },
+       };
+       static const struct fourcc_struct try_cb[] = {
+               { (__force u32)cpu_to_be32(0x41424344), "ABCD (0x41424344)", },
+       };
 
-       for (i = 0; i < ARRAY_SIZE(try); i++)
-               test(try[i].str, "%p4cc", &try[i].code);
+       fourcc_pointer_test(kunittest, try_cc, ARRAY_SIZE(try_cc), "%p4cc");
+       fourcc_pointer_test(kunittest, try_ch, ARRAY_SIZE(try_ch), "%p4ch");
+       fourcc_pointer_test(kunittest, try_cn, ARRAY_SIZE(try_cn), "%p4cn");
+       fourcc_pointer_test(kunittest, try_cl, ARRAY_SIZE(try_cl), "%p4cl");
+       fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
 }
 
 static void