test_core_codec.c 9.7 KB

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