xref: /linux/lib/tests/uuid_kunit.c (revision 995832b2cebe6969d1b42635db698803ee31294d)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*
3  * Test cases for lib/uuid.c module.
4  */
5 
6 #include <kunit/test.h>
7 #include <linux/uuid.h>
8 
9 struct test_uuid_data {
10 	const char *uuid;
11 	guid_t le;
12 	uuid_t be;
13 };
14 
15 static const struct test_uuid_data test_uuid_test_data[] = {
16 	{
17 		.uuid = "c33f4995-3701-450e-9fbf-206a2e98e576",
18 		.le = GUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
19 		.be = UUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
20 	},
21 	{
22 		.uuid = "64b4371c-77c1-48f9-8221-29f054fc023b",
23 		.le = GUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
24 		.be = UUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
25 	},
26 	{
27 		.uuid = "0cb4ddff-a545-4401-9d06-688af53e7f84",
28 		.le = GUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
29 		.be = UUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
30 	},
31 };
32 
33 static const char * const test_uuid_wrong_data[] = {
34 	"c33f4995-3701-450e-9fbf206a2e98e576 ",	/* no hyphen(s) */
35 	"64b4371c-77c1-48f9-8221-29f054XX023b",	/* invalid character(s) */
36 	"0cb4ddff-a545-4401-9d06-688af53e",	/* not enough data */
37 };
38 
39 static void uuid_test_guid_valid(struct kunit *test)
40 {
41 	unsigned int i;
42 	const struct test_uuid_data *data;
43 	guid_t le;
44 
45 	for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++) {
46 		data = &test_uuid_test_data[i];
47 		KUNIT_EXPECT_EQ(test, guid_parse(data->uuid, &le), 0);
48 		KUNIT_EXPECT_TRUE(test, guid_equal(&data->le, &le));
49 	}
50 }
51 
52 static void uuid_test_uuid_valid(struct kunit *test)
53 {
54 	unsigned int i;
55 	const struct test_uuid_data *data;
56 	uuid_t be;
57 
58 	for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++) {
59 		data = &test_uuid_test_data[i];
60 		KUNIT_EXPECT_EQ(test, uuid_parse(data->uuid, &be), 0);
61 		KUNIT_EXPECT_TRUE(test, uuid_equal(&data->be, &be));
62 	}
63 }
64 
65 static void uuid_test_guid_invalid(struct kunit *test)
66 {
67 	unsigned int i;
68 	const char *uuid;
69 	guid_t le;
70 
71 	for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++) {
72 		uuid = test_uuid_wrong_data[i];
73 		KUNIT_EXPECT_EQ(test, guid_parse(uuid, &le), -EINVAL);
74 	}
75 }
76 
77 static void uuid_test_uuid_invalid(struct kunit *test)
78 {
79 	unsigned int i;
80 	const char *uuid;
81 	uuid_t be;
82 
83 	for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++) {
84 		uuid = test_uuid_wrong_data[i];
85 		KUNIT_EXPECT_EQ(test, uuid_parse(uuid, &be), -EINVAL);
86 	}
87 }
88 
89 /*
90  * RFC 4122 section 4.4 says random UUIDs/GUIDs (version 4) must have:
91  *   - version 4 in the high nibble of the version byte,
92  *   - variant DCE 1.1 (binary 10x) in the high bits of byte 8.
93  *
94  * The version byte is byte 6 in the "wire" uuid_t layout and byte 7 in
95  * the byte-swapped guid_t layout.
96  */
97 static void uuid_test_uuid_gen(struct kunit *test)
98 {
99 	uuid_t u;
100 
101 	for (unsigned int i = 0; i < 8; i++) {
102 		uuid_gen(&u);
103 		KUNIT_EXPECT_EQ(test, u.b[6] & 0xf0, 0x40);
104 		KUNIT_EXPECT_EQ(test, u.b[8] & 0xc0, 0x80);
105 	}
106 }
107 
108 static void uuid_test_guid_gen(struct kunit *test)
109 {
110 	guid_t g;
111 
112 	for (unsigned int i = 0; i < 8; i++) {
113 		guid_gen(&g);
114 		KUNIT_EXPECT_EQ(test, g.b[7] & 0xf0, 0x40);
115 		KUNIT_EXPECT_EQ(test, g.b[8] & 0xc0, 0x80);
116 	}
117 }
118 
119 static void uuid_test_generate_random_uuid(struct kunit *test)
120 {
121 	unsigned char buf[16];
122 
123 	for (unsigned int i = 0; i < 8; i++) {
124 		generate_random_uuid(buf);
125 		KUNIT_EXPECT_EQ(test, buf[6] & 0xf0, 0x40);
126 		KUNIT_EXPECT_EQ(test, buf[8] & 0xc0, 0x80);
127 	}
128 }
129 
130 static void uuid_test_generate_random_guid(struct kunit *test)
131 {
132 	unsigned char buf[16];
133 
134 	for (unsigned int i = 0; i < 8; i++) {
135 		generate_random_guid(buf);
136 		KUNIT_EXPECT_EQ(test, buf[7] & 0xf0, 0x40);
137 		KUNIT_EXPECT_EQ(test, buf[8] & 0xc0, 0x80);
138 	}
139 }
140 
141 static struct kunit_case uuid_test_cases[] = {
142 	KUNIT_CASE(uuid_test_guid_valid),
143 	KUNIT_CASE(uuid_test_uuid_valid),
144 	KUNIT_CASE(uuid_test_guid_invalid),
145 	KUNIT_CASE(uuid_test_uuid_invalid),
146 	KUNIT_CASE(uuid_test_uuid_gen),
147 	KUNIT_CASE(uuid_test_guid_gen),
148 	KUNIT_CASE(uuid_test_generate_random_uuid),
149 	KUNIT_CASE(uuid_test_generate_random_guid),
150 	{},
151 };
152 
153 static struct kunit_suite uuid_test_suite = {
154 	.name = "uuid",
155 	.test_cases = uuid_test_cases,
156 };
157 
158 kunit_test_suite(uuid_test_suite);
159 
160 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
161 MODULE_DESCRIPTION("Test cases for lib/uuid.c module");
162 MODULE_LICENSE("Dual BSD/GPL");
163