xref: /openbmc/linux/lib/test_uuid.c (revision 81de3bf3)
1 /*
2  * Test cases for lib/uuid.c module.
3  */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
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>
11 
12 struct test_uuid_data {
13 	const char *uuid;
14 	guid_t le;
15 	uuid_t be;
16 };
17 
18 static const struct test_uuid_data test_uuid_test_data[] = {
19 	{
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),
23 	},
24 	{
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),
28 	},
29 	{
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),
33 	},
34 };
35 
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 */
40 };
41 
42 static unsigned total_tests __initdata;
43 static unsigned failed_tests __initdata;
44 
45 static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
46 				    const char *data, const char *actual)
47 {
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++;
60 }
61 
62 static void __init test_uuid_test(const struct test_uuid_data *data)
63 {
64 	guid_t le;
65 	uuid_t be;
66 	char buf[48];
67 
68 	/* LE */
69 	total_tests++;
70 	if (guid_parse(data->uuid, &le))
71 		test_uuid_failed("conversion", false, false, data->uuid, NULL);
72 
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);
77 	}
78 
79 	/* BE */
80 	total_tests++;
81 	if (uuid_parse(data->uuid, &be))
82 		test_uuid_failed("conversion", false, true, data->uuid, NULL);
83 
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);
88 	}
89 }
90 
91 static void __init test_uuid_wrong(const char *data)
92 {
93 	guid_t le;
94 	uuid_t be;
95 
96 	/* LE */
97 	total_tests++;
98 	if (!guid_parse(data, &le))
99 		test_uuid_failed("negative", true, false, data, NULL);
100 
101 	/* BE */
102 	total_tests++;
103 	if (!uuid_parse(data, &be))
104 		test_uuid_failed("negative", true, true, data, NULL);
105 }
106 
107 static int __init test_uuid_init(void)
108 {
109 	unsigned int i;
110 
111 	for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++)
112 		test_uuid_test(&test_uuid_test_data[i]);
113 
114 	for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++)
115 		test_uuid_wrong(test_uuid_wrong_data[i]);
116 
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);
121 
122 	return failed_tests ? -EINVAL : 0;
123 }
124 module_init(test_uuid_init);
125 
126 static void __exit test_uuid_exit(void)
127 {
128 	/* do nothing */
129 }
130 module_exit(test_uuid_exit);
131 
132 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
133 MODULE_LICENSE("Dual BSD/GPL");
134