uuid_kunit.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*
  3. * Test cases for lib/uuid.c module.
  4. */
  5. #include <kunit/test.h>
  6. #include <linux/uuid.h>
  7. struct test_uuid_data {
  8. const char *uuid;
  9. guid_t le;
  10. uuid_t be;
  11. };
  12. static const struct test_uuid_data test_uuid_test_data[] = {
  13. {
  14. .uuid = "c33f4995-3701-450e-9fbf-206a2e98e576",
  15. .le = GUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
  16. .be = UUID_INIT(0xc33f4995, 0x3701, 0x450e, 0x9f, 0xbf, 0x20, 0x6a, 0x2e, 0x98, 0xe5, 0x76),
  17. },
  18. {
  19. .uuid = "64b4371c-77c1-48f9-8221-29f054fc023b",
  20. .le = GUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
  21. .be = UUID_INIT(0x64b4371c, 0x77c1, 0x48f9, 0x82, 0x21, 0x29, 0xf0, 0x54, 0xfc, 0x02, 0x3b),
  22. },
  23. {
  24. .uuid = "0cb4ddff-a545-4401-9d06-688af53e7f84",
  25. .le = GUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
  26. .be = UUID_INIT(0x0cb4ddff, 0xa545, 0x4401, 0x9d, 0x06, 0x68, 0x8a, 0xf5, 0x3e, 0x7f, 0x84),
  27. },
  28. };
  29. static const char * const test_uuid_wrong_data[] = {
  30. "c33f4995-3701-450e-9fbf206a2e98e576 ", /* no hyphen(s) */
  31. "64b4371c-77c1-48f9-8221-29f054XX023b", /* invalid character(s) */
  32. "0cb4ddff-a545-4401-9d06-688af53e", /* not enough data */
  33. };
  34. static void uuid_test_guid_valid(struct kunit *test)
  35. {
  36. unsigned int i;
  37. const struct test_uuid_data *data;
  38. guid_t le;
  39. for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++) {
  40. data = &test_uuid_test_data[i];
  41. KUNIT_EXPECT_EQ(test, guid_parse(data->uuid, &le), 0);
  42. KUNIT_EXPECT_TRUE(test, guid_equal(&data->le, &le));
  43. }
  44. }
  45. static void uuid_test_uuid_valid(struct kunit *test)
  46. {
  47. unsigned int i;
  48. const struct test_uuid_data *data;
  49. uuid_t be;
  50. for (i = 0; i < ARRAY_SIZE(test_uuid_test_data); i++) {
  51. data = &test_uuid_test_data[i];
  52. KUNIT_EXPECT_EQ(test, uuid_parse(data->uuid, &be), 0);
  53. KUNIT_EXPECT_TRUE(test, uuid_equal(&data->be, &be));
  54. }
  55. }
  56. static void uuid_test_guid_invalid(struct kunit *test)
  57. {
  58. unsigned int i;
  59. const char *uuid;
  60. guid_t le;
  61. for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++) {
  62. uuid = test_uuid_wrong_data[i];
  63. KUNIT_EXPECT_EQ(test, guid_parse(uuid, &le), -EINVAL);
  64. }
  65. }
  66. static void uuid_test_uuid_invalid(struct kunit *test)
  67. {
  68. unsigned int i;
  69. const char *uuid;
  70. uuid_t be;
  71. for (i = 0; i < ARRAY_SIZE(test_uuid_wrong_data); i++) {
  72. uuid = test_uuid_wrong_data[i];
  73. KUNIT_EXPECT_EQ(test, uuid_parse(uuid, &be), -EINVAL);
  74. }
  75. }
  76. static struct kunit_case uuid_test_cases[] = {
  77. KUNIT_CASE(uuid_test_guid_valid),
  78. KUNIT_CASE(uuid_test_uuid_valid),
  79. KUNIT_CASE(uuid_test_guid_invalid),
  80. KUNIT_CASE(uuid_test_uuid_invalid),
  81. {},
  82. };
  83. static struct kunit_suite uuid_test_suite = {
  84. .name = "uuid",
  85. .test_cases = uuid_test_cases,
  86. };
  87. kunit_test_suite(uuid_test_suite);
  88. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  89. MODULE_DESCRIPTION("Test cases for lib/uuid.c module");
  90. MODULE_LICENSE("Dual BSD/GPL");