Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / lib / test_uuid.c
blobcd819c397dc7ebed8be5bc6167254c543d0f26c8
1 /*
2 * Test cases for lib/uuid.c module.
3 */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/uuid.h>
12 struct test_uuid_data {
13 const char *uuid;
14 guid_t le;
15 uuid_t be;
18 static const struct test_uuid_data test_uuid_test_data[] = {
20 .uuid = "c33f4995-3701-450e-9fbf-206a2e98e576",
21 .le = GUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
22 .be = UUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
25 .uuid = "64b4371c-77c1-48f9-8221-29f054fc023b",
26 .le = GUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
27 .be = UUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
30 .uuid = "0cb4ddff-a545-4401-9d06-688af53e7f84",
31 .le = GUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
32 .be = UUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
36 static const char * const test_uuid_wrong_data[] = {
37 "c33f4995-3701-450e-9fbf206a2e98e576 ", /* no hyphen(s) */
38 "64b4371c-77c1-48f9-8221-29f054XX023b", /* invalid character(s) */
39 "0cb4ddff-a545-4401-9d06-688af53e", /* not enough data */
42 static unsigned total_tests __initdata;
43 static unsigned failed_tests __initdata;
45 static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
46 const char *data, const char *actual)
48 pr_err("%s test #%u %s %s data: '%s'\n",
49 prefix,
50 total_tests,
51 wrong ? "passed on wrong" : "failed on",
52 be ? "BE" : "LE",
53 data);
54 if (actual && *actual)
55 pr_err("%s test #%u actual data: '%s'\n",
56 prefix,
57 total_tests,
58 actual);
59 failed_tests++;
62 static void __init test_uuid_test(const struct test_uuid_data *data)
64 guid_t le;
65 uuid_t be;
66 char buf[48];
68 /* LE */
69 total_tests++;
70 if (guid_parse(data->uuid, &le))
71 test_uuid_failed("conversion", false, false, data->uuid, NULL);
73 total_tests++;
74 if (!guid_equal(&data->le, &le)) {
75 sprintf(buf, "%pUl", &le);
76 test_uuid_failed("cmp", false, false, data->uuid, buf);
79 /* BE */
80 total_tests++;
81 if (uuid_parse(data->uuid, &be))
82 test_uuid_failed("conversion", false, true, data->uuid, NULL);
84 total_tests++;
85 if (!uuid_equal(&data->be, &be)) {
86 sprintf(buf, "%pUb", &be);
87 test_uuid_failed("cmp", false, true, data->uuid, buf);
91 static void __init test_uuid_wrong(const char *data)
93 guid_t le;
94 uuid_t be;
96 /* LE */
97 total_tests++;
98 if (!guid_parse(data, &le))
99 test_uuid_failed("negative", true, false, data, NULL);
101 /* BE */
102 total_tests++;
103 if (!uuid_parse(data, &be))
104 test_uuid_failed("negative", true, true, data, NULL);
107 static int __init test_uuid_init(void)
109 unsigned int i;
111 for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++)
112 test_uuid_test(&test_uuid_test_data[i]);
114 for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++)
115 test_uuid_wrong(test_uuid_wrong_data[i]);
117 if (failed_tests == 0)
118 pr_info("all %u tests passed\n", total_tests);
119 else
120 pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
122 return failed_tests ? -EINVAL : 0;
124 module_init(test_uuid_init);
126 static void __exit test_uuid_exit(void)
128 /* do nothing */
130 module_exit(test_uuid_exit);
132 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
133 MODULE_LICENSE("Dual BSD/GPL");