test_optional_api.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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 \brief Test optional API.
  20. *
  21. * This tests exercise the underlying implementation functions. Acutal usage
  22. * won't look anything like this; it would use the wrapper macros.
  23. *
  24. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  25. *
  26. * \ingroup tests
  27. */
  28. /*** MODULEINFO
  29. <depend>TEST_FRAMEWORK</depend>
  30. <depend>OPTIONAL_API</depend>
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include "asterisk/module.h"
  36. #include "asterisk/optional_api.h"
  37. #include "asterisk/test.h"
  38. #define CATEGORY "/main/optional_api/"
  39. enum was_called {
  40. NONE,
  41. STUB,
  42. IMPL
  43. };
  44. enum was_called was_called_result;
  45. ast_optional_fn test_optional_ref;
  46. static void test_optional_stub(void)
  47. {
  48. was_called_result = STUB;
  49. }
  50. static void test_optional_impl(void)
  51. {
  52. was_called_result = IMPL;
  53. }
  54. static void test_optional(void)
  55. {
  56. was_called_result = NONE;
  57. if (test_optional_ref) {
  58. test_optional_ref();
  59. }
  60. }
  61. #define SYMNAME "test_option"
  62. AST_TEST_DEFINE(test_provide_first)
  63. {
  64. enum ast_test_result_state res;
  65. switch (cmd) {
  66. case TEST_INIT:
  67. info->name = __func__;
  68. info->category = CATEGORY;
  69. info->summary = "Test optional API publishing.";
  70. info->description = "Test optional API publishing.";
  71. return AST_TEST_NOT_RUN;
  72. case TEST_EXECUTE:
  73. break;
  74. }
  75. res = AST_TEST_FAIL;
  76. test_optional_ref = 0;
  77. ast_optional_api_provide(SYMNAME, test_optional_impl);
  78. ast_optional_api_use(SYMNAME, &test_optional_ref, test_optional_stub,
  79. AST_MODULE);
  80. test_optional();
  81. if (was_called_result != IMPL) {
  82. ast_test_status_update(test, "Expected %d, was %u",
  83. IMPL, was_called_result);
  84. goto done;
  85. }
  86. res = AST_TEST_PASS;
  87. done:
  88. ast_optional_api_unuse(SYMNAME, &test_optional_ref, AST_MODULE);
  89. ast_optional_api_unprovide(SYMNAME, test_optional_impl);
  90. return res;
  91. }
  92. AST_TEST_DEFINE(test_provide_last)
  93. {
  94. enum ast_test_result_state res;
  95. switch (cmd) {
  96. case TEST_INIT:
  97. info->name = __func__;
  98. info->category = CATEGORY;
  99. info->summary = "Test optional API publishing.";
  100. info->description = "Test optional API publishing.";
  101. return AST_TEST_NOT_RUN;
  102. case TEST_EXECUTE:
  103. break;
  104. }
  105. res = AST_TEST_FAIL;
  106. test_optional_ref = 0;
  107. ast_optional_api_use(SYMNAME, &test_optional_ref, test_optional_stub,
  108. AST_MODULE);
  109. test_optional();
  110. if (was_called_result != STUB) {
  111. ast_test_status_update(test, "Expected %d, was %u",
  112. STUB, was_called_result);
  113. goto done;
  114. }
  115. ast_optional_api_provide(SYMNAME, test_optional_impl);
  116. test_optional();
  117. if (was_called_result != IMPL) {
  118. ast_test_status_update(test, "Expected %d, was %u",
  119. IMPL, was_called_result);
  120. ast_optional_api_unprovide(SYMNAME, test_optional_impl);
  121. goto done;
  122. }
  123. ast_optional_api_unprovide(SYMNAME, test_optional_impl);
  124. test_optional();
  125. if (was_called_result != STUB) {
  126. ast_test_status_update(test, "Expected %d, was %u",
  127. STUB, was_called_result);
  128. ast_optional_api_unprovide(SYMNAME, test_optional_impl);
  129. goto done;
  130. }
  131. res = AST_TEST_PASS;
  132. done:
  133. ast_optional_api_unuse(SYMNAME, &test_optional_ref, AST_MODULE);
  134. return res;
  135. }
  136. static int unload_module(void)
  137. {
  138. AST_TEST_UNREGISTER(test_provide_first);
  139. AST_TEST_UNREGISTER(test_provide_last);
  140. return 0;
  141. }
  142. static int load_module(void)
  143. {
  144. AST_TEST_REGISTER(test_provide_first);
  145. AST_TEST_REGISTER(test_provide_last);
  146. return AST_MODULE_LOAD_SUCCESS;
  147. }
  148. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ARI testing",
  149. .support_level = AST_MODULE_SUPPORT_CORE,
  150. .load = load_module,
  151. .unload = unload_module,
  152. );