test_core_format.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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 Format 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. #define TEST_CATEGORY "/main/core_format/"
  36. static void test_core_format_destroy(struct ast_format *format);
  37. static int test_core_format_clone(const struct ast_format *src, struct ast_format *dst);
  38. static enum ast_format_cmp_res test_core_format_cmp(const struct ast_format *format1, const struct ast_format *format2);
  39. static struct ast_format *test_core_format_get_joint(const struct ast_format *format1, const struct ast_format *format2);
  40. static struct ast_format *test_core_format_attribute_set(const struct ast_format *format, const char *name, const char *value);
  41. static const void *test_core_format_attribute_get(const struct ast_format *format, const char *name);
  42. static struct ast_format *test_core_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes);
  43. static void test_core_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str);
  44. /*! \brief A format attribute 'module' used by the unit tests */
  45. static struct ast_format_interface test_core_format_attr = {
  46. .format_destroy = &test_core_format_destroy,
  47. .format_clone = &test_core_format_clone,
  48. .format_cmp = &test_core_format_cmp,
  49. .format_get_joint = &test_core_format_get_joint,
  50. .format_attribute_set = &test_core_format_attribute_set,
  51. .format_attribute_get = &test_core_format_attribute_get,
  52. .format_parse_sdp_fmtp = &test_core_format_parse_sdp_fmtp,
  53. .format_generate_sdp_fmtp = &test_core_format_generate_sdp_fmtp,
  54. };
  55. /*! \brief A test piece of data to associate with \ref test_core_format_attr */
  56. struct test_core_format_pvt {
  57. /*! Some data field */
  58. int field_one;
  59. /*! Another arbitrary data field */
  60. int field_two;
  61. };
  62. /*! \brief A test codec for these unit tests. Should be used with \c test_core_format */
  63. static struct ast_codec test_core_format_codec = {
  64. .name = "test_core_format_codec",
  65. .description = "Unit test codec used by test_core_format",
  66. .type = AST_MEDIA_TYPE_AUDIO,
  67. .sample_rate = 8000,
  68. .minimum_ms = 10,
  69. .maximum_ms = 150,
  70. .default_ms = 20,
  71. };
  72. /*! \brief Tracking object used to verify format attribute callbacks */
  73. struct callbacks_called {
  74. /*! Number of times \ref test_core_format_destroy was called */
  75. int format_destroy;
  76. /*! Number of times \ref test_core_format_clone was called */
  77. int format_clone;
  78. /*! Number of times \ref test_core_format_cmp was called */
  79. int format_cmp;
  80. /*! Number of times \ref test_core_format_get_joint was called */
  81. int format_get_joint;
  82. /*! Number of times \ref test_core_format_attribute_set was called */
  83. int format_attribute_set;
  84. /*! Number of times \ref test_core_format_parse_sdp_fmtp was called */
  85. int format_parse_sdp_fmtp;
  86. /*! Number of times \ref test_core_format_generate_sdp_fmtp was called */
  87. int format_generate_sdp_fmtp;
  88. };
  89. /*! \brief A global tracking object. Cleared out by the test init cb */
  90. static struct callbacks_called test_callbacks_called;
  91. /*! \brief Format attribute callback for when format attributes are to be destroyed */
  92. static void test_core_format_destroy(struct ast_format *format)
  93. {
  94. struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format);
  95. ast_free(pvt);
  96. ++test_callbacks_called.format_destroy;
  97. }
  98. /*! \brief Format attribute callback called during format cloning */
  99. static int test_core_format_clone(const struct ast_format *src, struct ast_format *dst)
  100. {
  101. struct test_core_format_pvt *pvt = ast_format_get_attribute_data(src);
  102. struct test_core_format_pvt *new_pvt;
  103. new_pvt = ast_calloc(1, sizeof(*new_pvt));
  104. if (!new_pvt) {
  105. return -1;
  106. }
  107. if (pvt) {
  108. *new_pvt = *pvt;
  109. }
  110. ast_format_set_attribute_data(dst, new_pvt);
  111. ++test_callbacks_called.format_clone;
  112. return 0;
  113. }
  114. /*! \brief Format attribute callback called during format comparison */
  115. static enum ast_format_cmp_res test_core_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
  116. {
  117. struct test_core_format_pvt *pvt1 = ast_format_get_attribute_data(format1);
  118. struct test_core_format_pvt *pvt2 = ast_format_get_attribute_data(format2);
  119. ++test_callbacks_called.format_cmp;
  120. if (pvt1 == pvt2) {
  121. return AST_FORMAT_CMP_EQUAL;
  122. }
  123. if ((!pvt1 && pvt2 && (pvt2->field_one != 0 || pvt2->field_two != 0))
  124. || (pvt1 && !pvt2 && (pvt1->field_one != 0 || pvt1->field_two != 0))) {
  125. return AST_FORMAT_CMP_NOT_EQUAL;
  126. }
  127. if (pvt1 && pvt2) {
  128. if (!memcmp(pvt1, pvt2, sizeof(*pvt1))) {
  129. return AST_FORMAT_CMP_EQUAL;
  130. } else {
  131. return AST_FORMAT_CMP_NOT_EQUAL;
  132. }
  133. }
  134. return AST_FORMAT_CMP_EQUAL;
  135. }
  136. /*!
  137. * \brief Format attribute callback called during joint format capability
  138. * \note Our test will assume the max of attributes \c field_one and \c field_two
  139. */
  140. static struct ast_format *test_core_format_get_joint(const struct ast_format *format1, const struct ast_format *format2)
  141. {
  142. struct test_core_format_pvt *pvt1 = ast_format_get_attribute_data(format1);
  143. struct test_core_format_pvt *pvt2 = ast_format_get_attribute_data(format2);
  144. struct ast_format *joint;
  145. struct test_core_format_pvt *joint_pvt;
  146. joint = ast_format_clone(format1);
  147. if (!joint) {
  148. return NULL;
  149. }
  150. joint_pvt = ast_format_get_attribute_data(joint);
  151. joint_pvt->field_one = MAX(pvt1 ? pvt1->field_one : 0, pvt2 ? pvt2->field_one : 0);
  152. joint_pvt->field_two = MAX(pvt2 ? pvt2->field_two : 0, pvt2 ? pvt2->field_two : 0);
  153. ++test_callbacks_called.format_get_joint;
  154. return joint;
  155. }
  156. /*! \brief Format attribute callback for setting an attribute on a format */
  157. static struct ast_format *test_core_format_attribute_set(const struct ast_format *format, const char *name, const char *value)
  158. {
  159. struct ast_format *clone = ast_format_clone(format);
  160. struct test_core_format_pvt *clone_pvt;
  161. if (!clone) {
  162. return NULL;
  163. }
  164. clone_pvt = ast_format_get_attribute_data(clone);
  165. if (!strcmp(name, "one")) {
  166. clone_pvt->field_one = atoi(value);
  167. } else if (!strcmp(name, "two")) {
  168. clone_pvt->field_two = atoi(value);
  169. }
  170. ++test_callbacks_called.format_attribute_set;
  171. return clone;
  172. }
  173. /*! \brief Format attribute callback for retrieving an attribute */
  174. static const void *test_core_format_attribute_get(const struct ast_format *format, const char *name)
  175. {
  176. struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format);
  177. if (!strcmp(name, "one")) {
  178. return &pvt->field_one;
  179. } else if (!strcmp(name, "two")) {
  180. return &pvt->field_two;
  181. }
  182. return NULL;
  183. }
  184. /*! \brief Format attribute callback to construct a format from an SDP fmtp line */
  185. static struct ast_format *test_core_format_parse_sdp_fmtp(const struct ast_format *format, const char *attributes)
  186. {
  187. struct ast_format *clone = ast_format_clone(format);
  188. struct test_core_format_pvt *pvt;
  189. if (!clone) {
  190. return NULL;
  191. }
  192. pvt = ast_format_get_attribute_data(clone);
  193. if (sscanf(attributes, "one=%d;two=%d", &pvt->field_one, &pvt->field_two) != 2) {
  194. ao2_ref(clone, -1);
  195. return NULL;
  196. }
  197. ++test_callbacks_called.format_parse_sdp_fmtp;
  198. return clone;
  199. }
  200. /*! \brief Format attribute callback to generate an SDP fmtp line from a format */
  201. static void test_core_format_generate_sdp_fmtp(const struct ast_format *format, unsigned int payload, struct ast_str **str)
  202. {
  203. struct test_core_format_pvt *pvt = ast_format_get_attribute_data(format);
  204. if (!pvt) {
  205. return;
  206. }
  207. ast_str_append(str, 0, "a=fmtp:%u one=%d;two=%d\r\n", payload, pvt->field_one, pvt->field_two);
  208. ++test_callbacks_called.format_generate_sdp_fmtp;
  209. }
  210. AST_TEST_DEFINE(format_create)
  211. {
  212. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  213. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  214. switch (cmd) {
  215. case TEST_INIT:
  216. info->name = __PRETTY_FUNCTION__;
  217. info->category = TEST_CATEGORY;
  218. info->summary = "Format creation unit test";
  219. info->description =
  220. "Test creation of a format";
  221. return AST_TEST_NOT_RUN;
  222. case TEST_EXECUTE:
  223. break;
  224. }
  225. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  226. if (!codec) {
  227. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  228. return AST_TEST_FAIL;
  229. }
  230. format = ast_format_create(codec);
  231. if (!format) {
  232. ast_test_status_update(test, "Could not create format using built-in codec\n");
  233. return AST_TEST_FAIL;
  234. } else if (ast_format_get_codec_id(format) != codec->id) {
  235. ast_test_status_update(test, "Created format does not contain provided codec\n");
  236. return AST_TEST_FAIL;
  237. }
  238. ao2_ref(format, -1);
  239. format = ast_format_create_named("super_ulaw", codec);
  240. if (!format) {
  241. ast_test_status_update(test, "Could not create format using built-in codec\n");
  242. return AST_TEST_FAIL;
  243. } else if (ast_format_get_codec_id(format) != codec->id) {
  244. ast_test_status_update(test, "Created format does not contain provided codec\n");
  245. return AST_TEST_FAIL;
  246. }
  247. return AST_TEST_PASS;
  248. }
  249. AST_TEST_DEFINE(format_create_attr)
  250. {
  251. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  252. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  253. RAII_VAR(struct ast_format *, format_w_attr, NULL, ao2_cleanup);
  254. switch (cmd) {
  255. case TEST_INIT:
  256. info->name = __PRETTY_FUNCTION__;
  257. info->category = TEST_CATEGORY;
  258. info->summary = "Format creation w/ attributes unit test";
  259. info->description =
  260. "Test creation of a format with attributes";
  261. return AST_TEST_NOT_RUN;
  262. case TEST_EXECUTE:
  263. break;
  264. }
  265. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  266. if (!codec) {
  267. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  268. return AST_TEST_FAIL;
  269. }
  270. format = ast_format_create(codec);
  271. if (!format) {
  272. ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n");
  273. return AST_TEST_FAIL;
  274. } else if (ast_format_get_codec_id(format) != codec->id) {
  275. ast_test_status_update(test, "Created format does not contain provided codec\n");
  276. return AST_TEST_FAIL;
  277. }
  278. format_w_attr = ast_format_attribute_set(format, "one", "1");
  279. if (!format_w_attr) {
  280. ast_test_status_update(test, "Could not create format with attributes using test_core_format_codec codec\n");
  281. return AST_TEST_FAIL;
  282. } else if (ast_format_get_codec_id(format_w_attr) != codec->id) {
  283. ast_test_status_update(test, "Created format does not contain provided codec\n");
  284. return AST_TEST_FAIL;
  285. } else if (ast_format_cmp(format, format_w_attr) == AST_FORMAT_CMP_EQUAL) {
  286. ast_test_status_update(test, "Format with attributes should not be equal to format without attributes\n");
  287. return AST_TEST_FAIL;
  288. }
  289. ast_test_validate(test, test_callbacks_called.format_attribute_set == 1);
  290. ast_test_validate(test, test_callbacks_called.format_cmp == 1);
  291. return AST_TEST_PASS;
  292. }
  293. AST_TEST_DEFINE(format_retrieve_attr)
  294. {
  295. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  296. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  297. RAII_VAR(struct ast_format *, format_w_attr, NULL, ao2_cleanup);
  298. switch (cmd) {
  299. case TEST_INIT:
  300. info->name = __PRETTY_FUNCTION__;
  301. info->category = TEST_CATEGORY;
  302. info->summary = "Format attribute retrieval unit test";
  303. info->description =
  304. "Test retrieval of format attributes";
  305. return AST_TEST_NOT_RUN;
  306. case TEST_EXECUTE:
  307. break;
  308. }
  309. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  310. if (!codec) {
  311. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  312. return AST_TEST_FAIL;
  313. }
  314. format = ast_format_create(codec);
  315. if (!format) {
  316. ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n");
  317. return AST_TEST_FAIL;
  318. }
  319. format_w_attr = ast_format_attribute_set(format, "one", "1");
  320. if (!format_w_attr) {
  321. ast_test_status_update(test, "Could not create format with attributes using test_core_format_codec codec\n");
  322. return AST_TEST_FAIL;
  323. }
  324. if (*((int *)ast_format_attribute_get(format_w_attr, "one")) != 1) {
  325. ast_test_status_update(test, "Could not retrieve valid format attribute\n");
  326. return AST_TEST_FAIL;
  327. }
  328. if (ast_format_attribute_get(format_w_attr, "foo") != NULL) {
  329. ast_test_status_update(test, "Retrieved invalid format attribute\n");
  330. return AST_TEST_FAIL;
  331. }
  332. return AST_TEST_PASS;
  333. }
  334. AST_TEST_DEFINE(format_clone)
  335. {
  336. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  337. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  338. RAII_VAR(struct ast_format *, format_w_attr, NULL, ao2_cleanup);
  339. RAII_VAR(struct ast_format *, clone, NULL, ao2_cleanup);
  340. switch (cmd) {
  341. case TEST_INIT:
  342. info->name = __PRETTY_FUNCTION__;
  343. info->category = TEST_CATEGORY;
  344. info->summary = "Format cloning unit test";
  345. info->description =
  346. "Test cloning of a format";
  347. return AST_TEST_NOT_RUN;
  348. case TEST_EXECUTE:
  349. break;
  350. }
  351. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  352. if (!codec) {
  353. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  354. return AST_TEST_FAIL;
  355. }
  356. format = ast_format_create(codec);
  357. if (!format) {
  358. ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n");
  359. return AST_TEST_FAIL;
  360. } else if (ast_format_get_codec_id(format) != codec->id) {
  361. ast_test_status_update(test, "Created format does not contain provided codec\n");
  362. return AST_TEST_FAIL;
  363. }
  364. format_w_attr = ast_format_attribute_set(format, "one", "1");
  365. if (!format_w_attr) {
  366. ast_test_status_update(test, "Could not create format with attributes using test_core_format_codec codec\n");
  367. return AST_TEST_FAIL;
  368. } else if (ast_format_get_codec_id(format_w_attr) != codec->id) {
  369. ast_test_status_update(test, "Created format does not contain provided codec\n");
  370. return AST_TEST_FAIL;
  371. }
  372. /* Test cloning a format without attributes */
  373. clone = ast_format_clone(format);
  374. if (!clone) {
  375. ast_test_status_update(test, "Could not create cloned format\n");
  376. return AST_TEST_FAIL;
  377. } else if (ast_format_get_codec_id(clone) != codec->id) {
  378. ast_test_status_update(test, "Cloned format does not contain provided codec\n");
  379. return AST_TEST_FAIL;
  380. } else if (clone == format) {
  381. ast_test_status_update(test, "Cloned format pointer is the same as original format pointer\n");
  382. return AST_TEST_FAIL;
  383. } else if (ast_format_cmp(clone, format) != AST_FORMAT_CMP_EQUAL) {
  384. ast_test_status_update(test, "Cloned format is not the same as its original format\n");
  385. return AST_TEST_FAIL;
  386. }
  387. ao2_ref(clone, -1);
  388. /* Test cloning a format with attributes */
  389. clone = ast_format_clone(format_w_attr);
  390. if (!clone) {
  391. ast_test_status_update(test, "Could not create cloned format\n");
  392. return AST_TEST_FAIL;
  393. } else if (ast_format_get_codec_id(clone) != codec->id) {
  394. ast_test_status_update(test, "Cloned format does not contain provided codec\n");
  395. return AST_TEST_FAIL;
  396. } else if (clone == format_w_attr) {
  397. ast_test_status_update(test, "Cloned format pointer is the same as original format pointer\n");
  398. return AST_TEST_FAIL;
  399. } else if (ast_format_cmp(clone, format_w_attr) != AST_FORMAT_CMP_EQUAL) {
  400. ast_test_status_update(test, "Cloned format is not the same as its original format\n");
  401. return AST_TEST_FAIL;
  402. }
  403. ast_test_validate(test, test_callbacks_called.format_attribute_set == 1);
  404. ast_test_validate(test, test_callbacks_called.format_clone == 3);
  405. ast_test_validate(test, test_callbacks_called.format_cmp == 2);
  406. return AST_TEST_PASS;
  407. }
  408. AST_TEST_DEFINE(format_cmp_same_codec)
  409. {
  410. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  411. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  412. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  413. RAII_VAR(struct ast_format *, named, NULL, ao2_cleanup);
  414. switch (cmd) {
  415. case TEST_INIT:
  416. info->name = __PRETTY_FUNCTION__;
  417. info->category = TEST_CATEGORY;
  418. info->summary = "Format comparison unit test";
  419. info->description =
  420. "Test comparison of two different formats with same codec";
  421. return AST_TEST_NOT_RUN;
  422. case TEST_EXECUTE:
  423. break;
  424. }
  425. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  426. if (!codec) {
  427. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  428. return AST_TEST_FAIL;
  429. }
  430. first = ast_format_create(codec);
  431. if (!first) {
  432. ast_test_status_update(test, "Could not create first format using built-in codec\n");
  433. return AST_TEST_FAIL;
  434. }
  435. second = ast_format_create(codec);
  436. if (!second) {
  437. ast_test_status_update(test, "Could not create second format using built-in codec\n");
  438. return AST_TEST_FAIL;
  439. }
  440. named = ast_format_create_named("super_ulaw", codec);
  441. if (!named) {
  442. ast_test_status_update(test, "Could not create named format using built-in codec\n");
  443. return AST_TEST_FAIL;
  444. }
  445. if (ast_format_cmp(first, second) != AST_FORMAT_CMP_EQUAL) {
  446. ast_test_status_update(test, "Two formats that are the same compared as not being equal\n");
  447. return AST_TEST_FAIL;
  448. }
  449. if (ast_format_cmp(first, named) != AST_FORMAT_CMP_EQUAL) {
  450. ast_test_status_update(test, "Two formats that are the same compared as not being equal\n");
  451. return AST_TEST_FAIL;
  452. }
  453. return AST_TEST_PASS;
  454. }
  455. AST_TEST_DEFINE(format_cmp_different_codec)
  456. {
  457. RAII_VAR(struct ast_codec *, first_codec, NULL, ao2_cleanup);
  458. RAII_VAR(struct ast_codec *, second_codec, NULL, ao2_cleanup);
  459. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  460. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  461. switch (cmd) {
  462. case TEST_INIT:
  463. info->name = __PRETTY_FUNCTION__;
  464. info->category = TEST_CATEGORY;
  465. info->summary = "Format comparison unit test";
  466. info->description =
  467. "Test comparison of two different formats with different codec";
  468. return AST_TEST_NOT_RUN;
  469. case TEST_EXECUTE:
  470. break;
  471. }
  472. first_codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  473. if (!first_codec) {
  474. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  475. return AST_TEST_FAIL;
  476. }
  477. first = ast_format_create(first_codec);
  478. if (!first) {
  479. ast_test_status_update(test, "Could not create first format using built-in codec\n");
  480. return AST_TEST_FAIL;
  481. }
  482. second_codec = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
  483. if (!second_codec) {
  484. ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
  485. return AST_TEST_FAIL;
  486. }
  487. second = ast_format_create(second_codec);
  488. if (!second) {
  489. ast_test_status_update(test, "Could not create second format using built-in codec\n");
  490. return AST_TEST_FAIL;
  491. }
  492. if (ast_format_cmp(first, second) != AST_FORMAT_CMP_NOT_EQUAL) {
  493. ast_test_status_update(test, "Two formats that have different codecs did not compare as being not equal\n");
  494. return AST_TEST_FAIL;
  495. }
  496. return AST_TEST_PASS;
  497. }
  498. AST_TEST_DEFINE(format_attr_cmp_same_codec)
  499. {
  500. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  501. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  502. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  503. RAII_VAR(struct ast_format *, original, NULL, ao2_cleanup);
  504. switch (cmd) {
  505. case TEST_INIT:
  506. info->name = __PRETTY_FUNCTION__;
  507. info->category = TEST_CATEGORY;
  508. info->summary = "Format with attributes comparison unit test";
  509. info->description =
  510. "Test comparison of two different formats with attributes with same codec";
  511. return AST_TEST_NOT_RUN;
  512. case TEST_EXECUTE:
  513. break;
  514. }
  515. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  516. if (!codec) {
  517. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  518. return AST_TEST_FAIL;
  519. }
  520. original = ast_format_create(codec);
  521. if (!original) {
  522. ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n");
  523. return AST_TEST_FAIL;
  524. }
  525. first = ast_format_attribute_set(original, "one", "1");
  526. if (!first) {
  527. ast_test_status_update(test, "Could not create first format with attributes\n");
  528. return AST_TEST_FAIL;
  529. }
  530. second = ast_format_attribute_set(original, "two", "1");
  531. if (!second) {
  532. ast_test_status_update(test, "Could not create second format with attributes\n");
  533. return AST_TEST_FAIL;
  534. }
  535. if (ast_format_cmp(first, second) == AST_FORMAT_CMP_EQUAL) {
  536. ast_test_status_update(test, "Formats with different attributes were compared to be equal when they should not\n");
  537. return AST_TEST_FAIL;
  538. }
  539. ao2_ref(second, -1);
  540. second = ast_format_attribute_set(original, "one", "1");
  541. if (ast_format_cmp(first, second) != AST_FORMAT_CMP_EQUAL) {
  542. ast_test_status_update(test, "Formats with the same attributes should be equal\n");
  543. return AST_TEST_FAIL;
  544. }
  545. ast_test_validate(test, test_callbacks_called.format_attribute_set == 3);
  546. ast_test_validate(test, test_callbacks_called.format_cmp == 2);
  547. return AST_TEST_PASS;
  548. }
  549. AST_TEST_DEFINE(format_joint_same_codec)
  550. {
  551. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  552. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  553. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  554. RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup);
  555. switch (cmd) {
  556. case TEST_INIT:
  557. info->name = __PRETTY_FUNCTION__;
  558. info->category = TEST_CATEGORY;
  559. info->summary = "Joint format unit test";
  560. info->description =
  561. "Test joint format creation using two different formats with same codec";
  562. return AST_TEST_NOT_RUN;
  563. case TEST_EXECUTE:
  564. break;
  565. }
  566. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  567. if (!codec) {
  568. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  569. return AST_TEST_FAIL;
  570. }
  571. first = ast_format_create(codec);
  572. if (!first) {
  573. ast_test_status_update(test, "Could not create first format using built-in codec\n");
  574. return AST_TEST_FAIL;
  575. }
  576. second = ast_format_create(codec);
  577. if (!second) {
  578. ast_test_status_update(test, "Could not create second format using built-in codec\n");
  579. return AST_TEST_FAIL;
  580. }
  581. joint = ast_format_joint(first, second);
  582. if (!joint) {
  583. ast_test_status_update(test, "Failed to create a joint format using two formats of same codec\n");
  584. return AST_TEST_FAIL;
  585. } else if (ast_format_get_codec_id(joint) != codec->id) {
  586. ast_test_status_update(test, "Returned joint format does not contain expected codec\n");
  587. return AST_TEST_FAIL;
  588. }
  589. return AST_TEST_PASS;
  590. }
  591. AST_TEST_DEFINE(format_attr_joint_same_codec)
  592. {
  593. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  594. RAII_VAR(struct ast_format *, original, NULL, ao2_cleanup);
  595. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  596. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  597. RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup);
  598. struct ast_str *fmtp = ast_str_alloca(64);
  599. switch (cmd) {
  600. case TEST_INIT:
  601. info->name = __PRETTY_FUNCTION__;
  602. info->category = TEST_CATEGORY;
  603. info->summary = "Joint format attribute unit test";
  604. info->description =
  605. "Test joint format creation using two different formats with attributes and with same codec";
  606. return AST_TEST_NOT_RUN;
  607. case TEST_EXECUTE:
  608. break;
  609. }
  610. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  611. if (!codec) {
  612. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  613. return AST_TEST_FAIL;
  614. }
  615. original = ast_format_create(codec);
  616. if (!original) {
  617. ast_test_status_update(test, "Could not create format from test_core_format_codec codec\n");
  618. return AST_TEST_FAIL;
  619. }
  620. first = ast_format_attribute_set(original, "one", "2");
  621. if (!first) {
  622. ast_test_status_update(test, "Could not create first format using test_core_format_codec codec\n");
  623. return AST_TEST_FAIL;
  624. }
  625. second = ast_format_attribute_set(original, "one", "5");
  626. if (!second) {
  627. ast_test_status_update(test, "Could not create second format using test_core_format_codec codec\n");
  628. return AST_TEST_FAIL;
  629. }
  630. joint = ast_format_joint(first, second);
  631. if (!joint) {
  632. ast_test_status_update(test, "Failed to create a joint format using two formats of same codec\n");
  633. return AST_TEST_FAIL;
  634. } else if (ast_format_get_codec_id(joint) != codec->id) {
  635. ast_test_status_update(test, "Returned joint format does not contain expected codec\n");
  636. return AST_TEST_FAIL;
  637. }
  638. ast_format_generate_sdp_fmtp(joint, 100, &fmtp);
  639. ast_test_validate(test, strcmp("a=fmtp:100 one=5;two=0\r\n", ast_str_buffer(fmtp)) == 0);
  640. ast_test_validate(test, test_callbacks_called.format_attribute_set == 2);
  641. ast_test_validate(test, test_callbacks_called.format_get_joint == 1);
  642. ast_test_validate(test, test_callbacks_called.format_generate_sdp_fmtp == 1);
  643. return AST_TEST_PASS;
  644. }
  645. AST_TEST_DEFINE(format_joint_different_codec)
  646. {
  647. RAII_VAR(struct ast_codec *, first_codec, NULL, ao2_cleanup);
  648. RAII_VAR(struct ast_codec *, second_codec, NULL, ao2_cleanup);
  649. RAII_VAR(struct ast_format *, first, NULL, ao2_cleanup);
  650. RAII_VAR(struct ast_format *, second, NULL, ao2_cleanup);
  651. RAII_VAR(struct ast_format *, joint, NULL, ao2_cleanup);
  652. switch (cmd) {
  653. case TEST_INIT:
  654. info->name = __PRETTY_FUNCTION__;
  655. info->category = TEST_CATEGORY;
  656. info->summary = "Joint format unit test";
  657. info->description =
  658. "Test that there is no joint format between two different formats with different codec";
  659. return AST_TEST_NOT_RUN;
  660. case TEST_EXECUTE:
  661. break;
  662. }
  663. first_codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  664. if (!first_codec) {
  665. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  666. return AST_TEST_FAIL;
  667. }
  668. first = ast_format_create(first_codec);
  669. if (!first) {
  670. ast_test_status_update(test, "Could not create first format using built-in codec\n");
  671. return AST_TEST_FAIL;
  672. }
  673. second_codec = ast_codec_get("alaw", AST_MEDIA_TYPE_AUDIO, 8000);
  674. if (!second_codec) {
  675. ast_test_status_update(test, "Could not retrieve built-in alaw codec\n");
  676. return AST_TEST_FAIL;
  677. }
  678. second = ast_format_create(second_codec);
  679. if (!second) {
  680. ast_test_status_update(test, "Could not create second format using built-in codec\n");
  681. return AST_TEST_FAIL;
  682. }
  683. joint = ast_format_joint(first, second);
  684. if (joint) {
  685. ast_test_status_update(test, "Got a joint format between two formats with different codecs\n");
  686. return AST_TEST_FAIL;
  687. }
  688. return AST_TEST_PASS;
  689. }
  690. AST_TEST_DEFINE(format_copy)
  691. {
  692. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  693. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  694. RAII_VAR(struct ast_format *, copy, NULL, ao2_cleanup);
  695. switch (cmd) {
  696. case TEST_INIT:
  697. info->name = __PRETTY_FUNCTION__;
  698. info->category = TEST_CATEGORY;
  699. info->summary = "Format copying unit test";
  700. info->description =
  701. "Test copying of a format";
  702. return AST_TEST_NOT_RUN;
  703. case TEST_EXECUTE:
  704. break;
  705. }
  706. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  707. if (!codec) {
  708. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  709. return AST_TEST_FAIL;
  710. }
  711. format = ast_format_create(codec);
  712. if (!format) {
  713. ast_test_status_update(test, "Could not create format using built-in codec\n");
  714. return AST_TEST_FAIL;
  715. }
  716. copy = ao2_bump(format);
  717. if (!copy) {
  718. ast_test_status_update(test, "Copying of a just created format failed\n");
  719. return AST_TEST_FAIL;
  720. } else if (copy != format) {
  721. ast_test_status_update(test, "Copying of a format returned a new format instead of the same one\n");
  722. return AST_TEST_FAIL;
  723. }
  724. return AST_TEST_PASS;
  725. }
  726. AST_TEST_DEFINE(format_attribute_set_without_interface)
  727. {
  728. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  729. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  730. struct ast_format *attr_set;
  731. switch (cmd) {
  732. case TEST_INIT:
  733. info->name = __PRETTY_FUNCTION__;
  734. info->category = TEST_CATEGORY;
  735. info->summary = "Format attribute setting unit test";
  736. info->description =
  737. "Test that attribute setting on a format without an interface fails";
  738. return AST_TEST_NOT_RUN;
  739. case TEST_EXECUTE:
  740. break;
  741. }
  742. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  743. if (!codec) {
  744. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  745. return AST_TEST_FAIL;
  746. }
  747. format = ast_format_create(codec);
  748. if (!format) {
  749. ast_test_status_update(test, "Could not create format using built-in codec\n");
  750. return AST_TEST_FAIL;
  751. }
  752. attr_set = ast_format_attribute_set(format, "bees", "cool");
  753. if (!attr_set) {
  754. ast_test_status_update(test, "Successfully set an attribute on a format without an interface\n");
  755. return AST_TEST_FAIL;
  756. }
  757. ao2_cleanup(attr_set);
  758. return AST_TEST_PASS;
  759. }
  760. AST_TEST_DEFINE(format_attribute_get_without_interface)
  761. {
  762. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  763. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  764. switch (cmd) {
  765. case TEST_INIT:
  766. info->name = __PRETTY_FUNCTION__;
  767. info->category = TEST_CATEGORY;
  768. info->summary = "Format attribute retrieval unit test";
  769. info->description =
  770. "Test that attribute retrieval on a format without an interface fails";
  771. return AST_TEST_NOT_RUN;
  772. case TEST_EXECUTE:
  773. break;
  774. }
  775. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  776. if (!codec) {
  777. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  778. return AST_TEST_FAIL;
  779. }
  780. format = ast_format_create(codec);
  781. if (!format) {
  782. ast_test_status_update(test, "Could not create format using built-in codec\n");
  783. return AST_TEST_FAIL;
  784. }
  785. if (ast_format_attribute_get(format, "bees") != NULL) {
  786. ast_test_status_update(test, "Successfully retrieved an attribute on a format without an interface\n");
  787. return AST_TEST_FAIL;
  788. }
  789. return AST_TEST_PASS;
  790. }
  791. AST_TEST_DEFINE(format_parse_sdp_fmtp_without_interface)
  792. {
  793. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  794. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  795. RAII_VAR(struct ast_format *, generated, NULL, ao2_cleanup);
  796. switch (cmd) {
  797. case TEST_INIT:
  798. info->name = __PRETTY_FUNCTION__;
  799. info->category = TEST_CATEGORY;
  800. info->summary = "Format sdp parse unit test";
  801. info->description =
  802. "Test that sdp parsing on a format without an interface fails";
  803. return AST_TEST_NOT_RUN;
  804. case TEST_EXECUTE:
  805. break;
  806. }
  807. codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
  808. if (!codec) {
  809. ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
  810. return AST_TEST_FAIL;
  811. }
  812. format = ast_format_create(codec);
  813. if (!format) {
  814. ast_test_status_update(test, "Could not create format using built-in codec\n");
  815. return AST_TEST_FAIL;
  816. }
  817. generated = ast_format_parse_sdp_fmtp(format, "tacos");
  818. if (generated != format) {
  819. ast_test_status_update(test, "Successfully parsed SDP on a format without an interface\n");
  820. return AST_TEST_FAIL;
  821. }
  822. return AST_TEST_PASS;
  823. }
  824. AST_TEST_DEFINE(format_parse_and_generate_sdp_fmtp)
  825. {
  826. RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
  827. RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
  828. RAII_VAR(struct ast_format *, generated, NULL, ao2_cleanup);
  829. struct ast_str *fmtp = ast_str_alloca(64);
  830. switch (cmd) {
  831. case TEST_INIT:
  832. info->name = __PRETTY_FUNCTION__;
  833. info->category = TEST_CATEGORY;
  834. info->summary = "Format sdp parse/generate unit test";
  835. info->description =
  836. "Test that sdp parsing and generation on a format with an interface succeeds";
  837. return AST_TEST_NOT_RUN;
  838. case TEST_EXECUTE:
  839. break;
  840. }
  841. codec = ast_codec_get("test_core_format_codec", AST_MEDIA_TYPE_AUDIO, 8000);
  842. if (!codec) {
  843. ast_test_status_update(test, "Could not retrieve test_core_format_codec codec\n");
  844. return AST_TEST_FAIL;
  845. }
  846. format = ast_format_create(codec);
  847. if (!format) {
  848. ast_test_status_update(test, "Could not create format using test_core_format_codec codec\n");
  849. return AST_TEST_FAIL;
  850. }
  851. generated = ast_format_parse_sdp_fmtp(format, "one=1000;two=256");
  852. if (format == generated) {
  853. ast_test_status_update(test, "Failed to parse SDP on a format without an interface\n");
  854. return AST_TEST_FAIL;
  855. }
  856. ast_format_generate_sdp_fmtp(generated, 8, &fmtp);
  857. ast_test_validate(test, strcmp("a=fmtp:8 one=1000;two=256\r\n", ast_str_buffer(fmtp)) == 0);
  858. ast_test_validate(test, test_callbacks_called.format_parse_sdp_fmtp == 1);
  859. ast_test_validate(test, test_callbacks_called.format_generate_sdp_fmtp == 1);
  860. return AST_TEST_PASS;
  861. }
  862. static int test_core_format_init(struct ast_test_info *info, struct ast_test *test)
  863. {
  864. memset(&test_callbacks_called, 0, sizeof(test_callbacks_called));
  865. return 0;
  866. }
  867. static int unload_module(void)
  868. {
  869. AST_TEST_UNREGISTER(format_create);
  870. AST_TEST_UNREGISTER(format_create_attr);
  871. AST_TEST_UNREGISTER(format_retrieve_attr);
  872. AST_TEST_UNREGISTER(format_clone);
  873. AST_TEST_UNREGISTER(format_cmp_same_codec);
  874. AST_TEST_UNREGISTER(format_attr_cmp_same_codec);
  875. AST_TEST_UNREGISTER(format_cmp_different_codec);
  876. AST_TEST_UNREGISTER(format_joint_same_codec);
  877. AST_TEST_UNREGISTER(format_attr_joint_same_codec);
  878. AST_TEST_UNREGISTER(format_joint_different_codec);
  879. AST_TEST_UNREGISTER(format_copy);
  880. AST_TEST_UNREGISTER(format_attribute_set_without_interface);
  881. AST_TEST_UNREGISTER(format_attribute_get_without_interface);
  882. AST_TEST_UNREGISTER(format_parse_sdp_fmtp_without_interface);
  883. AST_TEST_UNREGISTER(format_parse_and_generate_sdp_fmtp);
  884. return 0;
  885. }
  886. static int load_module(void)
  887. {
  888. /* Test codec/format interface used by this module */
  889. if (ast_codec_register(&test_core_format_codec)) {
  890. ast_log(AST_LOG_ERROR, "Failed to register test_core_format_codec\n");
  891. return AST_MODULE_LOAD_DECLINE;
  892. }
  893. if (ast_format_interface_register("test_core_format_codec", &test_core_format_attr)) {
  894. ast_log(AST_LOG_ERROR, "Failed to register format interface for test_core_format_codec\n");
  895. return AST_MODULE_LOAD_DECLINE;
  896. }
  897. AST_TEST_REGISTER(format_create);
  898. AST_TEST_REGISTER(format_create_attr);
  899. AST_TEST_REGISTER(format_retrieve_attr);
  900. AST_TEST_REGISTER(format_clone);
  901. AST_TEST_REGISTER(format_cmp_same_codec);
  902. AST_TEST_REGISTER(format_attr_cmp_same_codec);
  903. AST_TEST_REGISTER(format_cmp_different_codec);
  904. AST_TEST_REGISTER(format_joint_same_codec);
  905. AST_TEST_REGISTER(format_attr_joint_same_codec);
  906. AST_TEST_REGISTER(format_joint_different_codec);
  907. AST_TEST_REGISTER(format_copy);
  908. AST_TEST_REGISTER(format_attribute_set_without_interface);
  909. AST_TEST_REGISTER(format_attribute_get_without_interface);
  910. AST_TEST_REGISTER(format_parse_sdp_fmtp_without_interface);
  911. AST_TEST_REGISTER(format_parse_and_generate_sdp_fmtp);
  912. ast_test_register_init(TEST_CATEGORY, &test_core_format_init);
  913. return AST_MODULE_LOAD_SUCCESS;
  914. }
  915. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core format API test module");