test_core_codec.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 Core Codec 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. static struct ast_codec known_unknown = {
  35. .name = "unit_test",
  36. .description = "Unit test codec",
  37. .type = AST_MEDIA_TYPE_AUDIO,
  38. .sample_rate = 8000,
  39. .minimum_ms = 10,
  40. .maximum_ms = 150,
  41. .default_ms = 20,
  42. };
  43. static struct ast_codec doubly = {
  44. .name = "unit_test_double",
  45. .description = "Unit test codec",
  46. .type = AST_MEDIA_TYPE_AUDIO,
  47. .sample_rate = 8000,
  48. .minimum_ms = 10,
  49. .maximum_ms = 150,
  50. .default_ms = 20,
  51. };
  52. static struct ast_codec unknown = {
  53. .name = "unit_test_unknown",
  54. .description = "Unit test codec",
  55. .type = AST_MEDIA_TYPE_UNKNOWN,
  56. .sample_rate = 8000,
  57. .minimum_ms = 10,
  58. .maximum_ms = 150,
  59. .default_ms = 20,
  60. };
  61. static struct ast_codec audio_without_rate = {
  62. .name = "unit_test_audio_without_rate",
  63. .description = "Unit test codec",
  64. .type = AST_MEDIA_TYPE_AUDIO,
  65. .minimum_ms = 10,
  66. .maximum_ms = 150,
  67. .default_ms = 20,
  68. };
  69. static struct ast_codec audio_get = {
  70. .name = "unit_test_audio_get",
  71. .description = "Unit test codec",
  72. .type = AST_MEDIA_TYPE_AUDIO,
  73. .sample_rate = 8000,
  74. .minimum_ms = 10,
  75. .maximum_ms = 150,
  76. .default_ms = 20,
  77. };
  78. static struct ast_codec audio_get_unknown = {
  79. .name = "unit_test_audio_get_unknown",
  80. .description = "Unit test codec",
  81. .type = AST_MEDIA_TYPE_AUDIO,
  82. .sample_rate = 8000,
  83. .minimum_ms = 10,
  84. .maximum_ms = 150,
  85. .default_ms = 20,
  86. };
  87. static struct ast_codec audio_get_id = {
  88. .name = "unit_test_audio_get_id",
  89. .description = "Unit test codec",
  90. .type = AST_MEDIA_TYPE_AUDIO,
  91. .sample_rate = 8000,
  92. .minimum_ms = 10,
  93. .maximum_ms = 150,
  94. .default_ms = 20,
  95. };
  96. AST_TEST_DEFINE(codec_register)
  97. {
  98. switch (cmd) {
  99. case TEST_INIT:
  100. info->name = "codec_register";
  101. info->category = "/main/core_codec/";
  102. info->summary = "codec registration unit test";
  103. info->description =
  104. "Test registration of a core codec that is known to be unknown";
  105. return AST_TEST_NOT_RUN;
  106. case TEST_EXECUTE:
  107. break;
  108. }
  109. if (ast_codec_register(&known_unknown)) {
  110. ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
  111. return AST_TEST_FAIL;
  112. }
  113. return AST_TEST_PASS;
  114. }
  115. AST_TEST_DEFINE(codec_register_twice)
  116. {
  117. switch (cmd) {
  118. case TEST_INIT:
  119. info->name = "codec_register_twice";
  120. info->category = "/main/core_codec/";
  121. info->summary = "codec registration unit test";
  122. info->description =
  123. "Test double registration of a core codec to confirm it fails";
  124. return AST_TEST_NOT_RUN;
  125. case TEST_EXECUTE:
  126. break;
  127. }
  128. if (ast_codec_register(&doubly)) {
  129. ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
  130. return AST_TEST_FAIL;
  131. }
  132. if (!ast_codec_register(&doubly)) {
  133. ast_test_status_update(test, "Successfully registered a codec twice\n");
  134. return AST_TEST_FAIL;
  135. }
  136. return AST_TEST_PASS;
  137. }
  138. AST_TEST_DEFINE(codec_register_unknown)
  139. {
  140. switch (cmd) {
  141. case TEST_INIT:
  142. info->name = "codec_register_unknown";
  143. info->category = "/main/core_codec/";
  144. info->summary = "codec registration unit test";
  145. info->description =
  146. "Test that registration of an unknown codec type fails";
  147. return AST_TEST_NOT_RUN;
  148. case TEST_EXECUTE:
  149. break;
  150. }
  151. if (!ast_codec_register(&unknown)) {
  152. ast_test_status_update(test, "Successfully registered a codec with an unknown media type\n");
  153. return AST_TEST_FAIL;
  154. }
  155. return AST_TEST_PASS;
  156. }
  157. AST_TEST_DEFINE(codec_register_audio_no_sample_rate)
  158. {
  159. switch (cmd) {
  160. case TEST_INIT:
  161. info->name = "codec_register_audio_no_sample_rate";
  162. info->category = "/main/core_codec/";
  163. info->summary = "codec registration unit test";
  164. info->description =
  165. "Test that registration of an audio codec without sample rate fails";
  166. return AST_TEST_NOT_RUN;
  167. case TEST_EXECUTE:
  168. break;
  169. }
  170. if (!ast_codec_register(&audio_without_rate)) {
  171. ast_test_status_update(test, "Successfully registered an audio codec without a sample rate\n");
  172. return AST_TEST_FAIL;
  173. }
  174. return AST_TEST_PASS;
  175. }
  176. AST_TEST_DEFINE(codec_get)
  177. {
  178. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  179. switch (cmd) {
  180. case TEST_INIT:
  181. info->name = "codec_get";
  182. info->category = "/main/core_codec/";
  183. info->summary = "codec get unit test";
  184. info->description =
  185. "Test that getting of a known codec succeeds";
  186. return AST_TEST_NOT_RUN;
  187. case TEST_EXECUTE:
  188. break;
  189. }
  190. if (ast_codec_register(&audio_get)) {
  191. ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
  192. return AST_TEST_FAIL;
  193. }
  194. codec = ast_codec_get("unit_test_audio_get", AST_MEDIA_TYPE_AUDIO, 8000);
  195. if (!codec) {
  196. ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
  197. return AST_TEST_FAIL;
  198. } else if (strcmp(codec->name, audio_get.name)) {
  199. ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
  200. return AST_TEST_FAIL;
  201. } else if (codec->type != audio_get.type) {
  202. ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
  203. return AST_TEST_FAIL;
  204. } else if (codec->sample_rate != audio_get.sample_rate) {
  205. ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
  206. return AST_TEST_FAIL;
  207. }
  208. return AST_TEST_PASS;
  209. }
  210. AST_TEST_DEFINE(codec_get_unregistered)
  211. {
  212. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  213. switch (cmd) {
  214. case TEST_INIT:
  215. info->name = "codec_get_unregistered";
  216. info->category = "/main/core_codec/";
  217. info->summary = "codec get unit test";
  218. info->description =
  219. "Test that getting of a codec that is not registered fails";
  220. return AST_TEST_NOT_RUN;
  221. case TEST_EXECUTE:
  222. break;
  223. }
  224. codec = ast_codec_get("goats", AST_MEDIA_TYPE_AUDIO, 8000);
  225. if (codec) {
  226. ast_test_status_update(test, "Successfully got a codec named '%s' when getting a codec named 'goats'\n",
  227. codec->name);
  228. return AST_TEST_FAIL;
  229. }
  230. return AST_TEST_PASS;
  231. }
  232. AST_TEST_DEFINE(codec_get_unknown)
  233. {
  234. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  235. switch (cmd) {
  236. case TEST_INIT:
  237. info->name = "codec_get_unknown";
  238. info->category = "/main/core_codec/";
  239. info->summary = "codec get unit test";
  240. info->description =
  241. "Test that getting of a known codec using name and unknown type succeeds";
  242. return AST_TEST_NOT_RUN;
  243. case TEST_EXECUTE:
  244. break;
  245. }
  246. if (ast_codec_register(&audio_get_unknown)) {
  247. ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
  248. return AST_TEST_FAIL;
  249. }
  250. codec = ast_codec_get("unit_test_audio_get_unknown", AST_MEDIA_TYPE_UNKNOWN, 8000);
  251. if (!codec) {
  252. ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
  253. return AST_TEST_FAIL;
  254. } else if (strcmp(codec->name, audio_get_unknown.name)) {
  255. ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
  256. return AST_TEST_FAIL;
  257. } else if (codec->type != audio_get_unknown.type) {
  258. ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
  259. return AST_TEST_FAIL;
  260. } else if (codec->sample_rate != audio_get_unknown.sample_rate) {
  261. ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
  262. return AST_TEST_FAIL;
  263. }
  264. return AST_TEST_PASS;
  265. }
  266. AST_TEST_DEFINE(codec_get_id)
  267. {
  268. RAII_VAR(struct ast_codec *, named, NULL, ao2_cleanup);
  269. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  270. switch (cmd) {
  271. case TEST_INIT:
  272. info->name = "codec_get_unknown";
  273. info->category = "/main/core_codec/";
  274. info->summary = "codec get unit test";
  275. info->description =
  276. "Test that getting of a known codec using name and unknown type succeeds";
  277. return AST_TEST_NOT_RUN;
  278. case TEST_EXECUTE:
  279. break;
  280. }
  281. if (ast_codec_register(&audio_get_id)) {
  282. ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
  283. return AST_TEST_FAIL;
  284. }
  285. named = ast_codec_get("unit_test_audio_get_id", AST_MEDIA_TYPE_AUDIO, 8000);
  286. if (!named) {
  287. ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
  288. return AST_TEST_FAIL;
  289. }
  290. codec = ast_codec_get_by_id(named->id);
  291. if (!codec) {
  292. ast_test_status_update(test, "Unsuccessfully retrieved a codec using id of a named codec we just got\n");
  293. return AST_TEST_FAIL;
  294. }
  295. return AST_TEST_PASS;
  296. }
  297. static int unload_module(void)
  298. {
  299. AST_TEST_UNREGISTER(codec_register);
  300. AST_TEST_UNREGISTER(codec_register_twice);
  301. AST_TEST_UNREGISTER(codec_register_unknown);
  302. AST_TEST_UNREGISTER(codec_register_audio_no_sample_rate);
  303. AST_TEST_UNREGISTER(codec_get);
  304. AST_TEST_UNREGISTER(codec_get_unregistered);
  305. AST_TEST_UNREGISTER(codec_get_unknown);
  306. AST_TEST_UNREGISTER(codec_get_id);
  307. return 0;
  308. }
  309. static int load_module(void)
  310. {
  311. AST_TEST_REGISTER(codec_register);
  312. AST_TEST_REGISTER(codec_register_twice);
  313. AST_TEST_REGISTER(codec_register_unknown);
  314. AST_TEST_REGISTER(codec_register_audio_no_sample_rate);
  315. AST_TEST_REGISTER(codec_get);
  316. AST_TEST_REGISTER(codec_get_unregistered);
  317. AST_TEST_REGISTER(codec_get_unknown);
  318. AST_TEST_REGISTER(codec_get_id);
  319. return AST_MODULE_LOAD_SUCCESS;
  320. }
  321. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core codec API test module");