2
0

test-uuid.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * QEMU UUID Library
  3. *
  4. * Copyright (c) 2016 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/uuid.h"
  22. struct {
  23. const char *uuidstr;
  24. QemuUUID uuid;
  25. bool uuidstr_is_valid;
  26. bool check_unparse;
  27. } uuid_test_data[] = {
  28. { /* Normal */
  29. "586ece27-7f09-41e0-9e74-e901317e9d42",
  30. { { {
  31. 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
  32. 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42,
  33. } } },
  34. true, true,
  35. }, { /* NULL */
  36. "00000000-0000-0000-0000-000000000000",
  37. { },
  38. true, true,
  39. }, { /* Upper case */
  40. "0CC6C752-3961-4028-A286-C05CC616D396",
  41. { { {
  42. 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
  43. 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
  44. } } },
  45. true, false,
  46. }, { /* Mixed case */
  47. "0CC6C752-3961-4028-a286-c05cc616D396",
  48. { { {
  49. 0x0c, 0xc6, 0xc7, 0x52, 0x39, 0x61, 0x40, 0x28,
  50. 0xa2, 0x86, 0xc0, 0x5c, 0xc6, 0x16, 0xd3, 0x96,
  51. } } },
  52. true, false,
  53. }, { /* Empty */
  54. ""
  55. }, { /* Too short */
  56. "abc",
  57. }, { /* Non-hex */
  58. "abcdefgh-0000-0000-0000-000000000000",
  59. }, { /* No '-' */
  60. "0cc6c75239614028a286c05cc616d396",
  61. }, { /* '-' in wrong position */
  62. "0cc6c-7523961-4028-a286-c05cc616d396",
  63. }, { /* Double '-' */
  64. "0cc6c752--3961-4028-a286-c05cc616d396",
  65. }, { /* Too long */
  66. "0000000000000000000000000000000000000000000000",
  67. }, { /* Invalid char in the beginning */
  68. ")cc6c752-3961-4028-a286-c05cc616d396",
  69. }, { /* Invalid char in the beginning, in extra */
  70. ")0cc6c752-3961-4028-a286-c05cc616d396",
  71. }, { /* Invalid char in the middle */
  72. "0cc6c752-39*1-4028-a286-c05cc616d396",
  73. }, { /* Invalid char in the middle, in extra */
  74. "0cc6c752-39*61-4028-a286-c05cc616d396",
  75. }, { /* Invalid char in the end */
  76. "0cc6c752-3961-4028-a286-c05cc616d39&",
  77. }, { /* Invalid char in the end, in extra */
  78. "0cc6c752-3961-4028-a286-c05cc616d396&",
  79. }, { /* Short end and trailing space */
  80. "0cc6c752-3961-4028-a286-c05cc616d39 ",
  81. }, { /* Leading space and short end */
  82. " 0cc6c752-3961-4028-a286-c05cc616d39",
  83. },
  84. };
  85. static inline bool uuid_is_valid(QemuUUID *uuid)
  86. {
  87. return qemu_uuid_is_null(uuid) ||
  88. ((uuid->data[6] & 0xf0) == 0x40 && (uuid->data[8] & 0xc0) == 0x80);
  89. }
  90. static void test_uuid_generate(void)
  91. {
  92. QemuUUID uuid_not_null = { { {
  93. 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
  94. 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
  95. } } };
  96. QemuUUID uuid;
  97. int i;
  98. for (i = 0; i < 100; ++i) {
  99. qemu_uuid_generate(&uuid);
  100. g_assert(uuid_is_valid(&uuid));
  101. g_assert_false(qemu_uuid_is_null(&uuid));
  102. g_assert_false(qemu_uuid_is_equal(&uuid_not_null, &uuid));
  103. }
  104. }
  105. static void test_uuid_is_null(void)
  106. {
  107. QemuUUID uuid_null = { };
  108. QemuUUID uuid_not_null = { { {
  109. 0x58, 0x6e, 0xce, 0x27, 0x7f, 0x09, 0x41, 0xe0,
  110. 0x9e, 0x74, 0xe9, 0x01, 0x31, 0x7e, 0x9d, 0x42
  111. } } };
  112. QemuUUID uuid_not_null_2 = { { { 1 } } };
  113. g_assert(qemu_uuid_is_null(&uuid_null));
  114. g_assert_false(qemu_uuid_is_null(&uuid_not_null));
  115. g_assert_false(qemu_uuid_is_null(&uuid_not_null_2));
  116. }
  117. static void test_uuid_parse(void)
  118. {
  119. int i, r;
  120. for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
  121. QemuUUID uuid;
  122. bool is_valid = uuid_test_data[i].uuidstr_is_valid;
  123. r = qemu_uuid_parse(uuid_test_data[i].uuidstr, &uuid);
  124. g_assert_cmpint(!r, ==, is_valid);
  125. if (is_valid) {
  126. g_assert_cmpint(is_valid, ==, uuid_is_valid(&uuid));
  127. g_assert_cmpmem(&uuid_test_data[i].uuid, sizeof(uuid),
  128. &uuid, sizeof(uuid));
  129. }
  130. }
  131. }
  132. static void test_uuid_unparse(void)
  133. {
  134. int i;
  135. for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
  136. char out[37];
  137. if (!uuid_test_data[i].check_unparse) {
  138. continue;
  139. }
  140. qemu_uuid_unparse(&uuid_test_data[i].uuid, out);
  141. g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
  142. }
  143. }
  144. static void test_uuid_unparse_strdup(void)
  145. {
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(uuid_test_data); i++) {
  148. char *out;
  149. if (!uuid_test_data[i].check_unparse) {
  150. continue;
  151. }
  152. out = qemu_uuid_unparse_strdup(&uuid_test_data[i].uuid);
  153. g_assert_cmpstr(uuid_test_data[i].uuidstr, ==, out);
  154. g_free(out);
  155. }
  156. }
  157. int main(int argc, char **argv)
  158. {
  159. g_test_init(&argc, &argv, NULL);
  160. g_test_add_func("/uuid/is_null", test_uuid_is_null);
  161. g_test_add_func("/uuid/generate", test_uuid_generate);
  162. g_test_add_func("/uuid/parse", test_uuid_parse);
  163. g_test_add_func("/uuid/unparse", test_uuid_unparse);
  164. g_test_add_func("/uuid/unparse_strdup", test_uuid_unparse_strdup);
  165. return g_test_run();
  166. }