test_format_cache.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Format Cache API Unit Tests
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/test.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/codec.h"
  34. #include "asterisk/format.h"
  35. #include "asterisk/format_cache.h"
  36. AST_TEST_DEFINE(format_cache_set)
  37. {
  38. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  39. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  40. switch (cmd) {
  41. case TEST_INIT:
  42. info->name = "format_cache_set";
  43. info->category = "/main/format_cache/";
  44. info->summary = "format cache add unit test";
  45. info->description =
  46. "Test that adding of a cached format succeeds";
  47. return AST_TEST_NOT_RUN;
  48. case TEST_EXECUTE:
  49. break;
  50. }
  51. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  52. if (!codec) {
  53. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  54. return AST_TEST_FAIL;
  55. }
  56. format = ast_format_create_named("ulaw@20_1", codec);
  57. if (!format) {
  58. ast_test_status_update(test, "Could not create format using built-in codec\n");
  59. return AST_TEST_FAIL;
  60. }
  61. if (ast_format_cache_set(format)) {
  62. ast_test_status_update(test, "Could not add just created format to cache\n");
  63. return AST_TEST_FAIL;
  64. }
  65. return AST_TEST_PASS;
  66. }
  67. AST_TEST_DEFINE(format_cache_set_duplicate)
  68. {
  69. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  70. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  71. switch (cmd) {
  72. case TEST_INIT:
  73. info->name = "format_cache_set_duplicate";
  74. info->category = "/main/format_cache/";
  75. info->summary = "format cache add unit test";
  76. info->description =
  77. "Test that adding of a cached format multiple times succeeds";
  78. return AST_TEST_NOT_RUN;
  79. case TEST_EXECUTE:
  80. break;
  81. }
  82. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  83. if (!codec) {
  84. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  85. return AST_TEST_FAIL;
  86. }
  87. format = ast_format_create_named("ulaw@20_2", codec);
  88. if (!format) {
  89. ast_test_status_update(test, "Could not create format using built-in codec\n");
  90. return AST_TEST_FAIL;
  91. }
  92. if (ast_format_cache_set(format)) {
  93. ast_test_status_update(test, "Could not add just created format to cache\n");
  94. return AST_TEST_FAIL;
  95. }
  96. if (ast_format_cache_set(format)) {
  97. ast_test_status_update(test, "Failed to update cached format\n");
  98. return AST_TEST_FAIL;
  99. }
  100. return AST_TEST_PASS;
  101. }
  102. AST_TEST_DEFINE(format_cache_set_null)
  103. {
  104. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  105. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  106. switch (cmd) {
  107. case TEST_INIT:
  108. info->name = "format_cache_set_null";
  109. info->category = "/main/format_cache/";
  110. info->summary = "format cache add unit test";
  111. info->description =
  112. "Test that adding a NULL or empty format to the cache does not succeed";
  113. return AST_TEST_NOT_RUN;
  114. case TEST_EXECUTE:
  115. break;
  116. }
  117. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  118. if (!codec) {
  119. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  120. return AST_TEST_FAIL;
  121. }
  122. format = ast_format_create_named("", codec);
  123. if (!format) {
  124. ast_test_status_update(test, "Could not create format using built-in codec\n");
  125. return AST_TEST_FAIL;
  126. }
  127. if (!ast_format_cache_set(format)) {
  128. ast_test_status_update(test, "Successfully cached a format with an empty name\n");
  129. return AST_TEST_FAIL;
  130. }
  131. return AST_TEST_PASS;
  132. }
  133. AST_TEST_DEFINE(format_cache_get)
  134. {
  135. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  136. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  137. RAII_VAR(struct ast_format *, cached, NULL, ao2_cleanup);
  138. switch (cmd) {
  139. case TEST_INIT:
  140. info->name = "format_cache_get";
  141. info->category = "/main/format_cache/";
  142. info->summary = "format cache get unit test";
  143. info->description =
  144. "Test that getting of a cached format succeeds";
  145. return AST_TEST_NOT_RUN;
  146. case TEST_EXECUTE:
  147. break;
  148. }
  149. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  150. if (!codec) {
  151. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  152. return AST_TEST_FAIL;
  153. }
  154. format = ast_format_create_named("ulaw@20", codec);
  155. if (!format) {
  156. ast_test_status_update(test, "Could not create format using built-in codec\n");
  157. return AST_TEST_FAIL;
  158. }
  159. if (ast_format_cache_set(format)) {
  160. ast_test_status_update(test, "Could not add just created format to cache\n");
  161. return AST_TEST_FAIL;
  162. }
  163. cached = ast_format_cache_get("ulaw@20");
  164. if (!cached) {
  165. ast_test_status_update(test, "Failed to retrieve a format we just cached\n");
  166. return AST_TEST_FAIL;
  167. } else if (cached != format) {
  168. ast_test_status_update(test, "Returned cached format does not match format we just added\n");
  169. return AST_TEST_FAIL;
  170. }
  171. return AST_TEST_PASS;
  172. }
  173. AST_TEST_DEFINE(format_cache_get_nonexistent)
  174. {
  175. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  176. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  177. RAII_VAR(struct ast_format *, cached, NULL, ao2_cleanup);
  178. switch (cmd) {
  179. case TEST_INIT:
  180. info->name = "format_cache_get_nonxistent";
  181. info->category = "/main/format_cache/";
  182. info->summary = "format cache get unit test";
  183. info->description =
  184. "Test that getting of a non-existent cached format does not succeed";
  185. return AST_TEST_NOT_RUN;
  186. case TEST_EXECUTE:
  187. break;
  188. }
  189. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  190. if (!codec) {
  191. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  192. return AST_TEST_FAIL;
  193. }
  194. format = ast_format_create_named("ulaw@40", codec);
  195. if (!format) {
  196. ast_test_status_update(test, "Could not create format using built-in codec\n");
  197. return AST_TEST_FAIL;
  198. }
  199. if (ast_format_cache_set(format)) {
  200. ast_test_status_update(test, "Could not add just created format to cache\n");
  201. return AST_TEST_FAIL;
  202. }
  203. cached = ast_format_cache_get("ulaw@60");
  204. if (cached) {
  205. ast_test_status_update(test, "Retrieved a cached format when one should not have existed\n");
  206. return AST_TEST_FAIL;
  207. }
  208. cached = ast_format_cache_get("");
  209. if (cached) {
  210. ast_test_status_update(test, "Retrieved a cached format when we provided an empty name\n");
  211. return AST_TEST_FAIL;
  212. }
  213. cached = ast_format_cache_get(NULL);
  214. if (cached) {
  215. ast_test_status_update(test, "Retrieved a cached format when we provided a NULL name\n");
  216. return AST_TEST_FAIL;
  217. }
  218. return AST_TEST_PASS;
  219. }
  220. static int unload_module(void)
  221. {
  222. AST_TEST_UNREGISTER(format_cache_set);
  223. AST_TEST_UNREGISTER(format_cache_set_duplicate);
  224. AST_TEST_UNREGISTER(format_cache_set_null);
  225. AST_TEST_UNREGISTER(format_cache_get);
  226. AST_TEST_UNREGISTER(format_cache_get_nonexistent);
  227. return 0;
  228. }
  229. static int load_module(void)
  230. {
  231. AST_TEST_REGISTER(format_cache_set);
  232. AST_TEST_REGISTER(format_cache_set_duplicate);
  233. AST_TEST_REGISTER(format_cache_set_null);
  234. AST_TEST_REGISTER(format_cache_get);
  235. AST_TEST_REGISTER(format_cache_get_nonexistent);
  236. return AST_MODULE_LOAD_SUCCESS;
  237. }
  238. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Format cache API test module");