Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Test cases for lib/uuid.c module.
0003  */
0004 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0005 
0006 #include <linux/init.h>
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/string.h>
0010 #include <linux/uuid.h>
0011 
0012 struct test_uuid_data {
0013     const char *uuid;
0014     guid_t le;
0015     uuid_t be;
0016 };
0017 
0018 static const struct test_uuid_data test_uuid_test_data[] = {
0019     {
0020         .uuid = "c33f4995-3701-450e-9fbf-206a2e98e576",
0021         .le = GUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
0022         .be = UUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
0023     },
0024     {
0025         .uuid = "64b4371c-77c1-48f9-8221-29f054fc023b",
0026         .le = GUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
0027         .be = UUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
0028     },
0029     {
0030         .uuid = "0cb4ddff-a545-4401-9d06-688af53e7f84",
0031         .le = GUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
0032         .be = UUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
0033     },
0034 };
0035 
0036 static const char * const test_uuid_wrong_data[] = {
0037     "c33f4995-3701-450e-9fbf206a2e98e576 ", /* no hyphen(s) */
0038     "64b4371c-77c1-48f9-8221-29f054XX023b", /* invalid character(s) */
0039     "0cb4ddff-a545-4401-9d06-688af53e", /* not enough data */
0040 };
0041 
0042 static unsigned total_tests __initdata;
0043 static unsigned failed_tests __initdata;
0044 
0045 static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
0046                     const char *data, const char *actual)
0047 {
0048     pr_err("%s test #%u %s %s data: '%s'\n",
0049            prefix,
0050            total_tests,
0051            wrong ? "passed on wrong" : "failed on",
0052            be ? "BE" : "LE",
0053            data);
0054     if (actual && *actual)
0055         pr_err("%s test #%u actual data: '%s'\n",
0056                prefix,
0057                total_tests,
0058                actual);
0059     failed_tests++;
0060 }
0061 
0062 static void __init test_uuid_test(const struct test_uuid_data *data)
0063 {
0064     guid_t le;
0065     uuid_t be;
0066     char buf[48];
0067 
0068     /* LE */
0069     total_tests++;
0070     if (guid_parse(data->uuid, &le))
0071         test_uuid_failed("conversion", false, false, data->uuid, NULL);
0072 
0073     total_tests++;
0074     if (!guid_equal(&data->le, &le)) {
0075         sprintf(buf, "%pUl", &le);
0076         test_uuid_failed("cmp", false, false, data->uuid, buf);
0077     }
0078 
0079     /* BE */
0080     total_tests++;
0081     if (uuid_parse(data->uuid, &be))
0082         test_uuid_failed("conversion", false, true, data->uuid, NULL);
0083 
0084     total_tests++;
0085     if (!uuid_equal(&data->be, &be)) {
0086         sprintf(buf, "%pUb", &be);
0087         test_uuid_failed("cmp", false, true, data->uuid, buf);
0088     }
0089 }
0090 
0091 static void __init test_uuid_wrong(const char *data)
0092 {
0093     guid_t le;
0094     uuid_t be;
0095 
0096     /* LE */
0097     total_tests++;
0098     if (!guid_parse(data, &le))
0099         test_uuid_failed("negative", true, false, data, NULL);
0100 
0101     /* BE */
0102     total_tests++;
0103     if (!uuid_parse(data, &be))
0104         test_uuid_failed("negative", true, true, data, NULL);
0105 }
0106 
0107 static int __init test_uuid_init(void)
0108 {
0109     unsigned int i;
0110 
0111     for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++)
0112         test_uuid_test(&test_uuid_test_data[i]);
0113 
0114     for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++)
0115         test_uuid_wrong(test_uuid_wrong_data[i]);
0116 
0117     if (failed_tests == 0)
0118         pr_info("all %u tests passed\n", total_tests);
0119     else
0120         pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
0121 
0122     return failed_tests ? -EINVAL : 0;
0123 }
0124 module_init(test_uuid_init);
0125 
0126 static void __exit test_uuid_exit(void)
0127 {
0128     /* do nothing */
0129 }
0130 module_exit(test_uuid_exit);
0131 
0132 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
0133 MODULE_LICENSE("Dual BSD/GPL");