test_event.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@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. /*!
  19. * \file
  20. * \brief Tests for the ast_event API
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. *
  24. * \ingroup tests
  25. *
  26. * \todo API Calls not yet touched by a test: XXX TODO
  27. * - ast_event_get_ie_type_name()
  28. * - ast_event_get_ie_pltype()
  29. * - ast_event_iterator_init()
  30. * - ast_event_iterator_next()
  31. * - ast_event_iterator_get_ie_type()
  32. * - ast_event_iterator_get_ie_uint()
  33. * - ast_event_iterator_get_ie_str()
  34. */
  35. /*** MODULEINFO
  36. <depend>TEST_FRAMEWORK</depend>
  37. <support_level>core</support_level>
  38. ***/
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include "asterisk/module.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/test.h"
  44. #include "asterisk/event.h"
  45. static int check_event(struct ast_event *event, struct ast_test *test,
  46. enum ast_event_type expected_type, const char *type_name,
  47. const char *str, uint32_t uint)
  48. {
  49. enum ast_event_type type;
  50. const void *foo;
  51. /* Check #1: Ensure event type is set properly. */
  52. type = ast_event_get_type(event);
  53. if (ast_event_get_type(event) != type) {
  54. ast_test_status_update(test, "Expected event type: '%u', got '%u'\n",
  55. expected_type, type);
  56. return -1;
  57. }
  58. /* Check #4: Check for the string IE */
  59. if (strcmp(str, ast_event_get_ie_str(event, AST_EVENT_IE_CEL_USEREVENT_NAME))) {
  60. ast_test_status_update(test, "Failed to get string IE.\n");
  61. return -1;
  62. }
  63. /* Check #5: Check for the uint IE */
  64. if (uint != ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_AMAFLAGS)) {
  65. ast_test_status_update(test, "Failed to get uint IE.\n");
  66. return -1;
  67. }
  68. /* Check #6: Check if a check for a str IE that isn't there works */
  69. if ((foo = ast_event_get_ie_str(event, AST_EVENT_IE_CEL_CIDNAME))) {
  70. ast_test_status_update(test, "CEL_CIDNAME IE check returned non-NULL %p\n", foo);
  71. return -1;
  72. }
  73. /* Check #7: Check if a check for a uint IE that isn't there returns 0 */
  74. if (ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TIME_USEC)) {
  75. ast_test_status_update(test, "UNIQUEID IE should be 0\n");
  76. return -1;
  77. }
  78. ast_test_status_update(test, "Event looks good.\n");
  79. return 0;
  80. }
  81. /*!
  82. * \internal
  83. */
  84. AST_TEST_DEFINE(event_new_test)
  85. {
  86. enum ast_test_result_state res = AST_TEST_PASS;
  87. struct ast_event *event = NULL, *event2 = NULL;
  88. static const enum ast_event_type type = AST_EVENT_CUSTOM;
  89. static const char str[] = "SIP/alligatormittens";
  90. static const uint32_t uint = 0xb00bface;
  91. switch (cmd) {
  92. case TEST_INIT:
  93. info->name = "ast_event_new_test";
  94. info->category = "/main/event/";
  95. info->summary = "Test event creation";
  96. info->description =
  97. "This test exercises the API calls that allow allocation "
  98. "of an ast_event.";
  99. return AST_TEST_NOT_RUN;
  100. case TEST_EXECUTE:
  101. break;
  102. }
  103. /*
  104. * Test 2 methods of event creation:
  105. *
  106. * 1) Dynamic via appending each IE individually.
  107. * 2) Statically, with all IEs in ast_event_new().
  108. */
  109. ast_test_status_update(test, "First, test dynamic event creation...\n");
  110. if (!(event = ast_event_new(type, AST_EVENT_IE_END))) {
  111. ast_test_status_update(test, "Failed to allocate ast_event object.\n");
  112. res = AST_TEST_FAIL;
  113. goto return_cleanup;
  114. }
  115. if (ast_event_append_ie_str(&event, AST_EVENT_IE_CEL_USEREVENT_NAME, str)) {
  116. ast_test_status_update(test, "Failed to append str IE\n");
  117. res = AST_TEST_FAIL;
  118. goto return_cleanup;
  119. }
  120. if (ast_event_append_ie_uint(&event, AST_EVENT_IE_CEL_AMAFLAGS, uint)) {
  121. ast_test_status_update(test, "Failed to append uint IE\n");
  122. res = AST_TEST_FAIL;
  123. goto return_cleanup;
  124. }
  125. if (check_event(event, test, type, "Custom", str, uint)) {
  126. ast_test_status_update(test, "Dynamically generated event broken\n");
  127. res = AST_TEST_FAIL;
  128. goto return_cleanup;
  129. }
  130. event2 = ast_event_new(type,
  131. AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, str,
  132. AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, uint,
  133. AST_EVENT_IE_END);
  134. if (!event2) {
  135. ast_test_status_update(test, "Failed to allocate ast_event object.\n");
  136. res = AST_TEST_FAIL;
  137. goto return_cleanup;
  138. }
  139. if (check_event(event2, test, type, "Custom", str, uint)) {
  140. ast_test_status_update(test, "Statically generated event broken\n");
  141. res = AST_TEST_FAIL;
  142. goto return_cleanup;
  143. }
  144. if (ast_event_get_size(event) != ast_event_get_size(event2)) {
  145. ast_test_status_update(test, "Events expected to be identical have different size: %d != %d\n",
  146. (int) ast_event_get_size(event),
  147. (int) ast_event_get_size(event2));
  148. res = AST_TEST_FAIL;
  149. goto return_cleanup;
  150. }
  151. return_cleanup:
  152. if (event) {
  153. ast_event_destroy(event);
  154. event = NULL;
  155. }
  156. if (event2) {
  157. ast_event_destroy(event2);
  158. event2 = NULL;
  159. }
  160. return res;
  161. }
  162. struct event_sub_data {
  163. unsigned int count;
  164. };
  165. static int unload_module(void)
  166. {
  167. AST_TEST_UNREGISTER(event_new_test);
  168. return 0;
  169. }
  170. static int load_module(void)
  171. {
  172. AST_TEST_REGISTER(event_new_test);
  173. return AST_MODULE_LOAD_SUCCESS;
  174. }
  175. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ast_event API Tests");