blob: cd819c397dc7ebed8be5bc6167254c543d0f26c8 [file] [log] [blame]
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +03001/*
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
12struct test_uuid_data {
13 const char *uuid;
Christoph Hellwigf9727a12017-05-17 10:02:48 +020014 guid_t le;
15 uuid_t be;
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030016};
17
18static const struct test_uuid_data test_uuid_test_data[] = {
19 {
20 .uuid = "c33f4995-3701-450e-9fbf-206a2e98e576",
Christoph Hellwigf9727a12017-05-17 10:02:48 +020021 .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),
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030023 },
24 {
25 .uuid = "64b4371c-77c1-48f9-8221-29f054fc023b",
Christoph Hellwigf9727a12017-05-17 10:02:48 +020026 .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),
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030028 },
29 {
30 .uuid = "0cb4ddff-a545-4401-9d06-688af53e7f84",
Christoph Hellwigf9727a12017-05-17 10:02:48 +020031 .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),
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030033 },
34};
35
36static 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
42static unsigned total_tests __initdata;
43static unsigned failed_tests __initdata;
44
45static 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
62static void __init test_uuid_test(const struct test_uuid_data *data)
63{
Christoph Hellwigf9727a12017-05-17 10:02:48 +020064 guid_t le;
65 uuid_t be;
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030066 char buf[48];
67
68 /* LE */
69 total_tests++;
Christoph Hellwigf9727a12017-05-17 10:02:48 +020070 if (guid_parse(data->uuid, &le))
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030071 test_uuid_failed("conversion", false, false, data->uuid, NULL);
72
73 total_tests++;
Christoph Hellwigdf337672017-05-11 14:00:57 +020074 if (!guid_equal(&data->le, &le)) {
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030075 sprintf(buf, "%pUl", &le);
76 test_uuid_failed("cmp", false, false, data->uuid, buf);
77 }
78
79 /* BE */
80 total_tests++;
Christoph Hellwigf9727a12017-05-17 10:02:48 +020081 if (uuid_parse(data->uuid, &be))
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030082 test_uuid_failed("conversion", false, true, data->uuid, NULL);
83
84 total_tests++;
Christoph Hellwigd9cf4842017-07-20 09:54:03 +020085 if (!uuid_equal(&data->be, &be)) {
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030086 sprintf(buf, "%pUb", &be);
87 test_uuid_failed("cmp", false, true, data->uuid, buf);
88 }
89}
90
91static void __init test_uuid_wrong(const char *data)
92{
Christoph Hellwigf9727a12017-05-17 10:02:48 +020093 guid_t le;
94 uuid_t be;
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030095
96 /* LE */
97 total_tests++;
Christoph Hellwigf9727a12017-05-17 10:02:48 +020098 if (!guid_parse(data, &le))
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +030099 test_uuid_failed("negative", true, false, data, NULL);
100
101 /* BE */
102 total_tests++;
Christoph Hellwigf9727a12017-05-17 10:02:48 +0200103 if (!uuid_parse(data, &be))
Andy Shevchenkocfaff0e2016-05-30 17:40:41 +0300104 test_uuid_failed("negative", true, true, data, NULL);
105}
106
107static 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}
124module_init(test_uuid_init);
125
126static void __exit test_uuid_exit(void)
127{
128 /* do nothing */
129}
130module_exit(test_uuid_exit);
131
132MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
133MODULE_LICENSE("Dual BSD/GPL");