test_uuid.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Test cases for lib/uuid.c module.
  3. */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/string.h>
  9. #include <linux/uuid.h>
  10. struct test_uuid_data {
  11. const char *uuid;
  12. guid_t le;
  13. uuid_t be;
  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. static const char * const test_uuid_wrong_data[] = {
  33. "c33f4995-3701-450e-9fbf206a2e98e576 ", /* no hyphen(s) */
  34. "64b4371c-77c1-48f9-8221-29f054XX023b", /* invalid character(s) */
  35. "0cb4ddff-a545-4401-9d06-688af53e", /* not enough data */
  36. };
  37. static unsigned total_tests __initdata;
  38. static unsigned failed_tests __initdata;
  39. static void __init test_uuid_failed(const char *prefix, bool wrong, bool be,
  40. const char *data, const char *actual)
  41. {
  42. pr_err("%s test #%u %s %s data: '%s'\n",
  43. prefix,
  44. total_tests,
  45. wrong ? "passed on wrong" : "failed on",
  46. be ? "BE" : "LE",
  47. data);
  48. if (actual && *actual)
  49. pr_err("%s test #%u actual data: '%s'\n",
  50. prefix,
  51. total_tests,
  52. actual);
  53. failed_tests++;
  54. }
  55. static void __init test_uuid_test(const struct test_uuid_data *data)
  56. {
  57. guid_t le;
  58. uuid_t be;
  59. char buf[48];
  60. /* LE */
  61. total_tests++;
  62. if (guid_parse(data->uuid, &le))
  63. test_uuid_failed("conversion", false, false, data->uuid, NULL);
  64. total_tests++;
  65. if (!guid_equal(&data->le, &le)) {
  66. sprintf(buf, "%pUl", &le);
  67. test_uuid_failed("cmp", false, false, data->uuid, buf);
  68. }
  69. /* BE */
  70. total_tests++;
  71. if (uuid_parse(data->uuid, &be))
  72. test_uuid_failed("conversion", false, true, data->uuid, NULL);
  73. total_tests++;
  74. if (!uuid_equal(&data->be, &be)) {
  75. sprintf(buf, "%pUb", &be);
  76. test_uuid_failed("cmp", false, true, data->uuid, buf);
  77. }
  78. }
  79. static void __init test_uuid_wrong(const char *data)
  80. {
  81. guid_t le;
  82. uuid_t be;
  83. /* LE */
  84. total_tests++;
  85. if (!guid_parse(data, &le))
  86. test_uuid_failed("negative", true, false, data, NULL);
  87. /* BE */
  88. total_tests++;
  89. if (!uuid_parse(data, &be))
  90. test_uuid_failed("negative", true, true, data, NULL);
  91. }
  92. static int __init test_uuid_init(void)
  93. {
  94. unsigned int i;
  95. for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++)
  96. test_uuid_test(&test_uuid_test_data[i]);
  97. for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++)
  98. test_uuid_wrong(test_uuid_wrong_data[i]);
  99. if (failed_tests == 0)
  100. pr_info("all %u tests passed\n", total_tests);
  101. else
  102. pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
  103. return failed_tests ? -EINVAL : 0;
  104. }
  105. module_init(test_uuid_init);
  106. static void __exit test_uuid_exit(void)
  107. {
  108. /* do nothing */
  109. }
  110. module_exit(test_uuid_exit);
  111. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  112. MODULE_LICENSE("Dual BSD/GPL");