xref: /linux/lib/tests/uuid_kunit.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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 static struct kunit_case uuid_test_cases[] = {
90 	KUNIT_CASE(uuid_test_guid_valid),
91 	KUNIT_CASE(uuid_test_uuid_valid),
92 	KUNIT_CASE(uuid_test_guid_invalid),
93 	KUNIT_CASE(uuid_test_uuid_invalid),
94 	{},
95 };
96 
97 static struct kunit_suite uuid_test_suite = {
98 	.name = "uuid",
99 	.test_cases = uuid_test_cases,
100 };
101 
102 kunit_test_suite(uuid_test_suite);
103 
104 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
105 MODULE_DESCRIPTION("Test cases for lib/uuid.c module");
106 MODULE_LICENSE("Dual BSD/GPL");
107