test_uuid.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Mark Michelson <mmmichelson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Universally unique identifier tests
  20. */
  21. /*** MODULEINFO
  22. <depend>TEST_FRAMEWORK</depend>
  23. <support_level>core</support_level>
  24. ***/
  25. #include "asterisk.h"
  26. #include "asterisk/test.h"
  27. #include "asterisk/uuid.h"
  28. #include "asterisk/module.h"
  29. AST_TEST_DEFINE(uuid)
  30. {
  31. struct ast_uuid *uuid1 = NULL;
  32. struct ast_uuid *uuid2 = NULL;
  33. struct ast_uuid *uuid3 = NULL;
  34. char uuid_str[AST_UUID_STR_LEN];
  35. enum ast_test_result_state res = AST_TEST_FAIL;
  36. switch (cmd) {
  37. case TEST_INIT:
  38. info->name = "uuid";
  39. info->category = "/main/uuid/";
  40. info->summary = "UUID unit test";
  41. info->description =
  42. "This tests basic UUID operations to ensure they work properly";
  43. return AST_TEST_NOT_RUN;
  44. case TEST_EXECUTE:
  45. break;
  46. }
  47. /* Use method of generating UUID directly as a string. */
  48. ast_uuid_generate_str(uuid_str, sizeof(uuid_str));
  49. if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
  50. ast_test_status_update(test, "Failed to directly generate UUID string\n");
  51. goto end;
  52. }
  53. ast_test_status_update(test, "Generate UUID direct to string, got %s\n", uuid_str);
  54. /* Now convert the direct UUID string to a UUID */
  55. uuid1 = ast_str_to_uuid(uuid_str);
  56. if (!uuid1) {
  57. ast_test_status_update(test, "Unable to convert direct UUID string %s to UUID\n", uuid_str);
  58. goto end;
  59. }
  60. ast_free(uuid1);
  61. /* Make sure that we can generate a UUID */
  62. uuid1 = ast_uuid_generate();
  63. if (!uuid1) {
  64. ast_test_status_update(test, "Unable to generate a UUID\n");
  65. goto end;
  66. }
  67. /* Make sure we're not generating nil UUIDs */
  68. if (ast_uuid_is_nil(uuid1)) {
  69. ast_test_status_update(test, "We generated a nil UUID. Something is wrong\n");
  70. goto end;
  71. }
  72. /* Convert it to a string */
  73. ast_uuid_to_str(uuid1, uuid_str, sizeof(uuid_str));
  74. if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
  75. ast_test_status_update(test, "Failed to convert the UUID to a string\n");
  76. goto end;
  77. }
  78. ast_test_status_update(test, "Second generated UUID converted to string, got %s\n", uuid_str);
  79. /* Now convert the string back to a UUID */
  80. uuid2 = ast_str_to_uuid(uuid_str);
  81. if (!uuid2) {
  82. ast_test_status_update(test, "Unable to convert string %s to UUID\n", uuid_str);
  83. goto end;
  84. }
  85. /* Make sure the UUIDs are identical */
  86. if (ast_uuid_compare(uuid1, uuid2) != 0) {
  87. ast_test_status_update(test, "UUIDs that should be identical are different\n");
  88. goto end;
  89. }
  90. /* Try copying a UUID */
  91. uuid3 = ast_uuid_copy(uuid1);
  92. if (!uuid3) {
  93. ast_test_status_update(test, "Unable to copy UUID\n");
  94. goto end;
  95. }
  96. /* Make sure copied UUIDs are identical */
  97. if (ast_uuid_compare(uuid1, uuid3) != 0) {
  98. ast_test_status_update(test, "UUIDs that should be identical are different\n");
  99. goto end;
  100. }
  101. if (ast_uuid_compare(uuid2, uuid3) != 0) {
  102. ast_test_status_update(test, "UUIDs that should be identical are different\n");
  103. goto end;
  104. }
  105. /* Clear a UUID and ensure that it registers as nil */
  106. ast_uuid_clear(uuid1);
  107. if (!ast_uuid_is_nil(uuid1)) {
  108. ast_test_status_update(test, "UUID that was cleared does not appear to be nil\n");
  109. goto end;
  110. }
  111. res = AST_TEST_PASS;
  112. end:
  113. ast_free(uuid1);
  114. ast_free(uuid2);
  115. ast_free(uuid3);
  116. return res;
  117. }
  118. static int unload_module(void)
  119. {
  120. AST_TEST_UNREGISTER(uuid);
  121. return 0;
  122. }
  123. static int load_module(void)
  124. {
  125. AST_TEST_REGISTER(uuid);
  126. return AST_MODULE_LOAD_SUCCESS;
  127. }
  128. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "UUID test module");